1 /* 2 * Copyright (C) 2017 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.wearable.sysapp.janktests; 18 19 import android.app.Instrumentation; 20 import android.support.test.uiautomator.By; 21 import android.support.test.uiautomator.Direction; 22 import android.support.test.uiautomator.UiDevice; 23 import android.support.test.uiautomator.UiObject2; 24 import android.support.test.uiautomator.UiObjectNotFoundException; 25 import android.support.test.uiautomator.UiScrollable; 26 import android.support.test.uiautomator.UiSelector; 27 import android.support.test.uiautomator.Until; 28 import android.util.Log; 29 30 import junit.framework.Assert; 31 32 import java.util.List; 33 import java.util.concurrent.TimeUnit; 34 35 public class WatchFaceHelper { 36 37 public static final long LONG_TIMEOUT = TimeUnit.SECONDS.toMillis(20); 38 public static final long SHORT_TIMEOUT = TimeUnit.MILLISECONDS.toMillis(500); 39 private static final String WATCHFACE_PREVIEW_NAME = "preview_image"; 40 private static final String WATCHFACE_SHOW_ALL_BTN_NAME = "show_all_btn"; 41 private static final String WATCHFACE_PICKER_ALL_LIST_NAME = "watch_face_picker_all_list"; 42 private static final String WATCHFACE_PICKER_FAVORITE_LIST_NAME = "watch_face_picker_list"; 43 private static final String PERMISSION_ALLOW_BUTTON_ID = 44 "com.android.packageinstaller:id/permission_allow_button"; 45 private static final String TAG = WatchFaceHelper.class.getSimpleName(); 46 47 private static WatchFaceHelper sWatchFaceHelperInstance; 48 private SysAppTestHelper mHelper; 49 private UiDevice mDevice; 50 private Instrumentation mInstrumentation; 51 52 /** 53 * @param mDevice Instance to represent the current device. 54 * @param instrumentation Instance for instrumentation. 55 */ 56 private WatchFaceHelper(UiDevice device, Instrumentation instrumentation) { 57 this.mDevice = device; 58 this.mHelper = SysAppTestHelper.getInstance(mDevice, instrumentation); 59 } 60 61 public static WatchFaceHelper getInstance(UiDevice device, Instrumentation instrumentation) { 62 if (sWatchFaceHelperInstance == null) { 63 sWatchFaceHelperInstance = new WatchFaceHelper(device, instrumentation); 64 } 65 return sWatchFaceHelperInstance; 66 } 67 68 /** 69 * Check if there is only one watch face in favorites list. 70 * 71 * @return True is +id/show_all_btn is on the screen. False otherwise. 72 */ 73 public boolean isOnlyOneWatchFaceInFavorites() { 74 // Make sure we are not at the last watch face. 75 mHelper.swipeRight(); 76 return mHelper.waitForSysAppUiObject2(WATCHFACE_SHOW_ALL_BTN_NAME) != null; 77 } 78 79 public void openPicker() { 80 // Try 5 times in case WFP is not ready 81 for (int i = 0; i < 5; i++) { 82 mHelper.swipeRight(); 83 if (mHelper.waitForSysAppUiObject2(WATCHFACE_PREVIEW_NAME) != null) { 84 return; 85 } 86 } 87 Assert.fail("Still cannot open WFP after several attempts"); 88 } 89 90 public void openPickerAllList() { 91 // Assume the screen is on WatchFace picker for favorites. 92 // Swipe to the end of the list. 93 while (mHelper.waitForSysAppUiObject2(WATCHFACE_SHOW_ALL_BTN_NAME) == null) { 94 Log.v(TAG, "Swiping to the end of favorites list ..."); 95 mHelper.flingLeft(); 96 } 97 98 UiObject2 showAllButton = mHelper.waitForSysAppUiObject2(WATCHFACE_SHOW_ALL_BTN_NAME); 99 Assert.assertNotNull(showAllButton); 100 Log.v(TAG, "Tapping to show all watchfaces ..."); 101 showAllButton.click(); 102 UiObject2 watchFacePickerAllList = 103 mHelper.waitForSysAppUiObject2(WATCHFACE_PICKER_ALL_LIST_NAME); 104 Assert.assertNotNull(watchFacePickerAllList); 105 } 106 107 public void selectWatchFaceFromFullList(int index) { 108 // Assume the screen is on watch face picker for all faces. 109 UiObject2 watchFacePickerAllList = mHelper.waitForSysAppUiObject2( 110 WATCHFACE_PICKER_ALL_LIST_NAME); 111 Assert.assertNotNull(watchFacePickerAllList); 112 int swipes = index / 4; // Showing 4 for each scroll. 113 for (int i = 0; i < swipes; ++i) { 114 mHelper.swipeDown(); 115 } 116 List<UiObject2> watchFaces = watchFacePickerAllList.getChildren(); 117 Assert.assertNotNull(watchFaces); 118 int localIndex = index % 4; 119 if (watchFaces.size() <= localIndex) { 120 mDevice.pressBack(); 121 return; 122 } 123 124 Log.v(TAG, "Tapping the " + localIndex + " watchface on screen ..."); 125 watchFaces.get(localIndex).click(); 126 // Verify the watchface is selected properly. 127 UiObject2 watchFacePickerList = 128 mHelper.waitForSysAppUiObject2(WATCHFACE_PICKER_FAVORITE_LIST_NAME); 129 Assert.assertNotNull(watchFacePickerList); 130 131 dismissPermissionDialogs(); 132 } 133 134 public void selectWatchFaceFromFullList(String watchFaceName) throws UiObjectNotFoundException { 135 UiScrollable watchFacePickerAllList = new UiScrollable(new UiSelector().scrollable(true)); 136 Assert.assertNotNull(watchFacePickerAllList); 137 watchFacePickerAllList.scrollTextIntoView(watchFaceName); 138 139 // Select from full list 140 UiObject2 watchFace = mDevice.wait(Until.findObject(By.text(watchFaceName)), SHORT_TIMEOUT); 141 Assert.assertNotNull(watchFace); 142 watchFace.click(); 143 144 // Select from favorites list 145 watchFace = mDevice.wait(Until.findObject(By.desc(watchFaceName)), SHORT_TIMEOUT); 146 Assert.assertNotNull(watchFace); 147 watchFace.click(); 148 149 // Dismiss all permission dialogs 150 dismissPermissionDialogs(); 151 } 152 153 public void startFromWatchFacePicker() { 154 Log.v(TAG, "Starting from watchface picker ..."); 155 mHelper.goBackHome(); 156 openPicker(); 157 } 158 159 public void removeAllButOneWatchFace() { 160 int count = 0; 161 do { 162 Log.v(TAG, "Removing all but one watch faces ..."); 163 startFromWatchFacePicker(); 164 removeWatchFaceFromFavorites(); 165 } while (isOnlyOneWatchFaceInFavorites() && count++ < 5); 166 } 167 168 public void removeWatchFaceFromFavorites() { 169 mHelper.flingRight(); 170 171 // Assume the favorites list has at least 2 watch faces. 172 UiObject2 watchFacePicker = 173 mHelper.waitForSysAppUiObject2(WATCHFACE_PICKER_FAVORITE_LIST_NAME); 174 Assert.assertNotNull(watchFacePicker); 175 List<UiObject2> watchFaces = watchFacePicker.getChildren(); 176 Assert.assertNotNull(watchFaces); 177 if (isOnlyOneWatchFaceInFavorites()) { 178 return; 179 } 180 181 Log.v(TAG, "Removing first watch face from favorites ..."); 182 watchFaces.get(0).swipe(Direction.DOWN, 1.0f); 183 } 184 185 private void dismissPermissionDialogs() { 186 UiObject2 permissionDialog = null; 187 do { 188 permissionDialog = mDevice.wait( 189 Until.findObject(By.res(PERMISSION_ALLOW_BUTTON_ID)), 190 SHORT_TIMEOUT); 191 192 if (permissionDialog != null) { 193 permissionDialog.click(); 194 } 195 } while (permissionDialog != null); 196 } 197 } 198