blob: b6335f3681830b50e958b0db33a7941b52409f02 [file] [log] [blame]
Jason Monk340b0e52017-03-08 14:57:56 -05001/*
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
15package com.android.systemui;
16
Jason Monk893f0bd2017-06-01 11:21:14 -040017import static org.mockito.Mockito.spy;
18import static org.mockito.Mockito.when;
19
Jason Monk340b0e52017-03-08 14:57:56 -050020import android.app.Fragment;
Jason Monk893f0bd2017-06-01 11:21:14 -040021import android.app.Instrumentation;
Jason Monk340b0e52017-03-08 14:57:56 -050022import android.support.test.InstrumentationRegistry;
23import android.testing.BaseFragmentTest;
Adrian Roos3150dbf2018-03-28 18:06:52 +020024import android.testing.DexmakerShareClassLoaderRule;
Jason Monk340b0e52017-03-08 14:57:56 -050025
26import com.android.systemui.utils.leaks.LeakCheckedTest;
27import com.android.systemui.utils.leaks.LeakCheckedTest.SysuiLeakCheck;
28
Jason Monk893f0bd2017-06-01 11:21:14 -040029import org.junit.After;
Jason Monk340b0e52017-03-08 14:57:56 -050030import org.junit.Before;
31import org.junit.Rule;
32
33public 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
Adrian Roos3150dbf2018-03-28 18:06:52 +020040 @Rule
41 public final DexmakerShareClassLoaderRule mDexmakerShareClassLoaderRule =
42 new DexmakerShareClassLoaderRule();
43
Jason Monk340b0e52017-03-08 14:57:56 -050044 protected final TestableDependency mDependency = new TestableDependency(mContext);
45 protected SysuiTestableContext mSysuiContext;
Jason Monk893f0bd2017-06-01 11:21:14 -040046 private Instrumentation mRealInstrumentation;
Jason Monk340b0e52017-03-08 14:57:56 -050047
48 public SysuiBaseFragmentTest(Class<? extends Fragment> cls) {
49 super(cls);
50 }
51
52 @Before
53 public void SysuiSetup() {
Jason Monk340b0e52017-03-08 14:57:56 -050054 SystemUIFactory.createFromConfig(mContext);
55 // TODO: Figure out another way to give reference to a SysuiTestableContext.
56 mSysuiContext = (SysuiTestableContext) mContext;
Jason Monk893f0bd2017-06-01 11:21:14 -040057
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
68 public void SysuiTeardown() {
69 InstrumentationRegistry.registerInstance(mRealInstrumentation,
70 InstrumentationRegistry.getArguments());
Jason Monk340b0e52017-03-08 14:57:56 -050071 }
72
73 @Override
74 protected SysuiTestableContext getContext() {
75 return new SysuiTestableContext(InstrumentationRegistry.getContext(), mLeakCheck);
76 }
77
78 public void injectLeakCheckedDependencies(Class<?>... cls) {
79 for (Class<?> c : cls) {
80 injectLeakCheckedDependency(c);
81 }
82 }
83
84 public <T> void injectLeakCheckedDependency(Class<T> c) {
85 mDependency.injectTestDependency(c, mLeakCheck.getLeakChecker(c));
86 }
87}