| 1 |
// Copyright 2011 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.testing.fs.BlobTest'); |
| 16 |
goog.setTestOnly('goog.testing.fs.BlobTest'); |
| 17 |
|
| 18 |
goog.require('goog.dom'); |
| 19 |
goog.require('goog.testing.fs.Blob'); |
| 20 |
goog.require('goog.testing.jsunit'); |
| 21 |
|
| 22 |
var hasArrayBuffer = goog.isDef(goog.global.ArrayBuffer); |
| 23 |
|
| 24 |
function testInput() { |
| 25 |
var blob = new goog.testing.fs.Blob(); |
| 26 |
assertEquals('', blob.toString()); |
| 27 |
assertEquals(0, blob.size); |
| 28 |
|
| 29 |
// input is a string |
| 30 |
blob = new goog.testing.fs.Blob('資'); |
| 31 |
assertEquals('資', blob.toString()); |
| 32 |
assertEquals(3, blob.size); |
| 33 |
|
| 34 |
if (!hasArrayBuffer) { |
| 35 |
return; |
| 36 |
} |
| 37 |
|
| 38 |
// input is an Array of Arraybuffer. |
| 39 |
// decimal code for e8 b3 87, that's 資 in UTF-8 |
| 40 |
blob = new goog.testing.fs.Blob([new Uint8Array([232, 179, 135])]); |
| 41 |
assertEquals('資', blob.toString()); |
| 42 |
assertEquals(3, blob.size); |
| 43 |
|
| 44 |
// input is an Array of Arraybuffer with control characters. |
| 45 |
var data = goog.dom.getWindow().atob('iVBORw0KGgo='); |
| 46 |
var arr = []; |
| 47 |
for (var i = 0; i < data.length; i++) { |
| 48 |
arr.push(data.charCodeAt(i)); |
| 49 |
} |
| 50 |
blob = new goog.testing.fs.Blob([new Uint8Array(arr)]); |
| 51 |
assertArrayEquals([137, 80, 78, 71, 13, 10, 26, 10], blob.data_); |
| 52 |
assertEquals(8, blob.size); |
| 53 |
|
| 54 |
// input is an Array of strings |
| 55 |
blob = new goog.testing.fs.Blob(['資', 'é']); |
| 56 |
assertEquals('資é', blob.toString()); |
| 57 |
assertEquals(5, blob.size); |
| 58 |
|
| 59 |
// input is an Array of Arraybuffer + string |
| 60 |
blob = new goog.testing.fs.Blob([new Uint8Array([232, 179, 135]), 'é']); |
| 61 |
assertEquals('資é', blob.toString()); |
| 62 |
assertEquals(5, blob.size); |
| 63 |
} |
| 64 |
|
| 65 |
function testType() { |
| 66 |
var blob = new goog.testing.fs.Blob(); |
| 67 |
assertEquals('', blob.type); |
| 68 |
|
| 69 |
blob = new goog.testing.fs.Blob('foo bar baz', 'text/plain'); |
| 70 |
assertEquals('text/plain', blob.type); |
| 71 |
} |
| 72 |
|
| 73 |
function testSlice() { |
| 74 |
var blob = new goog.testing.fs.Blob('abcdef'); |
| 75 |
assertEquals('bc', blob.slice(1, 3).toString()); |
| 76 |
assertEquals('def', blob.slice(3, 10).toString()); |
| 77 |
assertEquals('abcd', blob.slice(0, -2).toString()); |
| 78 |
assertEquals('', blob.slice(10, 1).toString()); |
| 79 |
assertEquals('', blob.slice(10, 30).toString()); |
| 80 |
assertEquals('b', blob.slice(-5, 2).toString()); |
| 81 |
|
| 82 |
assertEquals('abcdef', blob.slice().toString()); |
| 83 |
assertEquals('abc', blob.slice(/* opt_start */ undefined, 3).toString()); |
| 84 |
assertEquals('def', blob.slice(3).toString()); |
| 85 |
|
| 86 |
assertEquals('text/plain', blob.slice(1, 2, 'text/plain').type); |
| 87 |
|
| 88 |
blob = new goog.testing.fs.Blob('ab資cd'); |
| 89 |
assertEquals('ab資', blob.slice(0, 5).toString()); // 資 is 3-bytes long. |
| 90 |
assertEquals('資', blob.slice(2, 5).toString()); |
| 91 |
assertEquals('資cd', blob.slice(2, 10).toString()); |
| 92 |
assertEquals('ab', blob.slice(0, -5).toString()); |
| 93 |
assertEquals('c', blob.slice(-2, -1).toString()); |
| 94 |
assertEquals('資c', blob.slice(-5, -1).toString()); |
| 95 |
|
| 96 |
assertEquals('ab資cd', blob.slice().toString()); |
| 97 |
assertEquals('ab資', blob.slice(/* opt_start */ undefined, 5).toString()); |
| 98 |
assertEquals('cd', blob.slice(5).toString()); |
| 99 |
assertArrayEquals([232], blob.slice(2, 3).data_); // first byte of 資. |
| 100 |
} |
| 101 |
|
| 102 |
function testToArrayBuffer() { |
| 103 |
if (!hasArrayBuffer) { |
| 104 |
return; |
| 105 |
} |
| 106 |
var blob = new goog.testing.fs.Blob('資'); |
| 107 |
var buf = new ArrayBuffer(this.size); |
| 108 |
var arr = new Uint8Array(buf); |
| 109 |
arr = [232, 179, 135]; |
| 110 |
assertElementsEquals(buf, blob.toArrayBuffer()); |
| 111 |
} |
| 112 |
|
| 113 |
function testToDataUrl() { |
| 114 |
var blob = new goog.testing.fs.Blob('資', 'text'); |
| 115 |
assertEquals('data:text;base64,6LOH', blob.toDataUrl()); |
| 116 |
} |