Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 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 | |
Jason Monk | 893f0bd | 2017-06-01 11:21:14 -0400 | [diff] [blame] | 17 | import static org.mockito.Mockito.spy; |
| 18 | import static org.mockito.Mockito.when; |
| 19 | |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 20 | import android.app.Fragment; |
Jason Monk | 893f0bd | 2017-06-01 11:21:14 -0400 | [diff] [blame] | 21 | import android.app.Instrumentation; |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 22 | import android.testing.BaseFragmentTest; |
Adrian Roos | 3150dbf | 2018-03-28 18:06:52 +0200 | [diff] [blame] | 23 | import android.testing.DexmakerShareClassLoaderRule; |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 24 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 25 | import androidx.test.InstrumentationRegistry; |
| 26 | |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 27 | import com.android.systemui.utils.leaks.LeakCheckedTest; |
| 28 | import com.android.systemui.utils.leaks.LeakCheckedTest.SysuiLeakCheck; |
| 29 | |
Jason Monk | 893f0bd | 2017-06-01 11:21:14 -0400 | [diff] [blame] | 30 | import org.junit.After; |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 31 | import org.junit.Before; |
| 32 | import org.junit.Rule; |
| 33 | |
| 34 | public 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 Roos | 3150dbf | 2018-03-28 18:06:52 +0200 | [diff] [blame] | 41 | @Rule |
| 42 | public final DexmakerShareClassLoaderRule mDexmakerShareClassLoaderRule = |
| 43 | new DexmakerShareClassLoaderRule(); |
| 44 | |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 45 | protected final TestableDependency mDependency = new TestableDependency(mContext); |
| 46 | protected SysuiTestableContext mSysuiContext; |
Jason Monk | 893f0bd | 2017-06-01 11:21:14 -0400 | [diff] [blame] | 47 | private Instrumentation mRealInstrumentation; |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 48 | |
| 49 | public SysuiBaseFragmentTest(Class<? extends Fragment> cls) { |
| 50 | super(cls); |
| 51 | } |
| 52 | |
| 53 | @Before |
| 54 | public void SysuiSetup() { |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 55 | SystemUIFactory.createFromConfig(mContext); |
| 56 | // TODO: Figure out another way to give reference to a SysuiTestableContext. |
| 57 | mSysuiContext = (SysuiTestableContext) mContext; |
Jason Monk | 893f0bd | 2017-06-01 11:21:14 -0400 | [diff] [blame] | 58 | |
| 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 Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 72 | } |
| 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 | } |