gecko/dom/contacts/tests/test_contacts_international.html

256 lines
7.1 KiB
HTML

<!DOCTYPE html>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=815833
-->
<head>
<title>Test for Bug 815833 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=815833">Mozilla Bug 815833</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
"use strict";
if (SpecialPowers.isMainProcess()) {
SpecialPowers.Cu.import("resource://gre/modules/ContactService.jsm");
SpecialPowers.Cu.import("resource://gre/modules/PermissionPromptHelper.jsm");
}
SpecialPowers.addPermission("contacts-write", true, document);
SpecialPowers.addPermission("contacts-read", true, document);
SpecialPowers.addPermission("contacts-create", true, document);
function onFailure() {
ok(false, "in on Failure!");
}
var number1 = {
local: "7932012345",
international: "+557932012345"
};
var number2 = {
local: "7932012346",
international: "+557932012346"
};
var properties1 = {
name: "Testname1",
tel: [{type: ["work"], value: number1.local, carrier: "testCarrier"} , {type: ["home", "fax"], value: number2.local}],
};
var shortNumber = "888";
var properties2 = {
name: "Testname2",
tel: [{type: ["work"], value: shortNumber, carrier: "testCarrier"}]
};
var req;
var index = 0;
var createResult1;
var findResult1;
var sample_id1;
var mozContacts = window.navigator.mozContacts;
var steps = [
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 contact1");
createResult1 = new mozContact();
createResult1.init(properties1);
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, "Adding a new contact2");
var createResult2 = new mozContact();
createResult2.init(properties2);
req = navigator.mozContacts.save(createResult2);
req.onsuccess = function () {
ok(createResult2.id, "The contact now has an ID.");
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Searching for local number");
var options = {filterBy: ["tel"],
filterOp: "startsWith",
filterValue: number1.local};
req = mozContacts.find(options);
req.onsuccess = function () {
ok(req.result.length == 1, "Found exactly 1 contact.");
findResult1 = req.result[0];
ok(findResult1.id == sample_id1, "Same ID");
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Searching for international number");
var options = {filterBy: ["tel"],
filterOp: "startsWith",
filterValue: number1.international};
req = mozContacts.find(options);
req.onsuccess = function () {
ok(req.result.length == 0, "Found exactly 0 contacts.");
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Searching for a short number matching the prefix");
var shortNumber = number1.local.substring(0, 3);
var options = {filterBy: ["tel"],
filterOp: "equals",
filterValue: shortNumber};
req = mozContacts.find(options);
req.onsuccess = function() {
ok(req.result.length == 0, "The prefix short number should not match any contact.");
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Searching for a short number matching the suffix");
var shortNumber = number1.local.substring(number1.local.length - 3);
var options = {filterBy: ["tel"],
filterOp: "equals",
filterValue: shortNumber};
req = mozContacts.find(options);
req.onsuccess = function() {
ok(req.result.length == 0, "The suffix short number should not match any contact.");
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Searching for a short number matching a contact");
var options = {filterBy: ["tel"],
filterOp: "equals",
filterValue: shortNumber};
req = mozContacts.find(options);
req.onsuccess = function() {
ok(req.result.length == 1, "Found the contact equally matching the shortNumber.");
next();
};
req.onerror = onFailure;
},
function() {
ok(true, "Modifying number");
findResult1.tel[0].value = number2.local;
req = mozContacts.save(findResult1);
req.onsuccess = function () {
next();
};
},
function () {
ok(true, "Searching for local number");
var options = {filterBy: ["tel"],
filterOp: "startsWith",
filterValue: number1.local};
req = mozContacts.find(options);
req.onsuccess = function () {
ok(req.result.length == 0, "Found exactly 0 contact.");
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Searching for local number");
var options = {filterBy: ["tel"],
filterOp: "startsWith",
filterValue: number1.international};
req = mozContacts.find(options);
req.onsuccess = function () {
ok(req.result.length == 0, "Found exactly 0 contact.");
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Searching for local number");
var options = {filterBy: ["tel"],
filterOp: "startsWith",
filterValue: number2.local};
req = mozContacts.find(options);
req.onsuccess = function () {
ok(req.result.length == 1, "Found exactly 1 contact.");
findResult1 = req.result[0];
ok(findResult1.id == sample_id1, "Same ID");
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Searching for local number");
var options = {filterBy: ["tel"],
filterOp: "startsWith",
filterValue: number2.international};
req = mozContacts.find(options);
req.onsuccess = function () {
ok(req.result.length == 0, "Found exactly 1 contact.");
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, "all done!\n");
SimpleTest.finish();
}
];
function next() {
ok(true, "Begin!");
if (index >= steps.length) {
ok(false, "Shouldn't get here!");
return;
}
try {
steps[index]();
} catch(ex) {
ok(false, "Caught exception", ex);
}
index += 1;
}
SimpleTest.waitForExplicitFinish();
addLoadEvent(next);
</script>
</pre>
</body>
</html>