diff --git a/testing/mochitest/b2g.json b/testing/mochitest/b2g.json index bf7570575140..af712397a47c 100644 --- a/testing/mochitest/b2g.json +++ b/testing/mochitest/b2g.json @@ -4,7 +4,8 @@ "content": "", "docshell": "", "dom": "", - "layout": "" + "layout": "", + "toolkit/devtools/apps": "" }, "excludetests": { "content/xul":"tests that use xul", diff --git a/toolkit/devtools/apps/tests/Makefile.in b/toolkit/devtools/apps/tests/Makefile.in new file mode 100644 index 000000000000..0a321db2f8c3 --- /dev/null +++ b/toolkit/devtools/apps/tests/Makefile.in @@ -0,0 +1,23 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +DEPTH = @DEPTH@ +topsrcdir = @top_srcdir@ +srcdir = @srcdir@ +VPATH = @srcdir@ +relativesrcdir = @relativesrcdir@ + +include $(DEPTH)/config/autoconf.mk + +# The mochitest doesn't work on fennec yet +ifneq (android,$(MOZ_WIDGET_TOOLKIT)) +MOCHITEST_FILES = \ + test_webapps_actor.html \ + debugger-protocol-helper.js \ + data/ \ + redirect.sjs \ + $(NULL) +endif + +include $(topsrcdir)/config/rules.mk diff --git a/toolkit/devtools/apps/tests/data/app-redirect.zip b/toolkit/devtools/apps/tests/data/app-redirect.zip new file mode 100644 index 000000000000..30b2bebccd90 Binary files /dev/null and b/toolkit/devtools/apps/tests/data/app-redirect.zip differ diff --git a/toolkit/devtools/apps/tests/data/app-updated.zip b/toolkit/devtools/apps/tests/data/app-updated.zip new file mode 100644 index 000000000000..b40f8e7c3248 Binary files /dev/null and b/toolkit/devtools/apps/tests/data/app-updated.zip differ diff --git a/toolkit/devtools/apps/tests/data/app.zip b/toolkit/devtools/apps/tests/data/app.zip new file mode 100644 index 000000000000..511025975f9e Binary files /dev/null and b/toolkit/devtools/apps/tests/data/app.zip differ diff --git a/toolkit/devtools/apps/tests/debugger-protocol-helper.js b/toolkit/devtools/apps/tests/debugger-protocol-helper.js new file mode 100644 index 000000000000..e45f6589943d --- /dev/null +++ b/toolkit/devtools/apps/tests/debugger-protocol-helper.js @@ -0,0 +1,86 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +const Cc = Components.classes; +const Ci = Components.interfaces; +const Cu = Components.utils; + +const { DebuggerServer } = Cu.import("resource://gre/modules/devtools/dbg-server.jsm"); +const { DebuggerClient } = Cu.import("resource://gre/modules/devtools/dbg-client.jsm"); + +const { FileUtils } = Cu.import("resource://gre/modules/FileUtils.jsm"); +const { Services } = Cu.import("resource://gre/modules/Services.jsm"); + +let gClient, gActor; + +function connect(onDone) { + // Initialize a loopback remote protocol connection + DebuggerServer.init(function () { return true; }); + // We need to register browser actors to have `listTabs` working + // and also have a root actor + + if (Services.appinfo.name == "B2G") { + DebuggerServer.addActors("resource://gre/modules/devtools/server/actors/webbrowser.js"); + DebuggerServer.addActors('chrome://browser/content/dbg-browser-actors.js'); + DebuggerServer.addActors("resource://gre/modules/devtools/server/actors/webapps.js"); + } else { + DebuggerServer.addBrowserActors(); + } + // Setup client and actor used in all tests + gClient = new DebuggerClient(DebuggerServer.connectPipe()); + gClient.connect(function onConnect() { + gClient.listTabs(function onListTabs(aResponse) { + gActor = aResponse.webappsActor; + if (gActor) + onDone(); + }); + }); + +} + +function webappActorRequest(request, onResponse) { + if (!gActor) { + connect(webappActorRequest.bind(null, request, onResponse)); + return; + } + + request.to = gActor; + gClient.request(request, onResponse); +} + + +function downloadURL(url, file) { + let channel = Services.io.newChannel(url, null, null); + let istream = channel.open(); + let bstream = Cc["@mozilla.org/binaryinputstream;1"] + .createInstance(Ci.nsIBinaryInputStream); + bstream.setInputStream(istream); + let data = bstream.readBytes(bstream.available()); + + let ostream = Cc["@mozilla.org/network/safe-file-output-stream;1"] + .createInstance(Ci.nsIFileOutputStream); + ostream.init(file, 0x04 | 0x08 | 0x20, 0600, 0); + ostream.write(data, data.length); + ostream.QueryInterface(Ci.nsISafeOutputStream).finish(); +} + +// Install a test packaged webapp from data folder +addMessageListener("install", function (aMessage) { + let url = aMessage.url; + let appId = aMessage.appId; + + try { + // Download its content from mochitest http server + // Copy our package to tmp folder, where the actor retrieves it + let zip = FileUtils.getDir("TmpD", ["b2g", appId], true, true); + zip.append("application.zip"); + downloadURL(url, zip); + + let request = {type: "install", appId: appId}; + webappActorRequest(request, function (aResponse) { + sendAsyncMessage("installed", aResponse); + }); + } catch(e) { + dump("installTestApp exception: " + e + "\n"); + } +}); diff --git a/toolkit/devtools/apps/tests/redirect.sjs b/toolkit/devtools/apps/tests/redirect.sjs new file mode 100644 index 000000000000..394cb8bfaf68 --- /dev/null +++ b/toolkit/devtools/apps/tests/redirect.sjs @@ -0,0 +1,7 @@ +const REDIRECTION_URL = "http://example.com/redirection-target.html"; + +function handleRequest(request, response) { + response.setStatusLine(request.httpVersion, 301, "Moved Permanently"); + response.setHeader("Location", REDIRECTION_URL, false); +} + diff --git a/toolkit/devtools/apps/tests/test_webapps_actor.html b/toolkit/devtools/apps/tests/test_webapps_actor.html new file mode 100644 index 000000000000..6fc970bc930d --- /dev/null +++ b/toolkit/devtools/apps/tests/test_webapps_actor.html @@ -0,0 +1,157 @@ + + + + + Test for Bug {821589} Packaged apps installation and update + + + + + + +Mozilla Bug {821589} +

+ +
+
+
+ + diff --git a/toolkit/devtools/apps/tests/unit/data/app-redirect.zip b/toolkit/devtools/apps/tests/unit/data/app-redirect.zip deleted file mode 100644 index ddfcab193c56..000000000000 Binary files a/toolkit/devtools/apps/tests/unit/data/app-redirect.zip and /dev/null differ diff --git a/toolkit/devtools/apps/tests/unit/head_apps.js b/toolkit/devtools/apps/tests/unit/head_apps.js index c46329c93deb..264cee5a5af7 100644 --- a/toolkit/devtools/apps/tests/unit/head_apps.js +++ b/toolkit/devtools/apps/tests/unit/head_apps.js @@ -58,7 +58,7 @@ function installTestApp(zipName, appId, onDone) { // The install request is asynchronous and send back an event to tell // if the installation succeed or failed - gClient.addListener("webappsEvent", function (aState, aType, aPacket) { + gClient.addOneTimeListener("webappsEvent", function (aState, aType, aPacket) { do_check_eq(aType.appId, appId); if ("error" in aType) { do_throw("Error: " + aType.error); diff --git a/toolkit/devtools/apps/tests/unit/test_appInstall.js b/toolkit/devtools/apps/tests/unit/test_appInstall.js deleted file mode 100644 index 196454dd1734..000000000000 --- a/toolkit/devtools/apps/tests/unit/test_appInstall.js +++ /dev/null @@ -1,71 +0,0 @@ -/* Any copyright is dedicated to the Public Domain. - http://creativecommons.org/publicdomain/zero/1.0/ */ - -Cu.import("resource://testing-common/httpd.js"); - -// We need to lazily instanciate apps service, -// as we have to wait for head setup before being -// able to instanciate it without exception -XPCOMUtils.defineLazyGetter(this, "appsService", function() { - return Cc["@mozilla.org/AppsService;1"] - .getService(Ci.nsIAppsService); -}); - -const SERVER_PORT = 4444; -const HTTP_BASE = "http://localhost:" + SERVER_PORT + "/"; - -const APP_ID = "actor-test"; -const APP_BASE = "app://" + APP_ID + "/"; - -const APP_MANIFEST = APP_BASE + "manifest.webapp"; -// The remote url being redirect to... -const REDIRECTED_URL = HTTP_BASE + "redirection-source.html"; -// ...redirected to this another remote url. -const REMOTE_REDIRECTION_URL = HTTP_BASE + "redirection-target.html"; -// The app local file URL that overide REMOTE_REDIRECTION_URL -const LOCAL_REDIRECTION_URL = APP_BASE + "redirected.html"; - -add_test(function testInstallApp() { - installTestApp("app-redirect.zip", APP_ID, run_next_test); -}); - -add_test(function testLaunchApp() { - let server = new HttpServer(); - - // First register the URL that redirect - // from /redirection-source.html - // to /redirection-target.html - server.registerPathHandler("/redirection-source.html", function handle(request, response) { - response.setStatusLine(request.httpVersion, 301, "Moved Permanently"); - response.setHeader("Location", REMOTE_REDIRECTION_URL, false); - - // We have to wait for one even loop cycle before checking the new document location - do_timeout(0, function () { - do_check_eq(d.document.documentURIObject.spec, LOCAL_REDIRECTION_URL); - server.stop(run_next_test); - }); - }); - server.registerPathHandler("/redirection-target.html", function handle(request, response) { - do_throw("We shouldn't receive any request to the remote redirection"); - }); - server.start(SERVER_PORT) - - // Load the remote URL in a docshell configured as an app - let d = Cc["@mozilla.org/docshell;1"].createInstance(Ci.nsIDocShell); - d.QueryInterface(Ci.nsIWebNavigation); - d.QueryInterface(Ci.nsIWebProgress); - - let localAppId = appsService.getAppLocalIdByManifestURL(APP_MANIFEST); - d.setIsApp(localAppId); - do_check_true(d.isApp); - do_check_eq(d.appId, localAppId); - - d.loadURI(REDIRECTED_URL, Ci.nsIWebNavigation.LOAD_FLAGS_NONE, null, null, null); -}); - -function run_test() { - setup(); - - run_next_test(); -} - diff --git a/toolkit/devtools/apps/tests/unit/test_webappsActor.js b/toolkit/devtools/apps/tests/unit/test_webappsActor.js index 69149a1eb051..09699f6ceb12 100644 --- a/toolkit/devtools/apps/tests/unit/test_webappsActor.js +++ b/toolkit/devtools/apps/tests/unit/test_webappsActor.js @@ -28,33 +28,11 @@ add_test(function testCloseInexistantApp() { // Install a test app add_test(function testInstallPackaged() { - // Copy our test webapp to tmp folder, where the actor retrieves it - let zip = do_get_file("data/app.zip"); - let appDir = FileUtils.getDir("TmpD", ["b2g", gAppId], true, true); - zip.copyTo(appDir, "application.zip"); - - let request = {type: "install", appId: gAppId}; - webappActorRequest(request, function (aResponse) { - do_check_eq(aResponse.appId, gAppId); - }); - - // The install request is asynchronous and send back an event to tell - // if the installation succeed or failed - gClient.addOneTimeListener("webappsEvent", function listener(aState, aType, aPacket) { - do_check_eq(aType.appId, gAppId); - if ("error" in aType) { - do_print("Error: " + aType.error); - } - if ("message" in aType) { - do_print("Error message: " + aType.message); - } - do_check_eq("error" in aType, false); - + installTestApp("app.zip", gAppId, function () { run_next_test(); }); }); - // Now check that the app appear in getAll add_test(function testGetAll() { let request = {type: "getAll"}; @@ -68,7 +46,7 @@ add_test(function testGetAll() { do_check_eq(app.name, "Test app"); do_check_eq(app.manifest.description, "Testing webapps actor"); do_check_eq(app.manifest.launch_path, "/index.html"); - do_check_eq(app.origin, "app://" + gAppId); + do_check_eq(app.origin, APP_ORIGIN); do_check_eq(app.installOrigin, app.origin); do_check_eq(app.manifestURL, app.origin + "/manifest.webapp"); run_next_test(); @@ -80,7 +58,7 @@ add_test(function testGetAll() { }); add_test(function testLaunchApp() { - let manifestURL = "app://" + gAppId + "/manifest.webapp"; + let manifestURL = APP_ORIGIN + "/manifest.webapp"; let startPoint = "/index.html"; let request = { type: "launch", @@ -101,7 +79,7 @@ add_test(function testLaunchApp() { }); add_test(function testCloseApp() { - let manifestURL = "app://" + gAppId + "/manifest.webapp"; + let manifestURL = APP_ORIGIN + "/manifest.webapp"; let request = { type: "close", manifestURL: manifestURL @@ -158,8 +136,7 @@ add_test(function testGetIconWithCustomSize() { }); add_test(function testUninstall() { - let origin = "app://" + gAppId; - let manifestURL = origin + "/manifest.webapp"; + let manifestURL = APP_ORIGIN + "/manifest.webapp"; let request = { type: "uninstall", manifestURL: manifestURL @@ -169,7 +146,7 @@ add_test(function testUninstall() { Services.obs.removeObserver(observer, topic); let json = JSON.parse(data); do_check_eq(json.manifestURL, manifestURL); - do_check_eq(json.origin, origin); + do_check_eq(json.origin, APP_ORIGIN); do_check_eq(json.id, gAppId); run_next_test(); }, "webapps-uninstall", false); diff --git a/toolkit/devtools/apps/tests/unit/xpcshell.ini b/toolkit/devtools/apps/tests/unit/xpcshell.ini index d73c51ada562..fef789904fba 100644 --- a/toolkit/devtools/apps/tests/unit/xpcshell.ini +++ b/toolkit/devtools/apps/tests/unit/xpcshell.ini @@ -4,6 +4,3 @@ tail = tail_apps.js [test_webappsActor.js] skip-if = (os == "win" || "linux" || "mac") -[test_appInstall.js] -# Persistent failures. -skip-if = true