content/base/test/test_blobconstructor.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/base/test/test_blobconstructor.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,246 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<!--
     1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=721569
     1.8 +-->
     1.9 +<head>
    1.10 +  <title>Test for Blob constructor (Bug 721569)</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 +  <script type="text/javascript" src="fileutils.js"></script>
    1.14 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    1.15 +</head>
    1.16 +<body>
    1.17 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=721569">Mozilla Bug 721569</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 class="testbody" type="text/javascript;version=1.7">
    1.24 +"use strict";
    1.25 +/** Test for Bug 721569 **/
    1.26 +var blob = Blob();
    1.27 +ok(blob, "Blob should exist");
    1.28 +
    1.29 +ok(blob.size !== undefined, "Blob should have a size property");
    1.30 +ok(blob.type !== undefined, "Blob should have a type property");
    1.31 +ok(blob.slice, "Blob should have a slice method");
    1.32 +
    1.33 +blob = Blob([], {type: null});
    1.34 +ok(blob, "Blob should exist");
    1.35 +is(blob.type, "null", "Blob type should be stringified");
    1.36 +
    1.37 +blob = Blob([], {type: undefined});
    1.38 +ok(blob, "Blob should exist");
    1.39 +is(blob.type, "", "Blob type should be treated as missing");
    1.40 +
    1.41 +try {
    1.42 +blob = Blob([]);
    1.43 +ok(true, "an empty blobParts argument should not throw");
    1.44 +} catch(e) {
    1.45 +ok(false, "NOT REACHED");
    1.46 +}
    1.47 +
    1.48 +try {
    1.49 +blob = Blob(null);
    1.50 +ok(false, "NOT REACHED");
    1.51 +} catch(e) {
    1.52 +ok(true, "a null blobParts member should throw");
    1.53 +}
    1.54 +
    1.55 +try {
    1.56 +blob = Blob([], null);
    1.57 +ok(true, "a null options member should not throw");
    1.58 +} catch(e) {
    1.59 +ok(false, "NOT REACHED");
    1.60 +}
    1.61 +
    1.62 +try {
    1.63 +blob = Blob([], undefined);
    1.64 +ok(true, "an undefined options member should not throw");
    1.65 +} catch(e) {
    1.66 +ok(false, "NOT REACHED");
    1.67 +}
    1.68 +
    1.69 +try {
    1.70 +blob = Blob([], false);
    1.71 +ok(false, "NOT REACHED");
    1.72 +} catch(e) {
    1.73 +ok(true, "a boolean options member should throw");
    1.74 +}
    1.75 +
    1.76 +try {
    1.77 +blob = Blob([], 0);
    1.78 +ok(false, "NOT REACHED");
    1.79 +} catch(e) {
    1.80 +ok(true, "a numeric options member should throw");
    1.81 +}
    1.82 +
    1.83 +try {
    1.84 +blob = Blob([], "");
    1.85 +ok(false, "NOT REACHED");
    1.86 +} catch(e) {
    1.87 +ok(true, "a string options member should throw");
    1.88 +}
    1.89 +
    1.90 +/** Test for dictionary initialization order **/
    1.91 +(function() {
    1.92 +  var o = {};
    1.93 +  var p = {type: "text/plain", endings: "transparent"};
    1.94 +  var called = [];
    1.95 +  function add_to_called(n) {
    1.96 +    called.push(n);
    1.97 +    return p[n];
    1.98 +  }
    1.99 +  ["type", "endings"].forEach(function(n) {
   1.100 +    Object.defineProperty(o, n, { get: add_to_called.bind(null, n) });
   1.101 +  });
   1.102 +  var b = new Blob([], o);
   1.103 +  is(JSON.stringify(called), JSON.stringify(["endings", "type"]), "dictionary members should be get in lexicographical order");
   1.104 +})();
   1.105 +
   1.106 +let blob1 = Blob(["squiggle"]);
   1.107 +ok(blob1 instanceof Blob, "Blob constructor should produce Blobs");
   1.108 +ok(!(blob1 instanceof File), "Blob constructor should not produce Files");
   1.109 +is(blob1.type, "", "Blob constructor with no options should return Blob with empty type");
   1.110 +is(blob1.size, 8, "Blob constructor should return Blob with correct size");
   1.111 +
   1.112 +let blob2 = Blob(["steak"], {type: "content/type"});
   1.113 +ok(blob2 instanceof Blob, "Blob constructor should produce Blobs");
   1.114 +ok(!(blob2 instanceof File), "Blob constructor should not produce Files");
   1.115 +is(blob2.type, "content/type", "Blob constructor with a type option should return Blob with the type");
   1.116 +is(blob2.size, 5, "Blob constructor should return Blob with correct size");
   1.117 +
   1.118 +
   1.119 +let aB = new ArrayBuffer(16);
   1.120 +var int8View = new Int8Array(aB);
   1.121 +for (var i = 0; i < 16; i++) {
   1.122 +  int8View[i] = i+65;
   1.123 +}
   1.124 +
   1.125 +let testData = 
   1.126 + [
   1.127 +    // Test 3 strings
   1.128 +    [["foo", "bar", "baz"], {},
   1.129 +                            [{start: 0, length: 9, contents: "foobarbaz"},
   1.130 +                             {start: 0, length: 3, contents: "foo"},
   1.131 +                             {start: 3, length:6, contents:  "barbaz"},
   1.132 +                             {start: 6, length: 3, contents:  "baz"},
   1.133 +                             {start: 6, length: 6, contents: "baz"},
   1.134 +                             {start: 0, length: 9, contents:  "foobarbaz"},
   1.135 +                             {start: 0, length: 11, contents: "foobarbaz"},
   1.136 +                             {start: 10, length: 5, contents: ""}]],
   1.137 +    // Test string, Blob, string
   1.138 +    [["foo", blob1, "baz"], {},
   1.139 +                            [{start: 0, length: 3, contents:  "foo"},
   1.140 +                             {start: 3, length: 8, contents:  "squiggle"},
   1.141 +                             {start: 2, length: 2, contents:  "os"},
   1.142 +                             {start: 10, length: 2, contents: "eb"}]],
   1.143 +    // Test blob, string, blob
   1.144 +    [[blob1, "foo", blob1], {},
   1.145 +                            [{start: 0, length: 8, contents:  "squiggle"},
   1.146 +                             {start: 7, length: 2, contents:  "ef"},
   1.147 +                             {start: 10, length: 2, contents: "os"},
   1.148 +                             {start: 1, length: 3, contents:  "qui"},
   1.149 +                             {start: 12, length: 3, contents: "qui"},
   1.150 +                             {start: 40, length: 20, contents: ""}]],
   1.151 +    // Test blobs all the way down
   1.152 +    [[blob2, blob1, blob2], {},
   1.153 +                            [{start: 0, length: 5, contents:  "steak"},
   1.154 +                             {start: 5, length: 8, contents:  "squiggle"},
   1.155 +                             {start: 13, length: 5, contents: "steak"},
   1.156 +                             {start: 1, length: 2, contents:  "te"},
   1.157 +                             {start: 6, length: 4, contents:  "quig"}]],
   1.158 +    // Test an array buffer
   1.159 +    [[aB, blob1, "foo"], {},
   1.160 +                            [{start: 0, length: 8, contents:  "ABCDEFGH"},
   1.161 +                             {start: 8, length:10, contents:  "IJKLMNOPsq"},
   1.162 +                             {start: 17, length: 3, contents: "qui"},
   1.163 +                             {start: 4, length: 8, contents:  "EFGHIJKL"}]],
   1.164 +    // Test an ArrayBufferView
   1.165 +    [[int8View, blob1, "foo"], {},
   1.166 +                            [{start: 0, length: 8, contents:  "ABCDEFGH"},
   1.167 +                             {start: 8, length:10, contents:  "IJKLMNOPsq"},
   1.168 +                             {start: 17, length: 3, contents: "qui"},
   1.169 +                             {start: 4, length: 8, contents:  "EFGHIJKL"}]],
   1.170 +    // Test a partial ArrayBufferView
   1.171 +    [[new Uint8Array(aB, 3, 5), blob1, "foo"], {},
   1.172 +                            [{start: 0, length: 8, contents:  "DEFGHsqu"},
   1.173 +                             {start: 8, length:10, contents:  "igglefoo"},
   1.174 +                             {start: 4, length: 8, contents:  "Hsquiggl"}]],
   1.175 +    // Test transparent line endings
   1.176 +    [["foo\r\n", "bar\r", "baz\n"], { endings: "transparent" },
   1.177 +                            [{start: 0, length: 5, contents:  "foo\r\n"},
   1.178 +                             {start: 5, length: 4, contents:  "bar\r"},
   1.179 +                             {start: 9, length: 4, contents: "baz\n"}]],
   1.180 +    // Test transparent line endings when the second argument is omitted
   1.181 +    [["foo\r\n", "bar\r", "baz\n"], undefined,
   1.182 +                            [{start: 0, length: 5, contents:  "foo\r\n"},
   1.183 +                             {start: 5, length: 4, contents:  "bar\r"},
   1.184 +                             {start: 9, length: 4, contents: "baz\n"}]],
   1.185 +    // Test native line endings
   1.186 +    [["foo\r\n", "bar\r", "baz\n"], { endings: "native" },
   1.187 +                            navigator.platform.indexOf("Win") != -1 ?
   1.188 +                            [{start: 0, length: 5, contents:  "foo\r\n"},
   1.189 +                             {start: 5, length: 5, contents:  "bar\r\n"},
   1.190 +                             {start: 10, length: 5, contents: "baz\r\n"}] :
   1.191 +                            [{start: 0, length: 4, contents:  "foo\n"},
   1.192 +                             {start: 4, length: 4, contents:  "bar\n"},
   1.193 +                             {start: 8, length: 4, contents: "baz\n"}]],
   1.194 +    // Test type coercion of a number
   1.195 +    [[3, int8View, "foo"], {},
   1.196 +                            [{start: 0, length: 8, contents:  "3ABCDEFG"},
   1.197 +                             {start: 8, length:10, contents:  "HIJKLMNOPf"},
   1.198 +                             {start: 17, length: 4, contents: "foo"},
   1.199 +                             {start: 4, length: 8, contents:  "DEFGHIJK"}]]
   1.200 + ];
   1.201 +
   1.202 +let testCounter = 0;
   1.203 +
   1.204 +function doTest(data) {
   1.205 +  testCounter++;
   1.206 +
   1.207 +  var [blobs, options, tests] = data;
   1.208 +
   1.209 +  function runTest(test) {
   1.210 +
   1.211 +    let blob;
   1.212 +    if (options !== undefined) {
   1.213 +      blob = new Blob(blobs, options);
   1.214 +    } else {
   1.215 +      blob = new Blob(blobs);
   1.216 +    }
   1.217 +    ok(blob, "Test " + testCounter + " got blob");
   1.218 +    ok(blob instanceof Blob, "Test " + testCounter + " blob is a Blob");
   1.219 +    ok(!(blob instanceof File), "Test " + testCounter + " blob is not a File");
   1.220 +
   1.221 +    let slice = blob.slice(test.start, test.start + test.length);
   1.222 +    ok(slice, "Test " + testCounter + " got slice");
   1.223 +    ok(slice instanceof Blob, "Test " + testCounter + " slice is a Blob");
   1.224 +    ok(!(slice instanceof File), "Test " + testCounter + " slice is not a File");
   1.225 +    is(slice.size, test.contents.length,
   1.226 +       "Test " + testCounter + " slice is correct size");
   1.227 +
   1.228 +    testFile(slice, test.contents, "Test " + testCounter);
   1.229 +  }
   1.230 +  if (Array.isArray(tests)) {
   1.231 +    tests.forEach(runTest);
   1.232 +  } else {
   1.233 +    try {
   1.234 +      let blob = new Blob(blobs, options);
   1.235 +      ok(false, "NOT REACHED");
   1.236 +    } catch (e) {
   1.237 +      is(e.name, tests, "Blob constructor should throw " + tests);
   1.238 +    }
   1.239 +  }
   1.240 +  SpecialPowers.gc();
   1.241 +}
   1.242 +
   1.243 +SimpleTest.waitForExplicitFinish();
   1.244 +testData.forEach(doTest);
   1.245 +
   1.246 +</script>
   1.247 +</pre>
   1.248 +</body>
   1.249 +</html>

mercurial