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