Bug 1162920 - JavaScript error at aboutServiceWorkers.js when updating the service worker. r=fabrice

This commit is contained in:
Fernando Jimenez 2015-05-13 07:46:01 +02:00
parent 341645c295
commit df59ea233e
3 changed files with 171 additions and 27 deletions

View File

@ -4,7 +4,7 @@
"use strict"
this.EXPORTED_SYMBOLS = [];
this.EXPORTED_SYMBOLS = ["AboutServiceWorkers"];
const { interfaces: Ci, utils: Cu } = Components;
@ -47,19 +47,6 @@ function serializeServiceWorkerInfo(aServiceWorkerInfo) {
return result;
}
function sendResult(aId, aResult) {
SystemAppProxy._sendCustomEvent("mozAboutServiceWorkersChromeEvent", {
id: aId,
result: aResult
});
}
function sendError(aId, aError) {
SystemAppProxy._sendCustomEvent("mozAboutServiceWorkersChromeEvent", {
id: aId,
error: aError
});
}
this.AboutServiceWorkers = {
get enabled() {
@ -75,7 +62,21 @@ this.AboutServiceWorkers = {
init: function() {
SystemAppProxy.addEventListener("mozAboutServiceWorkersContentEvent",
AboutServiceWorkers);
AboutServiceWorkers);
},
sendResult: function(aId, aResult) {
SystemAppProxy._sendCustomEvent("mozAboutServiceWorkersChromeEvent", {
id: aId,
result: aResult
});
},
sendError: function(aId, aError) {
SystemAppProxy._sendCustomEvent("mozAboutServiceWorkersChromeEvent", {
id: aId,
error: aError
});
},
handleEvent: function(aEvent) {
@ -88,10 +89,12 @@ this.AboutServiceWorkers = {
return;
}
let self = AboutServiceWorkers;
switch(message.name) {
case "init":
if (!this.enabled) {
sendResult({
if (!self.enabled) {
self.sendResult(message.id, {
enabled: false,
registrations: []
});
@ -100,7 +103,7 @@ this.AboutServiceWorkers = {
let data = gServiceWorkerManager.getAllRegistrations();
if (!data) {
sendError(message.id, "NoServiceWorkersRegistrations");
self.sendError(message.id, "NoServiceWorkersRegistrations");
return;
}
@ -116,26 +119,26 @@ this.AboutServiceWorkers = {
registrations.push(serializeServiceWorkerInfo(info));
}
sendResult(message.id, {
enabled: this.enabled,
self.sendResult(message.id, {
enabled: self.enabled,
registrations: registrations
});
break;
case "update":
if (!message.scope) {
sendError(message.id, "MissingScope");
self.sendError(message.id, "MissingScope");
return;
}
gServiceWorkerManager.update(message.scope);
sendResult(message.id, true);
gServiceWorkerManager.softUpdate(message.scope);
self.sendResult(message.id, true);
break;
case "unregister":
if (!message.principal ||
!message.principal.origin ||
!message.principal.appId) {
sendError("MissingPrincipal");
self.sendError("MissingPrincipal");
return;
}
@ -146,17 +149,17 @@ this.AboutServiceWorkers = {
);
if (!message.scope) {
sendError("MissingScope");
self.sendError("MissingScope");
return;
}
let serviceWorkerUnregisterCallback = {
unregisterSucceeded: function() {
sendResult(message.id, true);
self.sendResult(message.id, true);
},
unregisterFailed: function() {
sendError(message.id, "UnregisterError");
self.sendError(message.id, "UnregisterError");
},
QueryInterface: XPCOMUtils.generateQI([

View File

@ -0,0 +1,139 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
const {utils: Cu} = Components;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "AboutServiceWorkers",
"resource://gre/modules/AboutServiceWorkers.jsm");
XPCOMUtils.defineLazyServiceGetter(this, "gServiceWorkerManager",
"@mozilla.org/serviceworkers/manager;1",
"nsIServiceWorkerManager");
const CHROME_MSG = "mozAboutServiceWorkersChromeEvent";
const ORIGINAL_SENDRESULT = AboutServiceWorkers.sendResult;
const ORIGINAL_SENDERROR = AboutServiceWorkers.sendError;
do_get_profile();
let mockSendResult = (aId, aResult) => {
let msg = {
id: aId,
result: aResult
};
Services.obs.notifyObservers({wrappedJSObject: msg}, CHROME_MSG, null);
};
let mockSendError = (aId, aError) => {
let msg = {
id: aId,
result: aError
};
Services.obs.notifyObservers({wrappedJSObject: msg}, CHROME_MSG, null);
};
function attachMocks() {
AboutServiceWorkers.sendResult = mockSendResult;
AboutServiceWorkers.sendError = mockSendError;
}
function restoreMocks() {
AboutServiceWorkers.sendResult = ORIGINAL_SENDRESULT;
AboutServiceWorkers.sendError = ORIGINAL_SENDERROR;
}
do_register_cleanup(restoreMocks);
function run_test() {
run_next_test();
}
/**
* "init" tests
*/
[
// Pref disabled, no registrations
{
prefEnabled: false,
expectedMessage: {
id: Date.now(),
result: {
enabled: false,
registrations: []
}
}
},
// Pref enabled, no registrations
{
prefEnabled: true,
expectedMessage: {
id: Date.now(),
result: {
enabled: true,
registrations: []
}
}
}].forEach(test => {
add_test(function() {
Services.prefs.setBoolPref("dom.serviceWorkers.enabled", test.prefEnabled);
let id = test.expectedMessage.id;
function onMessage(subject, topic, data) {
let message = subject.wrappedJSObject;
let expected = test.expectedMessage;
do_check_true(message.id, "Message should have id");
do_check_eq(message.id, test.expectedMessage.id,
"Id should be the expected one");
do_check_eq(message.result.enabled, expected.result.enabled,
"Pref should be disabled");
do_check_true(message.result.registrations, "Registrations should exist");
do_check_eq(message.result.registrations.length,
expected.result.registrations.length,
"Registrations length should be the expected one");
Services.obs.removeObserver(onMessage, CHROME_MSG);
run_next_test();
}
Services.obs.addObserver(onMessage, CHROME_MSG, false);
attachMocks();
AboutServiceWorkers.handleEvent({ detail: {
id: id,
name: "init"
}});
});
});
/**
* ServiceWorkerManager tests.
*/
// We cannot register a sw via ServiceWorkerManager cause chrome
// registrations are not allowed.
// All we can do for now is to test the interface of the swm.
add_test(function test_swm() {
do_check_true(gServiceWorkerManager, "SWM exists");
do_check_true(gServiceWorkerManager.getAllRegistrations,
"SWM.getAllRegistrations exists");
do_check_true(typeof gServiceWorkerManager.getAllRegistrations == "function",
"SWM.getAllRegistrations is a function");
do_check_true(gServiceWorkerManager.softUpdate, "SWM.softUpdate exists");
do_check_true(typeof gServiceWorkerManager.softUpdate == "function",
"SWM.softUpdate is a function");
do_check_true(gServiceWorkerManager.unregister, "SWM.unregister exists");
do_check_true(typeof gServiceWorkerManager.unregister == "function",
"SWM.unregister exists");
run_next_test();
});

View File

@ -28,3 +28,5 @@ skip-if = toolkit != "gonk"
[test_logshake_gonk.js]
# only run on b2g builds due to requiring b2g-specific log files to exist
skip-if = (toolkit != "gonk")
[test_aboutserviceworkers.js]