1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.net;
7 import android.content.Context;
8 import android.content.Intent;
9 import android.net.ConnectivityManager;
10 import android.telephony.TelephonyManager;
11 import android.test.InstrumentationTestCase;
12 import android.test.UiThreadTest;
13 import android.test.suitebuilder.annotation.MediumTest;
15 import org.chromium.base.ActivityStatus;
16 import org.chromium.base.test.util.Feature;
18 public class NetworkChangeNotifierTest extends InstrumentationTestCase {
20 * Listens for alerts fired by the NetworkChangeNotifier when network status changes.
22 private static class NetworkChangeNotifierTestObserver
23 implements NetworkChangeNotifier.ConnectionTypeObserver {
24 private boolean mReceivedNotification = false;
27 public void onConnectionTypeChanged(int connectionType) {
28 mReceivedNotification = true;
31 public boolean hasReceivedNotification() {
32 return mReceivedNotification;
35 public void resetHasReceivedNotification() {
36 mReceivedNotification = false;
41 * Mocks out calls to the ConnectivityManager.
43 class MockConnectivityManagerDelegate
44 extends NetworkChangeNotifierAutoDetect.ConnectivityManagerDelegate {
45 private boolean mActiveNetworkExists;
46 private int mNetworkType;
47 private int mNetworkSubtype;
50 boolean activeNetworkExists() {
51 return mActiveNetworkExists;
55 boolean isConnected() {
56 return getNetworkType() != NetworkChangeNotifier.CONNECTION_NONE;
59 void setActiveNetworkExists(boolean networkExists) {
60 mActiveNetworkExists = networkExists;
64 int getNetworkType() {
68 void setNetworkType(int networkType) {
69 mNetworkType = networkType;
73 int getNetworkSubtype() {
74 return mNetworkSubtype;
77 void setNetworkSubtype(int networkSubtype) {
78 mNetworkSubtype = networkSubtype;
83 * Tests that when Chrome gets an intent indicating a change in network connectivity, it sends a
84 * notification to Java observers.
88 @Feature({"Android-AppBase"})
89 public void testNetworkChangeNotifierJavaObservers() throws InterruptedException {
90 // Create a new notifier that doesn't have a native-side counterpart.
91 Context context = getInstrumentation().getTargetContext();
92 NetworkChangeNotifier.resetInstanceForTests(context);
94 NetworkChangeNotifier.setAutoDetectConnectivityState(true);
95 NetworkChangeNotifierAutoDetect receiver = NetworkChangeNotifier.getAutoDetectorForTest();
96 assertTrue(receiver != null);
98 MockConnectivityManagerDelegate connectivityDelegate =
99 new MockConnectivityManagerDelegate();
100 connectivityDelegate.setActiveNetworkExists(true);
101 connectivityDelegate.setNetworkType(NetworkChangeNotifier.CONNECTION_UNKNOWN);
102 connectivityDelegate.setNetworkSubtype(TelephonyManager.NETWORK_TYPE_UNKNOWN);
103 receiver.setConnectivityManagerDelegateForTests(connectivityDelegate);
105 // Initialize the NetworkChangeNotifier with a connection.
106 Intent connectivityIntent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
107 receiver.onReceive(getInstrumentation().getTargetContext(), connectivityIntent);
109 // We shouldn't be re-notified if the connection hasn't actually changed.
110 NetworkChangeNotifierTestObserver observer = new NetworkChangeNotifierTestObserver();
111 NetworkChangeNotifier.addConnectionTypeObserver(observer);
112 receiver.onReceive(getInstrumentation().getTargetContext(), connectivityIntent);
113 assertFalse(observer.hasReceivedNotification());
115 // Mimic that connectivity has been lost and ensure that Chrome notifies our observer.
116 connectivityDelegate.setActiveNetworkExists(false);
117 connectivityDelegate.setNetworkType(NetworkChangeNotifier.CONNECTION_NONE);
118 Intent noConnectivityIntent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
119 receiver.onReceive(getInstrumentation().getTargetContext(), noConnectivityIntent);
120 assertTrue(observer.hasReceivedNotification());
122 observer.resetHasReceivedNotification();
123 // Pretend we got moved to the background.
124 receiver.onActivityStateChange(ActivityStatus.PAUSED);
126 connectivityDelegate.setActiveNetworkExists(true);
127 connectivityDelegate.setNetworkType(NetworkChangeNotifier.CONNECTION_WIFI);
128 // The NetworkChangeNotifierAutoDetect doesn't receive any notification while we are in the
129 // background, but when we get back to the foreground the state changed should be detected
130 // and a notification sent.
131 receiver.onActivityStateChange(ActivityStatus.RESUMED);
132 assertTrue(observer.hasReceivedNotification());