test_wasm_getAll.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * Any copyright is dedicated to the Public Domain.
  3. * http://creativecommons.org/publicdomain/zero/1.0/
  4. */
  5. var testGenerator = testSteps();
  6. function testSteps()
  7. {
  8. const name =
  9. this.window ? window.location.pathname : "test_wasm_getAll.js";
  10. const objectStoreName = "Wasm";
  11. const wasmData = [
  12. { key: 1, value: 42 },
  13. { key: 2, value: [null, null, null] },
  14. { key: 3, value: [null, null, null, null, null] }
  15. ];
  16. if (!isWasmSupported()) {
  17. finishTest();
  18. yield undefined;
  19. }
  20. getWasmBinary('(module (func (result i32) (i32.const 1)))');
  21. let binary = yield undefined;
  22. wasmData[1].value[0] = getWasmModule(binary);
  23. getWasmBinary('(module (func (result i32) (i32.const 2)))');
  24. binary = yield undefined;
  25. wasmData[1].value[1] = getWasmModule(binary);
  26. getWasmBinary('(module (func (result i32) (i32.const 3)))');
  27. binary = yield undefined;
  28. wasmData[1].value[2] = getWasmModule(binary);
  29. getWasmBinary('(module (func (result i32) (i32.const 4)))');
  30. binary = yield undefined;
  31. wasmData[2].value[0] = getWasmModule(binary);
  32. getWasmBinary('(module (func (result i32) (i32.const 5)))');
  33. binary = yield undefined;
  34. wasmData[2].value[1] = getWasmModule(binary);
  35. getWasmBinary('(module (func (result i32) (i32.const 6)))');
  36. binary = yield undefined;
  37. wasmData[2].value[2] = getWasmModule(binary);
  38. getWasmBinary('(module (func (result i32) (i32.const 7)))');
  39. binary = yield undefined;
  40. wasmData[2].value[3] = getWasmModule(binary);
  41. getWasmBinary('(module (func (result i32) (i32.const 8)))');
  42. binary = yield undefined;
  43. wasmData[2].value[4] = getWasmModule(binary);
  44. let request = indexedDB.open(name, 1);
  45. request.onerror = errorHandler;
  46. request.onupgradeneeded = continueToNextStepSync;
  47. request.onsuccess = unexpectedSuccessHandler;
  48. yield undefined;
  49. // upgradeneeded
  50. request.onupgradeneeded = unexpectedSuccessHandler;
  51. request.onsuccess = continueToNextStepSync;
  52. info("Creating objectStore");
  53. request.result.createObjectStore(objectStoreName);
  54. yield undefined;
  55. // success
  56. let db = request.result;
  57. db.onerror = errorHandler;
  58. info("Storing values");
  59. let objectStore = db.transaction([objectStoreName], "readwrite")
  60. .objectStore(objectStoreName);
  61. let addedCount = 0;
  62. for (let i in wasmData) {
  63. request = objectStore.add(wasmData[i].value, wasmData[i].key);
  64. request.onsuccess = function(event) {
  65. if (++addedCount == wasmData.length) {
  66. continueToNextStep();
  67. }
  68. }
  69. }
  70. yield undefined;
  71. info("Getting values");
  72. request = db.transaction(objectStoreName)
  73. .objectStore(objectStoreName)
  74. .getAll();
  75. request.onsuccess = continueToNextStepSync;
  76. yield undefined;
  77. info("Verifying values");
  78. // Can't call yield inside of the verify function.
  79. let modulesToProcess = [];
  80. function verifyArray(array1, array2) {
  81. is(array1 instanceof Array, true, "Got an array object");
  82. is(array1.length, array2.length, "Same length");
  83. }
  84. function verifyData(data1, data2) {
  85. if (data2 instanceof Array) {
  86. verifyArray(data1, data2);
  87. for (let i in data2) {
  88. verifyData(data1[i], data2[i]);
  89. }
  90. } else if (data2 instanceof WebAssembly.Module) {
  91. modulesToProcess.push({ module1: data1, module2: data2 });
  92. } else {
  93. is(data1, data2, "Same value");
  94. }
  95. }
  96. verifyArray(request.result, wasmData);
  97. for (let i in wasmData) {
  98. verifyData(request.result[i], wasmData[i].value);
  99. }
  100. for (let moduleToProcess of modulesToProcess) {
  101. verifyWasmModule(moduleToProcess.module1, moduleToProcess.module2);
  102. yield undefined;
  103. }
  104. finishTest();
  105. yield undefined;
  106. }