toolkit/mozapps/extensions/test/browser/browser_metadataTimeout.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/mozapps/extensions/test/browser/browser_metadataTimeout.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,113 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 + * http://creativecommons.org/publicdomain/zero/1.0/
     1.6 + */
     1.7 +
     1.8 +// Test how update window behaves when metadata ping times out
     1.9 +// bug 965788
    1.10 +
    1.11 +const URI_EXTENSION_UPDATE_DIALOG = "chrome://mozapps/content/extensions/update.xul";
    1.12 +
    1.13 +const PREF_GETADDONS_BYIDS            = "extensions.getAddons.get.url";
    1.14 +const PREF_MIN_PLATFORM_COMPAT        = "extensions.minCompatiblePlatformVersion";
    1.15 +
    1.16 +Components.utils.import("resource://gre/modules/Promise.jsm");
    1.17 +
    1.18 +let repo = {};
    1.19 +let ARContext = Components.utils.import("resource://gre/modules/addons/AddonRepository.jsm", repo);
    1.20 +info("ARContext: " + Object.keys(ARContext).join(", "));
    1.21 +
    1.22 +// Mock out the XMLHttpRequest factory for AddonRepository so
    1.23 +// we can reply with a timeout
    1.24 +let pXHRStarted = Promise.defer();
    1.25 +let oldXHRConstructor = ARContext.XHRequest;
    1.26 +ARContext.XHRequest = function() {
    1.27 +  this._handlers = new Map();
    1.28 +  this.mozBackgroundRequest = false;
    1.29 +  this.timeout = undefined;
    1.30 +  this.open = function(aMethod, aURI, aAsync) {
    1.31 +      this.method = aMethod;
    1.32 +      this.uri = aURI;
    1.33 +      this.async = aAsync;
    1.34 +      info("Opened XHR for " + aMethod + " " + aURI);
    1.35 +    };
    1.36 +  this.overrideMimeType = function(aMimeType) {
    1.37 +      this.mimeType = aMimeType;
    1.38 +    };
    1.39 +  this.addEventListener = function(aEvent, aHandler, aCapture) {
    1.40 +      this._handlers.set(aEvent, aHandler);
    1.41 +    };
    1.42 +  this.send = function(aBody) {
    1.43 +      info("Send XHR for " + this.method + " " + this.uri + " handlers: " + [this._handlers.keys()].join(", "));
    1.44 +      pXHRStarted.resolve(this);
    1.45 +    }
    1.46 +};
    1.47 +
    1.48 +
    1.49 +// Returns promise{window}, resolves with a handle to the compatibility
    1.50 +// check window
    1.51 +function promise_open_compatibility_window(aInactiveAddonIds) {
    1.52 +  let deferred = Promise.defer();
    1.53 +  // This will reset the longer timeout multiplier to 2 which will give each
    1.54 +  // test that calls open_compatibility_window a minimum of 60 seconds to
    1.55 +  // complete.
    1.56 +  requestLongerTimeout(100 /* XXX was 2 */);
    1.57 +
    1.58 +  var variant = Cc["@mozilla.org/variant;1"].
    1.59 +                createInstance(Ci.nsIWritableVariant);
    1.60 +  variant.setFromVariant(aInactiveAddonIds);
    1.61 +
    1.62 +  // Cannot be modal as we want to interract with it, shouldn't cause problems
    1.63 +  // with testing though.
    1.64 +  var features = "chrome,centerscreen,dialog,titlebar";
    1.65 +  var ww = Cc["@mozilla.org/embedcomp/window-watcher;1"].
    1.66 +           getService(Ci.nsIWindowWatcher);
    1.67 +  var win = ww.openWindow(null, URI_EXTENSION_UPDATE_DIALOG, "", features, variant);
    1.68 +
    1.69 +  win.addEventListener("load", function() {
    1.70 +    function page_shown(aEvent) {
    1.71 +      if (aEvent.target.pageid)
    1.72 +        info("Page " + aEvent.target.pageid + " shown");
    1.73 +    }
    1.74 +
    1.75 +    win.removeEventListener("load", arguments.callee, false);
    1.76 +
    1.77 +    info("Compatibility dialog opened");
    1.78 +
    1.79 +    win.addEventListener("pageshow", page_shown, false);
    1.80 +    win.addEventListener("unload", function() {
    1.81 +      win.removeEventListener("unload", arguments.callee, false);
    1.82 +      win.removeEventListener("pageshow", page_shown, false);
    1.83 +      dump("Compatibility dialog closed\n");
    1.84 +    }, false);
    1.85 +
    1.86 +    deferred.resolve(win);
    1.87 +  }, false);
    1.88 +  return deferred.promise;
    1.89 +}
    1.90 +
    1.91 +function promise_window_close(aWindow) {
    1.92 +  let deferred = Promise.defer();
    1.93 +  aWindow.addEventListener("unload", function() {
    1.94 +    aWindow.removeEventListener("unload", arguments.callee, false);
    1.95 +    deferred.resolve(aWindow);
    1.96 +  }, false);
    1.97 +  return deferred.promise;
    1.98 +}
    1.99 +
   1.100 +// Start the compatibility update dialog, but use the mock XHR to respond with
   1.101 +// a timeout
   1.102 +add_task(function* amo_ping_timeout() {
   1.103 +  Services.prefs.setBoolPref(PREF_GETADDONS_CACHE_ENABLED, true);
   1.104 +  let compatWindow = yield promise_open_compatibility_window([]);
   1.105 +
   1.106 +  let xhr = yield pXHRStarted.promise;
   1.107 +  is(xhr.timeout, 30000, "XHR request should have 30 second timeout");
   1.108 +  ok(xhr._handlers.has("timeout"), "Timeout handler set on XHR");
   1.109 +  // call back the timeout handler
   1.110 +  xhr._handlers.get("timeout")();
   1.111 +
   1.112 +  // Put the old XHR constructor back
   1.113 +  ARContext.XHRequest = oldXHRConstructor;
   1.114 +  // The window should close without further interaction
   1.115 +  yield promise_window_close(compatWindow);
   1.116 +});

mercurial