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 17 package android.accessibilityservice.cts; 18 19 import android.accessibilityservice.AccessibilityService; 20 import android.os.SystemClock; 21 import android.platform.test.annotations.AppModeFull; 22 import android.platform.test.annotations.Presubmit; 23 import android.test.InstrumentationTestCase; 24 import android.test.suitebuilder.annotation.MediumTest; 25 26 import java.util.concurrent.TimeoutException; 27 28 /** 29 * Test global actions 30 */ 31 @Presubmit 32 @AppModeFull 33 public class AccessibilityGlobalActionsTest extends InstrumentationTestCase { 34 /** 35 * Timeout required for pending Binder calls or event processing to 36 * complete. 37 */ 38 private static final long TIMEOUT_ASYNC_PROCESSING = 5000; 39 40 /** 41 * The timeout since the last accessibility event to consider the device idle. 42 */ 43 private static final long TIMEOUT_ACCESSIBILITY_STATE_IDLE = 500; 44 45 @MediumTest 46 public void testPerformGlobalActionBack() throws Exception { 47 assertTrue(getInstrumentation().getUiAutomation().performGlobalAction( 48 AccessibilityService.GLOBAL_ACTION_BACK)); 49 50 // Sleep a bit so the UI is settled. 51 waitForIdle(); 52 } 53 54 @MediumTest 55 public void testPerformGlobalActionHome() throws Exception { 56 assertTrue(getInstrumentation().getUiAutomation().performGlobalAction( 57 AccessibilityService.GLOBAL_ACTION_HOME)); 58 59 // Sleep a bit so the UI is settled. 60 waitForIdle(); 61 } 62 63 @MediumTest 64 public void testPerformGlobalActionRecents() throws Exception { 65 // Perform the action. 66 boolean actionWasPerformed = 67 getInstrumentation().getUiAutomation().performGlobalAction( 68 AccessibilityService.GLOBAL_ACTION_RECENTS); 69 70 // Sleep a bit so the UI is settled. 71 waitForIdle(); 72 73 if (actionWasPerformed) { 74 // This is a necessary since the back action does not 75 // dismiss recents until they stop animating. Sigh... 76 SystemClock.sleep(5000); 77 78 // Clean up. 79 getInstrumentation().getUiAutomation().performGlobalAction( 80 AccessibilityService.GLOBAL_ACTION_BACK); 81 } 82 83 // Sleep a bit so the UI is settled. 84 waitForIdle(); 85 } 86 87 @MediumTest 88 public void testPerformGlobalActionNotifications() throws Exception { 89 // Perform the action under test 90 assertTrue(getInstrumentation().getUiAutomation().performGlobalAction( 91 AccessibilityService.GLOBAL_ACTION_NOTIFICATIONS)); 92 93 // Sleep a bit so the UI is settled. 94 waitForIdle(); 95 96 // Clean up. 97 assertTrue(getInstrumentation().getUiAutomation().performGlobalAction( 98 AccessibilityService.GLOBAL_ACTION_BACK)); 99 100 // Sleep a bit so the UI is settled. 101 waitForIdle(); 102 } 103 104 @MediumTest 105 public void testPerformGlobalActionQuickSettings() throws Exception { 106 // Check whether the action succeeded. 107 assertTrue(getInstrumentation().getUiAutomation().performGlobalAction( 108 AccessibilityService.GLOBAL_ACTION_QUICK_SETTINGS)); 109 110 // Sleep a bit so the UI is settled. 111 waitForIdle(); 112 113 // Clean up. 114 // The GLOBAL_ACTION_HOME is needed in the case where additional 115 // notifications showing up, we need to clear them as well for the upcoming 116 // new tests to work properly. 117 getInstrumentation().getUiAutomation().performGlobalAction( 118 AccessibilityService.GLOBAL_ACTION_HOME); 119 120 // Sleep a bit so the UI is settled. 121 waitForIdle(); 122 } 123 124 @MediumTest 125 public void testPerformGlobalActionPowerDialog() throws Exception { 126 // Check whether the action succeeded. 127 assertTrue(getInstrumentation().getUiAutomation().performGlobalAction( 128 AccessibilityService.GLOBAL_ACTION_POWER_DIALOG)); 129 130 // Sleep a bit so the UI is settled. 131 waitForIdle(); 132 133 // Clean up. 134 getInstrumentation().getUiAutomation().performGlobalAction( 135 AccessibilityService.GLOBAL_ACTION_BACK); 136 137 // Sleep a bit so the UI is settled. 138 waitForIdle(); 139 } 140 141 @MediumTest 142 public void testPerformActionScreenshot() throws Exception { 143 // Action should succeed 144 assertTrue(getInstrumentation().getUiAutomation().performGlobalAction( 145 AccessibilityService.GLOBAL_ACTION_TAKE_SCREENSHOT)); 146 // Ideally should verify that we actually have a screenshot, but it's also possible 147 // for the screenshot to fail 148 waitForIdle(); 149 } 150 151 private void waitForIdle() throws TimeoutException { 152 getInstrumentation().getUiAutomation().waitForIdle( 153 TIMEOUT_ACCESSIBILITY_STATE_IDLE, 154 TIMEOUT_ASYNC_PROCESSING); 155 } 156 } 157