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

Side by Side Diff: webrtc/api/androidtests/src/org/webrtc/NetworkMonitorTest.java

Issue 2541823002: Move webrtc/api/androidtests to webrtc/sdk/android/instrumentationtests (Closed)
Patch Set: Make include for webrtc.gni absolute Created 4 years 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
(Empty)
1 /*
2 * Copyright 2015 The WebRTC project authors. All Rights Reserved.
3 *
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10 package org.webrtc;
11
12 import static org.webrtc.NetworkMonitorAutoDetect.ConnectionType;
13 import static org.webrtc.NetworkMonitorAutoDetect.ConnectivityManagerDelegate;
14 import static org.webrtc.NetworkMonitorAutoDetect.INVALID_NET_ID;
15 import static org.webrtc.NetworkMonitorAutoDetect.NetworkInformation;
16 import static org.webrtc.NetworkMonitorAutoDetect.NetworkState;
17
18 import android.annotation.SuppressLint;
19 import android.content.Context;
20 import android.content.Intent;
21 import android.net.ConnectivityManager;
22 import android.net.Network;
23 import android.net.wifi.WifiManager;
24 import android.os.Build;
25 import android.os.Handler;
26 import android.os.Looper;
27 import android.test.ActivityTestCase;
28 import android.test.UiThreadTest;
29 import android.test.suitebuilder.annotation.MediumTest;
30 import android.test.suitebuilder.annotation.SmallTest;
31
32 /**
33 * Tests for org.webrtc.NetworkMonitor.
34 */
35 @SuppressLint("NewApi")
36 public class NetworkMonitorTest extends ActivityTestCase {
37 /**
38 * Listens for alerts fired by the NetworkMonitor when network status changes.
39 */
40 private static class NetworkMonitorTestObserver implements NetworkMonitor.Netw orkObserver {
41 private boolean receivedNotification = false;
42
43 @Override
44 public void onConnectionTypeChanged(ConnectionType connectionType) {
45 receivedNotification = true;
46 }
47
48 public boolean hasReceivedNotification() {
49 return receivedNotification;
50 }
51
52 public void resetHasReceivedNotification() {
53 receivedNotification = false;
54 }
55 }
56
57 /**
58 * Mocks out calls to the ConnectivityManager.
59 */
60 private static class MockConnectivityManagerDelegate extends ConnectivityManag erDelegate {
61 private boolean activeNetworkExists;
62 private int networkType;
63 private int networkSubtype;
64
65 @Override
66 public NetworkState getNetworkState() {
67 return new NetworkState(activeNetworkExists, networkType, networkSubtype);
68 }
69
70 // Dummy implementations to avoid NullPointerExceptions in default implement ations:
71
72 @Override
73 public long getDefaultNetId() {
74 return INVALID_NET_ID;
75 }
76
77 @Override
78 public Network[] getAllNetworks() {
79 return new Network[0];
80 }
81
82 @Override
83 public NetworkState getNetworkState(Network network) {
84 return new NetworkState(false, -1, -1);
85 }
86
87 public void setActiveNetworkExists(boolean networkExists) {
88 activeNetworkExists = networkExists;
89 }
90
91 public void setNetworkType(int networkType) {
92 this.networkType = networkType;
93 }
94
95 public void setNetworkSubtype(int networkSubtype) {
96 this.networkSubtype = networkSubtype;
97 }
98 }
99
100 /**
101 * Mocks out calls to the WifiManager.
102 */
103 private static class MockWifiManagerDelegate
104 extends NetworkMonitorAutoDetect.WifiManagerDelegate {
105 private String wifiSSID;
106
107 @Override
108 public String getWifiSSID() {
109 return wifiSSID;
110 }
111
112 public void setWifiSSID(String wifiSSID) {
113 this.wifiSSID = wifiSSID;
114 }
115 }
116
117 // A dummy NetworkMonitorAutoDetect.Observer.
118 private static class TestNetworkMonitorAutoDetectObserver
119 implements NetworkMonitorAutoDetect.Observer {
120 @Override
121 public void onConnectionTypeChanged(ConnectionType newConnectionType) {}
122
123 @Override
124 public void onNetworkConnect(NetworkInformation networkInfo) {}
125
126 @Override
127 public void onNetworkDisconnect(long networkHandle) {}
128 }
129
130 private static final Object lock = new Object();
131 private static Handler uiThreadHandler = null;
132
133 private NetworkMonitorAutoDetect receiver;
134 private MockConnectivityManagerDelegate connectivityDelegate;
135 private MockWifiManagerDelegate wifiDelegate;
136
137 private static Handler getUiThreadHandler() {
138 synchronized (lock) {
139 if (uiThreadHandler == null) {
140 uiThreadHandler = new Handler(Looper.getMainLooper());
141 }
142 return uiThreadHandler;
143 }
144 }
145
146 /**
147 * Helper method to create a network monitor and delegates for testing.
148 */
149 private void createTestMonitor() {
150 Context context = getInstrumentation().getTargetContext();
151 NetworkMonitor.resetInstanceForTests(context);
152 NetworkMonitor.setAutoDetectConnectivityState(true);
153 receiver = NetworkMonitor.getAutoDetectorForTest();
154 assertNotNull(receiver);
155
156 connectivityDelegate = new MockConnectivityManagerDelegate();
157 connectivityDelegate.setActiveNetworkExists(true);
158 receiver.setConnectivityManagerDelegateForTests(connectivityDelegate);
159
160 wifiDelegate = new MockWifiManagerDelegate();
161 receiver.setWifiManagerDelegateForTests(wifiDelegate);
162 wifiDelegate.setWifiSSID("foo");
163 }
164
165 private NetworkMonitorAutoDetect.ConnectionType getCurrentConnectionType() {
166 final NetworkMonitorAutoDetect.NetworkState networkState = receiver.getCurre ntNetworkState();
167 return receiver.getConnectionType(networkState);
168 }
169
170 @Override
171 protected void setUp() throws Exception {
172 super.setUp();
173 getUiThreadHandler().post(new Runnable() {
174 public void run() {
175 createTestMonitor();
176 }
177 });
178 }
179
180 /**
181 * Tests that the receiver registers for connectivity intents during construct ion.
182 */
183 @UiThreadTest
184 @SmallTest
185 public void testNetworkMonitorRegistersInConstructor() throws InterruptedExcep tion {
186 Context context = getInstrumentation().getTargetContext();
187
188 NetworkMonitorAutoDetect.Observer observer = new TestNetworkMonitorAutoDetec tObserver();
189
190 NetworkMonitorAutoDetect receiver = new NetworkMonitorAutoDetect(observer, c ontext);
191
192 assertTrue(receiver.isReceiverRegisteredForTesting());
193 }
194
195 /**
196 * Tests that when there is an intent indicating a change in network connectiv ity, it sends a
197 * notification to Java observers.
198 */
199 @UiThreadTest
200 @MediumTest
201 public void testNetworkMonitorJavaObservers() throws InterruptedException {
202 // Initialize the NetworkMonitor with a connection.
203 Intent connectivityIntent = new Intent(ConnectivityManager.CONNECTIVITY_ACTI ON);
204 receiver.onReceive(getInstrumentation().getTargetContext(), connectivityInte nt);
205
206 // We shouldn't be re-notified if the connection hasn't actually changed.
207 NetworkMonitorTestObserver observer = new NetworkMonitorTestObserver();
208 NetworkMonitor.addNetworkObserver(observer);
209 receiver.onReceive(getInstrumentation().getTargetContext(), connectivityInte nt);
210 assertFalse(observer.hasReceivedNotification());
211
212 // We shouldn't be notified if we're connected to non-Wifi and the Wifi SSID changes.
213 wifiDelegate.setWifiSSID("bar");
214 receiver.onReceive(getInstrumentation().getTargetContext(), connectivityInte nt);
215 assertFalse(observer.hasReceivedNotification());
216
217 // We should be notified when we change to Wifi.
218 connectivityDelegate.setNetworkType(ConnectivityManager.TYPE_WIFI);
219 receiver.onReceive(getInstrumentation().getTargetContext(), connectivityInte nt);
220 assertTrue(observer.hasReceivedNotification());
221 observer.resetHasReceivedNotification();
222
223 // We should be notified when the Wifi SSID changes.
224 wifiDelegate.setWifiSSID("foo");
225 receiver.onReceive(getInstrumentation().getTargetContext(), connectivityInte nt);
226 assertTrue(observer.hasReceivedNotification());
227 observer.resetHasReceivedNotification();
228
229 // We shouldn't be re-notified if the Wifi SSID hasn't actually changed.
230 receiver.onReceive(getInstrumentation().getTargetContext(), connectivityInte nt);
231 assertFalse(observer.hasReceivedNotification());
232
233 // Mimic that connectivity has been lost and ensure that the observer gets t he notification.
234 connectivityDelegate.setActiveNetworkExists(false);
235 Intent noConnectivityIntent = new Intent(ConnectivityManager.CONNECTIVITY_AC TION);
236 receiver.onReceive(getInstrumentation().getTargetContext(), noConnectivityIn tent);
237 assertTrue(observer.hasReceivedNotification());
238 }
239
240 /**
241 * Tests that ConnectivityManagerDelegate doesn't crash. This test cannot rely on having any
242 * active network connections so it cannot usefully check results, but it can at least check
243 * that the functions don't crash.
244 */
245 @UiThreadTest
246 @SmallTest
247 public void testConnectivityManagerDelegateDoesNotCrash() {
248 ConnectivityManagerDelegate delegate =
249 new ConnectivityManagerDelegate(getInstrumentation().getTargetContext()) ;
250 delegate.getNetworkState();
251 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
252 Network[] networks = delegate.getAllNetworks();
253 if (networks.length >= 1) {
254 delegate.getNetworkState(networks[0]);
255 delegate.hasInternetCapability(networks[0]);
256 }
257 delegate.getDefaultNetId();
258 }
259 }
260
261 /**
262 * Tests that NetworkMonitorAutoDetect queryable APIs don't crash. This test c annot rely
263 * on having any active network connections so it cannot usefully check result s, but it can at
264 * least check that the functions don't crash.
265 */
266 @UiThreadTest
267 @SmallTest
268 public void testQueryableAPIsDoNotCrash() {
269 NetworkMonitorAutoDetect.Observer observer = new TestNetworkMonitorAutoDetec tObserver();
270 NetworkMonitorAutoDetect ncn =
271 new NetworkMonitorAutoDetect(observer, getInstrumentation().getTargetCon text());
272 ncn.getDefaultNetId();
273 }
274 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698