Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(119)

Side by Side Diff: tests/html/indexeddb_2_test.dart

Issue 12159005: Dartifying some IndexedDB APIs. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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:html' as html; 5 import 'dart:html' as html;
5 import 'dart:indexed_db' as idb; 6 import 'dart:indexed_db' as idb;
6 import 'dart:collection'; 7 import 'dart:collection';
7 import 'utils.dart'; 8 import 'utils.dart';
8 9
9 // Write and re-read Maps: simple Maps; Maps with DAGs; Maps with cycles. 10 // Write and re-read Maps: simple Maps; Maps with DAGs; Maps with cycles.
10 11
11 const String DB_NAME = 'Test'; 12 const String DB_NAME = 'Test';
12 const String STORE_NAME = 'TEST'; 13 const String STORE_NAME = 'TEST';
13 const int VERSION = 1; 14 const int VERSION = 1;
14 15
15 testReadWrite(key, value, check, 16 testReadWrite(key, value, check,
16 [dbName = DB_NAME, 17 [dbName = DB_NAME, storeName = STORE_NAME, version = VERSION]) {
17 storeName = STORE_NAME,
18 version = VERSION]) => () {
19 var db;
20 18
21 fail(e) { 19 createObjectStore(e) {
22 guardAsync(() { 20 var store = e.target.result.createObjectStore(storeName);
23 throw new Exception('IndexedDB failure');
24 });
25 }
26
27 createObjectStore(db) {
28 var store = db.createObjectStore(storeName);
29 expect(store, isNotNull); 21 expect(store, isNotNull);
30 } 22 }
31 23
32 step2(e) { 24 var db;
33 var transaction = db.transaction(storeName, 'readonly'); 25 return chainSteps([
34 var request = transaction.objectStore(storeName).getObject(key); 26 () {
35 request.onSuccess.listen(expectAsync1((e) { 27 // Delete any existing DBs.
36 var object = e.target.result; 28 return html.window.indexedDB.deleteDatabase(dbName);
29 },
30 (_) {
31 return html.window.indexedDB.open(dbName, version: version,
32 onUpgradeNeeded: createObjectStore);
33 },
34 (result) {
35 db = result;
36 var transaction = db.transaction([storeName], 'readwrite');
37 return transaction.objectStore(storeName).put(value, key);
38 },
39 (key) {
40 var transaction = db.transaction(storeName, 'readonly');
41 return transaction.objectStore(storeName).getObject(key);
42 },
43 (object) {
37 db.close(); 44 db.close();
38 check(value, object); 45 check(value, object);
39 }));
40 request.onError.listen(fail);
41 }
42
43 step1() {
44 var transaction = db.transaction([storeName], 'readwrite');
45 var request = transaction.objectStore(storeName).put(value, key);
46 request.onSuccess.listen(expectAsync1(step2));
47 request.onError.listen(fail);
48 }
49
50 initDb(e) {
51 db = e.target.result;
52 if (version != db.version) {
53 // Legacy 'setVersion' upgrade protocol.
54 var request = db.setVersion('$version');
55 request.onSuccess.listen(
56 expectAsync1((e) {
57 createObjectStore(db);
58 var transaction = e.target.result;
59 transaction.onComplete.listen(expectAsync1((e) => step1()));
60 transaction.onError.listen(fail);
61 })
62 );
63 request.onError.listen(fail);
64 } else {
65 step1();
66 } 46 }
67 } 47 ]);
68 48 }
69 openDb(e) {
70 var request = html.window.indexedDB.open(dbName, version);
71 expect(request, isNotNull);
72 request.onSuccess.listen(expectAsync1(initDb));
73 request.onError.listen(fail);
74 if (request is idb.OpenDBRequest) {
75 // New upgrade protocol. Old API has no 'upgradeNeeded' and uses
76 // setVersion instead.
77 request.onUpgradeNeeded.listen((e) {
78 guardAsync(() {
79 createObjectStore(e.target.result);
80 });
81 });
82 }
83 }
84
85 // Delete any existing DB.
86 var deleteRequest = html.window.indexedDB.deleteDatabase(dbName);
87 deleteRequest.onSuccess.listen(expectAsync1(openDb));
88 deleteRequest.onError.listen(fail);
89 };
90 49
91 50
92 main() { 51 main() {
93 useHtmlConfiguration(); 52 useHtmlConfiguration();
94 53
95 var obj1 = {'a': 100, 'b': 's'}; 54 var obj1 = {'a': 100, 'b': 's'};
96 var obj2 = {'x': obj1, 'y': obj1}; // DAG. 55 var obj2 = {'x': obj1, 'y': obj1}; // DAG.
97 56
98 var obj3 = {}; 57 var obj3 = {};
99 obj3['a'] = 100; 58 obj3['a'] = 100;
100 obj3['b'] = obj3; // Cycle. 59 obj3['b'] = obj3; // Cycle.
101 60
102 var obj4 = new SplayTreeMap<String, dynamic>(); // Different implementation. 61 var obj4 = new SplayTreeMap<String, dynamic>(); // Different implementation.
103 obj4['a'] = 100; 62 obj4['a'] = 100;
104 obj4['b'] = 's'; 63 obj4['b'] = 's';
105 64
106 var cyclic_list = [1, 2, 3]; 65 var cyclic_list = [1, 2, 3];
107 cyclic_list[1] = cyclic_list; 66 cyclic_list[1] = cyclic_list;
108 67
109 go(name, data) => test(name, testReadWrite(123, data, verifyGraph)); 68 go(name, data) => futureTest(name,
69 () => testReadWrite(123, data, verifyGraph));
110 70
111 test('test_verifyGraph', () { 71 test('test_verifyGraph', () {
112 // Nice to know verifyGraph is working before we rely on it. 72 // Nice to know verifyGraph is working before we rely on it.
113 verifyGraph(obj4, obj4); 73 verifyGraph(obj4, obj4);
114 verifyGraph(obj1, new Map.from(obj1)); 74 verifyGraph(obj1, new Map.from(obj1));
115 verifyGraph(obj4, new Map.from(obj4)); 75 verifyGraph(obj4, new Map.from(obj4));
116 76
117 var l1 = [1,2,3]; 77 var l1 = [1,2,3];
118 var l2 = [const [1, 2, 3], const [1, 2, 3]]; 78 var l2 = [const [1, 2, 3], const [1, 2, 3]];
119 verifyGraph([l1, l1], l2); 79 verifyGraph([l1, l1], l2);
120 expect(() => verifyGraph([[1, 2, 3], [1, 2, 3]], l2), throws); 80 expect(() => verifyGraph([[1, 2, 3], [1, 2, 3]], l2), throws);
121 81
122 verifyGraph(cyclic_list, cyclic_list); 82 verifyGraph(cyclic_list, cyclic_list);
123 }); 83 });
124 84
125 // Don't bother with these tests if it's unsupported. 85 // Don't bother with these tests if it's unsupported.
126 // Support is tested in indexeddb_1_test 86 // Support is tested in indexeddb_1_test
127 if (idb.IdbFactory.supported) { 87 if (idb.IdbFactory.supported) {
128 go('test_simple', obj1); 88 go('test_simple', obj1);
129 go('test_DAG', obj2); 89 go('test_DAG', obj2);
130 go('test_cycle', obj3); 90 go('test_cycle', obj3);
131 go('test_simple_splay', obj4); 91 go('test_simple_splay', obj4);
132 go('const_array_1', const [const [1], const [2]]); 92 go('const_array_1', const [const [1], const [2]]);
133 go('const_array_dag', const [const [1], const [1]]); 93 go('const_array_dag', const [const [1], const [1]]);
134 go('array_deferred_copy', [1,2,3, obj3, obj3, 6]); 94 go('array_deferred_copy', [1,2,3, obj3, obj3, 6]);
135 go('array_deferred_copy_2', [1,2,3, [4, 5, obj3], [obj3, 6]]); 95 go('array_deferred_copy_2', [1,2,3, [4, 5, obj3], [obj3, 6]]);
136 go('cyclic_list', cyclic_list); 96 go('cyclic_list', cyclic_list);
137 } 97 }
138 } 98 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698