dom/tests/browser/browser_webapps_permissions.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 /* vim:set ts=2 sw=2 sts=2 et: */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 const DEBUG = 0;
michael@0 7 function log()
michael@0 8 {
michael@0 9 if (DEBUG) {
michael@0 10 let output = [];
michael@0 11 for (let prop in arguments) {
michael@0 12 output.push(arguments[prop]);
michael@0 13 }
michael@0 14 dump("-*- browser_webapps_permissions test: " + output.join(" ") + "\n");
michael@0 15 }
michael@0 16 }
michael@0 17
michael@0 18 let scope = {};
michael@0 19 Cu.import("resource://gre/modules/PermissionSettings.jsm", scope);
michael@0 20
michael@0 21 var windowMediator = Cc["@mozilla.org/appshell/window-mediator;1"]
michael@0 22 .getService(Ci.nsIWindowMediator);
michael@0 23
michael@0 24 const TEST_URL =
michael@0 25 "http://mochi.test:8888/browser/dom/tests/browser/test-webapps-permissions.html";
michael@0 26 const TEST_MANIFEST_URL =
michael@0 27 "http://mochi.test:8888/browser/dom/tests/browser/test-webapp.webapp";
michael@0 28 const TEST_ORIGIN_URL = "http://mochi.test:8888";
michael@0 29
michael@0 30 const installedPermsToTest = {
michael@0 31 "storage": "unknown",
michael@0 32 "geolocation": "prompt",
michael@0 33 "alarms": "allow",
michael@0 34 "desktop-notification": "allow",
michael@0 35 "audio-channel-normal": "allow"
michael@0 36 };
michael@0 37
michael@0 38 const uninstalledPermsToTest = {
michael@0 39 "geolocation": "unknown",
michael@0 40 "alarms": "unknown",
michael@0 41 "desktop-notification": "unknown",
michael@0 42 "audio-channel-normal": "unknown"
michael@0 43 };
michael@0 44
michael@0 45 var permManager = Cc["@mozilla.org/permissionmanager;1"]
michael@0 46 .getService(Ci.nsIPermissionManager);
michael@0 47 permManager.addFromPrincipal(window.document.nodePrincipal,
michael@0 48 "webapps-manage",
michael@0 49 Ci.nsIPermissionManager.ALLOW_ACTION);
michael@0 50
michael@0 51 var gWindow, gNavigator;
michael@0 52
michael@0 53 function test() {
michael@0 54 waitForExplicitFinish();
michael@0 55
michael@0 56 var tab = gBrowser.addTab(TEST_URL);
michael@0 57 gBrowser.selectedTab = tab;
michael@0 58 var browser = gBrowser.selectedBrowser;
michael@0 59 PopupNotifications.panel.addEventListener("popupshown", handlePopup, false);
michael@0 60
michael@0 61 registerCleanupFunction(function () {
michael@0 62 gWindow = null;
michael@0 63 gBrowser.removeTab(tab);
michael@0 64
michael@0 65 // The installation may have created a XUL alert window
michael@0 66 // (see notifyInstallSuccess in WebappManager.jsm).
michael@0 67 // It need to be closed before the test finishes.
michael@0 68 var browsers = windowMediator.getEnumerator('alert:alert');
michael@0 69 while (browsers.hasMoreElements()) {
michael@0 70 browsers.getNext().close();
michael@0 71 }
michael@0 72 });
michael@0 73
michael@0 74 browser.addEventListener("DOMContentLoaded", function onLoad(event) {
michael@0 75 browser.removeEventListener("DOMContentLoaded", onLoad, false);
michael@0 76 gWindow = browser.contentWindow;
michael@0 77
michael@0 78 SpecialPowers.setBoolPref("dom.mozPermissionSettings.enabled", true);
michael@0 79 SpecialPowers.addPermission("permissions", true, browser.contentWindow.document);
michael@0 80 SpecialPowers.addPermission("permissions", true, browser.contentDocument);
michael@0 81 SpecialPowers.addPermission("webapps-manage", true, browser.contentWindow.document);
michael@0 82
michael@0 83 executeSoon(function (){
michael@0 84 gWindow.focus();
michael@0 85 var nav = XPCNativeWrapper.unwrap(browser.contentWindow.navigator);
michael@0 86 ok(nav.mozApps, "we have a mozApps property");
michael@0 87 var navMozPerms = nav.mozPermissionSettings;
michael@0 88 ok(navMozPerms, "mozPermissions is available");
michael@0 89
michael@0 90 // INSTALL app
michael@0 91 var pendingInstall = nav.mozApps.install(TEST_MANIFEST_URL, null);
michael@0 92 pendingInstall.onsuccess = function onsuccess()
michael@0 93 {
michael@0 94 ok(this.result, "we have a result: " + this.result);
michael@0 95
michael@0 96 function testPerm(aPerm, aAccess)
michael@0 97 {
michael@0 98 var res =
michael@0 99 navMozPerms.get(aPerm, TEST_MANIFEST_URL, TEST_ORIGIN_URL, false);
michael@0 100 is(res, aAccess, "install: " + aPerm + " is " + res);
michael@0 101 }
michael@0 102
michael@0 103 for (let permName in installedPermsToTest) {
michael@0 104 testPerm(permName, installedPermsToTest[permName]);
michael@0 105 }
michael@0 106
michael@0 107 // uninstall checks
michael@0 108 uninstallApp();
michael@0 109 };
michael@0 110
michael@0 111 pendingInstall.onerror = function onerror(e)
michael@0 112 {
michael@0 113 ok(false, "install()'s onerror was called: " + e);
michael@0 114 ok(false, "All permission checks failed, uninstal tests were not run");
michael@0 115 };
michael@0 116 });
michael@0 117 }, false);
michael@0 118 }
michael@0 119
michael@0 120 function uninstallApp()
michael@0 121 {
michael@0 122 var browser = gBrowser.selectedBrowser;
michael@0 123 var nav = XPCNativeWrapper.unwrap(browser.contentWindow.navigator);
michael@0 124 var navMozPerms = nav.mozPermissionSettings;
michael@0 125
michael@0 126 var pending = nav.mozApps.getInstalled();
michael@0 127 pending.onsuccess = function onsuccess() {
michael@0 128 var m = this.result;
michael@0 129 for (var i = 0; i < m.length; i++) {
michael@0 130 var app = m[i];
michael@0 131
michael@0 132 function uninstall() {
michael@0 133 var pendingUninstall = nav.mozApps.mgmt.uninstall(app);
michael@0 134
michael@0 135 pendingUninstall.onsuccess = function(r) {
michael@0 136 // test to make sure all permissions have been removed
michael@0 137 function testPerm(aPerm, aAccess)
michael@0 138 {
michael@0 139 var res =
michael@0 140 navMozPerms.get(aPerm, TEST_MANIFEST_URL, TEST_ORIGIN_URL, false);
michael@0 141 is(res, aAccess, "uninstall: " + aPerm + " is " + res);
michael@0 142 }
michael@0 143
michael@0 144 for (let permName in uninstalledPermsToTest) {
michael@0 145 testPerm(permName, uninstalledPermsToTest[permName]);
michael@0 146 }
michael@0 147
michael@0 148 finish();
michael@0 149 };
michael@0 150
michael@0 151 pending.onerror = function _onerror(e) {
michael@0 152 ok(false, e);
michael@0 153 ok(false, "All uninstall() permission checks failed!");
michael@0 154
michael@0 155 finish();
michael@0 156 };
michael@0 157 };
michael@0 158 uninstall();
michael@0 159 }
michael@0 160 };
michael@0 161 }
michael@0 162
michael@0 163 function handlePopup(aEvent)
michael@0 164 {
michael@0 165 aEvent.target.removeEventListener("popupshown", handlePopup, false);
michael@0 166 SpecialPowers.wrap(this).childNodes[0].button.doCommand();
michael@0 167 }

mercurial