blob: 256cfb28d4de9f869ac160824ad5bbcaa770c81a [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.testing.BaseFragmentTest;
Adrian Roos3150dbf2018-03-28 18:06:52 +020023import android.testing.DexmakerShareClassLoaderRule;
Jason Monk340b0e52017-03-08 14:57:56 -050024
Brett Chabot84151d92019-02-27 15:37:59 -080025import androidx.test.InstrumentationRegistry;
26
Jason Monk340b0e52017-03-08 14:57:56 -050027import com.android.systemui.utils.leaks.LeakCheckedTest;
28import com.android.systemui.utils.leaks.LeakCheckedTest.SysuiLeakCheck;
29
Jason Monk893f0bd2017-06-01 11:21:14 -040030import org.junit.After;
Jason Monk340b0e52017-03-08 14:57:56 -050031import org.junit.Before;
32import org.junit.Rule;
33
34public abstract class SysuiBaseFragmentTest extends BaseFragmentTest {
35
36 public static final Class<?>[] ALL_SUPPORTED_CLASSES = LeakCheckedTest.ALL_SUPPORTED_CLASSES;
37
38 @Rule
39 public final SysuiLeakCheck mLeakCheck = new SysuiLeakCheck();
40
Adrian Roos3150dbf2018-03-28 18:06:52 +020041 @Rule
42 public final DexmakerShareClassLoaderRule mDexmakerShareClassLoaderRule =
43 new DexmakerShareClassLoaderRule();
44
Jason Monk340b0e52017-03-08 14:57:56 -050045 protected final TestableDependency mDependency = new TestableDependency(mContext);
46 protected SysuiTestableContext mSysuiContext;
Jason Monk893f0bd2017-06-01 11:21:14 -040047 private Instrumentation mRealInstrumentation;
Jason Monk340b0e52017-03-08 14:57:56 -050048
49 public SysuiBaseFragmentTest(Class<? extends Fragment> cls) {
50 super(cls);
51 }
52
53 @Before
54 public void SysuiSetup() {
Jason Monk340b0e52017-03-08 14:57:56 -050055 SystemUIFactory.createFromConfig(mContext);
56 // TODO: Figure out another way to give reference to a SysuiTestableContext.
57 mSysuiContext = (SysuiTestableContext) mContext;
Jason Monk893f0bd2017-06-01 11:21:14 -040058
59 mRealInstrumentation = InstrumentationRegistry.getInstrumentation();
60 Instrumentation inst = spy(mRealInstrumentation);
61 when(inst.getContext()).thenThrow(new RuntimeException(
62 "SysUI Tests should use SysuiTestCase#getContext or SysuiTestCase#mContext"));
63 when(inst.getTargetContext()).thenThrow(new RuntimeException(
64 "SysUI Tests should use SysuiTestCase#getContext or SysuiTestCase#mContext"));
65 InstrumentationRegistry.registerInstance(inst, InstrumentationRegistry.getArguments());
66 }
67
68 @After
69 public void SysuiTeardown() {
70 InstrumentationRegistry.registerInstance(mRealInstrumentation,
71 InstrumentationRegistry.getArguments());
Jason Monk340b0e52017-03-08 14:57:56 -050072 }
73
74 @Override
75 protected SysuiTestableContext getContext() {
76 return new SysuiTestableContext(InstrumentationRegistry.getContext(), mLeakCheck);
77 }
78
79 public void injectLeakCheckedDependencies(Class<?>... cls) {
80 for (Class<?> c : cls) {
81 injectLeakCheckedDependency(c);
82 }
83 }
84
85 public <T> void injectLeakCheckedDependency(Class<T> c) {
86 mDependency.injectTestDependency(c, mLeakCheck.getLeakChecker(c));
87 }
88}