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