2 * Any copyright is dedicated to the Public Domain.
3 * http://creativecommons.org/publicdomain/zero/1.0/
6 /* exported testGenerator */
7 var testGenerator = testSteps();
9 function* testSteps() {
10 const name = this.window ? window.location.pathname : "Splendid Test";
12 // Open a datbase for the first time.
13 let request = indexedDB.open(name, 1);
16 ok(request instanceof IDBRequest, "Request should be an IDBRequest");
18 request instanceof IDBOpenDBRequest,
19 "Request should be an IDBOpenDBRequest"
21 ok(request instanceof EventTarget, "Request should be an EventTarget");
22 is(request.source, null, "Request should have no source");
25 ok(false, "Getter should have thrown!");
27 if (e.result == 0x8053000b /* NS_ERROR_DOM_INVALID_STATE_ERR */) {
28 ok(true, "Getter threw the right exception");
34 request.onerror = errorHandler;
35 request.onsuccess = grabEventAndContinueHandler;
36 let event = yield undefined;
38 let versionChangeEventCount = 0;
41 db1 = event.target.result;
42 db1.addEventListener("versionchange", function(event) {
43 ok(true, "Got version change event");
44 ok(event instanceof IDBVersionChangeEvent, "Event is of the right type");
45 is("source" in event.target, false, "Correct source");
46 is(event.target, db1, "Correct target");
47 is(event.target.version, 1, "Correct db version");
48 is(event.oldVersion, 1, "Correct event oldVersion");
49 is(event.newVersion, 2, "Correct event newVersion");
50 is(versionChangeEventCount++, 0, "Correct count");
54 // Open the database again and trigger an upgrade that should succeed
55 request = indexedDB.open(name, 2);
56 request.onerror = errorHandler;
57 request.onsuccess = errorHandler;
58 request.onupgradeneeded = grabEventAndContinueHandler;
59 request.onblocked = errorHandler;
61 event = yield undefined;
63 // Test the upgradeneeded event.
64 ok(event instanceof IDBVersionChangeEvent, "Event is of the right type");
65 ok(event.target.result instanceof IDBDatabase, "Good result");
66 db2 = event.target.result;
67 is(event.target.transaction.mode, "versionchange", "Correct mode");
68 is(db2.version, 2, "Correct db version");
69 is(event.oldVersion, 1, "Correct event oldVersion");
70 is(event.newVersion, 2, "Correct event newVersion");
72 request.onupgradeneeded = errorHandler;
73 request.onsuccess = grabEventAndContinueHandler;
74 event = yield undefined;
76 db2.addEventListener("versionchange", function(event) {
77 ok(true, "Got version change event");
78 ok(event instanceof IDBVersionChangeEvent, "Event is of the right type");
79 is("source" in event.target, false, "Correct source");
80 is(event.target, db2, "Correct target");
81 is(event.target.version, 2, "Correct db version");
82 is(event.oldVersion, 2, "Correct event oldVersion");
83 is(event.newVersion, 3, "Correct event newVersion");
84 is(versionChangeEventCount++, 1, "Correct count");
87 // Test opening the existing version again
88 request = indexedDB.open(name, 2);
89 request.onerror = errorHandler;
90 request.onsuccess = grabEventAndContinueHandler;
91 request.onblocked = errorHandler;
93 event = yield undefined;
95 db3 = event.target.result;
97 // Test an upgrade that should fail
98 request = indexedDB.open(name, 3);
99 request.onerror = errorHandler;
100 request.onsuccess = errorHandler;
101 request.onupgradeneeded = errorHandler;
102 request.onblocked = grabEventAndContinueHandler;
104 event = yield undefined;
105 ok(true, "Got version change blocked event");
106 ok(event instanceof IDBVersionChangeEvent, "Event is of the right type");
107 is(event.target.source, null, "Correct source");
108 is(event.target.transaction, null, "Correct transaction");
109 is(event.target, request, "Correct target");
110 is(db3.version, 2, "Correct db version");
111 is(event.oldVersion, 2, "Correct event oldVersion");
112 is(event.newVersion, 3, "Correct event newVersion");
113 versionChangeEventCount++;
117 request.onupgradeneeded = grabEventAndContinueHandler;
118 request.onsuccess = grabEventAndContinueHandler;
120 event = yield undefined;
121 event = yield undefined;
123 db3 = event.target.result;
126 // Test another upgrade that should succeed.
127 request = indexedDB.open(name, 4);
128 request.onerror = errorHandler;
129 request.onsuccess = errorHandler;
130 request.onupgradeneeded = grabEventAndContinueHandler;
131 request.onblocked = errorHandler;
133 event = yield undefined;
135 ok(event instanceof IDBVersionChangeEvent, "Event is of the right type");
136 ok(event.target.result instanceof IDBDatabase, "Good result");
137 is(event.target.transaction.mode, "versionchange", "Correct mode");
138 is(event.oldVersion, 3, "Correct event oldVersion");
139 is(event.newVersion, 4, "Correct event newVersion");
141 request.onsuccess = grabEventAndContinueHandler;
143 event = yield undefined;
144 ok(event.target.result instanceof IDBDatabase, "Expect a database here");
145 is(event.target.result.version, 4, "Right version");
146 is(db3.version, 3, "After closing the version should not change!");
147 is(db2.version, 2, "After closing the version should not change!");
148 is(db1.version, 1, "After closing the version should not change!");
150 is(versionChangeEventCount, 3, "Saw all expected events");
152 event = new IDBVersionChangeEvent("versionchange");
153 ok(event, "Should be able to create an event with just passing in the type");
154 event = new IDBVersionChangeEvent("versionchange", { oldVersion: 1 });
155 ok(event, "Should be able to create an event with just the old version");
156 is(event.oldVersion, 1, "Correct old version");
157 is(event.newVersion, null, "Correct new version");
158 event = new IDBVersionChangeEvent("versionchange", { newVersion: 1 });
159 ok(event, "Should be able to create an event with just the new version");
160 is(event.oldVersion, 0, "Correct old version");
161 is(event.newVersion, 1, "Correct new version");
162 event = new IDBVersionChangeEvent("versionchange", {
166 ok(event, "Should be able to create an event with both versions");
167 is(event.oldVersion, 1, "Correct old version");
168 is(event.newVersion, 2, "Correct new version");