1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/contacts/tests/test_contacts_blobs.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,227 @@ 1.4 +<!DOCTYPE html> 1.5 +<html> 1.6 +<!-- 1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=674720 1.8 +--> 1.9 +<head> 1.10 + <title>Test for Bug 674720 WebContacts</title> 1.11 + <script type="text/javascript" src="/MochiKit/MochiKit.js"></script> 1.12 + <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> 1.13 + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 1.14 +</head> 1.15 +<body> 1.16 + 1.17 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=674720">Mozilla Bug 674720</a> 1.18 +<p id="display"></p> 1.19 +<div id="content" style="display: none"> 1.20 + 1.21 +</div> 1.22 +<pre id="test"> 1.23 +<script type="text/javascript;version=1.8" src="http://mochi.test:8888/tests/dom/contacts/tests/shared.js"></script> 1.24 +<script class="testbody" type="text/javascript"> 1.25 +"use strict"; 1.26 + 1.27 +var utils = SpecialPowers.getDOMWindowUtils(window); 1.28 + 1.29 +function getView(size) 1.30 +{ 1.31 + var buffer = new ArrayBuffer(size); 1.32 + var view = new Uint8Array(buffer); 1.33 + is(buffer.byteLength, size, "Correct byte length"); 1.34 + return view; 1.35 +} 1.36 + 1.37 +function getRandomView(size) 1.38 +{ 1.39 + var view = getView(size); 1.40 + for (var i = 0; i < size; i++) { 1.41 + view[i] = parseInt(Math.random() * 255) 1.42 + } 1.43 + return view; 1.44 +} 1.45 + 1.46 +function getBlob(type, view) 1.47 +{ 1.48 + return SpecialPowers.unwrap(utils.getBlob([view], {type: type})); 1.49 +} 1.50 + 1.51 +function getRandomBlob(size) 1.52 +{ 1.53 + return getBlob("binary/random", getRandomView(size)); 1.54 +} 1.55 + 1.56 +function compareBuffers(buffer1, buffer2) 1.57 +{ 1.58 + if (buffer1.byteLength != buffer2.byteLength) { 1.59 + return false; 1.60 + } 1.61 + var view1 = new Uint8Array(buffer1); 1.62 + var view2 = new Uint8Array(buffer2); 1.63 + for (var i = 0; i < buffer1.byteLength; i++) { 1.64 + if (view1[i] != view2[i]) { 1.65 + return false; 1.66 + } 1.67 + } 1.68 + return true; 1.69 +} 1.70 + 1.71 +function verifyBuffers(buffer1, buffer2, isLast) 1.72 +{ 1.73 + ok(compareBuffers(buffer1, buffer2), "Correct blob data"); 1.74 + if (isLast) 1.75 + next(); 1.76 +} 1.77 + 1.78 +var randomBlob = getRandomBlob(1024); 1.79 +var randomBlob2 = getRandomBlob(1024); 1.80 + 1.81 +var properties1 = { 1.82 + name: ["xTestname1"], 1.83 + givenName: ["xTestname1"], 1.84 + photo: [randomBlob] 1.85 +}; 1.86 + 1.87 +var properties2 = { 1.88 + name: ["yTestname2"], 1.89 + givenName: ["yTestname2"], 1.90 + photo: [randomBlob, randomBlob2] 1.91 +}; 1.92 + 1.93 +var sample_id1; 1.94 +var createResult1; 1.95 +var findResult1; 1.96 + 1.97 +function verifyBlob(blob1, blob2, isLast) 1.98 +{ 1.99 + is(blob1 instanceof SpecialPowers.Ci.nsIDOMBlob, true, 1.100 + "blob1 is an instance of nsIDOMBlob"); 1.101 + is(blob2 instanceof SpecialPowers.Ci.nsIDOMBlob, true, 1.102 + "blob2 is an instance of nsIDOMBlob"); 1.103 + isnot(blob1 instanceof SpecialPowers.Ci.nsIDOMFile, true, 1.104 + "blob1 is an instance of nsIDOMFile"); 1.105 + isnot(blob2 instanceof SpecialPowers.Ci.nsIDOMFile, true, 1.106 + "blob2 is an instance of nsIDOMFile"); 1.107 + ise(blob1.size, blob2.size, "Same size"); 1.108 + ise(blob1.type, blob2.type, "Same type"); 1.109 + 1.110 + var buffer1; 1.111 + var buffer2; 1.112 + 1.113 + var reader1 = new FileReader(); 1.114 + reader1.readAsArrayBuffer(blob2); 1.115 + reader1.onload = function(event) { 1.116 + buffer2 = event.target.result; 1.117 + if (buffer1) { 1.118 + verifyBuffers(buffer1, buffer2, isLast); 1.119 + } 1.120 + } 1.121 + 1.122 + var reader2 = new FileReader(); 1.123 + reader2.readAsArrayBuffer(blob1); 1.124 + reader2.onload = function(event) { 1.125 + buffer1 = event.target.result; 1.126 + if (buffer2) { 1.127 + verifyBuffers(buffer1, buffer2, isLast); 1.128 + } 1.129 + } 1.130 +} 1.131 + 1.132 +function verifyBlobArray(blobs1, blobs2) 1.133 +{ 1.134 + is(blobs1 instanceof Array, true, "blobs1 is an array object"); 1.135 + is(blobs2 instanceof Array, true, "blobs2 is an array object"); 1.136 + ise(blobs1.length, blobs2.length, "Same length"); 1.137 + 1.138 + if (!blobs1.length) { 1.139 + next(); 1.140 + return; 1.141 + } 1.142 + 1.143 + for (var i = 0; i < blobs1.length; i++) { 1.144 + verifyBlob(blobs1[i], blobs2[i], i == blobs1.length - 1); 1.145 + } 1.146 +} 1.147 + 1.148 +var req; 1.149 + 1.150 +var steps = [ 1.151 + function () { 1.152 + ok(true, "Deleting database"); 1.153 + req = mozContacts.clear(); 1.154 + req.onsuccess = function () { 1.155 + ok(true, "Deleted the database"); 1.156 + next(); 1.157 + }; 1.158 + req.onerror = onFailure; 1.159 + }, 1.160 + function () { 1.161 + ok(true, "Adding contact with photo"); 1.162 + createResult1 = new mozContact(properties1); 1.163 + req = navigator.mozContacts.save(createResult1); 1.164 + req.onsuccess = function () { 1.165 + ok(createResult1.id, "The contact now has an ID."); 1.166 + sample_id1 = createResult1.id; 1.167 + next(); 1.168 + }; 1.169 + req.onerror = onFailure; 1.170 + }, 1.171 + function () { 1.172 + ok(true, "Retrieving by substring"); 1.173 + var options = {filterBy: ["givenName"], 1.174 + filterOp: "startsWith", 1.175 + filterValue: properties1.givenName[0].substring(0,3)}; 1.176 + req = mozContacts.find(options); 1.177 + req.onsuccess = function () { 1.178 + ok(req.result.length == 1, "Found exactly 1 contact."); 1.179 + findResult1 = req.result[0]; 1.180 + ok(findResult1.id == sample_id1, "Same ID"); 1.181 + verifyBlobArray(findResult1.photo, properties1.photo); 1.182 + }; 1.183 + req.onerror = onFailure; 1.184 + }, 1.185 + function () { 1.186 + ok(true, "Adding contact with 2 photos"); 1.187 + createResult1 = new mozContact(properties2); 1.188 + req = navigator.mozContacts.save(createResult1); 1.189 + req.onsuccess = function () { 1.190 + ok(createResult1.id, "The contact now has an ID."); 1.191 + sample_id1 = createResult1.id; 1.192 + next(); 1.193 + }; 1.194 + req.onerror = onFailure; 1.195 + }, 1.196 + function () { 1.197 + ok(true, "Retrieving by substring"); 1.198 + var options = {filterBy: ["givenName"], 1.199 + filterOp: "startsWith", 1.200 + filterValue: properties2.givenName[0].substring(0,3)}; 1.201 + req = mozContacts.find(options); 1.202 + req.onsuccess = function () { 1.203 + ok(req.result.length == 1, "Found exactly 1 contact."); 1.204 + findResult1 = req.result[0]; 1.205 + ok(findResult1.id == sample_id1, "Same ID"); 1.206 + verifyBlobArray(findResult1.photo, properties2.photo); 1.207 + }; 1.208 + req.onerror = onFailure; 1.209 + }, 1.210 + function () { 1.211 + ok(true, "Deleting database"); 1.212 + req = mozContacts.clear() 1.213 + req.onsuccess = function () { 1.214 + ok(true, "Deleted the database"); 1.215 + next(); 1.216 + } 1.217 + req.onerror = onFailure; 1.218 + }, 1.219 + function () { 1.220 + ok(true, "all done!\n"); 1.221 + 1.222 + SimpleTest.finish(); 1.223 + } 1.224 +]; 1.225 + 1.226 +start_tests(); 1.227 +</script> 1.228 +</pre> 1.229 +</body> 1.230 +</html>