|
1 /** |
|
2 * Any copyright is dedicated to the Public Domain. |
|
3 * http://creativecommons.org/publicdomain/zero/1.0/ |
|
4 */ |
|
5 |
|
6 var testGenerator = testSteps(); |
|
7 |
|
8 function testSteps() |
|
9 { |
|
10 const name = this.window ? window.location.pathname : "Splendid Test"; |
|
11 let openRequest = indexedDB.open(name, 1); |
|
12 openRequest.onerror = errorHandler; |
|
13 openRequest.onupgradeneeded = grabEventAndContinueHandler; |
|
14 openRequest.onsuccess = unexpectedSuccessHandler; |
|
15 let event = yield undefined; |
|
16 let db = event.target.result; |
|
17 let trans = event.target.transaction; |
|
18 |
|
19 for each (let autoincrement in [true, false]) { |
|
20 for each (let keypath in [false, true, "missing", "invalid"]) { |
|
21 for each (let method in ["put", "add"]) { |
|
22 for each (let explicit in [true, false, undefined, "invalid"]) { |
|
23 for each (let existing in [true, false]) { |
|
24 let speccedNoKey = (keypath == false || keypath == "missing") && |
|
25 !explicit; |
|
26 |
|
27 // We can't do 'existing' checks if we use autogenerated key |
|
28 if (speccedNoKey && autoincrement && existing) { |
|
29 continue; |
|
30 } |
|
31 |
|
32 // Create store |
|
33 if (db.objectStoreNames.contains("mystore")) |
|
34 db.deleteObjectStore("mystore"); |
|
35 let store = db.createObjectStore("mystore", |
|
36 { autoIncrement: autoincrement, |
|
37 keyPath: (keypath ? "id" : null) }); |
|
38 |
|
39 test = " for test " + JSON.stringify({ autoincrement: autoincrement, |
|
40 keypath: keypath, |
|
41 method: method, |
|
42 explicit: explicit === undefined ? "undefined" : explicit, |
|
43 existing: existing }); |
|
44 |
|
45 // Insert "existing" data if needed |
|
46 if (existing) { |
|
47 if (keypath) |
|
48 store.add({ existing: "data", id: 5 }).onsuccess = grabEventAndContinueHandler; |
|
49 else |
|
50 store.add({ existing: "data" }, 5).onsuccess = grabEventAndContinueHandler; |
|
51 |
|
52 let e = yield undefined; |
|
53 is(e.type, "success", "success inserting existing" + test); |
|
54 is(e.target.result, 5, "inserted correct key" + test); |
|
55 } |
|
56 |
|
57 // Set up value to be inserted |
|
58 let value = { theObj: true }; |
|
59 if (keypath === true) { |
|
60 value.id = 5; |
|
61 } |
|
62 else if (keypath === "invalid") { |
|
63 value.id = /x/; |
|
64 } |
|
65 |
|
66 // Which arguments are passed to function |
|
67 args = [value]; |
|
68 if (explicit === true) { |
|
69 args.push(5); |
|
70 } |
|
71 else if (explicit === undefined) { |
|
72 args.push(undefined); |
|
73 } |
|
74 else if (explicit === "invalid") { |
|
75 args.push(/x/); |
|
76 } |
|
77 |
|
78 let expected = expectedResult(method, keypath, explicit, autoincrement, existing); |
|
79 |
|
80 let valueJSON = JSON.stringify(value); |
|
81 |
|
82 ok(true, "making call" + test); |
|
83 |
|
84 // Make function call for throwing functions |
|
85 if (expected === "throw") { |
|
86 try { |
|
87 store[method].apply(store, args); |
|
88 ok(false, "should have thrown" + test); |
|
89 } |
|
90 catch (ex) { |
|
91 ok(true, "did throw" + test); |
|
92 ok(ex instanceof DOMException, "Got a DOMException" + test); |
|
93 is(ex.name, "DataError", "expect a DataError" + test); |
|
94 is(ex.code, 0, "expect zero" + test); |
|
95 is(JSON.stringify(value), valueJSON, "call didn't modify value" + test); |
|
96 } |
|
97 continue; |
|
98 } |
|
99 |
|
100 // Make non-throwing function call |
|
101 let req = store[method].apply(store, args); |
|
102 is(JSON.stringify(value), valueJSON, "call didn't modify value" + test); |
|
103 |
|
104 req.onsuccess = req.onerror = grabEventAndContinueHandler; |
|
105 let e = yield undefined; |
|
106 |
|
107 // Figure out what key we used |
|
108 let key = 5; |
|
109 if (autoincrement && speccedNoKey) { |
|
110 key = 1; |
|
111 } |
|
112 |
|
113 // Adjust value if expected |
|
114 if (autoincrement && keypath && speccedNoKey) { |
|
115 value.id = key; |
|
116 } |
|
117 |
|
118 // Check result |
|
119 if (expected === "error") { |
|
120 is(e.type, "error", "write should fail" + test); |
|
121 e.preventDefault(); |
|
122 e.stopPropagation(); |
|
123 continue; |
|
124 } |
|
125 |
|
126 is(e.type, "success", "write should succeed" + test); |
|
127 is(e.target.result, key, "write should return correct key" + test); |
|
128 |
|
129 store.get(key).onsuccess = grabEventAndContinueHandler; |
|
130 e = yield undefined; |
|
131 is(e.type, "success", "read back should succeed" + test); |
|
132 is(JSON.stringify(e.target.result), |
|
133 JSON.stringify(value), |
|
134 "read back should return correct value" + test); |
|
135 } |
|
136 } |
|
137 } |
|
138 } |
|
139 } |
|
140 |
|
141 |
|
142 function expectedResult(method, keypath, explicit, autoincrement, existing) { |
|
143 if (keypath && explicit) |
|
144 return "throw"; |
|
145 if (!keypath && !explicit && !autoincrement) |
|
146 return "throw"; |
|
147 if (keypath == "invalid") |
|
148 return "throw"; |
|
149 if (keypath == "missing" && !autoincrement) |
|
150 return "throw"; |
|
151 if (explicit == "invalid") |
|
152 return "throw"; |
|
153 |
|
154 if (method == "add" && existing) |
|
155 return "error"; |
|
156 |
|
157 return "success"; |
|
158 } |
|
159 |
|
160 openRequest.onsuccess = grabEventAndContinueHandler; |
|
161 yield undefined; |
|
162 |
|
163 finishTest(); |
|
164 yield undefined; |
|
165 } |