webapprt/content/mochitest-shared.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 /* Note: this script is loaded by both mochitest.js and head.js, so make sure
michael@0 6 * the code you put here can be evaluated by both! */
michael@0 7
michael@0 8 Cu.import("resource://webapprt/modules/WebappRT.jsm");
michael@0 9
michael@0 10 // When WebappsHandler opens an install confirmation dialog for apps we install,
michael@0 11 // close it, which will be seen as the equivalent of cancelling the install.
michael@0 12 // This doesn't prevent us from installing those apps, as we listen for the same
michael@0 13 // notification as WebappsHandler and do the install ourselves. It just
michael@0 14 // prevents the modal installation confirmation dialogs from hanging tests.
michael@0 15 Services.ww.registerNotification({
michael@0 16 observe: function(win, topic) {
michael@0 17 if (topic == "domwindowopened") {
michael@0 18 // Wait for load because the window is not yet sufficiently initialized.
michael@0 19 win.addEventListener("load", function onLoadWindow() {
michael@0 20 win.removeEventListener("load", onLoadWindow, false);
michael@0 21 if (win.location == "chrome://global/content/commonDialog.xul" &&
michael@0 22 win.opener == window) {
michael@0 23 win.close();
michael@0 24 }
michael@0 25 }, false);
michael@0 26 }
michael@0 27 }
michael@0 28 });
michael@0 29
michael@0 30 /**
michael@0 31 * Transmogrify the runtime session into one for the given webapp.
michael@0 32 *
michael@0 33 * @param {String} manifestURL
michael@0 34 * The URL of the webapp's manifest, relative to the base URL.
michael@0 35 * Note that the base URL points to the *chrome* WebappRT mochitests,
michael@0 36 * so you must supply an absolute URL to manifests elsewhere.
michael@0 37 * @param {Object} parameters
michael@0 38 * The value to pass as the "parameters" argument to
michael@0 39 * mozIDOMApplicationRegistry.install, e.g., { receipts: ... }.
michael@0 40 * Use undefined to pass nothing.
michael@0 41 * @param {Function} onBecome
michael@0 42 * The callback to call once the transmogrification is complete.
michael@0 43 */
michael@0 44 function becomeWebapp(manifestURL, parameters, onBecome) {
michael@0 45 function observeInstall(subj, topic, data) {
michael@0 46 Services.obs.removeObserver(observeInstall, "webapps-ask-install");
michael@0 47
michael@0 48 // Step 2: Configure the runtime session to represent the app.
michael@0 49 // We load DOMApplicationRegistry into a local scope to avoid appearing
michael@0 50 // to leak it.
michael@0 51
michael@0 52 let scope = {};
michael@0 53 Cu.import("resource://gre/modules/Webapps.jsm", scope);
michael@0 54 Cu.import("resource://webapprt/modules/Startup.jsm", scope);
michael@0 55 scope.DOMApplicationRegistry.confirmInstall(JSON.parse(data));
michael@0 56
michael@0 57 let installRecord = JSON.parse(data);
michael@0 58 installRecord.mm = subj;
michael@0 59 installRecord.registryDir = Services.dirsvc.get("ProfD", Ci.nsIFile).path;
michael@0 60 WebappRT.config = installRecord;
michael@0 61
michael@0 62 let win = Services.wm.getMostRecentWindow("webapprt:webapp");
michael@0 63 if (!win) {
michael@0 64 win = Services.ww.openWindow(null,
michael@0 65 "chrome://webapprt/content/webapp.xul",
michael@0 66 "_blank",
michael@0 67 "chrome,dialog=no,resizable,scrollbars,centerscreen",
michael@0 68 null);
michael@0 69 }
michael@0 70
michael@0 71 let promise = scope.startup(win);
michael@0 72
michael@0 73 // During chrome tests, we use the same window to load all the tests. We
michael@0 74 // need to change the buildID so that the permissions for the currently
michael@0 75 // tested application get installed.
michael@0 76 Services.prefs.setCharPref("webapprt.buildID", WebappRT.config.app.manifestURL);
michael@0 77
michael@0 78 // During tests, the webapps registry is already loaded.
michael@0 79 // The Startup module needs to be notified when the webapps registry
michael@0 80 // gets loaded, so we do that now.
michael@0 81 Services.obs.notifyObservers(this, "webapps-registry-start", null);
michael@0 82
michael@0 83 promise.then(onBecome);
michael@0 84 }
michael@0 85 Services.obs.addObserver(observeInstall, "webapps-ask-install", false);
michael@0 86
michael@0 87 // Step 1: Install the app at the URL specified by the manifest.
michael@0 88 navigator.mozApps.install(manifestURL, parameters);
michael@0 89 }

mercurial