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