| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2015 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 package org.webrtc; | 10 package org.webrtc; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 import android.test.suitebuilder.annotation.SmallTest; | 30 import android.test.suitebuilder.annotation.SmallTest; |
| 31 | 31 |
| 32 /** | 32 /** |
| 33 * Tests for org.webrtc.NetworkMonitor. | 33 * Tests for org.webrtc.NetworkMonitor. |
| 34 */ | 34 */ |
| 35 @SuppressLint("NewApi") | 35 @SuppressLint("NewApi") |
| 36 public class NetworkMonitorTest extends ActivityTestCase { | 36 public class NetworkMonitorTest extends ActivityTestCase { |
| 37 /** | 37 /** |
| 38 * Listens for alerts fired by the NetworkMonitor when network status changes. | 38 * Listens for alerts fired by the NetworkMonitor when network status changes. |
| 39 */ | 39 */ |
| 40 private static class NetworkMonitorTestObserver | 40 private static class NetworkMonitorTestObserver implements NetworkMonitor.Netw
orkObserver { |
| 41 implements NetworkMonitor.NetworkObserver { | |
| 42 private boolean receivedNotification = false; | 41 private boolean receivedNotification = false; |
| 43 | 42 |
| 44 @Override | 43 @Override |
| 45 public void onConnectionTypeChanged(ConnectionType connectionType) { | 44 public void onConnectionTypeChanged(ConnectionType connectionType) { |
| 46 receivedNotification = true; | 45 receivedNotification = true; |
| 47 } | 46 } |
| 48 | 47 |
| 49 public boolean hasReceivedNotification() { | 48 public boolean hasReceivedNotification() { |
| 50 return receivedNotification; | 49 return receivedNotification; |
| 51 } | 50 } |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 } | 110 } |
| 112 | 111 |
| 113 public void setWifiSSID(String wifiSSID) { | 112 public void setWifiSSID(String wifiSSID) { |
| 114 this.wifiSSID = wifiSSID; | 113 this.wifiSSID = wifiSSID; |
| 115 } | 114 } |
| 116 } | 115 } |
| 117 | 116 |
| 118 // A dummy NetworkMonitorAutoDetect.Observer. | 117 // A dummy NetworkMonitorAutoDetect.Observer. |
| 119 private static class TestNetworkMonitorAutoDetectObserver | 118 private static class TestNetworkMonitorAutoDetectObserver |
| 120 implements NetworkMonitorAutoDetect.Observer { | 119 implements NetworkMonitorAutoDetect.Observer { |
| 121 | |
| 122 @Override | 120 @Override |
| 123 public void onConnectionTypeChanged(ConnectionType newConnectionType) {} | 121 public void onConnectionTypeChanged(ConnectionType newConnectionType) {} |
| 124 | 122 |
| 125 @Override | 123 @Override |
| 126 public void onNetworkConnect(NetworkInformation networkInfo) {} | 124 public void onNetworkConnect(NetworkInformation networkInfo) {} |
| 127 | 125 |
| 128 @Override | 126 @Override |
| 129 public void onNetworkDisconnect(long networkHandle) {} | 127 public void onNetworkDisconnect(long networkHandle) {} |
| 130 } | 128 } |
| 131 | 129 |
| 132 private static final Object lock = new Object(); | 130 private static final Object lock = new Object(); |
| 133 private static Handler uiThreadHandler = null; | 131 private static Handler uiThreadHandler = null; |
| 134 | 132 |
| 135 private NetworkMonitorAutoDetect receiver; | 133 private NetworkMonitorAutoDetect receiver; |
| 136 private MockConnectivityManagerDelegate connectivityDelegate; | 134 private MockConnectivityManagerDelegate connectivityDelegate; |
| 137 private MockWifiManagerDelegate wifiDelegate; | 135 private MockWifiManagerDelegate wifiDelegate; |
| 138 | 136 |
| 139 private static Handler getUiThreadHandler() { | 137 private static Handler getUiThreadHandler() { |
| 140 synchronized (lock) { | 138 synchronized (lock) { |
| 141 if (uiThreadHandler == null ) { | 139 if (uiThreadHandler == null) { |
| 142 uiThreadHandler = new Handler(Looper.getMainLooper()); | 140 uiThreadHandler = new Handler(Looper.getMainLooper()); |
| 143 } | 141 } |
| 144 return uiThreadHandler; | 142 return uiThreadHandler; |
| 145 } | 143 } |
| 146 } | 144 } |
| 147 | 145 |
| 148 /** | 146 /** |
| 149 * Helper method to create a network monitor and delegates for testing. | 147 * Helper method to create a network monitor and delegates for testing. |
| 150 */ | 148 */ |
| 151 private void createTestMonitor() { | 149 private void createTestMonitor() { |
| 152 Context context = getInstrumentation().getTargetContext(); | 150 Context context = getInstrumentation().getTargetContext(); |
| 153 NetworkMonitor.resetInstanceForTests(context); | 151 NetworkMonitor.resetInstanceForTests(context); |
| 154 NetworkMonitor.setAutoDetectConnectivityState(true); | 152 NetworkMonitor.setAutoDetectConnectivityState(true); |
| 155 receiver = NetworkMonitor.getAutoDetectorForTest(); | 153 receiver = NetworkMonitor.getAutoDetectorForTest(); |
| 156 assertNotNull(receiver); | 154 assertNotNull(receiver); |
| 157 | 155 |
| 158 connectivityDelegate = new MockConnectivityManagerDelegate(); | 156 connectivityDelegate = new MockConnectivityManagerDelegate(); |
| 159 connectivityDelegate.setActiveNetworkExists(true); | 157 connectivityDelegate.setActiveNetworkExists(true); |
| 160 receiver.setConnectivityManagerDelegateForTests(connectivityDelegate); | 158 receiver.setConnectivityManagerDelegateForTests(connectivityDelegate); |
| 161 | 159 |
| 162 wifiDelegate = new MockWifiManagerDelegate(); | 160 wifiDelegate = new MockWifiManagerDelegate(); |
| 163 receiver.setWifiManagerDelegateForTests(wifiDelegate); | 161 receiver.setWifiManagerDelegateForTests(wifiDelegate); |
| 164 wifiDelegate.setWifiSSID("foo"); | 162 wifiDelegate.setWifiSSID("foo"); |
| 165 } | 163 } |
| 166 | 164 |
| 167 private NetworkMonitorAutoDetect.ConnectionType getCurrentConnectionType() { | 165 private NetworkMonitorAutoDetect.ConnectionType getCurrentConnectionType() { |
| 168 final NetworkMonitorAutoDetect.NetworkState networkState = | 166 final NetworkMonitorAutoDetect.NetworkState networkState = receiver.getCurre
ntNetworkState(); |
| 169 receiver.getCurrentNetworkState(); | |
| 170 return receiver.getConnectionType(networkState); | 167 return receiver.getConnectionType(networkState); |
| 171 } | 168 } |
| 172 | 169 |
| 173 @Override | 170 @Override |
| 174 protected void setUp() throws Exception { | 171 protected void setUp() throws Exception { |
| 175 super.setUp(); | 172 super.setUp(); |
| 176 getUiThreadHandler().post(new Runnable() { | 173 getUiThreadHandler().post(new Runnable() { |
| 177 public void run() { | 174 public void run() { |
| 178 createTestMonitor(); | 175 createTestMonitor(); |
| 179 } | 176 } |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 */ | 265 */ |
| 269 @UiThreadTest | 266 @UiThreadTest |
| 270 @SmallTest | 267 @SmallTest |
| 271 public void testQueryableAPIsDoNotCrash() { | 268 public void testQueryableAPIsDoNotCrash() { |
| 272 NetworkMonitorAutoDetect.Observer observer = new TestNetworkMonitorAutoDetec
tObserver(); | 269 NetworkMonitorAutoDetect.Observer observer = new TestNetworkMonitorAutoDetec
tObserver(); |
| 273 NetworkMonitorAutoDetect ncn = | 270 NetworkMonitorAutoDetect ncn = |
| 274 new NetworkMonitorAutoDetect(observer, getInstrumentation().getTargetCon
text()); | 271 new NetworkMonitorAutoDetect(observer, getInstrumentation().getTargetCon
text()); |
| 275 ncn.getDefaultNetId(); | 272 ncn.getDefaultNetId(); |
| 276 } | 273 } |
| 277 } | 274 } |
| OLD | NEW |