1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/webapprt/content/mochitest-shared.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,89 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +/* Note: this script is loaded by both mochitest.js and head.js, so make sure 1.9 + * the code you put here can be evaluated by both! */ 1.10 + 1.11 +Cu.import("resource://webapprt/modules/WebappRT.jsm"); 1.12 + 1.13 +// When WebappsHandler opens an install confirmation dialog for apps we install, 1.14 +// close it, which will be seen as the equivalent of cancelling the install. 1.15 +// This doesn't prevent us from installing those apps, as we listen for the same 1.16 +// notification as WebappsHandler and do the install ourselves. It just 1.17 +// prevents the modal installation confirmation dialogs from hanging tests. 1.18 +Services.ww.registerNotification({ 1.19 + observe: function(win, topic) { 1.20 + if (topic == "domwindowopened") { 1.21 + // Wait for load because the window is not yet sufficiently initialized. 1.22 + win.addEventListener("load", function onLoadWindow() { 1.23 + win.removeEventListener("load", onLoadWindow, false); 1.24 + if (win.location == "chrome://global/content/commonDialog.xul" && 1.25 + win.opener == window) { 1.26 + win.close(); 1.27 + } 1.28 + }, false); 1.29 + } 1.30 + } 1.31 +}); 1.32 + 1.33 +/** 1.34 + * Transmogrify the runtime session into one for the given webapp. 1.35 + * 1.36 + * @param {String} manifestURL 1.37 + * The URL of the webapp's manifest, relative to the base URL. 1.38 + * Note that the base URL points to the *chrome* WebappRT mochitests, 1.39 + * so you must supply an absolute URL to manifests elsewhere. 1.40 + * @param {Object} parameters 1.41 + * The value to pass as the "parameters" argument to 1.42 + * mozIDOMApplicationRegistry.install, e.g., { receipts: ... }. 1.43 + * Use undefined to pass nothing. 1.44 + * @param {Function} onBecome 1.45 + * The callback to call once the transmogrification is complete. 1.46 + */ 1.47 +function becomeWebapp(manifestURL, parameters, onBecome) { 1.48 + function observeInstall(subj, topic, data) { 1.49 + Services.obs.removeObserver(observeInstall, "webapps-ask-install"); 1.50 + 1.51 + // Step 2: Configure the runtime session to represent the app. 1.52 + // We load DOMApplicationRegistry into a local scope to avoid appearing 1.53 + // to leak it. 1.54 + 1.55 + let scope = {}; 1.56 + Cu.import("resource://gre/modules/Webapps.jsm", scope); 1.57 + Cu.import("resource://webapprt/modules/Startup.jsm", scope); 1.58 + scope.DOMApplicationRegistry.confirmInstall(JSON.parse(data)); 1.59 + 1.60 + let installRecord = JSON.parse(data); 1.61 + installRecord.mm = subj; 1.62 + installRecord.registryDir = Services.dirsvc.get("ProfD", Ci.nsIFile).path; 1.63 + WebappRT.config = installRecord; 1.64 + 1.65 + let win = Services.wm.getMostRecentWindow("webapprt:webapp"); 1.66 + if (!win) { 1.67 + win = Services.ww.openWindow(null, 1.68 + "chrome://webapprt/content/webapp.xul", 1.69 + "_blank", 1.70 + "chrome,dialog=no,resizable,scrollbars,centerscreen", 1.71 + null); 1.72 + } 1.73 + 1.74 + let promise = scope.startup(win); 1.75 + 1.76 + // During chrome tests, we use the same window to load all the tests. We 1.77 + // need to change the buildID so that the permissions for the currently 1.78 + // tested application get installed. 1.79 + Services.prefs.setCharPref("webapprt.buildID", WebappRT.config.app.manifestURL); 1.80 + 1.81 + // During tests, the webapps registry is already loaded. 1.82 + // The Startup module needs to be notified when the webapps registry 1.83 + // gets loaded, so we do that now. 1.84 + Services.obs.notifyObservers(this, "webapps-registry-start", null); 1.85 + 1.86 + promise.then(onBecome); 1.87 + } 1.88 + Services.obs.addObserver(observeInstall, "webapps-ask-install", false); 1.89 + 1.90 + // Step 1: Install the app at the URL specified by the manifest. 1.91 + navigator.mozApps.install(manifestURL, parameters); 1.92 +}