Bug 914604 - Convert webapps xpcshell test to mochitest-plain and cover app reinstall and redirects manifest property r=fabrice

This commit is contained in:
Alexandre Poirot 2013-09-15 12:27:00 +03:00
parent 1b1cc7da19
commit cdc337b988
13 changed files with 282 additions and 105 deletions

View File

@ -4,7 +4,8 @@
"content": "",
"docshell": "",
"dom": "",
"layout": ""
"layout": "",
"toolkit/devtools/apps": ""
},
"excludetests": {
"content/xul":"tests that use xul",

View File

@ -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

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -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");
}
});

View File

@ -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);
}

View File

@ -0,0 +1,157 @@
<!DOCTYPE html>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id={821589}
-->
<head>
<title>Test for Bug {821589} Packaged apps installation and update</title>
<script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id={821589}">Mozilla Bug {821589}</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script class="testbody" type="application/javascript;version=1.8">
"use strict";
var launchableValue = undefined;
var index = -1;
function debug(aMsg) {
//dump("== Tests debug == " + aMsg + "\n");
}
function next() {
index += 1;
if (index >= steps.length) {
ok(false, "Shouldn't get here!");
return;
}
try {
steps[index]();
} catch(ex) {
ok(false, "Caught exception", ex);
}
}
function start() {
next();
}
function finish() {
SpecialPowers.setAllAppsLaunchable(launchableValue);
SpecialPowers.removePermission("webapps-manage", document);
SimpleTest.finish();
}
function cbError(aError) {
ok(false, "Error callback invoked " + aError);
finish();
}
SimpleTest.waitForExplicitFinish();
var installTestApp;
var steps = [
function() {
ok(true, "Start setting up");
// Set up
launchableValue = SpecialPowers.setAllAppsLaunchable(true);
SpecialPowers.addPermission("webapps-manage", true, document);
SpecialPowers.addPermission("browser", true, document);
SpecialPowers.addPermission("embed-apps", true, document);
// Required on firefox as these prefs are only set on b2g:
SpecialPowers.pushPrefEnv({
set: [["dom.mozBrowserFramesEnabled", true],
["security.apps.privileged.CSP.default",
"default-src *; script-src 'self'; object-src 'none'; style-src 'self' 'unsafe-inline'"]
]
}, next);
},
function () {
// Load a chrome script in order to dispatch devtool debugger requests.
// Because of wrapping issues, we can't use SpecialPowers.Cu.import to load
// devtools jsm into mochitest scope. We end up not receiving
// DebuggerClient.addListener callback arguments...
let scriptUrl = SimpleTest.getTestFileURL("debugger-protocol-helper.js");
let mm = SpecialPowers.loadChromeScript(scriptUrl);
installTestApp = function (url, appId, callback) {
let installResponse, appObject;
mm.sendAsyncMessage("install", {url: url, appId: appId});
mm.addMessageListener("installed", function onInstalled(aResponse) {
mm.removeMessageListener("installed", onInstalled);
installResponse = aResponse;
if (appObject)
callback(aResponse, appObject);
});
navigator.mozApps.mgmt.oninstall = function(evt) {
appObject = evt.application;
if (installResponse)
callback(installResponse, appObject);
};
};
SpecialPowers.autoConfirmAppInstall(next);
},
function() {
ok(true, "== TEST == Install packaged app");
let appId = "test-app-id";
let url = SimpleTest.getTestFileURL("data/app.zip");
installTestApp(url, appId,
function (aResponse, aApp) {
ok(true, "Installed");
is(aResponse.appId, appId, "Got same app id");
if ("error" in aResponse) {
ok(false, "Error: " + aResponse.error);
}
if ("message" in aResponse) {
ok(false, "Error message: " + aResponse.message);
}
ok(!("error" in aResponse), "app installed without any error");
is(aApp.manifest.name, "Test app", "app name is correct");
next();
}
);
},
function () {
ok(true, "== TEST == Reinstall packaged app");
let appId = "test-app-id";
let url = SimpleTest.getTestFileURL("data/app-updated.zip");
installTestApp(url, appId,
function (aResponse, aApp) {
ok(true, "Reinstalled");
is(aResponse.appId, appId, "Got same app id");
if ("error" in aResponse) {
ok(false, "Error: " + aResponse.error);
}
if ("message" in aResponse) {
ok(false, "Error message: " + aResponse.message);
}
ok(!("error" in aResponse), "app installed without any error");
is(aApp.manifest.name, "updated-name", "app name on update is correct");
next();
}
);
},
function() {
ok(true, "all done!\n");
SpecialPowers.popPrefEnv(finish);
}
];
addLoadEvent(start);
</script>
</pre>
</body>
</html>

View File

@ -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);

View File

@ -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();
}

View File

@ -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);

View File

@ -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