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 android.content.Context;
22 import android.hardware.display.DisplayManager;
23 import android.testing.TestableContext;
24 
25 import androidx.test.InstrumentationRegistry;
26 
27 import org.junit.After;
28 import org.junit.Before;
29 import org.mockito.MockitoAnnotations;
30 
31 /**
32  * Base class that does shell test case setup.
33  */
34 public abstract class ShellTestCase {
35 
36     protected TestableContext mContext;
37 
38     @Before
shellSetup()39     public void shellSetup() {
40         MockitoAnnotations.initMocks(this);
41         final Context context =
42                 InstrumentationRegistry.getInstrumentation().getTargetContext();
43         final DisplayManager dm = context.getSystemService(DisplayManager.class);
44         mContext = new TestableContext(
45                 context.createDisplayContext(dm.getDisplay(DEFAULT_DISPLAY)));
46 
47         InstrumentationRegistry
48                 .getInstrumentation()
49                 .getUiAutomation()
50                 .adoptShellPermissionIdentity();
51     }
52 
53     @After
shellTearDown()54     public void shellTearDown() {
55         InstrumentationRegistry
56                 .getInstrumentation()
57                 .getUiAutomation()
58                 .dropShellPermissionIdentity();
59     }
60 
getContext()61     protected Context getContext() {
62         return mContext;
63     }
64 }
65