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

Side by Side Diff: net/android/javatests/src/org/chromium/net/NetworkChangeNotifierTest.java

Issue 1358163004: [Android] Introduce RegistrationPolicy for NetworkChangeNotifier. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: clean-up and rebase Created 5 years, 2 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 unified diff | Download patch
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.net; 5 package org.chromium.net;
6 6
7 import android.content.Context; 7 import android.content.Context;
8 import android.content.Intent; 8 import android.content.Intent;
9 import android.net.ConnectivityManager; 9 import android.net.ConnectivityManager;
10 import android.net.wifi.WifiManager; 10 import android.net.wifi.WifiManager;
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 164
165 NetworkChangeNotifierAutoDetect.Observer observer = 165 NetworkChangeNotifierAutoDetect.Observer observer =
166 new NetworkChangeNotifierAutoDetect.Observer() { 166 new NetworkChangeNotifierAutoDetect.Observer() {
167 @Override 167 @Override
168 public void onConnectionTypeChanged(int newConnectionType) {} 168 public void onConnectionTypeChanged(int newConnectionType) {}
169 @Override 169 @Override
170 public void onMaxBandwidthChanged(double maxBandwidthMbps) {} 170 public void onMaxBandwidthChanged(double maxBandwidthMbps) {}
171 }; 171 };
172 172
173 NetworkChangeNotifierAutoDetect receiver = new NetworkChangeNotifierAuto Detect( 173 NetworkChangeNotifierAutoDetect receiver = new NetworkChangeNotifierAuto Detect(
174 observer, context, false /* always watch for changes */) { 174 observer, context, new RegistrationPolicyApplicationStatus() {
175 @Override 175 @Override
176 int getApplicationState() { 176 int getApplicationState() {
177 return ApplicationState.HAS_RUNNING_ACTIVITIES; 177 return ApplicationState.HAS_RUNNING_ACTIVITIES;
178 } 178 }
179 }; 179 });
180 180
181 assertTrue(receiver.isReceiverRegisteredForTesting()); 181 assertTrue(receiver.isReceiverRegisteredForTesting());
182 } 182 }
183 183
184 /** 184 /**
185 * Tests that the receiver toggles registration for connectivity intents bas ed on activity 185 * Tests that the receiver toggles registration for connectivity intents bas ed on activity
186 * state. 186 * state.
187 */ 187 */
188 @UiThreadTest 188 @UiThreadTest
189 @MediumTest 189 @MediumTest
190 @Feature({"Android-AppBase"}) 190 @Feature({"Android-AppBase"})
191 public void testNetworkChangeNotifierRegistersForIntents() throws Interrupte dException { 191 public void testNetworkChangeNotifierRegistersForIntents() throws Interrupte dException {
192 mReceiver.onApplicationStateChange(ApplicationState.HAS_RUNNING_ACTIVITI ES); 192 RegistrationPolicyApplicationStatus policy =
193 (RegistrationPolicyApplicationStatus) mReceiver.getRegistrationP olicy();
194 policy.onApplicationStateChange(ApplicationState.HAS_RUNNING_ACTIVITIES) ;
193 assertTrue(mReceiver.isReceiverRegisteredForTesting()); 195 assertTrue(mReceiver.isReceiverRegisteredForTesting());
194 196
195 mReceiver.onApplicationStateChange(ApplicationState.HAS_PAUSED_ACTIVITIE S); 197 policy.onApplicationStateChange(ApplicationState.HAS_PAUSED_ACTIVITIES);
196 assertFalse(mReceiver.isReceiverRegisteredForTesting()); 198 assertFalse(mReceiver.isReceiverRegisteredForTesting());
197 199
198 mReceiver.onApplicationStateChange(ApplicationState.HAS_RUNNING_ACTIVITI ES); 200 policy.onApplicationStateChange(ApplicationState.HAS_RUNNING_ACTIVITIES) ;
199 assertTrue(mReceiver.isReceiverRegisteredForTesting()); 201 assertTrue(mReceiver.isReceiverRegisteredForTesting());
200 } 202 }
201 203
202 /** 204 /**
203 * Tests that changing the RSSI_CHANGED_ACTION intent updates MaxBandwidth. 205 * Tests that changing the RSSI_CHANGED_ACTION intent updates MaxBandwidth.
204 */ 206 */
205 @UiThreadTest 207 @UiThreadTest
206 @MediumTest 208 @MediumTest
207 @Feature({"Android-AppBase"}) 209 @Feature({"Android-AppBase"})
208 public void testNetworkChangeNotifierRSSIEventUpdatesMaxBandwidthForWiFi() 210 public void testNetworkChangeNotifierRSSIEventUpdatesMaxBandwidthForWiFi()
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
324 assertFalse(observer.hasReceivedNotification()); 326 assertFalse(observer.hasReceivedNotification());
325 327
326 // Mimic that connectivity has been lost and ensure that Chrome notifies our observer. 328 // Mimic that connectivity has been lost and ensure that Chrome notifies our observer.
327 mConnectivityDelegate.setActiveNetworkExists(false); 329 mConnectivityDelegate.setActiveNetworkExists(false);
328 Intent noConnectivityIntent = new Intent(ConnectivityManager.CONNECTIVIT Y_ACTION); 330 Intent noConnectivityIntent = new Intent(ConnectivityManager.CONNECTIVIT Y_ACTION);
329 mReceiver.onReceive(getInstrumentation().getTargetContext(), noConnectiv ityIntent); 331 mReceiver.onReceive(getInstrumentation().getTargetContext(), noConnectiv ityIntent);
330 assertTrue(observer.hasReceivedNotification()); 332 assertTrue(observer.hasReceivedNotification());
331 333
332 observer.resetHasReceivedNotification(); 334 observer.resetHasReceivedNotification();
333 // Pretend we got moved to the background. 335 // Pretend we got moved to the background.
334 mReceiver.onApplicationStateChange(ApplicationState.HAS_PAUSED_ACTIVITIE S); 336 final RegistrationPolicyApplicationStatus policy =
337 (RegistrationPolicyApplicationStatus) mReceiver.getRegistrationP olicy();
338 policy.onApplicationStateChange(ApplicationState.HAS_PAUSED_ACTIVITIES);
335 // Change the state. 339 // Change the state.
336 mConnectivityDelegate.setActiveNetworkExists(true); 340 mConnectivityDelegate.setActiveNetworkExists(true);
337 mConnectivityDelegate.setNetworkType(ConnectivityManager.TYPE_WIFI); 341 mConnectivityDelegate.setNetworkType(ConnectivityManager.TYPE_WIFI);
338 // The NetworkChangeNotifierAutoDetect doesn't receive any notification while we are in the 342 // The NetworkChangeNotifierAutoDetect doesn't receive any notification while we are in the
339 // background, but when we get back to the foreground the state changed should be detected 343 // background, but when we get back to the foreground the state changed should be detected
340 // and a notification sent. 344 // and a notification sent.
341 mReceiver.onApplicationStateChange(ApplicationState.HAS_RUNNING_ACTIVITI ES); 345 policy.onApplicationStateChange(ApplicationState.HAS_RUNNING_ACTIVITIES) ;
342 assertTrue(observer.hasReceivedNotification()); 346 assertTrue(observer.hasReceivedNotification());
343 } 347 }
344 348
345 /** 349 /**
346 * Tests that when setting {@code registerToReceiveNotificationsAlways()}, 350 * Tests that when setting {@code registerToReceiveNotificationsAlways()},
347 * a NetworkChangeNotifierAutoDetect object is successfully created. 351 * a NetworkChangeNotifierAutoDetect object is successfully created.
348 */ 352 */
349 @UiThreadTest 353 @UiThreadTest
350 @MediumTest 354 @MediumTest
351 @Feature({"Android-AppBase"}) 355 @Feature({"Android-AppBase"})
352 public void testCreateNetworkChangeNotifierAlwaysWatchForChanges() throws In terruptedException { 356 public void testCreateNetworkChangeNotifierAlwaysWatchForChanges() throws In terruptedException {
353 createTestNotifier(WatchForChanges.ALWAYS); 357 createTestNotifier(WatchForChanges.ALWAYS);
354 assertTrue(mReceiver.isReceiverRegisteredForTesting()); 358 assertTrue(mReceiver.isReceiverRegisteredForTesting());
355 359
356 // Make sure notifications can be received. 360 // Make sure notifications can be received.
357 NetworkChangeNotifierTestObserver observer = new NetworkChangeNotifierTe stObserver(); 361 NetworkChangeNotifierTestObserver observer = new NetworkChangeNotifierTe stObserver();
358 NetworkChangeNotifier.addConnectionTypeObserver(observer); 362 NetworkChangeNotifier.addConnectionTypeObserver(observer);
359 Intent connectivityIntent = new Intent(ConnectivityManager.CONNECTIVITY_ ACTION); 363 Intent connectivityIntent = new Intent(ConnectivityManager.CONNECTIVITY_ ACTION);
360 mReceiver.onReceive(getInstrumentation().getTargetContext(), connectivit yIntent); 364 mReceiver.onReceive(getInstrumentation().getTargetContext(), connectivit yIntent);
361 assertTrue(observer.hasReceivedNotification()); 365 assertTrue(observer.hasReceivedNotification());
362 } 366 }
363 } 367 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698