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 private 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 TEST_PREFIX = "TileTest_"; 37 38 // Time between checks for logs we expect. 39 private static final long CHECK_DELAY = 500; 40 // Number of times to check before failing. 41 private static final long CHECK_RETRIES = 30; 42 43 private final String mService; 44 private final String mComponent; 45 BaseTileServiceTest()46 public BaseTileServiceTest() { 47 this(""); 48 } 49 BaseTileServiceTest(String service)50 public BaseTileServiceTest(String service) { 51 mService = service; 52 mComponent = PACKAGE + "/." + mService; 53 } 54 55 @Override setUp()56 protected void setUp() throws Exception { 57 super.setUp(); 58 59 clearLogcat(); 60 } 61 62 @Override tearDown()63 protected void tearDown() throws Exception { 64 super.tearDown(); 65 66 if (!supportedHardware()) return; 67 collapse(); 68 remTile(); 69 // Try to wait for a onTileRemoved. 70 waitFor("onTileRemoved"); 71 } 72 showDialog()73 protected void showDialog() throws Exception { 74 execute(SHOW_DIALOG); 75 } 76 addTile()77 protected void addTile() throws Exception { 78 execute(ADD_TILE + mComponent); 79 } 80 remTile()81 protected void remTile() throws Exception { 82 execute(REM_TILE + mComponent); 83 } 84 clickTile()85 protected void clickTile() throws Exception { 86 execute(CLICK_TILE + mComponent); 87 } 88 openNotifications()89 protected void openNotifications() throws Exception { 90 execute(OPEN_NOTIFICATIONS); 91 } 92 openSettings()93 protected void openSettings() throws Exception { 94 execute(OPEN_SETTINGS); 95 } 96 collapse()97 protected void collapse() throws Exception { 98 execute(COLLAPSE); 99 } 100 execute(String cmd)101 private void execute(String cmd) throws Exception { 102 getDevice().executeShellCommand(cmd); 103 // All of the status bar commands tend to have animations associated 104 // everything seems to be happier if you give them time to finish. 105 Thread.sleep(100); 106 } 107 waitFor(String str)108 protected boolean waitFor(String str) throws DeviceNotAvailableException, InterruptedException { 109 final String searchStr = TEST_PREFIX + str; 110 int ct = 0; 111 while (!hasLog(searchStr) && (ct++ < CHECK_RETRIES)) { 112 Thread.sleep(CHECK_DELAY); 113 } 114 return hasLog(searchStr); 115 } 116 hasLog(String str)117 protected boolean hasLog(String str) throws DeviceNotAvailableException { 118 String logs = getDevice().executeAdbCommand("logcat", "-v", "brief", "-d", mService + ":I", 119 "*:S"); 120 return logs.contains(str); 121 } 122 clearLogcat()123 private void clearLogcat() throws DeviceNotAvailableException { 124 getDevice().executeAdbCommand("logcat", "-c"); 125 } 126 supportedHardware()127 protected boolean supportedHardware() throws DeviceNotAvailableException { 128 String features = getDevice().executeShellCommand("pm list features"); 129 return !features.contains("android.hardware.type.television") && 130 !features.contains("android.hardware.type.watch"); 131 } 132 } 133