1 /* 2 * Copyright (C) 2024 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.adservices.ui.util; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Mockito.spy; 22 23 import android.app.KeyguardManager; 24 import android.content.Context; 25 import android.os.RemoteException; 26 27 import androidx.test.platform.app.InstrumentationRegistry; 28 import androidx.test.uiautomator.By; 29 import androidx.test.uiautomator.UiDevice; 30 import androidx.test.uiautomator.UiObjectNotFoundException; 31 import androidx.test.uiautomator.Until; 32 33 import com.android.adservices.common.AdServicesExtendedMockitoTestCase; 34 import com.android.adservices.common.AdservicesTestHelper; 35 36 import org.junit.After; 37 import org.junit.Before; 38 39 /** 40 * Base class for all settings UI unit tests. 41 * 42 * <p>Contains basic device setup and teardown methods. 43 */ 44 public abstract class AdServicesUiTestCase extends AdServicesExtendedMockitoTestCase { 45 46 public static final int LAUNCH_TIMEOUT = 5_000; 47 48 protected final UiDevice mDevice = 49 UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); 50 protected Context mSpyContext; 51 52 @Before setUpDevice()53 public void setUpDevice() throws RemoteException { 54 mSpyContext = spy(appContext.get()); 55 56 // Unlock the device if required 57 KeyguardManager keyguardManager = mSpyContext.getSystemService(KeyguardManager.class); 58 if (keyguardManager.isKeyguardLocked()) { 59 mDevice.swipe( 60 mDevice.getDisplayWidth() / 2, 61 mDevice.getDisplayHeight(), 62 mDevice.getDisplayWidth() / 2, 63 0, 64 50); 65 } 66 // Start from the home screen 67 mDevice.pressHome(); 68 mDevice.setOrientationNatural(); 69 70 // Wait for launcher 71 String launcherPackage = mDevice.getLauncherPackageName(); 72 assertThat(launcherPackage).isNotNull(); 73 mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT); 74 } 75 76 @After teardown()77 public void teardown() throws UiObjectNotFoundException { 78 ApkTestUtil.takeScreenshot(mDevice, getClass().getSimpleName() + "_" + getTestName() + "_"); 79 80 AdservicesTestHelper.killAdservicesProcess(appContext.get()); 81 } 82 } 83