1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/tests/browser/browser_webapps_perms_reinstall.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,178 @@ 1.4 +/* vim:set ts=2 sw=2 sts=2 et: */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +let scope = {}; 1.10 +Cu.import("resource://gre/modules/PermissionSettings.jsm", scope); 1.11 + 1.12 +const TEST_URL = 1.13 + "http://mochi.test:8888/browser/dom/tests/browser/test-webapps-permissions.html"; 1.14 +const TEST_MANIFEST_URL = 1.15 + "http://mochi.test:8888/browser/dom/tests/browser/test-webapp.webapp"; 1.16 +const TEST_ORIGIN_URL = "http://mochi.test:8888"; 1.17 + 1.18 +const installedPermsToTest = { 1.19 + "geolocation": "prompt", 1.20 + "alarms": "allow", 1.21 + "contacts": "deny", 1.22 + "device-storage:apps": "deny", 1.23 +}; 1.24 + 1.25 +const reinstalledPermsToTest = { 1.26 + "geolocation": "prompt", 1.27 + "alarms": "unknown", 1.28 + "contacts": "deny", 1.29 + "device-storage:apps": "deny", 1.30 +}; 1.31 + 1.32 +var gWindow, gNavigator; 1.33 + 1.34 +function test() { 1.35 + waitForExplicitFinish(); 1.36 + 1.37 + var tab = gBrowser.addTab(TEST_URL); 1.38 + gBrowser.selectedTab = tab; 1.39 + var browser = gBrowser.selectedBrowser; 1.40 + PopupNotifications.panel.addEventListener("popupshown", handlePopup, false); 1.41 + 1.42 + registerCleanupFunction(function () { 1.43 + gWindow = null; 1.44 + gBrowser.removeTab(tab); 1.45 + }); 1.46 + 1.47 + browser.addEventListener("DOMContentLoaded", function onLoad(event) { 1.48 + browser.removeEventListener("DOMContentLoaded", onLoad, false); 1.49 + gWindow = browser.contentWindow; 1.50 + SpecialPowers.setBoolPref("dom.mozPermissionSettings.enabled", true); 1.51 + SpecialPowers.addPermission("permissions", true, browser.contentWindow.document); 1.52 + SpecialPowers.addPermission("permissions", true, browser.contentDocument); 1.53 + 1.54 + let pendingInstall; 1.55 + 1.56 + function testInstall() { 1.57 + var nav = XPCNativeWrapper.unwrap(browser.contentWindow.navigator); 1.58 + ok(nav.mozApps, "we have a mozApps property"); 1.59 + var navMozPerms = nav.mozPermissionSettings; 1.60 + ok(navMozPerms, "mozPermissions is available"); 1.61 + 1.62 + // INSTALL app 1.63 + pendingInstall = nav.mozApps.install(TEST_MANIFEST_URL, null); 1.64 + pendingInstall.onsuccess = function onsuccess() 1.65 + { 1.66 + ok(this.result, "we have a result: " + this.result); 1.67 + function testPerm(aPerm, aAccess) 1.68 + { 1.69 + var res = 1.70 + navMozPerms.get(aPerm, TEST_MANIFEST_URL, TEST_ORIGIN_URL, false); 1.71 + is(res, aAccess, "install: " + aPerm + " is " + res); 1.72 + } 1.73 + 1.74 + for (let permName in installedPermsToTest) { 1.75 + testPerm(permName, installedPermsToTest[permName]); 1.76 + } 1.77 + 1.78 + writeUpdatesToWebappManifest(); 1.79 + }; 1.80 + 1.81 + pendingInstall.onerror = function onerror(e) 1.82 + { 1.83 + ok(false, "install()'s onerror was called: " + e); 1.84 + ok(false, "All permission checks failed, reinstall tests were not run"); 1.85 + }; 1.86 + } 1.87 + testInstall(); 1.88 + }, false); 1.89 +} 1.90 + 1.91 +function reinstallApp() 1.92 +{ 1.93 + var browser = gBrowser.selectedBrowser; 1.94 + var nav = XPCNativeWrapper.unwrap(browser.contentWindow.navigator); 1.95 + var navMozPerms = nav.mozPermissionSettings; 1.96 + 1.97 + var pendingReinstall = nav.mozApps.install(TEST_MANIFEST_URL); 1.98 + pendingReinstall.onsuccess = function onsuccess() 1.99 + { 1.100 + ok(this.result, "we have a result: " + this.result); 1.101 + 1.102 + function testPerm(aPerm, aAccess) 1.103 + { 1.104 + var res = 1.105 + navMozPerms.get(aPerm, TEST_MANIFEST_URL, TEST_ORIGIN_URL, false); 1.106 + is(res, aAccess, "reinstall: " + aPerm + " is " + res); 1.107 + } 1.108 + 1.109 + for (let permName in reinstalledPermsToTest) { 1.110 + testPerm(permName, reinstalledPermsToTest[permName]); 1.111 + } 1.112 + writeUpdatesToWebappManifest(); 1.113 + finish(); 1.114 + }; 1.115 +}; 1.116 + 1.117 +var qtyPopups = 0; 1.118 + 1.119 +function handlePopup(aEvent) 1.120 +{ 1.121 + qtyPopups++; 1.122 + if (qtyPopups == 2) { 1.123 + aEvent.target.removeEventListener("popupshown", handlePopup, false); 1.124 + } 1.125 + SpecialPowers.wrap(this).childNodes[0].button.doCommand(); 1.126 +} 1.127 + 1.128 +function writeUpdatesToWebappManifest(aRestore) 1.129 +{ 1.130 + let newfile = Cc["@mozilla.org/file/directory_service;1"]. 1.131 + getService(Ci.nsIProperties). 1.132 + get("XCurProcD", Ci.nsIFile); 1.133 + 1.134 + let devPath = ["_tests", "testing", "mochitest", 1.135 + "browser", "dom" , "tests", "browser"]; 1.136 + // make package-tests moves tests to: 1.137 + // dist/test-package-stage/mochitest/browser/dom/tests/browser 1.138 + let slavePath = ["dist", "test-package-stage", "mochitest", 1.139 + "browser", "dom", "tests", "browser"]; 1.140 + 1.141 + newfile = newfile.parent; // up to dist/ 1.142 + newfile = newfile.parent;// up to obj-dir/ 1.143 + 1.144 + info(newfile.path); 1.145 + try { 1.146 + for (let idx in devPath) { 1.147 + newfile.append(devPath[idx]); 1.148 + if (!newfile.isDirectory()) { 1.149 + info("*** NOT RUNNING ON DEV MACHINE\n\n"); 1.150 + throw new Error("Test is not running on dev machine, try dev path"); 1.151 + } 1.152 + } 1.153 + } 1.154 + catch (ex) { 1.155 + info(ex + "\n\n"); 1.156 + for (let idx in slavePath) { 1.157 + newfile.append(slavePath[idx]); 1.158 + if (!newfile.isDirectory()) { 1.159 + info("*** NOT RUNNING ON BUILD SLAVE\n\n"); 1.160 + throw new Error("Test is not running on slave machine, abort test"); 1.161 + } 1.162 + } 1.163 + } 1.164 + 1.165 + info("Final path: " + newfile.path); 1.166 + 1.167 + if (aRestore) { 1.168 + newfile.append("test-webapp-original.webapp"); 1.169 + } else { 1.170 + newfile.append("test-webapp-reinstall.webapp"); 1.171 + } 1.172 + 1.173 + let oldfile = newfile.parent; 1.174 + oldfile.append("test-webapp.webapp"); 1.175 + 1.176 + newfile.copyTo(null, "test-webapp.webapp"); 1.177 + 1.178 + if (!aRestore) { 1.179 + executeSoon(function (){ reinstallApp(); }); 1.180 + } 1.181 +}