Bug 606695 - De-Sync.js-ify history store test [r=mconnor]

This commit is contained in:
Philipp von Weitershausen 2010-10-29 06:54:41 -07:00
parent e1a52f2169
commit 0291e51761

View File

@ -1,7 +1,6 @@
Cu.import("resource://gre/modules/XPCOMUtils.jsm"); Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://services-sync/engines/history.js"); Cu.import("resource://services-sync/engines/history.js");
Cu.import("resource://services-sync/type_records/history.js"); Cu.import("resource://services-sync/type_records/history.js");
Cu.import("resource://services-sync/ext/Sync.js");
Cu.import("resource://services-sync/util.js"); Cu.import("resource://services-sync/util.js");
const TIMESTAMP1 = (Date.now() - 103406528) * 1000; const TIMESTAMP1 = (Date.now() - 103406528) * 1000;
@ -28,30 +27,40 @@ function queryHistoryVisits(uri) {
return queryPlaces(uri, options); return queryPlaces(uri, options);
} }
function waitForTitleChanged(test) { function onNextTitleChanged(callback) {
Sync(function (callback) { Svc.History.addObserver({
Svc.History.addObserver({ onBeginUpdateBatch: function onBeginUpdateBatch() {},
onBeginUpdateBatch: function onBeginUpdateBatch() {}, onEndUpdateBatch: function onEndUpdateBatch() {},
onEndUpdateBatch: function onEndUpdateBatch() {}, onPageChanged: function onPageChanged() {},
onPageChanged: function onPageChanged() {}, onTitleChanged: function onTitleChanged() {
onTitleChanged: function onTitleChanged() { Svc.History.removeObserver(this);
Svc.History.removeObserver(this); Utils.delay(callback, 0, this);
callback(); },
}, onVisit: function onVisit() {},
onVisit: function onVisit() {}, onDeleteVisits: function onDeleteVisits() {},
onDeleteVisits: function onDeleteVisits() {}, onPageExpired: function onPageExpired() {},
onPageExpired: function onPageExpired() {}, onBeforeDeleteURI: function onBeforeDeleteURI() {},
onBeforeDeleteURI: function onBeforeDeleteURI() {}, onDeleteURI: function onDeleteURI() {},
onDeleteURI: function onDeleteURI() {}, onClearHistory: function onClearHistory() {},
onClearHistory: function onClearHistory() {}, QueryInterface: XPCOMUtils.generateQI([
QueryInterface: XPCOMUtils.generateQI([ Ci.nsINavHistoryObserver,
Ci.nsINavHistoryObserver, Ci.nsINavHistoryObserver_MOZILLA_1_9_1_ADDITIONS,
Ci.nsINavHistoryObserver_MOZILLA_1_9_1_ADDITIONS, Ci.nsISupportsWeakReference
Ci.nsISupportsWeakReference ])
]) }, true);
}, true); }
test();
})(); // Ensure exceptions from inside callbacks leads to test failures while
// we still clean up properly.
function ensureThrows(func) {
return function() {
try {
func.apply(this, arguments);
} catch (ex) {
Svc.History.removeAllPages();
do_throw(ex);
}
};
} }
function run_test() { function run_test() {
@ -59,15 +68,18 @@ function run_test() {
let store = new HistoryEngine()._store; let store = new HistoryEngine()._store;
do_check_eq([id for (id in store.getAllIDs())].length, 0); do_check_eq([id for (id in store.getAllIDs())].length, 0);
try { let fxuri, fxguid, tburi, tbguid;
do_test_pending();
Utils.asyncChain(function (next) {
_("Let's create an entry in the database."); _("Let's create an entry in the database.");
let fxuri = Utils.makeURI("http://getfirefox.com/"); fxuri = Utils.makeURI("http://getfirefox.com/");
Svc.History.addPageWithDetails(fxuri, "Get Firefox!", TIMESTAMP1); Svc.History.addPageWithDetails(fxuri, "Get Firefox!", TIMESTAMP1);
_("Verify that the entry exists."); _("Verify that the entry exists.");
let ids = [id for (id in store.getAllIDs())]; let ids = [id for (id in store.getAllIDs())];
do_check_eq(ids.length, 1); do_check_eq(ids.length, 1);
let fxguid = ids[0]; fxguid = ids[0];
do_check_true(store.itemExists(fxguid)); do_check_true(store.itemExists(fxguid));
_("If we query a non-existent record, it's marked as deleted."); _("If we query a non-existent record, it's marked as deleted.");
@ -85,33 +97,39 @@ function run_test() {
_("Let's modify the record and have the store update the database."); _("Let's modify the record and have the store update the database.");
let secondvisit = {date: TIMESTAMP2, let secondvisit = {date: TIMESTAMP2,
type: Ci.nsINavHistoryService.TRANSITION_TYPED}; type: Ci.nsINavHistoryService.TRANSITION_TYPED};
waitForTitleChanged(function() { onNextTitleChanged(ensureThrows(function() {
store.update({histUri: record.histUri, let queryres = queryHistoryVisits(fxuri);
title: "Hol Dir Firefox!", do_check_eq(queryres.length, 2);
visits: [record.visits[0], secondvisit]}); do_check_eq(queryres[0].time, TIMESTAMP1);
}); do_check_eq(queryres[0].title, "Hol Dir Firefox!");
let queryres = queryHistoryVisits(fxuri); do_check_eq(queryres[1].time, TIMESTAMP2);
do_check_eq(queryres.length, 2); do_check_eq(queryres[1].title, "Hol Dir Firefox!");
do_check_eq(queryres[0].time, TIMESTAMP1); next();
do_check_eq(queryres[0].title, "Hol Dir Firefox!"); }));
do_check_eq(queryres[1].time, TIMESTAMP2); store.update({histUri: record.histUri,
do_check_eq(queryres[1].title, "Hol Dir Firefox!"); title: "Hol Dir Firefox!",
visits: [record.visits[0], secondvisit]});
}, function (next) {
_("Create a brand new record through the store."); _("Create a brand new record through the store.");
let tbguid = Utils.makeGUID(); tbguid = Utils.makeGUID();
let tburi = Utils.makeURI("http://getthunderbird.com"); tburi = Utils.makeURI("http://getthunderbird.com");
waitForTitleChanged(function() { onNextTitleChanged(ensureThrows(function() {
store.create({id: tbguid, do_check_eq([id for (id in store.getAllIDs())].length, 2);
histUri: tburi.spec, let queryres = queryHistoryVisits(tburi);
title: "The bird is the word!", do_check_eq(queryres.length, 1);
visits: [{date: TIMESTAMP3, do_check_eq(queryres[0].time, TIMESTAMP3);
type: Ci.nsINavHistoryService.TRANSITION_TYPED}]}); do_check_eq(queryres[0].title, "The bird is the word!");
}); next();
do_check_eq([id for (id in store.getAllIDs())].length, 2); }));
queryres = queryHistoryVisits(tburi); store.create({id: tbguid,
do_check_eq(queryres.length, 1); histUri: tburi.spec,
do_check_eq(queryres[0].time, TIMESTAMP3); title: "The bird is the word!",
do_check_eq(queryres[0].title, "The bird is the word!"); visits: [{date: TIMESTAMP3,
type: Ci.nsINavHistoryService.TRANSITION_TYPED}]});
}, function (next) {
_("Make sure we handle invalid URLs in places databases gracefully."); _("Make sure we handle invalid URLs in places databases gracefully.");
let query = "INSERT INTO moz_places " let query = "INSERT INTO moz_places "
@ -124,7 +142,7 @@ function run_test() {
_("Remove a record from the store."); _("Remove a record from the store.");
store.remove({id: fxguid}); store.remove({id: fxguid});
do_check_false(store.itemExists(fxguid)); do_check_false(store.itemExists(fxguid));
queryres = queryHistoryVisits(fxuri); let queryres = queryHistoryVisits(fxuri);
do_check_eq(queryres.length, 0); do_check_eq(queryres.length, 0);
_("Make sure wipe works."); _("Make sure wipe works.");
@ -135,8 +153,10 @@ function run_test() {
queryres = queryHistoryVisits(tburi); queryres = queryHistoryVisits(tburi);
do_check_eq(queryres.length, 0); do_check_eq(queryres.length, 0);
} finally {
_("Clean up."); _("Clean up.");
Svc.History.removeAllPages(); Svc.History.removeAllPages();
} do_test_finished();
})();
} }