OLD | NEW |
| (Empty) |
1 #library('IndexedDB3Test'); | |
2 #import('../../../../lib/unittest/unittest.dart'); | |
3 #import('../../../../lib/unittest/dom_config.dart'); | |
4 #import('dart:dom'); | |
5 #import('dart:coreimpl'); | |
6 | |
7 // Read with cursor. | |
8 | |
9 final String DB_NAME = 'Test'; | |
10 final String STORE_NAME = 'TEST'; | |
11 final String VERSION = '1'; | |
12 | |
13 class Test { | |
14 var db; | |
15 | |
16 start() { | |
17 var request = window.webkitIndexedDB.open(DB_NAME); | |
18 Expect.isNotNull(request); | |
19 request.addEventListener('success', initDb); | |
20 request.addEventListener('error', fail('open')); | |
21 } | |
22 | |
23 initDb(e) { | |
24 db = e.target.result; | |
25 // TODO. Some browsers do this the w3 way - passing the VERSION to the | |
26 // open call and listening to onversionchange. Can we feature-detect the | |
27 // difference and make it work? | |
28 var request = db.setVersion(VERSION); | |
29 request.addEventListener('success', (e) { | |
30 try { | |
31 // Nuke object store if it already exists. | |
32 db.deleteObjectStore(STORE_NAME); | |
33 } catch (IDBDatabaseException e) { } | |
34 db.createObjectStore(STORE_NAME); | |
35 writeItems(0); | |
36 }); | |
37 request.addEventListener('blocked', fail('setVersion blocked')); | |
38 request.addEventListener('error', fail('setVersion error')); | |
39 } | |
40 | |
41 writeItems(int index) { | |
42 if (index < 100) { | |
43 var transaction = db.transaction([STORE_NAME], IDBTransaction.READ_WRITE); | |
44 var request = transaction.objectStore(STORE_NAME) | |
45 .put('Item $index', index); | |
46 request.addEventListener('success', (e) { writeItems(index + 1); }); | |
47 request.addEventListener('error', fail('put')); | |
48 } else { | |
49 callbackDone(); | |
50 } | |
51 } | |
52 | |
53 fail(message) => (e) { | |
54 callbackDone(); | |
55 Expect.fail('IndexedDB failure: $message'); | |
56 }; | |
57 | |
58 readAllViaCursor() { | |
59 IDBTransaction txn = db.transaction(STORE_NAME, IDBTransaction.READ_ONLY); | |
60 IDBObjectStore objectStore = txn.objectStore(STORE_NAME); | |
61 IDBRequest cursorRequest = objectStore.openCursor(); | |
62 int itemCount = 0; | |
63 int sumKeys = 0; | |
64 int lastKey = null; | |
65 cursorRequest.addEventListener("success", (e) { | |
66 var cursor = e.target.result; | |
67 if (cursor != null) { | |
68 lastKey = cursor.key; | |
69 itemCount += 1; | |
70 sumKeys += cursor.key; | |
71 Expect.equals('Item ${cursor.key}', cursor.value); | |
72 cursor.continueFunction(); | |
73 } else { | |
74 // Done | |
75 Expect.equals(99, lastKey); | |
76 Expect.equals(100, itemCount); | |
77 Expect.equals((100 * 99) ~/ 2, sumKeys); | |
78 callbackDone(); | |
79 } | |
80 }); | |
81 cursorRequest.addEventListener('error', fail('openCursor')); | |
82 } | |
83 | |
84 readAllReversedViaCursor() { | |
85 IDBTransaction txn = db.transaction(STORE_NAME, IDBTransaction.READ_ONLY); | |
86 IDBObjectStore objectStore = txn.objectStore(STORE_NAME); | |
87 // TODO: create a IDBKeyRange(0,100) | |
88 IDBRequest cursorRequest = objectStore.openCursor(null, IDBCursor.PREV); | |
89 int itemCount = 0; | |
90 int sumKeys = 0; | |
91 int lastKey = null; | |
92 cursorRequest.addEventListener("success", (e) { | |
93 var cursor = e.target.result; | |
94 if (cursor != null) { | |
95 lastKey = cursor.key; | |
96 itemCount += 1; | |
97 sumKeys += cursor.key; | |
98 Expect.equals('Item ${cursor.key}', cursor.value); | |
99 cursor.continueFunction(); | |
100 } else { | |
101 // Done | |
102 Expect.equals(0, lastKey); // i.e. first key (scanned in reverse). | |
103 Expect.equals(100, itemCount); | |
104 Expect.equals((100 * 99) ~/ 2, sumKeys); | |
105 callbackDone(); | |
106 } | |
107 }); | |
108 cursorRequest.addEventListener('error', fail('openCursor')); | |
109 } | |
110 } | |
111 | |
112 main() { | |
113 useDomConfiguration(); | |
114 | |
115 var test = new Test(); | |
116 asyncTest('prepare', 1, test.start); | |
117 asyncTest('readAll1', 1, test.readAllViaCursor); | |
118 asyncTest('readAll2', 1, test.readAllReversedViaCursor); | |
119 } | |
OLD | NEW |