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