Index: tests/html/fileapi_test.dart |
diff --git a/tests/html/fileapi_test.dart b/tests/html/fileapi_test.dart |
index 079197fc88204d4df20240bd87015c49d1017098..a089a3233b56e74351fceb17bcc495a80617ddd6 100644 |
--- a/tests/html/fileapi_test.dart |
+++ b/tests/html/fileapi_test.dart |
@@ -2,11 +2,12 @@ library fileapi; |
import '../../pkg/unittest/lib/unittest.dart'; |
import '../../pkg/unittest/lib/html_individual_config.dart'; |
import 'dart:html'; |
+import 'dart:async'; |
-void fail(message) { |
- guardAsync(() { |
- expect(false, isTrue, reason: message); |
- }); |
+class FileAndDir { |
+ FileEntry file; |
+ DirectoryEntry dir; |
+ FileAndDir(this.file, this.dir); |
} |
FileSystem fs; |
@@ -21,20 +22,17 @@ main() { |
}); |
getFileSystem() { |
- window.requestFileSystem(Window.TEMPORARY, 100, |
- expectAsync1((FileSystem fileSystem) { |
- fs = fileSystem; |
- }), |
- (e) { |
- fail('Got file error: ${e.code}'); |
- }); |
+ return window.requestFileSystem(Window.TEMPORARY, 100) |
+ .then((FileSystem fileSystem) { |
+ fs = fileSystem; |
+ }); |
} |
group('unsupported_throws', () { |
test('requestFileSystem', () { |
var expectation = FileSystem.supported ? returnsNormally : throws; |
expect(() { |
- window.requestFileSystem(Window.TEMPORARY, 100, (_) {}, (_) {}); |
+ window.requestFileSystem(Window.TEMPORARY, 100); |
}, expectation); |
}); |
}); |
@@ -44,27 +42,21 @@ main() { |
test('getFileSystem', getFileSystem); |
test('directoryDoesntExist', () { |
- fs.root.getDirectory( |
+ return fs.root.getDirectory( |
'directory2', |
- options: {}, |
- successCallback: (e) { |
- fail('Should not be reached'); |
- }, |
- errorCallback: expectAsync1((FileError e) { |
- expect(e.code, equals(FileError.NOT_FOUND_ERR)); |
- })); |
+ options: {}) |
+ .catchError((e) { |
+ expect(e.error.code, equals(FileError.NOT_FOUND_ERR)); |
+ }, test: (e) => e is FileError); |
}); |
test('directoryCreate', () { |
- fs.root.getDirectory( |
+ return fs.root.getDirectory( |
'directory3', |
- options: {'create': true}, |
- successCallback: expectAsync1((DirectoryEntry e) { |
- expect(e.name, equals('directory3')); |
- }), |
- errorCallback: (e) { |
- fail('Got file error: ${e.code}'); |
- }); |
+ options: {'create': true}) |
+ .then((DirectoryEntry e) { |
+ expect(e.name, equals('directory3')); |
+ }); |
}); |
} |
}); |
@@ -74,35 +66,137 @@ main() { |
test('getFileSystem', getFileSystem); |
test('fileDoesntExist', () { |
- fs.root.getFile( |
+ return fs.root.getFile( |
'file2', |
- options: {}, |
- successCallback: (e) { |
- fail('Should not be reached'); |
- }, |
- errorCallback: expectAsync1((FileError e) { |
- expect(e.code, equals(FileError.NOT_FOUND_ERR)); |
- })); |
+ options: {}) |
+ .catchError((e) { |
+ expect(e.error.code, equals(FileError.NOT_FOUND_ERR)); |
+ }, test: (e) => e is FileError); |
}); |
test('fileCreate', () { |
- fs.root.getFile( |
+ return fs.root.getFile( |
'file4', |
- options: {'create': true}, |
- successCallback: expectAsync1((FileEntry e) { |
- expect(e.name, equals('file4')); |
- expect(e.isFile, isTrue); |
- |
- e.getMetadata(expectAsync1((Metadata metadata) { |
- var changeTime = metadata.modificationTime; |
- expect(new DateTime.now().difference(changeTime).inSeconds, |
- lessThan(60)); |
- })); |
- }), |
- errorCallback: (e) { |
- fail('Got file error: ${e.code}'); |
+ options: {'create': true}) |
+ .then((FileEntry e) { |
+ expect(e.name, equals('file4')); |
+ expect(e.isFile, isTrue); |
+ return e.getMetadata(); |
+ }).then((Metadata metadata) { |
+ var changeTime = metadata.modificationTime; |
+ expect(new DateTime.now().difference(changeTime).inSeconds, |
+ lessThan(60)); |
+ expect(metadata.size, equals(0)); |
+ }); |
+ }); |
+ } |
+ }); |
+ |
+ // Do the boilerplate to get several files and directories created to then |
+ // test the functions that use those items. |
+ Future doDirSetup() { |
+ return fs.root.getFile( |
+ 'file4', |
+ options: {'create': true}) |
+ .then((FileEntry file) { |
+ return fs.root.getDirectory( |
+ 'directory3', |
+ options: {'create': true}) |
+ .then((DirectoryEntry dir) { |
+ return new Future.immediate(new FileAndDir(file, dir)); |
}); |
}); |
+ } |
+ |
+ group('directoryReader', () { |
+ if (FileSystem.supported) { |
+ test('getFileSystem', getFileSystem); |
+ |
+ test('readEntries', () { |
+ return doDirSetup() |
+ .then((fileAndDir) { |
+ var reader = fileAndDir.dir.createReader(); |
+ return reader.readEntries(); |
+ }).then((entries) { |
+ expect(entries is List, true); |
+ }); |
+ }); |
+ } |
+ }); |
+ |
+ group('entry', () { |
+ if (FileSystem.supported) { |
+ test('getFileSystem', getFileSystem); |
+ |
+ test('copyTo', () { |
+ return doDirSetup() |
+ .then((fileAndDir) { |
+ return fileAndDir.file.copyTo(fileAndDir.dir, name: 'copiedFile'); |
+ }).then((entry) { |
+ expect(entry.isFile, true); |
+ expect(entry.name, 'copiedFile'); |
+ }); |
+ }); |
+ |
+ test('getParent', () { |
+ return doDirSetup() |
+ .then((fileAndDir) { |
+ return fileAndDir.file.getParent(); |
+ }).then((entry) { |
+ expect(entry.name, ''); |
+ expect(entry.isFile, false); |
+ }); |
+ }); |
+ |
+ test('moveTo', () { |
+ return doDirSetup() |
+ .then((fileAndDir) { |
+ return fileAndDir.file.moveTo(fileAndDir.dir, name: 'movedFile'); |
+ }).then((entry) { |
+ expect(entry.name, 'movedFile'); |
+ expect(entry.fullPath, '/directory3/movedFile'); |
+ return fs.root.getFile('file4', options: {'create': false}); |
+ }).catchError((e) { |
+ expect(e.error.code, equals(FileError.NOT_FOUND_ERR)); |
+ }, test: (e) => e is FileError); |
+ }); |
+ |
+ test('remove', () { |
+ return doDirSetup() |
+ .then((fileAndDir) { |
+ return fileAndDir.file.remove().then((_) {}); |
+ }); |
+ }); |
+ } |
+ }); |
+ |
+ group('fileEntry', () { |
+ if (FileSystem.supported) { |
+ test('getFileSystem', getFileSystem); |
+ |
+ test('createWriter', () { |
+ return doDirSetup() |
+ .then((fileAndDir) { |
+ return fileAndDir.file.createWriter(); |
+ }).then((writer) { |
+ expect(writer.position, 0); |
+ expect(writer.readyState, FileWriter.INIT); |
+ expect(writer.length, 0); |
+ }); |
+ }); |
+ |
+ test('file', () { |
+ return doDirSetup() |
+ .then((fileAndDir) { |
+ return fileAndDir.file.file() |
+ .then((fileObj) { |
+ expect(fileObj.name, fileAndDir.file.name); |
+ expect(fileObj.relativePath, ''); |
+ expect(new DateTime.now().difference( |
+ fileObj.lastModifiedDate).inSeconds, lessThan(60)); |
+ }); |
+ }); |
+ }); |
} |
}); |
} |