3288506c5058851af2174d4f897542fe5fd749b0
[WebKit-https.git] / LayoutTests / imported / w3c / web-platform-tests / IndexedDB / transaction-lifetime-blocked.htm
1 <!DOCTYPE html>
2 <title>Blocked event</title>
3 <link rel="author" href="mailto:odinho@opera.com" title="Odin Hørthe Omdal">
4 <script src="/resources/testharness.js"></script>
5 <script src="/resources/testharnessreport.js"></script>
6 <script src="support.js"></script>
7
8 <div id="log"></div>
9
10 <script>
11
12     var db, db_got_versionchange, db2,
13         events = [],
14         t = async_test(document.title, {timeout: 10000});
15
16     t.step(function() {
17         var openrq = indexedDB.open('db', 3);
18
19         // 1
20         openrq.onupgradeneeded = t.step_func(function(e) {
21             events.push("open." + e.type);
22             e.target.result.createObjectStore('store');
23         });
24
25         // 2
26         openrq.onsuccess = t.step_func(function(e) {
27             db = e.target.result;
28
29             events.push("open." + e.type);
30
31             // 3
32             db.onversionchange = t.step_func(function(e) {
33                 events.push("db." + e.type);
34
35                 assert_equals(e.oldVersion, 3, "old version");
36                 assert_equals(e.newVersion, 4, "new version");
37                 // Do not close db here (as we should)
38             });
39
40             // Errors
41             db.onerror = fail(t, "db.error");
42             db.abort = fail(t, "db.abort");
43
44             setTimeout(t.step_func(OpenSecond), 10);
45         });
46
47         // Errors
48         openrq.onerror = fail(t, "open.error");
49         openrq.onblocked = fail(t, "open.blocked");
50
51     });
52
53     function OpenSecond (e) {
54         assert_equals(db2, undefined);
55         assert_equals(db + "", "[object IDBDatabase]");
56         assert_array_equals(db.objectStoreNames, [ "store" ]);
57
58         var openrq2 = indexedDB.open('db', 4);
59
60         // 4
61         openrq2.onblocked = t.step_func(function(e) {
62             events.push("open2." + e.type);
63             // We're closing connection from the other open()
64             db.close();
65         });
66
67         // 5
68         openrq2.onupgradeneeded = t.step_func(function(e) {
69             db2 = e.target.result;
70
71             events.push("open2." + e.type);
72
73             assert_equals(db2 + "", "[object IDBDatabase]");
74
75             // Errors
76             db2.onversionchange = fail(t, "db2.versionchange");
77             db2.onerror = fail(t, "db2.error");
78             db2.abort = fail(t, "db2.abort");
79         });
80
81         // 6
82         openrq2.onsuccess = t.step_func(function(e) {
83             events.push("open2." + e.type);
84
85             assert_array_equals(events,
86                 [ "open.upgradeneeded",
87                   "open.success",
88                   "db.versionchange",
89                   "open2.blocked",
90                   "open2.upgradeneeded",
91                   "open2.success",
92                 ]);
93
94             setTimeout(function() { t.done(); }, 10);
95         });
96
97         // Errors
98         openrq2.onerror = fail(t, "open2.error");
99     }
100
101
102     // Cleanup
103     add_completion_callback(function(tests) {
104         if (db2) db2.close();
105         indexedDB.deleteDatabase('db');
106     })
107
108 </script>