3 const Cu = Components.utils;
4 const Cc = Components.classes;
5 const Ci = Components.interfaces;
7 Cu.import("resource://gre/modules/OperatorApps.jsm");
8 Cu.import("resource://gre/modules/FileUtils.jsm");
9 Cu.import("resource://gre/modules/NetUtil.jsm");
13 const PR_CREATE_FILE = 0x08;
14 const PR_TRUNCATE = 0x20;
16 SimpleTest.waitForExplicitFinish();
21 var singlevariantDir = undefined;
23 function debug(aMsg) {
24 //dump("== Tests debug == " + aMsg + "\n");
29 name : "testOperatorApp1",
32 package_path: "http://test/tests/dom/apps/tests/file_packaged_app.sjs",
33 description: "Updated even faster than Firefox, just to annoy slashdotters",
35 name: "Tester Operator App",
36 url: "http://mochi.test:8888"
41 name : "testOperatorApp1",
43 description: "Updated even faster than Firefox, just to annoy slashdotters",
44 launch_path: "index.html",
46 name: "Tester Operator App",
47 url: "http://mochi.test:8888"
49 default_locale: "en-US"
53 id: "testOperatorApp1",
54 installOrigin: "http://mochi.test:8888",
55 manifestURL: "http://test/tests/dom/apps/tests/file_packaged_app.sjs",
59 function writeFile(aFile, aData, aCb) {
60 debug("Saving " + aFile.path);
61 // Initialize the file output stream.
62 let ostream = FileUtils.openSafeFileOutputStream(aFile);
64 // Obtain a converter to convert our data to a UTF-8 encoded input stream.
65 let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]
66 .createInstance(Ci.nsIScriptableUnicodeConverter);
67 converter.charset = "UTF-8";
69 // Asynchronously copy the data to the file.
70 let istream = converter.convertToInputStream(aData);
71 NetUtil.asyncCopy(istream, ostream, function(rc) {
72 FileUtils.closeSafeFileOutputStream(ostream);
78 // File and resources helpers
79 function addZipEntry(zipWriter, entry, entryName) {
80 var stream = Cc["@mozilla.org/io/string-input-stream;1"]
81 .createInstance(Ci.nsIStringInputStream);
82 stream.setData(entry, entry.length);
83 zipWriter.addEntryStream(entryName, Date.now(),
84 Ci.nsIZipWriter.COMPRESSION_BEST, stream, false);
87 function setupDataDirs(aCb) {
88 let dirNum = "tmp_" + Math.floor(Math.random() * 10000000 + 1);
89 let tmpDir = FileUtils.getDir("TmpD", [dirNum, "singlevariantapps"], true,
91 let appDir = FileUtils.getDir("TmpD", [dirNum, "singlevariantapps",
92 "testOperatorApp1"], true, true);
94 singlevariantDir = tmpDir.path;
95 let singlevariantFile = tmpDir.clone();
96 singlevariantFile.append("singlevariantconf.json");
99 writeFile(singlevariantFile, JSON.stringify({"214-007":["testOperatorApp1"]}),
101 let indexhtml = "<html></html>";
102 let manifest = JSON.stringify(manifestData);
103 // Create the application package.
104 var zipWriter = Cc["@mozilla.org/zipwriter;1"]
105 .createInstance(Ci.nsIZipWriter);
106 var zipFile = FileUtils.getFile("TmpD", [
111 zipWriter.open(zipFile, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE);
112 addZipEntry(zipWriter, indexhtml, "index.html");
113 addZipEntry(zipWriter, manifest, "manifest.webapp");
116 var metadataFile = appDir.clone();
117 metadataFile.append("metadata.json");
118 writeFile(metadataFile, JSON.stringify(metadataData), function() {
119 var updateFile = appDir.clone();
120 updateFile.append("update.webapp");
121 writeFile(updateFile, JSON.stringify(updateData), aCb);
128 if (index >= steps.length) {
129 ok(false, "Shouldn't get here!");
135 ok(false, "Caught exception", ex);
147 function mozAppsError() {
148 ok(false, "mozApps error: " + this.error.name);
152 function installOperatorApp(aMcc, aMnc) {
153 OperatorAppsRegistry.appsDir = singlevariantDir;
154 OperatorAppsRegistry._installOperatorApps(aMcc, aMnc);
157 function checkAppState(aApp,
161 debug(JSON.stringify(aApp, null, 2));
163 debug(JSON.stringify(aApp.manifest, null, 2));
166 if (aExpectedApp.name) {
168 is(aApp.manifest.name, aExpectedApp.name, "Check name");
170 is(aApp.updateManifest.name, aExpectedApp.name, "Check name mini-manifest");
173 is(aApp.manifest.version, aVersion, "Check version");
175 if (typeof aExpectedApp.size !== "undefined" && aApp.manifest) {
176 is(aApp.manifest.size, aExpectedApp.size, "Check size");
179 is(aApp.manifest.launch_path, "index.html", "Check launch path");
181 if (aExpectedApp.manifestURL) {
182 is(aApp.manifestURL, aExpectedApp.manifestURL, "Check manifestURL");
184 if (aExpectedApp.installOrigin) {
185 is(aApp.installOrigin, aExpectedApp.installOrigin, "Check installOrigin");
187 ok(aApp.removable, "Removable app");
188 if (typeof aExpectedApp.progress !== "undefined") {
189 todo(aApp.progress == aExpectedApp.progress, "Check progress");
191 if (aExpectedApp.installState) {
192 is(aApp.installState, aExpectedApp.installState, "Check installState");
194 if (typeof aExpectedApp.downloadAvailable !== "undefined") {
195 is(aApp.downloadAvailable, aExpectedApp.downloadAvailable,
196 "Check download available");
198 if (typeof aExpectedApp.downloading !== "undefined") {
199 is(aApp.downloading, aExpectedApp.downloading, "Check downloading");
201 if (typeof aExpectedApp.downloadSize !== "undefined") {
202 is(aApp.downloadSize, aExpectedApp.downloadSize, "Check downloadSize");
204 if (typeof aExpectedApp.readyToApplyDownload !== "undefined") {
205 is(aApp.readyToApplyDownload, aExpectedApp.readyToApplyDownload,
206 "Check readyToApplyDownload");
208 if (aCb && typeof aCb === 'function') {
217 ok(true, "Data directory set up to " + singlevariantDir);
220 ok(true, "autoConfirmAppInstall");
221 SpecialPowers.autoConfirmAppInstall(next);
224 ok(true, "== TEST == Install operator app");
226 navigator.mozApps.mgmt.oninstall = function(evt) {
227 ok(true, "Got oninstall event");
228 gApp = evt.application;
229 gApp.ondownloaderror = function() {
230 ok(false, "Download error " + gApp.downloadError.name);
233 let downloadsuccessHandler = function() {
234 gApp.ondownloadsuccess = null;
235 ok(true, "App downloaded");
238 name: manifestData.name,
239 manifestURL: metadataData.manifestURL,
240 installOrigin: metadataData.installOrigin,
242 installState: "installed",
243 downloadAvailable: false,
246 readyToApplyDownload: false
248 checkAppState(gApp, manifestData.version, expected, next);
250 gApp.ondownloadsuccess = downloadsuccessHandler;
251 if (!gApp.downloading && gApp.ondownloadsuccess) {
252 ok(true, "Got an earlier event");
253 // Seems we set the handler too late.
254 gApp.ondownloadsuccess = null;
255 downloadsuccessHandler();
258 installOperatorApp("214", "007");
261 ok(true, "all done!\n");