OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import "dart:async"; |
| 6 import "dart:io"; |
| 7 |
| 8 import "package:async_helper/async_helper.dart"; |
| 9 import "package:expect/expect.dart"; |
| 10 |
| 11 |
| 12 void testWatchCreateFile() { |
| 13 var dir = new Directory('').createTempSync(); |
| 14 var file = new File(dir.path + '/file'); |
| 15 |
| 16 var watcher = dir.watch(); |
| 17 |
| 18 asyncStart(); |
| 19 var sub; |
| 20 sub = watcher.listen((event) { |
| 21 if (event is FileSystemCreateEvent && |
| 22 event.path.endsWith('file')) { |
| 23 asyncEnd(); |
| 24 sub.cancel(); |
| 25 dir.deleteSync(recursive: true); |
| 26 } |
| 27 }, onError: (e) { |
| 28 dir.deleteSync(recursive: true); |
| 29 throw e; |
| 30 }); |
| 31 |
| 32 file.createSync(); |
| 33 } |
| 34 |
| 35 |
| 36 void testWatchModifyFile() { |
| 37 var dir = new Directory('').createTempSync(); |
| 38 var file = new File(dir.path + '/file'); |
| 39 file.createSync(); |
| 40 |
| 41 var watcher = dir.watch(); |
| 42 |
| 43 asyncStart(); |
| 44 var sub; |
| 45 sub = watcher.listen((event) { |
| 46 if (event is FileSystemModifyEvent) { |
| 47 Expect.isTrue(event.path.endsWith('file')); |
| 48 sub.cancel(); |
| 49 asyncEnd(); |
| 50 dir.deleteSync(recursive: true); |
| 51 } |
| 52 }, onError: (e) { |
| 53 dir.deleteSync(recursive: true); |
| 54 throw e; |
| 55 }); |
| 56 |
| 57 file.writeAsStringSync('a'); |
| 58 } |
| 59 |
| 60 |
| 61 void testWatchMoveFile() { |
| 62 var dir = new Directory('').createTempSync(); |
| 63 var file = new File(dir.path + '/file'); |
| 64 file.createSync(); |
| 65 |
| 66 var watcher = dir.watch(); |
| 67 |
| 68 asyncStart(); |
| 69 var sub; |
| 70 sub = watcher.listen((event) { |
| 71 if (event is FileSystemMoveEvent) { |
| 72 Expect.isTrue(event.path.endsWith('file')); |
| 73 Expect.isTrue(event.destination.endsWith('file2')); |
| 74 sub.cancel(); |
| 75 asyncEnd(); |
| 76 dir.deleteSync(recursive: true); |
| 77 } |
| 78 }, onError: (e) { |
| 79 dir.deleteSync(recursive: true); |
| 80 throw e; |
| 81 }); |
| 82 |
| 83 file.renameSync(dir.path + '/file2'); |
| 84 } |
| 85 |
| 86 |
| 87 void testWatchDeleteFile() { |
| 88 var dir = new Directory('').createTempSync(); |
| 89 var file = new File(dir.path + '/file'); |
| 90 file.createSync(); |
| 91 |
| 92 var watcher = dir.watch(); |
| 93 |
| 94 asyncStart(); |
| 95 var sub; |
| 96 sub = watcher.listen((event) { |
| 97 if (event is FileSystemDeleteEvent) { |
| 98 Expect.isTrue(event.path.endsWith('file')); |
| 99 sub.cancel(); |
| 100 asyncEnd(); |
| 101 dir.deleteSync(recursive: true); |
| 102 } |
| 103 }, onDone: () { |
| 104 Expect.isTrue(gotEvent); |
| 105 }, onError: (e) { |
| 106 dir.deleteSync(recursive: true); |
| 107 throw e; |
| 108 }); |
| 109 |
| 110 file.deleteSync(); |
| 111 } |
| 112 |
| 113 |
| 114 void testWatchOnlyModifyFile() { |
| 115 var dir = new Directory('').createTempSync(); |
| 116 var file = new File(dir.path + '/file'); |
| 117 |
| 118 var watcher = dir.watch(events: FileSystemEvent.MODIFY); |
| 119 |
| 120 asyncStart(); |
| 121 var sub; |
| 122 sub = watcher.listen((event) { |
| 123 Expect.isTrue(event is FileSystemModifyEvent); |
| 124 Expect.isTrue(event.path.endsWith('file')); |
| 125 sub.cancel(); |
| 126 asyncEnd(); |
| 127 dir.deleteSync(recursive: true); |
| 128 }, onError: (e) { |
| 129 dir.deleteSync(recursive: true); |
| 130 throw e; |
| 131 }); |
| 132 |
| 133 file.createSync(); |
| 134 file.writeAsStringSync('a'); |
| 135 } |
| 136 |
| 137 |
| 138 void testMultipleEvents() { |
| 139 var dir = new Directory('').createTempSync(); |
| 140 var file = new File(dir.path + '/file'); |
| 141 var file2 = new File(dir.path + '/file2'); |
| 142 |
| 143 var watcher = dir.watch(); |
| 144 |
| 145 asyncStart(); |
| 146 int state = 0; |
| 147 var sub; |
| 148 sub = watcher.listen((event) { |
| 149 switch (state) { |
| 150 case 0: |
| 151 Expect.isTrue(event is FileSystemCreateEvent); |
| 152 state = 1; |
| 153 break; |
| 154 |
| 155 case 1: |
| 156 if (event is FileSystemCreateEvent) break; |
| 157 Expect.isTrue(event is FileSystemModifyEvent); |
| 158 state = 2; |
| 159 break; |
| 160 |
| 161 case 2: |
| 162 if (event is FileSystemModifyEvent) break; |
| 163 Expect.isTrue(event is FileSystemMoveEvent); |
| 164 state = 3; |
| 165 break; |
| 166 |
| 167 case 3: |
| 168 if (event is FileSystemMoveEvent) break; |
| 169 Expect.isTrue(event is FileSystemDeleteEvent); |
| 170 sub.cancel(); |
| 171 asyncEnd(); |
| 172 dir.deleteSync(); |
| 173 break; |
| 174 } |
| 175 }); |
| 176 |
| 177 file.createSync(); |
| 178 file.writeAsStringSync('a'); |
| 179 file.renameSync(file2.path); |
| 180 file2.deleteSync(); |
| 181 } |
| 182 |
| 183 |
| 184 void testWatchRecursive() { |
| 185 var dir = new Directory('').createTempSync(); |
| 186 if (Platform.isLinux) { |
| 187 Expect.throws(() => dir.watch(recursive: true)); |
| 188 return; |
| 189 } |
| 190 var dir2 = new Directory(dir.path + '/dir'); |
| 191 dir2.createSync(); |
| 192 var file = new File(dir.path + '/dir/file'); |
| 193 |
| 194 var watcher = dir.watch(recursive: true); |
| 195 |
| 196 asyncStart(); |
| 197 var sub; |
| 198 sub = watcher.listen((event) { |
| 199 if (event.path.endsWith('file')) { |
| 200 sub.cancel(); |
| 201 asyncEnd(); |
| 202 dir.deleteSync(recursive: true); |
| 203 } |
| 204 }, onError: (e) { |
| 205 dir.deleteSync(recursive: true); |
| 206 throw e; |
| 207 }); |
| 208 |
| 209 file.createSync(); |
| 210 } |
| 211 |
| 212 |
| 213 void testWatchNonRecursive() { |
| 214 var dir = new Directory('').createTempSync(); |
| 215 var dir2 = new Directory(dir.path + '/dir'); |
| 216 dir2.createSync(); |
| 217 var file = new File(dir.path + '/dir/file'); |
| 218 |
| 219 var watcher = dir.watch(recursive: false); |
| 220 |
| 221 asyncStart(); |
| 222 var sub; |
| 223 sub = watcher.listen((event) { |
| 224 if (event.path.endsWith('file')) { |
| 225 throw "File change event not expected"; |
| 226 } |
| 227 }, onError: (e) { |
| 228 dir.deleteSync(recursive: true); |
| 229 throw e; |
| 230 }); |
| 231 |
| 232 file.createSync(); |
| 233 |
| 234 new Timer(const Duration(milliseconds: 300), () { |
| 235 sub.cancel(); |
| 236 asyncEnd(); |
| 237 dir.deleteSync(recursive: true); |
| 238 }); |
| 239 } |
| 240 |
| 241 |
| 242 void main() { |
| 243 testWatchCreateFile(); |
| 244 testWatchModifyFile(); |
| 245 testWatchMoveFile(); |
| 246 testWatchDeleteFile(); |
| 247 testWatchOnlyModifyFile(); |
| 248 testMultipleEvents(); |
| 249 testWatchNonRecursive(); |
| 250 } |
OLD | NEW |