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