test_blob_simple.html 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <!--
  2. Any copyright is dedicated to the Public Domain.
  3. http://creativecommons.org/publicdomain/zero/1.0/
  4. -->
  5. <html>
  6. <head>
  7. <title>Indexed Database Property Test</title>
  8. <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  9. <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  10. <script type="text/javascript;version=1.7">
  11. function testSteps()
  12. {
  13. info("Setting up test fixtures: create an IndexedDB database and object store.");
  14. let request = indexedDB.open(window.location.pathname, 1);
  15. request.onerror = errorHandler;
  16. request.onupgradeneeded = grabEventAndContinueHandler;
  17. request.onsuccess = unexpectedSuccessHandler;
  18. let event = yield undefined;
  19. let db = event.target.result;
  20. db.onerror = errorHandler;
  21. let objectStore = db.createObjectStore("foo", { autoIncrement: true });
  22. let index = objectStore.createIndex("foo", "index");
  23. request.onsuccess = grabEventAndContinueHandler;
  24. event = yield undefined;
  25. info("Let's create a blob and store it in IndexedDB twice.");
  26. const BLOB_DATA = ["fun ", "times ", "all ", "around!"];
  27. const INDEX_KEY = 5;
  28. let blob = new Blob(BLOB_DATA, { type: "text/plain" });
  29. let data = { blob: blob, index: INDEX_KEY };
  30. objectStore = db.transaction("foo", "readwrite").objectStore("foo");
  31. objectStore.add(data).onsuccess = grabEventAndContinueHandler;
  32. event = yield undefined;
  33. info("Added blob to database once");
  34. let key = event.target.result;
  35. objectStore.add(data).onsuccess = grabEventAndContinueHandler;
  36. event = yield undefined;
  37. info("Added blob to database twice");
  38. info("Let's retrieve the blob again and verify the contents is the same.");
  39. objectStore = db.transaction("foo").objectStore("foo");
  40. objectStore.get(key).onsuccess = grabEventAndContinueHandler;
  41. event = yield undefined;
  42. info("Got blob from database");
  43. let fileReader = new FileReader();
  44. fileReader.onload = grabEventAndContinueHandler;
  45. fileReader.readAsText(event.target.result.blob);
  46. event = yield undefined;
  47. is(event.target.result, BLOB_DATA.join(""), "Correct text");
  48. info("Let's retrieve it again, create an object URL for the blob, load" +
  49. "it via an XMLHttpRequest, and verify the contents is the same.");
  50. objectStore = db.transaction("foo").objectStore("foo");
  51. objectStore.get(key).onsuccess = grabEventAndContinueHandler;
  52. event = yield undefined;
  53. info("Got blob from database");
  54. let blobURL = URL.createObjectURL(event.target.result.blob);
  55. let xhr = new XMLHttpRequest();
  56. xhr.open("GET", blobURL);
  57. xhr.onload = grabEventAndContinueHandler;
  58. xhr.send();
  59. yield undefined;
  60. URL.revokeObjectURL(blobURL);
  61. is(xhr.responseText, BLOB_DATA.join(""), "Correct responseText");
  62. info("Retrieve both blob entries from the database and verify contents.");
  63. objectStore = db.transaction("foo").objectStore("foo");
  64. objectStore.mozGetAll().onsuccess = grabEventAndContinueHandler;
  65. event = yield undefined;
  66. is(event.target.result.length, 2, "Got right number of items");
  67. fileReader = new FileReader();
  68. fileReader.onload = grabEventAndContinueHandler;
  69. fileReader.readAsText(event.target.result[0].blob);
  70. event = yield undefined;
  71. is(event.target.result, BLOB_DATA.join(""), "Correct text");
  72. let cursorResults = [];
  73. objectStore = db.transaction("foo").objectStore("foo");
  74. objectStore.openCursor().onsuccess = function(event) {
  75. let cursor = event.target.result;
  76. if (cursor) {
  77. info("Got item from cursor");
  78. cursorResults.push(cursor.value);
  79. cursor.continue();
  80. }
  81. else {
  82. info("Finished cursor");
  83. continueToNextStep();
  84. }
  85. };
  86. yield undefined;
  87. is(cursorResults.length, 2, "Got right number of items");
  88. fileReader = new FileReader();
  89. fileReader.onload = grabEventAndContinueHandler;
  90. fileReader.readAsText(cursorResults[0].blob);
  91. event = yield undefined;
  92. is(event.target.result, BLOB_DATA.join(""), "Correct text");
  93. info("Retrieve blobs from database via index and verify contents.");
  94. index = db.transaction("foo").objectStore("foo").index("foo");
  95. index.get(INDEX_KEY).onsuccess = grabEventAndContinueHandler;
  96. event = yield undefined;
  97. info("Got blob from database");
  98. fileReader = new FileReader();
  99. fileReader.onload = grabEventAndContinueHandler;
  100. fileReader.readAsText(event.target.result.blob);
  101. event = yield undefined;
  102. is(event.target.result, BLOB_DATA.join(""), "Correct text");
  103. index = db.transaction("foo").objectStore("foo").index("foo");
  104. index.mozGetAll().onsuccess = grabEventAndContinueHandler;
  105. event = yield undefined;
  106. is(event.target.result.length, 2, "Got right number of items");
  107. fileReader = new FileReader();
  108. fileReader.onload = grabEventAndContinueHandler;
  109. fileReader.readAsText(event.target.result[0].blob);
  110. event = yield undefined;
  111. is(event.target.result, BLOB_DATA.join(""), "Correct text");
  112. cursorResults = [];
  113. index = db.transaction("foo").objectStore("foo").index("foo");
  114. index.openCursor().onsuccess = function(event) {
  115. let cursor = event.target.result;
  116. if (cursor) {
  117. info("Got item from cursor");
  118. cursorResults.push(cursor.value);
  119. cursor.continue();
  120. }
  121. else {
  122. info("Finished cursor");
  123. continueToNextStep();
  124. }
  125. };
  126. yield undefined;
  127. is(cursorResults.length, 2, "Got right number of items");
  128. fileReader = new FileReader();
  129. fileReader.onload = grabEventAndContinueHandler;
  130. fileReader.readAsText(cursorResults[0].blob);
  131. event = yield undefined;
  132. is(event.target.result, BLOB_DATA.join(""), "Correct text");
  133. fileReader = new FileReader();
  134. fileReader.onload = grabEventAndContinueHandler;
  135. fileReader.readAsText(cursorResults[1].blob);
  136. event = yield undefined;
  137. is(event.target.result, BLOB_DATA.join(""), "Correct text");
  138. info("Slice the the retrieved blob and verify its contents.");
  139. let slice = cursorResults[1].blob.slice(0, BLOB_DATA[0].length);
  140. fileReader = new FileReader();
  141. fileReader.onload = grabEventAndContinueHandler;
  142. fileReader.readAsText(slice);
  143. event = yield undefined;
  144. is(event.target.result, BLOB_DATA[0], "Correct text");
  145. info("Send blob to a worker, read its contents there, and verify results.");
  146. function workerScript() {
  147. onmessage = function(event) {
  148. var reader = new FileReaderSync();
  149. postMessage(reader.readAsText(event.data));
  150. var slice = event.data.slice(1, 2);
  151. postMessage(reader.readAsText(slice));
  152. }
  153. }
  154. let url =
  155. URL.createObjectURL(new Blob(["(", workerScript.toSource(), ")()"]));
  156. let worker = new Worker(url);
  157. worker.postMessage(slice);
  158. worker.onmessage = grabEventAndContinueHandler;
  159. event = yield undefined;
  160. is(event.data, BLOB_DATA[0], "Correct text");
  161. event = yield undefined;
  162. is(event.data, BLOB_DATA[0][1], "Correct text");
  163. info("Store a blob back in the database, and keep holding on to the " +
  164. "blob, verifying that it still can be read.");
  165. objectStore = db.transaction("foo").objectStore("foo");
  166. objectStore.get(key).onsuccess = grabEventAndContinueHandler;
  167. event = yield undefined;
  168. let blobFromDB = event.target.result.blob;
  169. info("Got blob from database");
  170. let txn = db.transaction("foo", "readwrite");
  171. txn.objectStore("foo").put(event.target.result, key);
  172. txn.oncomplete = grabEventAndContinueHandler;
  173. event = yield undefined;
  174. info("Stored blob back into database");
  175. fileReader = new FileReader();
  176. fileReader.onload = grabEventAndContinueHandler;
  177. fileReader.readAsText(blobFromDB);
  178. event = yield undefined;
  179. is(event.target.result, BLOB_DATA.join(""), "Correct text");
  180. blobURL = URL.createObjectURL(blobFromDB);
  181. xhr = new XMLHttpRequest();
  182. xhr.open("GET", blobURL);
  183. xhr.onload = grabEventAndContinueHandler;
  184. xhr.send();
  185. yield undefined;
  186. URL.revokeObjectURL(blobURL);
  187. is(xhr.responseText, BLOB_DATA.join(""), "Correct responseText");
  188. finishTest();
  189. yield undefined;
  190. }
  191. </script>
  192. <script type="text/javascript;version=1.7" src="helpers.js"></script>
  193. </head>
  194. <body onload="runTest();"></body>
  195. </html>