gecko/dom/indexedDB/test/test_blob_simple.html
Ben Turner 98a57bfb9c Bug 778968 - 'Avoid stat-ing remote blobs on the main thread'. r=janv.
--HG--
extra : transplant_source : %90%87%047%07BA%97%E0%F1n%1DY%08D%7C%E39%20%0F
2012-08-02 23:25:30 -07:00

192 lines
5.5 KiB
HTML

<!--
Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/
-->
<html>
<head>
<title>Indexed Database Property Test</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<script type="text/javascript;version=1.7">
function testSteps()
{
const BLOB_DATA = ["fun ", "times ", "all ", "around!"];
const INDEX_KEY = 5;
let request = indexedDB.open(window.location.pathname, 1);
request.onerror = errorHandler;
request.onupgradeneeded = grabEventAndContinueHandler;
request.onsuccess = unexpectedSuccessHandler;
let event = yield;
let db = event.target.result;
db.onerror = errorHandler;
let objectStore = db.createObjectStore("foo", { autoIncrement: true });
let index = objectStore.createIndex("foo", "index");
request.onsuccess = grabEventAndContinueHandler;
event = yield;
let blob = new Blob(BLOB_DATA, { type: "text/plain" });
let data = { blob: blob, index: INDEX_KEY };
objectStore = db.transaction("foo", "readwrite").objectStore("foo");
objectStore.add(data).onsuccess = grabEventAndContinueHandler;
event = yield;
let key = event.target.result;
objectStore.add(data).onsuccess = grabEventAndContinueHandler;
event = yield;
objectStore = db.transaction("foo").objectStore("foo");
objectStore.get(key).onsuccess = grabEventAndContinueHandler;
event = yield;
let fileReader = new FileReader();
fileReader.onload = grabEventAndContinueHandler;
fileReader.readAsText(event.target.result.blob);
event = yield;
is(event.target.result, BLOB_DATA.join(""), "Correct text");
objectStore = db.transaction("foo").objectStore("foo");
objectStore.mozGetAll().onsuccess = grabEventAndContinueHandler;
event = yield;
is(event.target.result.length, 2, "Got right number of items");
fileReader = new FileReader();
fileReader.onload = grabEventAndContinueHandler;
fileReader.readAsText(event.target.result[0].blob);
event = yield;
is(event.target.result, BLOB_DATA.join(""), "Correct text");
let cursorResults = [];
objectStore = db.transaction("foo").objectStore("foo");
objectStore.openCursor().onsuccess = function(event) {
let cursor = event.target.result;
if (cursor) {
cursorResults.push(cursor.value);
cursor.continue();
}
else {
continueToNextStep();
}
};
yield;
is(cursorResults.length, 2, "Got right number of items");
fileReader = new FileReader();
fileReader.onload = grabEventAndContinueHandler;
fileReader.readAsText(cursorResults[0].blob);
event = yield;
is(event.target.result, BLOB_DATA.join(""), "Correct text");
let index = db.transaction("foo").objectStore("foo").index("foo");
index.get(INDEX_KEY).onsuccess = grabEventAndContinueHandler;
event = yield;
fileReader = new FileReader();
fileReader.onload = grabEventAndContinueHandler;
fileReader.readAsText(event.target.result.blob);
event = yield;
is(event.target.result, BLOB_DATA.join(""), "Correct text");
index = db.transaction("foo").objectStore("foo").index("foo");
index.mozGetAll().onsuccess = grabEventAndContinueHandler;
event = yield;
is(event.target.result.length, 2, "Got right number of items");
fileReader = new FileReader();
fileReader.onload = grabEventAndContinueHandler;
fileReader.readAsText(event.target.result[0].blob);
event = yield;
is(event.target.result, BLOB_DATA.join(""), "Correct text");
cursorResults = [];
index = db.transaction("foo").objectStore("foo").index("foo");
index.openCursor().onsuccess = function(event) {
let cursor = event.target.result;
if (cursor) {
cursorResults.push(cursor.value);
cursor.continue();
}
else {
continueToNextStep();
}
};
yield;
is(cursorResults.length, 2, "Got right number of items");
fileReader = new FileReader();
fileReader.onload = grabEventAndContinueHandler;
fileReader.readAsText(cursorResults[0].blob);
event = yield;
is(event.target.result, BLOB_DATA.join(""), "Correct text");
fileReader = new FileReader();
fileReader.onload = grabEventAndContinueHandler;
fileReader.readAsText(cursorResults[1].blob);
event = yield;
is(event.target.result, BLOB_DATA.join(""), "Correct text");
let slice = cursorResults[1].blob.slice(0, BLOB_DATA[0].length);
fileReader = new FileReader();
fileReader.onload = grabEventAndContinueHandler;
fileReader.readAsText(slice);
event = yield;
is(event.target.result, BLOB_DATA[0], "Correct text");
function workerScript() {
onmessage = function(event) {
var reader = new FileReaderSync();
postMessage(reader.readAsText(event.data));
var slice = event.data.slice(1, 2);
postMessage(reader.readAsText(slice));
}
}
let url =
URL.createObjectURL(new Blob(["(", workerScript.toSource(), ")()"]));
let worker = new Worker(url);
worker.postMessage(slice);
worker.onmessage = grabEventAndContinueHandler;
event = yield;
is(event.data, BLOB_DATA[0], "Correct text");
event = yield;
is(event.data, BLOB_DATA[0][1], "Correct text");
finishTest();
yield;
}
</script>
<script type="text/javascript;version=1.7" src="helpers.js"></script>
</head>
<body onload="runTest();"></body>
</html>