OLD | NEW |
1 library IndexedDB1Test; | 1 library IndexedDB1Test; |
2 import '../../pkg/unittest/lib/unittest.dart'; | 2 import '../../pkg/unittest/lib/unittest.dart'; |
3 import '../../pkg/unittest/lib/html_config.dart'; | 3 import '../../pkg/unittest/lib/html_config.dart'; |
4 import 'dart:async'; | 4 import 'dart:async'; |
5 import 'dart:html' as html; | 5 import 'dart:html' as html; |
6 import 'dart:indexed_db' as idb; | 6 import 'dart:indexed_db' as idb; |
7 | 7 |
8 main() { | 8 main() { |
9 useHtmlConfiguration(); | 9 useHtmlConfiguration(); |
10 | 10 |
(...skipping 24 matching lines...) Expand all Loading... |
35 if (html.window.indexedDB.supportsDatabaseNames) { | 35 if (html.window.indexedDB.supportsDatabaseNames) { |
36 test('getDatabaseNames', () { | 36 test('getDatabaseNames', () { |
37 return html.window.indexedDB.getDatabaseNames().then((names) { | 37 return html.window.indexedDB.getDatabaseNames().then((names) { |
38 expect(names.contains(dbName), isTrue); | 38 expect(names.contains(dbName), isTrue); |
39 }); | 39 }); |
40 }); | 40 }); |
41 } | 41 } |
42 | 42 |
43 var value = {'name_index': 'one', 'value': 'add_value'}; | 43 var value = {'name_index': 'one', 'value': 'add_value'}; |
44 test('add/delete', () { | 44 test('add/delete', () { |
45 var transaction = db.transaction([storeName], 'readwrite'); | 45 var transaction = db.transaction(storeName, 'readwrite'); |
46 var key; | 46 var key; |
47 return transaction.objectStore(storeName).add(value).then((addedKey) { | 47 return transaction.objectStore(storeName).add(value).then((addedKey) { |
48 key = addedKey; | 48 key = addedKey; |
49 }).then((_) { | 49 }).then((_) { |
50 return transaction.completed; | 50 return transaction.completed; |
51 }).then((_) { | 51 }).then((_) { |
52 transaction = db.transaction([storeName], 'readonly'); | 52 transaction = db.transaction(storeName, 'readonly'); |
53 return transaction.objectStore(storeName).getObject(key); | 53 return transaction.objectStore(storeName).getObject(key); |
54 }).then((readValue) { | 54 }).then((readValue) { |
55 expect(readValue['value'], value['value']); | 55 expect(readValue['value'], value['value']); |
56 return transaction.completed; | 56 return transaction.completed; |
57 }).then((_) { | 57 }).then((_) { |
58 transaction = db.transaction([storeName], 'readwrite'); | 58 transaction = db.transactionList([storeName], 'readwrite'); |
59 return transaction.objectStore(storeName).delete(key); | 59 return transaction.objectStore(storeName).delete(key); |
60 }).then((_) { | 60 }).then((_) { |
61 return transaction.completed; | 61 return transaction.completed; |
62 }).then((_) { | 62 }).then((_) { |
63 var transaction = db.transaction([storeName], 'readonly'); | 63 var transaction = db.transactionList([storeName], 'readonly'); |
64 return transaction.objectStore(storeName).count(); | 64 return transaction.objectStore(storeName).count(); |
65 }).then((count) { | 65 }).then((count) { |
66 expect(count, 0); | 66 expect(count, 0); |
67 }); | 67 }); |
68 }); | 68 }); |
69 | 69 |
70 test('clear/count', () { | 70 test('clear/count', () { |
71 var transaction = db.transaction([storeName], 'readwrite'); | 71 var transaction = db.transaction(storeName, 'readwrite'); |
72 transaction.objectStore(storeName).add(value); | 72 transaction.objectStore(storeName).add(value); |
73 | 73 |
74 return transaction.completed.then((_) { | 74 return transaction.completed.then((_) { |
75 transaction = db.transaction([storeName], 'readonly'); | 75 transaction = db.transaction(storeName, 'readonly'); |
76 return transaction.objectStore(storeName).count(); | 76 return transaction.objectStore(storeName).count(); |
77 }).then((count) { | 77 }).then((count) { |
78 expect(count, 1); | 78 expect(count, 1); |
79 }).then((_) { | 79 }).then((_) { |
80 return transaction.completed; | 80 return transaction.completed; |
81 }).then((_) { | 81 }).then((_) { |
82 transaction = db.transaction([storeName], 'readwrite'); | 82 transaction = db.transactionList([storeName], 'readwrite'); |
83 transaction.objectStore(storeName).clear(); | 83 transaction.objectStore(storeName).clear(); |
84 return transaction.completed; | 84 return transaction.completed; |
85 }).then((_) { | 85 }).then((_) { |
86 var transaction = db.transaction([storeName], 'readonly'); | 86 var transaction = db.transactionList([storeName], 'readonly'); |
87 return transaction.objectStore(storeName).count(); | 87 return transaction.objectStore(storeName).count(); |
88 }).then((count) { | 88 }).then((count) { |
89 expect(count, 0); | 89 expect(count, 0); |
90 }); | 90 }); |
91 }); | 91 }); |
92 | 92 |
93 test('index', () { | 93 test('index', () { |
94 var transaction = db.transaction([storeName], 'readwrite'); | 94 var transaction = db.transaction(storeName, 'readwrite'); |
95 transaction.objectStore(storeName).add(value); | 95 transaction.objectStore(storeName).add(value); |
96 transaction.objectStore(storeName).add(value); | 96 transaction.objectStore(storeName).add(value); |
97 transaction.objectStore(storeName).add(value); | 97 transaction.objectStore(storeName).add(value); |
98 transaction.objectStore(storeName).add(value); | 98 transaction.objectStore(storeName).add(value); |
99 | 99 |
100 return transaction.completed.then((_) { | 100 return transaction.completed.then((_) { |
101 transaction = db.transaction([storeName], 'readonly'); | 101 transaction = db.transactionList([storeName], 'readonly'); |
102 var index = transaction.objectStore(storeName).index(indexName); | 102 var index = transaction.objectStore(storeName).index(indexName); |
103 return index.count(); | 103 return index.count(); |
104 }).then((count) { | 104 }).then((count) { |
105 expect(count, 4); | 105 expect(count, 4); |
106 return transaction.completed; | 106 return transaction.completed; |
107 }).then((_) { | 107 }).then((_) { |
108 transaction = db.transaction([storeName], 'readonly'); | 108 transaction = db.transaction(storeName, 'readonly'); |
109 var index = transaction.objectStore(storeName).index(indexName); | 109 var index = transaction.objectStore(storeName).index(indexName); |
110 return index.openCursor(autoAdvance: true).length; | 110 return index.openCursor(autoAdvance: true).length; |
111 }).then((cursorsLength) { | 111 }).then((cursorsLength) { |
112 expect(cursorsLength, 4); | 112 expect(cursorsLength, 4); |
113 return transaction.completed; | 113 return transaction.completed; |
114 }).then((_) { | 114 }).then((_) { |
115 transaction = db.transaction([storeName], 'readonly'); | 115 transaction = db.transaction(storeName, 'readonly'); |
116 var index = transaction.objectStore(storeName).index(indexName); | 116 var index = transaction.objectStore(storeName).index(indexName); |
117 return index.openKeyCursor(autoAdvance: true).length; | 117 return index.openKeyCursor(autoAdvance: true).length; |
118 }).then((cursorsLength) { | 118 }).then((cursorsLength) { |
119 expect(cursorsLength, 4); | 119 expect(cursorsLength, 4); |
120 return transaction.completed; | 120 return transaction.completed; |
121 }).then((_) { | 121 }).then((_) { |
122 transaction = db.transaction([storeName], 'readonly'); | 122 transaction = db.transaction(storeName, 'readonly'); |
123 var index = transaction.objectStore(storeName).index(indexName); | 123 var index = transaction.objectStore(storeName).index(indexName); |
124 return index.get('one'); | 124 return index.get('one'); |
125 }).then((readValue) { | 125 }).then((readValue) { |
126 expect(readValue['value'], value['value']); | 126 expect(readValue['value'], value['value']); |
127 return transaction.completed; | 127 return transaction.completed; |
128 }).then((_) { | 128 }).then((_) { |
129 transaction = db.transaction([storeName], 'readwrite'); | 129 transaction = db.transaction(storeName, 'readwrite'); |
130 transaction.objectStore(storeName).clear(); | 130 transaction.objectStore(storeName).clear(); |
131 return transaction.completed; | 131 return transaction.completed; |
132 }); | 132 }); |
133 }); | 133 }); |
134 | 134 |
135 var deleteValue = {'name_index': 'two', 'value': 'delete_value'}; | 135 var deleteValue = {'name_index': 'two', 'value': 'delete_value'}; |
136 var updateValue = {'name_index': 'three', 'value': 'update_value'}; | 136 var updateValue = {'name_index': 'three', 'value': 'update_value'}; |
137 var updatedValue = {'name_index': 'three', 'value': 'updated_value'}; | 137 var updatedValue = {'name_index': 'three', 'value': 'updated_value'}; |
138 test('cursor', () { | 138 test('cursor', () { |
139 var transaction = db.transaction([storeName], 'readwrite'); | 139 var transaction = db.transaction(storeName, 'readwrite'); |
140 transaction.objectStore(storeName).add(value); | 140 transaction.objectStore(storeName).add(value); |
141 transaction.objectStore(storeName).add(deleteValue); | 141 transaction.objectStore(storeName).add(deleteValue); |
142 transaction.objectStore(storeName).add(updateValue); | 142 transaction.objectStore(storeName).add(updateValue); |
143 | 143 |
144 return transaction.completed.then((_) { | 144 return transaction.completed.then((_) { |
145 transaction = db.transaction([storeName], 'readwrite'); | 145 transaction = db.transactionList([storeName], 'readwrite'); |
146 var index = transaction.objectStore(storeName).index(indexName); | 146 var index = transaction.objectStore(storeName).index(indexName); |
147 var cursors = index.openCursor().asBroadcastStream(); | 147 var cursors = index.openCursor().asBroadcastStream(); |
148 | 148 |
149 cursors.listen((cursor) { | 149 cursors.listen((cursor) { |
150 var value = cursor.value; | 150 var value = cursor.value; |
151 if (value['value'] == 'delete_value') { | 151 if (value['value'] == 'delete_value') { |
152 cursor.delete().then((_) { | 152 cursor.delete().then((_) { |
153 cursor.next(); | 153 cursor.next(); |
154 }); | 154 }); |
155 } else if (value['value'] == 'update_value') { | 155 } else if (value['value'] == 'update_value') { |
156 cursor.update(updatedValue).then((_) { | 156 cursor.update(updatedValue).then((_) { |
157 cursor.next(); | 157 cursor.next(); |
158 }); | 158 }); |
159 } else { | 159 } else { |
160 cursor.next(); | 160 cursor.next(); |
161 } | 161 } |
162 }); | 162 }); |
163 return cursors.last; | 163 return cursors.last; |
164 | 164 |
165 }).then((_) { | 165 }).then((_) { |
166 return transaction.completed; | 166 return transaction.completed; |
167 }).then((_) { | 167 }).then((_) { |
168 transaction = db.transaction([storeName], 'readonly'); | 168 transaction = db.transaction(storeName, 'readonly'); |
169 var index = transaction.objectStore(storeName).index(indexName); | 169 var index = transaction.objectStore(storeName).index(indexName); |
170 return index.get('three'); | 170 return index.get('three'); |
171 }).then((readValue) { | 171 }).then((readValue) { |
172 expect(readValue['value'], 'updated_value'); | 172 expect(readValue['value'], 'updated_value'); |
173 return transaction.completed; | 173 return transaction.completed; |
174 }).then((_) { | 174 }).then((_) { |
175 transaction = db.transaction([storeName], 'readonly'); | 175 transaction = db.transaction(storeName, 'readonly'); |
176 var index = transaction.objectStore(storeName).index(indexName); | 176 var index = transaction.objectStore(storeName).index(indexName); |
177 return index.get('two'); | 177 return index.get('two'); |
178 }).then((readValue) { | 178 }).then((readValue) { |
179 expect(readValue, isNull); | 179 expect(readValue, isNull); |
180 return transaction.completed; | 180 return transaction.completed; |
181 }); | 181 }); |
182 }); | 182 }); |
183 } | 183 } |
OLD | NEW |