| 1 |
<!-- |
| 2 |
Any copyright is dedicated to the Public Domain. |
| 3 |
http://creativecommons.org/publicdomain/zero/1.0/ |
| 4 |
--> |
| 5 |
<html> |
| 6 |
<head> |
| 7 |
<meta charset="utf-8"> |
| 8 |
<meta name="timeout" content="long"> |
| 9 |
<script src="/resources/testharness.js"></script> |
| 10 |
<script src="/resources/testharnessreport.js"></script> |
| 11 |
</head> |
| 12 |
<body> |
| 13 |
<script> |
| 14 |
|
| 15 |
var fileCounter = 0; |
| 16 |
var globalIdx = 0; |
| 17 |
|
| 18 |
const fileListSize = 3; |
| 19 |
const elementSize = 8196; |
| 20 |
|
| 21 |
function makeFileList(dataGenerators) { |
| 22 |
const fileOpts = { type: "text/plain" }; |
| 23 |
|
| 24 |
const dataTransfer = new DataTransfer(); |
| 25 |
dataGenerators.forEach((generator, i) => { |
| 26 |
const file = new File(generator(i), "test_" + fileCounter, fileOpts); |
| 27 |
dataTransfer.items.add(file); |
| 28 |
++fileCounter; |
| 29 |
}); |
| 30 |
|
| 31 |
return dataTransfer.files; |
| 32 |
} |
| 33 |
|
| 34 |
function indexTest(testName, indexValueName, elements) { |
| 35 |
promise_test(async t => { |
| 36 |
const dbName = "indexTest_" + indexValueName; |
| 37 |
|
| 38 |
const array = new Uint32Array(elementSize); |
| 39 |
crypto.getRandomValues(array); |
| 40 |
const randomFile = new Blob(array); |
| 41 |
|
| 42 |
const fileListStatic = makeFileList( |
| 43 |
Array(fileListSize).fill(() => [randomFile]) |
| 44 |
); |
| 45 |
|
| 46 |
const makeDataItem = keyName => { |
| 47 |
return { key: keyName, idx: globalIdx++, fileList: fileListStatic, blob: randomFile }; |
| 48 |
}; |
| 49 |
|
| 50 |
const objectStoreInfo = [ |
| 51 |
{ |
| 52 |
name: "FileLists", |
| 53 |
options: { keyPath: "key" }, |
| 54 |
data: makeDataItem("A"), |
| 55 |
}, |
| 56 |
{ |
| 57 |
name: "Other FileLists", |
| 58 |
options: { keyPath: "key" }, |
| 59 |
data: makeDataItem("B"), |
| 60 |
}, |
| 61 |
]; |
| 62 |
|
| 63 |
|
| 64 |
let db = await new Promise((resolve, reject) => { |
| 65 |
const openReq = indexedDB.open(dbName, 1); |
| 66 |
openReq.onerror = () => { |
| 67 |
reject(openReq.error); |
| 68 |
}; |
| 69 |
openReq.onupgradeneeded = ev => { |
| 70 |
const dbObj = ev.target.result; |
| 71 |
for (const info of objectStoreInfo) { |
| 72 |
const objectStore = dbObj.createObjectStore(info.name, info.options); |
| 73 |
objectStore.add(info.data); |
| 74 |
} |
| 75 |
}; |
| 76 |
openReq.onsuccess = () => { |
| 77 |
resolve(openReq.result); |
| 78 |
}; |
| 79 |
}); |
| 80 |
|
| 81 |
t.add_cleanup(() => { |
| 82 |
if (db) { |
| 83 |
db.close(); |
| 84 |
indexedDB.deleteDatabase(db.name); |
| 85 |
} |
| 86 |
}); |
| 87 |
|
| 88 |
db.close(); |
| 89 |
|
| 90 |
db = await new Promise((resolve, reject) => { |
| 91 |
const openReq = indexedDB.open(dbName, 2); |
| 92 |
openReq.onerror = () => { |
| 93 |
reject(openReq.error); |
| 94 |
}; |
| 95 |
openReq.onupgradeneeded = ev => { |
| 96 |
let objectStore = event.target.transaction.objectStore("FileLists"); |
| 97 |
objectStore.createIndex("test", indexValueName, {unique: false}); |
| 98 |
}; |
| 99 |
openReq.onsuccess = () => { |
| 100 |
resolve(openReq.result); |
| 101 |
}; |
| 102 |
}); |
| 103 |
|
| 104 |
db.close(); |
| 105 |
|
| 106 |
db = await new Promise((resolve, reject) => { |
| 107 |
const openReq = indexedDB.open(dbName, 2); |
| 108 |
let done = false; |
| 109 |
openReq.onerror = () => { |
| 110 |
reject(openReq.error); |
| 111 |
}; |
| 112 |
openReq.onupgradeneeded = () => { |
| 113 |
done = true; |
| 114 |
reject(openReq.error); |
| 115 |
}; |
| 116 |
openReq.onsuccess = () => { |
| 117 |
if (!done) { |
| 118 |
resolve(openReq.result); |
| 119 |
} |
| 120 |
}; |
| 121 |
}); |
| 122 |
|
| 123 |
let tx = db.transaction(["FileLists"], "readonly"); |
| 124 |
let store = tx.objectStore("FileLists"); |
| 125 |
t.step(() => { |
| 126 |
assert_equals(store.indexNames.length, 1, "Do we have exactly one index?"); |
| 127 |
assert_equals(store.indexNames[0], "test", "Is the only index name as expected?"); |
| 128 |
}); |
| 129 |
|
| 130 |
let testIndex = store.index("test"); |
| 131 |
request = testIndex.openCursor(); |
| 132 |
var cursorItems = 0; |
| 133 |
await new Promise((resolve, reject) => { |
| 134 |
request.onerror = () => reject(request.error); |
| 135 |
request.onsuccess = (event) => { |
| 136 |
const cursor = event.target.result; |
| 137 |
if (cursor) { |
| 138 |
cursorItems = cursorItems + 1; |
| 139 |
cursor.continue(); |
| 140 |
} else { |
| 141 |
t.step(() => { |
| 142 |
assert_equals(cursorItems, elements); |
| 143 |
}); |
| 144 |
resolve(); |
| 145 |
} |
| 146 |
}; |
| 147 |
}); |
| 148 |
t.done(); |
| 149 |
}, testName); |
| 150 |
} |
| 151 |
|
| 152 |
// FileList - exposed in Workers, but not constructable. |
| 153 |
if ("document" in self) { |
| 154 |
indexTest("Indexing on an integer value of an object containing a filelist should work", "idx", 1); |
| 155 |
|
| 156 |
indexTest("Indexing on a filelist value of an object containing a filelist should work", "filelist", 0); |
| 157 |
|
| 158 |
indexTest("Indexing on a blob value of an object containing a filelist should work", "blob", 0); |
| 159 |
} |
| 160 |
|
| 161 |
</script> |
| 162 |
</body> |
| 163 |
</html> |