NetworkChangeNotifierTest.java revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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. 4 5package org.chromium.net; 6 7import android.content.Context; 8import android.content.Intent; 9import android.net.ConnectivityManager; 10import android.telephony.TelephonyManager; 11import android.test.InstrumentationTestCase; 12import android.test.UiThreadTest; 13import android.test.suitebuilder.annotation.MediumTest; 14 15import org.chromium.base.ActivityStatus; 16import org.chromium.base.test.util.Feature; 17 18public class NetworkChangeNotifierTest extends InstrumentationTestCase { 19 /** 20 * Listens for alerts fired by the NetworkChangeNotifier when network status changes. 21 */ 22 private static class NetworkChangeNotifierTestObserver 23 implements NetworkChangeNotifier.ConnectionTypeObserver { 24 private boolean mReceivedNotification = false; 25 26 @Override 27 public void onConnectionTypeChanged(int connectionType) { 28 mReceivedNotification = true; 29 } 30 31 public boolean hasReceivedNotification() { 32 return mReceivedNotification; 33 } 34 35 public void resetHasReceivedNotification() { 36 mReceivedNotification = false; 37 } 38 } 39 40 /** 41 * Mocks out calls to the ConnectivityManager. 42 */ 43 class MockConnectivityManagerDelegate 44 extends NetworkChangeNotifierAutoDetect.ConnectivityManagerDelegate { 45 private boolean mActiveNetworkExists; 46 private int mNetworkType; 47 private int mNetworkSubtype; 48 49 @Override 50 boolean activeNetworkExists() { 51 return mActiveNetworkExists; 52 } 53 54 @Override 55 boolean isConnected() { 56 return getNetworkType() != NetworkChangeNotifier.CONNECTION_NONE; 57 } 58 59 void setActiveNetworkExists(boolean networkExists) { 60 mActiveNetworkExists = networkExists; 61 } 62 63 @Override 64 int getNetworkType() { 65 return mNetworkType; 66 } 67 68 void setNetworkType(int networkType) { 69 mNetworkType = networkType; 70 } 71 72 @Override 73 int getNetworkSubtype() { 74 return mNetworkSubtype; 75 } 76 77 void setNetworkSubtype(int networkSubtype) { 78 mNetworkSubtype = networkSubtype; 79 } 80 } 81 82 /** 83 * Tests that when Chrome gets an intent indicating a change in network connectivity, it sends a 84 * notification to Java observers. 85 */ 86 @UiThreadTest 87 @MediumTest 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); 93 94 NetworkChangeNotifier.setAutoDetectConnectivityState(true); 95 NetworkChangeNotifierAutoDetect receiver = NetworkChangeNotifier.getAutoDetectorForTest(); 96 assertTrue(receiver != null); 97 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); 104 105 // Initialize the NetworkChangeNotifier with a connection. 106 Intent connectivityIntent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION); 107 receiver.onReceive(getInstrumentation().getTargetContext(), connectivityIntent); 108 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()); 114 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()); 121 122 observer.resetHasReceivedNotification(); 123 // Pretend we got moved to the background. 124 receiver.onActivityStateChange(ActivityStatus.PAUSED); 125 // Change the state. 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()); 133 } 134} 135