test_schema21upgrade.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 testName = "schema21upgrade";
  9. const testKeys = [
  10. -1/0,
  11. -1.7e308,
  12. -10000,
  13. -2,
  14. -1.5,
  15. -1,
  16. -1.00001e-200,
  17. -1e-200,
  18. 0,
  19. 1e-200,
  20. 1.00001e-200,
  21. 1,
  22. 2,
  23. 10000,
  24. 1.7e308,
  25. 1/0,
  26. new Date("1750-01-02"),
  27. new Date("1800-12-31T12:34:56.001Z"),
  28. new Date(-1000),
  29. new Date(-10),
  30. new Date(-1),
  31. new Date(0),
  32. new Date(1),
  33. new Date(2),
  34. new Date(1000),
  35. new Date("1971-01-01"),
  36. new Date("1971-01-01T01:01:01Z"),
  37. new Date("1971-01-01T01:01:01.001Z"),
  38. new Date("1971-01-01T01:01:01.01Z"),
  39. new Date("1971-01-01T01:01:01.1Z"),
  40. new Date("1980-02-02"),
  41. new Date("3333-03-19T03:33:33.333Z"),
  42. "",
  43. "\x00",
  44. "\x00\x00",
  45. "\x00\x01",
  46. "\x01",
  47. "\x02",
  48. "\x03",
  49. "\x04",
  50. "\x07",
  51. "\x08",
  52. "\x0F",
  53. "\x10",
  54. "\x1F",
  55. "\x20",
  56. "01234",
  57. "\x3F",
  58. "\x40",
  59. "A",
  60. "A\x00",
  61. "A1",
  62. "ZZZZ",
  63. "a",
  64. "a\x00",
  65. "aa",
  66. "azz",
  67. "}",
  68. "\x7E",
  69. "\x7F",
  70. "\x80",
  71. "\xFF",
  72. "\u0100",
  73. "\u01FF",
  74. "\u0200",
  75. "\u03FF",
  76. "\u0400",
  77. "\u07FF",
  78. "\u0800",
  79. "\u0FFF",
  80. "\u1000",
  81. "\u1FFF",
  82. "\u2000",
  83. "\u3FFF",
  84. "\u4000",
  85. "\u7FFF",
  86. "\u8000",
  87. "\uD800",
  88. "\uD800a",
  89. "\uD800\uDC01",
  90. "\uDBFF",
  91. "\uDC00",
  92. "\uDFFF\uD800",
  93. "\uFFFE",
  94. "\uFFFF",
  95. "\uFFFF\x00",
  96. "\uFFFFZZZ",
  97. [],
  98. [-1/0],
  99. [-1],
  100. [0],
  101. [1],
  102. [1, "a"],
  103. [1, []],
  104. [1, [""]],
  105. [2, 3],
  106. [2, 3.0000000000001],
  107. [12, [[]]],
  108. [12, [[[]]]],
  109. [12, [[[""]]]],
  110. [12, [[["foo"]]]],
  111. [12, [[[[[3]]]]]],
  112. [12, [[[[[[3]]]]]]],
  113. [12, [[[[[[3],[[[[[4.2]]]]]]]]]]],
  114. [new Date(-1)],
  115. [new Date(1)],
  116. [""],
  117. ["", [[]]],
  118. ["", [[[]]]],
  119. ["abc"],
  120. ["abc", "def"],
  121. ["abc\x00"],
  122. ["abc\x00", "\x00\x01"],
  123. ["abc\x00", "\x00def"],
  124. ["abc\x00\x00def"],
  125. ["x", [[]]],
  126. ["x", [[[]]]],
  127. [[]],
  128. [[],"foo"],
  129. [[],[]],
  130. [[[]]],
  131. [[[]], []],
  132. [[[]], [[]]],
  133. [[[]], [[1]]],
  134. [[[]], [[[]]]],
  135. [[[1]]],
  136. [[[[]], []]],
  137. ];
  138. const testString =
  139. "abcdefghijklmnopqrstuvwxyz0123456789`~!@#$%^&*()-_+=,<.>/?\\|";
  140. clearAllDatabases(continueToNextStepSync);
  141. yield undefined;
  142. info("Installing profile");
  143. installPackagedProfile(testName + "_profile");
  144. info("Opening database with no version");
  145. let request = indexedDB.open(testName);
  146. request.onerror = errorHandler;
  147. request.onupgradeneeded = unexpectedSuccessHandler;
  148. request.onsuccess = grabEventAndContinueHandler;
  149. let event = yield undefined;
  150. let db = event.target.result;
  151. is(db.version, 1, "Correct db version");
  152. let transaction = db.transaction(testName);
  153. transaction.oncomplete = grabEventAndContinueHandler;
  154. let objectStore = transaction.objectStore(testName);
  155. let index = objectStore.index("uniqueIndex");
  156. info("Starting 'uniqueIndex' cursor");
  157. let keyIndex = 0;
  158. index.openCursor().onsuccess = event => {
  159. let cursor = event.target.result;
  160. if (cursor) {
  161. info("Comparing " + JSON.stringify(cursor.primaryKey) + " to " +
  162. JSON.stringify(testKeys[cursor.key]) +
  163. " [" + cursor.key + "]");
  164. is(indexedDB.cmp(cursor.primaryKey, testKeys[cursor.key]), 0,
  165. "Keys compare equally via 'indexedDB.cmp'");
  166. is(compareKeys(cursor.primaryKey, testKeys[cursor.key]), true,
  167. "Keys compare equally via 'compareKeys'");
  168. let indexProperty = cursor.value.index;
  169. is(Array.isArray(indexProperty), true, "index property is Array");
  170. is(indexProperty[0], cursor.key, "index property first item correct");
  171. is(indexProperty[1], cursor.key + 1, "index property second item correct");
  172. is(cursor.key, keyIndex, "Cursor key property is correct");
  173. is(cursor.value.testString, testString, "Test string compared equally");
  174. keyIndex++;
  175. cursor.continue();
  176. }
  177. };
  178. yield undefined;
  179. is(keyIndex, testKeys.length, "Saw all keys");
  180. transaction = db.transaction(testName, "readwrite");
  181. transaction.oncomplete = grabEventAndContinueHandler;
  182. objectStore = transaction.objectStore(testName);
  183. index = objectStore.index("index");
  184. info("Getting all 'index' keys");
  185. index.getAllKeys().onsuccess = grabEventAndContinueHandler;
  186. event = yield undefined;
  187. is(event.target.result.length, testKeys.length * 2, "Got all keys");
  188. info("Starting objectStore cursor");
  189. objectStore.openCursor().onsuccess = event => {
  190. let cursor = event.target.result;
  191. if (cursor) {
  192. let value = cursor.value;
  193. is(value.testString, testString, "Test string compared equally");
  194. delete value.index;
  195. cursor.update(value);
  196. cursor.continue();
  197. } else {
  198. continueToNextStepSync();
  199. }
  200. };
  201. yield undefined;
  202. info("Getting all 'index' keys");
  203. index.getAllKeys().onsuccess = grabEventAndContinueHandler;
  204. event = yield undefined;
  205. is(event.target.result.length, 0, "Removed all keys");
  206. yield undefined;
  207. db.close();
  208. info("Opening database with new version");
  209. request = indexedDB.open(testName, 2);
  210. request.onerror = errorHandler;
  211. request.onupgradeneeded = grabEventAndContinueHandler;
  212. request.onsuccess = grabEventAndContinueHandler;
  213. event = yield undefined;
  214. info("Deleting indexes");
  215. objectStore = event.target.transaction.objectStore(testName);
  216. objectStore.deleteIndex("index");
  217. objectStore.deleteIndex("uniqueIndex");
  218. event = yield undefined;
  219. db = event.target.result;
  220. transaction = db.transaction(testName, "readwrite");
  221. transaction.oncomplete = grabEventAndContinueHandler;
  222. info("Starting objectStore cursor");
  223. objectStore = transaction.objectStore(testName);
  224. objectStore.openCursor().onsuccess = event => {
  225. let cursor = event.target.result;
  226. if (cursor) {
  227. let value = cursor.value;
  228. is(value.testString, testString, "Test string compared equally");
  229. value.index = value.keyPath;
  230. cursor.update(value);
  231. cursor.continue();
  232. }
  233. };
  234. event = yield undefined;
  235. db.close();
  236. info("Opening database with new version");
  237. request = indexedDB.open(testName, 3);
  238. request.onerror = errorHandler;
  239. request.onupgradeneeded = grabEventAndContinueHandler;
  240. request.onsuccess = grabEventAndContinueHandler;
  241. event = yield undefined;
  242. info("Creating indexes");
  243. objectStore = event.target.transaction.objectStore(testName);
  244. objectStore.createIndex("index", "index");
  245. event = yield undefined;
  246. db = event.target.result;
  247. transaction = db.transaction(testName);
  248. transaction.oncomplete = grabEventAndContinueHandler;
  249. objectStore = transaction.objectStore(testName);
  250. index = objectStore.index("index");
  251. info("Starting 'index' cursor");
  252. keyIndex = 0;
  253. index.openCursor().onsuccess = event => {
  254. let cursor = event.target.result;
  255. if (cursor) {
  256. is(indexedDB.cmp(cursor.primaryKey, testKeys[keyIndex]), 0,
  257. "Keys compare equally via 'indexedDB.cmp'");
  258. is(compareKeys(cursor.primaryKey, testKeys[keyIndex]), true,
  259. "Keys compare equally via 'compareKeys'");
  260. is(indexedDB.cmp(cursor.key, testKeys[keyIndex]), 0,
  261. "Keys compare equally via 'indexedDB.cmp'");
  262. is(compareKeys(cursor.key, testKeys[keyIndex]), true,
  263. "Keys compare equally via 'compareKeys'");
  264. let indexProperty = cursor.value.index;
  265. is(indexedDB.cmp(indexProperty, testKeys[keyIndex]), 0,
  266. "Keys compare equally via 'indexedDB.cmp'");
  267. is(compareKeys(indexProperty, testKeys[keyIndex]), true,
  268. "Keys compare equally via 'compareKeys'");
  269. is(cursor.value.testString, testString, "Test string compared equally");
  270. keyIndex++;
  271. cursor.continue();
  272. }
  273. };
  274. yield undefined;
  275. is(keyIndex, testKeys.length, "Added all keys again");
  276. finishTest();
  277. yield undefined;
  278. }