1 /* 2 * Copyright (C) 2015 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.uibench.janktests; 18 19 import android.R; 20 import android.util.Log; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.os.SystemClock; 24 import android.support.test.uiautomator.By; 25 import android.support.test.uiautomator.UiDevice; 26 import android.support.test.uiautomator.UiObject2; 27 import android.support.test.uiautomator.UiObjectNotFoundException; 28 import android.support.test.uiautomator.Until; 29 30 import java.io.IOException; 31 32 import junit.framework.Assert; 33 34 /** 35 * Jank benchmark tests helper for UiBench app 36 */ 37 38 public class UiBenchJankTestsHelper { 39 public static final int LONG_TIMEOUT = 5000; 40 public static final int TIMEOUT = 250; 41 public static final int SHORT_TIMEOUT = 250; 42 public static final int INNER_LOOP = 3; 43 public static final int EXPECTED_FRAMES = 100; 44 public static final int CW_FLING_RATE = 5000; 45 46 public static final String LOG_TAG = "UiBenchJankTestsHelper"; 47 public static final String RES_PACKAGE_NAME = "android"; 48 public static final String PACKAGE_NAME = "com.android.test.uibench"; 49 public static final String ROOT_NAME = "root"; 50 public static final String LAUNCHER_VIEW_NAME = "launcher_view"; 51 public static final String TEXT_OBJECT_NAME = "text1"; 52 public static final String UIBENCH_OBJECT_NAME = "UiBench"; 53 public static final String KEYBOARD_SERVICE_NAME = 54 "com.google.android.inputmethod.latin/com.google.android.apps.inputmethod.wear.WearIME"; 55 56 private static UiBenchJankTestsHelper mInstance; 57 private UiDevice mDevice; 58 private Context mContext; 59 60 private UiBenchJankTestsHelper(UiDevice device, Context context) { 61 mDevice = device; 62 mContext = context; 63 } 64 65 public static UiBenchJankTestsHelper getInstance(UiDevice device) { 66 return new UiBenchJankTestsHelper(device, null); 67 } 68 69 public static UiBenchJankTestsHelper getInstance(UiDevice device, Context context) { 70 if (mInstance == null) { 71 mInstance = new UiBenchJankTestsHelper(device, context); 72 } 73 return mInstance; 74 } 75 76 // Launch UiBench app 77 public void launchUiBench() { 78 Intent intent = mContext.getPackageManager() 79 .getLaunchIntentForPackage(PACKAGE_NAME); 80 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 81 mContext.startActivity(intent); 82 mDevice.waitForIdle(); 83 // ensure test starts from home despite last failed test left UiBench in weird state 84 UiObject2 initScreen = mDevice.wait(Until.findObject(By.text(UIBENCH_OBJECT_NAME)), 2000); 85 int counter = 3; 86 while (initScreen == null && --counter > 0) { 87 mDevice.pressBack(); 88 initScreen = mDevice.wait(Until.findObject(By.text(UIBENCH_OBJECT_NAME)), 2000); 89 } 90 } 91 92 // Helper method to go back to home screen 93 public void goBackHome() throws UiObjectNotFoundException { 94 String launcherPackage = mDevice.getLauncherPackageName(); 95 UiObject2 homeScreen = mDevice.findObject(By.res(launcherPackage, ROOT_NAME)); 96 int count = 0; 97 while (homeScreen == null && count < 5) { 98 mDevice.pressBack(); 99 homeScreen = mDevice.findObject(By.res(launcherPackage, ROOT_NAME)); 100 count ++; 101 } 102 // Make sure we're not in the launcher 103 homeScreen = mDevice.findObject(By.res(launcherPackage, LAUNCHER_VIEW_NAME)); 104 if (homeScreen != null) { 105 mDevice.pressBack(); 106 } 107 } 108 109 public void openTextInList(String itemName) { 110 int count = 0; 111 UiObject2 component = mDevice.wait(Until.findObject( 112 By.res(RES_PACKAGE_NAME, TEXT_OBJECT_NAME).text(itemName)), TIMEOUT); 113 while (component == null && count < 5) { 114 swipeDown(); 115 component = mDevice.wait(Until.findObject( 116 By.res(RES_PACKAGE_NAME, TEXT_OBJECT_NAME).text(itemName)), TIMEOUT); 117 count ++; 118 } 119 while (component == null && count < 10) { 120 swipeUp(); 121 component = mDevice.wait(Until.findObject( 122 By.res(RES_PACKAGE_NAME, TEXT_OBJECT_NAME).text(itemName)), TIMEOUT); 123 count ++; 124 } 125 Assert.assertNotNull(itemName + ": isn't found", component); 126 component.clickAndWait(Until.newWindow(), LONG_TIMEOUT); 127 SystemClock.sleep(TIMEOUT); 128 } 129 130 public void swipeRight() { 131 mDevice.swipe(50, 132 mDevice.getDisplayHeight() / 2, 133 mDevice.getDisplayWidth() - 50, 134 mDevice.getDisplayHeight() / 2, 135 40); // slow speed 136 } 137 138 public void swipeDown() { 139 mDevice.swipe(mDevice.getDisplayWidth() / 2, 140 mDevice.getDisplayHeight() / 2 + 100, 141 mDevice.getDisplayWidth() / 2, 142 mDevice.getDisplayHeight() / 2 - 100, 143 40); // slow speed 144 } 145 146 public void swipeUp() { 147 mDevice.swipe(mDevice.getDisplayWidth() / 2, 148 mDevice.getDisplayHeight() / 2 - 50, 149 mDevice.getDisplayWidth() / 2, 150 mDevice.getDisplayHeight() / 2 + 100, 151 40); // slow speed 152 } 153 154 // Helper method to turn on/off keyboard IME 155 public void enableKeyboardIME(Boolean turnOn) { 156 try { 157 String cmd = null; 158 if (turnOn) { 159 cmd = "ime enable %s"; 160 } else { 161 cmd = "ime disable %s"; 162 } 163 mDevice.executeShellCommand(String.format(cmd, KEYBOARD_SERVICE_NAME)); 164 } catch (IOException e) { 165 Log.w(LOG_TAG, String.format("Exception when toggling keyboard. %s", e.toString())); 166 } 167 } 168 } 169