test_wasm_index_getAllObjects.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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: { name: "foo1", data: 42 } },
  13. { key: 2, value: { name: "foo2", data: [null, null, null] } },
  14. { key: 3, value: { name: "foo3", data: [null, null, null, null, null] } }
  15. ];
  16. const indexData = { name: "nameIndex", keyPath: "name", options: { } };
  17. if (!isWasmSupported()) {
  18. finishTest();
  19. yield undefined;
  20. }
  21. getWasmBinary('(module (func (result i32) (i32.const 1)))');
  22. let binary = yield undefined;
  23. wasmData[1].value.data[0] = getWasmModule(binary);
  24. getWasmBinary('(module (func (result i32) (i32.const 2)))');
  25. binary = yield undefined;
  26. wasmData[1].value.data[1] = getWasmModule(binary);
  27. getWasmBinary('(module (func (result i32) (i32.const 3)))');
  28. binary = yield undefined;
  29. wasmData[1].value.data[2] = getWasmModule(binary);
  30. getWasmBinary('(module (func (result i32) (i32.const 4)))');
  31. binary = yield undefined;
  32. wasmData[2].value.data[0] = getWasmModule(binary);
  33. getWasmBinary('(module (func (result i32) (i32.const 5)))');
  34. binary = yield undefined;
  35. wasmData[2].value.data[1] = getWasmModule(binary);
  36. getWasmBinary('(module (func (result i32) (i32.const 6)))');
  37. binary = yield undefined;
  38. wasmData[2].value.data[2] = getWasmModule(binary);
  39. getWasmBinary('(module (func (result i32) (i32.const 7)))');
  40. binary = yield undefined;
  41. wasmData[2].value.data[3] = getWasmModule(binary);
  42. getWasmBinary('(module (func (result i32) (i32.const 8)))');
  43. binary = yield undefined;
  44. wasmData[2].value.data[4] = getWasmModule(binary);
  45. let request = indexedDB.open(name, 1);
  46. request.onerror = errorHandler;
  47. request.onupgradeneeded = continueToNextStepSync;
  48. request.onsuccess = unexpectedSuccessHandler;
  49. yield undefined;
  50. // upgradeneeded
  51. request.onupgradeneeded = unexpectedSuccessHandler;
  52. request.onsuccess = continueToNextStepSync;
  53. info("Creating objectStore");
  54. let objectStore = request.result.createObjectStore(objectStoreName);
  55. info("Creating index");
  56. objectStore.createIndex(indexData.name, indexData.keyPath, indexData.options);
  57. yield undefined;
  58. // success
  59. let db = request.result;
  60. db.onerror = errorHandler;
  61. info("Storing values");
  62. objectStore = db.transaction([objectStoreName], "readwrite")
  63. .objectStore(objectStoreName);
  64. let addedCount = 0;
  65. for (let i in wasmData) {
  66. request = objectStore.add(wasmData[i].value, wasmData[i].key);
  67. request.onsuccess = function(event) {
  68. if (++addedCount == wasmData.length) {
  69. continueToNextStep();
  70. }
  71. }
  72. }
  73. yield undefined;
  74. info("Getting values");
  75. request = db.transaction(objectStoreName)
  76. .objectStore(objectStoreName)
  77. .index("nameIndex")
  78. .getAll();
  79. request.addEventListener("error", new ExpectError("UnknownError", true));
  80. request.onsuccess = unexpectedSuccessHandler;
  81. yield undefined;
  82. finishTest();
  83. yield undefined;
  84. }