1 /* 2 * Copyright (C) 2018 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 package android.angle.cts; 17 18 import com.android.tradefed.device.ITestDevice; 19 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test; 20 21 import java.util.HashMap; 22 import java.util.Map; 23 24 class CtsAngleCommon { 25 private static final int TEST_WAIT_TIME_MS = 1000; 26 27 // Settings.Global 28 static final String SETTINGS_GLOBAL_ALL_USE_ANGLE = "angle_gl_driver_all_angle"; 29 static final String SETTINGS_GLOBAL_DRIVER_PKGS = "angle_gl_driver_selection_pkgs"; 30 static final String SETTINGS_GLOBAL_DRIVER_VALUES = "angle_gl_driver_selection_values"; 31 static final String SETTINGS_GLOBAL_WHITELIST = "angle_whitelist"; 32 static final String SETTINGS_GLOBAL_ANGLE_IN_USE_DIALOG_BOX = "show_angle_in_use_dialog_box"; 33 34 // System Properties 35 static final String PROPERTY_GFX_ANGLE_SUPPORTED = "ro.gfx.angle.supported"; 36 static final String PROPERTY_TEMP_RULES_FILE = "debug.angle.rules"; 37 38 // Rules File 39 static final String DEVICE_TEMP_RULES_FILE_DIRECTORY = "/data/local/tmp"; 40 static final String DEVICE_TEMP_RULES_FILE_FILENAME = "a4a_rules.json"; 41 static final String DEVICE_TEMP_RULES_FILE_PATH = DEVICE_TEMP_RULES_FILE_DIRECTORY + "/" + DEVICE_TEMP_RULES_FILE_FILENAME; 42 43 // ANGLE 44 static final String ANGLE_DRIVER_TEST_PKG = "com.android.angleIntegrationTest.driverTest"; 45 static final String ANGLE_DRIVER_TEST_SEC_PKG = "com.android.angleIntegrationTest.driverTestSecondary"; 46 static final String ANGLE_DRIVER_TEST_CLASS = "AngleDriverTestActivity"; 47 static final String ANGLE_DRIVER_TEST_DEFAULT_METHOD = "testUseDefaultDriver"; 48 static final String ANGLE_DRIVER_TEST_ANGLE_METHOD = "testUseAngleDriver"; 49 static final String ANGLE_DRIVER_TEST_NATIVE_METHOD = "testUseNativeDriver"; 50 static final String ANGLE_DRIVER_TEST_APP = "CtsAngleDriverTestCases.apk"; 51 static final String ANGLE_DRIVER_TEST_SEC_APP = "CtsAngleDriverTestCasesSecondary.apk"; 52 static final String ANGLE_DRIVER_TEST_ACTIVITY = 53 ANGLE_DRIVER_TEST_PKG + "/com.android.angleIntegrationTest.common.AngleIntegrationTestActivity"; 54 static final String ANGLE_DRIVER_TEST_SEC_ACTIVITY = 55 ANGLE_DRIVER_TEST_SEC_PKG + "/com.android.angleIntegrationTest.common.AngleIntegrationTestActivity"; 56 static final String ANGLE_MAIN_ACTIVTY = "android.app.action.ANGLE_FOR_ANDROID"; 57 58 enum OpenGlDriverChoice { 59 DEFAULT, 60 NATIVE, 61 ANGLE 62 } 63 64 static final Map<OpenGlDriverChoice, String> sDriverGlobalSettingMap = buildDriverGlobalSettingMap(); buildDriverGlobalSettingMap()65 static Map<OpenGlDriverChoice, String> buildDriverGlobalSettingMap() { 66 Map<OpenGlDriverChoice, String> map = new HashMap<>(); 67 map.put(OpenGlDriverChoice.DEFAULT, "default"); 68 map.put(OpenGlDriverChoice.ANGLE, "angle"); 69 map.put(OpenGlDriverChoice.NATIVE, "native"); 70 71 return map; 72 } 73 74 static final Map<OpenGlDriverChoice, String> sDriverTestMethodMap = buildDriverTestMethodMap(); buildDriverTestMethodMap()75 static Map<OpenGlDriverChoice, String> buildDriverTestMethodMap() { 76 Map<OpenGlDriverChoice, String> map = new HashMap<>(); 77 map.put(OpenGlDriverChoice.DEFAULT, ANGLE_DRIVER_TEST_DEFAULT_METHOD); 78 map.put(OpenGlDriverChoice.ANGLE, ANGLE_DRIVER_TEST_ANGLE_METHOD); 79 map.put(OpenGlDriverChoice.NATIVE, ANGLE_DRIVER_TEST_NATIVE_METHOD); 80 81 return map; 82 } 83 getGlobalSetting(ITestDevice device, String globalSetting)84 static String getGlobalSetting(ITestDevice device, String globalSetting) throws Exception { 85 return device.getSetting("global", globalSetting); 86 } 87 setGlobalSetting(ITestDevice device, String globalSetting, String value)88 static void setGlobalSetting(ITestDevice device, String globalSetting, String value) throws Exception { 89 device.setSetting("global", globalSetting, value); 90 } 91 clearSettings(ITestDevice device)92 static void clearSettings(ITestDevice device) throws Exception { 93 setGlobalSetting(device, SETTINGS_GLOBAL_ALL_USE_ANGLE, "0"); 94 setGlobalSetting(device, SETTINGS_GLOBAL_ANGLE_IN_USE_DIALOG_BOX, "0"); 95 setGlobalSetting(device, SETTINGS_GLOBAL_DRIVER_PKGS, "\"\""); 96 setGlobalSetting(device, SETTINGS_GLOBAL_DRIVER_VALUES, "\"\""); 97 setGlobalSetting(device, SETTINGS_GLOBAL_WHITELIST, "\"\""); 98 setProperty(device, PROPERTY_TEMP_RULES_FILE, "\"\""); 99 } 100 isAngleLoadable(ITestDevice device)101 static boolean isAngleLoadable(ITestDevice device) throws Exception { 102 String angleSupported = device.getProperty(PROPERTY_GFX_ANGLE_SUPPORTED); 103 104 return (angleSupported != null) && (angleSupported.equals("true")); 105 } 106 startActivity(ITestDevice device, String action)107 static void startActivity(ITestDevice device, String action) throws Exception { 108 // Pause for a moment for the settings to propagate, with the hope that adb and the ANGLE 109 // APK Global.Settings updates are the same for everyone. 110 Thread.sleep(TEST_WAIT_TIME_MS); 111 // Run the ANGLE activity so it'll clear up any 'default' settings. 112 device.executeShellCommand("am start --user " + device.getCurrentUser() + 113 " -S -W -a \"" + action + "\""); 114 Thread.sleep(TEST_WAIT_TIME_MS); 115 } 116 stopPackage(ITestDevice device, String pkgName)117 static void stopPackage(ITestDevice device, String pkgName) throws Exception { 118 device.executeShellCommand("am force-stop " + pkgName); 119 } 120 121 /** 122 * Work around the fact that INativeDevice.enableAdbRoot() is not supported. 123 */ setProperty(ITestDevice device, String property, String value)124 static void setProperty(ITestDevice device, String property, String value) throws Exception { 125 device.executeShellCommand("setprop " + property + " " + value); 126 } 127 128 /** 129 * Wait for a bit for things to settle down before running the device tests. 130 * @param pkgName 131 * @param testClassName 132 * @param testMethodName 133 * @return 134 * @throws Exception 135 */ waitThenRunDeviceTests(BaseHostJUnit4Test test, String pkgName, String testClassName, String testMethodName)136 static boolean waitThenRunDeviceTests(BaseHostJUnit4Test test, 137 String pkgName, 138 String testClassName, 139 String testMethodName) throws Exception { 140 // Pause for a moment for the settings to propagate, with the hope that adb and the ANGLE 141 // APK Global.Settings updates are the same for everyone. 142 Thread.sleep(TEST_WAIT_TIME_MS); 143 return test.runDeviceTests(pkgName, testClassName, testMethodName); 144 } 145 } 146