toolkit/components/captivedetect/test/unit/test_abort_pending_request.js

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

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

mercurial