1 /*
2  * Copyright (C) 2020 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 com.android.server.accessibility.magnification;
18 
19 import static android.view.MotionEvent.ACTION_CANCEL;
20 import static android.view.MotionEvent.ACTION_DOWN;
21 import static android.view.MotionEvent.ACTION_UP;
22 
23 import static org.mockito.ArgumentMatchers.eq;
24 import static org.mockito.Mockito.verify;
25 import static org.testng.AssertJUnit.assertTrue;
26 
27 import android.annotation.NonNull;
28 import android.provider.Settings;
29 import android.view.InputDevice;
30 import android.view.MotionEvent;
31 
32 import androidx.test.runner.AndroidJUnit4;
33 
34 import com.android.server.accessibility.AccessibilityTraceManager;
35 
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
41 
42 /**
43  * Tests for {@link MagnificationGestureHandler}.
44  */
45 @RunWith(AndroidJUnit4.class)
46 public class MagnificationGestureHandlerTest {
47 
48     private TestMagnificationGestureHandler mMgh;
49     private static final int DISPLAY_0 = 0;
50     private static final int FULLSCREEN_MODE =
51             Settings.Secure.ACCESSIBILITY_MAGNIFICATION_MODE_FULLSCREEN;
52 
53     @Mock
54     AccessibilityTraceManager mTraceManager;
55     @Mock
56     MagnificationGestureHandler.Callback mCallback;
57 
58     @Before
setUp()59     public void setUp() {
60         MockitoAnnotations.initMocks(this);
61         mMgh = new TestMagnificationGestureHandler(DISPLAY_0,
62                 /* detectSingleFingerTripleTap= */true,
63                 /* detectTwoFingerTripleTap= */true,
64                 /* detectShortcutTrigger= */true,
65                 mTraceManager,
66                 mCallback);
67     }
68 
69     @Test
onMotionEvent_isFromScreen_onMotionEventInternal()70     public void onMotionEvent_isFromScreen_onMotionEventInternal() {
71         final MotionEvent downEvent = MotionEvent.obtain(0, 0, ACTION_DOWN, 0, 0, 0);
72         downEvent.setSource(InputDevice.SOURCE_TOUCHSCREEN);
73 
74         mMgh.onMotionEvent(downEvent, downEvent, /* policyFlags= */ 0);
75 
76         try {
77             assertTrue(mMgh.mIsInternalMethodCalled);
78         } finally {
79             downEvent.recycle();
80         }
81     }
82 
83     @Test
onMotionEvent_downEvent_handleInteractionStart()84     public void onMotionEvent_downEvent_handleInteractionStart() {
85         final MotionEvent downEvent = MotionEvent.obtain(0, 0, ACTION_DOWN, 0, 0, 0);
86         downEvent.setSource(InputDevice.SOURCE_TOUCHSCREEN);
87 
88         mMgh.onMotionEvent(downEvent, downEvent, /* policyFlags= */ 0);
89 
90         try {
91             verify(mCallback).onTouchInteractionStart(eq(DISPLAY_0), eq(mMgh.getMode()));
92         } finally {
93             downEvent.recycle();
94         }
95     }
96 
97     @Test
onMotionEvent_upEvent_handleInteractionEnd()98     public void onMotionEvent_upEvent_handleInteractionEnd() {
99         final MotionEvent upEvent = MotionEvent.obtain(0, 0, ACTION_UP, 0, 0, 0);
100         upEvent.setSource(InputDevice.SOURCE_TOUCHSCREEN);
101 
102         mMgh.onMotionEvent(upEvent, upEvent, /* policyFlags= */ 0);
103 
104         try {
105             verify(mCallback).onTouchInteractionEnd(eq(DISPLAY_0), eq(mMgh.getMode()));
106         } finally {
107             upEvent.recycle();
108         }
109     }
110 
111     @Test
onMotionEvent_cancelEvent_handleInteractionEnd()112     public void onMotionEvent_cancelEvent_handleInteractionEnd() {
113         final MotionEvent cancelEvent = MotionEvent.obtain(0, 0, ACTION_CANCEL, 0, 0, 0);
114         cancelEvent.setSource(InputDevice.SOURCE_TOUCHSCREEN);
115 
116         mMgh.onMotionEvent(cancelEvent, cancelEvent, /* policyFlags= */ 0);
117 
118         try {
119             verify(mCallback).onTouchInteractionEnd(eq(DISPLAY_0), eq(mMgh.getMode()));
120         } finally {
121             cancelEvent.recycle();
122         }
123     }
124 
125     private static class TestMagnificationGestureHandler extends MagnificationGestureHandler {
126 
127         boolean mIsInternalMethodCalled = false;
128 
TestMagnificationGestureHandler(int displayId, boolean detectSingleFingerTripleTap, boolean detectTwoFingerTripleTap, boolean detectShortcutTrigger, @NonNull AccessibilityTraceManager trace, @NonNull Callback callback)129         TestMagnificationGestureHandler(int displayId, boolean detectSingleFingerTripleTap,
130                 boolean detectTwoFingerTripleTap,
131                 boolean detectShortcutTrigger, @NonNull AccessibilityTraceManager trace,
132                 @NonNull Callback callback) {
133             super(displayId, detectSingleFingerTripleTap, detectTwoFingerTripleTap,
134                     detectShortcutTrigger, trace, callback);
135         }
136 
137         @Override
onMotionEventInternal(MotionEvent event, MotionEvent rawEvent, int policyFlags)138         void onMotionEventInternal(MotionEvent event, MotionEvent rawEvent, int policyFlags) {
139             mIsInternalMethodCalled = true;
140         }
141 
142         @Override
notifyShortcutTriggered()143         public void notifyShortcutTriggered() {
144             super.notifyShortcutTriggered();
145         }
146 
147         @Override
handleShortcutTriggered()148         public void handleShortcutTriggered() {
149         }
150 
151         @Override
getMode()152         public int getMode() {
153             return FULLSCREEN_MODE;
154         }
155     }
156 }
157