1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15 package com.android.systemui; 16 17 import static org.mockito.Mockito.spy; 18 import static org.mockito.Mockito.when; 19 20 import android.app.Fragment; 21 import android.app.Instrumentation; 22 import android.testing.BaseFragmentTest; 23 import android.testing.DexmakerShareClassLoaderRule; 24 25 import androidx.test.InstrumentationRegistry; 26 27 import com.android.systemui.assist.AssistManager; 28 import com.android.systemui.utils.leaks.LeakCheckedTest; 29 import com.android.systemui.utils.leaks.LeakCheckedTest.SysuiLeakCheck; 30 31 import org.junit.After; 32 import org.junit.AfterClass; 33 import org.junit.Before; 34 import org.junit.Rule; 35 import org.mockito.Mockito; 36 37 import java.util.concurrent.ExecutionException; 38 39 public abstract class SysuiBaseFragmentTest extends BaseFragmentTest { 40 41 public static final Class<?>[] ALL_SUPPORTED_CLASSES = LeakCheckedTest.ALL_SUPPORTED_CLASSES; 42 43 @Rule 44 public final SysuiLeakCheck mLeakCheck = new SysuiLeakCheck(); 45 46 @Rule 47 public final DexmakerShareClassLoaderRule mDexmakerShareClassLoaderRule = 48 new DexmakerShareClassLoaderRule(); 49 50 protected TestableDependency mDependency; 51 protected SysuiTestableContext mSysuiContext; 52 private Instrumentation mRealInstrumentation; 53 SysuiBaseFragmentTest(Class<? extends Fragment> cls)54 public SysuiBaseFragmentTest(Class<? extends Fragment> cls) { 55 super(cls); 56 } 57 58 @Before sysuiSetup()59 public void sysuiSetup() throws ExecutionException, InterruptedException { 60 SystemUIInitializer initializer = new SystemUIInitializerImpl(mContext); 61 initializer.init(true); 62 mDependency = new TestableDependency(initializer.getSysUIComponent().createDependency()); 63 Dependency.setInstance(mDependency); 64 65 // TODO: Figure out another way to give reference to a SysuiTestableContext. 66 mSysuiContext = (SysuiTestableContext) mContext; 67 68 mRealInstrumentation = InstrumentationRegistry.getInstrumentation(); 69 Instrumentation inst = spy(mRealInstrumentation); 70 when(inst.getContext()).thenThrow(new RuntimeException( 71 "SysUI Tests should use SysuiTestCase#getContext or SysuiTestCase#mContext")); 72 when(inst.getTargetContext()).thenThrow(new RuntimeException( 73 "SysUI Tests should use SysuiTestCase#getContext or SysuiTestCase#mContext")); 74 InstrumentationRegistry.registerInstance(inst, InstrumentationRegistry.getArguments()); 75 mDependency.injectMockDependency(AssistManager.class); 76 } 77 78 @After SysuiTeardown()79 public void SysuiTeardown() { 80 InstrumentationRegistry.registerInstance(mRealInstrumentation, 81 InstrumentationRegistry.getArguments()); 82 } 83 84 @AfterClass mockitoTeardown()85 public static void mockitoTeardown() { 86 Mockito.framework().clearInlineMocks(); 87 } 88 89 @Override getContext()90 protected SysuiTestableContext getContext() { 91 return new SysuiTestableContext(InstrumentationRegistry.getContext(), mLeakCheck); 92 } 93 injectLeakCheckedDependencies(Class<?>.... cls)94 public void injectLeakCheckedDependencies(Class<?>... cls) { 95 for (Class<?> c : cls) { 96 injectLeakCheckedDependency(c); 97 } 98 } 99 injectLeakCheckedDependency(Class<T> c)100 public <T> void injectLeakCheckedDependency(Class<T> c) { 101 mDependency.injectTestDependency(c, mLeakCheck.getLeakChecker(c)); 102 } 103 } 104