test_indexes_funny_things.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. // Blob constructor is not implemented outside of windows yet (Bug 827723).
  9. if (!this.window) {
  10. finishTest();
  11. yield undefined;
  12. }
  13. const name = this.window ? window.location.pathname : "Splendid Test";
  14. const objectStoreName = "Things";
  15. const blob1 = new Blob(["foo", "bar"], { type: "text/plain" });
  16. const blob2 = new Blob(["foobazybar"], { type: "text/plain" });
  17. const blob3 = new Blob(["2"], { type: "bogus/" });
  18. const str = "The Book of Mozilla";
  19. str.type = blob1;
  20. const arr = [1, 2, 3, 4, 5];
  21. const objectStoreData = [
  22. { key: "1", value: blob1},
  23. { key: "2", value: blob2},
  24. { key: "3", value: blob3},
  25. { key: "4", value: str},
  26. { key: "5", value: arr},
  27. ];
  28. const indexData = [
  29. { name: "type", keyPath: "type", options: { } },
  30. { name: "length", keyPath: "length", options: { unique: true } }
  31. ];
  32. const objectStoreDataTypeSort = [
  33. { key: "3", value: blob3},
  34. { key: "1", value: blob1},
  35. { key: "2", value: blob2},
  36. ];
  37. const objectStoreDataLengthSort = [
  38. { key: "5", value: arr},
  39. { key: "4", value: str},
  40. ];
  41. let request = indexedDB.open(name, 1);
  42. request.onerror = errorHandler;
  43. request.onupgradeneeded = grabEventAndContinueHandler;
  44. request.onsuccess = grabEventAndContinueHandler;
  45. let event = yield undefined;
  46. let db = event.target.result;
  47. let objectStore = db.createObjectStore(objectStoreName, { keyPath: null });
  48. // First, add all our data to the object store.
  49. let addedData = 0;
  50. for (let i in objectStoreData) {
  51. request = objectStore.add(objectStoreData[i].value,
  52. objectStoreData[i].key);
  53. request.onerror = errorHandler;
  54. request.onsuccess = function(event) {
  55. if (++addedData == objectStoreData.length) {
  56. testGenerator.send(event);
  57. }
  58. }
  59. }
  60. event = yield undefined;
  61. // Now create the indexes.
  62. for (let i in indexData) {
  63. objectStore.createIndex(indexData[i].name, indexData[i].keyPath,
  64. indexData[i].options);
  65. }
  66. is(objectStore.indexNames.length, indexData.length, "Good index count");
  67. yield undefined;
  68. objectStore = db.transaction(objectStoreName)
  69. .objectStore(objectStoreName);
  70. // Check global properties to make sure they are correct.
  71. is(objectStore.indexNames.length, indexData.length, "Good index count");
  72. for (let i in indexData) {
  73. let found = false;
  74. for (let j = 0; j < objectStore.indexNames.length; j++) {
  75. if (objectStore.indexNames.item(j) == indexData[i].name) {
  76. found = true;
  77. break;
  78. }
  79. }
  80. is(found, true, "objectStore has our index");
  81. let index = objectStore.index(indexData[i].name);
  82. is(index.name, indexData[i].name, "Correct name");
  83. is(index.objectStore.name, objectStore.name, "Correct store name");
  84. is(index.keyPath, indexData[i].keyPath, "Correct keyPath");
  85. is(index.unique, indexData[i].options.unique ? true : false,
  86. "Correct unique value");
  87. }
  88. ok(true, "Test group 1");
  89. let keyIndex = 0;
  90. request = objectStore.index("type").openKeyCursor();
  91. request.onerror = errorHandler;
  92. request.onsuccess = function (event) {
  93. let cursor = event.target.result;
  94. if (cursor) {
  95. is(cursor.key, objectStoreDataTypeSort[keyIndex].value.type,
  96. "Correct key");
  97. is(cursor.primaryKey, objectStoreDataTypeSort[keyIndex].key,
  98. "Correct primary key");
  99. ok(!("value" in cursor), "No value");
  100. cursor.continue();
  101. is(cursor.key, objectStoreDataTypeSort[keyIndex].value.type,
  102. "Correct key");
  103. is(cursor.primaryKey, objectStoreDataTypeSort[keyIndex].key,
  104. "Correct value");
  105. ok(!("value" in cursor), "No value");
  106. keyIndex++;
  107. }
  108. else {
  109. testGenerator.next();
  110. }
  111. }
  112. yield undefined;
  113. is(keyIndex, objectStoreDataTypeSort.length, "Saw all the expected keys");
  114. ok(true, "Test group 2");
  115. keyIndex = 0;
  116. request = objectStore.index("length").openKeyCursor(null, "next");
  117. request.onerror = errorHandler;
  118. request.onsuccess = function (event) {
  119. let cursor = event.target.result;
  120. if (cursor) {
  121. is(cursor.key, objectStoreDataLengthSort[keyIndex].value.length,
  122. "Correct key");
  123. is(cursor.primaryKey, objectStoreDataLengthSort[keyIndex].key,
  124. "Correct value");
  125. cursor.continue();
  126. is(cursor.key, objectStoreDataLengthSort[keyIndex].value.length,
  127. "Correct key");
  128. is(cursor.primaryKey, objectStoreDataLengthSort[keyIndex].key,
  129. "Correct value");
  130. keyIndex++;
  131. }
  132. else {
  133. testGenerator.next();
  134. }
  135. }
  136. yield undefined;
  137. is(keyIndex, objectStoreDataLengthSort.length, "Saw all the expected keys");
  138. finishTest();
  139. yield undefined;
  140. }