gecko/dom/contacts/tests/test_contacts_cache.xul

169 lines
5.0 KiB
XML

<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="chrome://global/skin"?>
<?xml-stylesheet type="text/css" href="/tests/SimpleTest/test.css"?>
<!--
Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/
-->
<window title="Mozilla Bug 1114520"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script type="application/javascript"
src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
<script type="application/javascript;version=1.7">
<![CDATA[
"use strict";
const { 'utils': Cu } = Components;
Cu.import("resource://gre/modules/ContactDB.jsm", window);
let contactsDB = new ContactDB();
contactsDB.init();
function makeFailure(reason, skipDelete) {
return function() {
ok(false, reason);
if (skipDelete) {
SimpleTest.finish();
return;
}
deleteDatabase(SimpleTest.finish);
};
};
function deleteDatabase(then) {
contactsDB.close();
let req = indexedDB.deleteDatabase(DB_NAME);
req.onsuccess = then;
req.onblocked = makeFailure("blocked", true);
req.onupgradeneeded = makeFailure("onupgradeneeded", true);
req.onerror = makeFailure("onerror", true);
};
function checkRevision(expectedRevision, then) {
contactsDB.getRevision(function(revision) {
ok(expectedRevision === revision, "Revision OK");
then();
}, makeFailure("Could not get revision"));
};
let CONTACT_PROPS = {
id: "ab74671e36be41b680f8f030e7e24ea2",
properties: {
name: ["Magnificentest foo bar the third"],
givenName: ["foo"],
familyName: ["bar"]
}
};
let ANOTHER_CONTACT_PROPS = {
id: "b461d53d548b4e8aaa8256911a415f79",
properties: {
name: ["Magnificentest foo bar the fourth"],
givenName: ["foo"],
familyName: ["bar"]
}
};
let Tests = [function() {
info("Deleting database");
deleteDatabase(next);
}, function() {
info("Checking initial revision");
checkRevision(0, next);
}, function() {
info("Save contact");
contactsDB.saveContact(CONTACT_PROPS, function() {
ok(true, "Saved contact successfully");
checkRevision(1, next);
}, makeFailure("Could not save contact"));
}, function() {
info("Save another contact");
contactsDB.saveContact(ANOTHER_CONTACT_PROPS, function() {
ok(true, "Saved contact successfully");
checkRevision(2, next);
}, makeFailure("Could not save contact"));
}, function() {
info("Get all contacts so cache is built");
contactsDB.getAll(function(contacts) {
ok(true, "Got all contacts " + contacts.length);
next();
}, makeFailure("Unexpected error getting contacts"), {
"sortBy":"givenName","sortOrder":"ascending"
});
}, function() {
info("Contacts cache should have both ids");
let contactsCount = 0;
contactsDB.newTxn("readonly", SAVED_GETALL_STORE_NAME, function(txn, store) {
store.openCursor().onsuccess = function(e) {
let cursor = e.target.result;
if (!cursor) {
makeFailure("Wrong cache")();
return;
}
ok(cursor.value.length == 2, "Both contacts ids are in the cache");
next();
};
}, null, makeFailure("Txn error"));
}, function() {
info("Remove contact " + CONTACT_PROPS.id);
contactsDB.removeContact(CONTACT_PROPS.id, function() {
ok(true, "Removed contact");
checkRevision(3, next);
}, makeFailure("Unexpected error removing contact "));
}, function() {
info("Check that contact has been removed for good");
contactsDB.newTxn("readonly", STORE_NAME, function(txn, store) {
let req = store.openCursor(IDBKeyRange.only(CONTACT_PROPS.id));
req.onsuccess = function(event) {
if (event.target.result) {
makeFailure("Should not have cursor")();
return;
}
ok(true, "Yep, the contact was removed");
next();
};
req.onerror = makeFailure("OpenCursor error");
});
}, function() {
info("Contacts cache should have only one id");
contactsDB.newTxn("readonly", SAVED_GETALL_STORE_NAME, function(txn, store) {
store.openCursor().onsuccess = function(e) {
let cursor = e.target.result;
if (!cursor) {
makeFailure("Missing cursor")();
return;
}
ok(cursor.value.length == 1, "Only one contacts id is in the cache");
ok(cursor.value[0] == ANOTHER_CONTACT_PROPS.id, "And it is the right id");
next();
};
}, null, makeFailure("Txn error"));
}, function() {
deleteDatabase(next);
}];
function next() {
let step = Tests.shift();
if (step) {
step();
} else {
info("All done");
SimpleTest.finish();
}
}
SimpleTest.waitForExplicitFinish();
next();
]]>
</script>
<body xmlns="http://www.w3.org/1999/xhtml">
<a href="https://bugzilla.mozilla.org/show_bug.cgi?id=1114520"
target="_blank">Mozilla Bug 1114520</a>
</body>
</window>