1/*
2* Copyright (C) 2016 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
17package android.view;
18
19import android.content.Context;
20import android.support.test.InstrumentationRegistry;
21import android.support.test.runner.AndroidJUnit4;
22import android.test.ActivityInstrumentationTestCase2;
23import android.util.DisplayMetrics;
24import android.view.PinchZoomAction;
25import android.view.ScaleGesture;
26import android.view.WindowManager;
27import android.widget.TextView;
28
29import com.android.frameworks.coretests.R;
30
31import org.junit.After;
32import org.junit.Before;
33import org.junit.Test;
34import org.junit.runner.RunWith;
35
36import static android.support.test.espresso.matcher.ViewMatchers.withId;
37import static android.support.test.espresso.Espresso.onView;
38
39public class ScaleGestureDetectorTest extends ActivityInstrumentationTestCase2<ScaleGesture> {
40    private ScaleGesture mScaleGestureActivity;
41
42    public ScaleGestureDetectorTest() {
43        super("com.android.frameworks.coretests", ScaleGesture.class);
44    }
45
46    @Before
47    public void setUp() throws Exception {
48        super.setUp();
49        injectInstrumentation(InstrumentationRegistry.getInstrumentation());
50        mScaleGestureActivity = getActivity();
51    }
52
53    @After
54    public void tearDown() throws Exception {
55        super.tearDown();
56    }
57
58    @Test
59    public void testScaleGestureDetector() {
60        // No scaling should have occurred prior to performing pinch zoom action.
61        final float initialScaleFactor = 1.0f;
62        assertEquals(initialScaleFactor, mScaleGestureActivity.getScaleFactor());
63
64        // Specify start and end coordinates, irrespective of device display size.
65        final DisplayMetrics dm = new DisplayMetrics();
66        final WindowManager wm = (WindowManager) (mScaleGestureActivity.getApplicationContext())
67                .getSystemService(Context.WINDOW_SERVICE);
68        wm.getDefaultDisplay().getMetrics(dm);
69        final int displayWidth = dm.widthPixels;
70        final int displayHeight = dm.heightPixels;
71
72        // Obtain coordinates to perform pinch and zoom from the center, to 75% of the display.
73        final int centerX = displayWidth / 2;
74        final int centerY = displayHeight / 2;
75
76        // Offset center coordinates by one, so that the two starting points are different.
77        final float[] firstFingerStartCoords = new float[] {centerX + 1.0f, centerY - 1.0f};
78        final float[] firstFingerEndCoords =
79        new float[] {0.75f * displayWidth, 0.25f * displayHeight};
80        final float[] secondFingerStartCoords = new float[] {centerX - 1.0f, centerY + 1.0f};
81        final float[] secondFingerEndCoords =
82        new float[] {0.25f * displayWidth, 0.75f * displayHeight};
83
84        onView(withId(R.id.article)).perform(new PinchZoomAction(firstFingerStartCoords,
85                firstFingerEndCoords, secondFingerStartCoords, secondFingerEndCoords,
86                TextView.class));
87
88        // Text should have been 'zoomed', meaning scale factor increased.
89        assertTrue(mScaleGestureActivity.getScaleFactor() > initialScaleFactor);
90    }
91}