| 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 import 'dart:async'; | |
| 6 | 5 |
| 7 class FileAndDir { | 6 void fail(message) { |
| 8 FileEntry file; | 7 guardAsync(() { |
| 9 DirectoryEntry dir; | 8 expect(false, isTrue, reason: message); |
| 10 FileAndDir(this.file, this.dir); | 9 }); |
| 11 } | 10 } |
| 12 | 11 |
| 13 FileSystem fs; | 12 FileSystem fs; |
| 14 | 13 |
| 15 main() { | 14 main() { |
| 16 useHtmlIndividualConfiguration(); | 15 useHtmlIndividualConfiguration(); |
| 17 | 16 |
| 18 group('supported', () { | 17 group('supported', () { |
| 19 test('supported', () { | 18 test('supported', () { |
| 20 expect(FileSystem.supported, true); | 19 expect(FileSystem.supported, true); |
| 21 }); | 20 }); |
| 22 }); | 21 }); |
| 23 | 22 |
| 24 getFileSystem() { | 23 getFileSystem() { |
| 25 return window.requestFileSystem(Window.TEMPORARY, 100) | 24 window.requestFileSystem(Window.TEMPORARY, 100, |
| 26 .then((FileSystem fileSystem) { | 25 expectAsync1((FileSystem fileSystem) { |
| 27 fs = fileSystem; | 26 fs = fileSystem; |
| 28 }); | 27 }), |
| 28 (e) { |
| 29 fail('Got file error: ${e.code}'); |
| 30 }); |
| 29 } | 31 } |
| 30 | 32 |
| 31 group('unsupported_throws', () { | 33 group('unsupported_throws', () { |
| 32 test('requestFileSystem', () { | 34 test('requestFileSystem', () { |
| 33 var expectation = FileSystem.supported ? returnsNormally : throws; | 35 var expectation = FileSystem.supported ? returnsNormally : throws; |
| 34 expect(() { | 36 expect(() { |
| 35 window.requestFileSystem(Window.TEMPORARY, 100); | 37 window.requestFileSystem(Window.TEMPORARY, 100, (_) {}, (_) {}); |
| 36 }, expectation); | 38 }, expectation); |
| 37 }); | 39 }); |
| 38 }); | 40 }); |
| 39 | 41 |
| 40 group('getDirectory', () { | 42 group('getDirectory', () { |
| 41 if (FileSystem.supported) { | 43 if (FileSystem.supported) { |
| 42 test('getFileSystem', getFileSystem); | 44 test('getFileSystem', getFileSystem); |
| 43 | 45 |
| 44 test('directoryDoesntExist', () { | 46 test('directoryDoesntExist', () { |
| 45 return fs.root.getDirectory( | 47 fs.root.getDirectory( |
| 46 'directory2', | 48 'directory2', |
| 47 options: {}) | 49 options: {}, |
| 48 .catchError((e) { | 50 successCallback: (e) { |
| 49 expect(e.error.code, equals(FileError.NOT_FOUND_ERR)); | 51 fail('Should not be reached'); |
| 50 }, test: (e) => e is FileError); | 52 }, |
| 53 errorCallback: expectAsync1((FileError e) { |
| 54 expect(e.code, equals(FileError.NOT_FOUND_ERR)); |
| 55 })); |
| 51 }); | 56 }); |
| 52 | 57 |
| 53 test('directoryCreate', () { | 58 test('directoryCreate', () { |
| 54 return fs.root.getDirectory( | 59 fs.root.getDirectory( |
| 55 'directory3', | 60 'directory3', |
| 56 options: {'create': true}) | 61 options: {'create': true}, |
| 57 .then((DirectoryEntry e) { | 62 successCallback: expectAsync1((DirectoryEntry e) { |
| 58 expect(e.name, equals('directory3')); | 63 expect(e.name, equals('directory3')); |
| 59 }); | 64 }), |
| 65 errorCallback: (e) { |
| 66 fail('Got file error: ${e.code}'); |
| 67 }); |
| 60 }); | 68 }); |
| 61 } | 69 } |
| 62 }); | 70 }); |
| 63 | 71 |
| 64 group('getFile', () { | 72 group('getFile', () { |
| 65 if (FileSystem.supported) { | 73 if (FileSystem.supported) { |
| 66 test('getFileSystem', getFileSystem); | 74 test('getFileSystem', getFileSystem); |
| 67 | 75 |
| 68 test('fileDoesntExist', () { | 76 test('fileDoesntExist', () { |
| 69 return fs.root.getFile( | 77 fs.root.getFile( |
| 70 'file2', | 78 'file2', |
| 71 options: {}) | 79 options: {}, |
| 72 .catchError((e) { | 80 successCallback: (e) { |
| 73 expect(e.error.code, equals(FileError.NOT_FOUND_ERR)); | 81 fail('Should not be reached'); |
| 74 }, test: (e) => e is FileError); | 82 }, |
| 83 errorCallback: expectAsync1((FileError e) { |
| 84 expect(e.code, equals(FileError.NOT_FOUND_ERR)); |
| 85 })); |
| 75 }); | 86 }); |
| 76 | 87 |
| 77 test('fileCreate', () { | 88 test('fileCreate', () { |
| 78 return fs.root.getFile( | 89 fs.root.getFile( |
| 79 'file4', | 90 'file4', |
| 80 options: {'create': true}) | 91 options: {'create': true}, |
| 81 .then((FileEntry e) { | 92 successCallback: expectAsync1((FileEntry e) { |
| 82 expect(e.name, equals('file4')); | 93 expect(e.name, equals('file4')); |
| 83 expect(e.isFile, isTrue); | 94 expect(e.isFile, isTrue); |
| 84 return e.getMetadata(); | 95 |
| 85 }).then((Metadata metadata) { | 96 e.getMetadata(expectAsync1((Metadata metadata) { |
| 86 var changeTime = metadata.modificationTime; | 97 var changeTime = metadata.modificationTime; |
| 87 expect(new DateTime.now().difference(changeTime).inSeconds, | 98 expect(new DateTime.now().difference(changeTime).inSeconds, |
| 88 lessThan(60)); | 99 lessThan(60)); |
| 89 expect(metadata.size, equals(0)); | 100 })); |
| 90 }); | 101 }), |
| 102 errorCallback: (e) { |
| 103 fail('Got file error: ${e.code}'); |
| 104 }); |
| 91 }); | 105 }); |
| 92 } | 106 } |
| 93 }); | 107 }); |
| 94 | |
| 95 // Do the boilerplate to get several files and directories created to then | |
| 96 // test the functions that use those items. | |
| 97 Future doDirSetup() { | |
| 98 return fs.root.getFile( | |
| 99 'file4', | |
| 100 options: {'create': true}) | |
| 101 .then((FileEntry file) { | |
| 102 return fs.root.getDirectory( | |
| 103 'directory3', | |
| 104 options: {'create': true}) | |
| 105 .then((DirectoryEntry dir) { | |
| 106 return new Future.immediate(new FileAndDir(file, dir)); | |
| 107 }); | |
| 108 }); | |
| 109 } | |
| 110 | |
| 111 group('directoryReader', () { | |
| 112 if (FileSystem.supported) { | |
| 113 test('getFileSystem', getFileSystem); | |
| 114 | |
| 115 test('readEntries', () { | |
| 116 return doDirSetup() | |
| 117 .then((fileAndDir) { | |
| 118 var reader = fileAndDir.dir.createReader(); | |
| 119 return reader.readEntries(); | |
| 120 }).then((entries) { | |
| 121 expect(entries is List, true); | |
| 122 }); | |
| 123 }); | |
| 124 } | |
| 125 }); | |
| 126 | |
| 127 group('entry', () { | |
| 128 if (FileSystem.supported) { | |
| 129 test('getFileSystem', getFileSystem); | |
| 130 | |
| 131 test('copyTo', () { | |
| 132 return doDirSetup() | |
| 133 .then((fileAndDir) { | |
| 134 return fileAndDir.file.copyTo(fileAndDir.dir, name: 'copiedFile'); | |
| 135 }).then((entry) { | |
| 136 expect(entry.isFile, true); | |
| 137 expect(entry.name, 'copiedFile'); | |
| 138 }); | |
| 139 }); | |
| 140 | |
| 141 test('getParent', () { | |
| 142 return doDirSetup() | |
| 143 .then((fileAndDir) { | |
| 144 return fileAndDir.file.getParent(); | |
| 145 }).then((entry) { | |
| 146 expect(entry.name, ''); | |
| 147 expect(entry.isFile, false); | |
| 148 }); | |
| 149 }); | |
| 150 | |
| 151 test('moveTo', () { | |
| 152 return doDirSetup() | |
| 153 .then((fileAndDir) { | |
| 154 return fileAndDir.file.moveTo(fileAndDir.dir, name: 'movedFile'); | |
| 155 }).then((entry) { | |
| 156 expect(entry.name, 'movedFile'); | |
| 157 expect(entry.fullPath, '/directory3/movedFile'); | |
| 158 return fs.root.getFile('file4', options: {'create': false}); | |
| 159 }).catchError((e) { | |
| 160 expect(e.error.code, equals(FileError.NOT_FOUND_ERR)); | |
| 161 }, test: (e) => e is FileError); | |
| 162 }); | |
| 163 | |
| 164 test('remove', () { | |
| 165 return doDirSetup() | |
| 166 .then((fileAndDir) { | |
| 167 return fileAndDir.file.remove().then((_) {}); | |
| 168 }); | |
| 169 }); | |
| 170 } | |
| 171 }); | |
| 172 | |
| 173 group('fileEntry', () { | |
| 174 if (FileSystem.supported) { | |
| 175 test('getFileSystem', getFileSystem); | |
| 176 | |
| 177 test('createWriter', () { | |
| 178 return doDirSetup() | |
| 179 .then((fileAndDir) { | |
| 180 return fileAndDir.file.createWriter(); | |
| 181 }).then((writer) { | |
| 182 expect(writer.position, 0); | |
| 183 expect(writer.readyState, FileWriter.INIT); | |
| 184 expect(writer.length, 0); | |
| 185 }); | |
| 186 }); | |
| 187 | |
| 188 test('file', () { | |
| 189 return doDirSetup() | |
| 190 .then((fileAndDir) { | |
| 191 return fileAndDir.file.file() | |
| 192 .then((fileObj) { | |
| 193 expect(fileObj.name, fileAndDir.file.name); | |
| 194 expect(fileObj.relativePath, ''); | |
| 195 expect(new DateTime.now().difference( | |
| 196 fileObj.lastModifiedDate).inSeconds, lessThan(60)); | |
| 197 }); | |
| 198 }); | |
| 199 }); | |
| 200 } | |
| 201 }); | |
| 202 } | 108 } |
| OLD | NEW |