OLD | NEW |
1 library IndexedDB4Test; | 1 library IndexedDB4Test; |
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:html'; | 4 import 'dart:html'; |
5 import 'dart:indexed_db'; | 5 import 'dart:indexed_db'; |
6 | 6 |
7 // Test for KeyRange and Cursor. | 7 // Test for KeyRange and Cursor. |
8 | 8 |
9 const String DB_NAME = 'Test'; | 9 const String DB_NAME = 'Test'; |
10 const String STORE_NAME = 'TEST'; | 10 const String STORE_NAME = 'TEST'; |
(...skipping 14 matching lines...) Expand all Loading... |
25 catch(e) { } // Chrome and Firefox | 25 catch(e) { } // Chrome and Firefox |
26 db.createObjectStore(STORE_NAME); | 26 db.createObjectStore(STORE_NAME); |
27 } | 27 } |
28 | 28 |
29 var db; | 29 var db; |
30 | 30 |
31 _openDb(afterOpen()) { | 31 _openDb(afterOpen()) { |
32 var request = window.indexedDB.open(DB_NAME, VERSION); | 32 var request = window.indexedDB.open(DB_NAME, VERSION); |
33 if (request is OpenDBRequest) { | 33 if (request is OpenDBRequest) { |
34 // New upgrade protocol. | 34 // New upgrade protocol. |
35 request.on.success.add(expectAsync1((e) { | 35 request.onSuccess.listen(expectAsync1((e) { |
36 db = e.target.result; | 36 db = e.target.result; |
37 afterOpen(); | 37 afterOpen(); |
38 })); | 38 })); |
39 request.on.upgradeNeeded.add((e) { | 39 request.onUpgradeNeeded.listen((e) { |
40 guardAsync(() { | 40 guardAsync(() { |
41 _createObjectStore(e.target.result); | 41 _createObjectStore(e.target.result); |
42 }); | 42 }); |
43 }); | 43 }); |
44 request.on.error.add(fail('open')); | 44 request.onError.listen(fail('open')); |
45 } else { | 45 } else { |
46 // Legacy setVersion upgrade protocol. | 46 // Legacy setVersion upgrade protocol. |
47 request.on.success.add(expectAsync1((e) { | 47 request.onSuccess.listen(expectAsync1((e) { |
48 db = e.target.result; | 48 db = e.target.result; |
49 if (db.version != '$VERSION') { | 49 if (db.version != '$VERSION') { |
50 var setRequest = db.setVersion('$VERSION'); | 50 var setRequest = db.setVersion('$VERSION'); |
51 setRequest.on.success.add( | 51 setRequest.onSuccess.listen( |
52 expectAsync1((e) { | 52 expectAsync1((e) { |
53 _createObjectStore(db); | 53 _createObjectStore(db); |
54 var transaction = e.target.result; | 54 var transaction = e.target.result; |
55 transaction.on.complete.add( | 55 transaction.onComplete.listen( |
56 expectAsync1((e) => afterOpen())); | 56 expectAsync1((e) => afterOpen())); |
57 transaction.on.error.add(fail('Upgrade')); | 57 transaction.onError.listen(fail('Upgrade')); |
58 })); | 58 })); |
59 setRequest.on.error.add(fail('setVersion error')); | 59 setRequest.onError.listen(fail('setVersion error')); |
60 } else { | 60 } else { |
61 afterOpen(); | 61 afterOpen(); |
62 } | 62 } |
63 })); | 63 })); |
64 request.on.error.add(fail('open')); | 64 request.onError.listen(fail('open')); |
65 } | 65 } |
66 } | 66 } |
67 | 67 |
68 _createAndOpenDb(afterOpen()) { | 68 _createAndOpenDb(afterOpen()) { |
69 var request = window.indexedDB.deleteDatabase(DB_NAME); | 69 var request = window.indexedDB.deleteDatabase(DB_NAME); |
70 request.on.success.add(expectAsync1((e) { _openDb(afterOpen); })); | 70 request.onSuccess.listen(expectAsync1((e) { _openDb(afterOpen); })); |
71 request.on.error.add(fail('delete old Db')); | 71 request.onError.listen(fail('delete old Db')); |
72 } | 72 } |
73 | 73 |
74 writeItems(int index) { | 74 writeItems(int index) { |
75 if (index < 100) { | 75 if (index < 100) { |
76 var transaction = db.transaction([STORE_NAME], 'readwrite'); | 76 var transaction = db.transaction([STORE_NAME], 'readwrite'); |
77 var request = transaction.objectStore(STORE_NAME) | 77 var request = transaction.objectStore(STORE_NAME) |
78 .put('Item $index', index); | 78 .put('Item $index', index); |
79 request.on.success.add(expectAsync1((e) { | 79 request.onSuccess.listen(expectAsync1((e) { |
80 writeItems(index + 1); | 80 writeItems(index + 1); |
81 } | 81 } |
82 )); | 82 )); |
83 request.on.error.add(fail('put')); | 83 request.onError.listen(fail('put')); |
84 } | 84 } |
85 } | 85 } |
86 | 86 |
87 setupDb() { _createAndOpenDb(() => writeItems(0)); } | 87 setupDb() { _createAndOpenDb(() => writeItems(0)); } |
88 | 88 |
89 testRange(range, expectedFirst, expectedLast) { | 89 testRange(range, expectedFirst, expectedLast) { |
90 Transaction txn = db.transaction(STORE_NAME, 'readonly'); | 90 Transaction txn = db.transaction(STORE_NAME, 'readonly'); |
91 ObjectStore objectStore = txn.objectStore(STORE_NAME); | 91 ObjectStore objectStore = txn.objectStore(STORE_NAME); |
92 Request cursorRequest = objectStore.openCursor(range); | 92 Request cursorRequest = objectStore.openCursor(range); |
93 int itemCount = 0; | 93 int itemCount = 0; |
94 int firstKey = null; | 94 int firstKey = null; |
95 int lastKey = null; | 95 int lastKey = null; |
96 cursorRequest.on.success.add(expectAsync1((e) { | 96 cursorRequest.onSuccess.listen(expectAsync1((e) { |
97 var cursor = e.target.result; | 97 var cursor = e.target.result; |
98 if (cursor != null) { | 98 if (cursor != null) { |
99 if (firstKey == null) firstKey = cursor.key; | 99 if (firstKey == null) firstKey = cursor.key; |
100 lastKey = cursor.key; | 100 lastKey = cursor.key; |
101 itemCount += 1; | 101 itemCount += 1; |
102 expect(cursor.value, 'Item ${cursor.key}'); | 102 expect(cursor.value, 'Item ${cursor.key}'); |
103 cursor.continueFunction(); | 103 cursor.continueFunction(); |
104 } else { | 104 } else { |
105 // Done | 105 // Done |
106 expect(firstKey, expectedFirst); | 106 expect(firstKey, expectedFirst); |
107 expect(lastKey, expectedLast); | 107 expect(lastKey, expectedLast); |
108 if (expectedFirst == null) { | 108 if (expectedFirst == null) { |
109 expect(itemCount, isZero); | 109 expect(itemCount, isZero); |
110 } else { | 110 } else { |
111 expect(itemCount, expectedLast - expectedFirst + 1); | 111 expect(itemCount, expectedLast - expectedFirst + 1); |
112 } | 112 } |
113 } | 113 } |
114 }, | 114 }, |
115 count: 1 + ((expectedFirst == null) ? | 115 count: 1 + ((expectedFirst == null) ? |
116 0 : (expectedLast - expectedFirst + 1)))); | 116 0 : (expectedLast - expectedFirst + 1)))); |
117 cursorRequest.on.error.add(fail('openCursor')); | 117 cursorRequest.onError.listen(fail('openCursor')); |
118 } | 118 } |
119 | 119 |
120 only1() => testRange(new KeyRange.only(55), 55, 55); | 120 only1() => testRange(new KeyRange.only(55), 55, 55); |
121 only2() => testRange(new KeyRange.only(100), null, null); | 121 only2() => testRange(new KeyRange.only(100), null, null); |
122 only3() => testRange(new KeyRange.only(-1), null, null); | 122 only3() => testRange(new KeyRange.only(-1), null, null); |
123 | 123 |
124 lower1() => testRange(new KeyRange.lowerBound(40), 40, 99); | 124 lower1() => testRange(new KeyRange.lowerBound(40), 40, 99); |
125 // OPTIONALS lower2() => testRange(new KeyRange.lowerBound(40, open: true), 41
, 99); | 125 // OPTIONALS lower2() => testRange(new KeyRange.lowerBound(40, open: true), 41
, 99); |
126 lower2() => testRange(new KeyRange.lowerBound(40, true), 41, 99); | 126 lower2() => testRange(new KeyRange.lowerBound(40, true), 41, 99); |
127 // OPTIONALS lower3() => testRange(new KeyRange.lowerBound(40, open: false), 4
0, 99); | 127 // OPTIONALS lower3() => testRange(new KeyRange.lowerBound(40, open: false), 4
0, 99); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 test('upper2', test_.upper2); | 175 test('upper2', test_.upper2); |
176 test('upper3', test_.upper3); | 176 test('upper3', test_.upper3); |
177 | 177 |
178 test('bound1', test_.bound1); | 178 test('bound1', test_.bound1); |
179 test('bound2', test_.bound2); | 179 test('bound2', test_.bound2); |
180 test('bound3', test_.bound3); | 180 test('bound3', test_.bound3); |
181 test('bound4', test_.bound4); | 181 test('bound4', test_.bound4); |
182 test('bound5', test_.bound5); | 182 test('bound5', test_.bound5); |
183 } | 183 } |
184 } | 184 } |
OLD | NEW |