dom/tests/mochitest/webapps/test_getNotInstalled.xul

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

     1 <?xml version="1.0"?>
     3 <!-- Any copyright is dedicated to the Public Domain.
     4    - http://creativecommons.org/publicdomain/zero/1.0/ -->
     6 <?xml-stylesheet type="text/css" href="chrome://global/skin"?>
     7 <?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
     9 <window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
    10         title="Mozilla Bug 781379">
    11   <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
    12   <script type="application/javascript" src="head.js"/>
    13   <!-- test results are displayed in the html:body -->
    14   <body xmlns="http://www.w3.org/1999/xhtml">
    15   <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=741549"
    16      target="_blank">Mozilla Bug 781379</a>
    17   </body>
    19 <script type="application/javascript;version=1.8">
    21 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
    22 Cu.import("resource://gre/modules/Webapps.jsm");
    24 // We use a different origin than other webapps test files because we compare
    25 // the number of apps before and after installing this one; and if a test file
    26 // installs our app and then doesn't uninstall it (f.e. because it isn't written
    27 // to clean up after itself, or because it throws an exception or times out),
    28 // then this test will *reinstall* the app, and the number of apps won't change,
    29 // which will look like a failure in this test although it's actually a failure
    30 // in the other one.
    31 //
    32 // Using a different origin here isn't a foolproof solution, as another test
    33 // could start using it.  Reviewer vigilance is required!  And to anyone reading
    34 // this: don't use this origin without good reason and due consideration for
    35 // the potential consequences!
    36 //
    37 // Alternately, we could define a test-specific domain, getNotInstalled.com,
    38 // in source/build/pgo/server-locations.txt.  But that seems like overkill,
    39 // and this problem will go away once we support multiple apps per origin,
    40 // since then we can make this test install its own personal webapp from any
    41 // origin.
    42 //
    43 let url = "http://example.com/chrome/dom/tests/mochitest/webapps/apps/basic.webapp";
    45 let app, notInstalled, _isLaunchable;
    47 let steps = [
    48   monkeyPatchDOMApplicationRegistry,
    49   getNotInstalled,
    50   installApp,
    51   compareNotInstalled,
    52   unmonkeyPatchDOMApplicationRegistry,
    53   uninstallApp,
    54 ];
    56 runAll(steps);
    58 // Monkey patch DOMApplicationRegistry._isLaunchable for testing.
    59 // This way, we don't have to create a platform specific application with a
    60 // status other than "installed".
    61 function monkeyPatchDOMApplicationRegistry(next) {
    62   _isLaunchable = DOMApplicationRegistry._isLaunchable;
    63   DOMApplicationRegistry._isLaunchable = function mockIsLaunchable(aOrigin) {
    64     return false;
    65   }
    66   next();
    67 }
    69 // Call navigator.mozApps.mgmt.getNotInstalled and save the result.
    70 function getNotInstalled(next) {
    71   window.navigator.mozApps.mgmt.getNotInstalled().onsuccess =
    72   function onGetNotInstalled() {
    73     notInstalled = this.result.length;
    74     next();
    75   };
    76 }
    78 // Add an app to the appregistry
    79 function installApp(next) {
    80   confirmNextInstall();
    81   navigator.mozApps.install(url, null).onsuccess = function onInstall() {
    82     app = this.result;
    83     next();
    84   }
    85 }
    87 // Call navigator.mozApps.mgmt.getNotInstalled and make sure there is one more.
    88 function compareNotInstalled(next) {
    89   let results;
    90   function getNotInstalledError() {
    91     ok(false, "window.mozApps.mgmt.getNotInstalled onerror called");
    92     next();
    93   }
    94   function getNotInstalledSuccess() {
    95     ok(true, "window.mozApps.mgmt.getNotInstalled onsuccess called");
    96     is(this.result.length, notInstalled + 1,
    97        "should get one more notInstalled app");
    99     if (this.result.length > 0) {
   100       is(this.result[this.result.length-1].origin, "http://example.com",
   101          "getNotInstalled returned the expected app");
   102     }
   103     next();
   104   }
   106   let type = typeof window.navigator.mozApps.getNotInstalled;
   107   is(type, "undefined", "getNotInstalled moved from window.navigator");
   108   type = typeof window.navigator.mozApps.mgmt.getNotInstalled;
   109   if (type === "function") {
   110     is(type, "function", "getNotInstalled moved to window.navigator.mgmt");
   111     results = window.navigator.mozApps.mgmt.getNotInstalled();
   112     results.onerror = getNotInstalledError;
   113     results.onsuccess = getNotInstalledSuccess;
   114   } else {
   115     ok(false, "getNotInstalled not a function");
   116     next();
   117   }
   118 }
   120 function unmonkeyPatchDOMApplicationRegistry(next) {
   121   if (typeof _isLaunchable === "function") {
   122     DOMApplicationRegistry._isLaunchable = _isLaunchable;
   123     ok(true, "restored DOMApplicationRegistry._isLaunchable");
   124   } else {
   125     ok(false, "can't restore DOMApplicationRegistry._isLaunchable");
   126   }
   127   next();
   128 }
   130 // Remove the app from the appregistry
   131 function uninstallApp(next) {
   132   window.navigator.mozApps.mgmt.uninstall(app).onsuccess = function onUninstall() {
   133     app = null;
   134     next();
   135   }
   136 }
   138 </script>
   139 </window>

mercurial