Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(103)

Unified Diff: webrtc/sdk/android/instrumentationtests/src/org/webrtc/NetworkMonitorTest.java

Issue 2627043002: Update Android instrumentation tests to use JUnit4. (Closed)
Patch Set: Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: webrtc/sdk/android/instrumentationtests/src/org/webrtc/NetworkMonitorTest.java
diff --git a/webrtc/sdk/android/instrumentationtests/src/org/webrtc/NetworkMonitorTest.java b/webrtc/sdk/android/instrumentationtests/src/org/webrtc/NetworkMonitorTest.java
index 9e732c469924305b41fc64742a9ac08aa993b620..b70bb16b6812623985f814636858b8eaab7ac340 100644
--- a/webrtc/sdk/android/instrumentationtests/src/org/webrtc/NetworkMonitorTest.java
+++ b/webrtc/sdk/android/instrumentationtests/src/org/webrtc/NetworkMonitorTest.java
@@ -7,8 +7,12 @@
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
+
package org.webrtc;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
import static org.webrtc.NetworkMonitorAutoDetect.ConnectionType;
import static org.webrtc.NetworkMonitorAutoDetect.ConnectivityManagerDelegate;
import static org.webrtc.NetworkMonitorAutoDetect.INVALID_NET_ID;
@@ -24,16 +28,25 @@ import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
-import android.test.ActivityTestCase;
-import android.test.UiThreadTest;
-import android.test.suitebuilder.annotation.MediumTest;
-import android.test.suitebuilder.annotation.SmallTest;
+import android.support.test.InstrumentationRegistry;
+import android.support.test.annotation.UiThreadTest;
+import android.support.test.filters.MediumTest;
+import android.support.test.filters.SmallTest;
+import android.support.test.rule.UiThreadTestRule;
+import org.chromium.base.test.BaseJUnit4ClassRunner;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
/**
* Tests for org.webrtc.NetworkMonitor.
*/
@SuppressLint("NewApi")
-public class NetworkMonitorTest extends ActivityTestCase {
+@RunWith(BaseJUnit4ClassRunner.class)
+public class NetworkMonitorTest {
+ @Rule public UiThreadTestRule uiThreadTestRule = new UiThreadTestRule();
+
/**
* Listens for alerts fired by the NetworkMonitor when network status changes.
*/
@@ -147,7 +160,7 @@ public class NetworkMonitorTest extends ActivityTestCase {
* Helper method to create a network monitor and delegates for testing.
*/
private void createTestMonitor() {
- Context context = getInstrumentation().getTargetContext();
+ Context context = InstrumentationRegistry.getTargetContext();
NetworkMonitor.resetInstanceForTests(context);
NetworkMonitor.setAutoDetectConnectivityState(true);
receiver = NetworkMonitor.getAutoDetectorForTest();
@@ -167,23 +180,19 @@ public class NetworkMonitorTest extends ActivityTestCase {
return receiver.getConnectionType(networkState);
}
- @Override
- protected void setUp() throws Exception {
- super.setUp();
- getUiThreadHandler().post(new Runnable() {
- public void run() {
- createTestMonitor();
- }
- });
+ @Before
+ public void setUp() {
+ createTestMonitor();
}
/**
* Tests that the receiver registers for connectivity intents during construction.
*/
+ @Test
@UiThreadTest
@SmallTest
public void testNetworkMonitorRegistersInConstructor() throws InterruptedException {
- Context context = getInstrumentation().getTargetContext();
+ Context context = InstrumentationRegistry.getTargetContext();
NetworkMonitorAutoDetect.Observer observer = new TestNetworkMonitorAutoDetectObserver();
@@ -196,44 +205,45 @@ public class NetworkMonitorTest extends ActivityTestCase {
* Tests that when there is an intent indicating a change in network connectivity, it sends a
* notification to Java observers.
*/
+ @Test
@UiThreadTest
@MediumTest
public void testNetworkMonitorJavaObservers() throws InterruptedException {
// Initialize the NetworkMonitor with a connection.
Intent connectivityIntent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
- receiver.onReceive(getInstrumentation().getTargetContext(), connectivityIntent);
+ receiver.onReceive(InstrumentationRegistry.getTargetContext(), connectivityIntent);
// We shouldn't be re-notified if the connection hasn't actually changed.
NetworkMonitorTestObserver observer = new NetworkMonitorTestObserver();
NetworkMonitor.addNetworkObserver(observer);
- receiver.onReceive(getInstrumentation().getTargetContext(), connectivityIntent);
+ receiver.onReceive(InstrumentationRegistry.getTargetContext(), connectivityIntent);
assertFalse(observer.hasReceivedNotification());
// We shouldn't be notified if we're connected to non-Wifi and the Wifi SSID changes.
wifiDelegate.setWifiSSID("bar");
- receiver.onReceive(getInstrumentation().getTargetContext(), connectivityIntent);
+ receiver.onReceive(InstrumentationRegistry.getTargetContext(), connectivityIntent);
assertFalse(observer.hasReceivedNotification());
// We should be notified when we change to Wifi.
connectivityDelegate.setNetworkType(ConnectivityManager.TYPE_WIFI);
- receiver.onReceive(getInstrumentation().getTargetContext(), connectivityIntent);
+ receiver.onReceive(InstrumentationRegistry.getTargetContext(), connectivityIntent);
assertTrue(observer.hasReceivedNotification());
observer.resetHasReceivedNotification();
// We should be notified when the Wifi SSID changes.
wifiDelegate.setWifiSSID("foo");
- receiver.onReceive(getInstrumentation().getTargetContext(), connectivityIntent);
+ receiver.onReceive(InstrumentationRegistry.getTargetContext(), connectivityIntent);
assertTrue(observer.hasReceivedNotification());
observer.resetHasReceivedNotification();
// We shouldn't be re-notified if the Wifi SSID hasn't actually changed.
- receiver.onReceive(getInstrumentation().getTargetContext(), connectivityIntent);
+ receiver.onReceive(InstrumentationRegistry.getTargetContext(), connectivityIntent);
assertFalse(observer.hasReceivedNotification());
// Mimic that connectivity has been lost and ensure that the observer gets the notification.
connectivityDelegate.setActiveNetworkExists(false);
Intent noConnectivityIntent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
- receiver.onReceive(getInstrumentation().getTargetContext(), noConnectivityIntent);
+ receiver.onReceive(InstrumentationRegistry.getTargetContext(), noConnectivityIntent);
assertTrue(observer.hasReceivedNotification());
}
@@ -242,11 +252,12 @@ public class NetworkMonitorTest extends ActivityTestCase {
* active network connections so it cannot usefully check results, but it can at least check
* that the functions don't crash.
*/
+ @Test
@UiThreadTest
@SmallTest
public void testConnectivityManagerDelegateDoesNotCrash() {
ConnectivityManagerDelegate delegate =
- new ConnectivityManagerDelegate(getInstrumentation().getTargetContext());
+ new ConnectivityManagerDelegate(InstrumentationRegistry.getTargetContext());
delegate.getNetworkState();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Network[] networks = delegate.getAllNetworks();
@@ -263,12 +274,13 @@ public class NetworkMonitorTest extends ActivityTestCase {
* on having any active network connections so it cannot usefully check results, but it can at
* least check that the functions don't crash.
*/
+ @Test
@UiThreadTest
@SmallTest
public void testQueryableAPIsDoNotCrash() {
NetworkMonitorAutoDetect.Observer observer = new TestNetworkMonitorAutoDetectObserver();
NetworkMonitorAutoDetect ncn =
- new NetworkMonitorAutoDetect(observer, getInstrumentation().getTargetContext());
+ new NetworkMonitorAutoDetect(observer, InstrumentationRegistry.getTargetContext());
ncn.getDefaultNetId();
}
}

Powered by Google App Engine
This is Rietveld 408576698