Bug 1024090 - Ensure proper loading of Notification database. r=gwagner

This commit is contained in:
Alexandre Lissy 2014-06-14 03:52:00 -04:00
parent 246fa4cdb5
commit 80b6debe50
6 changed files with 132 additions and 64 deletions

View File

@ -82,9 +82,9 @@ let NotificationDB = {
var promise = OS.File.read(NOTIFICATION_STORE_PATH, { encoding: "utf-8"});
return promise.then(
function onSuccess(data) {
// JSON parsing failure will get handled by a later catch in the promise
// chain
this.notifications = JSON.parse(data);
if (data.length > 0) {
this.notifications = JSON.parse(data);
}
// populate the list of notifications by tag
if (this.notifications) {
for (var origin in this.notifications) {

View File

@ -150,9 +150,12 @@ NotificationStorage.prototype = {
} catch (e) {
debug("Error calling callback done: " + e);
}
case kMessageNotificationSaveOk:
case kMessageNotificationDeleteOk:
debug("Error received when treating: '" + message.name + "': " + message.data.errorMsg);
case kMessageNotificationSaveKo:
case kMessageNotificationDeleteKo:
if (DEBUG) {
debug("Error received when treating: '" + message.name +
"': " + message.data.errorMsg);
}
break;
default:

View File

@ -0,0 +1,63 @@
"use strict";
const Cu = Components.utils;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
"@mozilla.org/childprocessmessagemanager;1",
"nsIMessageSender");
let systemNotification = {
origin: "app://system.gaiamobile.org/manifest.webapp",
id: "{2bc883bf-2809-4432-b0f4-f54e10372764}",
title: "SystemNotification:" + Date.now(),
dir: "auto",
lang: "",
body: "System notification body",
tag: "",
icon: "icon.png"
};
let calendarNotification = {
origin: "app://calendar.gaiamobile.org/manifest.webapp",
id: "{d8d11299-a58e-429b-9a9a-57c562982fbf}",
title: "CalendarNotification:" + Date.now(),
dir: "auto",
lang: "",
body: "Calendar notification body",
tag: "",
icon: "icon.png"
};
// Helper to start the NotificationDB
function startNotificationDB() {
Cu.import("resource://gre/modules/NotificationDB.jsm");
}
// Helper function to add a listener, send message and treat the reply
function addAndSend(msg, reply, callback, payload, runNext = true) {
let handler = {
receiveMessage: function(message) {
if (message.name === reply) {
cpmm.removeMessageListener(reply, handler);
callback(message);
if (runNext) {
run_next_test();
}
}
}
};
cpmm.addMessageListener(reply, handler);
cpmm.sendAsyncMessage(msg, payload);
}
// helper fonction, comparing two notifications
function compareNotification(notif1, notif2) {
// retrieved notification should be the second one sent
for (let prop in notif1) {
// compare each property
do_check_eq(notif1[prop], notif2[prop]);
}
}

View File

@ -1,66 +1,11 @@
const Cu = Components.utils;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
"@mozilla.org/childprocessmessagemanager;1",
"nsIMessageSender");
let systemNotification = {
origin: "app://system.gaiamobile.org/manifest.webapp",
id: "{2bc883bf-2809-4432-b0f4-f54e10372764}",
title: "SystemNotification:" + Date.now(),
dir: "auto",
lang: "",
body: "System notification body",
tag: "",
icon: "icon.png"
};
let calendarNotification = {
origin: "app://calendar.gaiamobile.org/manifest.webapp",
id: "{d8d11299-a58e-429b-9a9a-57c562982fbf}",
title: "CalendarNotification:" + Date.now(),
dir: "auto",
lang: "",
body: "Calendar notification body",
tag: "",
icon: "icon.png"
};
"use strict";
function run_test() {
do_get_profile();
Cu.import("resource://gre/modules/NotificationDB.jsm");
startNotificationDB();
run_next_test();
}
// Helper function to add a listener, send message and treat the reply
function addAndSend(msg, reply, callback, payload, runNext = true) {
let handler = {
receiveMessage: function(message) {
if (message.name === reply) {
cpmm.removeMessageListener(reply, handler);
callback(message);
if (runNext) {
run_next_test();
}
}
}
};
cpmm.addMessageListener(reply, handler);
cpmm.sendAsyncMessage(msg, payload);
}
// helper fonction, comparing two notifications
function compareNotification(notif1, notif2) {
// retrieved notification should be the second one sent
for (let prop in notif1) {
// compare each property
do_check_eq(notif1[prop], notif2[prop]);
}
}
// Get one notification, none exists
add_test(function test_get_none() {
let requestID = 0;

View File

@ -0,0 +1,56 @@
"use strict";
function run_test() {
do_get_profile();
run_next_test();
}
/// For bug 1024090: test edge case of notificationstore.json
add_test(function test_bug1024090_purge() {
Cu.import("resource://gre/modules/osfile.jsm");
const NOTIFICATION_STORE_PATH =
OS.Path.join(OS.Constants.Path.profileDir, "notificationstore.json");
let cleanup = OS.File.writeAtomic(NOTIFICATION_STORE_PATH, "");
cleanup.then(
function onSuccess() {
ok(true, "Notification database cleaned.");
},
function onError(reason) {
ok(false, "Notification database error when cleaning: " + reason);
}
).then(function next() {
do_print("Cleanup steps completed: " + NOTIFICATION_STORE_PATH);
startNotificationDB();
run_next_test();
});
});
// Store one notification
add_test(function test_bug1024090_send_one() {
let requestID = 1;
let msgReply = "Notification:Save:Return:OK";
let msgHandler = function(message) {
equal(requestID, message.data.requestID, "Checking requestID");
};
addAndSend("Notification:Save", msgReply, msgHandler, {
origin: systemNotification.origin,
notification: systemNotification,
requestID: requestID
});
});
// Get one notification, one exists
add_test(function test_bug1024090_get_one() {
let requestID = 2;
let msgReply = "Notification:GetAll:Return:OK";
let msgHandler = function(message) {
equal(requestID, message.data.requestID, "Checking requestID");
equal(1, message.data.notifications.length, "One notification stored");
};
addAndSend("Notification:GetAll", msgReply, msgHandler, {
origin: systemNotification.origin,
requestID: requestID
});
});

View File

@ -1,5 +1,6 @@
[DEFAULT]
head =
head = common_test_notificationdb.js
tail =
[test_notificationdb.js]
[test_notificationdb_bug1024090.js]