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.platform.app.InstrumentationRegistry; 26 27 import com.android.internal.protolog.common.ProtoLog; 28 29 import org.junit.After; 30 import org.junit.Before; 31 import org.mockito.MockitoAnnotations; 32 33 /** 34 * Base class that does shell test case setup. 35 */ 36 public abstract class ShellTestCase { 37 38 protected TestableContext mContext; 39 40 @Before shellSetup()41 public void shellSetup() { 42 // Disable protolog tool when running the tests from studio 43 ProtoLog.REQUIRE_PROTOLOGTOOL = false; 44 45 MockitoAnnotations.initMocks(this); 46 final Context context = 47 InstrumentationRegistry.getInstrumentation().getTargetContext(); 48 final DisplayManager dm = context.getSystemService(DisplayManager.class); 49 mContext = new TestableContext( 50 context.createDisplayContext(dm.getDisplay(DEFAULT_DISPLAY))); 51 52 InstrumentationRegistry 53 .getInstrumentation() 54 .getUiAutomation() 55 .adoptShellPermissionIdentity(); 56 } 57 58 @After shellTearDown()59 public void shellTearDown() { 60 InstrumentationRegistry 61 .getInstrumentation() 62 .getUiAutomation() 63 .dropShellPermissionIdentity(); 64 } 65 getContext()66 protected Context getContext() { 67 return mContext; 68 } 69 } 70