michael@0: /* vim:set ts=2 sw=2 sts=2 et: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: let scope = {}; michael@0: Cu.import("resource://gre/modules/PermissionSettings.jsm", scope); michael@0: michael@0: const TEST_URL = michael@0: "http://mochi.test:8888/browser/dom/tests/browser/test-webapps-permissions.html"; michael@0: const TEST_MANIFEST_URL = michael@0: "http://mochi.test:8888/browser/dom/tests/browser/test-webapp.webapp"; michael@0: const TEST_ORIGIN_URL = "http://mochi.test:8888"; michael@0: michael@0: const installedPermsToTest = { michael@0: "geolocation": "prompt", michael@0: "alarms": "allow", michael@0: "contacts": "deny", michael@0: "device-storage:apps": "deny", michael@0: }; michael@0: michael@0: const reinstalledPermsToTest = { michael@0: "geolocation": "prompt", michael@0: "alarms": "unknown", michael@0: "contacts": "deny", michael@0: "device-storage:apps": "deny", michael@0: }; michael@0: michael@0: var gWindow, gNavigator; michael@0: michael@0: function test() { michael@0: waitForExplicitFinish(); michael@0: michael@0: var tab = gBrowser.addTab(TEST_URL); michael@0: gBrowser.selectedTab = tab; michael@0: var browser = gBrowser.selectedBrowser; michael@0: PopupNotifications.panel.addEventListener("popupshown", handlePopup, false); michael@0: michael@0: registerCleanupFunction(function () { michael@0: gWindow = null; michael@0: gBrowser.removeTab(tab); michael@0: }); michael@0: michael@0: browser.addEventListener("DOMContentLoaded", function onLoad(event) { michael@0: browser.removeEventListener("DOMContentLoaded", onLoad, false); michael@0: gWindow = browser.contentWindow; michael@0: SpecialPowers.setBoolPref("dom.mozPermissionSettings.enabled", true); michael@0: SpecialPowers.addPermission("permissions", true, browser.contentWindow.document); michael@0: SpecialPowers.addPermission("permissions", true, browser.contentDocument); michael@0: michael@0: let pendingInstall; michael@0: michael@0: function testInstall() { michael@0: var nav = XPCNativeWrapper.unwrap(browser.contentWindow.navigator); michael@0: ok(nav.mozApps, "we have a mozApps property"); michael@0: var navMozPerms = nav.mozPermissionSettings; michael@0: ok(navMozPerms, "mozPermissions is available"); michael@0: michael@0: // INSTALL app michael@0: pendingInstall = nav.mozApps.install(TEST_MANIFEST_URL, null); michael@0: pendingInstall.onsuccess = function onsuccess() michael@0: { michael@0: ok(this.result, "we have a result: " + this.result); michael@0: function testPerm(aPerm, aAccess) michael@0: { michael@0: var res = michael@0: navMozPerms.get(aPerm, TEST_MANIFEST_URL, TEST_ORIGIN_URL, false); michael@0: is(res, aAccess, "install: " + aPerm + " is " + res); michael@0: } michael@0: michael@0: for (let permName in installedPermsToTest) { michael@0: testPerm(permName, installedPermsToTest[permName]); michael@0: } michael@0: michael@0: writeUpdatesToWebappManifest(); michael@0: }; michael@0: michael@0: pendingInstall.onerror = function onerror(e) michael@0: { michael@0: ok(false, "install()'s onerror was called: " + e); michael@0: ok(false, "All permission checks failed, reinstall tests were not run"); michael@0: }; michael@0: } michael@0: testInstall(); michael@0: }, false); michael@0: } michael@0: michael@0: function reinstallApp() michael@0: { michael@0: var browser = gBrowser.selectedBrowser; michael@0: var nav = XPCNativeWrapper.unwrap(browser.contentWindow.navigator); michael@0: var navMozPerms = nav.mozPermissionSettings; michael@0: michael@0: var pendingReinstall = nav.mozApps.install(TEST_MANIFEST_URL); michael@0: pendingReinstall.onsuccess = function onsuccess() michael@0: { michael@0: ok(this.result, "we have a result: " + this.result); michael@0: michael@0: function testPerm(aPerm, aAccess) michael@0: { michael@0: var res = michael@0: navMozPerms.get(aPerm, TEST_MANIFEST_URL, TEST_ORIGIN_URL, false); michael@0: is(res, aAccess, "reinstall: " + aPerm + " is " + res); michael@0: } michael@0: michael@0: for (let permName in reinstalledPermsToTest) { michael@0: testPerm(permName, reinstalledPermsToTest[permName]); michael@0: } michael@0: writeUpdatesToWebappManifest(); michael@0: finish(); michael@0: }; michael@0: }; michael@0: michael@0: var qtyPopups = 0; michael@0: michael@0: function handlePopup(aEvent) michael@0: { michael@0: qtyPopups++; michael@0: if (qtyPopups == 2) { michael@0: aEvent.target.removeEventListener("popupshown", handlePopup, false); michael@0: } michael@0: SpecialPowers.wrap(this).childNodes[0].button.doCommand(); michael@0: } michael@0: michael@0: function writeUpdatesToWebappManifest(aRestore) michael@0: { michael@0: let newfile = Cc["@mozilla.org/file/directory_service;1"]. michael@0: getService(Ci.nsIProperties). michael@0: get("XCurProcD", Ci.nsIFile); michael@0: michael@0: let devPath = ["_tests", "testing", "mochitest", michael@0: "browser", "dom" , "tests", "browser"]; michael@0: // make package-tests moves tests to: michael@0: // dist/test-package-stage/mochitest/browser/dom/tests/browser michael@0: let slavePath = ["dist", "test-package-stage", "mochitest", michael@0: "browser", "dom", "tests", "browser"]; michael@0: michael@0: newfile = newfile.parent; // up to dist/ michael@0: newfile = newfile.parent;// up to obj-dir/ michael@0: michael@0: info(newfile.path); michael@0: try { michael@0: for (let idx in devPath) { michael@0: newfile.append(devPath[idx]); michael@0: if (!newfile.isDirectory()) { michael@0: info("*** NOT RUNNING ON DEV MACHINE\n\n"); michael@0: throw new Error("Test is not running on dev machine, try dev path"); michael@0: } michael@0: } michael@0: } michael@0: catch (ex) { michael@0: info(ex + "\n\n"); michael@0: for (let idx in slavePath) { michael@0: newfile.append(slavePath[idx]); michael@0: if (!newfile.isDirectory()) { michael@0: info("*** NOT RUNNING ON BUILD SLAVE\n\n"); michael@0: throw new Error("Test is not running on slave machine, abort test"); michael@0: } michael@0: } michael@0: } michael@0: michael@0: info("Final path: " + newfile.path); michael@0: michael@0: if (aRestore) { michael@0: newfile.append("test-webapp-original.webapp"); michael@0: } else { michael@0: newfile.append("test-webapp-reinstall.webapp"); michael@0: } michael@0: michael@0: let oldfile = newfile.parent; michael@0: oldfile.append("test-webapp.webapp"); michael@0: michael@0: newfile.copyTo(null, "test-webapp.webapp"); michael@0: michael@0: if (!aRestore) { michael@0: executeSoon(function (){ reinstallApp(); }); michael@0: } michael@0: }