• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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.cts.tradefed.testtype;
18 
19 import com.android.cts.tradefed.build.CtsBuildHelper;
20 import com.android.cts.tradefed.targetprep.SettingsToggler;
21 import com.android.tradefed.build.IBuildInfo;
22 import com.android.tradefed.device.DeviceNotAvailableException;
23 import com.android.tradefed.device.ITestDevice;
24 import com.android.tradefed.result.ITestInvocationListener;
25 import com.android.tradefed.util.FileUtil;
26 
27 import junit.framework.TestCase;
28 
29 import java.io.File;
30 
31 /**
32  * Running the accessibility tests requires modification of secure
33  * settings. Secure settings cannot be changed from device CTS tests
34  * since system signature permission is required. Such settings can
35  * be modified by the shell user, so a host side test is used for
36  * installing a package with some accessibility services, enabling
37  * these services, running the tests, disabling the services, and removing
38  * the accessibility services package.
39  */
40 public class AccessibilityTestRunner extends InstrumentationApkTest {
41 
42     private static final String SOME_ACCESSIBLITY_SERVICES_PACKAGE_NAME =
43         "android.view.accessibility.services";
44 
45     private static final String SPEAKING_ACCESSIBLITY_SERVICE_NAME =
46         "android.view.accessibility.services.SpeakingAccessibilityService";
47 
48     private static final String VIBRATING_ACCESSIBLITY_SERVICE_NAME =
49         "android.view.accessibility.services.VibratingAccessibilityService";
50 
51     private static final String SOME_ACCESSIBLITY_SERVICES_APK =
52         "CtsSomeAccessibilityServices.apk";
53 
54     private CtsBuildHelper mCtsBuild;
55 
56     @Override
setBuild(IBuildInfo build)57     public void setBuild(IBuildInfo build) {
58         super.setBuild(build);
59         mCtsBuild  = CtsBuildHelper.createBuildHelper(build);
60     }
61 
62     @Override
run(ITestInvocationListener listener)63     public void run(ITestInvocationListener listener) throws DeviceNotAvailableException {
64         beforeTest();
65         super.run(listener);
66         afterTest();
67     }
68 
beforeTest()69     private void beforeTest() throws DeviceNotAvailableException {
70         installApkAndAssert(SOME_ACCESSIBLITY_SERVICES_APK);
71         enableAccessibilityAndServicesAndTouchExploration();
72     }
73 
afterTest()74     private void afterTest() throws DeviceNotAvailableException {
75         disableAccessibilityAndServicesAndTouchExploration(getDevice());
76         uninstallAndAssert(SOME_ACCESSIBLITY_SERVICES_PACKAGE_NAME);
77     }
78 
installApkAndAssert(String apkName)79     private void installApkAndAssert(String apkName) throws DeviceNotAvailableException {
80         File file = FileUtil.getFileForPath(mCtsBuild.getTestCasesDir(), apkName);
81         String errorMessage = getDevice().installPackage(file, true);
82         TestCase.assertNull("Error installing: " + apkName, errorMessage);
83     }
84 
uninstallAndAssert(String packageName)85     private void uninstallAndAssert(String packageName) throws DeviceNotAvailableException {
86         String errorMessage = getDevice().uninstallPackage(packageName);
87         TestCase.assertNull("Error uninstalling: " + packageName, errorMessage);
88     }
89 
enableAccessibilityAndServicesAndTouchExploration()90     private void enableAccessibilityAndServicesAndTouchExploration()
91             throws DeviceNotAvailableException {
92         String enabledServicesValue =
93               SOME_ACCESSIBLITY_SERVICES_PACKAGE_NAME + "/" + SPEAKING_ACCESSIBLITY_SERVICE_NAME
94             + ":"
95             + SOME_ACCESSIBLITY_SERVICES_PACKAGE_NAME + "/" + VIBRATING_ACCESSIBLITY_SERVICE_NAME;
96         enableAccessibilityAndServicesAndTouchExploration(getDevice(), enabledServicesValue);
97     }
98 
enableAccessibilityAndServicesAndTouchExploration(ITestDevice device, String value)99     static void enableAccessibilityAndServicesAndTouchExploration(ITestDevice device, String value)
100             throws DeviceNotAvailableException {
101         SettingsToggler.setSecureString(device, "enabled_accessibility_services", value);
102         SettingsToggler.setSecureInt(device, "accessibility_enabled", 1);
103         SettingsToggler.setSecureInt(device, "touch_exploration_enabled", 1);
104     }
105 
disableAccessibilityAndServicesAndTouchExploration(ITestDevice device)106     static void disableAccessibilityAndServicesAndTouchExploration(ITestDevice device)
107             throws DeviceNotAvailableException {
108         SettingsToggler.updateSecureString(device, "enabled_accessibility_services", "");
109         SettingsToggler.updateSecureInt(device, "accessibility_enabled", 0);
110         SettingsToggler.updateSecureInt(device, "touch_exploration_enabled", 0);
111     }
112 }
113