OLD | NEW |
---|---|
1 library fileapi; | 1 library fileapi; |
2 import '../../pkg/unittest/lib/unittest.dart'; | 2 import '../../pkg/unittest/lib/unittest.dart'; |
3 import '../../pkg/unittest/lib/html_individual_config.dart'; | 3 import '../../pkg/unittest/lib/html_individual_config.dart'; |
4 import 'dart:html'; | 4 import 'dart:html'; |
5 | 5 |
6 void fail(message) { | 6 void fail(message) { |
7 guardAsync(() { | 7 guardAsync(() { |
8 expect(false, isTrue, reason: message); | 8 expect(false, isTrue, reason: message); |
9 }); | 9 }); |
10 } | 10 } |
11 | 11 |
12 FileSystem fs; | 12 FileSystem fs; |
13 | 13 |
14 main() { | 14 main() { |
15 useHtmlIndividualConfiguration(); | 15 useHtmlIndividualConfiguration(); |
16 | 16 |
17 group('supported', () { | 17 group('supported', () { |
18 test('supported', () { | 18 test('supported', () { |
19 expect(FileSystem.supported, true); | 19 expect(FileSystem.supported, true); |
20 }); | 20 }); |
21 }); | 21 }); |
22 | 22 |
23 getFileSystem() { | 23 getFileSystem() { |
24 window.requestFileSystem(Window.TEMPORARY, 100, | 24 return window.requestFileSystem(Window.TEMPORARY, 100).then( |
25 expectAsync1((FileSystem fileSystem) { | 25 (FileSystem fileSystem) { |
26 fs = fileSystem; | 26 fs = fileSystem; |
27 }), | 27 }).catchError( |
blois
2013/03/06 00:34:29
Should not need to do the catch error- it should p
Emily Fortuna
2013/03/06 21:29:18
Done.
| |
28 (e) { | 28 (e) { |
29 fail('Got file error: ${e.code}'); | 29 fail('Got file error: ${e.error.code}'); |
30 }); | 30 }); |
31 } | 31 } |
32 | 32 |
33 group('unsupported_throws', () { | 33 group('unsupported_throws', () { |
34 test('requestFileSystem', () { | 34 test('requestFileSystem', () { |
35 var expectation = FileSystem.supported ? returnsNormally : throws; | 35 var expectation = FileSystem.supported ? returnsNormally : throws; |
36 expect(() { | 36 expect(() { |
37 window.requestFileSystem(Window.TEMPORARY, 100, (_) {}, (_) {}); | 37 window.requestFileSystem(Window.TEMPORARY, 100); |
38 }, expectation); | 38 }, expectation); |
39 }); | 39 }); |
40 }); | 40 }); |
41 | 41 |
42 group('getDirectory', () { | 42 group('getDirectory', () { |
43 if (FileSystem.supported) { | 43 if (FileSystem.supported) { |
44 test('getFileSystem', getFileSystem); | 44 test('getFileSystem', getFileSystem); |
45 | 45 |
46 test('directoryDoesntExist', () { | 46 test('directoryDoesntExist', () { |
47 fs.root.getDirectory( | 47 return fs.root.getDirectory( |
48 'directory2', | 48 'directory2', |
49 options: {}, | 49 options: {}).then( |
50 successCallback: (e) { | 50 (e) { |
51 fail('Should not be reached'); | 51 fail('Should not be reached'); |
52 }, | 52 }).catchError( |
53 errorCallback: expectAsync1((FileError e) { | 53 (AsyncError e) { |
blois
2013/03/06 00:34:29
Should probably provide a test function as well an
| |
54 expect(e.code, equals(FileError.NOT_FOUND_ERR)); | 54 expect(e.error.code, equals(FileError.NOT_FOUND_ERR)); |
55 })); | 55 }); |
56 }); | 56 }); |
57 | 57 |
58 test('directoryCreate', () { | 58 test('directoryCreate', () { |
59 fs.root.getDirectory( | 59 return fs.root.getDirectory( |
60 'directory3', | 60 'directory3', |
61 options: {'create': true}, | 61 options: {'create': true}).then( |
62 successCallback: expectAsync1((DirectoryEntry e) { | 62 (DirectoryEntry e) { |
63 expect(e.name, equals('directory3')); | 63 expect(e.name, equals('directory3')); |
64 }), | 64 }).catchError( |
65 errorCallback: (e) { | 65 (e) { |
blois
2013/03/06 00:34:29
Can just ignore this error, test framework will ge
Emily Fortuna
2013/03/06 21:29:18
Done.
| |
66 fail('Got file error: ${e.code}'); | 66 fail('Got file error: ${e.error.code}'); |
67 }); | 67 }); |
68 }); | 68 }); |
69 } | 69 } |
70 }); | 70 }); |
71 | 71 |
72 group('getFile', () { | 72 group('getFile', () { |
73 if (FileSystem.supported) { | 73 if (FileSystem.supported) { |
74 test('getFileSystem', getFileSystem); | 74 test('getFileSystem', getFileSystem); |
75 | 75 |
76 test('fileDoesntExist', () { | 76 test('fileDoesntExist', () { |
77 fs.root.getFile( | 77 return fs.root.getFile( |
78 'file2', | 78 'file2', |
79 options: {}, | 79 options: {}).then( |
80 successCallback: (e) { | 80 (e) { |
81 fail('Should not be reached'); | 81 fail('Should not be reached'); |
82 }, | 82 }).catchError( |
83 errorCallback: expectAsync1((FileError e) { | 83 (AsyncError e) { |
84 expect(e.code, equals(FileError.NOT_FOUND_ERR)); | 84 expect(e.error.code, equals(FileError.NOT_FOUND_ERR)); |
blois
2013/03/06 00:34:29
Ditto for test function for catchError.
| |
85 })); | 85 }); |
86 }); | 86 }); |
87 | 87 |
88 test('fileCreate', () { | 88 test('fileCreate', () { |
89 fs.root.getFile( | 89 return fs.root.getFile( |
90 'file4', | 90 'file4', |
91 options: {'create': true}, | 91 options: {'create': true}).then( |
92 successCallback: expectAsync1((FileEntry e) { | 92 (FileEntry e) { |
93 expect(e.name, equals('file4')); | 93 expect(e.name, equals('file4')); |
94 expect(e.isFile, isTrue); | 94 expect(e.isFile, isTrue); |
95 | 95 |
96 e.getMetadata(expectAsync1((Metadata metadata) { | 96 return e.getMetadata().then((Metadata metadata) { |
blois
2013/03/06 00:34:29
sent mail with suggested style changes.
| |
97 var changeTime = metadata.modificationTime; | 97 var changeTime = metadata.modificationTime; |
98 expect(new DateTime.now().difference(changeTime).inSeconds, | 98 expect(new DateTime.now().difference(changeTime).inSeconds, |
99 lessThan(60)); | 99 lessThan(60)); |
100 })); | 100 expect(metadata.size, equals(0)); |
101 }), | 101 }); |
102 errorCallback: (e) { | 102 }).catchError( |
103 fail('Got file error: ${e.code}'); | 103 (e) { |
104 }); | 104 fail('Got file error: ${e.error.code}'); |
blois
2013/03/06 00:34:29
Don't need catchError.
Emily Fortuna
2013/03/06 21:29:18
Done.
| |
105 }); | |
105 }); | 106 }); |
106 } | 107 } |
107 }); | 108 }); |
109 | |
110 // Do the boilerplate to get several files and directories created to then | |
111 // test the functions that use those items. | |
112 Future doDirSetup(successCallback) { | |
blois
2013/03/06 00:34:29
Seems like this should have a class which holds th
| |
113 return fs.root.getFile( | |
114 'file4', | |
115 options: {'create': true}).then( | |
blois
2013/03/06 00:34:29
ditto on style changes.
| |
116 (FileEntry file) { | |
117 return fs.root.getDirectory( | |
118 'directory3', | |
119 options: {'create': true}).then( | |
120 (DirectoryEntry dir) { | |
121 return successCallback(file, dir); | |
122 }); | |
123 }); | |
124 } | |
125 | |
126 group('directoryReader', () { | |
127 if (FileSystem.supported) { | |
128 test('getFileSystem', getFileSystem); | |
129 | |
130 test('readEntries', () { | |
131 return doDirSetup((file, dir) { | |
132 var reader = dir.createReader(); | |
133 reader.readEntries().then( | |
134 (entries) { expect(entries is List, true);}) | |
135 .catchError( | |
136 (e) { | |
blois
2013/03/06 00:34:29
don't need catchError
Emily Fortuna
2013/03/06 21:29:18
Done.
| |
137 fail("Shouldn't be reached error: ${e.error.code}"); | |
138 }); | |
139 }); | |
140 }); | |
141 } | |
142 }); | |
143 | |
144 group('entry', () { | |
145 if (FileSystem.supported) { | |
146 test('getFileSystem', getFileSystem); | |
147 | |
148 test('copyTo', () { | |
149 return doDirSetup((file, dir) { | |
150 return file.copyTo(dir, name: 'copiedFile').then( | |
151 (entry) { | |
152 expect(entry.isFile, true); | |
153 expect(entry.name, 'copiedFile'); | |
154 }).catchError( | |
155 (error) { fail('CopyToFailure ${error.error.code}'); }); }); | |
156 }); | |
157 | |
158 test('getParent', () { | |
159 return doDirSetup((file, dir) { | |
160 return file.getParent().then((entry) { | |
161 expect(entry.name, ''); | |
162 expect(entry.isFile, false); | |
163 }); | |
164 }); | |
165 }); | |
166 | |
167 test('moveTo', () { | |
168 return doDirSetup((file, dir) { | |
169 return file.moveTo(dir, name: 'movedFile').then( | |
170 (entry) { | |
171 expect(entry.name, 'movedFile'); | |
172 expect(entry.fullPath, '/directory3/movedFile'); | |
173 return fs.root.getFile('file4', options: {'create': false}) | |
174 .then( | |
175 (e) { fail("File should be moved."); }) | |
176 .catchError( | |
177 (e) { | |
178 expect(e.error.code, | |
179 equals(FileError.NOT_FOUND_ERR)); | |
180 }); | |
181 }) | |
182 .catchError((e) { fail("Failed move to ${e.error.code}"); }); | |
183 }); | |
184 }); | |
185 | |
186 test('remove', () { | |
187 return doDirSetup((file, dir) { | |
188 return file.remove().then((_) {}) | |
189 .catchError((e) { fail("Failed move to ${e.error.code}"); }); | |
190 }); | |
191 }); | |
192 } | |
193 }); | |
194 | |
195 group('fileEntry', () { | |
196 if (FileSystem.supported) { | |
197 test('getFileSystem', getFileSystem); | |
198 | |
199 test('createWriter', () { | |
200 return doDirSetup((file, dir) { | |
201 return file.createWriter().then( | |
202 (writer) { | |
203 expect(writer.position, 0); | |
204 expect(writer.readyState, FileWriter.INIT); | |
205 expect(writer.length, 0); | |
206 }).catchError((e) { | |
207 fail("Create writer failed. ${e.error.code}"); | |
208 }); | |
209 }); | |
210 }); | |
211 | |
212 test('file', () { | |
213 return doDirSetup((file, dir) { | |
214 return file.file().then( | |
215 (fileObj) { | |
216 expect(fileObj.name, file.name); | |
217 expect(fileObj.relativePath, ''); | |
218 expect(new DateTime.now().difference( | |
219 fileObj.lastModifiedDate).inSeconds, lessThan(60)); | |
220 }).catchError((e) { | |
221 fail("File failed. ${e.error.code}"); | |
222 }); | |
223 }); | |
224 }); | |
225 } | |
226 }); | |
108 } | 227 } |
OLD | NEW |