test_key_requirements.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 request = indexedDB.open(name, 1);
  10. request.onerror = errorHandler;
  11. request.onupgradeneeded = grabEventAndContinueHandler;
  12. request.onsuccess = grabEventAndContinueHandler;
  13. let event = yield undefined;
  14. let db = event.target.result;
  15. db.addEventListener("error", function(event) {
  16. event.preventDefault();
  17. }, false);
  18. let objectStore = db.createObjectStore("foo", { autoIncrement: true });
  19. request = objectStore.add({});
  20. request.onerror = errorHandler;
  21. request.onsuccess = grabEventAndContinueHandler;
  22. event = yield undefined;
  23. let key1 = event.target.result;
  24. request = objectStore.put({}, key1);
  25. request.onerror = errorHandler;
  26. request.onsuccess = grabEventAndContinueHandler;
  27. event = yield undefined;
  28. is(event.target.result, key1, "put gave the same key back");
  29. let key2 = 10;
  30. request = objectStore.put({}, key2);
  31. request.onerror = errorHandler;
  32. request.onsuccess = grabEventAndContinueHandler;
  33. event = yield undefined;
  34. is(event.target.result, key2, "put gave the same key back");
  35. key2 = 100;
  36. request = objectStore.add({}, key2);
  37. request.onerror = errorHandler;
  38. request.onsuccess = grabEventAndContinueHandler;
  39. event = yield undefined;
  40. is(event.target.result, key2, "put gave the same key back");
  41. try {
  42. objectStore.put({});
  43. ok(true, "put with no key should not throw with autoIncrement!");
  44. }
  45. catch (e) {
  46. ok(false, "put with no key threw with autoIncrement");
  47. }
  48. try {
  49. objectStore.put({});
  50. ok(true, "put with no key should not throw with autoIncrement!");
  51. }
  52. catch (e) {
  53. ok(false, "put with no key threw with autoIncrement");
  54. }
  55. try {
  56. objectStore.delete();
  57. ok(false, "remove with no key should throw!");
  58. }
  59. catch (e) {
  60. ok(true, "remove with no key threw");
  61. }
  62. objectStore = db.createObjectStore("bar");
  63. try {
  64. objectStore.add({});
  65. ok(false, "add with no key should throw!");
  66. }
  67. catch (e) {
  68. ok(true, "add with no key threw");
  69. }
  70. try {
  71. objectStore.put({});
  72. ok(false, "put with no key should throw!");
  73. }
  74. catch (e) {
  75. ok(true, "put with no key threw");
  76. }
  77. try {
  78. objectStore.put({});
  79. ok(false, "put with no key should throw!");
  80. }
  81. catch (e) {
  82. ok(true, "put with no key threw");
  83. }
  84. try {
  85. objectStore.delete();
  86. ok(false, "remove with no key should throw!");
  87. }
  88. catch (e) {
  89. ok(true, "remove with no key threw");
  90. }
  91. objectStore = db.createObjectStore("baz", { keyPath: "id" });
  92. try {
  93. objectStore.add({});
  94. ok(false, "add with no key should throw!");
  95. }
  96. catch (e) {
  97. ok(true, "add with no key threw");
  98. }
  99. try {
  100. objectStore.add({id:5}, 5);
  101. ok(false, "add with inline key and passed key should throw!");
  102. }
  103. catch (e) {
  104. ok(true, "add with inline key and passed key threw");
  105. }
  106. try {
  107. objectStore.put({});
  108. ok(false, "put with no key should throw!");
  109. }
  110. catch (e) {
  111. ok(true, "put with no key threw");
  112. }
  113. try {
  114. objectStore.put({});
  115. ok(false, "put with no key should throw!");
  116. }
  117. catch (e) {
  118. ok(true, "put with no key threw");
  119. }
  120. try {
  121. objectStore.delete();
  122. ok(false, "remove with no key should throw!");
  123. }
  124. catch (e) {
  125. ok(true, "remove with no key threw");
  126. }
  127. key1 = 10;
  128. request = objectStore.add({id:key1});
  129. request.onerror = errorHandler;
  130. request.onsuccess = grabEventAndContinueHandler;
  131. event = yield undefined;
  132. is(event.target.result, key1, "add gave back the same key");
  133. request = objectStore.put({id:10});
  134. request.onerror = errorHandler;
  135. request.onsuccess = grabEventAndContinueHandler;
  136. event = yield undefined;
  137. is(event.target.result, key1, "put gave back the same key");
  138. request = objectStore.put({id:10});
  139. request.onerror = errorHandler;
  140. request.onsuccess = grabEventAndContinueHandler;
  141. event = yield undefined;
  142. is(event.target.result, key1, "put gave back the same key");
  143. request = objectStore.add({id:10});
  144. request.addEventListener("error", new ExpectError("ConstraintError", true));
  145. request.onsuccess = unexpectedSuccessHandler;
  146. event = yield undefined;
  147. try {
  148. objectStore.add({}, null);
  149. ok(false, "add with null key should throw!");
  150. }
  151. catch (e) {
  152. ok(true, "add with null key threw");
  153. }
  154. try {
  155. objectStore.put({}, null);
  156. ok(false, "put with null key should throw!");
  157. }
  158. catch (e) {
  159. ok(true, "put with null key threw");
  160. }
  161. try {
  162. objectStore.put({}, null);
  163. ok(false, "put with null key should throw!");
  164. }
  165. catch (e) {
  166. ok(true, "put with null key threw");
  167. }
  168. try {
  169. objectStore.delete({}, null);
  170. ok(false, "remove with null key should throw!");
  171. }
  172. catch (e) {
  173. ok(true, "remove with null key threw");
  174. }
  175. objectStore = db.createObjectStore("bazing", { keyPath: "id",
  176. autoIncrement: true });
  177. request = objectStore.add({});
  178. request.onerror = errorHandler;
  179. request.onsuccess = grabEventAndContinueHandler;
  180. event = yield undefined;
  181. key1 = event.target.result;
  182. request = objectStore.put({id:key1});
  183. request.onerror = errorHandler;
  184. request.onsuccess = grabEventAndContinueHandler;
  185. event = yield undefined;
  186. is(event.target.result, key1, "put gave the same key back");
  187. key2 = 10;
  188. request = objectStore.put({id:key2});
  189. request.onerror = errorHandler;
  190. request.onsuccess = grabEventAndContinueHandler;
  191. event = yield undefined;
  192. is(event.target.result, key2, "put gave the same key back");
  193. try {
  194. objectStore.put({});
  195. ok(true, "put with no key should not throw with autoIncrement!");
  196. }
  197. catch (e) {
  198. ok(false, "put with no key threw with autoIncrement");
  199. }
  200. try {
  201. objectStore.put({});
  202. ok(true, "put with no key should not throw with autoIncrement!");
  203. }
  204. catch (e) {
  205. ok(false, "put with no key threw with autoIncrement");
  206. }
  207. try {
  208. objectStore.delete();
  209. ok(false, "remove with no key should throw!");
  210. }
  211. catch (e) {
  212. ok(true, "remove with no key threw");
  213. }
  214. try {
  215. objectStore.add({id:5}, 5);
  216. ok(false, "add with inline key and passed key should throw!");
  217. }
  218. catch (e) {
  219. ok(true, "add with inline key and passed key threw");
  220. }
  221. request = objectStore.delete(key2);
  222. request.onerror = errorHandler;
  223. request.onsuccess = grabEventAndContinueHandler;
  224. event = yield undefined;
  225. // Wait for success
  226. yield undefined;
  227. finishTest();
  228. yield undefined;
  229. }