1 /*
2  * Copyright 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.testutils;
18 
19 import android.annotation.SuppressLint;
20 import android.view.View;
21 
22 import androidx.annotation.NonNull;
23 import androidx.test.espresso.action.CoordinatesProvider;
24 
25 /**
26  * Translates a {@link CoordinatesProvider} by the given x and y distances. The distances are given
27  * in pixels. Common providers to start with can be found in
28  * {@link androidx.test.espresso.action.GeneralLocation GeneralLocation}.
29  */
30 public class TranslatedCoordinatesProvider implements CoordinatesProvider {
31     private CoordinatesProvider mProvider;
32     private float mDx;
33     private float mDy;
34 
35     /**
36      * Creates an instance of {@link TranslatedCoordinatesProvider}
37      *
38      * @param coordinatesProvider the {@link CoordinatesProvider} to translate
39      * @param dx the distance in x direction
40      * @param dy the distance in y direction
41      */
42     @SuppressLint("LambdaLast")
TranslatedCoordinatesProvider(@onNull CoordinatesProvider coordinatesProvider, float dx, float dy)43     public TranslatedCoordinatesProvider(@NonNull CoordinatesProvider coordinatesProvider, float dx,
44             float dy) {
45         mProvider = coordinatesProvider;
46         mDx = dx;
47         mDy = dy;
48     }
49 
50     @NonNull
51     @Override
calculateCoordinates(@onNull View view)52     public float[] calculateCoordinates(@NonNull View view) {
53         float[] coords = mProvider.calculateCoordinates(view);
54         coords[0] += mDx;
55         coords[1] += mDy;
56         return coords;
57     }
58 }
59