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

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 html.window.console.log('starting');
36 var object = e.target.result; 28 // Delete any existing DBs.
29 return html.window.indexedDB.deleteDatabase(dbName);
30 },
31 (_) {
32 html.window.console.log('open');
33 return html.window.indexedDB.open(dbName, version: version,
34 onUpgradeNeeded: createObjectStore);
35 },
36 (result) {
37 html.window.console.log('write');
38 db = result;
39 var transaction = db.transaction([storeName], 'readwrite');
40 transaction.objectStore(storeName).put(value, key);
41
42 return transaction.completed;
43 },
44 (db) {
45 html.window.console.log('read');
46 var transaction = db.transaction(storeName, 'readonly');
47 return transaction.objectStore(storeName).getObject(key);
48 },
49 (object) {
50 html.window.console.log('got close');
37 db.close(); 51 db.close();
38 check(value, object); 52 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.onError.listen(fail);
47
48 transaction.onComplete.listen(expectAsync1(step2));
49 }
50
51 initDb(e) {
52 db = e.target.result;
53 if (version != db.version) {
54 // Legacy 'setVersion' upgrade protocol.
55 var request = db.setVersion('$version');
56 request.onSuccess.listen(
57 expectAsync1((e) {
58 createObjectStore(db);
59 var transaction = e.target.result;
60 transaction.onComplete.listen(expectAsync1((e) => step1()));
61 transaction.onError.listen(fail);
62 })
63 );
64 request.onError.listen(fail);
65 } else {
66 step1();
67 } 53 }
68 } 54 ]).catchError((e) {
69 55 if (db != null) {
70 openDb(e) { 56 db.close();
71 var request = html.window.indexedDB.open(dbName, version);
72 expect(request, isNotNull);
73 request.onSuccess.listen(expectAsync1(initDb));
74 request.onError.listen(fail);
75 if (request is idb.OpenDBRequest) {
76 // New upgrade protocol. Old API has no 'upgradeNeeded' and uses
77 // setVersion instead.
78 request.onUpgradeNeeded.listen((e) {
79 guardAsync(() {
80 createObjectStore(e.target.result);
81 });
82 });
83 } 57 }
84 } 58 throw e;
85 59 });
86 // Delete any existing DB. 60 }
87 var deleteRequest = html.window.indexedDB.deleteDatabase(dbName);
88 deleteRequest.onSuccess.listen(expectAsync1(openDb));
89 deleteRequest.onError.listen(fail);
90 };
91 61
92 62
93 main() { 63 main() {
94 useHtmlConfiguration(); 64 useHtmlConfiguration();
95 65
96 var obj1 = {'a': 100, 'b': 's'}; 66 var obj1 = {'a': 100, 'b': 's'};
97 var obj2 = {'x': obj1, 'y': obj1}; // DAG. 67 var obj2 = {'x': obj1, 'y': obj1}; // DAG.
98 68
99 var obj3 = {}; 69 var obj3 = {};
100 obj3['a'] = 100; 70 obj3['a'] = 100;
101 obj3['b'] = obj3; // Cycle. 71 obj3['b'] = obj3; // Cycle.
102 72
103 var obj4 = new SplayTreeMap<String, dynamic>(); // Different implementation. 73 var obj4 = new SplayTreeMap<String, dynamic>(); // Different implementation.
104 obj4['a'] = 100; 74 obj4['a'] = 100;
105 obj4['b'] = 's'; 75 obj4['b'] = 's';
106 76
107 var cyclic_list = [1, 2, 3]; 77 var cyclic_list = [1, 2, 3];
108 cyclic_list[1] = cyclic_list; 78 cyclic_list[1] = cyclic_list;
109 79
110 go(name, data) => test(name, testReadWrite(123, data, verifyGraph)); 80 go(name, data) => futureTest(name,
111 81 () => testReadWrite(123, data, verifyGraph));
82 /*
112 test('test_verifyGraph', () { 83 test('test_verifyGraph', () {
113 // Nice to know verifyGraph is working before we rely on it. 84 // Nice to know verifyGraph is working before we rely on it.
114 verifyGraph(obj4, obj4); 85 verifyGraph(obj4, obj4);
115 verifyGraph(obj1, new Map.from(obj1)); 86 verifyGraph(obj1, new Map.from(obj1));
116 verifyGraph(obj4, new Map.from(obj4)); 87 verifyGraph(obj4, new Map.from(obj4));
117 88
118 var l1 = [1,2,3]; 89 var l1 = [1,2,3];
119 var l2 = [const [1, 2, 3], const [1, 2, 3]]; 90 var l2 = [const [1, 2, 3], const [1, 2, 3]];
120 verifyGraph([l1, l1], l2); 91 verifyGraph([l1, l1], l2);
121 expect(() => verifyGraph([[1, 2, 3], [1, 2, 3]], l2), throws); 92 expect(() => verifyGraph([[1, 2, 3], [1, 2, 3]], l2), throws);
122 93
123 verifyGraph(cyclic_list, cyclic_list); 94 verifyGraph(cyclic_list, cyclic_list);
124 }); 95 });
125 96 */
126 // Don't bother with these tests if it's unsupported. 97 // Don't bother with these tests if it's unsupported.
127 // Support is tested in indexeddb_1_test 98 // Support is tested in indexeddb_1_test
128 if (idb.IdbFactory.supported) { 99 if (idb.IdbFactory.supported) {
129 go('test_simple', obj1); 100 go('test_simple', obj1);
130 go('test_DAG', obj2); 101 go('test_DAG', obj2);
131 go('test_cycle', obj3); 102 //go('test_cycle', obj3);
132 go('test_simple_splay', obj4); 103 //go('test_simple_splay', obj4);
133 go('const_array_1', const [const [1], const [2]]); 104 //go('const_array_1', const [const [1], const [2]]);
134 go('const_array_dag', const [const [1], const [1]]); 105 //go('const_array_dag', const [const [1], const [1]]);
135 go('array_deferred_copy', [1,2,3, obj3, obj3, 6]); 106 //go('array_deferred_copy', [1,2,3, obj3, obj3, 6]);
136 go('array_deferred_copy_2', [1,2,3, [4, 5, obj3], [obj3, 6]]); 107 //go('array_deferred_copy_2', [1,2,3, [4, 5, obj3], [obj3, 6]]);
137 go('cyclic_list', cyclic_list); 108 //go('cyclic_list', cyclic_list);
138 } 109 }
139 } 110 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698