1 /* 2 * Copyright (C) 2020 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.wm.shell; 18 19 import static android.view.Display.DEFAULT_DISPLAY; 20 21 import static org.junit.Assume.assumeTrue; 22 23 import android.content.Context; 24 import android.content.pm.PackageManager; 25 import android.hardware.display.DisplayManager; 26 import android.platform.test.flag.junit.CheckFlagsRule; 27 import android.platform.test.flag.junit.DeviceFlagsValueProvider; 28 import android.platform.test.flag.junit.SetFlagsRule; 29 import android.testing.TestableContext; 30 31 import androidx.test.platform.app.InstrumentationRegistry; 32 33 import com.android.internal.protolog.ProtoLog; 34 35 import org.junit.After; 36 import org.junit.Before; 37 import org.junit.ClassRule; 38 import org.junit.Rule; 39 import org.mockito.MockitoAnnotations; 40 41 /** 42 * Base class that does shell test case setup. 43 */ 44 public abstract class ShellTestCase { 45 46 @ClassRule 47 public static final SetFlagsRule.ClassRule mClassRule = new SetFlagsRule.ClassRule(); 48 49 @Rule 50 public final CheckFlagsRule mCheckFlagsRule = 51 DeviceFlagsValueProvider.createCheckFlagsRule(); 52 53 @Rule 54 public final SetFlagsRule mSetFlagsRule = mClassRule.createSetFlagsRule(); 55 56 protected TestableContext mContext; 57 private PackageManager mPm; 58 59 @Before shellSetup()60 public void shellSetup() { 61 // Disable protolog tool when running the tests from studio 62 ProtoLog.REQUIRE_PROTOLOGTOOL = false; 63 64 // Make sure ProtoLog is initialized before any logging occurs. 65 ProtoLog.init(); 66 67 MockitoAnnotations.initMocks(this); 68 final Context context = 69 InstrumentationRegistry.getInstrumentation().getTargetContext(); 70 final DisplayManager dm = context.getSystemService(DisplayManager.class); 71 mPm = context.getPackageManager(); 72 mContext = new TestableContext( 73 context.createDisplayContext(dm.getDisplay(DEFAULT_DISPLAY))); 74 75 InstrumentationRegistry 76 .getInstrumentation() 77 .getUiAutomation() 78 .adoptShellPermissionIdentity(); 79 } 80 81 @After shellTearDown()82 public void shellTearDown() { 83 InstrumentationRegistry 84 .getInstrumentation() 85 .getUiAutomation() 86 .dropShellPermissionIdentity(); 87 } 88 getContext()89 protected Context getContext() { 90 return mContext; 91 } 92 93 /** 94 * Makes an assumption that the test device is a TV device, used to guard tests that should 95 * only be run on TVs. 96 */ assumeTelevision()97 protected void assumeTelevision() { 98 assumeTrue(isTelevision()); 99 } 100 101 /** 102 * Returns whether this test device is a TV device. 103 */ isTelevision()104 protected boolean isTelevision() { 105 return mPm.hasSystemFeature(PackageManager.FEATURE_LEANBACK) 106 || mPm.hasSystemFeature(PackageManager.FEATURE_LEANBACK_ONLY); 107 } 108 } 109