| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 import 'dart:async'; | 5 import 'dart:async'; |
| 6 import 'dart:io'; | 6 import 'dart:io'; |
| 7 | 7 |
| 8 import 'package:scheduled_test/scheduled_test.dart'; | 8 import 'package:scheduled_test/scheduled_test.dart'; |
| 9 import 'package:watcher/watcher.dart'; | 9 import 'package:watcher/watcher.dart'; |
| 10 | 10 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 writeFile("a.txt", contents: "after", updateModified: false); | 83 writeFile("a.txt", contents: "after", updateModified: false); |
| 84 writeFile("b.txt", contents: "after"); | 84 writeFile("b.txt", contents: "after"); |
| 85 expectModifyEvent("b.txt"); | 85 expectModifyEvent("b.txt"); |
| 86 }); | 86 }); |
| 87 | 87 |
| 88 test('watches files in subdirectories', () { | 88 test('watches files in subdirectories', () { |
| 89 createWatcher(); | 89 createWatcher(); |
| 90 writeFile("a/b/c/d/file.txt"); | 90 writeFile("a/b/c/d/file.txt"); |
| 91 expectAddEvent("a/b/c/d/file.txt"); | 91 expectAddEvent("a/b/c/d/file.txt"); |
| 92 }); | 92 }); |
| 93 | |
| 94 test('does not notify for changes when there were no subscribers', () { | |
| 95 // Note that this test doesn't rely as heavily on the test functions in | |
| 96 // utils.dart because it needs to be very explicit about when the event | |
| 97 // stream is and is not subscribed. | |
| 98 var watcher = createWatcher(); | |
| 99 | |
| 100 // Subscribe to the events. | |
| 101 var completer = new Completer(); | |
| 102 var subscription = watcher.events.listen((event) { | |
| 103 expect(event.type, equals(ChangeType.ADD)); | |
| 104 expect(event.path, endsWith("file.txt")); | |
| 105 completer.complete(); | |
| 106 }); | |
| 107 | |
| 108 writeFile("file.txt"); | |
| 109 | |
| 110 // Then wait until we get an event for it. | |
| 111 schedule(() => completer.future); | |
| 112 | |
| 113 // Unsubscribe. | |
| 114 schedule(() { | |
| 115 subscription.cancel(); | |
| 116 }); | |
| 117 | |
| 118 // Now write a file while we aren't listening. | |
| 119 writeFile("unwatched.txt"); | |
| 120 | |
| 121 // Then start listening again. | |
| 122 schedule(() { | |
| 123 completer = new Completer(); | |
| 124 subscription = watcher.events.listen((event) { | |
| 125 // We should get an event for the third file, not the one added while | |
| 126 // we weren't subscribed. | |
| 127 expect(event.type, equals(ChangeType.ADD)); | |
| 128 expect(event.path, endsWith("added.txt")); | |
| 129 completer.complete(); | |
| 130 }); | |
| 131 }); | |
| 132 | |
| 133 // The watcher will have been cancelled and then resumed in the middle of | |
| 134 // its pause between polling loops. That means the second scan to skip | |
| 135 // what changed while we were unsubscribed won't happen until after that | |
| 136 // delay is done. Wait long enough for that to happen. | |
| 137 schedule(() => new Future.delayed(new Duration(seconds: 1))); | |
| 138 | |
| 139 // And add a third file. | |
| 140 writeFile("added.txt"); | |
| 141 | |
| 142 // Wait until we get an event for the third file. | |
| 143 schedule(() => completer.future); | |
| 144 | |
| 145 schedule(() { | |
| 146 subscription.cancel(); | |
| 147 }); | |
| 148 }); | |
| 149 | |
| 150 | |
| 151 test('ready does not complete until after subscription', () { | |
| 152 var watcher = createWatcher(waitForReady: false); | |
| 153 | |
| 154 var ready = false; | |
| 155 watcher.ready.then((_) { | |
| 156 ready = true; | |
| 157 }); | |
| 158 | |
| 159 // Should not be ready yet. | |
| 160 schedule(() { | |
| 161 expect(ready, isFalse); | |
| 162 }); | |
| 163 | |
| 164 // Subscribe to the events. | |
| 165 schedule(() { | |
| 166 var subscription = watcher.events.listen((event) {}); | |
| 167 | |
| 168 currentSchedule.onComplete.schedule(() { | |
| 169 subscription.cancel(); | |
| 170 }); | |
| 171 }); | |
| 172 | |
| 173 // Should eventually be ready. | |
| 174 schedule(() => watcher.ready); | |
| 175 | |
| 176 schedule(() { | |
| 177 expect(ready, isTrue); | |
| 178 }); | |
| 179 }); | |
| 180 | |
| 181 test('ready completes immediately when already ready', () { | |
| 182 var watcher = createWatcher(waitForReady: false); | |
| 183 | |
| 184 // Subscribe to the events. | |
| 185 schedule(() { | |
| 186 var subscription = watcher.events.listen((event) {}); | |
| 187 | |
| 188 currentSchedule.onComplete.schedule(() { | |
| 189 subscription.cancel(); | |
| 190 }); | |
| 191 }); | |
| 192 | |
| 193 // Should eventually be ready. | |
| 194 schedule(() => watcher.ready); | |
| 195 | |
| 196 // Now ready should be a future that immediately completes. | |
| 197 var ready = false; | |
| 198 schedule(() { | |
| 199 watcher.ready.then((_) { | |
| 200 ready = true; | |
| 201 }); | |
| 202 }); | |
| 203 | |
| 204 schedule(() { | |
| 205 expect(ready, isTrue); | |
| 206 }); | |
| 207 }); | |
| 208 | |
| 209 test('ready returns a future that does not complete after unsubscribing', () { | |
| 210 var watcher = createWatcher(waitForReady: false); | |
| 211 | |
| 212 // Subscribe to the events. | |
| 213 var subscription; | |
| 214 schedule(() { | |
| 215 subscription = watcher.events.listen((event) {}); | |
| 216 }); | |
| 217 | |
| 218 var ready = false; | |
| 219 | |
| 220 // Wait until ready. | |
| 221 schedule(() => watcher.ready); | |
| 222 | |
| 223 // Now unsubscribe. | |
| 224 schedule(() { | |
| 225 subscription.cancel(); | |
| 226 | |
| 227 // Track when it's ready again. | |
| 228 ready = false; | |
| 229 watcher.ready.then((_) { | |
| 230 ready = true; | |
| 231 }); | |
| 232 }); | |
| 233 | |
| 234 // Should be back to not ready. | |
| 235 schedule(() { | |
| 236 expect(ready, isFalse); | |
| 237 }); | |
| 238 }); | |
| 239 } | 93 } |
| OLD | NEW |