blob: cf778504190aaf9abf2e491c9d60fa7b4c4c04ee [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
Dave Mankoff0cf8dfc2019-09-27 12:46:41 -040027import com.android.systemui.assist.AssistManager;
Jason Monk340b0e52017-03-08 14:57:56 -050028import com.android.systemui.utils.leaks.LeakCheckedTest;
29import com.android.systemui.utils.leaks.LeakCheckedTest.SysuiLeakCheck;
30
Jason Monk893f0bd2017-06-01 11:21:14 -040031import org.junit.After;
Jason Monk340b0e52017-03-08 14:57:56 -050032import org.junit.Before;
33import org.junit.Rule;
34
35public 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
Adrian Roos3150dbf2018-03-28 18:06:52 +020042 @Rule
43 public final DexmakerShareClassLoaderRule mDexmakerShareClassLoaderRule =
44 new DexmakerShareClassLoaderRule();
45
Dave Mankoffa4a71362019-08-27 14:40:05 -040046 protected TestableDependency mDependency;
Jason Monk340b0e52017-03-08 14:57:56 -050047 protected SysuiTestableContext mSysuiContext;
Jason Monk893f0bd2017-06-01 11:21:14 -040048 private Instrumentation mRealInstrumentation;
Jason Monk340b0e52017-03-08 14:57:56 -050049
50 public SysuiBaseFragmentTest(Class<? extends Fragment> cls) {
51 super(cls);
52 }
53
54 @Before
55 public void SysuiSetup() {
Jason Monk340b0e52017-03-08 14:57:56 -050056 SystemUIFactory.createFromConfig(mContext);
Dave Mankoffa4a71362019-08-27 14:40:05 -040057 mDependency = new TestableDependency(mContext);
Jason Monk340b0e52017-03-08 14:57:56 -050058 // TODO: Figure out another way to give reference to a SysuiTestableContext.
59 mSysuiContext = (SysuiTestableContext) mContext;
Jason Monk893f0bd2017-06-01 11:21:14 -040060
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());
Dave Mankoff0cf8dfc2019-09-27 12:46:41 -040068 mDependency.injectMockDependency(AssistManager.class);
Jason Monk893f0bd2017-06-01 11:21:14 -040069 }
70
71 @After
72 public void SysuiTeardown() {
73 InstrumentationRegistry.registerInstance(mRealInstrumentation,
74 InstrumentationRegistry.getArguments());
Dave Mankoffa4a71362019-08-27 14:40:05 -040075 SystemUIFactory.cleanup();
Jason Monk340b0e52017-03-08 14:57:56 -050076 }
77
78 @Override
79 protected SysuiTestableContext getContext() {
80 return new SysuiTestableContext(InstrumentationRegistry.getContext(), mLeakCheck);
81 }
82
83 public void injectLeakCheckedDependencies(Class<?>... cls) {
84 for (Class<?> c : cls) {
85 injectLeakCheckedDependency(c);
86 }
87 }
88
89 public <T> void injectLeakCheckedDependency(Class<T> c) {
90 mDependency.injectTestDependency(c, mLeakCheck.getLeakChecker(c));
91 }
92}