file.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /**
  2. * Any copyright is dedicated to the Public Domain.
  3. * http://creativecommons.org/publicdomain/zero/1.0/
  4. */
  5. var bufferCache = [];
  6. var utils = SpecialPowers.getDOMWindowUtils(window);
  7. function getBuffer(size)
  8. {
  9. let buffer = new ArrayBuffer(size);
  10. is(buffer.byteLength, size, "Correct byte length");
  11. return buffer;
  12. }
  13. function getRandomBuffer(size)
  14. {
  15. let buffer = getBuffer(size);
  16. let view = new Uint8Array(buffer);
  17. for (let i = 0; i < size; i++) {
  18. view[i] = parseInt(Math.random() * 255)
  19. }
  20. return buffer;
  21. }
  22. function getView(size)
  23. {
  24. let buffer = new ArrayBuffer(size);
  25. let view = new Uint8Array(buffer);
  26. is(buffer.byteLength, size, "Correct byte length");
  27. return view;
  28. }
  29. function getRandomView(size)
  30. {
  31. let view = getView(size);
  32. for (let i = 0; i < size; i++) {
  33. view[i] = parseInt(Math.random() * 255)
  34. }
  35. return view;
  36. }
  37. function compareBuffers(buffer1, buffer2)
  38. {
  39. if (buffer1.byteLength != buffer2.byteLength) {
  40. return false;
  41. }
  42. let view1 = buffer1 instanceof Uint8Array ? buffer1 : new Uint8Array(buffer1);
  43. let view2 = buffer2 instanceof Uint8Array ? buffer2 : new Uint8Array(buffer2);
  44. for (let i = 0; i < buffer1.byteLength; i++) {
  45. if (view1[i] != view2[i]) {
  46. return false;
  47. }
  48. }
  49. return true;
  50. }
  51. function getBlob(type, view)
  52. {
  53. return new Blob([view], {type: type});
  54. }
  55. function getFile(name, type, view)
  56. {
  57. return new File([view], name, {type: type});
  58. }
  59. function getRandomBlob(size)
  60. {
  61. return getBlob("binary/random", getRandomView(size));
  62. }
  63. function getRandomFile(name, size)
  64. {
  65. return getFile(name, "binary/random", getRandomView(size));
  66. }
  67. function getNullBlob(size)
  68. {
  69. return getBlob("binary/null", getView(size));
  70. }
  71. function getNullFile(name, size)
  72. {
  73. return getFile(name, "binary/null", getView(size));
  74. }
  75. // This needs to be async to make it available on workers too.
  76. function getWasmBinary(text)
  77. {
  78. let binary = getWasmBinarySync(text);
  79. SimpleTest.executeSoon(function() {
  80. testGenerator.send(binary);
  81. });
  82. }
  83. function getWasmModule(binary)
  84. {
  85. let module = new WebAssembly.Module(binary);
  86. return module;
  87. }
  88. function verifyBuffers(buffer1, buffer2)
  89. {
  90. ok(compareBuffers(buffer1, buffer2), "Correct buffer data");
  91. }
  92. function verifyBlob(blob1, blob2, fileId, blobReadHandler)
  93. {
  94. is(blob1 instanceof Components.interfaces.nsIDOMBlob, true,
  95. "Instance of nsIDOMBlob");
  96. is(blob1 instanceof File, blob2 instanceof File,
  97. "Instance of DOM File");
  98. is(blob1.size, blob2.size, "Correct size");
  99. is(blob1.type, blob2.type, "Correct type");
  100. if (blob2 instanceof File) {
  101. is(blob1.name, blob2.name, "Correct name");
  102. }
  103. is(utils.getFileId(blob1), fileId, "Correct file id");
  104. let buffer1;
  105. let buffer2;
  106. for (let i = 0; i < bufferCache.length; i++) {
  107. if (bufferCache[i].blob == blob2) {
  108. buffer2 = bufferCache[i].buffer;
  109. break;
  110. }
  111. }
  112. if (!buffer2) {
  113. let reader = new FileReader();
  114. reader.readAsArrayBuffer(blob2);
  115. reader.onload = function(event) {
  116. buffer2 = event.target.result;
  117. bufferCache.push({ blob: blob2, buffer: buffer2 });
  118. if (buffer1) {
  119. verifyBuffers(buffer1, buffer2);
  120. if (blobReadHandler) {
  121. blobReadHandler();
  122. }
  123. else {
  124. testGenerator.next();
  125. }
  126. }
  127. }
  128. }
  129. let reader = new FileReader();
  130. reader.readAsArrayBuffer(blob1);
  131. reader.onload = function(event) {
  132. buffer1 = event.target.result;
  133. if (buffer2) {
  134. verifyBuffers(buffer1, buffer2);
  135. if (blobReadHandler) {
  136. blobReadHandler();
  137. }
  138. else {
  139. testGenerator.next();
  140. }
  141. }
  142. }
  143. }
  144. function verifyBlobArray(blobs1, blobs2, expectedFileIds)
  145. {
  146. is(blobs1 instanceof Array, true, "Got an array object");
  147. is(blobs1.length, blobs2.length, "Correct length");
  148. if (!blobs1.length) {
  149. return;
  150. }
  151. let verifiedCount = 0;
  152. function blobReadHandler() {
  153. if (++verifiedCount == blobs1.length) {
  154. testGenerator.next();
  155. }
  156. else {
  157. verifyBlob(blobs1[verifiedCount], blobs2[verifiedCount],
  158. expectedFileIds[verifiedCount], blobReadHandler);
  159. }
  160. }
  161. verifyBlob(blobs1[verifiedCount], blobs2[verifiedCount],
  162. expectedFileIds[verifiedCount], blobReadHandler);
  163. }
  164. function verifyMutableFile(mutableFile1, file2)
  165. {
  166. ok(mutableFile1 instanceof IDBMutableFile, "Instance of IDBMutableFile");
  167. is(mutableFile1.name, file2.name, "Correct name");
  168. is(mutableFile1.type, file2.type, "Correct type");
  169. continueToNextStep();
  170. }
  171. function verifyView(view1, view2)
  172. {
  173. is(view1.byteLength, view2.byteLength, "Correct byteLength");
  174. verifyBuffers(view1, view2);
  175. continueToNextStep();
  176. }
  177. function verifyWasmModule(module1, module2)
  178. {
  179. let getGlobalForObject = SpecialPowers.Cu.getGlobalForObject;
  180. let testingFunctions = SpecialPowers.Cu.getJSTestingFunctions();
  181. let wasmExtractCode = SpecialPowers.unwrap(testingFunctions.wasmExtractCode);
  182. let exp1 = wasmExtractCode(module1);
  183. let exp2 = wasmExtractCode(module2);
  184. let code1 = exp1.code;
  185. let code2 = exp2.code;
  186. ok(code1 instanceof getGlobalForObject(code1).Uint8Array, "Instance of Uint8Array");
  187. ok(code2 instanceof getGlobalForObject(code1).Uint8Array, "Instance of Uint8Array");
  188. ok(code1.length == code2.length, "Correct length");
  189. verifyBuffers(code1, code2);
  190. continueToNextStep();
  191. }
  192. function grabFileUsageAndContinueHandler(request)
  193. {
  194. testGenerator.send(request.result.fileUsage);
  195. }
  196. function getCurrentUsage(usageHandler)
  197. {
  198. let qms = SpecialPowers.Services.qms;
  199. let principal = SpecialPowers.wrap(document).nodePrincipal;
  200. let cb = SpecialPowers.wrapCallback(usageHandler);
  201. qms.getUsageForPrincipal(principal, cb);
  202. }
  203. function getFileId(file)
  204. {
  205. return utils.getFileId(file);
  206. }
  207. function getFilePath(file)
  208. {
  209. return utils.getFilePath(file);
  210. }
  211. function hasFileInfo(name, id)
  212. {
  213. return utils.getFileReferences(name, id);
  214. }
  215. function getFileRefCount(name, id)
  216. {
  217. let count = {};
  218. utils.getFileReferences(name, id, null, count);
  219. return count.value;
  220. }
  221. function getFileDBRefCount(name, id)
  222. {
  223. let count = {};
  224. utils.getFileReferences(name, id, null, {}, count);
  225. return count.value;
  226. }
  227. function flushPendingFileDeletions()
  228. {
  229. utils.flushPendingFileDeletions();
  230. }