1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/captivedetect/test/unit/test_abort_pending_request.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,71 @@ 1.4 +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.7 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 +'use strict'; 1.9 + 1.10 +const kInterfaceName = 'wifi'; 1.11 +const kOtherInterfaceName = 'ril'; 1.12 + 1.13 +var server; 1.14 +var step = 0; 1.15 +var loginFinished = false; 1.16 + 1.17 +function xhr_handler(metadata, response) { 1.18 + response.setStatusLine(metadata.httpVersion, 200, 'OK'); 1.19 + response.setHeader('Cache-Control', 'no-cache', false); 1.20 + response.setHeader('Content-Type', 'text/plain', false); 1.21 + if (loginFinished) { 1.22 + response.write('true'); 1.23 + } else { 1.24 + response.write('false'); 1.25 + } 1.26 +} 1.27 + 1.28 +function fakeUIResponse() { 1.29 + Services.obs.addObserver(function observe(subject, topic, data) { 1.30 + if (topic === 'captive-portal-login') { 1.31 + let xhr = Cc['@mozilla.org/xmlextras/xmlhttprequest;1'] 1.32 + .createInstance(Ci.nsIXMLHttpRequest); 1.33 + xhr.open('GET', gServerURL + kCanonicalSitePath, true); 1.34 + xhr.send(); 1.35 + loginFinished = true; 1.36 + do_check_eq(++step, 2); 1.37 + } 1.38 + }, 'captive-portal-login', false); 1.39 +} 1.40 + 1.41 +function test_abort() { 1.42 + do_test_pending(); 1.43 + 1.44 + let callback = { 1.45 + QueryInterface: XPCOMUtils.generateQI([Ci.nsICaptivePortalCallback]), 1.46 + prepare: function prepare() { 1.47 + do_check_eq(++step, 1); 1.48 + gCaptivePortalDetector.finishPreparation(kInterfaceName); 1.49 + }, 1.50 + complete: function complete(success) { 1.51 + do_check_eq(++step, 3); 1.52 + do_check_true(success); 1.53 + gServer.stop(do_test_finished); 1.54 + }, 1.55 + }; 1.56 + 1.57 + let otherCallback = { 1.58 + QueryInterface: XPCOMUtils.generateQI([Ci.nsICaptivePortalCallback]), 1.59 + prepare: function prepare() { 1.60 + do_throw('should not execute |prepare| callback for ' + kOtherInterfaceName); 1.61 + }, 1.62 + complete: function complete(success) { 1.63 + do_throw('should not execute |complete| callback for ' + kInterfaceName); 1.64 + } 1.65 + }; 1.66 + 1.67 + gCaptivePortalDetector.checkCaptivePortal(kInterfaceName, callback); 1.68 + gCaptivePortalDetector.checkCaptivePortal(kOtherInterfaceName, otherCallback); 1.69 + gCaptivePortalDetector.abort(kOtherInterfaceName); 1.70 +} 1.71 + 1.72 +function run_test() { 1.73 + run_captivedetect_test(xhr_handler, fakeUIResponse, test_abort); 1.74 +}