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.platform.test.flag.junit.SetFlagsRule; 23 import android.testing.BaseFragmentTest; 24 import android.testing.DexmakerShareClassLoaderRule; 25 26 import androidx.test.InstrumentationRegistry; 27 28 import com.android.systemui.assist.AssistManager; 29 import com.android.systemui.utils.leaks.LeakCheckedTest; 30 import com.android.systemui.utils.leaks.LeakCheckedTest.SysuiLeakCheck; 31 32 import org.junit.After; 33 import org.junit.AfterClass; 34 import org.junit.Before; 35 import org.junit.ClassRule; 36 import org.junit.Rule; 37 import org.mockito.Mockito; 38 39 import java.util.concurrent.ExecutionException; 40 41 public abstract class SysuiBaseFragmentTest extends BaseFragmentTest { 42 43 public static final Class<?>[] ALL_SUPPORTED_CLASSES = LeakCheckedTest.ALL_SUPPORTED_CLASSES; 44 45 @Rule 46 public final SysuiLeakCheck mLeakCheck = new SysuiLeakCheck(); 47 48 @ClassRule 49 public static final SetFlagsRule.ClassRule mSetFlagsClassRule = 50 new SetFlagsRule.ClassRule( 51 com.android.systemui.Flags.class); 52 @Rule public final SetFlagsRule mSetFlagsRule = mSetFlagsClassRule.createSetFlagsRule(); 53 54 @Rule 55 public final DexmakerShareClassLoaderRule mDexmakerShareClassLoaderRule = 56 new DexmakerShareClassLoaderRule(); 57 58 protected TestableDependency mDependency; 59 protected SysuiTestableContext mSysuiContext; 60 private Instrumentation mRealInstrumentation; 61 SysuiBaseFragmentTest(Class<? extends Fragment> cls)62 public SysuiBaseFragmentTest(Class<? extends Fragment> cls) { 63 super(cls); 64 } 65 66 @Before sysuiSetup()67 public void sysuiSetup() throws ExecutionException, InterruptedException { 68 SystemUIInitializer initializer = new SystemUIInitializerImpl(mContext); 69 initializer.init(true); 70 mDependency = new TestableDependency(initializer.getSysUIComponent().createDependency()); 71 Dependency.setInstance(mDependency); 72 73 // TODO: Figure out another way to give reference to a SysuiTestableContext. 74 mSysuiContext = (SysuiTestableContext) mContext; 75 76 mRealInstrumentation = InstrumentationRegistry.getInstrumentation(); 77 Instrumentation inst = spy(mRealInstrumentation); 78 when(inst.getContext()).thenThrow(new RuntimeException( 79 "SysUI Tests should use SysuiTestCase#getContext or SysuiTestCase#mContext")); 80 when(inst.getTargetContext()).thenThrow(new RuntimeException( 81 "SysUI Tests should use SysuiTestCase#getContext or SysuiTestCase#mContext")); 82 InstrumentationRegistry.registerInstance(inst, InstrumentationRegistry.getArguments()); 83 mDependency.injectMockDependency(AssistManager.class); 84 } 85 86 @After SysuiTeardown()87 public void SysuiTeardown() { 88 InstrumentationRegistry.registerInstance(mRealInstrumentation, 89 InstrumentationRegistry.getArguments()); 90 } 91 92 @AfterClass mockitoTeardown()93 public static void mockitoTeardown() { 94 Mockito.framework().clearInlineMocks(); 95 } 96 97 @Override getContext()98 protected SysuiTestableContext getContext() { 99 return new SysuiTestableContext(InstrumentationRegistry.getContext(), mLeakCheck); 100 } 101 injectLeakCheckedDependencies(Class<?>.... cls)102 public void injectLeakCheckedDependencies(Class<?>... cls) { 103 for (Class<?> c : cls) { 104 injectLeakCheckedDependency(c); 105 } 106 } 107 injectLeakCheckedDependency(Class<T> c)108 public <T> void injectLeakCheckedDependency(Class<T> c) { 109 mDependency.injectTestDependency(c, mLeakCheck.getLeakChecker(c)); 110 } 111 } 112