Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / test / data / extensions / api_test / networking_private / chromeos / test.js
blob33740bfd77e20bf406f1bc1a5ddcbe781a01fa0a
1 // Copyright (c) 2013 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 // The expectations in this test for the Chrome OS implementation. See
6 // networking_private_chromeos_apitest.cc for more info.
8 var callbackPass = chrome.test.callbackPass;
9 var callbackFail = chrome.test.callbackFail;
10 var assertTrue = chrome.test.assertTrue;
11 var assertFalse = chrome.test.assertFalse;
12 var assertEq = chrome.test.assertEq;
14 var ActivationStateType = chrome.networkingPrivate.ActivationStateType;
15 var ConnectionStateType = chrome.networkingPrivate.ConnectionStateType;
16 var NetworkType = chrome.networkingPrivate.NetworkType;
18 var kCellularGuid = 'stub_cellular1_guid';
19 var kDefaultPin = '1111';
20 var kDefaultPuk = '12345678';
22 // Test properties for the verification API.
23 var verificationProperties = {
24   certificate: 'certificate',
25   intermediateCertificates: ['ica1', 'ica2', 'ica3'],
26   publicKey: 'cHVibGljX2tleQ==',  // Base64('public_key')
27   nonce: 'nonce',
28   signedData: 'c2lnbmVkX2RhdGE=',  // Base64('signed_data')
29   deviceSerial: 'device_serial',
30   deviceSsid: 'Device 0123',
31   deviceBssid: '00:01:02:03:04:05'
34 var privateHelpers = {
35   // Watches for the states |expectedStates| in reverse order. If all states
36   // were observed in the right order, succeeds and calls |done|. If any
37   // unexpected state is observed, fails.
38   watchForStateChanges: function(network, expectedStates, done) {
39     var self = this;
40     var collectProperties = function(properties) {
41       var finishTest = function() {
42         chrome.networkingPrivate.onNetworksChanged.removeListener(
43             self.onNetworkChange);
44         done();
45       };
46       if (expectedStates.length > 0) {
47         var expectedState = expectedStates.pop();
48         assertEq(expectedState, properties.ConnectionState);
49         if (expectedStates.length == 0)
50           finishTest();
51       }
52     };
53     this.onNetworkChange = function(changes) {
54       assertEq([network], changes);
55       chrome.networkingPrivate.getProperties(
56           network,
57           callbackPass(collectProperties));
58     };
59     chrome.networkingPrivate.onNetworksChanged.addListener(
60         this.onNetworkChange);
61   },
62   listListener: function(expected, done) {
63     var self = this;
64     this.listenForChanges = function(list) {
65       assertEq(expected, list);
66       chrome.networkingPrivate.onNetworkListChanged.removeListener(
67           self.listenForChanges);
68       done();
69     };
70   },
71   watchForCaptivePortalState: function(expectedGuid,
72                                        expectedState,
73                                        done) {
74     var self = this;
75     this.onPortalDetectionCompleted = function(guid, state) {
76       assertEq(expectedGuid, guid);
77       assertEq(expectedState, state);
78       chrome.networkingPrivate.onPortalDetectionCompleted.removeListener(
79           self.onPortalDetectionCompleted);
80       done();
81     };
82     chrome.networkingPrivate.onPortalDetectionCompleted.addListener(
83         self.onPortalDetectionCompleted);
84   }
87 var kFailure = 'Failure';
89 function networkCallbackPass() {
90   var callbackCompleted = chrome.test.callbackAdded();
91   return function(result) {
92     chrome.test.assertNoLastError();
93     if (result === false || result === kFailure)
94       chrome.test.fail('Failed: ' + result);
95     callbackCompleted();
96   };
99 var availableTests = [
100   function startConnect() {
101     chrome.networkingPrivate.startConnect('stub_wifi2_guid',
102                                           networkCallbackPass());
103   },
104   function startDisconnect() {
105     // Must connect to a network before we can disconnect from it.
106     chrome.networkingPrivate.startConnect(
107         'stub_wifi2_guid',
108         callbackPass(function() {
109           chrome.networkingPrivate.startDisconnect(
110               'stub_wifi2_guid', networkCallbackPass());
111         }));
112   },
113   function startActivate() {
114     // Must connect to a network before we can activate it.
115     chrome.networkingPrivate.startConnect(
116         kCellularGuid, callbackPass(function() {
117           chrome.networkingPrivate.startActivate(
118               kCellularGuid, callbackPass(function() {
119                 // For non Sprint networks, startActivate will delegate
120                 // showing the activation UI to the browser host and not
121                 // immediately activate the network.
122                 chrome.networkingPrivate.getState(
123                     kCellularGuid, callbackPass(function(state) {
124                       assertEq(ActivationStateType.NOT_ACTIVATED,
125                                state.Cellular.ActivationState);
126                     }));
127               }));
128         }));
129   },
130   function startActivateSprint() {
131     // Must connect to a network before we can activate it.
132     chrome.networkingPrivate.startConnect(
133         kCellularGuid, callbackPass(function() {
134           chrome.networkingPrivate.startActivate(
135               kCellularGuid, callbackPass(function() {
136                 chrome.networkingPrivate.getState(
137                     kCellularGuid, callbackPass(function(state) {
138                       assertEq(ActivationStateType.ACTIVATED,
139                                state.Cellular.ActivationState);
140                     }));
141               }));
142         }));
143   },
144   function startConnectNonexistent() {
145     chrome.networkingPrivate.startConnect(
146       'nonexistent_path',
147       callbackFail('Error.InvalidNetworkGuid'));
148   },
149   function startDisconnectNonexistent() {
150     chrome.networkingPrivate.startDisconnect(
151       'nonexistent_path',
152       callbackFail('Error.InvalidNetworkGuid'));
153   },
154   function startGetPropertiesNonexistent() {
155     chrome.networkingPrivate.getProperties(
156       'nonexistent_path',
157       callbackFail('Error.InvalidNetworkGuid'));
158   },
159   function createNetwork() {
160     chrome.networkingPrivate.createNetwork(
161       false,  // shared
162       { Type: NetworkType.WI_FI,
163         GUID: 'ignored_guid',
164         WiFi: {
165           SSID: 'wifi_created',
166           Security: 'WEP-PSK'
167         }
168       },
169       callbackPass(function(guid) {
170         assertFalse(guid == '');
171         assertFalse(guid == 'ignored_guid');
172         chrome.networkingPrivate.getProperties(
173             guid, callbackPass(function(properties) {
174               assertEq(NetworkType.WI_FI, properties.Type);
175               assertEq(guid, properties.GUID);
176               assertEq('wifi_created', properties.WiFi.SSID);
177               assertEq('WEP-PSK', properties.WiFi.Security);
178             }));
179       }));
180   },
181   function forgetNetwork() {
182     var kNumNetworks = 2;
183     var kTestNetworkGuid = 'stub_wifi1_guid';
184     function guidExists(networks, guid) {
185       for (var n of networks) {
186         if (n.GUID == kTestNetworkGuid)
187           return true;
188       }
189       return false;
190     }
191     var filter = {
192       networkType: NetworkType.WI_FI,
193       visible: true,
194       configured: true
195     };
196     chrome.networkingPrivate.getNetworks(
197         filter, callbackPass(function(networks) {
198           assertEq(kNumNetworks, networks.length);
199           assertTrue(guidExists(networks, kTestNetworkGuid));
200           chrome.networkingPrivate.forgetNetwork(
201               kTestNetworkGuid, callbackPass(function() {
202                 chrome.networkingPrivate.getNetworks(
203                     filter, callbackPass(function(networks) {
204                       assertEq(kNumNetworks - 1, networks.length);
205                       assertFalse(guidExists(networks, kTestNetworkGuid));
206                     }));
207               }));
208         }));
209   },
210   function getNetworks() {
211     // Test 'type' and 'configured'.
212     var filter = {
213       networkType: NetworkType.WI_FI,
214       configured: true
215     };
216     chrome.networkingPrivate.getNetworks(
217       filter,
218       callbackPass(function(result) {
219         assertEq([{
220           Connectable: true,
221           ConnectionState: ConnectionStateType.CONNECTED,
222           GUID: 'stub_wifi1_guid',
223           Name: 'wifi1',
224           Priority: 0,
225           Source: 'User',
226           Type: NetworkType.WI_FI,
227           WiFi: {
228             Security: 'WEP-PSK',
229             SignalStrength: 40
230           }
231         }, {
232           GUID: 'stub_wifi2_guid',
233           Name: 'wifi2_PSK',
234           Priority: 0,
235           Source: 'User',
236           Type: NetworkType.WI_FI,
237           WiFi: {
238             Security: 'WPA-PSK',
239           }
240         }], result);
242         // Test 'visible' (and 'configured').
243         filter.visible = true;
244         chrome.networkingPrivate.getNetworks(
245           filter,
246           callbackPass(function(result) {
247             assertEq([{
248               Connectable: true,
249               ConnectionState: ConnectionStateType.CONNECTED,
250               GUID: 'stub_wifi1_guid',
251               Name: 'wifi1',
252               Priority: 0,
253               Source: 'User',
254               Type: NetworkType.WI_FI,
255               WiFi: {
256                 Security: 'WEP-PSK',
257                 SignalStrength: 40
258               }
259             }], result);
261             // Test 'limit'.
262             filter = {
263               networkType: NetworkType.ALL,
264               limit: 1
265             };
266             chrome.networkingPrivate.getNetworks(
267               filter,
268               callbackPass(function(result) {
269                 assertEq([{
270                   ConnectionState: ConnectionStateType.CONNECTED,
271                   Ethernet: {
272                     Authentication: 'None'
273                   },
274                   GUID: 'stub_ethernet_guid',
275                   Name: 'eth0',
276                   Priority: 0,
277                   Source: 'Device',
278                   Type: NetworkType.ETHERNET
279                 }], result);
280               }));
281           }));
282       }));
283   },
284   function getVisibleNetworks() {
285     chrome.networkingPrivate.getVisibleNetworks(
286       NetworkType.ALL,
287       callbackPass(function(result) {
288         assertEq([{
289           ConnectionState: ConnectionStateType.CONNECTED,
290           Ethernet: {
291             Authentication: 'None'
292           },
293           GUID: 'stub_ethernet_guid',
294           Name: 'eth0',
295           Priority: 0,
296           Source: 'Device',
297           Type: NetworkType.ETHERNET
298         }, {
299           Connectable: true,
300           ConnectionState: ConnectionStateType.CONNECTED,
301           GUID: 'stub_wifi1_guid',
302           Name: 'wifi1',
303           Priority: 0,
304           Source: 'User',
305           Type: NetworkType.WI_FI,
306           WiFi: {
307             Security: 'WEP-PSK',
308             SignalStrength: 40
309           }
310         }, {
311           Connectable: true,
312           ConnectionState: ConnectionStateType.CONNECTED,
313           GUID: 'stub_wimax_guid',
314           Name: 'wimax',
315           Priority: 0,
316           Source: 'User',
317           Type: NetworkType.WI_MAX,
318           WiMAX: {
319             SignalStrength: 40
320           }
321         }, {
322           ConnectionState: ConnectionStateType.CONNECTED,
323           GUID: 'stub_vpn1_guid',
324           Name: 'vpn1',
325           Priority: 0,
326           Source: 'User',
327           Type: NetworkType.VPN,
328           VPN: {
329             Type: 'OpenVPN'
330           }
331         }, {
332           ConnectionState: ConnectionStateType.NOT_CONNECTED,
333           GUID: 'stub_vpn2_guid',
334           Name: 'vpn2',
335           Priority: 0,
336           Source: 'User',
337           Type: NetworkType.VPN,
338           VPN: {
339             ThirdPartyVPN: {
340               ExtensionID: 'third_party_provider_extension_id'
341             },
342             Type: 'ThirdPartyVPN'
343           }
344         }, {
345           Connectable: true,
346           ConnectionState: ConnectionStateType.NOT_CONNECTED,
347           GUID: 'stub_wifi2_guid',
348           Name: 'wifi2_PSK',
349           Priority: 0,
350           Source: 'User',
351           Type: NetworkType.WI_FI,
352           WiFi: {
353             Security: 'WPA-PSK',
354             SignalStrength: 80
355           }
356         }], result);
357       }));
358   },
359   function getVisibleNetworksWifi() {
360     chrome.networkingPrivate.getVisibleNetworks(
361       NetworkType.WI_FI,
362       callbackPass(function(result) {
363         assertEq([{
364           Connectable: true,
365           ConnectionState: ConnectionStateType.CONNECTED,
366           GUID: 'stub_wifi1_guid',
367           Name: 'wifi1',
368           Priority: 0,
369           Source: 'User',
370           Type: NetworkType.WI_FI,
371           WiFi: {
372             Security: 'WEP-PSK',
373             SignalStrength: 40
374           }
375         }, {
376           Connectable: true,
377           ConnectionState: ConnectionStateType.NOT_CONNECTED,
378           GUID: 'stub_wifi2_guid',
379           Name: 'wifi2_PSK',
380           Priority: 0,
381           Source: 'User',
382           Type: NetworkType.WI_FI,
383           WiFi: {
384             Security: 'WPA-PSK',
385             SignalStrength: 80
386           }
387         }], result);
388       }));
389   },
390   function enabledNetworkTypes() {
391     // Note: We call getEnabledNetworkTypes twice after each enable/dsiable
392     // to ensure that Chrome has processed the command (since enable/disable
393     // are 'synchronous' even though the action of enabling/disabling is not).
394     chrome.networkingPrivate.getEnabledNetworkTypes(
395         callbackPass(function(types) {
396           assertTrue(types.indexOf('WiFi') >= 0);
397           chrome.networkingPrivate.disableNetworkType('WiFi');
398           chrome.networkingPrivate.getEnabledNetworkTypes(
399               callbackPass(function(types) {
400                 chrome.networkingPrivate.getEnabledNetworkTypes(
401                     callbackPass(function(types) {
402                       assertFalse(types.indexOf('WiFi') >= 0);
403                       chrome.networkingPrivate.enableNetworkType('WiFi');
404                       chrome.networkingPrivate.getEnabledNetworkTypes(
405                           callbackPass(function(types) {
406                             chrome.networkingPrivate.getEnabledNetworkTypes(
407                                 callbackPass(function(types) {
408                                   assertTrue(types.indexOf('WiFi') >= 0);
409                                 }));
410                           }));
411                     }));
412               }));
413         }));
414   },
415   function getDeviceStates() {
416     chrome.networkingPrivate.getDeviceStates(callbackPass(function(result) {
417       assertEq([
418         {Scanning: false, State: 'Enabled', Type: 'Ethernet'},
419         {Scanning: false, State: 'Enabled', Type: 'WiFi'},
420         {State: 'Uninitialized', Type: 'Cellular', SimPresent: true},
421         {State: 'Disabled', Type: 'WiMAX'},
422       ],
423                result);
424     }));
425   },
426   function requestNetworkScan() {
427     // Connected or Connecting networks should be listed first, sorted by type.
428     var expected = ['stub_ethernet_guid',
429                     'stub_wifi1_guid',
430                     'stub_wimax_guid',
431                     'stub_vpn1_guid',
432                     'stub_vpn2_guid',
433                     'stub_wifi2_guid'];
434     var done = chrome.test.callbackAdded();
435     var listener = new privateHelpers.listListener(expected, done);
436     chrome.networkingPrivate.onNetworkListChanged.addListener(
437       listener.listenForChanges);
438     chrome.networkingPrivate.requestNetworkScan();
439   },
440   function getProperties() {
441     chrome.networkingPrivate.getProperties(
442       'stub_wifi1_guid',
443       callbackPass(function(result) {
444         assertEq({
445           Connectable: true,
446           ConnectionState: ConnectionStateType.CONNECTED,
447           GUID: 'stub_wifi1_guid',
448           IPAddressConfigType: chrome.networkingPrivate.IPConfigType.STATIC,
449           IPConfigs: [{
450             Gateway: '0.0.0.1',
451             IPAddress: '0.0.0.0',
452             RoutingPrefix: 0,
453             Type: 'IPv4'
454           }],
455           MacAddress: '00:11:22:AA:BB:CC',
456           Name: 'wifi1',
457           Source: 'User',
458           StaticIPConfig: {
459             IPAddress: '1.2.3.4',
460             Type: 'IPv4'
461           },
462           Type: NetworkType.WI_FI,
463           WiFi: {
464             HexSSID: '7769666931', // 'wifi1'
465             Frequency: 2400,
466             FrequencyList: [2400],
467             SSID: 'wifi1',
468             Security: 'WEP-PSK',
469             SignalStrength: 40
470           }
471         }, result);
472       }));
473   },
474   function getPropertiesCellular() {
475     chrome.networkingPrivate.getProperties(
476       kCellularGuid,
477       callbackPass(function(result) {
478         assertEq({
479           Cellular: {
480             ActivationState: ActivationStateType.NOT_ACTIVATED,
481             AllowRoaming: false,
482             AutoConnect: true,
483             Carrier: 'Cellular1_Carrier',
484             Family: 'GSM',
485             HomeProvider: {
486               Country: 'us',
487               Name: 'Cellular1_Provider'
488             },
489             NetworkTechnology: 'GSM',
490             RoamingState: 'Home',
491             SIMLockStatus: {LockEnabled: true, LockType: '', RetriesLeft: 3}
492           },
493           ConnectionState: ConnectionStateType.NOT_CONNECTED,
494           GUID: kCellularGuid,
495           Name: 'cellular1',
496           Source: 'User',
497           Type: NetworkType.CELLULAR,
498         }, result);
499       }));
500   },
501   function getManagedProperties() {
502     chrome.networkingPrivate.getManagedProperties(
503       'stub_wifi2',
504       callbackPass(function(result) {
505         assertEq({
506           Connectable: true,
507           ConnectionState: ConnectionStateType.NOT_CONNECTED,
508           GUID: 'stub_wifi2',
509           Name: {
510             Active: 'wifi2_PSK',
511             Effective: 'UserPolicy',
512             UserPolicy: 'My WiFi Network'
513           },
514           Source: 'UserPolicy',
515           Type: NetworkType.WI_FI,
516           WiFi: {
517             AutoConnect: {
518               Active: false,
519               UserEditable: true
520             },
521             HexSSID: {
522               Active: '77696669325F50534B', // 'wifi2_PSK'
523               Effective: 'UserPolicy',
524               UserPolicy: '77696669325F50534B'
525             },
526             Frequency: 5000,
527             FrequencyList: [2400, 5000],
528             Passphrase: {
529               Effective: 'UserSetting',
530               UserEditable: true,
531               UserSetting: 'FAKE_CREDENTIAL_VPaJDV9x'
532             },
533             SSID: {
534               Active: 'wifi2_PSK',
535               Effective: 'UserPolicy',
536             },
537             Security: {
538               Active: 'WPA-PSK',
539               Effective: 'UserPolicy',
540               UserPolicy: 'WPA-PSK'
541             },
542             SignalStrength: 80,
543           }
544         }, result);
545       }));
546   },
547   function setCellularProperties() {
548     var network_guid = kCellularGuid;
549     // Make sure we test Cellular.Carrier since it requires a special call
550     // to Shill.Device.SetCarrier.
551     var newCarrier = 'new_carrier';
552     chrome.networkingPrivate.getProperties(
553         network_guid,
554         callbackPass(function(result) {
555           assertEq(network_guid, result.GUID);
556           assertTrue(!result.Cellular || result.Cellular.Carrier != newCarrier);
557           var new_properties = {
558             Priority: 1,
559             Cellular: {
560               Carrier: newCarrier,
561             },
562           };
563           chrome.networkingPrivate.setProperties(
564               network_guid,
565               new_properties,
566               callbackPass(function() {
567                 chrome.networkingPrivate.getProperties(
568                     network_guid,
569                     callbackPass(function(result) {
570                       // Ensure that the GUID doesn't change.
571                       assertEq(network_guid, result.GUID);
572                       // Ensure that the properties were set.
573                       assertEq(1, result['Priority']);
574                       assertTrue('Cellular' in result);
575                       assertEq(newCarrier, result.Cellular.Carrier);
576                     }));
577               }));
578         }));
579   },
580   function setWiFiProperties() {
581     var network_guid = 'stub_wifi1_guid';
582     chrome.networkingPrivate.getProperties(
583         network_guid,
584         callbackPass(function(result) {
585           assertEq(network_guid, result.GUID);
586           var new_properties = {
587             Priority: 1,
588             WiFi: {
589               AutoConnect: true
590             },
591             IPAddressConfigType: 'Static',
592             StaticIPConfig: {
593               IPAddress: '1.2.3.4'
594             }
595           };
596           chrome.networkingPrivate.setProperties(
597               network_guid,
598               new_properties,
599               callbackPass(function() {
600                 chrome.networkingPrivate.getProperties(
601                     network_guid,
602                     callbackPass(function(result) {
603                       // Ensure that the GUID doesn't change.
604                       assertEq(network_guid, result.GUID);
605                       // Ensure that the properties were set.
606                       assertEq(1, result['Priority']);
607                       assertTrue('WiFi' in result);
608                       assertTrue('AutoConnect' in result['WiFi']);
609                       assertEq(true, result['WiFi']['AutoConnect']);
610                       assertTrue('StaticIPConfig' in result);
611                       assertEq('1.2.3.4',
612                                result['StaticIPConfig']['IPAddress']);
613                     }));
614               }));
615         }));
616   },
617   function setVPNProperties() {
618     var network_guid = 'stub_vpn1_guid';
619     chrome.networkingPrivate.getProperties(
620         network_guid,
621         callbackPass(function(result) {
622           assertEq(network_guid, result.GUID);
623           var new_properties = {
624             Priority: 1,
625             VPN: {
626               Host: 'vpn.host1'
627             }
628           };
629           chrome.networkingPrivate.setProperties(
630               network_guid,
631               new_properties,
632               callbackPass(function() {
633                 chrome.networkingPrivate.getProperties(
634                     network_guid,
635                     callbackPass(function(result) {
636                       // Ensure that the properties were set.
637                       assertEq(1, result['Priority']);
638                       assertTrue('VPN' in result);
639                       assertTrue('Host' in result['VPN']);
640                       assertEq('vpn.host1', result['VPN']['Host']);
641                       // Ensure that the GUID doesn't change.
642                       assertEq(network_guid, result.GUID);
643                     }));
644               }));
645         }));
646   },
647   function getState() {
648     chrome.networkingPrivate.getState(
649       'stub_wifi2_guid',
650       callbackPass(function(result) {
651         assertEq({
652           Connectable: true,
653           ConnectionState: ConnectionStateType.NOT_CONNECTED,
654           GUID: 'stub_wifi2_guid',
655           Name: 'wifi2_PSK',
656           Priority: 0,
657           Source: 'User',
658           Type: NetworkType.WI_FI,
659           WiFi: {
660             Security: 'WPA-PSK',
661             SignalStrength: 80
662           }
663         }, result);
664       }));
665   },
666   function getStateNonExistent() {
667     chrome.networkingPrivate.getState(
668       'non_existent',
669       callbackFail('Error.InvalidNetworkGuid'));
670   },
671   function getErrorState() {
672     // Both getState and getProperties should have ErrorState set.
673     chrome.networkingPrivate.getState(
674         'stub_wifi1_guid', callbackPass(function(result) {
675           assertEq('TestErrorState', result.ErrorState);
676           chrome.networkingPrivate.getProperties(
677               'stub_wifi1_guid', callbackPass(function(result2) {
678                 assertEq('TestErrorState', result2.ErrorState);
679               }));
680         }));
681   },
682   function onNetworksChangedEventConnect() {
683     var network = 'stub_wifi2_guid';
684     var done = chrome.test.callbackAdded();
685     var expectedStates = [ConnectionStateType.CONNECTED];
686     var listener =
687         new privateHelpers.watchForStateChanges(network, expectedStates, done);
688     chrome.networkingPrivate.startConnect(network,
689                                           networkCallbackPass());
690   },
691   function onNetworksChangedEventDisconnect() {
692     var network = 'stub_wifi1_guid';
693     var done = chrome.test.callbackAdded();
694     var expectedStates = [ConnectionStateType.NOT_CONNECTED];
695     var listener =
696         new privateHelpers.watchForStateChanges(network, expectedStates, done);
697     chrome.networkingPrivate.startDisconnect(network,
698                                              networkCallbackPass());
699   },
700   function onNetworkListChangedEvent() {
701     // Connecting to wifi2 should set wifi1 to offline. Connected or Connecting
702     // networks should be listed first, sorted by type.
703     var expected = ['stub_ethernet_guid',
704                     'stub_wifi2_guid',
705                     'stub_wimax_guid',
706                     'stub_vpn1_guid',
707                     'stub_wifi1_guid',
708                     'stub_vpn2_guid'];
709     var done = chrome.test.callbackAdded();
710     var listener = new privateHelpers.listListener(expected, done);
711     chrome.networkingPrivate.onNetworkListChanged.addListener(
712       listener.listenForChanges);
713     var network = 'stub_wifi2_guid';
714     chrome.networkingPrivate.startConnect(network,
715                                           networkCallbackPass());
716   },
717   function onDeviceStateListChangedEvent() {
718     var listener = callbackPass(function() {
719       chrome.networkingPrivate.onDeviceStateListChanged.removeListener(
720           listener);
721     });
722     chrome.networkingPrivate.onDeviceStateListChanged.addListener(listener);
723     chrome.networkingPrivate.disableNetworkType('WiFi');
724   },
725   function verifyDestination() {
726     chrome.networkingPrivate.verifyDestination(
727       verificationProperties,
728       callbackPass(function(isValid) {
729         assertTrue(isValid);
730       }));
731   },
732   function verifyAndEncryptCredentials() {
733     var network_guid = 'stub_wifi2_guid';
734     chrome.networkingPrivate.verifyAndEncryptCredentials(
735       verificationProperties,
736       network_guid,
737       callbackPass(function(result) {
738         assertEq('encrypted_credentials', result);
739       }));
740   },
741   function verifyAndEncryptData() {
742     chrome.networkingPrivate.verifyAndEncryptData(
743       verificationProperties,
744       'data',
745       callbackPass(function(result) {
746         assertEq('encrypted_data', result);
747       }));
748   },
749   function setWifiTDLSEnabledState() {
750     chrome.networkingPrivate.setWifiTDLSEnabledState(
751         'aa:bb:cc:dd:ee:ff', true, callbackPass(function(result) {
752           assertEq(ConnectionStateType.CONNECTED, result);
753         }));
754   },
755   function getWifiTDLSStatus() {
756     chrome.networkingPrivate.getWifiTDLSStatus(
757         'aa:bb:cc:dd:ee:ff', callbackPass(function(result) {
758           assertEq(ConnectionStateType.CONNECTED, result);
759         }));
760   },
761   function getCaptivePortalStatus() {
762     var networks = [['stub_ethernet_guid', 'Online'],
763                     ['stub_wifi1_guid', 'Offline'],
764                     ['stub_wifi2_guid', 'Portal'],
765                     ['stub_cellular1_guid', 'ProxyAuthRequired'],
766                     ['stub_vpn1_guid', 'Unknown']];
767     networks.forEach(function(network) {
768       var guid = network[0];
769       var expectedStatus = network[1];
770       chrome.networkingPrivate.getCaptivePortalStatus(
771         guid,
772         callbackPass(function(status) {
773           assertEq(expectedStatus, status);
774         }));
775     });
776   },
777   function captivePortalNotification() {
778     var done = chrome.test.callbackAdded();
779     var listener =
780         new privateHelpers.watchForCaptivePortalState(
781             'wifi_guid', 'Online', done);
782     chrome.test.sendMessage('notifyPortalDetectorObservers');
783   },
784   function unlockCellularSim() {
785     var incorrectPin = '2222';
786     // Try with incorrect PIN, expect failure.
787     chrome.networkingPrivate.unlockCellularSim(
788         kCellularGuid, incorrectPin, '',
789         callbackFail('incorrect-pin', function() {
790           // Try with correct PIN, expect success.
791           chrome.networkingPrivate.unlockCellularSim(
792               kCellularGuid, kDefaultPin, '', networkCallbackPass());
793         }));
794   },
795   function setCellularSimState() {
796     var newPin = '6666';
797     var simState = {requirePin: true, currentPin: kDefaultPin, newPin: newPin};
798     // Test setting 'requirePin' and 'newPin'.
799     chrome.networkingPrivate.getProperties(
800         kCellularGuid, callbackPass(function(result) {
801           // Ensure the SIM is initially unlocked.
802           assertTrue(result.Cellular.SIMLockStatus == undefined ||
803                      result.Cellular.SIMLockStatus.LockType == '');
804           chrome.networkingPrivate.setCellularSimState(
805               kCellularGuid, simState, callbackPass(function() {
806                 chrome.networkingPrivate.getProperties(
807                     kCellularGuid, callbackPass(function(result) {
808                       // The SIM should still be unlocked.
809                       assertEq('', result.Cellular.SIMLockStatus.LockType);
810                       // Ensure SIM locking is enabled.
811                       assertTrue(result.Cellular.SIMLockStatus.LockEnabled);
812                       // Ensure the new pin is set by using the new PIN
813                       // to change the PIN back.
814                       simState.currentPin = newPin;
815                       simState.newPin = kDefaultPin;
816                       chrome.networkingPrivate.setCellularSimState(
817                           kCellularGuid, simState, networkCallbackPass());
818                     }));
819               }));
820         }));
821   },
822   function cellularSimPuk() {
823     var newPin = '6666';
824     var incorrectPin = '2222';
825     var incorrectPuk = '22222222';
826     var unlockFailFunc = function(nextFunc) {
827       chrome.networkingPrivate.unlockCellularSim(
828           kCellularGuid, incorrectPin, '',
829           callbackFail('incorrect-pin', nextFunc));
830     };
831     // Try with incorrect PIN three times, SIM should become PUK locked.
832     unlockFailFunc(unlockFailFunc(unlockFailFunc(function() {
833       // Ensure the SIM is PUK locked.
834       chrome.networkingPrivate.getProperties(
835           kCellularGuid, callbackPass(function(result) {
836             assertEq('sim-puk', result.Cellular.SIMLockStatus.LockType);
837             // Try to unlock with an incorrect PUK, expect failure.
838             chrome.networkingPrivate.unlockCellularSim(
839                 kCellularGuid, newPin, incorrectPuk,
840                 callbackFail('incorrect-pin', function() {
841                   // Try with the correct PUK, expect success.
842                   chrome.networkingPrivate.unlockCellularSim(
843                       kCellularGuid, newPin, kDefaultPuk,
844                       callbackPass(function() {
845                         // Set state with the new PIN, expect success.
846                         var simState = {requirePin: true, currentPin: newPin};
847                         chrome.networkingPrivate.setCellularSimState(
848                             kCellularGuid, simState, networkCallbackPass());
849                       }));
850                 }));
851           }));
852     })));
853   }
856 var testToRun = window.location.search.substring(1);
857 chrome.test.runTests(availableTests.filter(function(op) {
858   return op.name == testToRun;
859 }));