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.targetprep.SettingsToggler; 20 import com.android.cts.util.AbiUtils; 21 import com.android.ddmlib.Log; 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 CtsInstrumentationApkTest { 41 42 private static final String LOG_TAG = AccessibilityTestRunner.class.getSimpleName(); 43 44 private static final String SOME_ACCESSIBLITY_SERVICES_PACKAGE_NAME = 45 "android.view.accessibility.services"; 46 47 private static final String SPEAKING_ACCESSIBLITY_SERVICE_NAME = 48 "android.view.accessibility.services.SpeakingAccessibilityService"; 49 50 private static final String VIBRATING_ACCESSIBLITY_SERVICE_NAME = 51 "android.view.accessibility.services.VibratingAccessibilityService"; 52 53 private static final String SOME_ACCESSIBLITY_SERVICES_APK = 54 "CtsSomeAccessibilityServices.apk"; 55 56 @Override 57 public void run(ITestInvocationListener listener) throws DeviceNotAvailableException { 58 beforeTest(); 59 super.run(listener); 60 afterTest(); 61 } 62 63 private void beforeTest() throws DeviceNotAvailableException { 64 installApkAndAssert(SOME_ACCESSIBLITY_SERVICES_APK); 65 enableAccessibilityAndServices(); 66 } 67 68 private void afterTest() throws DeviceNotAvailableException { 69 disableAccessibilityAndServices(getDevice()); 70 uninstallAndAssert(SOME_ACCESSIBLITY_SERVICES_PACKAGE_NAME); 71 } 72 73 private void installApkAndAssert(String apkName) throws DeviceNotAvailableException { 74 File file = FileUtil.getFileForPath(mCtsBuild.getTestCasesDir(), apkName); 75 String[] options = {AbiUtils.createAbiFlag(mAbi.getName())}; 76 String errorMessage = getDevice().installPackage(file, true, options); 77 TestCase.assertNull("Error installing: " + apkName, errorMessage); 78 } 79 80 private void uninstallAndAssert(String packageName) throws DeviceNotAvailableException { 81 String errorMessage = getDevice().uninstallPackage(packageName); 82 TestCase.assertNull("Error uninstalling: " + packageName, errorMessage); 83 } 84 85 private void enableAccessibilityAndServices() throws DeviceNotAvailableException { 86 String enabledServicesValue = 87 SOME_ACCESSIBLITY_SERVICES_PACKAGE_NAME + "/" + SPEAKING_ACCESSIBLITY_SERVICE_NAME 88 + ":" 89 + SOME_ACCESSIBLITY_SERVICES_PACKAGE_NAME + "/" + VIBRATING_ACCESSIBLITY_SERVICE_NAME; 90 enableAccessibilityAndServices(getDevice(), enabledServicesValue); 91 } 92 93 static void enableAccessibilityAndServices(ITestDevice device, String value) 94 throws DeviceNotAvailableException { 95 SettingsToggler.setSecureString(device, "enabled_accessibility_services", value); 96 SettingsToggler.setSecureString(device, 97 "touch_exploration_granted_accessibility_services", value); 98 SettingsToggler.setSecureInt(device, "accessibility_enabled", 1); 99 } 100 101 static void disableAccessibilityAndServices(ITestDevice device) 102 throws DeviceNotAvailableException { 103 SettingsToggler.updateSecureString(device, "enabled_accessibility_services", ""); 104 SettingsToggler.updateSecureString(device, 105 "touch_exploration_granted_accessibility_services", ""); 106 SettingsToggler.updateSecureInt(device, "accessibility_enabled", 0); 107 } 108 } 109