gecko/dom/contacts/tests/test_contacts_international.html
2013-01-09 13:45:48 +01:00

266 lines
7.5 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";
// this shouldn't be necessary when bug 792594 is fixed
if (!SpecialPowers.getBoolPref("dom.mozContacts.enabled")) {
var comp = SpecialPowers.wrap(SpecialPowers.Components);
comp.utils.import("resource://gre/modules/ContactService.jsm");
comp.utils.import("resource://gre/modules/PermissionPromptHelper.jsm");
SpecialPowers.setBoolPref("dom.mozContacts.enabled", true);
}
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: "contains",
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: "contains",
filterValue: number1.international};
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 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: "contains",
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: "contains",
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: "contains",
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: "contains",
filterValue: number2.international};
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, "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;
}
function permissionTest() {
if (gContactsEnabled) {
next();
} else {
is(mozContacts, null, "mozContacts is null when not enabled.");
SimpleTest.finish();
}
}
var gContactsEnabled = SpecialPowers.getBoolPref("dom.mozContacts.enabled");
SimpleTest.waitForExplicitFinish();
addLoadEvent(permissionTest);
ok(true, "test passed");
</script>
</pre>
</body>
</html>