dom/contacts/tests/test_contacts_blobs.html

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 <!DOCTYPE html>
michael@0 2 <html>
michael@0 3 <!--
michael@0 4 https://bugzilla.mozilla.org/show_bug.cgi?id=674720
michael@0 5 -->
michael@0 6 <head>
michael@0 7 <title>Test for Bug 674720 WebContacts</title>
michael@0 8 <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
michael@0 9 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
michael@0 10 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
michael@0 11 </head>
michael@0 12 <body>
michael@0 13
michael@0 14 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=674720">Mozilla Bug 674720</a>
michael@0 15 <p id="display"></p>
michael@0 16 <div id="content" style="display: none">
michael@0 17
michael@0 18 </div>
michael@0 19 <pre id="test">
michael@0 20 <script type="text/javascript;version=1.8" src="http://mochi.test:8888/tests/dom/contacts/tests/shared.js"></script>
michael@0 21 <script class="testbody" type="text/javascript">
michael@0 22 "use strict";
michael@0 23
michael@0 24 var utils = SpecialPowers.getDOMWindowUtils(window);
michael@0 25
michael@0 26 function getView(size)
michael@0 27 {
michael@0 28 var buffer = new ArrayBuffer(size);
michael@0 29 var view = new Uint8Array(buffer);
michael@0 30 is(buffer.byteLength, size, "Correct byte length");
michael@0 31 return view;
michael@0 32 }
michael@0 33
michael@0 34 function getRandomView(size)
michael@0 35 {
michael@0 36 var view = getView(size);
michael@0 37 for (var i = 0; i < size; i++) {
michael@0 38 view[i] = parseInt(Math.random() * 255)
michael@0 39 }
michael@0 40 return view;
michael@0 41 }
michael@0 42
michael@0 43 function getBlob(type, view)
michael@0 44 {
michael@0 45 return SpecialPowers.unwrap(utils.getBlob([view], {type: type}));
michael@0 46 }
michael@0 47
michael@0 48 function getRandomBlob(size)
michael@0 49 {
michael@0 50 return getBlob("binary/random", getRandomView(size));
michael@0 51 }
michael@0 52
michael@0 53 function compareBuffers(buffer1, buffer2)
michael@0 54 {
michael@0 55 if (buffer1.byteLength != buffer2.byteLength) {
michael@0 56 return false;
michael@0 57 }
michael@0 58 var view1 = new Uint8Array(buffer1);
michael@0 59 var view2 = new Uint8Array(buffer2);
michael@0 60 for (var i = 0; i < buffer1.byteLength; i++) {
michael@0 61 if (view1[i] != view2[i]) {
michael@0 62 return false;
michael@0 63 }
michael@0 64 }
michael@0 65 return true;
michael@0 66 }
michael@0 67
michael@0 68 function verifyBuffers(buffer1, buffer2, isLast)
michael@0 69 {
michael@0 70 ok(compareBuffers(buffer1, buffer2), "Correct blob data");
michael@0 71 if (isLast)
michael@0 72 next();
michael@0 73 }
michael@0 74
michael@0 75 var randomBlob = getRandomBlob(1024);
michael@0 76 var randomBlob2 = getRandomBlob(1024);
michael@0 77
michael@0 78 var properties1 = {
michael@0 79 name: ["xTestname1"],
michael@0 80 givenName: ["xTestname1"],
michael@0 81 photo: [randomBlob]
michael@0 82 };
michael@0 83
michael@0 84 var properties2 = {
michael@0 85 name: ["yTestname2"],
michael@0 86 givenName: ["yTestname2"],
michael@0 87 photo: [randomBlob, randomBlob2]
michael@0 88 };
michael@0 89
michael@0 90 var sample_id1;
michael@0 91 var createResult1;
michael@0 92 var findResult1;
michael@0 93
michael@0 94 function verifyBlob(blob1, blob2, isLast)
michael@0 95 {
michael@0 96 is(blob1 instanceof SpecialPowers.Ci.nsIDOMBlob, true,
michael@0 97 "blob1 is an instance of nsIDOMBlob");
michael@0 98 is(blob2 instanceof SpecialPowers.Ci.nsIDOMBlob, true,
michael@0 99 "blob2 is an instance of nsIDOMBlob");
michael@0 100 isnot(blob1 instanceof SpecialPowers.Ci.nsIDOMFile, true,
michael@0 101 "blob1 is an instance of nsIDOMFile");
michael@0 102 isnot(blob2 instanceof SpecialPowers.Ci.nsIDOMFile, true,
michael@0 103 "blob2 is an instance of nsIDOMFile");
michael@0 104 ise(blob1.size, blob2.size, "Same size");
michael@0 105 ise(blob1.type, blob2.type, "Same type");
michael@0 106
michael@0 107 var buffer1;
michael@0 108 var buffer2;
michael@0 109
michael@0 110 var reader1 = new FileReader();
michael@0 111 reader1.readAsArrayBuffer(blob2);
michael@0 112 reader1.onload = function(event) {
michael@0 113 buffer2 = event.target.result;
michael@0 114 if (buffer1) {
michael@0 115 verifyBuffers(buffer1, buffer2, isLast);
michael@0 116 }
michael@0 117 }
michael@0 118
michael@0 119 var reader2 = new FileReader();
michael@0 120 reader2.readAsArrayBuffer(blob1);
michael@0 121 reader2.onload = function(event) {
michael@0 122 buffer1 = event.target.result;
michael@0 123 if (buffer2) {
michael@0 124 verifyBuffers(buffer1, buffer2, isLast);
michael@0 125 }
michael@0 126 }
michael@0 127 }
michael@0 128
michael@0 129 function verifyBlobArray(blobs1, blobs2)
michael@0 130 {
michael@0 131 is(blobs1 instanceof Array, true, "blobs1 is an array object");
michael@0 132 is(blobs2 instanceof Array, true, "blobs2 is an array object");
michael@0 133 ise(blobs1.length, blobs2.length, "Same length");
michael@0 134
michael@0 135 if (!blobs1.length) {
michael@0 136 next();
michael@0 137 return;
michael@0 138 }
michael@0 139
michael@0 140 for (var i = 0; i < blobs1.length; i++) {
michael@0 141 verifyBlob(blobs1[i], blobs2[i], i == blobs1.length - 1);
michael@0 142 }
michael@0 143 }
michael@0 144
michael@0 145 var req;
michael@0 146
michael@0 147 var steps = [
michael@0 148 function () {
michael@0 149 ok(true, "Deleting database");
michael@0 150 req = mozContacts.clear();
michael@0 151 req.onsuccess = function () {
michael@0 152 ok(true, "Deleted the database");
michael@0 153 next();
michael@0 154 };
michael@0 155 req.onerror = onFailure;
michael@0 156 },
michael@0 157 function () {
michael@0 158 ok(true, "Adding contact with photo");
michael@0 159 createResult1 = new mozContact(properties1);
michael@0 160 req = navigator.mozContacts.save(createResult1);
michael@0 161 req.onsuccess = function () {
michael@0 162 ok(createResult1.id, "The contact now has an ID.");
michael@0 163 sample_id1 = createResult1.id;
michael@0 164 next();
michael@0 165 };
michael@0 166 req.onerror = onFailure;
michael@0 167 },
michael@0 168 function () {
michael@0 169 ok(true, "Retrieving by substring");
michael@0 170 var options = {filterBy: ["givenName"],
michael@0 171 filterOp: "startsWith",
michael@0 172 filterValue: properties1.givenName[0].substring(0,3)};
michael@0 173 req = mozContacts.find(options);
michael@0 174 req.onsuccess = function () {
michael@0 175 ok(req.result.length == 1, "Found exactly 1 contact.");
michael@0 176 findResult1 = req.result[0];
michael@0 177 ok(findResult1.id == sample_id1, "Same ID");
michael@0 178 verifyBlobArray(findResult1.photo, properties1.photo);
michael@0 179 };
michael@0 180 req.onerror = onFailure;
michael@0 181 },
michael@0 182 function () {
michael@0 183 ok(true, "Adding contact with 2 photos");
michael@0 184 createResult1 = new mozContact(properties2);
michael@0 185 req = navigator.mozContacts.save(createResult1);
michael@0 186 req.onsuccess = function () {
michael@0 187 ok(createResult1.id, "The contact now has an ID.");
michael@0 188 sample_id1 = createResult1.id;
michael@0 189 next();
michael@0 190 };
michael@0 191 req.onerror = onFailure;
michael@0 192 },
michael@0 193 function () {
michael@0 194 ok(true, "Retrieving by substring");
michael@0 195 var options = {filterBy: ["givenName"],
michael@0 196 filterOp: "startsWith",
michael@0 197 filterValue: properties2.givenName[0].substring(0,3)};
michael@0 198 req = mozContacts.find(options);
michael@0 199 req.onsuccess = function () {
michael@0 200 ok(req.result.length == 1, "Found exactly 1 contact.");
michael@0 201 findResult1 = req.result[0];
michael@0 202 ok(findResult1.id == sample_id1, "Same ID");
michael@0 203 verifyBlobArray(findResult1.photo, properties2.photo);
michael@0 204 };
michael@0 205 req.onerror = onFailure;
michael@0 206 },
michael@0 207 function () {
michael@0 208 ok(true, "Deleting database");
michael@0 209 req = mozContacts.clear()
michael@0 210 req.onsuccess = function () {
michael@0 211 ok(true, "Deleted the database");
michael@0 212 next();
michael@0 213 }
michael@0 214 req.onerror = onFailure;
michael@0 215 },
michael@0 216 function () {
michael@0 217 ok(true, "all done!\n");
michael@0 218
michael@0 219 SimpleTest.finish();
michael@0 220 }
michael@0 221 ];
michael@0 222
michael@0 223 start_tests();
michael@0 224 </script>
michael@0 225 </pre>
michael@0 226 </body>
michael@0 227 </html>

mercurial