Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Branch: master
Fetching contributors…

Cannot retrieve contributors at this time

189 lines (180 sloc) 6.022 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 = {
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 sample_id;
let contact;
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(function (contacts) {
assert(contacts.length == 0, "Empty database is empty.");
next();
}, onFailure, {});
},
function () {
console.log("Adding a new contact", sample_contact);
let contact = window.navigator.mozContacts.create(sample_contact);
contact.save(function () {
assert(contact.id, "The contact now has an ID.");
sample_id = contact.id;
assert(contact.displayName == sample_contact.displayName, "Same displayName");
//TODO compare more things
next();
}, onFailure);
},
function () {
console.log("Retrieving all contacts...");
window.navigator.mozContacts.find(function (contacts) {
assert(contacts.length == 1, "Found exactly 1 contact.");
contact = contacts[0];
assert(contact.id == sample_id, "Same ID");
assert(contact.displayName == sample_contact.displayName, "Same displayName");
assert(contact.ims.length == 0, "No IMS info.");
next();
}, onFailure, {});
},
function () {
console.log("Retrieving a specific contact by ID...");
let options = {filterBy: ["id"],
filterOp: "equals",
filterValue: sample_contact.id};
window.navigator.mozContacts.find(function (contacts) {
assert(contacts.length == 1, "Found exactly 1 contact.");
contact = contacts[0];
assert(contact.id == sample_id, "Same ID");
assert(contact.displayName == sample_contact.displayName, "Same displayName");
assert(contact.ims.length == 0, "No IMS info.");
next();
}, onFailure, options);
},
/*
function () {
console.log("Retrieving a specific contact by givenName...");
let options = {filterBy: ["displayName"],
filterOp: "equals",
filterValue: sample_contact.displayName};
window.navigator.mozContacts.find(function (contacts) {
assert(contacts.length == 1, "Found exactly 1 contact.");
contact = contacts[0];
assert(contact.id == sample_id, "Same ID");
assert(contact.displayName == sample_contact.displayName, "Same displayName");
assert(contact.ims.length == 0, "No IMS info.");
next();
}, onFailure, options);
},
*/
function () {
contact.ims.push({type: "IRC",
value: "philikon",
pref: true});
console.log("Modifying contact", contact);
contact.save(function () {
console.log("Retrieving all contacts...");
window.navigator.mozContacts.find(function (contacts) {
assert(contacts.length == 1, "Found exactly 1 contact.");
contact = contacts[0];
assert(contact.id == sample_id, "Same ID");
assert(contact.displayName == sample_contact.displayName, "Same displayName");
assert(contact.ims.length == 1, "Found exactly 1 IMS info.");
assert(contact.ims[0].type == "IRC", "IMS type is correct.")
assert(contact.ims[0].value == "philikon", "IMS value is correct.")
assert(contact.ims[0].pref == true, "IMS pref is correct.")
next();
}, onFailure, {});
}, onFailure, sample_contact);
},
function () {
console.log("Searching contacts by query...");
let options = {filterBy: ["displayName", "email"],
filterOp: "icontains",
filterValue: "phil"};
window.navigator.mozContacts.find(function (contacts) {
assert(contacts.length == 1, "Found exactly 1 contact.");
contact = contacts[0];
assert(contact.id == sample_id, "Same ID");
assert(contact.displayName == sample_contact.displayName, "Same displayName");
next();
}, onFailure, options);
},
function () {
console.log("Deleting contact", contact.id);
contact.remove(function () {
window.navigator.mozContacts.find(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>
Jump to Line
Something went wrong with that request. Please try again.