1 /* 2 * Copyright 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 androidx.test.uiautomator.testapp; 18 19 import static org.junit.Assert.assertThrows; 20 import static org.junit.Assert.assertTrue; 21 22 import android.app.Activity; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.os.Bundle; 26 import android.util.Log; 27 28 import androidx.test.core.app.ApplicationProvider; 29 import androidx.test.ext.junit.runners.AndroidJUnit4; 30 import androidx.test.platform.app.InstrumentationRegistry; 31 import androidx.test.uiautomator.By; 32 import androidx.test.uiautomator.Configurator; 33 import androidx.test.uiautomator.UiDevice; 34 import androidx.test.uiautomator.UiObjectNotFoundException; 35 import androidx.test.uiautomator.Until; 36 37 import org.jspecify.annotations.NonNull; 38 import org.jspecify.annotations.Nullable; 39 import org.junit.Before; 40 import org.junit.Rule; 41 import org.junit.function.ThrowingRunnable; 42 import org.junit.rules.TestWatcher; 43 import org.junit.runner.Description; 44 import org.junit.runner.RunWith; 45 46 @RunWith(AndroidJUnit4.class) 47 public abstract class BaseTest { 48 49 protected static final long TIMEOUT_MS = 10_000; 50 protected static final String TEST_APP = "androidx.test.uiautomator.testapp"; 51 protected static final int DEFAULT_FLAGS = 52 Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK; 53 54 // Dumps the UI hierarchy to logcat on failure. 55 @Rule 56 public TestWatcher mDumpHierarchyWatcher = new TestWatcher() { 57 @Override 58 protected void failed(Throwable t, Description description) { 59 try { 60 mDevice.dumpWindowHierarchy(System.err); 61 } catch (Exception e) { 62 Log.e(description.getTestClass().getSimpleName(), "Failed to dump hierarchy", e); 63 } 64 } 65 }; 66 67 protected UiDevice mDevice; 68 69 @Before setUp()70 public void setUp() throws Exception { 71 mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); 72 mDevice.wakeUp(); 73 mDevice.pressMenu(); // Try to dismiss the lock screen if necessary. 74 mDevice.pressHome(); 75 mDevice.setOrientationNatural(); 76 } 77 launchTestActivity(@onNull Class<? extends Activity> activity)78 protected void launchTestActivity(@NonNull Class<? extends Activity> activity) { 79 launchTestActivity(activity, new Intent().setFlags(DEFAULT_FLAGS), null); 80 } 81 launchTestActivity(@onNull Class<? extends Activity> activity, @NonNull Intent intent, @Nullable Bundle options)82 protected void launchTestActivity(@NonNull Class<? extends Activity> activity, 83 @NonNull Intent intent, @Nullable Bundle options) { 84 Context context = ApplicationProvider.getApplicationContext(); 85 context.startActivity(new Intent(intent).setClass(context, activity), options); 86 assertTrue("Test app not visible after launching activity", 87 mDevice.wait(Until.hasObject(By.pkg(TEST_APP)), TIMEOUT_MS)); 88 } 89 90 // Helper to verify that an operation throws a UiObjectNotFoundException without waiting for 91 // the full 10s default timeout. assertUiObjectNotFound(ThrowingRunnable runnable)92 protected static void assertUiObjectNotFound(ThrowingRunnable runnable) { 93 Configurator configurator = Configurator.getInstance(); 94 long timeout = configurator.getWaitForSelectorTimeout(); 95 configurator.setWaitForSelectorTimeout(1_000); 96 try { 97 assertThrows(UiObjectNotFoundException.class, runnable); 98 } finally { 99 configurator.setWaitForSelectorTimeout(timeout); 100 } 101 } 102 } 103