blob_test.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. goog.provide('goog.testing.fs.BlobTest');
  15. goog.setTestOnly('goog.testing.fs.BlobTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.testing.fs.Blob');
  18. goog.require('goog.testing.jsunit');
  19. var hasArrayBuffer = goog.isDef(goog.global.ArrayBuffer);
  20. function testInput() {
  21. var blob = new goog.testing.fs.Blob();
  22. assertEquals('', blob.toString());
  23. assertEquals(0, blob.size);
  24. // input is a string
  25. blob = new goog.testing.fs.Blob('資');
  26. assertEquals('資', blob.toString());
  27. assertEquals(3, blob.size);
  28. if (!hasArrayBuffer) {
  29. return;
  30. }
  31. // input is an Array of Arraybuffer.
  32. // decimal code for e8 b3 87, that's 資 in UTF-8
  33. blob = new goog.testing.fs.Blob([new Uint8Array([232, 179, 135])]);
  34. assertEquals('資', blob.toString());
  35. assertEquals(3, blob.size);
  36. // input is an Array of Arraybuffer with control characters.
  37. var data = goog.dom.getWindow().atob('iVBORw0KGgo=');
  38. var arr = [];
  39. for (var i = 0; i < data.length; i++) {
  40. arr.push(data.charCodeAt(i));
  41. }
  42. blob = new goog.testing.fs.Blob([new Uint8Array(arr)]);
  43. assertArrayEquals([137, 80, 78, 71, 13, 10, 26, 10], blob.data_);
  44. assertEquals(8, blob.size);
  45. // input is an Array of strings
  46. blob = new goog.testing.fs.Blob(['資', 'é']);
  47. assertEquals('資é', blob.toString());
  48. assertEquals(5, blob.size);
  49. // input is an Array of Arraybuffer + string
  50. blob = new goog.testing.fs.Blob([new Uint8Array([232, 179, 135]), 'é']);
  51. assertEquals('資é', blob.toString());
  52. assertEquals(5, blob.size);
  53. }
  54. function testType() {
  55. var blob = new goog.testing.fs.Blob();
  56. assertEquals('', blob.type);
  57. blob = new goog.testing.fs.Blob('foo bar baz', 'text/plain');
  58. assertEquals('text/plain', blob.type);
  59. }
  60. function testSlice() {
  61. var blob = new goog.testing.fs.Blob('abcdef');
  62. assertEquals('bc', blob.slice(1, 3).toString());
  63. assertEquals('def', blob.slice(3, 10).toString());
  64. assertEquals('abcd', blob.slice(0, -2).toString());
  65. assertEquals('', blob.slice(10, 1).toString());
  66. assertEquals('', blob.slice(10, 30).toString());
  67. assertEquals('b', blob.slice(-5, 2).toString());
  68. assertEquals('abcdef', blob.slice().toString());
  69. assertEquals('abc', blob.slice(/* opt_start */ undefined, 3).toString());
  70. assertEquals('def', blob.slice(3).toString());
  71. assertEquals('text/plain', blob.slice(1, 2, 'text/plain').type);
  72. blob = new goog.testing.fs.Blob('ab資cd');
  73. assertEquals('ab資', blob.slice(0, 5).toString()); // 資 is 3-bytes long.
  74. assertEquals('資', blob.slice(2, 5).toString());
  75. assertEquals('資cd', blob.slice(2, 10).toString());
  76. assertEquals('ab', blob.slice(0, -5).toString());
  77. assertEquals('c', blob.slice(-2, -1).toString());
  78. assertEquals('資c', blob.slice(-5, -1).toString());
  79. assertEquals('ab資cd', blob.slice().toString());
  80. assertEquals('ab資', blob.slice(/* opt_start */ undefined, 5).toString());
  81. assertEquals('cd', blob.slice(5).toString());
  82. assertArrayEquals([232], blob.slice(2, 3).data_); // first byte of 資.
  83. }
  84. function testToArrayBuffer() {
  85. if (!hasArrayBuffer) {
  86. return;
  87. }
  88. var blob = new goog.testing.fs.Blob('資');
  89. var buf = new ArrayBuffer(this.size);
  90. var arr = new Uint8Array(buf);
  91. arr = [232, 179, 135];
  92. assertElementsEquals(buf, blob.toArrayBuffer());
  93. }
  94. function testToDataUrl() {
  95. var blob = new goog.testing.fs.Blob('資', 'text');
  96. assertEquals('data:text;base64,6LOH', blob.toDataUrl());
  97. }