Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / storage / indexeddb / resources / index-population.js
blobe2142561ae340a26e768b9c67799c69ca38440f2
1 if (this.importScripts) {
2     importScripts('../../../resources/js-test.js');
3     importScripts('shared.js');
6 description("Test IndexedDB index population.");
8 indexedDBTest(prepareDatabase, doSetVersion2);
9 function prepareDatabase()
11     db = event.target.result;
12     evalAndLog("transaction = event.target.transaction");
13     transaction.onerror = unexpectedErrorCallback;
14     transaction.onabort = unexpectedAbortCallback;
15     store = evalAndLog("store = db.createObjectStore('store1')");
16     evalAndLog("store.put({data: 'a', indexKey: 10}, 1)");
17     evalAndLog("store.put({data: 'b', indexKey: 20}, 2)");
18     evalAndLog("store.put({data: 'c', indexKey: 10}, 3)");
19     evalAndLog("store.put({data: 'd', indexKey: 20}, 4)");
20     evalAndLog("index = store.createIndex('index1', 'indexKey')");
21     shouldBeTrue("index instanceof IDBIndex");
22     shouldBeFalse("index.unique");
23     request = evalAndLog("request = index.count(IDBKeyRange.bound(-Infinity, Infinity))");
24     request.onerror = unexpectedErrorCallback;
25     request.onsuccess = function () {
26         shouldBe("request.result", "4");
27     };
30 function doSetVersion2() {
31     debug("");
32     debug("doSetVersion2():");
33     evalAndLog("db.close()");
34     evalAndLog("request = indexedDB.open(dbname, 2)");
35     request.onupgradeneeded = setVersion2;
36     request.onblocked = unexpectedBlockedCallback;
39 function setVersion2()
41     debug("");
42     debug("setVersion2():");
43     db = event.target.result;
44     transaction2 = evalAndLog("transaction = request.transaction");
45     transaction2.onabort = setVersion2Abort;
46     transaction2.oncomplete = unexpectedCompleteCallback;
48     var capturePhase = true;
49     transaction2.addEventListener("error", unexpectedErrorCallback, !capturePhase);
50     transaction2.addEventListener("error", unexpectedErrorCallback, capturePhase);
51     db.addEventListener("error", unexpectedErrorCallback, !capturePhase);
52     db.addEventListener("error", unexpectedErrorCallback, capturePhase);
54     store = evalAndLog("store = db.createObjectStore('store2')");
55     evalAndLog("store.put({data: 'a', indexKey: 10}, 1)");
56     evalAndLog("store.put({data: 'b', indexKey: 20}, 2)");
57     evalAndLog("store.put({data: 'c', indexKey: 10}, 3)");
58     evalAndLog("store.put({data: 'd', indexKey: 20}, 4)");
59     evalAndLog("index2 = store.createIndex('index2', 'indexKey', { unique: true })");
60     shouldBeTrue("index2 instanceof IDBIndex");
61     shouldBeTrue("index2.unique");
64 function setVersion2Abort()
66     debug("");
67     debug("setVersion2Abort():");
68     shouldBe("db.objectStoreNames.length", "1");
69     shouldBeEqualToString("db.objectStoreNames[0]", "store1");
70     finishJSTest();