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 | 6 |
7 import 'package:scheduled_test/scheduled_test.dart'; | 7 import 'package:scheduled_test/scheduled_test.dart'; |
8 import 'package:watcher/watcher.dart'; | 8 import 'package:watcher/watcher.dart'; |
9 | 9 |
10 import '../utils.dart'; | 10 import '../utils.dart'; |
11 | 11 |
12 sharedTests() { | 12 sharedTests() { |
13 test('does not notify for changes when there are no subscribers', () { | 13 test('does not notify for changes when there are no subscribers', () { |
14 // Note that this test doesn't rely as heavily on the test functions in | 14 // Note that this test doesn't rely as heavily on the test functions in |
15 // utils.dart because it needs to be very explicit about when the event | 15 // utils.dart because it needs to be very explicit about when the event |
16 // stream is and is not subscribed. | 16 // stream is and is not subscribed. |
17 var watcher = createWatcher(); | 17 var watcher = createWatcher(); |
18 | 18 |
19 // Subscribe to the events. | 19 // Subscribe to the events. |
20 var completer = new Completer(); | 20 var completer = new Completer(); |
21 var subscription = watcher.events.listen(wrapAsync((event) { | 21 var subscription = watcher.events.listen(wrapAsync((event) { |
22 expect(event.type, equals(ChangeType.ADD)); | 22 expect(event, isWatchEvent(ChangeType.ADD, "file.txt")); |
23 expect(event.path, endsWith("file.txt")); | |
24 completer.complete(); | 23 completer.complete(); |
25 })); | 24 })); |
26 | 25 |
27 writeFile("file.txt"); | 26 writeFile("file.txt"); |
28 | 27 |
29 // Then wait until we get an event for it. | 28 // Then wait until we get an event for it. |
30 schedule(() => completer.future); | 29 schedule(() => completer.future); |
31 | 30 |
32 // Unsubscribe. | 31 // Unsubscribe. |
33 schedule(() { | 32 schedule(() { |
34 subscription.cancel(); | 33 subscription.cancel(); |
35 }); | 34 }); |
36 | 35 |
37 // Now write a file while we aren't listening. | 36 // Now write a file while we aren't listening. |
38 writeFile("unwatched.txt"); | 37 writeFile("unwatched.txt"); |
39 | 38 |
40 // Then start listening again. | 39 // Then start listening again. |
41 schedule(() { | 40 schedule(() { |
42 completer = new Completer(); | 41 completer = new Completer(); |
43 subscription = watcher.events.listen(wrapAsync((event) { | 42 subscription = watcher.events.listen(wrapAsync((event) { |
| 43 // TODO(nweiz): Remove this when either issue 14373 or 14793 is fixed. |
| 44 // Issue 14373 means that the new [Directory.watch] will emit an event |
| 45 // for "unwatched.txt" being created, and issue 14793 means we have to |
| 46 // check the filesystem, which leads us to assume that the file has been |
| 47 // modified. |
| 48 if (Platform.isMacOS && event.path.endsWith("unwatched.txt")) { |
| 49 expect(event, isWatchEvent(ChangeType.MODIFY, "unwatched.txt")); |
| 50 return; |
| 51 } |
| 52 |
44 // We should get an event for the third file, not the one added while | 53 // We should get an event for the third file, not the one added while |
45 // we weren't subscribed. | 54 // we weren't subscribed. |
46 expect(event.type, equals(ChangeType.ADD)); | 55 expect(event, isWatchEvent(ChangeType.ADD, "added.txt")); |
47 expect(event.path, endsWith("added.txt")); | |
48 completer.complete(); | 56 completer.complete(); |
49 })); | 57 })); |
50 | 58 |
51 // Wait until the watcher is ready to dispatch events again. | 59 // Wait until the watcher is ready to dispatch events again. |
52 return watcher.ready; | 60 return watcher.ready; |
53 }); | 61 }); |
54 | 62 |
55 // And add a third file. | 63 // And add a third file. |
56 writeFile("added.txt"); | 64 writeFile("added.txt"); |
57 | 65 |
58 // Wait until we get an event for the third file. | 66 // Wait until we get an event for the third file. |
59 schedule(() => completer.future); | 67 schedule(() => completer.future); |
60 | 68 |
61 schedule(() { | 69 schedule(() { |
62 subscription.cancel(); | 70 subscription.cancel(); |
63 }); | 71 }); |
64 }); | 72 }); |
65 } | 73 } |
OLD | NEW |