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 30 /** 31 * Base class that does shell test case setup. 32 */ 33 public abstract class ShellTestCase { 34 35 protected TestableContext mContext; 36 37 @Before shellSetup()38 public void shellSetup() { 39 final Context context = 40 InstrumentationRegistry.getInstrumentation().getTargetContext(); 41 final DisplayManager dm = context.getSystemService(DisplayManager.class); 42 mContext = new TestableContext( 43 context.createDisplayContext(dm.getDisplay(DEFAULT_DISPLAY))); 44 45 InstrumentationRegistry 46 .getInstrumentation() 47 .getUiAutomation() 48 .adoptShellPermissionIdentity(); 49 } 50 51 @After shellTearDown()52 public void shellTearDown() { 53 InstrumentationRegistry 54 .getInstrumentation() 55 .getUiAutomation() 56 .dropShellPermissionIdentity(); 57 } 58 getContext()59 protected Context getContext() { 60 return mContext; 61 } 62 } 63