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 com.android.helpers; 17 18 import android.app.KeyguardManager; 19 import android.content.Context; 20 import android.os.SystemClock; 21 import android.hardware.display.DisplayManager; 22 import android.support.test.uiautomator.UiDevice; 23 import android.view.Display; 24 import androidx.test.InstrumentationRegistry; 25 26 import java.io.IOException; 27 28 public class HelperTestUtility { 29 30 // Clear the cache. 31 private static final String CLEAR_CACHE_CMD = "echo 3 > /proc/sys/vm/drop_caches"; 32 // Dismiss the lockscreen. 33 private static final String UNLOCK_CMD = "wm dismiss-keyguard"; 34 // Input a keycode. 35 private static final String KEYEVENT_CMD_TEMPLATE = "input keyevent %s"; 36 // Launch an app. 37 private static final String LAUNCH_APP_CMD_TEMPLATE = 38 "monkey -p %s -c android.intent.category.LAUNCHER 1"; 39 // Keycode(s) for convenience functions. 40 private static final String KEYCODE_WAKEUP = "KEYCODE_WAKEUP"; 41 // Delay between actions happening in the device. 42 public static final int ACTION_DELAY = 2000; 43 44 private static UiDevice mDevice; 45 46 /** 47 * Returns the active {@link UiDevice} to interact with. 48 */ getUiDevice()49 public static UiDevice getUiDevice() { 50 if (mDevice == null) { 51 mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); 52 } 53 return mDevice; 54 } 55 56 /** 57 * Runs a shell command, {@code cmd}, and returns the output. 58 */ executeShellCommand(String cmd)59 public static String executeShellCommand(String cmd) { 60 try { 61 return getUiDevice().executeShellCommand(cmd); 62 } catch (IOException e) { 63 throw new RuntimeException(e); 64 } 65 } 66 67 /** 68 * Clears the cache. 69 */ clearCache()70 public static void clearCache() { 71 SystemClock.sleep(HelperTestUtility.ACTION_DELAY); 72 executeShellCommand(CLEAR_CACHE_CMD); 73 SystemClock.sleep(HelperTestUtility.ACTION_DELAY); 74 } 75 76 /** 77 * Close the test app and clear the cache. 78 * TODO: Replace it with the rules. 79 */ clearApp(String killCmd)80 public static void clearApp(String killCmd) { 81 SystemClock.sleep(HelperTestUtility.ACTION_DELAY); 82 executeShellCommand(killCmd); 83 clearCache(); 84 } 85 86 /** 87 * Send a keycode via adb. 88 */ sendKeyCode(String keyCode)89 public static void sendKeyCode(String keyCode) { 90 executeShellCommand(String.format(KEYEVENT_CMD_TEMPLATE, keyCode)); 91 SystemClock.sleep(HelperTestUtility.ACTION_DELAY); 92 } 93 94 /** 95 * Launch an app via adb. 96 */ launchPackageViaAdb(String pkgName)97 public static void launchPackageViaAdb(String pkgName) { 98 executeShellCommand(String.format(LAUNCH_APP_CMD_TEMPLATE, pkgName)); 99 SystemClock.sleep(HelperTestUtility.ACTION_DELAY); 100 } 101 102 /** 103 * Wake up and unlock the device if the device is not in the unlocked state already. 104 */ wakeUpAndUnlock()105 public static void wakeUpAndUnlock() { 106 // Check whether display is on using DisplayManager. 107 Context context = InstrumentationRegistry.getContext(); 108 DisplayManager displayManager = 109 (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE); 110 boolean displayIsOn = (displayManager.getDisplays()[0].getState() == Display.STATE_ON); 111 // Check whether lockscreen is dismissed using KeyguardManager. 112 KeyguardManager keyguardManager = 113 (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); 114 boolean displayIsUnlocked = (!keyguardManager.isKeyguardLocked()); 115 if (!(displayIsOn && displayIsUnlocked)) { 116 sendKeyCode(KEYCODE_WAKEUP); 117 executeShellCommand(UNLOCK_CMD); 118 SystemClock.sleep(HelperTestUtility.ACTION_DELAY); 119 } 120 } 121 } 122 123