2015-01-15 10:47:49 +03:00
|
|
|
<?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);
|
|
|
|
};
|
|
|
|
|
2015-01-30 12:29:39 +03:00
|
|
|
function checkRevision(expectedRevision, then) {
|
|
|
|
contactsDB.getRevision(function(revision) {
|
|
|
|
ok(expectedRevision === revision, "Revision OK");
|
|
|
|
then();
|
|
|
|
}, makeFailure("Could not get revision"));
|
|
|
|
};
|
|
|
|
|
2015-01-15 10:47:49 +03:00
|
|
|
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() {
|
2015-01-30 12:29:39 +03:00
|
|
|
info("Deleting database");
|
|
|
|
deleteDatabase(next);
|
|
|
|
}, function() {
|
|
|
|
info("Checking initial revision");
|
|
|
|
checkRevision(0, next);
|
|
|
|
}, function() {
|
2015-01-15 10:47:49 +03:00
|
|
|
info("Save contact");
|
|
|
|
contactsDB.saveContact(CONTACT_PROPS, function() {
|
|
|
|
ok(true, "Saved contact successfully");
|
2015-01-30 12:29:39 +03:00
|
|
|
checkRevision(1, next);
|
|
|
|
}, makeFailure("Could not save contact"));
|
2015-01-15 10:47:49 +03:00
|
|
|
}, function() {
|
|
|
|
info("Save another contact");
|
|
|
|
contactsDB.saveContact(ANOTHER_CONTACT_PROPS, function() {
|
|
|
|
ok(true, "Saved contact successfully");
|
2015-01-30 12:29:39 +03:00
|
|
|
checkRevision(2, next);
|
|
|
|
}, makeFailure("Could not save contact"));
|
2015-01-15 10:47:49 +03:00
|
|
|
}, function() {
|
|
|
|
info("Get all contacts so cache is built");
|
|
|
|
contactsDB.getAll(function(contacts) {
|
|
|
|
ok(true, "Got all contacts " + contacts.length);
|
|
|
|
next();
|
2015-01-30 12:29:39 +03:00
|
|
|
}, makeFailure("Unexpected error getting contacts"), {
|
|
|
|
"sortBy":"givenName","sortOrder":"ascending"
|
|
|
|
});
|
2015-01-15 10:47:49 +03:00
|
|
|
}, 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) {
|
2015-01-30 12:29:39 +03:00
|
|
|
makeFailure("Wrong cache")();
|
2015-01-15 10:47:49 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
ok(cursor.value.length == 2, "Both contacts ids are in the cache");
|
|
|
|
next();
|
|
|
|
};
|
2015-01-30 12:29:39 +03:00
|
|
|
}, null, makeFailure("Txn error"));
|
2015-01-15 10:47:49 +03:00
|
|
|
}, function() {
|
|
|
|
info("Remove contact " + CONTACT_PROPS.id);
|
|
|
|
contactsDB.removeContact(CONTACT_PROPS.id, function() {
|
|
|
|
ok(true, "Removed contact");
|
2015-01-30 12:29:39 +03:00
|
|
|
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");
|
2015-01-15 10:47:49 +03:00
|
|
|
});
|
|
|
|
}, 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) {
|
2015-01-30 12:29:39 +03:00
|
|
|
makeFailure("Missing cursor")();
|
2015-01-15 10:47:49 +03:00
|
|
|
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();
|
|
|
|
};
|
2015-01-30 12:29:39 +03:00
|
|
|
}, null, makeFailure("Txn error"));
|
2015-01-15 10:47:49 +03:00
|
|
|
}, 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>
|