1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/tests/mochitest/webapps/test_getNotInstalled.xul Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,139 @@ 1.4 +<?xml version="1.0"?> 1.5 + 1.6 +<!-- Any copyright is dedicated to the Public Domain. 1.7 + - http://creativecommons.org/publicdomain/zero/1.0/ --> 1.8 + 1.9 +<?xml-stylesheet type="text/css" href="chrome://global/skin"?> 1.10 +<?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?> 1.11 + 1.12 +<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" 1.13 + title="Mozilla Bug 781379"> 1.14 + <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/> 1.15 + <script type="application/javascript" src="head.js"/> 1.16 + <!-- test results are displayed in the html:body --> 1.17 + <body xmlns="http://www.w3.org/1999/xhtml"> 1.18 + <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=741549" 1.19 + target="_blank">Mozilla Bug 781379</a> 1.20 + </body> 1.21 + 1.22 +<script type="application/javascript;version=1.8"> 1.23 + 1.24 +const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; 1.25 +Cu.import("resource://gre/modules/Webapps.jsm"); 1.26 + 1.27 +// We use a different origin than other webapps test files because we compare 1.28 +// the number of apps before and after installing this one; and if a test file 1.29 +// installs our app and then doesn't uninstall it (f.e. because it isn't written 1.30 +// to clean up after itself, or because it throws an exception or times out), 1.31 +// then this test will *reinstall* the app, and the number of apps won't change, 1.32 +// which will look like a failure in this test although it's actually a failure 1.33 +// in the other one. 1.34 +// 1.35 +// Using a different origin here isn't a foolproof solution, as another test 1.36 +// could start using it. Reviewer vigilance is required! And to anyone reading 1.37 +// this: don't use this origin without good reason and due consideration for 1.38 +// the potential consequences! 1.39 +// 1.40 +// Alternately, we could define a test-specific domain, getNotInstalled.com, 1.41 +// in source/build/pgo/server-locations.txt. But that seems like overkill, 1.42 +// and this problem will go away once we support multiple apps per origin, 1.43 +// since then we can make this test install its own personal webapp from any 1.44 +// origin. 1.45 +// 1.46 +let url = "http://example.com/chrome/dom/tests/mochitest/webapps/apps/basic.webapp"; 1.47 + 1.48 +let app, notInstalled, _isLaunchable; 1.49 + 1.50 +let steps = [ 1.51 + monkeyPatchDOMApplicationRegistry, 1.52 + getNotInstalled, 1.53 + installApp, 1.54 + compareNotInstalled, 1.55 + unmonkeyPatchDOMApplicationRegistry, 1.56 + uninstallApp, 1.57 +]; 1.58 + 1.59 +runAll(steps); 1.60 + 1.61 +// Monkey patch DOMApplicationRegistry._isLaunchable for testing. 1.62 +// This way, we don't have to create a platform specific application with a 1.63 +// status other than "installed". 1.64 +function monkeyPatchDOMApplicationRegistry(next) { 1.65 + _isLaunchable = DOMApplicationRegistry._isLaunchable; 1.66 + DOMApplicationRegistry._isLaunchable = function mockIsLaunchable(aOrigin) { 1.67 + return false; 1.68 + } 1.69 + next(); 1.70 +} 1.71 + 1.72 +// Call navigator.mozApps.mgmt.getNotInstalled and save the result. 1.73 +function getNotInstalled(next) { 1.74 + window.navigator.mozApps.mgmt.getNotInstalled().onsuccess = 1.75 + function onGetNotInstalled() { 1.76 + notInstalled = this.result.length; 1.77 + next(); 1.78 + }; 1.79 +} 1.80 + 1.81 +// Add an app to the appregistry 1.82 +function installApp(next) { 1.83 + confirmNextInstall(); 1.84 + navigator.mozApps.install(url, null).onsuccess = function onInstall() { 1.85 + app = this.result; 1.86 + next(); 1.87 + } 1.88 +} 1.89 + 1.90 +// Call navigator.mozApps.mgmt.getNotInstalled and make sure there is one more. 1.91 +function compareNotInstalled(next) { 1.92 + let results; 1.93 + function getNotInstalledError() { 1.94 + ok(false, "window.mozApps.mgmt.getNotInstalled onerror called"); 1.95 + next(); 1.96 + } 1.97 + function getNotInstalledSuccess() { 1.98 + ok(true, "window.mozApps.mgmt.getNotInstalled onsuccess called"); 1.99 + is(this.result.length, notInstalled + 1, 1.100 + "should get one more notInstalled app"); 1.101 + 1.102 + if (this.result.length > 0) { 1.103 + is(this.result[this.result.length-1].origin, "http://example.com", 1.104 + "getNotInstalled returned the expected app"); 1.105 + } 1.106 + next(); 1.107 + } 1.108 + 1.109 + let type = typeof window.navigator.mozApps.getNotInstalled; 1.110 + is(type, "undefined", "getNotInstalled moved from window.navigator"); 1.111 + type = typeof window.navigator.mozApps.mgmt.getNotInstalled; 1.112 + if (type === "function") { 1.113 + is(type, "function", "getNotInstalled moved to window.navigator.mgmt"); 1.114 + results = window.navigator.mozApps.mgmt.getNotInstalled(); 1.115 + results.onerror = getNotInstalledError; 1.116 + results.onsuccess = getNotInstalledSuccess; 1.117 + } else { 1.118 + ok(false, "getNotInstalled not a function"); 1.119 + next(); 1.120 + } 1.121 +} 1.122 + 1.123 +function unmonkeyPatchDOMApplicationRegistry(next) { 1.124 + if (typeof _isLaunchable === "function") { 1.125 + DOMApplicationRegistry._isLaunchable = _isLaunchable; 1.126 + ok(true, "restored DOMApplicationRegistry._isLaunchable"); 1.127 + } else { 1.128 + ok(false, "can't restore DOMApplicationRegistry._isLaunchable"); 1.129 + } 1.130 + next(); 1.131 +} 1.132 + 1.133 +// Remove the app from the appregistry 1.134 +function uninstallApp(next) { 1.135 + window.navigator.mozApps.mgmt.uninstall(app).onsuccess = function onUninstall() { 1.136 + app = null; 1.137 + next(); 1.138 + } 1.139 +} 1.140 + 1.141 +</script> 1.142 +</window>