gecko-dev/dom/contacts/tests/test_contacts_blobs.html

223 строки
5.5 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 utils = SpecialPowers.getDOMWindowUtils(window);
function getView(size)
{
var buffer = new ArrayBuffer(size);
var view = new Uint8Array(buffer);
is(buffer.byteLength, size, "Correct byte length");
return view;
}
function getRandomView(size)
{
var view = getView(size);
for (var i = 0; i < size; i++) {
view[i] = parseInt(Math.random() * 255)
}
return view;
}
function getRandomBlob(size)
{
return new Blob([getRandomView(size)], { type: "binary/random" });
}
function compareBuffers(buffer1, buffer2)
{
if (buffer1.byteLength != buffer2.byteLength) {
return false;
}
var view1 = new Uint8Array(buffer1);
var view2 = new Uint8Array(buffer2);
for (var i = 0; i < buffer1.byteLength; i++) {
if (view1[i] != view2[i]) {
return false;
}
}
return true;
}
function verifyBuffers(buffer1, buffer2, isLast)
{
ok(compareBuffers(buffer1, buffer2), "Correct blob data");
if (isLast)
next();
}
var randomBlob = getRandomBlob(1024);
var randomBlob2 = getRandomBlob(1024);
var properties1 = {
name: ["xTestname1"],
givenName: ["xTestname1"],
photo: [randomBlob]
};
var properties2 = {
name: ["yTestname2"],
givenName: ["yTestname2"],
photo: [randomBlob, randomBlob2]
};
var sample_id1;
var createResult1;
var findResult1;
function verifyBlob(blob1, blob2, isLast)
{
is(blob1 instanceof Blob, true,
"blob1 is an instance of DOMBlob");
is(blob2 instanceof Blob, true,
"blob2 is an instance of DOMBlob");
isnot(blob1 instanceof File, true,
"blob1 is an instance of File");
isnot(blob2 instanceof File, true,
"blob2 is an instance of File");
ise(blob1.size, blob2.size, "Same size");
ise(blob1.type, blob2.type, "Same type");
var buffer1;
var buffer2;
var reader1 = new FileReader();
reader1.readAsArrayBuffer(blob2);
reader1.onload = function(event) {
buffer2 = event.target.result;
if (buffer1) {
verifyBuffers(buffer1, buffer2, isLast);
}
}
var reader2 = new FileReader();
reader2.readAsArrayBuffer(blob1);
reader2.onload = function(event) {
buffer1 = event.target.result;
if (buffer2) {
verifyBuffers(buffer1, buffer2, isLast);
}
}
}
function verifyBlobArray(blobs1, blobs2)
{
is(blobs1 instanceof Array, true, "blobs1 is an array object");
is(blobs2 instanceof Array, true, "blobs2 is an array object");
ise(blobs1.length, blobs2.length, "Same length");
if (!blobs1.length) {
next();
return;
}
for (var i = 0; i < blobs1.length; i++) {
verifyBlob(blobs1[i], blobs2[i], i == blobs1.length - 1);
}
}
var req;
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 contact with photo");
createResult1 = new mozContact(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, "Retrieving by substring");
var options = {filterBy: ["givenName"],
filterOp: "startsWith",
filterValue: properties1.givenName[0].substring(0,3)};
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");
verifyBlobArray(findResult1.photo, properties1.photo);
};
req.onerror = onFailure;
},
function () {
ok(true, "Adding contact with 2 photos");
createResult1 = new mozContact(properties2);
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, "Retrieving by substring");
var options = {filterBy: ["givenName"],
filterOp: "startsWith",
filterValue: properties2.givenName[0].substring(0,3)};
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");
verifyBlobArray(findResult1.photo, properties2.photo);
};
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();
}
];
start_tests();
</script>
</pre>
</body>
</html>