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