1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15 package android.host.systemui; 16 17 import com.android.tradefed.device.DeviceNotAvailableException; 18 import com.android.tradefed.testtype.DeviceTestCase; 19 20 public class BaseTileServiceTest extends DeviceTestCase { 21 // Constants for generating commands below. 22 protected static final String PACKAGE = "android.systemui.cts"; 23 private static final String ACTION_SHOW_DIALOG = "android.sysui.testtile.action.SHOW_DIALOG"; 24 25 // Commands used on the device. 26 private static final String ADD_TILE = "cmd statusbar add-tile "; 27 private static final String REM_TILE = "cmd statusbar remove-tile "; 28 private static final String CLICK_TILE = "cmd statusbar click-tile "; 29 30 private static final String OPEN_NOTIFICATIONS = "cmd statusbar expand-notifications"; 31 private static final String OPEN_SETTINGS = "cmd statusbar expand-settings"; 32 private static final String COLLAPSE = "cmd statusbar collapse"; 33 34 private static final String SHOW_DIALOG = "am broadcast -a " + ACTION_SHOW_DIALOG; 35 36 public static final String REQUEST_SUPPORTED = "cmd statusbar check-support"; 37 public static final String TEST_PREFIX = "TileTest_"; 38 39 // Time between checks for logs we expect. 40 private static final long CHECK_DELAY = 500; 41 // Number of times to check before failing. 42 private static final long CHECK_RETRIES = 30; 43 44 private final String mService; 45 private final String mComponent; 46 BaseTileServiceTest()47 public BaseTileServiceTest() { 48 this(""); 49 } 50 BaseTileServiceTest(String service)51 public BaseTileServiceTest(String service) { 52 mService = service; 53 mComponent = PACKAGE + "/." + mService; 54 } 55 56 @Override setUp()57 protected void setUp() throws Exception { 58 super.setUp(); 59 60 clearLogcat(); 61 } 62 63 @Override tearDown()64 protected void tearDown() throws Exception { 65 super.tearDown(); 66 67 if (!supported()) return; 68 collapse(); 69 remTile(); 70 // Try to wait for a onTileRemoved. 71 waitFor("onTileRemoved"); 72 } 73 showDialog()74 protected void showDialog() throws Exception { 75 execute(SHOW_DIALOG); 76 } 77 addTile()78 protected void addTile() throws Exception { 79 execute(ADD_TILE + mComponent); 80 } 81 remTile()82 protected void remTile() throws Exception { 83 execute(REM_TILE + mComponent); 84 } 85 clickTile()86 protected void clickTile() throws Exception { 87 execute(CLICK_TILE + mComponent); 88 } 89 openNotifications()90 protected void openNotifications() throws Exception { 91 execute(OPEN_NOTIFICATIONS); 92 } 93 openSettings()94 protected void openSettings() throws Exception { 95 execute(OPEN_SETTINGS); 96 } 97 collapse()98 protected void collapse() throws Exception { 99 execute(COLLAPSE); 100 } 101 execute(String cmd)102 private void execute(String cmd) throws Exception { 103 getDevice().executeShellCommand(cmd); 104 // All of the status bar commands tend to have animations associated 105 // everything seems to be happier if you give them time to finish. 106 Thread.sleep(100); 107 } 108 waitFor(String str)109 protected boolean waitFor(String str) throws DeviceNotAvailableException, InterruptedException { 110 final String searchStr = TEST_PREFIX + str; 111 int ct = 0; 112 while (!hasLog(searchStr) && (ct++ < CHECK_RETRIES)) { 113 Thread.sleep(CHECK_DELAY); 114 } 115 return hasLog(searchStr); 116 } 117 hasLog(String str)118 protected boolean hasLog(String str) throws DeviceNotAvailableException { 119 String logs = getDevice().executeAdbCommand("logcat", "-v", "brief", "-d", mService + ":I", 120 "*:S"); 121 return logs.contains(str); 122 } 123 clearLogcat()124 private void clearLogcat() throws DeviceNotAvailableException { 125 getDevice().executeAdbCommand("logcat", "-c"); 126 } 127 supported()128 protected boolean supported() throws DeviceNotAvailableException { 129 return supportedHardware() && supportedSoftware(); 130 } 131 supportedSoftware()132 private boolean supportedSoftware() throws DeviceNotAvailableException { 133 String supported = getDevice().executeShellCommand(REQUEST_SUPPORTED); 134 return Boolean.parseBoolean(supported); 135 } 136 supportedHardware()137 private boolean supportedHardware() throws DeviceNotAvailableException { 138 String features = getDevice().executeShellCommand("pm list features"); 139 return !features.contains("android.hardware.type.television") && 140 !features.contains("android.hardware.type.watch"); 141 } 142 } 143