content/base/test/test_blobconstructor.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=721569
michael@0 5 -->
michael@0 6 <head>
michael@0 7 <title>Test for Blob constructor (Bug 721569)</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 <script type="text/javascript" src="fileutils.js"></script>
michael@0 11 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
michael@0 12 </head>
michael@0 13 <body>
michael@0 14 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=721569">Mozilla Bug 721569</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 class="testbody" type="text/javascript;version=1.7">
michael@0 21 "use strict";
michael@0 22 /** Test for Bug 721569 **/
michael@0 23 var blob = Blob();
michael@0 24 ok(blob, "Blob should exist");
michael@0 25
michael@0 26 ok(blob.size !== undefined, "Blob should have a size property");
michael@0 27 ok(blob.type !== undefined, "Blob should have a type property");
michael@0 28 ok(blob.slice, "Blob should have a slice method");
michael@0 29
michael@0 30 blob = Blob([], {type: null});
michael@0 31 ok(blob, "Blob should exist");
michael@0 32 is(blob.type, "null", "Blob type should be stringified");
michael@0 33
michael@0 34 blob = Blob([], {type: undefined});
michael@0 35 ok(blob, "Blob should exist");
michael@0 36 is(blob.type, "", "Blob type should be treated as missing");
michael@0 37
michael@0 38 try {
michael@0 39 blob = Blob([]);
michael@0 40 ok(true, "an empty blobParts argument should not throw");
michael@0 41 } catch(e) {
michael@0 42 ok(false, "NOT REACHED");
michael@0 43 }
michael@0 44
michael@0 45 try {
michael@0 46 blob = Blob(null);
michael@0 47 ok(false, "NOT REACHED");
michael@0 48 } catch(e) {
michael@0 49 ok(true, "a null blobParts member should throw");
michael@0 50 }
michael@0 51
michael@0 52 try {
michael@0 53 blob = Blob([], null);
michael@0 54 ok(true, "a null options member should not throw");
michael@0 55 } catch(e) {
michael@0 56 ok(false, "NOT REACHED");
michael@0 57 }
michael@0 58
michael@0 59 try {
michael@0 60 blob = Blob([], undefined);
michael@0 61 ok(true, "an undefined options member should not throw");
michael@0 62 } catch(e) {
michael@0 63 ok(false, "NOT REACHED");
michael@0 64 }
michael@0 65
michael@0 66 try {
michael@0 67 blob = Blob([], false);
michael@0 68 ok(false, "NOT REACHED");
michael@0 69 } catch(e) {
michael@0 70 ok(true, "a boolean options member should throw");
michael@0 71 }
michael@0 72
michael@0 73 try {
michael@0 74 blob = Blob([], 0);
michael@0 75 ok(false, "NOT REACHED");
michael@0 76 } catch(e) {
michael@0 77 ok(true, "a numeric options member should throw");
michael@0 78 }
michael@0 79
michael@0 80 try {
michael@0 81 blob = Blob([], "");
michael@0 82 ok(false, "NOT REACHED");
michael@0 83 } catch(e) {
michael@0 84 ok(true, "a string options member should throw");
michael@0 85 }
michael@0 86
michael@0 87 /** Test for dictionary initialization order **/
michael@0 88 (function() {
michael@0 89 var o = {};
michael@0 90 var p = {type: "text/plain", endings: "transparent"};
michael@0 91 var called = [];
michael@0 92 function add_to_called(n) {
michael@0 93 called.push(n);
michael@0 94 return p[n];
michael@0 95 }
michael@0 96 ["type", "endings"].forEach(function(n) {
michael@0 97 Object.defineProperty(o, n, { get: add_to_called.bind(null, n) });
michael@0 98 });
michael@0 99 var b = new Blob([], o);
michael@0 100 is(JSON.stringify(called), JSON.stringify(["endings", "type"]), "dictionary members should be get in lexicographical order");
michael@0 101 })();
michael@0 102
michael@0 103 let blob1 = Blob(["squiggle"]);
michael@0 104 ok(blob1 instanceof Blob, "Blob constructor should produce Blobs");
michael@0 105 ok(!(blob1 instanceof File), "Blob constructor should not produce Files");
michael@0 106 is(blob1.type, "", "Blob constructor with no options should return Blob with empty type");
michael@0 107 is(blob1.size, 8, "Blob constructor should return Blob with correct size");
michael@0 108
michael@0 109 let blob2 = Blob(["steak"], {type: "content/type"});
michael@0 110 ok(blob2 instanceof Blob, "Blob constructor should produce Blobs");
michael@0 111 ok(!(blob2 instanceof File), "Blob constructor should not produce Files");
michael@0 112 is(blob2.type, "content/type", "Blob constructor with a type option should return Blob with the type");
michael@0 113 is(blob2.size, 5, "Blob constructor should return Blob with correct size");
michael@0 114
michael@0 115
michael@0 116 let aB = new ArrayBuffer(16);
michael@0 117 var int8View = new Int8Array(aB);
michael@0 118 for (var i = 0; i < 16; i++) {
michael@0 119 int8View[i] = i+65;
michael@0 120 }
michael@0 121
michael@0 122 let testData =
michael@0 123 [
michael@0 124 // Test 3 strings
michael@0 125 [["foo", "bar", "baz"], {},
michael@0 126 [{start: 0, length: 9, contents: "foobarbaz"},
michael@0 127 {start: 0, length: 3, contents: "foo"},
michael@0 128 {start: 3, length:6, contents: "barbaz"},
michael@0 129 {start: 6, length: 3, contents: "baz"},
michael@0 130 {start: 6, length: 6, contents: "baz"},
michael@0 131 {start: 0, length: 9, contents: "foobarbaz"},
michael@0 132 {start: 0, length: 11, contents: "foobarbaz"},
michael@0 133 {start: 10, length: 5, contents: ""}]],
michael@0 134 // Test string, Blob, string
michael@0 135 [["foo", blob1, "baz"], {},
michael@0 136 [{start: 0, length: 3, contents: "foo"},
michael@0 137 {start: 3, length: 8, contents: "squiggle"},
michael@0 138 {start: 2, length: 2, contents: "os"},
michael@0 139 {start: 10, length: 2, contents: "eb"}]],
michael@0 140 // Test blob, string, blob
michael@0 141 [[blob1, "foo", blob1], {},
michael@0 142 [{start: 0, length: 8, contents: "squiggle"},
michael@0 143 {start: 7, length: 2, contents: "ef"},
michael@0 144 {start: 10, length: 2, contents: "os"},
michael@0 145 {start: 1, length: 3, contents: "qui"},
michael@0 146 {start: 12, length: 3, contents: "qui"},
michael@0 147 {start: 40, length: 20, contents: ""}]],
michael@0 148 // Test blobs all the way down
michael@0 149 [[blob2, blob1, blob2], {},
michael@0 150 [{start: 0, length: 5, contents: "steak"},
michael@0 151 {start: 5, length: 8, contents: "squiggle"},
michael@0 152 {start: 13, length: 5, contents: "steak"},
michael@0 153 {start: 1, length: 2, contents: "te"},
michael@0 154 {start: 6, length: 4, contents: "quig"}]],
michael@0 155 // Test an array buffer
michael@0 156 [[aB, blob1, "foo"], {},
michael@0 157 [{start: 0, length: 8, contents: "ABCDEFGH"},
michael@0 158 {start: 8, length:10, contents: "IJKLMNOPsq"},
michael@0 159 {start: 17, length: 3, contents: "qui"},
michael@0 160 {start: 4, length: 8, contents: "EFGHIJKL"}]],
michael@0 161 // Test an ArrayBufferView
michael@0 162 [[int8View, blob1, "foo"], {},
michael@0 163 [{start: 0, length: 8, contents: "ABCDEFGH"},
michael@0 164 {start: 8, length:10, contents: "IJKLMNOPsq"},
michael@0 165 {start: 17, length: 3, contents: "qui"},
michael@0 166 {start: 4, length: 8, contents: "EFGHIJKL"}]],
michael@0 167 // Test a partial ArrayBufferView
michael@0 168 [[new Uint8Array(aB, 3, 5), blob1, "foo"], {},
michael@0 169 [{start: 0, length: 8, contents: "DEFGHsqu"},
michael@0 170 {start: 8, length:10, contents: "igglefoo"},
michael@0 171 {start: 4, length: 8, contents: "Hsquiggl"}]],
michael@0 172 // Test transparent line endings
michael@0 173 [["foo\r\n", "bar\r", "baz\n"], { endings: "transparent" },
michael@0 174 [{start: 0, length: 5, contents: "foo\r\n"},
michael@0 175 {start: 5, length: 4, contents: "bar\r"},
michael@0 176 {start: 9, length: 4, contents: "baz\n"}]],
michael@0 177 // Test transparent line endings when the second argument is omitted
michael@0 178 [["foo\r\n", "bar\r", "baz\n"], undefined,
michael@0 179 [{start: 0, length: 5, contents: "foo\r\n"},
michael@0 180 {start: 5, length: 4, contents: "bar\r"},
michael@0 181 {start: 9, length: 4, contents: "baz\n"}]],
michael@0 182 // Test native line endings
michael@0 183 [["foo\r\n", "bar\r", "baz\n"], { endings: "native" },
michael@0 184 navigator.platform.indexOf("Win") != -1 ?
michael@0 185 [{start: 0, length: 5, contents: "foo\r\n"},
michael@0 186 {start: 5, length: 5, contents: "bar\r\n"},
michael@0 187 {start: 10, length: 5, contents: "baz\r\n"}] :
michael@0 188 [{start: 0, length: 4, contents: "foo\n"},
michael@0 189 {start: 4, length: 4, contents: "bar\n"},
michael@0 190 {start: 8, length: 4, contents: "baz\n"}]],
michael@0 191 // Test type coercion of a number
michael@0 192 [[3, int8View, "foo"], {},
michael@0 193 [{start: 0, length: 8, contents: "3ABCDEFG"},
michael@0 194 {start: 8, length:10, contents: "HIJKLMNOPf"},
michael@0 195 {start: 17, length: 4, contents: "foo"},
michael@0 196 {start: 4, length: 8, contents: "DEFGHIJK"}]]
michael@0 197 ];
michael@0 198
michael@0 199 let testCounter = 0;
michael@0 200
michael@0 201 function doTest(data) {
michael@0 202 testCounter++;
michael@0 203
michael@0 204 var [blobs, options, tests] = data;
michael@0 205
michael@0 206 function runTest(test) {
michael@0 207
michael@0 208 let blob;
michael@0 209 if (options !== undefined) {
michael@0 210 blob = new Blob(blobs, options);
michael@0 211 } else {
michael@0 212 blob = new Blob(blobs);
michael@0 213 }
michael@0 214 ok(blob, "Test " + testCounter + " got blob");
michael@0 215 ok(blob instanceof Blob, "Test " + testCounter + " blob is a Blob");
michael@0 216 ok(!(blob instanceof File), "Test " + testCounter + " blob is not a File");
michael@0 217
michael@0 218 let slice = blob.slice(test.start, test.start + test.length);
michael@0 219 ok(slice, "Test " + testCounter + " got slice");
michael@0 220 ok(slice instanceof Blob, "Test " + testCounter + " slice is a Blob");
michael@0 221 ok(!(slice instanceof File), "Test " + testCounter + " slice is not a File");
michael@0 222 is(slice.size, test.contents.length,
michael@0 223 "Test " + testCounter + " slice is correct size");
michael@0 224
michael@0 225 testFile(slice, test.contents, "Test " + testCounter);
michael@0 226 }
michael@0 227 if (Array.isArray(tests)) {
michael@0 228 tests.forEach(runTest);
michael@0 229 } else {
michael@0 230 try {
michael@0 231 let blob = new Blob(blobs, options);
michael@0 232 ok(false, "NOT REACHED");
michael@0 233 } catch (e) {
michael@0 234 is(e.name, tests, "Blob constructor should throw " + tests);
michael@0 235 }
michael@0 236 }
michael@0 237 SpecialPowers.gc();
michael@0 238 }
michael@0 239
michael@0 240 SimpleTest.waitForExplicitFinish();
michael@0 241 testData.forEach(doTest);
michael@0 242
michael@0 243 </script>
michael@0 244 </pre>
michael@0 245 </body>
michael@0 246 </html>

mercurial