• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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);
57         mDependency = new TestableDependency(mContext);
58         // TODO: Figure out another way to give reference to a SysuiTestableContext.
59         mSysuiContext = (SysuiTestableContext) mContext;
60 
61         mRealInstrumentation = InstrumentationRegistry.getInstrumentation();
62         Instrumentation inst = spy(mRealInstrumentation);
63         when(inst.getContext()).thenThrow(new RuntimeException(
64                 "SysUI Tests should use SysuiTestCase#getContext or SysuiTestCase#mContext"));
65         when(inst.getTargetContext()).thenThrow(new RuntimeException(
66                 "SysUI Tests should use SysuiTestCase#getContext or SysuiTestCase#mContext"));
67         InstrumentationRegistry.registerInstance(inst, InstrumentationRegistry.getArguments());
68         mDependency.injectMockDependency(AssistManager.class);
69     }
70 
71     @After
SysuiTeardown()72     public void SysuiTeardown() {
73         InstrumentationRegistry.registerInstance(mRealInstrumentation,
74                 InstrumentationRegistry.getArguments());
75         SystemUIFactory.cleanup();
76     }
77 
78     @Override
getContext()79     protected SysuiTestableContext getContext() {
80         return new SysuiTestableContext(InstrumentationRegistry.getContext(), mLeakCheck);
81     }
82 
injectLeakCheckedDependencies(Class<?>.... cls)83     public void injectLeakCheckedDependencies(Class<?>... cls) {
84         for (Class<?> c : cls) {
85             injectLeakCheckedDependency(c);
86         }
87     }
88 
injectLeakCheckedDependency(Class<T> c)89     public <T> void injectLeakCheckedDependency(Class<T> c) {
90         mDependency.injectTestDependency(c, mLeakCheck.getLeakChecker(c));
91     }
92 }
93