Convert SysUI tests to use JUnit4

Run tests with android.support.test.runner.AndroidJUnitRunner,
modification to runtest target in separate CL.

No longer inherit from TestCase, stripped unneeded code from
SysuiTestCase, which now assigns mContext via
InstrumentationRegistry.getTargetContext().

Can no longer create Handlers with default constructor,
Looper.myLooper() was never able to receive messages in tests
and it is now enforced that you pass the Looper.getMainLooper().

Change-Id: I13f499175a459cef1a554431911f4b21126e126e
This commit is contained in:
Geoffrey Pitsch
2016-08-26 09:09:39 -04:00
parent c148ba90fb
commit 2c403db659
17 changed files with 252 additions and 75 deletions

View File

@@ -18,13 +18,14 @@ import android.content.Context;
import android.net.INetworkPolicyListener;
import android.net.NetworkPolicyManager;
import android.os.Handler;
import android.os.Looper;
import android.os.RemoteException;
import java.util.ArrayList;
public class DataSaverController {
private final Handler mHandler = new Handler();
private final Handler mHandler = new Handler(Looper.getMainLooper());
private final ArrayList<Listener> mListeners = new ArrayList<>();
private final NetworkPolicyManager mPolicyManager;

View File

@@ -20,6 +20,7 @@ import android.content.Intent;
import android.net.NetworkCapabilities;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.Messenger;
import android.util.Log;
@@ -49,7 +50,7 @@ public class WifiSignalController extends
mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
mWifiTracker = new WifiStatusTracker(mWifiManager);
mHasMobileData = hasMobileData;
Handler handler = new WifiHandler();
Handler handler = new WifiHandler(Looper.getMainLooper());
mWifiChannel = new AsyncChannel();
Messenger wifiMessenger = mWifiManager.getWifiServiceMessenger();
if (wifiMessenger != null) {
@@ -121,6 +122,10 @@ public class WifiSignalController extends
* Handler to receive the data activity on wifi.
*/
private class WifiHandler extends Handler {
WifiHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
switch (msg.what) {

View File

@@ -42,6 +42,7 @@ LOCAL_STATIC_ANDROID_LIBRARIES := \
android-support-v17-leanback
LOCAL_STATIC_JAVA_LIBRARIES := \
android-support-test \
mockito-target \
SystemUI-proto-tags

View File

@@ -35,7 +35,7 @@
android:process=":killable" />
</application>
<instrumentation android:name="android.test.InstrumentationTestRunner"
<instrumentation android:name="android.support.test.runner.AndroidJUnitRunner"
android:targetPackage="com.android.systemui.tests"
android:label="Tests for SystemUI">
</instrumentation>

View File

@@ -15,17 +15,23 @@
*/
package com.android.systemui;
import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.test.AndroidTestCase;
import org.junit.Before;
/**
* Base class that does System UI specific setup.
*/
public class SysuiTestCase extends AndroidTestCase {
@Override
protected void setUp() throws Exception {
super.setUp();
// Mockito stuff.
System.setProperty("dexmaker.dexcache", mContext.getCacheDir().getPath());
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
public class SysuiTestCase {
protected Context mContext;
@Before
public void SysuiSetup() throws Exception {
mContext = InstrumentationRegistry.getTargetContext();
}
protected Context getContext() {
return mContext;
}
}

View File

@@ -16,14 +16,21 @@
package com.android.systemui.phone;
import com.android.systemui.statusbar.phone.DozeParameters.IntInOutMatcher;
import android.test.AndroidTestCase;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.SmallTest;
import com.android.systemui.statusbar.phone.DozeParameters.IntInOutMatcher;
import org.junit.Test;
import org.junit.runner.RunWith;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.fail;
@SmallTest
public class DozeParametersTests extends AndroidTestCase {
@RunWith(AndroidJUnit4.class)
public class DozeParametersTests {
@Test
public void test_inOutMatcher_defaultIn() {
IntInOutMatcher intInOutMatcher = new IntInOutMatcher("*");
@@ -32,6 +39,7 @@ public class DozeParametersTests extends AndroidTestCase {
assertTrue(intInOutMatcher.isIn(0));
}
@Test
public void test_inOutMatcher_defaultOut() {
IntInOutMatcher intInOutMatcher = new IntInOutMatcher("!*");
@@ -40,6 +48,7 @@ public class DozeParametersTests extends AndroidTestCase {
assertFalse(intInOutMatcher.isIn(0));
}
@Test
public void test_inOutMatcher_someIn() {
IntInOutMatcher intInOutMatcher = new IntInOutMatcher("1,2,3,!*");
@@ -51,6 +60,7 @@ public class DozeParametersTests extends AndroidTestCase {
assertFalse(intInOutMatcher.isIn(4));
}
@Test
public void test_inOutMatcher_someOut() {
IntInOutMatcher intInOutMatcher = new IntInOutMatcher("!1,!2,!3,*");
@@ -62,6 +72,7 @@ public class DozeParametersTests extends AndroidTestCase {
assertTrue(intInOutMatcher.isIn(4));
}
@Test
public void test_inOutMatcher_mixed() {
IntInOutMatcher intInOutMatcher = new IntInOutMatcher("!1,2,!3,*");
@@ -73,6 +84,7 @@ public class DozeParametersTests extends AndroidTestCase {
assertTrue(intInOutMatcher.isIn(4));
}
@Test
public void test_inOutMatcher_failEmpty() {
try {
new IntInOutMatcher("");
@@ -82,6 +94,7 @@ public class DozeParametersTests extends AndroidTestCase {
}
}
@Test
public void test_inOutMatcher_failNull() {
try {
new IntInOutMatcher(null);
@@ -91,6 +104,7 @@ public class DozeParametersTests extends AndroidTestCase {
}
}
@Test
public void test_inOutMatcher_failEmptyClause() {
try {
new IntInOutMatcher("!1,*,");
@@ -100,6 +114,7 @@ public class DozeParametersTests extends AndroidTestCase {
}
}
@Test
public void test_inOutMatcher_failDuplicate() {
try {
new IntInOutMatcher("!1,*,!1");
@@ -109,6 +124,7 @@ public class DozeParametersTests extends AndroidTestCase {
}
}
@Test
public void test_inOutMatcher_failDuplicateDefault() {
try {
new IntInOutMatcher("!1,*,*");
@@ -118,6 +134,7 @@ public class DozeParametersTests extends AndroidTestCase {
}
}
@Test
public void test_inOutMatcher_failMalformedNot() {
try {
new IntInOutMatcher("!,*");
@@ -127,6 +144,7 @@ public class DozeParametersTests extends AndroidTestCase {
}
}
@Test
public void test_inOutMatcher_failText() {
try {
new IntInOutMatcher("!abc,*");
@@ -136,6 +154,7 @@ public class DozeParametersTests extends AndroidTestCase {
}
}
@Test
public void test_inOutMatcher_failContradiction() {
try {
new IntInOutMatcher("1,!1,*");
@@ -145,6 +164,7 @@ public class DozeParametersTests extends AndroidTestCase {
}
}
@Test
public void test_inOutMatcher_failContradictionDefault() {
try {
new IntInOutMatcher("1,*,!*");
@@ -154,6 +174,7 @@ public class DozeParametersTests extends AndroidTestCase {
}
}
@Test
public void test_inOutMatcher_failMissingDefault() {
try {
new IntInOutMatcher("1");
@@ -163,4 +184,4 @@ public class DozeParametersTests extends AndroidTestCase {
}
}
}
}

View File

@@ -14,26 +14,32 @@
package com.android.systemui.qs;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.SmallTest;
import android.view.View;
import com.android.systemui.SysuiTestCase;
import com.android.systemui.qs.TouchAnimator.Listener;
import com.android.systemui.SysuiTestCase;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.junit.Test;
import org.mockito.Mockito;
import static junit.framework.Assert.assertEquals;
@SmallTest
@RunWith(AndroidJUnit4.class)
public class TouchAnimatorTests extends SysuiTestCase {
private Listener mTouchListener;
private View mTestView;
@Override
protected void setUp() throws Exception {
super.setUp();
@Before
public void setUp() throws Exception {
mTestView = new View(getContext());
mTouchListener = Mockito.mock(Listener.class);
}
@Test
public void testSetValueFloat() {
TouchAnimator animator = new TouchAnimator.Builder()
.addFloat(mTestView, "x", 0, 50)
@@ -49,6 +55,7 @@ public class TouchAnimatorTests extends SysuiTestCase {
assertEquals(50f, mTestView.getX());
}
@Test
public void testSetValueInt() {
TouchAnimator animator = new TouchAnimator.Builder()
.addInt(mTestView, "top", 0, 50)
@@ -64,6 +71,7 @@ public class TouchAnimatorTests extends SysuiTestCase {
assertEquals(50, mTestView.getTop());
}
@Test
public void testStartDelay() {
TouchAnimator animator = new TouchAnimator.Builder()
.addFloat(mTestView, "x", 0, 50)
@@ -83,6 +91,7 @@ public class TouchAnimatorTests extends SysuiTestCase {
assertEquals(50f, mTestView.getX());
}
@Test
public void testEndDelay() {
TouchAnimator animator = new TouchAnimator.Builder()
.addFloat(mTestView, "x", 0, 50)
@@ -102,6 +111,7 @@ public class TouchAnimatorTests extends SysuiTestCase {
assertEquals(50f, mTestView.getX());
}
@Test
public void testOnAnimationAtStartCallback() {
TouchAnimator animator = new TouchAnimator.Builder()
.setListener(mTouchListener)
@@ -126,6 +136,7 @@ public class TouchAnimatorTests extends SysuiTestCase {
verifyOnAnimationAtStart(3);
}
@Test
public void testOnAnimationAtEndCallback() {
TouchAnimator animator = new TouchAnimator.Builder()
.setListener(mTouchListener)
@@ -150,6 +161,7 @@ public class TouchAnimatorTests extends SysuiTestCase {
verifyOnAnimationAtEnd(3);
}
@Test
public void testOnAnimationStartedCallback() {
TouchAnimator animator = new TouchAnimator.Builder()
.setListener(mTouchListener)

View File

@@ -31,15 +31,24 @@ import android.os.UserHandle;
import android.service.quicksettings.IQSService;
import android.service.quicksettings.IQSTileService;
import android.service.quicksettings.Tile;
import android.test.AndroidTestCase;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.ArraySet;
import android.util.Log;
import com.android.systemui.SysuiTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.fail;
@SmallTest
public class TileLifecycleManagerTests extends AndroidTestCase {
@RunWith(AndroidJUnit4.class)
public class TileLifecycleManagerTests extends SysuiTestCase {
public static final String TILE_UPDATE_BROADCAST = "com.android.systemui.tests.TILE_UPDATE";
public static final String EXTRA_CALLBACK = "callback";
@@ -50,13 +59,12 @@ public class TileLifecycleManagerTests extends AndroidTestCase {
private final ArraySet<String> mCallbacks = new ArraySet<>();
private boolean mBound;
@Override
protected void setUp() throws Exception {
super.setUp();
@Before
public void setUp() throws Exception {
mThread = new HandlerThread("TestThread");
mThread.start();
mHandler = new Handler(mThread.getLooper());
ComponentName component = new ComponentName(mContext, FakeTileService.class);
ComponentName component = new ComponentName(getContext(), FakeTileService.class);
mStateManager = new TileLifecycleManager(mHandler, getContext(),
Mockito.mock(IQSService.class), new Tile(),
new Intent().setComponent(component),
@@ -65,9 +73,8 @@ public class TileLifecycleManagerTests extends AndroidTestCase {
getContext().registerReceiver(mReceiver, new IntentFilter(TILE_UPDATE_BROADCAST));
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
@After
public void tearDown() throws Exception {
if (mBound) {
unbindService();
}
@@ -75,15 +82,18 @@ public class TileLifecycleManagerTests extends AndroidTestCase {
getContext().unregisterReceiver(mReceiver);
}
@Test
public void testSync() {
syncWithHandler();
}
@Test
public void testBind() {
bindService();
waitForCallback("onCreate");
}
@Test
public void testUnbind() {
bindService();
waitForCallback("onCreate");
@@ -91,6 +101,7 @@ public class TileLifecycleManagerTests extends AndroidTestCase {
waitForCallback("onDestroy");
}
@Test
public void testTileServiceCallbacks() {
bindService();
waitForCallback("onCreate");
@@ -109,6 +120,7 @@ public class TileLifecycleManagerTests extends AndroidTestCase {
unbindService();
}
@Test
public void testAddedBeforeBind() {
mStateManager.onTileAdded();
@@ -117,6 +129,7 @@ public class TileLifecycleManagerTests extends AndroidTestCase {
waitForCallback("onTileAdded");
}
@Test
public void testListeningBeforeBind() {
mStateManager.onTileAdded();
mStateManager.onStartListening();
@@ -127,6 +140,7 @@ public class TileLifecycleManagerTests extends AndroidTestCase {
waitForCallback("onStartListening");
}
@Test
public void testClickBeforeBind() {
mStateManager.onTileAdded();
mStateManager.onStartListening();
@@ -139,6 +153,7 @@ public class TileLifecycleManagerTests extends AndroidTestCase {
waitForCallback("onClick");
}
@Test
public void testListeningNotListeningBeforeBind() {
mStateManager.onTileAdded();
mStateManager.onStartListening();
@@ -151,6 +166,7 @@ public class TileLifecycleManagerTests extends AndroidTestCase {
assertFalse(mCallbacks.contains("onStartListening"));
}
@Test
public void testNoClickOfNotListeningAnymore() {
mStateManager.onTileAdded();
mStateManager.onStartListening();
@@ -164,6 +180,7 @@ public class TileLifecycleManagerTests extends AndroidTestCase {
assertFalse(mCallbacks.contains("onClick"));
}
@Test
public void testComponentEnabling() {
mStateManager.onTileAdded();
mStateManager.onStartListening();
@@ -180,6 +197,7 @@ public class TileLifecycleManagerTests extends AndroidTestCase {
waitForCallback("onCreate");
}
@Test
public void testKillProcess() {
mStateManager.onStartListening();
bindService();

View File

@@ -18,12 +18,22 @@ package com.android.systemui.qs.external;
import android.content.ComponentName;
import android.os.Handler;
import android.os.HandlerThread;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.SmallTest;
import com.android.systemui.SysuiTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
@SmallTest
@RunWith(AndroidJUnit4.class)
public class TileServiceManagerTests extends SysuiTestCase {
private TileServices mTileServices;
@@ -32,9 +42,8 @@ public class TileServiceManagerTests extends SysuiTestCase {
private Handler mHandler;
private TileServiceManager mTileServiceManager;
@Override
protected void setUp() throws Exception {
super.setUp();
@Before
public void setUp() throws Exception {
mThread = new HandlerThread("TestThread");
mThread.start();
mHandler = new Handler(mThread.getLooper());
@@ -48,12 +57,12 @@ public class TileServiceManagerTests extends SysuiTestCase {
mTileServiceManager = new TileServiceManager(mTileServices, mHandler, mTileLifecycle);
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
@After
public void tearDown() throws Exception {
mThread.quit();
}
@Test
public void testSetBindRequested() {
// Request binding.
mTileServiceManager.setBindRequested(true);
@@ -72,12 +81,14 @@ public class TileServiceManagerTests extends SysuiTestCase {
assertEquals(Integer.MIN_VALUE, mTileServiceManager.getBindPriority());
}
@Test
public void testPendingClickPriority() {
Mockito.when(mTileLifecycle.hasPendingClick()).thenReturn(true);
mTileServiceManager.calculateBindPriority(0);
assertEquals(Integer.MAX_VALUE, mTileServiceManager.getBindPriority());
}
@Test
public void testBind() {
// Trigger binding requested and allowed.
mTileServiceManager.setBindRequested(true);

View File

@@ -15,30 +15,37 @@
*/
package com.android.systemui.qs.external;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
import android.content.ComponentName;
import android.content.Context;
import android.os.Looper;
import android.service.quicksettings.Tile;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.SmallTest;
import com.android.systemui.SysuiTestCase;
import com.android.systemui.statusbar.phone.QSTileHost;
import com.android.systemui.statusbar.policy.DataSaverController;
import com.android.systemui.statusbar.policy.HotspotController;
import com.android.systemui.statusbar.policy.NetworkController;
import java.util.ArrayList;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import java.util.ArrayList;
@SmallTest
@RunWith(AndroidJUnit4.class)
public class TileServicesTests extends SysuiTestCase {
private static int NUM_FAKES = TileServices.DEFAULT_MAX_BOUND * 2;
private TileServices mTileService;
private ArrayList<TileServiceManager> mManagers;
@Override
protected void setUp() throws Exception {
super.setUp();
@Before
public void setUp() throws Exception {
mManagers = new ArrayList<>();
final NetworkController networkController = Mockito.mock(NetworkController.class);
Mockito.when(networkController.getDataSaverController()).thenReturn(
@@ -47,9 +54,10 @@ public class TileServicesTests extends SysuiTestCase {
networkController, null,
Mockito.mock(HotspotController.class), null,
null, null, null, null, null, null, null, null);
mTileService = new TestTileServices(host, Looper.myLooper());
mTileService = new TestTileServices(host, Looper.getMainLooper());
}
@Test
public void testRecalculateBindAllowance() {
// Add some fake tiles.
for (int i = 0; i < NUM_FAKES; i++) {
@@ -72,6 +80,7 @@ public class TileServicesTests extends SysuiTestCase {
}
}
@Test
public void testSetMemoryPressure() {
testRecalculateBindAllowance();
mTileService.setMemoryPressure(true);
@@ -85,6 +94,7 @@ public class TileServicesTests extends SysuiTestCase {
}
}
@Test
public void testCalcFew() {
for (int i = 0; i < TileServices.DEFAULT_MAX_BOUND - 1; i++) {
mTileService.getTileWrapper(Mockito.mock(CustomTile.class));

View File

@@ -19,30 +19,42 @@ import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.os.FileObserver;
import android.test.ActivityInstrumentationTestCase2;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.LargeTest;
import android.util.Log;
import android.view.KeyEvent;
import com.android.systemui.screenshot.ScreenshotStubActivity;
import java.io.File;
import org.junit.Rule;
import org.junit.runner.RunWith;
import org.junit.Test;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.assertNotNull;
/**
* Functional tests for the global screenshot feature.
*/
@LargeTest
public class ScreenshotTest extends ActivityInstrumentationTestCase2<ScreenshotStubActivity> {
@RunWith(AndroidJUnit4.class)
public class ScreenshotTest {
private static final String LOG_TAG = "ScreenshotTest";
private static final int SCREEN_WAIT_TIME_SEC = 5;
public ScreenshotTest() {
super(ScreenshotStubActivity.class);
}
public ScreenshotTest() {}
@Rule
public ActivityTestRule<ScreenshotStubActivity> mActivityRule =
new ActivityTestRule<>(ScreenshotStubActivity.class);
/**
* A simple test for screenshots that launches an Activity, injects the key event combo
* to trigger the screenshot, and verifies the screenshot was taken successfully.
*/
@Test
public void testScreenshot() throws Exception {
if (true) {
// Disable until this works again.
@@ -50,7 +62,7 @@ public class ScreenshotTest extends ActivityInstrumentationTestCase2<ScreenshotS
}
Log.d(LOG_TAG, "starting testScreenshot");
// launch the activity.
ScreenshotStubActivity activity = getActivity();
ScreenshotStubActivity activity = mActivityRule.getActivity();
assertNotNull(activity);
File screenshotDir = getScreenshotDir();
@@ -105,20 +117,20 @@ public class ScreenshotTest extends ActivityInstrumentationTestCase2<ScreenshotS
* Inject the key sequence to take a screenshot.
*/
private void takeScreenshot() {
getInstrumentation().sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_POWER));
getInstrumentation().sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_VOLUME_DOWN));
InstrumentationRegistry.getInstrumentation()
.sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_POWER));
InstrumentationRegistry.getInstrumentation()
.sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_VOLUME_DOWN));
// the volume down key event will cause the 'volume adjustment' UI to appear in the
// foreground, and steal UI focus
// unfortunately this means the next key event will get directed to the
// 'volume adjustment' UI, instead of this test's activity
// for this reason this test must be signed with platform certificate, to grant this test
// permission to inject key events to another process
getInstrumentation().sendKeySync(new KeyEvent(KeyEvent.ACTION_UP,
KeyEvent.KEYCODE_VOLUME_DOWN));
getInstrumentation().sendKeySync(new KeyEvent(KeyEvent.ACTION_UP,
KeyEvent.KEYCODE_POWER));
InstrumentationRegistry.getInstrumentation()
.sendKeySync(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_VOLUME_DOWN));
InstrumentationRegistry.getInstrumentation()
.sendKeySync(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_POWER));
}
/**

View File

@@ -16,6 +16,7 @@
package com.android.systemui.statusbar.policy;
import android.os.HandlerThread;
import android.support.test.runner.AndroidJUnit4;
import android.telephony.SubscriptionInfo;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;
@@ -23,16 +24,22 @@ import com.android.systemui.R;
import com.android.systemui.statusbar.policy.NetworkController.EmergencyListener;
import com.android.systemui.statusbar.policy.NetworkController.IconState;
import com.android.systemui.statusbar.policy.NetworkController.SignalCallback;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import java.util.ArrayList;
import java.util.List;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.assertEquals;
@SmallTest
public class CallbackHandlerTest extends AndroidTestCase {
@RunWith(AndroidJUnit4.class)
public class CallbackHandlerTest {
private CallbackHandler mHandler;
private HandlerThread mHandlerThread;
@@ -42,10 +49,8 @@ public class CallbackHandlerTest extends AndroidTestCase {
@Mock
private SignalCallback mSignalCallback;
@Override
protected void setUp() throws Exception {
super.setUp();
@Before
public void setUp() throws Exception {
mHandlerThread = new HandlerThread("TestThread");
mHandlerThread.start();
mHandler = new CallbackHandler(mHandlerThread.getLooper());
@@ -55,6 +60,7 @@ public class CallbackHandlerTest extends AndroidTestCase {
mHandler.setListening(mSignalCallback, true);
}
@Test
public void testEmergencyListener() {
mHandler.setEmergencyCallsOnly(true);
waitForCallbacks();
@@ -64,6 +70,7 @@ public class CallbackHandlerTest extends AndroidTestCase {
assertTrue(captor.getValue());
}
@Test
public void testSignalCallback_setWifiIndicators() {
boolean enabled = true;
IconState status = new IconState(true, 0, "");
@@ -91,6 +98,7 @@ public class CallbackHandlerTest extends AndroidTestCase {
assertEquals(description, descArg.getValue());
}
@Test
public void testSignalCallback_setMobileDataIndicators() {
IconState status = new IconState(true, 0, "");
IconState qs = new IconState(true, 1, "");
@@ -133,6 +141,7 @@ public class CallbackHandlerTest extends AndroidTestCase {
}
@SuppressWarnings("unchecked")
@Test
public void testSignalCallback_setSubs() {
List<SubscriptionInfo> subs = new ArrayList<>();
mHandler.setSubs(subs);
@@ -143,6 +152,7 @@ public class CallbackHandlerTest extends AndroidTestCase {
assertTrue(subs == subsArg.getValue());
}
@Test
public void testSignalCallback_setNoSims() {
boolean noSims = true;
mHandler.setNoSims(noSims);
@@ -153,6 +163,7 @@ public class CallbackHandlerTest extends AndroidTestCase {
assertEquals(noSims, (boolean) noSimsArg.getValue());
}
@Test
public void testSignalCallback_setEthernetIndicators() {
IconState state = new IconState(true, R.drawable.stat_sys_ethernet, "Test Description");
mHandler.setEthernetIndicators(state);
@@ -163,6 +174,7 @@ public class CallbackHandlerTest extends AndroidTestCase {
assertEquals(state, iconArg.getValue());
}
@Test
public void testSignalCallback_setIsAirplaneMode() {
IconState state = new IconState(true, R.drawable.stat_sys_airplane_mode, "Test Description");
mHandler.setIsAirplaneMode(state);

View File

@@ -30,11 +30,13 @@ import android.telephony.TelephonyManager;
import android.util.Log;
import com.android.internal.telephony.cdma.EriInfo;
import com.android.settingslib.net.DataUsageController;
import com.android.systemui.SysuiTestCase;
import com.android.systemui.statusbar.policy.NetworkController.IconState;
import com.android.systemui.statusbar.policy.NetworkController.SignalCallback;
import com.android.systemui.statusbar.policy.NetworkControllerImpl.Config;
import com.android.systemui.statusbar.policy.NetworkControllerImpl.SubscriptionDefaults;
import com.android.systemui.SysuiTestCase;
import org.junit.After;
import org.junit.Before;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
@@ -43,6 +45,7 @@ import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import static junit.framework.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -73,10 +76,8 @@ public class NetworkControllerBaseTest extends SysuiTestCase {
private NetworkCapabilities mNetCapabilities;
@Override
protected void setUp() throws Exception {
super.setUp();
@Before
public void setUp() throws Exception {
mMockWm = mock(WifiManager.class);
mMockTm = mock(TelephonyManager.class);
mMockSm = mock(SubscriptionManager.class);
@@ -136,7 +137,7 @@ public class NetworkControllerBaseTest extends SysuiTestCase {
when(mMockCm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE)).thenReturn(false);
NetworkControllerImpl networkControllerNoMobile
= new NetworkControllerImpl(mContext, mMockCm, mMockTm, mMockWm, mMockSm,
mConfig, Looper.getMainLooper(), mCallbackHandler,
mConfig, mContext.getMainLooper(), mCallbackHandler,
mock(AccessPointControllerImpl.class),
mock(DataUsageController.class), mMockSubDefaults);
@@ -146,14 +147,13 @@ public class NetworkControllerBaseTest extends SysuiTestCase {
}
@Override
protected void tearDown() throws Exception {
@After
public void tearDown() throws Exception {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
mNetworkController.dump(null, pw, null);
pw.flush();
Log.d(TAG, sw.toString());
super.tearDown();
}
// 2 Bars 3G GSM.

View File

@@ -2,14 +2,19 @@ package com.android.systemui.statusbar.policy;
import android.net.NetworkCapabilities;
import android.os.Looper;
import android.support.test.runner.AndroidJUnit4;
import android.telephony.TelephonyManager;
import android.test.suitebuilder.annotation.SmallTest;
import com.android.settingslib.net.DataUsageController;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
@SmallTest
@RunWith(AndroidJUnit4.class)
public class NetworkControllerDataTest extends NetworkControllerBaseTest {
@Test
public void test3gDataIcon() {
setupDefaultSignal();
@@ -17,6 +22,7 @@ public class NetworkControllerDataTest extends NetworkControllerBaseTest {
TelephonyIcons.QS_DATA_3G);
}
@Test
public void testRoamingDataIcon() {
setupDefaultSignal();
setGsmRoaming(true);
@@ -29,6 +35,7 @@ public class NetworkControllerDataTest extends NetworkControllerBaseTest {
TelephonyIcons.QS_DATA_R, false, false);
}
@Test
public void test2gDataIcon() {
setupDefaultSignal();
updateDataConnectionState(TelephonyManager.DATA_CONNECTED,
@@ -38,6 +45,7 @@ public class NetworkControllerDataTest extends NetworkControllerBaseTest {
TelephonyIcons.QS_DATA_G);
}
@Test
public void testCdmaDataIcon() {
setupDefaultSignal();
updateDataConnectionState(TelephonyManager.DATA_CONNECTED,
@@ -47,6 +55,7 @@ public class NetworkControllerDataTest extends NetworkControllerBaseTest {
TelephonyIcons.QS_DATA_1X);
}
@Test
public void testEdgeDataIcon() {
setupDefaultSignal();
updateDataConnectionState(TelephonyManager.DATA_CONNECTED,
@@ -56,6 +65,7 @@ public class NetworkControllerDataTest extends NetworkControllerBaseTest {
TelephonyIcons.QS_DATA_E);
}
@Test
public void testLteDataIcon() {
setupDefaultSignal();
updateDataConnectionState(TelephonyManager.DATA_CONNECTED,
@@ -65,6 +75,7 @@ public class NetworkControllerDataTest extends NetworkControllerBaseTest {
TelephonyIcons.QS_DATA_LTE);
}
@Test
public void testHspaDataIcon() {
setupDefaultSignal();
updateDataConnectionState(TelephonyManager.DATA_CONNECTED,
@@ -74,6 +85,7 @@ public class NetworkControllerDataTest extends NetworkControllerBaseTest {
TelephonyIcons.QS_DATA_H);
}
@Test
public void testWfcNoDataIcon() {
setupDefaultSignal();
updateDataConnectionState(TelephonyManager.DATA_CONNECTED,
@@ -82,6 +94,7 @@ public class NetworkControllerDataTest extends NetworkControllerBaseTest {
verifyDataIndicators(0, 0);
}
@Test
public void test4gDataIcon() {
// Switch to showing 4g icon and re-initialize the NetworkController.
mConfig.show4gForLte = true;
@@ -99,6 +112,7 @@ public class NetworkControllerDataTest extends NetworkControllerBaseTest {
TelephonyIcons.QS_DATA_4G);
}
@Test
public void testDataDisabledIcon() {
setupNetworkController();
Mockito.when(mMockTm.getDataEnabled(mSubId)).thenReturn(false);
@@ -110,6 +124,7 @@ public class NetworkControllerDataTest extends NetworkControllerBaseTest {
TelephonyIcons.QS_ICON_DATA_DISABLED);
}
@Test
public void testDataDisabledIcon_UserNotSetup() {
setupNetworkController();
Mockito.when(mMockTm.getDataEnabled(mSubId)).thenReturn(false);
@@ -122,6 +137,7 @@ public class NetworkControllerDataTest extends NetworkControllerBaseTest {
verifyDataIndicators(0, 0);
}
@Test
public void test4gDataIconConfigChange() {
setupDefaultSignal();
updateDataConnectionState(TelephonyManager.DATA_CONNECTED,
@@ -138,6 +154,7 @@ public class NetworkControllerDataTest extends NetworkControllerBaseTest {
TelephonyIcons.QS_DATA_4G);
}
@Test
public void testDataChangeWithoutConnectionState() {
setupDefaultSignal();
updateDataConnectionState(TelephonyManager.DATA_CONNECTED,
@@ -153,6 +170,7 @@ public class NetworkControllerDataTest extends NetworkControllerBaseTest {
TelephonyIcons.QS_DATA_H);
}
@Test
public void testDataActivity() {
setupDefaultSignal();

View File

@@ -1,16 +1,23 @@
package com.android.systemui.statusbar.policy;
import android.net.NetworkCapabilities;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.SmallTest;
import com.android.systemui.statusbar.policy.NetworkController.IconState;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import static junit.framework.Assert.assertEquals;
@SmallTest
@RunWith(AndroidJUnit4.class)
public class NetworkControllerEthernetTest extends NetworkControllerBaseTest {
@Test
public void testEthernetIcons() {
verifyLastEthernetIcon(false, 0);

View File

@@ -15,12 +15,11 @@
*/
package com.android.systemui.statusbar.policy;
import static org.mockito.Mockito.mock;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkCapabilities;
import android.os.Looper;
import android.support.test.runner.AndroidJUnit4;
import android.telephony.ServiceState;
import android.telephony.SignalStrength;
import android.telephony.SubscriptionInfo;
@@ -32,15 +31,24 @@ import com.android.internal.telephony.TelephonyIntents;
import com.android.settingslib.net.DataUsageController;
import com.android.systemui.R;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import java.util.ArrayList;
import java.util.List;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.assertFalse;
import static org.mockito.Mockito.mock;
@SmallTest
@RunWith(AndroidJUnit4.class)
public class NetworkControllerSignalTest extends NetworkControllerBaseTest {
@Test
public void testNoIconWithoutMobile() {
// Turn off mobile network support.
Mockito.when(mMockCm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE)).thenReturn(false);
@@ -54,6 +62,7 @@ public class NetworkControllerSignalTest extends NetworkControllerBaseTest {
verifyLastMobileDataIndicators(false, 0, 0);
}
@Test
public void testNoSimsIconPresent() {
// No Subscriptions.
mNetworkController.mMobileSignalControllers.clear();
@@ -62,6 +71,7 @@ public class NetworkControllerSignalTest extends NetworkControllerBaseTest {
verifyHasNoSims(true);
}
@Test
public void testEmergencyOnly() {
setupDefaultSignal();
mNetworkController.recalculateEmergency();
@@ -72,6 +82,7 @@ public class NetworkControllerSignalTest extends NetworkControllerBaseTest {
verifyEmergencyOnly(true);
}
@Test
public void testEmergencyOnlyNoSubscriptions() {
setupDefaultSignal();
setSubscriptions();
@@ -81,6 +92,7 @@ public class NetworkControllerSignalTest extends NetworkControllerBaseTest {
verifyEmergencyOnly(true);
}
@Test
public void testNoEmengencyNoSubscriptions() {
setupDefaultSignal();
setSubscriptions();
@@ -90,6 +102,7 @@ public class NetworkControllerSignalTest extends NetworkControllerBaseTest {
verifyEmergencyOnly(false);
}
@Test
public void testNoSimlessIconWithoutMobile() {
// Turn off mobile network support.
Mockito.when(mMockCm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE)).thenReturn(false);
@@ -107,6 +120,7 @@ public class NetworkControllerSignalTest extends NetworkControllerBaseTest {
verifyHasNoSims(false);
}
@Test
public void testSignalStrength() {
for (int testStrength = SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
testStrength <= SignalStrength.SIGNAL_STRENGTH_GREAT; testStrength++) {
@@ -123,6 +137,7 @@ public class NetworkControllerSignalTest extends NetworkControllerBaseTest {
}
}
@Test
public void testCdmaSignalStrength() {
for (int testStrength = SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
testStrength <= SignalStrength.SIGNAL_STRENGTH_GREAT; testStrength++) {
@@ -136,6 +151,7 @@ public class NetworkControllerSignalTest extends NetworkControllerBaseTest {
}
}
@Test
public void testSignalRoaming() {
for (int testStrength = SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
testStrength <= SignalStrength.SIGNAL_STRENGTH_GREAT; testStrength++) {
@@ -150,6 +166,7 @@ public class NetworkControllerSignalTest extends NetworkControllerBaseTest {
}
}
@Test
public void testCdmaSignalRoaming() {
for (int testStrength = SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
testStrength <= SignalStrength.SIGNAL_STRENGTH_GREAT; testStrength++) {
@@ -164,6 +181,7 @@ public class NetworkControllerSignalTest extends NetworkControllerBaseTest {
}
}
@Test
public void testQsSignalStrength() {
for (int testStrength = SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
testStrength <= SignalStrength.SIGNAL_STRENGTH_GREAT; testStrength++) {
@@ -176,6 +194,7 @@ public class NetworkControllerSignalTest extends NetworkControllerBaseTest {
}
}
@Test
public void testCdmaQsSignalStrength() {
for (int testStrength = SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
testStrength <= SignalStrength.SIGNAL_STRENGTH_GREAT; testStrength++) {
@@ -189,6 +208,7 @@ public class NetworkControllerSignalTest extends NetworkControllerBaseTest {
}
}
@Test
public void testNoBangWithWifi() {
setupDefaultSignal();
setConnectivity(mMobileSignalController.mTransportType, false, false);
@@ -199,6 +219,7 @@ public class NetworkControllerSignalTest extends NetworkControllerBaseTest {
// Some tests of actual NetworkController code, just internals not display stuff
// TODO: Put this somewhere else, maybe in its own file.
@Test
public void testHasCorrectMobileControllers() {
int[] testSubscriptions = new int[] { 1, 5, 3 };
int notTestSubscription = 0;
@@ -225,6 +246,7 @@ public class NetworkControllerSignalTest extends NetworkControllerBaseTest {
assertFalse(mNetworkController.hasCorrectMobileControllers(subscriptions));
}
@Test
public void testSetCurrentSubscriptions() {
// We will not add one controller to make sure it gets created.
int indexToSkipController = 0;
@@ -277,6 +299,7 @@ public class NetworkControllerSignalTest extends NetworkControllerBaseTest {
}
}
@Test
public void testHistorySize() {
// Verify valid history size, otherwise it gits printed out the wrong order and whatnot.
assertEquals(0, SignalController.HISTORY_SIZE & (SignalController.HISTORY_SIZE - 1));
@@ -289,6 +312,7 @@ public class NetworkControllerSignalTest extends NetworkControllerBaseTest {
setCdmaRoaming(false);
}
@Test
public void testOnReceive_stringsUpdatedAction_spn() {
String expectedMNetworkName = "Test";
Intent intent = createStringsUpdatedIntent(true /* showSpn */,
@@ -301,6 +325,7 @@ public class NetworkControllerSignalTest extends NetworkControllerBaseTest {
assertNetworkNameEquals(expectedMNetworkName);
}
@Test
public void testOnReceive_stringsUpdatedAction_plmn() {
String expectedMNetworkName = "Test";
@@ -314,6 +339,7 @@ public class NetworkControllerSignalTest extends NetworkControllerBaseTest {
assertNetworkNameEquals(expectedMNetworkName);
}
@Test
public void testOnReceive_stringsUpdatedAction_bothFalse() {
Intent intent = createStringsUpdatedIntent(false /* showSpn */,
"Irrelevant" /* spn */,
@@ -328,6 +354,7 @@ public class NetworkControllerSignalTest extends NetworkControllerBaseTest {
assertNetworkNameEquals(defaultNetworkName);
}
@Test
public void testOnReceive_stringsUpdatedAction_bothTrueAndNull() {
Intent intent = createStringsUpdatedIntent(true /* showSpn */,
null /* spn */,
@@ -341,6 +368,7 @@ public class NetworkControllerSignalTest extends NetworkControllerBaseTest {
assertNetworkNameEquals(defaultNetworkName);
}
@Test
public void testOnReceive_stringsUpdatedAction_bothTrueAndNonNull() {
String spn = "Test1";
String plmn = "Test2";
@@ -374,6 +402,7 @@ public class NetworkControllerSignalTest extends NetworkControllerBaseTest {
return intent;
}
@Test
public void testOnUpdateDataActivity_dataIn() {
setupDefaultSignal();
@@ -387,6 +416,7 @@ public class NetworkControllerSignalTest extends NetworkControllerBaseTest {
}
@Test
public void testOnUpdateDataActivity_dataOut() {
setupDefaultSignal();
@@ -400,6 +430,7 @@ public class NetworkControllerSignalTest extends NetworkControllerBaseTest {
}
@Test
public void testOnUpdateDataActivity_dataInOut() {
setupDefaultSignal();
@@ -413,6 +444,7 @@ public class NetworkControllerSignalTest extends NetworkControllerBaseTest {
}
@Test
public void testOnUpdateDataActivity_dataActivityNone() {
setupDefaultSignal();
@@ -426,6 +458,7 @@ public class NetworkControllerSignalTest extends NetworkControllerBaseTest {
}
@Test
public void testCarrierNetworkChange_carrierNetworkChange() {
int strength = SignalStrength.SIGNAL_STRENGTH_GREAT;

View File

@@ -5,19 +5,26 @@ import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.SmallTest;
import com.android.systemui.statusbar.policy.NetworkController.IconState;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import static junit.framework.Assert.assertEquals;
@SmallTest
@RunWith(AndroidJUnit4.class)
public class NetworkControllerWifiTest extends NetworkControllerBaseTest {
// These match the constants in WifiManager and need to be kept up to date.
private static final int MIN_RSSI = -100;
private static final int MAX_RSSI = -55;
@Test
public void testWifiIcon() {
String testSsid = "Test SSID";
setWifiEnabled(true);
@@ -36,6 +43,7 @@ public class NetworkControllerWifiTest extends NetworkControllerBaseTest {
}
}
@Test
public void testQsWifiIcon() {
String testSsid = "Test SSID";
@@ -58,6 +66,7 @@ public class NetworkControllerWifiTest extends NetworkControllerBaseTest {
}
}
@Test
public void testQsDataDirection() {
// Setup normal connection
String testSsid = "Test SSID";
@@ -79,6 +88,7 @@ public class NetworkControllerWifiTest extends NetworkControllerBaseTest {
verifyLastQsDataDirection(true, true);
}
@Test
public void testRoamingIconDuringWifi() {
// Setup normal connection
String testSsid = "Test SSID";