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 static android.accessibilityservice.cts.utils.ActivityLaunchUtils.homeScreenOrBust;
20 
21 import static org.junit.Assert.assertTrue;
22 
23 import android.accessibility.cts.common.AccessibilityDumpOnFailureRule;
24 import android.accessibilityservice.AccessibilityService;
25 import android.accessibilityservice.AccessibilityServiceInfo;
26 import android.app.Instrumentation;
27 import android.app.UiAutomation;
28 import android.platform.test.annotations.AppModeFull;
29 import android.platform.test.annotations.Presubmit;
30 
31 import androidx.test.filters.LargeTest;
32 import androidx.test.filters.MediumTest;
33 import androidx.test.platform.app.InstrumentationRegistry;
34 import androidx.test.runner.AndroidJUnit4;
35 import androidx.test.uiautomator.UiDevice;
36 
37 import com.android.compatibility.common.util.CddTest;
38 
39 import org.junit.After;
40 import org.junit.AfterClass;
41 import org.junit.BeforeClass;
42 import org.junit.Rule;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 
46 /**
47  * Test invoking the various {@link AccessibilityService#performGlobalAction(int)}} actions.
48  */
49 @Presubmit
50 @AppModeFull
51 @RunWith(AndroidJUnit4.class)
52 @CddTest(requirements = {"3.10/C-1-1,C-1-2"})
53 public class AccessibilityGlobalActionsTest {
54 
55     private static Instrumentation sInstrumentation;
56     private static UiAutomation sUiAutomation;
57     private static UiDevice sUiDevice;
58 
59     @Rule
60     public final AccessibilityDumpOnFailureRule mDumpOnFailureRule =
61             new AccessibilityDumpOnFailureRule();
62 
63     @BeforeClass
oneTimeSetup()64     public static void oneTimeSetup() {
65         sInstrumentation = InstrumentationRegistry.getInstrumentation();
66         sUiAutomation = sInstrumentation.getUiAutomation();
67         sUiDevice = UiDevice.getInstance(sInstrumentation);
68         AccessibilityServiceInfo info = sUiAutomation.getServiceInfo();
69         info.flags |= AccessibilityServiceInfo.FLAG_RETRIEVE_INTERACTIVE_WINDOWS;
70         sUiAutomation.setServiceInfo(info);
71         // Start on a clean home screen with any system dialogs removed.
72         homeScreenOrBust(sInstrumentation.getContext(), sUiAutomation);
73     }
74 
75     @AfterClass
postTestTearDown()76     public static void postTestTearDown() {
77         sUiAutomation.destroy();
78     }
79 
80     @After
tearDown()81     public void tearDown() throws Exception {
82         // The majority of system actions involve System UI requests that both:
83         //   - Can take a few seconds to take effect on certain device types.
84         //   - Perform behavior that depends on the specific SystemUI implementation of the device,
85         //     making it untestable to a device-agnostic CTS test like this.
86         // So instead of waiting for any specific condition, we repeatedly try to get to the home
87         // screen to clean up before starting the next test.
88         sUiDevice.pressHome();
89     }
90 
91     @MediumTest
92     @Test
testPerformGlobalActionBack()93     public void testPerformGlobalActionBack() {
94         assertTrue(sUiAutomation.performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK));
95     }
96 
97     @MediumTest
98     @Test
testPerformGlobalActionHome()99     public void testPerformGlobalActionHome() {
100         assertTrue(sUiAutomation.performGlobalAction(AccessibilityService.GLOBAL_ACTION_HOME));
101     }
102 
103     @LargeTest
104     @Test
testPerformGlobalActionRecents()105     public void testPerformGlobalActionRecents() {
106         // Not all devices support GLOBAL_ACTION_RECENTS, but there is no current feature flag for
107         // this. Our best hope is to test that this does throw a runtime error.
108         sUiAutomation.performGlobalAction(
109                 AccessibilityService.GLOBAL_ACTION_RECENTS);
110     }
111 
112     @MediumTest
113     @Test
testPerformGlobalActionNotifications()114     public void testPerformGlobalActionNotifications() {
115         assertTrue(sUiAutomation.performGlobalAction(
116                 AccessibilityService.GLOBAL_ACTION_NOTIFICATIONS));
117     }
118 
119     @MediumTest
120     @Test
testPerformGlobalActionQuickSettings()121     public void testPerformGlobalActionQuickSettings() {
122         assertTrue(sUiAutomation.performGlobalAction(
123                 AccessibilityService.GLOBAL_ACTION_QUICK_SETTINGS));
124     }
125 
126     @MediumTest
127     @Test
testPerformGlobalActionPowerDialog()128     public void testPerformGlobalActionPowerDialog() {
129         assertTrue(sUiAutomation.performGlobalAction(
130                 AccessibilityService.GLOBAL_ACTION_POWER_DIALOG));
131     }
132 
133     @LargeTest
134     @Test
testPerformActionScreenshot()135     public void testPerformActionScreenshot() {
136         assertTrue(sUiAutomation.performGlobalAction(
137                 AccessibilityService.GLOBAL_ACTION_TAKE_SCREENSHOT));
138         // Ideally should verify that we actually have a screenshot, but it's also possible
139         // for the screenshot to fail.
140     }
141 
142     @MediumTest
143     @Test
testPerformGlobalActionDpadUp()144     public void testPerformGlobalActionDpadUp() {
145         assertTrue(sUiAutomation.performGlobalAction(
146                 AccessibilityService.GLOBAL_ACTION_DPAD_UP));
147     }
148 
149     @MediumTest
150     @Test
testPerformGlobalActionDpadDown()151     public void testPerformGlobalActionDpadDown() {
152         assertTrue(sUiAutomation.performGlobalAction(
153                 AccessibilityService.GLOBAL_ACTION_DPAD_DOWN));
154     }
155 
156     @MediumTest
157     @Test
testPerformGlobalActionDpadLeft()158     public void testPerformGlobalActionDpadLeft() {
159         assertTrue(sUiAutomation.performGlobalAction(
160                 AccessibilityService.GLOBAL_ACTION_DPAD_LEFT));
161     }
162 
163     @MediumTest
164     @Test
testPerformGlobalActionDpadRight()165     public void testPerformGlobalActionDpadRight() {
166         assertTrue(sUiAutomation.performGlobalAction(
167                 AccessibilityService.GLOBAL_ACTION_DPAD_RIGHT));
168     }
169 
170     @MediumTest
171     @Test
testPerformGlobalActionDpadCenter()172     public void testPerformGlobalActionDpadCenter() {
173         assertTrue(sUiAutomation.performGlobalAction(
174                 AccessibilityService.GLOBAL_ACTION_DPAD_CENTER));
175     }
176 }
177