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: const DEBUG = 0; michael@0: function log() michael@0: { michael@0: if (DEBUG) { michael@0: let output = []; michael@0: for (let prop in arguments) { michael@0: output.push(arguments[prop]); michael@0: } michael@0: dump("-*- browser_webapps_permissions test: " + output.join(" ") + "\n"); michael@0: } michael@0: } michael@0: michael@0: let scope = {}; michael@0: Cu.import("resource://gre/modules/PermissionSettings.jsm", scope); michael@0: michael@0: var windowMediator = Cc["@mozilla.org/appshell/window-mediator;1"] michael@0: .getService(Ci.nsIWindowMediator); 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: "storage": "unknown", michael@0: "geolocation": "prompt", michael@0: "alarms": "allow", michael@0: "desktop-notification": "allow", michael@0: "audio-channel-normal": "allow" michael@0: }; michael@0: michael@0: const uninstalledPermsToTest = { michael@0: "geolocation": "unknown", michael@0: "alarms": "unknown", michael@0: "desktop-notification": "unknown", michael@0: "audio-channel-normal": "unknown" michael@0: }; michael@0: michael@0: var permManager = Cc["@mozilla.org/permissionmanager;1"] michael@0: .getService(Ci.nsIPermissionManager); michael@0: permManager.addFromPrincipal(window.document.nodePrincipal, michael@0: "webapps-manage", michael@0: Ci.nsIPermissionManager.ALLOW_ACTION); 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: // The installation may have created a XUL alert window michael@0: // (see notifyInstallSuccess in WebappManager.jsm). michael@0: // It need to be closed before the test finishes. michael@0: var browsers = windowMediator.getEnumerator('alert:alert'); michael@0: while (browsers.hasMoreElements()) { michael@0: browsers.getNext().close(); michael@0: } 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: 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: SpecialPowers.addPermission("webapps-manage", true, browser.contentWindow.document); michael@0: michael@0: executeSoon(function (){ michael@0: gWindow.focus(); 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: var 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: 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: // uninstall checks michael@0: uninstallApp(); 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, uninstal tests were not run"); michael@0: }; michael@0: }); michael@0: }, false); michael@0: } michael@0: michael@0: function uninstallApp() 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 pending = nav.mozApps.getInstalled(); michael@0: pending.onsuccess = function onsuccess() { michael@0: var m = this.result; michael@0: for (var i = 0; i < m.length; i++) { michael@0: var app = m[i]; michael@0: michael@0: function uninstall() { michael@0: var pendingUninstall = nav.mozApps.mgmt.uninstall(app); michael@0: michael@0: pendingUninstall.onsuccess = function(r) { michael@0: // test to make sure all permissions have been removed 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, "uninstall: " + aPerm + " is " + res); michael@0: } michael@0: michael@0: for (let permName in uninstalledPermsToTest) { michael@0: testPerm(permName, uninstalledPermsToTest[permName]); michael@0: } michael@0: michael@0: finish(); michael@0: }; michael@0: michael@0: pending.onerror = function _onerror(e) { michael@0: ok(false, e); michael@0: ok(false, "All uninstall() permission checks failed!"); michael@0: michael@0: finish(); michael@0: }; michael@0: }; michael@0: uninstall(); michael@0: } michael@0: }; michael@0: } michael@0: michael@0: function handlePopup(aEvent) michael@0: { michael@0: aEvent.target.removeEventListener("popupshown", handlePopup, false); michael@0: SpecialPowers.wrap(this).childNodes[0].button.doCommand(); michael@0: }