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.Before;
33 import org.junit.Rule;
34 
35 public abstract class SysuiBaseFragmentTest extends BaseFragmentTest {
36 
37     public static final Class<?>[] ALL_SUPPORTED_CLASSES = LeakCheckedTest.ALL_SUPPORTED_CLASSES;
38 
39     @Rule
40     public final SysuiLeakCheck mLeakCheck = new SysuiLeakCheck();
41 
42     @Rule
43     public final DexmakerShareClassLoaderRule mDexmakerShareClassLoaderRule =
44             new DexmakerShareClassLoaderRule();
45 
46     protected TestableDependency mDependency;
47     protected SysuiTestableContext mSysuiContext;
48     private Instrumentation mRealInstrumentation;
49 
SysuiBaseFragmentTest(Class<? extends Fragment> cls)50     public SysuiBaseFragmentTest(Class<? extends Fragment> cls) {
51         super(cls);
52     }
53 
54     @Before
SysuiSetup()55     public void SysuiSetup() {
56         SystemUIFactory.createFromConfig(mContext, true);
57         mDependency = new TestableDependency(
58                 SystemUIFactory.getInstance().getSysUIComponent().createDependency());
59         Dependency.setInstance(mDependency);
60 
61         // TODO: Figure out another way to give reference to a SysuiTestableContext.
62         mSysuiContext = (SysuiTestableContext) mContext;
63 
64         mRealInstrumentation = InstrumentationRegistry.getInstrumentation();
65         Instrumentation inst = spy(mRealInstrumentation);
66         when(inst.getContext()).thenThrow(new RuntimeException(
67                 "SysUI Tests should use SysuiTestCase#getContext or SysuiTestCase#mContext"));
68         when(inst.getTargetContext()).thenThrow(new RuntimeException(
69                 "SysUI Tests should use SysuiTestCase#getContext or SysuiTestCase#mContext"));
70         InstrumentationRegistry.registerInstance(inst, InstrumentationRegistry.getArguments());
71         mDependency.injectMockDependency(AssistManager.class);
72     }
73 
74     @After
SysuiTeardown()75     public void SysuiTeardown() {
76         InstrumentationRegistry.registerInstance(mRealInstrumentation,
77                 InstrumentationRegistry.getArguments());
78         SystemUIFactory.cleanup();
79     }
80 
81     @Override
getContext()82     protected SysuiTestableContext getContext() {
83         return new SysuiTestableContext(InstrumentationRegistry.getContext(), mLeakCheck);
84     }
85 
injectLeakCheckedDependencies(Class<?>.... cls)86     public void injectLeakCheckedDependencies(Class<?>... cls) {
87         for (Class<?> c : cls) {
88             injectLeakCheckedDependency(c);
89         }
90     }
91 
injectLeakCheckedDependency(Class<T> c)92     public <T> void injectLeakCheckedDependency(Class<T> c) {
93         mDependency.injectTestDependency(c, mLeakCheck.getLeakChecker(c));
94     }
95 }
96