Merge "Add checks against incorrect context use in sysui tests" into oc-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
227e65d818
@@ -14,13 +14,18 @@
|
|||||||
|
|
||||||
package com.android.systemui;
|
package com.android.systemui;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.spy;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
import android.app.Fragment;
|
import android.app.Fragment;
|
||||||
|
import android.app.Instrumentation;
|
||||||
import android.support.test.InstrumentationRegistry;
|
import android.support.test.InstrumentationRegistry;
|
||||||
import android.testing.BaseFragmentTest;
|
import android.testing.BaseFragmentTest;
|
||||||
|
|
||||||
import com.android.systemui.utils.leaks.LeakCheckedTest;
|
import com.android.systemui.utils.leaks.LeakCheckedTest;
|
||||||
import com.android.systemui.utils.leaks.LeakCheckedTest.SysuiLeakCheck;
|
import com.android.systemui.utils.leaks.LeakCheckedTest.SysuiLeakCheck;
|
||||||
|
|
||||||
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
|
|
||||||
@@ -33,6 +38,7 @@ public abstract class SysuiBaseFragmentTest extends BaseFragmentTest {
|
|||||||
|
|
||||||
protected final TestableDependency mDependency = new TestableDependency(mContext);
|
protected final TestableDependency mDependency = new TestableDependency(mContext);
|
||||||
protected SysuiTestableContext mSysuiContext;
|
protected SysuiTestableContext mSysuiContext;
|
||||||
|
private Instrumentation mRealInstrumentation;
|
||||||
|
|
||||||
public SysuiBaseFragmentTest(Class<? extends Fragment> cls) {
|
public SysuiBaseFragmentTest(Class<? extends Fragment> cls) {
|
||||||
super(cls);
|
super(cls);
|
||||||
@@ -44,6 +50,20 @@ public abstract class SysuiBaseFragmentTest extends BaseFragmentTest {
|
|||||||
SystemUIFactory.createFromConfig(mContext);
|
SystemUIFactory.createFromConfig(mContext);
|
||||||
// TODO: Figure out another way to give reference to a SysuiTestableContext.
|
// TODO: Figure out another way to give reference to a SysuiTestableContext.
|
||||||
mSysuiContext = (SysuiTestableContext) mContext;
|
mSysuiContext = (SysuiTestableContext) mContext;
|
||||||
|
|
||||||
|
mRealInstrumentation = InstrumentationRegistry.getInstrumentation();
|
||||||
|
Instrumentation inst = spy(mRealInstrumentation);
|
||||||
|
when(inst.getContext()).thenThrow(new RuntimeException(
|
||||||
|
"SysUI Tests should use SysuiTestCase#getContext or SysuiTestCase#mContext"));
|
||||||
|
when(inst.getTargetContext()).thenThrow(new RuntimeException(
|
||||||
|
"SysUI Tests should use SysuiTestCase#getContext or SysuiTestCase#mContext"));
|
||||||
|
InstrumentationRegistry.registerInstance(inst, InstrumentationRegistry.getArguments());
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void SysuiTeardown() {
|
||||||
|
InstrumentationRegistry.registerInstance(mRealInstrumentation,
|
||||||
|
InstrumentationRegistry.getArguments());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -15,6 +15,10 @@
|
|||||||
*/
|
*/
|
||||||
package com.android.systemui;
|
package com.android.systemui;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.spy;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import android.app.Instrumentation;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.Looper;
|
import android.os.Looper;
|
||||||
@@ -24,6 +28,7 @@ import android.support.test.filters.SmallTest;
|
|||||||
import android.testing.LeakCheck;
|
import android.testing.LeakCheck;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
|
|
||||||
@@ -42,11 +47,26 @@ public abstract class SysuiTestCase {
|
|||||||
public SysuiTestableContext mContext = new SysuiTestableContext(
|
public SysuiTestableContext mContext = new SysuiTestableContext(
|
||||||
InstrumentationRegistry.getContext(), getLeakCheck());
|
InstrumentationRegistry.getContext(), getLeakCheck());
|
||||||
public TestableDependency mDependency = new TestableDependency(mContext);
|
public TestableDependency mDependency = new TestableDependency(mContext);
|
||||||
|
private Instrumentation mRealInstrumentation;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void SysuiSetup() throws Exception {
|
public void SysuiSetup() throws Exception {
|
||||||
System.setProperty("dexmaker.share_classloader", "true");
|
System.setProperty("dexmaker.share_classloader", "true");
|
||||||
SystemUIFactory.createFromConfig(mContext);
|
SystemUIFactory.createFromConfig(mContext);
|
||||||
|
|
||||||
|
mRealInstrumentation = InstrumentationRegistry.getInstrumentation();
|
||||||
|
Instrumentation inst = spy(mRealInstrumentation);
|
||||||
|
when(inst.getContext()).thenThrow(new RuntimeException(
|
||||||
|
"SysUI Tests should use SysuiTestCase#getContext or SysuiTestCase#mContext"));
|
||||||
|
when(inst.getTargetContext()).thenThrow(new RuntimeException(
|
||||||
|
"SysUI Tests should use SysuiTestCase#getContext or SysuiTestCase#mContext"));
|
||||||
|
InstrumentationRegistry.registerInstance(inst, InstrumentationRegistry.getArguments());
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void SysuiTeardown() {
|
||||||
|
InstrumentationRegistry.registerInstance(mRealInstrumentation,
|
||||||
|
InstrumentationRegistry.getArguments());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected LeakCheck getLeakCheck() {
|
protected LeakCheck getLeakCheck() {
|
||||||
|
|||||||
@@ -41,9 +41,7 @@ public class WakeLockTest extends SysuiTestCase {
|
|||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
Context context = InstrumentationRegistry.getContext();
|
mInner = WakeLock.createPartialInner(mContext, WakeLockTest.class.getName());
|
||||||
|
|
||||||
mInner = WakeLock.createPartialInner(context, WakeLockTest.class.getName());
|
|
||||||
mWakeLock = WakeLock.wrap(mInner);
|
mWakeLock = WakeLock.wrap(mInner);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -31,12 +31,10 @@ public class ViewUtils {
|
|||||||
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,
|
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,
|
||||||
LayoutParams.TYPE_APPLICATION_OVERLAY,
|
LayoutParams.TYPE_APPLICATION_OVERLAY,
|
||||||
0, PixelFormat.TRANSLUCENT);
|
0, PixelFormat.TRANSLUCENT);
|
||||||
InstrumentationRegistry.getContext()
|
view.getContext().getSystemService(WindowManager.class).addView(view, lp);
|
||||||
.getSystemService(WindowManager.class).addView(view, lp);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void detachView(View view) {
|
public static void detachView(View view) {
|
||||||
InstrumentationRegistry.getContext()
|
view.getContext().getSystemService(WindowManager.class).removeViewImmediate(view);
|
||||||
.getSystemService(WindowManager.class).removeViewImmediate(view);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user