Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | "use strict"; |
michael@0 | 2 | |
michael@0 | 3 | const Cu = Components.utils; |
michael@0 | 4 | const Cc = Components.classes; |
michael@0 | 5 | const Ci = Components.interfaces; |
michael@0 | 6 | |
michael@0 | 7 | Cu.import("resource://gre/modules/OperatorApps.jsm"); |
michael@0 | 8 | Cu.import("resource://gre/modules/FileUtils.jsm"); |
michael@0 | 9 | Cu.import("resource://gre/modules/NetUtil.jsm"); |
michael@0 | 10 | |
michael@0 | 11 | // From prio.h |
michael@0 | 12 | const PR_RDWR = 0x04; |
michael@0 | 13 | const PR_CREATE_FILE = 0x08; |
michael@0 | 14 | const PR_TRUNCATE = 0x20; |
michael@0 | 15 | |
michael@0 | 16 | SimpleTest.waitForExplicitFinish(); |
michael@0 | 17 | |
michael@0 | 18 | var gApp = null; |
michael@0 | 19 | |
michael@0 | 20 | var index = -1; |
michael@0 | 21 | var singlevariantDir = undefined; |
michael@0 | 22 | |
michael@0 | 23 | function debug(aMsg) { |
michael@0 | 24 | //dump("== Tests debug == " + aMsg + "\n"); |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | |
michael@0 | 28 | var updateData = { |
michael@0 | 29 | name : "testOperatorApp1", |
michael@0 | 30 | version : 2, |
michael@0 | 31 | size : 767, |
michael@0 | 32 | package_path: "http://test/tests/dom/apps/tests/file_packaged_app.sjs", |
michael@0 | 33 | description: "Updated even faster than Firefox, just to annoy slashdotters", |
michael@0 | 34 | developer: { |
michael@0 | 35 | name: "Tester Operator App", |
michael@0 | 36 | url: "http://mochi.test:8888" |
michael@0 | 37 | } |
michael@0 | 38 | }; |
michael@0 | 39 | |
michael@0 | 40 | var manifestData = { |
michael@0 | 41 | name : "testOperatorApp1", |
michael@0 | 42 | version : 2, |
michael@0 | 43 | description: "Updated even faster than Firefox, just to annoy slashdotters", |
michael@0 | 44 | launch_path: "index.html", |
michael@0 | 45 | developer: { |
michael@0 | 46 | name: "Tester Operator App", |
michael@0 | 47 | url: "http://mochi.test:8888" |
michael@0 | 48 | }, |
michael@0 | 49 | default_locale: "en-US" |
michael@0 | 50 | }; |
michael@0 | 51 | |
michael@0 | 52 | var metadataData = { |
michael@0 | 53 | id: "testOperatorApp1", |
michael@0 | 54 | installOrigin: "http://mochi.test:8888", |
michael@0 | 55 | manifestURL: "http://test/tests/dom/apps/tests/file_packaged_app.sjs", |
michael@0 | 56 | origin: "http://test" |
michael@0 | 57 | }; |
michael@0 | 58 | |
michael@0 | 59 | function writeFile(aFile, aData, aCb) { |
michael@0 | 60 | debug("Saving " + aFile.path); |
michael@0 | 61 | // Initialize the file output stream. |
michael@0 | 62 | let ostream = FileUtils.openSafeFileOutputStream(aFile); |
michael@0 | 63 | |
michael@0 | 64 | // Obtain a converter to convert our data to a UTF-8 encoded input stream. |
michael@0 | 65 | let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"] |
michael@0 | 66 | .createInstance(Ci.nsIScriptableUnicodeConverter); |
michael@0 | 67 | converter.charset = "UTF-8"; |
michael@0 | 68 | |
michael@0 | 69 | // Asynchronously copy the data to the file. |
michael@0 | 70 | let istream = converter.convertToInputStream(aData); |
michael@0 | 71 | NetUtil.asyncCopy(istream, ostream, function(rc) { |
michael@0 | 72 | FileUtils.closeSafeFileOutputStream(ostream); |
michael@0 | 73 | if (aCb) |
michael@0 | 74 | aCb(); |
michael@0 | 75 | }); |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | // File and resources helpers |
michael@0 | 79 | function addZipEntry(zipWriter, entry, entryName) { |
michael@0 | 80 | var stream = Cc["@mozilla.org/io/string-input-stream;1"] |
michael@0 | 81 | .createInstance(Ci.nsIStringInputStream); |
michael@0 | 82 | stream.setData(entry, entry.length); |
michael@0 | 83 | zipWriter.addEntryStream(entryName, Date.now(), |
michael@0 | 84 | Ci.nsIZipWriter.COMPRESSION_BEST, stream, false); |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | function setupDataDirs(aCb) { |
michael@0 | 88 | let dirNum = "tmp_" + Math.floor(Math.random() * 10000000 + 1); |
michael@0 | 89 | let tmpDir = FileUtils.getDir("TmpD", [dirNum, "singlevariantapps"], true, |
michael@0 | 90 | true); |
michael@0 | 91 | let appDir = FileUtils.getDir("TmpD", [dirNum, "singlevariantapps", |
michael@0 | 92 | "testOperatorApp1"], true, true); |
michael@0 | 93 | |
michael@0 | 94 | singlevariantDir = tmpDir.path; |
michael@0 | 95 | let singlevariantFile = tmpDir.clone(); |
michael@0 | 96 | singlevariantFile.append("singlevariantconf.json"); |
michael@0 | 97 | |
michael@0 | 98 | |
michael@0 | 99 | writeFile(singlevariantFile, JSON.stringify({"214-007":["testOperatorApp1"]}), |
michael@0 | 100 | function() { |
michael@0 | 101 | let indexhtml = "<html></html>"; |
michael@0 | 102 | let manifest = JSON.stringify(manifestData); |
michael@0 | 103 | // Create the application package. |
michael@0 | 104 | var zipWriter = Cc["@mozilla.org/zipwriter;1"] |
michael@0 | 105 | .createInstance(Ci.nsIZipWriter); |
michael@0 | 106 | var zipFile = FileUtils.getFile("TmpD", [ |
michael@0 | 107 | dirNum, |
michael@0 | 108 | "singlevariantapps", |
michael@0 | 109 | "testOperatorApp1", |
michael@0 | 110 | "application.zip"]); |
michael@0 | 111 | zipWriter.open(zipFile, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE); |
michael@0 | 112 | addZipEntry(zipWriter, indexhtml, "index.html"); |
michael@0 | 113 | addZipEntry(zipWriter, manifest, "manifest.webapp"); |
michael@0 | 114 | zipWriter.close(); |
michael@0 | 115 | |
michael@0 | 116 | var metadataFile = appDir.clone(); |
michael@0 | 117 | metadataFile.append("metadata.json"); |
michael@0 | 118 | writeFile(metadataFile, JSON.stringify(metadataData), function() { |
michael@0 | 119 | var updateFile = appDir.clone(); |
michael@0 | 120 | updateFile.append("update.webapp"); |
michael@0 | 121 | writeFile(updateFile, JSON.stringify(updateData), aCb); |
michael@0 | 122 | }); |
michael@0 | 123 | }); |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | function next() { |
michael@0 | 127 | index += 1; |
michael@0 | 128 | if (index >= steps.length) { |
michael@0 | 129 | ok(false, "Shouldn't get here!"); |
michael@0 | 130 | return; |
michael@0 | 131 | } |
michael@0 | 132 | try { |
michael@0 | 133 | steps[index](); |
michael@0 | 134 | } catch(ex) { |
michael@0 | 135 | ok(false, "Caught exception", ex); |
michael@0 | 136 | } |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | function go() { |
michael@0 | 140 | next(); |
michael@0 | 141 | } |
michael@0 | 142 | |
michael@0 | 143 | function finish() { |
michael@0 | 144 | SimpleTest.finish(); |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | function mozAppsError() { |
michael@0 | 148 | ok(false, "mozApps error: " + this.error.name); |
michael@0 | 149 | finish(); |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | function installOperatorApp(aMcc, aMnc) { |
michael@0 | 153 | OperatorAppsRegistry.appsDir = singlevariantDir; |
michael@0 | 154 | OperatorAppsRegistry._installOperatorApps(aMcc, aMnc); |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | function checkAppState(aApp, |
michael@0 | 158 | aVersion, |
michael@0 | 159 | aExpectedApp, |
michael@0 | 160 | aCb) { |
michael@0 | 161 | debug(JSON.stringify(aApp, null, 2)); |
michael@0 | 162 | if (aApp.manifest) { |
michael@0 | 163 | debug(JSON.stringify(aApp.manifest, null, 2)); |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | if (aExpectedApp.name) { |
michael@0 | 167 | if (aApp.manifest) { |
michael@0 | 168 | is(aApp.manifest.name, aExpectedApp.name, "Check name"); |
michael@0 | 169 | } |
michael@0 | 170 | is(aApp.updateManifest.name, aExpectedApp.name, "Check name mini-manifest"); |
michael@0 | 171 | } |
michael@0 | 172 | if (aApp.manifest) { |
michael@0 | 173 | is(aApp.manifest.version, aVersion, "Check version"); |
michael@0 | 174 | } |
michael@0 | 175 | if (typeof aExpectedApp.size !== "undefined" && aApp.manifest) { |
michael@0 | 176 | is(aApp.manifest.size, aExpectedApp.size, "Check size"); |
michael@0 | 177 | } |
michael@0 | 178 | if (aApp.manifest) { |
michael@0 | 179 | is(aApp.manifest.launch_path, "index.html", "Check launch path"); |
michael@0 | 180 | } |
michael@0 | 181 | if (aExpectedApp.manifestURL) { |
michael@0 | 182 | is(aApp.manifestURL, aExpectedApp.manifestURL, "Check manifestURL"); |
michael@0 | 183 | } |
michael@0 | 184 | if (aExpectedApp.installOrigin) { |
michael@0 | 185 | is(aApp.installOrigin, aExpectedApp.installOrigin, "Check installOrigin"); |
michael@0 | 186 | } |
michael@0 | 187 | ok(aApp.removable, "Removable app"); |
michael@0 | 188 | if (typeof aExpectedApp.progress !== "undefined") { |
michael@0 | 189 | todo(aApp.progress == aExpectedApp.progress, "Check progress"); |
michael@0 | 190 | } |
michael@0 | 191 | if (aExpectedApp.installState) { |
michael@0 | 192 | is(aApp.installState, aExpectedApp.installState, "Check installState"); |
michael@0 | 193 | } |
michael@0 | 194 | if (typeof aExpectedApp.downloadAvailable !== "undefined") { |
michael@0 | 195 | is(aApp.downloadAvailable, aExpectedApp.downloadAvailable, |
michael@0 | 196 | "Check download available"); |
michael@0 | 197 | } |
michael@0 | 198 | if (typeof aExpectedApp.downloading !== "undefined") { |
michael@0 | 199 | is(aApp.downloading, aExpectedApp.downloading, "Check downloading"); |
michael@0 | 200 | } |
michael@0 | 201 | if (typeof aExpectedApp.downloadSize !== "undefined") { |
michael@0 | 202 | is(aApp.downloadSize, aExpectedApp.downloadSize, "Check downloadSize"); |
michael@0 | 203 | } |
michael@0 | 204 | if (typeof aExpectedApp.readyToApplyDownload !== "undefined") { |
michael@0 | 205 | is(aApp.readyToApplyDownload, aExpectedApp.readyToApplyDownload, |
michael@0 | 206 | "Check readyToApplyDownload"); |
michael@0 | 207 | } |
michael@0 | 208 | if (aCb && typeof aCb === 'function') { |
michael@0 | 209 | aCb(); |
michael@0 | 210 | } |
michael@0 | 211 | return; |
michael@0 | 212 | } |
michael@0 | 213 | |
michael@0 | 214 | var steps = [ |
michael@0 | 215 | function() { |
michael@0 | 216 | setupDataDirs(next); |
michael@0 | 217 | ok(true, "Data directory set up to " + singlevariantDir); |
michael@0 | 218 | }, |
michael@0 | 219 | function() { |
michael@0 | 220 | ok(true, "autoConfirmAppInstall"); |
michael@0 | 221 | SpecialPowers.autoConfirmAppInstall(next); |
michael@0 | 222 | }, |
michael@0 | 223 | function() { |
michael@0 | 224 | ok(true, "== TEST == Install operator app"); |
michael@0 | 225 | |
michael@0 | 226 | navigator.mozApps.mgmt.oninstall = function(evt) { |
michael@0 | 227 | ok(true, "Got oninstall event"); |
michael@0 | 228 | gApp = evt.application; |
michael@0 | 229 | gApp.ondownloaderror = function() { |
michael@0 | 230 | ok(false, "Download error " + gApp.downloadError.name); |
michael@0 | 231 | finish(); |
michael@0 | 232 | }; |
michael@0 | 233 | let downloadsuccessHandler = function() { |
michael@0 | 234 | gApp.ondownloadsuccess = null; |
michael@0 | 235 | ok(true, "App downloaded"); |
michael@0 | 236 | |
michael@0 | 237 | var expected = { |
michael@0 | 238 | name: manifestData.name, |
michael@0 | 239 | manifestURL: metadataData.manifestURL, |
michael@0 | 240 | installOrigin: metadataData.installOrigin, |
michael@0 | 241 | progress: 0, |
michael@0 | 242 | installState: "installed", |
michael@0 | 243 | downloadAvailable: false, |
michael@0 | 244 | downloading: false, |
michael@0 | 245 | downloadSize: 767, |
michael@0 | 246 | readyToApplyDownload: false |
michael@0 | 247 | }; |
michael@0 | 248 | checkAppState(gApp, manifestData.version, expected, next); |
michael@0 | 249 | }; |
michael@0 | 250 | gApp.ondownloadsuccess = downloadsuccessHandler; |
michael@0 | 251 | if (!gApp.downloading && gApp.ondownloadsuccess) { |
michael@0 | 252 | ok(true, "Got an earlier event"); |
michael@0 | 253 | // Seems we set the handler too late. |
michael@0 | 254 | gApp.ondownloadsuccess = null; |
michael@0 | 255 | downloadsuccessHandler(); |
michael@0 | 256 | } |
michael@0 | 257 | }; |
michael@0 | 258 | installOperatorApp("214", "007"); |
michael@0 | 259 | }, |
michael@0 | 260 | function() { |
michael@0 | 261 | ok(true, "all done!\n"); |
michael@0 | 262 | finish(); |
michael@0 | 263 | } |
michael@0 | 264 | ]; |
michael@0 | 265 | |
michael@0 | 266 | go(); |
michael@0 | 267 |