test_add_put.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 = this.window ? window.location.pathname : "Splendid Test";
  9. let openRequest = indexedDB.open(name, 1);
  10. openRequest.onerror = errorHandler;
  11. openRequest.onupgradeneeded = grabEventAndContinueHandler;
  12. openRequest.onsuccess = unexpectedSuccessHandler;
  13. let event = yield undefined;
  14. let db = event.target.result;
  15. let trans = event.target.transaction;
  16. for (let autoincrement of [true, false]) {
  17. for (let keypath of [false, true, "missing", "invalid"]) {
  18. for (let method of ["put", "add"]) {
  19. for (let explicit of [true, false, undefined, "invalid"]) {
  20. for (let existing of [true, false]) {
  21. let speccedNoKey = (keypath == false || keypath == "missing") &&
  22. !explicit;
  23. // We can't do 'existing' checks if we use autogenerated key
  24. if (speccedNoKey && autoincrement && existing) {
  25. continue;
  26. }
  27. // Create store
  28. if (db.objectStoreNames.contains("mystore"))
  29. db.deleteObjectStore("mystore");
  30. let store = db.createObjectStore("mystore",
  31. { autoIncrement: autoincrement,
  32. keyPath: (keypath ? "id" : null) });
  33. test = " for test " + JSON.stringify({ autoincrement: autoincrement,
  34. keypath: keypath,
  35. method: method,
  36. explicit: explicit === undefined ? "undefined" : explicit,
  37. existing: existing });
  38. // Insert "existing" data if needed
  39. if (existing) {
  40. if (keypath)
  41. store.add({ existing: "data", id: 5 }).onsuccess = grabEventAndContinueHandler;
  42. else
  43. store.add({ existing: "data" }, 5).onsuccess = grabEventAndContinueHandler;
  44. let e = yield undefined;
  45. is(e.type, "success", "success inserting existing" + test);
  46. is(e.target.result, 5, "inserted correct key" + test);
  47. }
  48. // Set up value to be inserted
  49. let value = { theObj: true };
  50. if (keypath === true) {
  51. value.id = 5;
  52. }
  53. else if (keypath === "invalid") {
  54. value.id = /x/;
  55. }
  56. // Which arguments are passed to function
  57. args = [value];
  58. if (explicit === true) {
  59. args.push(5);
  60. }
  61. else if (explicit === undefined) {
  62. args.push(undefined);
  63. }
  64. else if (explicit === "invalid") {
  65. args.push(/x/);
  66. }
  67. let expected = expectedResult(method, keypath, explicit, autoincrement, existing);
  68. let valueJSON = JSON.stringify(value);
  69. ok(true, "making call" + test);
  70. // Make function call for throwing functions
  71. if (expected === "throw") {
  72. try {
  73. store[method].apply(store, args);
  74. ok(false, "should have thrown" + test);
  75. }
  76. catch (ex) {
  77. ok(true, "did throw" + test);
  78. ok(ex instanceof DOMException, "Got a DOMException" + test);
  79. is(ex.name, "DataError", "expect a DataError" + test);
  80. is(ex.code, 0, "expect zero" + test);
  81. is(JSON.stringify(value), valueJSON, "call didn't modify value" + test);
  82. }
  83. continue;
  84. }
  85. // Make non-throwing function call
  86. let req = store[method].apply(store, args);
  87. is(JSON.stringify(value), valueJSON, "call didn't modify value" + test);
  88. req.onsuccess = req.onerror = grabEventAndContinueHandler;
  89. let e = yield undefined;
  90. // Figure out what key we used
  91. let key = 5;
  92. if (autoincrement && speccedNoKey) {
  93. key = 1;
  94. }
  95. // Adjust value if expected
  96. if (autoincrement && keypath && speccedNoKey) {
  97. value.id = key;
  98. }
  99. // Check result
  100. if (expected === "error") {
  101. is(e.type, "error", "write should fail" + test);
  102. e.preventDefault();
  103. e.stopPropagation();
  104. continue;
  105. }
  106. is(e.type, "success", "write should succeed" + test);
  107. is(e.target.result, key, "write should return correct key" + test);
  108. store.get(key).onsuccess = grabEventAndContinueHandler;
  109. e = yield undefined;
  110. is(e.type, "success", "read back should succeed" + test);
  111. is(JSON.stringify(e.target.result),
  112. JSON.stringify(value),
  113. "read back should return correct value" + test);
  114. }
  115. }
  116. }
  117. }
  118. }
  119. function expectedResult(method, keypath, explicit, autoincrement, existing) {
  120. if (keypath && explicit)
  121. return "throw";
  122. if (!keypath && !explicit && !autoincrement)
  123. return "throw";
  124. if (keypath == "invalid")
  125. return "throw";
  126. if (keypath == "missing" && !autoincrement)
  127. return "throw";
  128. if (explicit == "invalid")
  129. return "throw";
  130. if (method == "add" && existing)
  131. return "error";
  132. return "success";
  133. }
  134. openRequest.onsuccess = grabEventAndContinueHandler;
  135. yield undefined;
  136. finishTest();
  137. yield undefined;
  138. }