Modern IDB: Fire blocked events for upgrade requests that are blocked.
[WebKit-https.git] / LayoutTests / storage / indexeddb / modern / resources / blocked-open-db-requests.js
1 description("Makes sure that an OpenDBRequest that would result in a version change is blocked because there are still open connections, it gets the blocked event. Also makes sure that request gets unblocked when the open connections close.");
2
3 indexedDBTest(prepareDatabase, versionChangeSuccessCallback);
4
5 blockedCount = 0;
6 receivedVersionChange = false;
7 function prepareDatabase()
8 {
9     evalAndLog("connection = event.target.result;");
10     evalAndLog("connection.onversionchange = firstVersionChange;");
11     evalAndLog("objectStore = connection.createObjectStore('testObjectStore');");
12 }
13
14 function versionChangeSuccessCallback()
15 {
16     debug("First version change transaction complete");
17     evalAndLog("firstTransaction = connection.transaction('testObjectStore', 'readwrite');");
18     evalAndLog("objectStore = firstTransaction.objectStore('testObjectStore');");
19     evalAndLog("objectStorePutLoop();");
20
21     evalAndLog("secondRequest = indexedDB.open(dbname, 2);");
22     evalAndLog("secondRequest.onblocked = secondBlocked;");
23     evalAndLog("secondRequest.onupgradeneeded = secondUpgradeNeeded;");
24 }
25
26 function firstVersionChange()
27 {
28     evalAndLog("receivedVersionChange = true;");
29 }
30
31 function objectStorePutLoop()
32 {
33     if (blockedCount == 1 && receivedVersionChange) {
34         connection.close();
35         return;
36     }
37
38     request = objectStore.put("bar", "foo");
39     request.onsuccess = objectStorePutLoop;
40 }
41
42 function secondBlocked()
43 {
44     evalAndLog("++blockedCount");
45     blockedEvent = event;
46     shouldBe("blockedEvent.oldVersion", "1");
47     shouldBe("blockedEvent.newVersion", "2");
48     shouldEvaluateTo("blockedEvent.target.readyState", "'pending'");
49 }
50
51 function secondUpgradeNeeded()
52 {
53     debug("2nd upgradeNeeded");
54     shouldBe("blockedCount", "1");
55     shouldBe("event.target.result.version", "2");
56     evalAndLog("event.target.result.close();");
57     finishJSTest();
58 }