gecko/dom/contacts/tests/test_contacts_basics2.html

853 lines
26 KiB
HTML

<!DOCTYPE html>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=674720
-->
<head>
<title>Test for Bug 674720 WebContacts</title>
<script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=674720">Mozilla Bug 674720</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script type="text/javascript;version=1.8" src="http://mochi.test:8888/tests/dom/contacts/tests/shared.js"></script>
<script class="testbody" type="text/javascript">
"use strict";
var req;
var steps = [
function () {
ok(true, "Adding a new contact");
createResult1 = new mozContact(properties1);
req = mozContacts.save(createResult1)
req.onsuccess = function () {
ok(createResult1.id, "The contact now has an ID.");
sample_id1 = createResult1.id;
next();
}
req.onerror = onFailure;
},
function () {
ok(true, "Adding a new contact2");
createResult2 = new mozContact(properties2);
req = mozContacts.save(createResult2);
req.onsuccess = function () {
ok(createResult2.id, "The contact now has an ID.");
sample_id2 = createResult2.id;
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Retrieving all contacts");
req = mozContacts.find({sortBy: "familyName"});
req.onsuccess = function () {
is(req.result.length, 2, "Found exactly 2 contact.");
checkContacts(req.result[1], properties1);
next();
}
req.onerror = onFailure;
},
function () {
console.log("Searching contacts by query1");
var options = {filterBy: ["givenName", "email"],
filterOp: "startsWith",
filterValue: properties1.givenName[0].substring(0, 4)}
req = mozContacts.find(options)
req.onsuccess = function () {
is(req.result.length, 1, "Found exactly 1 contact.");
findResult1 = req.result[0];
ok(findResult1.id == sample_id1, "Same ID");
checkContacts(findResult1, createResult1);
next();
}
req.onerror = onFailure;
},
function () {
ok(true, "Searching contacts by query2");
var options = {filterBy: ["givenName", "email"],
filterOp: "startsWith",
filterValue: properties2.givenName[0].substring(0, 4)};
req = mozContacts.find(options);
req.onsuccess = function () {
is(req.result.length, 1, "Found exactly 1 contact.");
findResult1 = req.result[0];
is(findResult1.adr.length, 2, "Adr length 2");
checkContacts(findResult1, createResult2);
next();
}
req.onerror = onFailure;
},
function () {
ok(true, "Searching contacts by tel");
var options = {filterBy: ["tel"],
filterOp: "contains",
filterValue: properties2.tel[0].value.substring(3, 7)};
req = mozContacts.find(options);
req.onsuccess = function () {
is(req.result.length, 1, "Found exactly 1 contact.");
findResult1 = req.result[0];
ok(findResult1.id == sample_id2, "Same ID");
checkContacts(findResult1, createResult2);
next();
}
req.onerror = onFailure;
},
function () {
ok(true, "Searching contacts by email");
var options = {filterBy: ["email"],
filterOp: "startsWith",
filterValue: properties2.email[0].value.substring(0, 4)};
req = mozContacts.find(options);
req.onsuccess = function () {
is(req.result.length, 1, "Found exactly 1 contact.");
findResult1 = req.result[0];
ok(findResult1.id == sample_id2, "Same ID");
checkContacts(findResult1, createResult2);
next();
}
req.onerror = onFailure;
},
function () {
ok(true, "Deleting database");
req = mozContacts.clear();
req.onsuccess = function () {
next();
}
req.onerror = onFailure;
},
function () {
ok(true, "Adding 20 contacts");
for (var i=0; i<19; i++) {
createResult1 = new mozContact(properties1);
req = mozContacts.save(createResult1);
req.onsuccess = function () {
ok(createResult1.id, "The contact now has an ID.");
};
req.onerror = onFailure;
};
createResult1 = new mozContact(properties1);
req = mozContacts.save(createResult1);
req.onsuccess = function () {
ok(createResult1.id, "The contact now has an ID.");
checkStrArray(createResult1.name, properties1.name, "Same Name");
checkCount(20, "20 contacts in DB", next);
};
req.onerror = onFailure;
},
function () {
ok(true, "Retrieving all contacts");
req = mozContacts.find(defaultOptions);
req.onsuccess = function () {
is(req.result.length, 20, "20 Entries.");
next();
}
req.onerror = onFailure;
},
function () {
ok(true, "Retrieving all contacts with limit 10");
var options = { filterLimit: 10 };
req = mozContacts.find(options);
req.onsuccess = function () {
is(req.result.length, 10, "10 Entries.");
next();
}
req.onerror = onFailure;
},
function () {
ok(true, "Retrieving all contacts with limit 10 and sorted");
var options = { filterLimit: 10,
sortBy: 'FamilyName',
sortOrder: 'descending' };
req = mozContacts.find(options);
req.onsuccess = function () {
is(req.result.length, 10, "10 Entries.");
next();
}
req.onerror = onFailure;
},
function () {
ok(true, "Retrieving all contacts2");
var options = {filterBy: ["givenName"],
filterOp: "startsWith",
filterValue: properties1.givenName[0].substring(0, 4)};
req = mozContacts.find(options);
req.onsuccess = function () {
is(req.result.length, 20, "20 Entries.");
checkContacts(createResult1, req.result[19]);
next();
}
req.onerror = onFailure;
},
function () {
ok(true, "Retrieving all contacts3");
var options = {filterBy: ["givenName", "tel", "email"],
filterOp: "startsWith",
filterValue: properties1.givenName[0].substring(0, 4)};
req = mozContacts.find(options);
req.onsuccess = function () {
is(req.result.length, 20, "20 Entries.");
checkContacts(createResult1, req.result[10]);
next();
}
req.onerror = onFailure;
},
function () {
ok(true, "Deleting database");
req = mozContacts.clear();
req.onsuccess = function () {
next();
}
req.onerror = onFailure;
},
function () {
ok(true, "Testing clone contact");
createResult1 = new mozContact(properties1);
req = mozContacts.save(createResult1);
req.onsuccess = function () {
ok(createResult1.id, "The contact now has an ID.");
checkStrArray(createResult1.name, properties1.name, "Same Name");
next();
}
req.onerror = onFailure;
},
function() {
ok(true, "Testing clone contact2");
var cloned = new mozContact(createResult1);
ok(cloned.id != createResult1.id, "Cloned contact has new ID");
cloned.email = [{value: "new email!"}];
cloned.givenName = ["Tom"];
req = mozContacts.save(cloned);
req.onsuccess = function () {
ok(cloned.id, "The contact now has an ID.");
is(cloned.email[0].value, "new email!", "Same Email");
isnot(createResult1.email[0].value, cloned.email[0].value, "Clone has different email");
is(cloned.givenName, "Tom", "New Name");
next();
}
req.onerror = onFailure;
},
function () {
ok(true, "Retrieving all contacts");
var options = {filterBy: ["givenName"],
filterOp: "startsWith",
filterValue: properties2.givenName[0].substring(0, 4)};
req = mozContacts.find(defaultOptions);
req.onsuccess = function () {
is(req.result.length, 2, "2 Entries.");
next();
}
req.onerror = onFailure;
},
function () {
ok(true, "Search with redundant fields should only return 1 contact");
createResult1 = new mozContact({name: ["XXX"],
givenName: ["XXX"],
email: [{value: "XXX"}],
tel: [{value: "XXX"}]
});
req = mozContacts.save(createResult1);
req.onsuccess = function() {
var options = {filterBy: ["givenName", "familyName"],
filterOp: "equals",
filterValue: "XXX"};
var req2 = mozContacts.find(options);
req2.onsuccess = function() {
is(req2.result.length, 1, "1 Entry");
next();
}
req2.onerror = onFailure;
}
req.onerror = onFailure;
},
function () {
ok(true, "Deleting database");
req = mozContacts.clear()
req.onsuccess = function () {
ok(true, "Deleted the database");
next();
}
req.onerror = onFailure;
},
function () {
ok(true, "Test sorting");
createResult1 = new mozContact(c3);
req = navigator.mozContacts.save(createResult1);
req.onsuccess = function () {
ok(createResult1.id, "The contact now has an ID.");
checkContacts(c3, createResult1);
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Test sorting");
createResult1 = new mozContact(c2);
req = navigator.mozContacts.save(createResult1);
req.onsuccess = function () {
ok(createResult1.id, "The contact now has an ID.");
checkContacts(c2, createResult1);
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Test sorting");
createResult1 = new mozContact(c4);
req = navigator.mozContacts.save(createResult1);
req.onsuccess = function () {
ok(createResult1.id, "The contact now has an ID.");
checkContacts(c4, createResult1);
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Test sorting");
createResult1 = new mozContact(c1);
req = navigator.mozContacts.save(createResult1);
req.onsuccess = function () {
ok(createResult1.id, "The contact now has an ID.");
checkContacts(c1, createResult1);
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Test sorting");
var options = {sortBy: "familyName",
sortOrder: "ascending"};
req = navigator.mozContacts.find(options);
req.onsuccess = function () {
is(req.result.length, 4, "4 results");
checkContacts(req.result[0], c1);
checkContacts(req.result[1], c2);
checkContacts(req.result[2], c3);
checkContacts(req.result[3], c4);
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Test sorting");
var options = {sortBy: "familyName",
sortOrder: "descending"};
req = navigator.mozContacts.find(options);
req.onsuccess = function () {
is(req.result.length, 4, "4 results");
checkContacts(req.result[0], c4);
checkContacts(req.result[1], c3);
checkContacts(req.result[2], c2);
checkContacts(req.result[3], c1);
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Test sorting");
createResult1 = new mozContact(c5);
req = navigator.mozContacts.save(createResult1);
req.onsuccess = function () {
ok(createResult1.id, "The contact now has an ID.");
checkContacts(c5, createResult1);
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Test sorting with empty string");
var options = {sortBy: "familyName",
sortOrder: "ascending"};
req = navigator.mozContacts.find(options);
req.onsuccess = function () {
is(req.result.length, 5, "5 results");
checkContacts(req.result[0], c5);
checkContacts(req.result[1], c1);
checkContacts(req.result[2], c2);
checkContacts(req.result[3], c3);
checkContacts(req.result[4], c4);
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Don't allow to add custom fields");
createResult1 = new mozContact({givenName: ["customTest"], yyy: "XXX"});
req = mozContacts.save(createResult1);
req.onsuccess = function() {
var options = {filterBy: ["givenName"],
filterOp: "equals",
filterValue: "customTest"};
var req2 = mozContacts.find(options);
req2.onsuccess = function() {
is(req2.result.length, 1, "1 Entry");
checkStrArray(req2.result[0].givenName, ["customTest"], "same name");
ok(req2.result.yyy === undefined, "custom property undefined");
next();
}
req2.onerror = onFailure;
}
req.onerror = onFailure;
},
function () {
ok(true, "Deleting database");
req = mozContacts.clear()
req.onsuccess = function () {
ok(true, "Deleted the database");
next();
}
req.onerror = onFailure;
},
function () {
ok(true, "Test sorting");
createResult1 = new mozContact(c7);
req = navigator.mozContacts.save(createResult1);
req.onsuccess = function () {
ok(createResult1.id, "The contact now has an ID.");
checkContacts(c7, createResult1);
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Test sorting");
createResult1 = new mozContact(c6);
req = navigator.mozContacts.save(createResult1);
req.onsuccess = function () {
ok(createResult1.id, "The contact now has an ID.");
checkContacts(c6, createResult1);
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Test sorting");
createResult1 = new mozContact(c8);
req = navigator.mozContacts.save(createResult1);
req.onsuccess = function () {
ok(createResult1.id, "The contact now has an ID.");
checkContacts(c8, createResult1);
next();
};
req.onerror = onFailure;
},
function () {
// Android does not support published/updated fields. Skip this.
if (isAndroid) {
next();
return;
}
ok(true, "Test sorting with published");
var options = {sortBy: "familyName",
sortOrder: "descending"};
req = navigator.mozContacts.find(options);
req.onsuccess = function () {
is(req.result.length, 3, "3 results");
ok(req.result[0].published < req.result[1].published, "Right sorting order");
ok(req.result[1].published < req.result[2].published, "Right sorting order");
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Deleting database");
req = mozContacts.clear();
req.onsuccess = function () {
ok(true, "Deleted the database");
next();
}
req.onerror = onFailure;
},
function () {
ok(true, "Adding a new contact with properties2");
createResult2 = new mozContact(properties2);
req = mozContacts.save(createResult2);
req.onsuccess = function () {
ok(createResult2.id, "The contact now has an ID.");
sample_id2 = createResult2.id;
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Test category search with startsWith");
var options = {filterBy: ["category"],
filterOp: "startsWith",
filterValue: properties2.category[0]};
req = mozContacts.find(options);
req.onsuccess = function () {
is(req.result.length, 1, "1 Entry.");
checkContacts(req.result[0], createResult2);
next();
}
req.onerror = onFailure;
},
function () {
ok(true, "Test category search with equals");
var options = {filterBy: ["category"],
filterOp: "equals",
filterValue: properties2.category[0]};
req = mozContacts.find(options);
req.onsuccess = function () {
is(req.result.length, 1, "1 Entry.");
checkContacts(req.result[0], createResult2);
next();
}
req.onerror = onFailure;
},
function () {
ok(true, "Deleting database");
req = mozContacts.clear()
req.onsuccess = function () {
ok(true, "Deleted the database");
next();
}
req.onerror = onFailure;
},
function () {
ok(true, "Adding contact for category search");
createResult1 = new mozContact({name: ["5"], givenName: ["5"]});
req = navigator.mozContacts.save(createResult1);
req.onsuccess = function () {
ok(createResult1.id, "The contact now has an ID.");
sample_id1 = createResult1.id;
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Test category search with equals");
var options = {filterBy: ["givenName"],
filterOp: "startsWith",
filterValue: "5"};
req = mozContacts.find(options);
req.onsuccess = function () {
is(req.result.length, 1, "1 Entry.");
checkContacts(req.result[0], createResult1);
next();
}
req.onerror = onFailure;
},
function () {
ok(true, "Deleting database");
req = mozContacts.clear()
req.onsuccess = function () {
ok(true, "Deleted the database");
next();
}
req.onerror = onFailure;
},
function () {
ok(true, "Adding contact with invalid data");
var obj = {
honorificPrefix: [],
honorificSuffix: [{foo: "bar"}],
sex: 17,
genderIdentity: 18,
email: [{type: ["foo"], value: "bar"}]
};
obj.honorificPrefix.__defineGetter__('0',(function() {
var c = 0;
return function() {
if (c == 0) {
c++;
return "string";
} else {
return {foo:"bar"};
}
}
})());
createResult1 = new mozContact(obj);
createResult1.email.push({aeiou: "abcde"});
req = mozContacts.save(createResult1);
req.onsuccess = function () {
checkContacts(createResult1, {
honorificPrefix: ["string"],
honorificSuffix: ["[object Object]"],
sex: "17",
genderIdentity: "18",
email: [{type: ["foo"], value: "bar"}, {}]
});
next();
};
},
function () {
ok(true, "Adding contact with no number but carrier");
createResult1 = new mozContact({ tel: [{type: ["home"], carrier: "myCarrier"} ] });
req = navigator.mozContacts.save(createResult1);
req.onsuccess = function () {
ok(createResult1.id, "The contact now has an ID.");
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Adding contact with email but no value");
createResult1 = new mozContact({ email: [{type: ["home"]}] });
req = navigator.mozContacts.save(createResult1);
req.onsuccess = function () {
ok(createResult1.id, "The contact now has an ID.");
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Testing numbersOnly search 1");
createResult1 = new mozContact({ name: ["aaaaaaaaa"], givenName: ["aaaaaaaaa"], tel: [{ value: "1234567890"}]});
req = navigator.mozContacts.save(createResult1);
req.onsuccess = function () {
ok(createResult1.id, "The contact now has an ID.");
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Test numbersOnly search 2");
var options = {filterBy: ["givenName", "tel"],
filterOp: "contains",
filterValue: "a"};
req = mozContacts.find(options);
req.onsuccess = function () {
ok(req.result.length == 1, "1 Entry.");
checkContacts(req.result[0], createResult1);
next();
}
req.onerror = onFailure;
},
function () {
ok(true, "Test numbersOnly search 3");
var options = {filterBy: ["givenName", "tel"],
filterOp: "contains",
filterValue: "b"};
req = mozContacts.find(options);
req.onsuccess = function () {
ok(req.result.length == 0, "0 Entries.");
next();
}
req.onerror = onFailure;
},
function () {
ok(true, "Test numbersOnly search 4");
var options = {filterBy: ["givenName", "tel"],
filterOp: "contains",
filterValue: "1a"};
req = mozContacts.find(options);
req.onsuccess = function () {
ok(req.result.length == 0, "0 Entries.");
next();
}
req.onerror = onFailure;
},
function () {
ok(true, "Test numbersOnly search 5");
var options = {filterBy: ["givenName", "tel"],
filterOp: "contains",
filterValue: "1(23)"};
req = mozContacts.find(options);
req.onsuccess = function () {
ok(req.result.length == 1, "1 Entry.");
next();
}
req.onerror = onFailure;
},
function () {
ok(true, "Test numbersOnly search 6");
var options = {filterBy: ["givenName", "tel"],
filterOp: "contains",
filterValue: "1(23)a"};
req = mozContacts.find(options);
req.onsuccess = function () {
ok(req.result.length == 0, "0 Entries.");
next();
}
req.onerror = onFailure;
},
function () {
ok(true, "Deleting database");
req = mozContacts.clear()
req.onsuccess = function () {
ok(true, "Deleted the database");
next();
}
req.onerror = onFailure;
},
function() {
ok(true, "Test that after setting array properties to scalar values the property os not a non-array")
const FIELDS = ["email","url","adr","tel","impp"];
createResult1 = new mozContact();
for (var prop of FIELDS) {
try {
createResult1[prop] = {type: ["foo"]};
} catch (e) {}
ok(createResult1[prop] === null ||
Array.isArray(createResult1[prop]), prop + " is array");
}
next();
},
function() {
ok(true, "Undefined properties of fields should be treated correctly");
var c = new mozContact({
adr: [{streetAddress: undefined}],
email: [{value: undefined}],
url: [{value: undefined}],
impp: [{value: undefined}],
tel: [{value: undefined}],
});
ise(c.adr[0].streetAddress, undefined, "adr.streetAddress is undefined");
ise(c.adr[0].locality, undefined, "adr.locality is undefined");
ise(c.adr[0].pref, undefined, "adr.pref is undefined");
ise(c.email[0].value, undefined, "email.value is undefined");
ise(c.url[0].value, undefined, "url.value is undefined");
ise(c.impp[0].value, undefined, "impp.value is undefined");
ise(c.tel[0].value, undefined, "tel.value is undefined");
next();
},
function() {
ok(true, "Setting array properties to an empty array should work");
var c = new mozContact();
function testArrayProp(prop) {
is(c[prop], null, "property is initially null");
c[prop] = [];
ok(Array.isArray(c[prop]), "property is an array after setting");
is(c[prop].length, 0, "property has length 0 after setting");
}
testArrayProp("email");
testArrayProp("adr");
testArrayProp("tel");
testArrayProp("impp");
testArrayProp("url");
next();
},
function() {
ok(true, "Passing a mozContact with invalid data to save() should throw");
var c = new mozContact({
photo: [],
tel: []
});
c.photo.push({});
SimpleTest.doesThrow(()=>navigator.mozContacts.save(c), "Invalid data in Blob array");
c.tel.push(123);
SimpleTest.doesThrow(()=>navigator.mozContacts.save(c), "Invalid data in dictionary array");
next();
},
function() {
ok(true, "Inline changes to array properties should be seen by save");
var c = new mozContact({
name: [],
familyName: [],
givenName: [],
nickname: [],
tel: [],
adr: [],
email: []
});
for (var prop of Object.getOwnPropertyNames(properties1)) {
if (!Array.isArray(properties1[prop])) {
continue;
}
for (var i = 0; i < properties1[prop].length; ++i) {
c[prop].push(properties1[prop][i]);
}
}
req = navigator.mozContacts.save(c);
req.onsuccess = function() {
req = navigator.mozContacts.find(defaultOptions);
req.onsuccess = function() {
ise(req.result.length, 1, "Got 1 contact");
checkContacts(req.result[0], properties1);
next();
};
req.onerror = onFailure;
};
req.onerror = onFailure;
},
clearDatabase,
function() {
ok(true, "mozContact.init deprecation message");
var c = new mozContact();
SimpleTest.monitorConsole(next, [
{ errorMessage: "mozContact.init is DEPRECATED. Use the mozContact constructor instead. " +
"See https://developer.mozilla.org/docs/WebAPI/Contacts for details." }
], /* forbidUnexpectedMsgs */ true);
c.init({name: ["Bar"]});
c.init({name: ["Bar"]});
SimpleTest.endMonitorConsole();
},
function() {
ok(true, "mozContact.init works as expected");
var c = new mozContact({name: ["Foo"]});
c.init({name: ["Bar"]});
ise(c.name[0], "Bar", "Same name");
next();
},
function() {
ok(true, "mozContact.init without parameters");
var c = new mozContact({name: ["Foo"]});
c.init();
next();
},
function() {
ok(true, "mozContact.init resets properties");
var c = new mozContact({jobTitle: ["Software Engineer"]});
c.init({nickname: ["Jobless Johnny"]});
ise(c.nickname[0], "Jobless Johnny", "Same nickname");
ok(!c.jobTitle, "jobTitle is not set");
next();
},
function() {
ok(true, "mozContacts.remove with an ID works");
var c = new mozContact({name: ["Ephemeral Jimmy"]});
req = navigator.mozContacts.save(c);
req.onsuccess = function() {
req = navigator.mozContacts.remove(c.id);
req.onsuccess = function() {
req = navigator.mozContacts.find({
filterBy: ["id"],
filterOp: "equals",
filterValue: c.id
});
req.onsuccess = function() {
ise(req.result.length, 0, "Successfully removed contact by ID");
next();
};
req.onerror = onFailure;
};
req.onerror = onFailure;
};
req.onerror = onFailure;
},
function () {
ok(true, "all done!\n");
SimpleTest.finish();
}
];
function next() {
ok(true, "Begin!");
if (index >= steps.length) {
ok(false, "Shouldn't get here!");
return;
}
try {
var i = index++;
steps[i]();
} catch(ex) {
ok(false, "Caught exception", ex);
}
}
start_tests();
</script>
</pre>
</body>
</html>