Home | History | Annotate | Download | only in systemui
      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.testing.BaseFragmentTest;
     23 import android.testing.DexmakerShareClassLoaderRule;
     24 
     25 import androidx.test.InstrumentationRegistry;
     26 
     27 import com.android.systemui.utils.leaks.LeakCheckedTest;
     28 import com.android.systemui.utils.leaks.LeakCheckedTest.SysuiLeakCheck;
     29 
     30 import org.junit.After;
     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 
     41     @Rule
     42     public final DexmakerShareClassLoaderRule mDexmakerShareClassLoaderRule =
     43             new DexmakerShareClassLoaderRule();
     44 
     45     protected final TestableDependency mDependency = new TestableDependency(mContext);
     46     protected SysuiTestableContext mSysuiContext;
     47     private Instrumentation mRealInstrumentation;
     48 
     49     public SysuiBaseFragmentTest(Class<? extends Fragment> cls) {
     50         super(cls);
     51     }
     52 
     53     @Before
     54     public void SysuiSetup() {
     55         SystemUIFactory.createFromConfig(mContext);
     56         // TODO: Figure out another way to give reference to a SysuiTestableContext.
     57         mSysuiContext = (SysuiTestableContext) mContext;
     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());
     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 }
     89