2412424fd4e71fc46be280edc7ab379da7295f25
[WebKit-https.git] / LayoutTests / imported / w3c / web-platform-tests / IndexedDB / transaction-lifetime.htm
1 <!DOCTYPE html>
2 <title>Test events opening a second database when one connection is open already</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                 db.close();
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.onupgradeneeded = t.step_func(function(e) {
62             db2 = e.target.result;
63
64             events.push("open2." + e.type);
65
66             assert_equals(db2 + "", "[object IDBDatabase]");
67
68             // Errors
69             db2.onversionchange = fail(t, "db2.versionchange");
70             db2.onerror = fail(t, "db2.error");
71             db2.abort = fail(t, "db2.abort");
72         });
73
74         // 5
75         openrq2.onsuccess = t.step_func(function(e) {
76             events.push("open2." + e.type);
77
78             assert_array_equals(events,
79                 [ "open.upgradeneeded",
80                   "open.success",
81                   "db.versionchange",
82                   "open2.upgradeneeded",
83                   "open2.success",
84                 ]);
85
86             setTimeout(function() { t.done(); }, 10);
87         });
88
89         // Errors
90         openrq2.onerror = fail(t, "open2.error");
91         openrq2.onblocked = fail(t, "open2.blocked");
92     }
93
94
95     // Cleanup
96     add_completion_callback(function(tests) {
97         if (db2) db2.close();
98         indexedDB.deleteDatabase('db');
99     })
100
101 </script>