Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
173 lines (163 sloc) 5.7 KB
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<button onclick="next();">Run tests</button>
<p>Warning! Running these tests will destroy all your WebContacts data!</p>
<script type="application/javascript;version=1.8" src="contacts.js"></script>
<script type="application/javascript;version=1.8">
let sample_contact = {
id: "c3b2c2b8-0eff-4ed6-865e-a372b7e0154f",
displayName: "Philipp von Weitershausen",
name: {
familyName: "von Weitershausen",
givenName: "Philipp",
honorificPrefix: "",
honorificSuffic: "",
middleName: "",
},
nickname: "philikon",
phoneNumbers: [],
emails: [],
addresses: [],
ims: [],
organizations: [],
birthday: null,
note: "",
photos: [],
categories: [],
urls: []
};
let fields = ["id", "name", "ims"];
function onSuccess(contacts) {
console.log("onSuccess", contacts);
next();
}
function onFailure(error) {
console.error("onFailure", error);
}
function assert(assertion, msg) {
if (assertion) {
console.info("TEST-PASS", msg);
} else {
throw "TEST-UNEXPECTED_FAIL " + msg;
}
}
let index = 0;
let steps = [
function () {
console.log("Deleting database");
let request = window.mozIndexedDB.deleteDatabase("contacts");
request.onsuccess = request.onerror = function (event) {
console.log("Deleted the database", event.errorCode);
next();
};
},
function () {
console.log("Retrieving all contacts...");
window.navigator.mozContacts.find(fields, function (contacts) {
assert(contacts.length == 0, "Empty database is empty.");
next();
}, onFailure, {});
},
function () {
console.log("Adding a new contact", sample_contact);
window.navigator.mozContacts.create(function (contact) {
assert(contact.id == sample_contact.id, "Same ID"); // TODO have the contacts service generate the ID?
assert(contact.displayName == sample_contact.displayName, "Same displayName");
next();
}, onFailure, sample_contact);
},
function () {
console.log("Retrieving all contacts...");
window.navigator.mozContacts.find(fields, function (contacts) {
assert(contacts.length == 1, "Found exactly 1 contact.");
assert(contacts[0].id == sample_contact.id, "Same ID");
assert(contacts[0].displayName == sample_contact.displayName, "Same displayName");
assert(contacts[0].ims.length == 0, "No IMS info.");
next();
}, onFailure, {});
},
function () {
console.log("Retrieving a specific contact by ID...");
window.navigator.mozContacts.find(fields, function (contacts) {
assert(contacts.length == 1, "Found exactly 1 contact.");
assert(contacts[0].id == sample_contact.id, "Same ID");
assert(contacts[0].displayName == sample_contact.displayName, "Same displayName");
assert(contacts[0].ims.length == 0, "No IMS info.");
next();
}, onFailure, {filter: {id: sample_contact.id}});
},
/*
function () {
console.log("Retrieving a specific contact by givenName...");
window.navigator.mozContacts.find(fields, function (contacts) {
assert(contacts.length == 1, "Found exactly 1 contact.");
assert(contacts[0].id == sample_contact.id, "Same ID");
assert(contacts[0].displayName == sample_contact.displayName, "Same displayName");
assert(contacts[0].ims.length == 0, "No IMS info.");
next();
}, onFailure, {filter: {givenName: sample_contact.name.givenName}});
},
*/
function () {
sample_contact.ims.push({type: "IRC",
value: "philikon",
pref: true});
console.log("Modifying contact", sample_contact);
window.navigator.mozContacts.update(function () {
console.log("Retrieving all contacts...");
window.navigator.mozContacts.find(fields, function (contacts) {
assert(contacts.length == 1, "Found exactly 1 contact.");
assert(contacts[0].id == sample_contact.id, "Same ID");
assert(contacts[0].displayName == sample_contact.displayName, "Same displayName");
assert(contacts[0].ims.length == 1, "Found exactly 1 IMS info.");
assert(contacts[0].ims[0].type == "IRC", "IMS type is correct.")
assert(contacts[0].ims[0].value == "philikon", "IMS value is correct.")
assert(contacts[0].ims[0].pref == true, "IMS pref is correct.")
next();
}, onFailure, {});
}, onFailure, sample_contact);
},
function () {
console.log("Searching contacts by query...");
window.navigator.mozContacts.find(fields, function (contacts) {
assert(contacts.length == 1, "Found exactly 1 contact.");
assert(contacts[0].id == sample_contact.id, "Same ID");
assert(contacts[0].displayName == sample_contact.displayName, "Same displayName");
next();
}, onFailure, {search: {query: "phil", fields: ["displayName", "email"]}});
},
function () {
console.log("Deleting contact", sample_contact.id);
window.navigator.mozContacts.delete(function () {
window.navigator.mozContacts.find(fields, function (contacts) {
assert(contacts.length == 0, "Empty database is empty.");
next();
}, onFailure, {});
}, onFailure, sample_contact.id);
},
function () {
console.log("Deleting database");
window.mozIndexedDB.deleteDatabase("contacts").onsuccess = onSuccess;
},
function () {
console.log("All done");
}
];
function next() {
if (index >= steps.length) {
console.log("Shouldn't get here!", Error().stack);
return;
}
try {
steps[index]();
} catch(ex) {
console.log("Caught exception", ex);
}
index += 1;
}
</script>
</body>
</html>
Something went wrong with that request. Please try again.