1 /* 2 * Copyright (C) 2022 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.pixel.tests; 18 19 import static androidx.test.platform.app.InstrumentationRegistry.getArguments; 20 import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation; 21 22 import android.app.KeyguardManager; 23 import android.support.test.uiautomator.By; 24 import android.support.test.uiautomator.UiDevice; 25 import android.support.test.uiautomator.Until; 26 27 import com.android.pixel.utils.DeviceUtils; 28 29 import org.junit.After; 30 import org.junit.Assert; 31 import org.junit.Before; 32 33 /** Base class for Pixel app compatibility tests. */ 34 public abstract class PixelAppCompatTestBase { 35 private static final String KEY_PACKAGE_NAME = "package"; 36 private DeviceUtils mDeviceUtils; 37 private UiDevice mDevice; 38 private KeyguardManager mKeyguardManager; 39 private String mPackage; 40 41 @Before setUp()42 public void setUp() throws Exception { 43 getDeviceUtils().setTestName(this.getClass().getSimpleName()); 44 getDeviceUtils().createLogDataDir(); 45 getDeviceUtils().wakeAndUnlockScreen(); 46 // Start from the home screen 47 getDeviceUtils().backToHome(getUiDevice().getLauncherPackageName()); 48 getDeviceUtils().startRecording(getPackage()); 49 } 50 51 @After tearDown()52 public void tearDown() throws Exception { 53 getDeviceUtils().stopRecording(); 54 } 55 getUiDevice()56 protected UiDevice getUiDevice() { 57 if (mDevice == null) { 58 mDevice = UiDevice.getInstance(getInstrumentation()); 59 } 60 return mDevice; 61 } 62 getDeviceUtils()63 protected DeviceUtils getDeviceUtils() { 64 if (mDeviceUtils == null) { 65 mDeviceUtils = new DeviceUtils(getUiDevice()); 66 } 67 return mDeviceUtils; 68 } 69 getKeyguardManager()70 protected KeyguardManager getKeyguardManager() { 71 if (mKeyguardManager == null) { 72 mKeyguardManager = 73 getInstrumentation().getContext().getSystemService(KeyguardManager.class); 74 } 75 return mKeyguardManager; 76 } 77 getPackage()78 protected String getPackage() { 79 if (mPackage == null) { 80 mPackage = getArguments().getString(KEY_PACKAGE_NAME); 81 } 82 return mPackage; 83 } 84 launchAndWaitAppOpen(long timeout)85 protected void launchAndWaitAppOpen(long timeout) { 86 // Launch the 3P app 87 getDeviceUtils().launchApp(getPackage()); 88 89 // Wait given timeout to ensure the 3P app completely loads 90 getUiDevice().wait(Until.hasObject(By.text(getPackage())), timeout); 91 Assert.assertTrue( 92 "3P app main page should show up", 93 getUiDevice().hasObject(By.pkg(getPackage()).depth(0))); 94 } 95 } 96