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 import static org.junit.Assume.assumeTrue; 21 22 import android.content.Context; 23 import android.content.pm.PackageManager; 24 import android.hardware.display.DisplayManager; 25 import android.testing.TestableContext; 26 27 import androidx.test.platform.app.InstrumentationRegistry; 28 29 import com.android.internal.protolog.common.ProtoLog; 30 31 import org.junit.After; 32 import org.junit.Before; 33 import org.mockito.MockitoAnnotations; 34 35 /** 36 * Base class that does shell test case setup. 37 */ 38 public abstract class ShellTestCase { 39 40 protected TestableContext mContext; 41 private PackageManager mPm; 42 43 @Before shellSetup()44 public void shellSetup() { 45 // Disable protolog tool when running the tests from studio 46 ProtoLog.REQUIRE_PROTOLOGTOOL = false; 47 48 MockitoAnnotations.initMocks(this); 49 final Context context = 50 InstrumentationRegistry.getInstrumentation().getTargetContext(); 51 final DisplayManager dm = context.getSystemService(DisplayManager.class); 52 mPm = context.getPackageManager(); 53 mContext = new TestableContext( 54 context.createDisplayContext(dm.getDisplay(DEFAULT_DISPLAY))); 55 56 InstrumentationRegistry 57 .getInstrumentation() 58 .getUiAutomation() 59 .adoptShellPermissionIdentity(); 60 } 61 62 @After shellTearDown()63 public void shellTearDown() { 64 InstrumentationRegistry 65 .getInstrumentation() 66 .getUiAutomation() 67 .dropShellPermissionIdentity(); 68 } 69 getContext()70 protected Context getContext() { 71 return mContext; 72 } 73 74 /** 75 * Makes an assumption that the test device is a TV device, used to guard tests that should 76 * only be run on TVs. 77 */ assumeTelevision()78 protected void assumeTelevision() { 79 assumeTrue(isTelevision()); 80 } 81 82 /** 83 * Returns whether this test device is a TV device. 84 */ isTelevision()85 protected boolean isTelevision() { 86 return mPm.hasSystemFeature(PackageManager.FEATURE_LEANBACK) 87 || mPm.hasSystemFeature(PackageManager.FEATURE_LEANBACK_ONLY); 88 } 89 } 90