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.support.test.InstrumentationRegistry;
     23 import android.testing.BaseFragmentTest;
     24 
     25 import com.android.systemui.utils.leaks.LeakCheckedTest;
     26 import com.android.systemui.utils.leaks.LeakCheckedTest.SysuiLeakCheck;
     27 
     28 import org.junit.After;
     29 import org.junit.Before;
     30 import org.junit.Rule;
     31 
     32 public abstract class SysuiBaseFragmentTest extends BaseFragmentTest {
     33 
     34     public static final Class<?>[] ALL_SUPPORTED_CLASSES = LeakCheckedTest.ALL_SUPPORTED_CLASSES;
     35 
     36     @Rule
     37     public final SysuiLeakCheck mLeakCheck = new SysuiLeakCheck();
     38 
     39     protected final TestableDependency mDependency = new TestableDependency(mContext);
     40     protected SysuiTestableContext mSysuiContext;
     41     private Instrumentation mRealInstrumentation;
     42 
     43     public SysuiBaseFragmentTest(Class<? extends Fragment> cls) {
     44         super(cls);
     45     }
     46 
     47     @Before
     48     public void SysuiSetup() {
     49         System.setProperty("dexmaker.share_classloader", "true");
     50         SystemUIFactory.createFromConfig(mContext);
     51         // TODO: Figure out another way to give reference to a SysuiTestableContext.
     52         mSysuiContext = (SysuiTestableContext) mContext;
     53 
     54         mRealInstrumentation = InstrumentationRegistry.getInstrumentation();
     55         Instrumentation inst = spy(mRealInstrumentation);
     56         when(inst.getContext()).thenThrow(new RuntimeException(
     57                 "SysUI Tests should use SysuiTestCase#getContext or SysuiTestCase#mContext"));
     58         when(inst.getTargetContext()).thenThrow(new RuntimeException(
     59                 "SysUI Tests should use SysuiTestCase#getContext or SysuiTestCase#mContext"));
     60         InstrumentationRegistry.registerInstance(inst, InstrumentationRegistry.getArguments());
     61     }
     62 
     63     @After
     64     public void SysuiTeardown() {
     65         InstrumentationRegistry.registerInstance(mRealInstrumentation,
     66                 InstrumentationRegistry.getArguments());
     67     }
     68 
     69     @Override
     70     protected SysuiTestableContext getContext() {
     71         return new SysuiTestableContext(InstrumentationRegistry.getContext(), mLeakCheck);
     72     }
     73 
     74     public void injectLeakCheckedDependencies(Class<?>... cls) {
     75         for (Class<?> c : cls) {
     76             injectLeakCheckedDependency(c);
     77         }
     78     }
     79 
     80     public <T> void injectLeakCheckedDependency(Class<T> c) {
     81         mDependency.injectTestDependency(c, mLeakCheck.getLeakChecker(c));
     82     }
     83 }
     84