|
1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 'use strict'; |
|
6 |
|
7 const kInterfaceName = 'wifi'; |
|
8 const kOtherInterfaceName = 'ril'; |
|
9 |
|
10 var server; |
|
11 var step = 0; |
|
12 var loginFinished = false; |
|
13 |
|
14 function xhr_handler(metadata, response) { |
|
15 response.setStatusLine(metadata.httpVersion, 200, 'OK'); |
|
16 response.setHeader('Cache-Control', 'no-cache', false); |
|
17 response.setHeader('Content-Type', 'text/plain', false); |
|
18 if (loginFinished) { |
|
19 response.write('true'); |
|
20 } else { |
|
21 response.write('false'); |
|
22 } |
|
23 } |
|
24 |
|
25 function fakeUIResponse() { |
|
26 Services.obs.addObserver(function observe(subject, topic, data) { |
|
27 if (topic === 'captive-portal-login') { |
|
28 let xhr = Cc['@mozilla.org/xmlextras/xmlhttprequest;1'] |
|
29 .createInstance(Ci.nsIXMLHttpRequest); |
|
30 xhr.open('GET', gServerURL + kCanonicalSitePath, true); |
|
31 xhr.send(); |
|
32 loginFinished = true; |
|
33 do_check_eq(++step, 3); |
|
34 } |
|
35 }, 'captive-portal-login', false); |
|
36 } |
|
37 |
|
38 function test_multiple_requests_abort() { |
|
39 do_test_pending(); |
|
40 |
|
41 let callback = { |
|
42 QueryInterface: XPCOMUtils.generateQI([Ci.nsICaptivePortalCallback]), |
|
43 prepare: function prepare() { |
|
44 do_check_eq(++step, 1); |
|
45 gCaptivePortalDetector.finishPreparation(kInterfaceName); |
|
46 }, |
|
47 complete: function complete(success) { |
|
48 do_throw('should not execute |complete| callback for ' + kInterfaceName); |
|
49 }, |
|
50 }; |
|
51 |
|
52 let otherCallback = { |
|
53 QueryInterface: XPCOMUtils.generateQI([Ci.nsICaptivePortalCallback]), |
|
54 prepare: function prepare() { |
|
55 do_check_eq(++step, 2); |
|
56 gCaptivePortalDetector.finishPreparation(kOtherInterfaceName); |
|
57 }, |
|
58 complete: function complete(success) { |
|
59 do_check_eq(++step, 4); |
|
60 do_check_true(success); |
|
61 gServer.stop(do_test_finished); |
|
62 } |
|
63 }; |
|
64 |
|
65 gCaptivePortalDetector.checkCaptivePortal(kInterfaceName, callback); |
|
66 gCaptivePortalDetector.checkCaptivePortal(kOtherInterfaceName, otherCallback); |
|
67 gCaptivePortalDetector.abort(kInterfaceName); |
|
68 } |
|
69 |
|
70 function run_test() { |
|
71 run_captivedetect_test(xhr_handler, fakeUIResponse, test_multiple_requests_abort); |
|
72 } |