|
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 objectStoreData = [ |
|
11 // This one will be removed. |
|
12 { ss: "237-23-7732", name: "Bob" }, |
|
13 |
|
14 // These will always be included. |
|
15 { ss: "237-23-7733", name: "Ann" }, |
|
16 { ss: "237-23-7734", name: "Ron" }, |
|
17 { ss: "237-23-7735", name: "Sue" }, |
|
18 { ss: "237-23-7736", name: "Joe" }, |
|
19 |
|
20 // This one will be added. |
|
21 { ss: "237-23-7737", name: "Pat" } |
|
22 ]; |
|
23 |
|
24 // Post-add and post-remove data ordered by name. |
|
25 const objectStoreDataNameSort = [ 1, 4, 5, 2, 3 ]; |
|
26 |
|
27 let request = indexedDB.open(this.window ? window.location.pathname : "Splendid Test", 1); |
|
28 request.onerror = errorHandler; |
|
29 request.onupgradeneeded = grabEventAndContinueHandler; |
|
30 let event = yield undefined; |
|
31 |
|
32 let db = event.target.result; |
|
33 event.target.onsuccess = continueToNextStep; |
|
34 |
|
35 let objectStore = db.createObjectStore("foo", { keyPath: "ss" }); |
|
36 objectStore.createIndex("name", "name", { unique: true }); |
|
37 |
|
38 for (let i = 0; i < objectStoreData.length - 1; i++) { |
|
39 objectStore.add(objectStoreData[i]); |
|
40 } |
|
41 yield undefined; |
|
42 |
|
43 let count = 0; |
|
44 |
|
45 let sawAdded = false; |
|
46 let sawRemoved = false; |
|
47 |
|
48 db.transaction("foo").objectStore("foo").openCursor().onsuccess = |
|
49 function(event) { |
|
50 event.target.transaction.oncomplete = continueToNextStep; |
|
51 let cursor = event.target.result; |
|
52 if (cursor) { |
|
53 if (cursor.value.name == objectStoreData[0].name) { |
|
54 sawRemoved = true; |
|
55 } |
|
56 if (cursor.value.name == |
|
57 objectStoreData[objectStoreData.length - 1].name) { |
|
58 sawAdded = true; |
|
59 } |
|
60 cursor.continue(); |
|
61 count++; |
|
62 } |
|
63 }; |
|
64 yield undefined; |
|
65 |
|
66 is(count, objectStoreData.length - 1, "Good initial count"); |
|
67 is(sawAdded, false, "Didn't see item that is about to be added"); |
|
68 is(sawRemoved, true, "Saw item that is about to be removed"); |
|
69 |
|
70 count = 0; |
|
71 sawAdded = false; |
|
72 sawRemoved = false; |
|
73 |
|
74 db.transaction("foo", "readwrite").objectStore("foo") |
|
75 .index("name").openCursor().onsuccess = function(event) { |
|
76 event.target.transaction.oncomplete = continueToNextStep; |
|
77 let cursor = event.target.result; |
|
78 if (cursor) { |
|
79 if (cursor.value.name == objectStoreData[0].name) { |
|
80 sawRemoved = true; |
|
81 } |
|
82 if (cursor.value.name == |
|
83 objectStoreData[objectStoreData.length - 1].name) { |
|
84 sawAdded = true; |
|
85 } |
|
86 |
|
87 is(cursor.value.name, |
|
88 objectStoreData[objectStoreDataNameSort[count++]].name, |
|
89 "Correct name"); |
|
90 |
|
91 if (count == 1) { |
|
92 let objectStore = event.target.transaction.objectStore("foo"); |
|
93 objectStore.delete(objectStoreData[0].ss) |
|
94 .onsuccess = function(event) { |
|
95 objectStore.add(objectStoreData[objectStoreData.length - 1]) |
|
96 .onsuccess = |
|
97 function(event) { |
|
98 cursor.continue(); |
|
99 }; |
|
100 }; |
|
101 } |
|
102 else { |
|
103 cursor.continue(); |
|
104 } |
|
105 } |
|
106 }; |
|
107 yield undefined; |
|
108 |
|
109 is(count, objectStoreData.length - 1, "Good final count"); |
|
110 is(sawAdded, true, "Saw item that was added"); |
|
111 is(sawRemoved, false, "Didn't see item that was removed"); |
|
112 |
|
113 finishTest(); |
|
114 |
|
115 objectStore = null; // Bug 943409 workaround. |
|
116 |
|
117 yield undefined; |
|
118 } |