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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/captivedetect/test/unit/test_captive_portal_found.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,58 @@
     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 +
    1.12 +var server;
    1.13 +var step = 0;
    1.14 +var loginFinished = false;
    1.15 +
    1.16 +function xhr_handler(metadata, response) {
    1.17 +  response.setStatusLine(metadata.httpVersion, 200, 'OK');
    1.18 +  response.setHeader('Cache-Control', 'no-cache', false);
    1.19 +  response.setHeader('Content-Type', 'text/plain', false);
    1.20 +  if (loginFinished) {
    1.21 +    response.write('true');
    1.22 +  } else {
    1.23 +    response.write('false');
    1.24 +  }
    1.25 +}
    1.26 +
    1.27 +function fakeUIResponse() {
    1.28 +  Services.obs.addObserver(function observe(subject, topic, data) {
    1.29 +    if (topic === 'captive-portal-login') {
    1.30 +      let xhr = Cc['@mozilla.org/xmlextras/xmlhttprequest;1']
    1.31 +                  .createInstance(Ci.nsIXMLHttpRequest);
    1.32 +      xhr.open('GET', gServerURL + kCanonicalSitePath, true);
    1.33 +      xhr.send();
    1.34 +      loginFinished = true;
    1.35 +      do_check_eq(++step, 2);
    1.36 +    }
    1.37 +  }, 'captive-portal-login', false);
    1.38 +}
    1.39 +
    1.40 +function test_portal_found() {
    1.41 +  do_test_pending();
    1.42 +
    1.43 +  let callback = {
    1.44 +    QueryInterface: XPCOMUtils.generateQI([Ci.nsICaptivePortalCallback]),
    1.45 +    prepare: function prepare() {
    1.46 +      do_check_eq(++step, 1);
    1.47 +      gCaptivePortalDetector.finishPreparation(kInterfaceName);
    1.48 +    },
    1.49 +    complete: function complete(success) {
    1.50 +      do_check_eq(++step, 3);
    1.51 +      do_check_true(success);
    1.52 +      gServer.stop(do_test_finished);
    1.53 +    },
    1.54 +  };
    1.55 +
    1.56 +  gCaptivePortalDetector.checkCaptivePortal(kInterfaceName, callback);
    1.57 +}
    1.58 +
    1.59 +function run_test() {
    1.60 +  run_captivedetect_test(xhr_handler, fakeUIResponse, test_portal_found);
    1.61 +}

mercurial