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.android.adservices.service.FlagsConstants.KEY_GA_UX_FEATURE_ENABLED; 20 21 import static com.google.common.truth.Truth.assertThat; 22 23 import static org.mockito.Mockito.spy; 24 25 import android.app.KeyguardManager; 26 import android.content.Context; 27 28 import androidx.test.platform.app.InstrumentationRegistry; 29 import androidx.test.uiautomator.By; 30 import androidx.test.uiautomator.UiDevice; 31 import androidx.test.uiautomator.Until; 32 33 import com.android.adservices.common.AdServicesExtendedMockitoTestCase; 34 import com.android.adservices.common.AdServicesFlagsSetterRule; 35 import com.android.adservices.common.AdservicesTestHelper; 36 import com.android.adservices.common.annotations.DisableGlobalKillSwitch; 37 import com.android.adservices.common.annotations.SetAllLogcatTags; 38 import com.android.adservices.common.annotations.SetCompatModeFlags; 39 import com.android.adservices.shared.testing.annotations.SetFlagTrue; 40 41 import org.junit.After; 42 import org.junit.Before; 43 import org.junit.Rule; 44 45 /** 46 * Base class for all settings UI unit tests. 47 * 48 * <p>Contains basic device setup and teardown methods. 49 */ 50 @DisableGlobalKillSwitch 51 @SetAllLogcatTags 52 @SetCompatModeFlags 53 @SetFlagTrue(KEY_GA_UX_FEATURE_ENABLED) 54 public abstract class AdServicesUiTestCase extends AdServicesExtendedMockitoTestCase { 55 56 public static final int LAUNCH_TIMEOUT_MS = 5_000; 57 58 // TODO(b/384798806): called realFlags because it's "really" changing the Flags using 59 // DeviceConfig (and superclass will eventually provide a flags object that uses 60 // AdServicesFakeFlagsSetterRule). Ideally this class should use that same flags, but it doesn't 61 // support DebugFlags (we'll need to wait until the DebugFlags logic is moved to its own rule). 62 @Rule(order = 11) 63 public final AdServicesFlagsSetterRule realFlags = AdServicesFlagsSetterRule.newInstance(); 64 65 protected final UiDevice mDevice = 66 UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); 67 protected Context mSpyContext; 68 69 @Before setUpDevice()70 public final void setUpDevice() throws Exception { 71 mSpyContext = spy(appContext.get()); 72 73 // Unlock the device if required 74 KeyguardManager keyguardManager = mSpyContext.getSystemService(KeyguardManager.class); 75 if (keyguardManager.isKeyguardLocked()) { 76 mDevice.swipe( 77 mDevice.getDisplayWidth() / 2, 78 mDevice.getDisplayHeight(), 79 mDevice.getDisplayWidth() / 2, 80 0, 81 50); 82 } 83 // Start from the home screen 84 mDevice.pressHome(); 85 mDevice.setOrientationNatural(); 86 87 // Wait for launcher 88 String launcherPackage = mDevice.getLauncherPackageName(); 89 assertThat(launcherPackage).isNotNull(); 90 mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT_MS); 91 } 92 93 @After takeScreenshotAndKillProcess()94 public final void takeScreenshotAndKillProcess() throws Exception { 95 ApkTestUtil.takeScreenshot(mDevice, getClass().getSimpleName() + "_" + getTestName() + "_"); 96 97 AdservicesTestHelper.killAdservicesProcess(appContext.get()); 98 } 99 } 100