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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 * http://creativecommons.org/publicdomain/zero/1.0/
michael@0 3 */
michael@0 4
michael@0 5 // Test how update window behaves when metadata ping times out
michael@0 6 // bug 965788
michael@0 7
michael@0 8 const URI_EXTENSION_UPDATE_DIALOG = "chrome://mozapps/content/extensions/update.xul";
michael@0 9
michael@0 10 const PREF_GETADDONS_BYIDS = "extensions.getAddons.get.url";
michael@0 11 const PREF_MIN_PLATFORM_COMPAT = "extensions.minCompatiblePlatformVersion";
michael@0 12
michael@0 13 Components.utils.import("resource://gre/modules/Promise.jsm");
michael@0 14
michael@0 15 let repo = {};
michael@0 16 let ARContext = Components.utils.import("resource://gre/modules/addons/AddonRepository.jsm", repo);
michael@0 17 info("ARContext: " + Object.keys(ARContext).join(", "));
michael@0 18
michael@0 19 // Mock out the XMLHttpRequest factory for AddonRepository so
michael@0 20 // we can reply with a timeout
michael@0 21 let pXHRStarted = Promise.defer();
michael@0 22 let oldXHRConstructor = ARContext.XHRequest;
michael@0 23 ARContext.XHRequest = function() {
michael@0 24 this._handlers = new Map();
michael@0 25 this.mozBackgroundRequest = false;
michael@0 26 this.timeout = undefined;
michael@0 27 this.open = function(aMethod, aURI, aAsync) {
michael@0 28 this.method = aMethod;
michael@0 29 this.uri = aURI;
michael@0 30 this.async = aAsync;
michael@0 31 info("Opened XHR for " + aMethod + " " + aURI);
michael@0 32 };
michael@0 33 this.overrideMimeType = function(aMimeType) {
michael@0 34 this.mimeType = aMimeType;
michael@0 35 };
michael@0 36 this.addEventListener = function(aEvent, aHandler, aCapture) {
michael@0 37 this._handlers.set(aEvent, aHandler);
michael@0 38 };
michael@0 39 this.send = function(aBody) {
michael@0 40 info("Send XHR for " + this.method + " " + this.uri + " handlers: " + [this._handlers.keys()].join(", "));
michael@0 41 pXHRStarted.resolve(this);
michael@0 42 }
michael@0 43 };
michael@0 44
michael@0 45
michael@0 46 // Returns promise{window}, resolves with a handle to the compatibility
michael@0 47 // check window
michael@0 48 function promise_open_compatibility_window(aInactiveAddonIds) {
michael@0 49 let deferred = Promise.defer();
michael@0 50 // This will reset the longer timeout multiplier to 2 which will give each
michael@0 51 // test that calls open_compatibility_window a minimum of 60 seconds to
michael@0 52 // complete.
michael@0 53 requestLongerTimeout(100 /* XXX was 2 */);
michael@0 54
michael@0 55 var variant = Cc["@mozilla.org/variant;1"].
michael@0 56 createInstance(Ci.nsIWritableVariant);
michael@0 57 variant.setFromVariant(aInactiveAddonIds);
michael@0 58
michael@0 59 // Cannot be modal as we want to interract with it, shouldn't cause problems
michael@0 60 // with testing though.
michael@0 61 var features = "chrome,centerscreen,dialog,titlebar";
michael@0 62 var ww = Cc["@mozilla.org/embedcomp/window-watcher;1"].
michael@0 63 getService(Ci.nsIWindowWatcher);
michael@0 64 var win = ww.openWindow(null, URI_EXTENSION_UPDATE_DIALOG, "", features, variant);
michael@0 65
michael@0 66 win.addEventListener("load", function() {
michael@0 67 function page_shown(aEvent) {
michael@0 68 if (aEvent.target.pageid)
michael@0 69 info("Page " + aEvent.target.pageid + " shown");
michael@0 70 }
michael@0 71
michael@0 72 win.removeEventListener("load", arguments.callee, false);
michael@0 73
michael@0 74 info("Compatibility dialog opened");
michael@0 75
michael@0 76 win.addEventListener("pageshow", page_shown, false);
michael@0 77 win.addEventListener("unload", function() {
michael@0 78 win.removeEventListener("unload", arguments.callee, false);
michael@0 79 win.removeEventListener("pageshow", page_shown, false);
michael@0 80 dump("Compatibility dialog closed\n");
michael@0 81 }, false);
michael@0 82
michael@0 83 deferred.resolve(win);
michael@0 84 }, false);
michael@0 85 return deferred.promise;
michael@0 86 }
michael@0 87
michael@0 88 function promise_window_close(aWindow) {
michael@0 89 let deferred = Promise.defer();
michael@0 90 aWindow.addEventListener("unload", function() {
michael@0 91 aWindow.removeEventListener("unload", arguments.callee, false);
michael@0 92 deferred.resolve(aWindow);
michael@0 93 }, false);
michael@0 94 return deferred.promise;
michael@0 95 }
michael@0 96
michael@0 97 // Start the compatibility update dialog, but use the mock XHR to respond with
michael@0 98 // a timeout
michael@0 99 add_task(function* amo_ping_timeout() {
michael@0 100 Services.prefs.setBoolPref(PREF_GETADDONS_CACHE_ENABLED, true);
michael@0 101 let compatWindow = yield promise_open_compatibility_window([]);
michael@0 102
michael@0 103 let xhr = yield pXHRStarted.promise;
michael@0 104 is(xhr.timeout, 30000, "XHR request should have 30 second timeout");
michael@0 105 ok(xhr._handlers.has("timeout"), "Timeout handler set on XHR");
michael@0 106 // call back the timeout handler
michael@0 107 xhr._handlers.get("timeout")();
michael@0 108
michael@0 109 // Put the old XHR constructor back
michael@0 110 ARContext.XHRequest = oldXHRConstructor;
michael@0 111 // The window should close without further interaction
michael@0 112 yield promise_window_close(compatWindow);
michael@0 113 });

mercurial