OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 import 'dart:html' hide Notification, NotificationEvent; | 4 import 'dart:html' hide Notification, NotificationEvent; |
5 import 'package:unittest/unittest.dart'; | 5 import 'package:unittest/unittest.dart'; |
6 import 'package:observatory/mocks.dart'; | |
7 import 'package:observatory/models.dart' as M; | 6 import 'package:observatory/models.dart' as M; |
8 import 'package:observatory/src/elements/nav/notify.dart'; | 7 import 'package:observatory/src/elements/nav/notify.dart'; |
9 import 'package:observatory/src/elements/nav/notify_event.dart'; | 8 import 'package:observatory/src/elements/nav/notify_event.dart'; |
10 import 'package:observatory/src/elements/nav/notify_exception.dart'; | 9 import 'package:observatory/src/elements/nav/notify_exception.dart'; |
| 10 import '../../mocks.dart'; |
11 | 11 |
12 main() { | 12 main() { |
13 NavNotifyElement.tag.ensureRegistration(); | 13 NavNotifyElement.tag.ensureRegistration(); |
14 | 14 |
15 final evTag = NavNotifyEventElement.tag.name; | 15 final evTag = NavNotifyEventElement.tag.name; |
16 final exTag = NavNotifyExceptionElement.tag.name; | 16 final exTag = NavNotifyExceptionElement.tag.name; |
17 | 17 |
18 const vm = const VMRefMock(); | 18 const vm = const VMRefMock(); |
19 const isolate = const IsolateRefMock(id: 'i-id', name: 'i-name'); | 19 const isolate = const IsolateRefMock(id: 'i-id', name: 'i-name'); |
20 | 20 |
21 group('instantiation', () { | 21 group('instantiation', () { |
| 22 NotificationRepositoryMock repository; |
| 23 setUp(() { |
| 24 repository = new NotificationRepositoryMock(); |
| 25 }); |
22 test('default', () { | 26 test('default', () { |
23 final NavNotifyElement e = new NavNotifyElement( | 27 final e = new NavNotifyElement(repository); |
24 new NotificationRepositoryMock()); | |
25 expect(e, isNotNull, reason: 'element correctly created'); | 28 expect(e, isNotNull, reason: 'element correctly created'); |
26 expect(e.notifyOnPause, isTrue, reason: 'notifyOnPause is default'); | 29 expect(e.notifyOnPause, isTrue, reason: 'notifyOnPause is default'); |
27 }); | 30 }); |
28 test('notify on pause', () { | 31 test('notify on pause', () { |
29 final NavNotifyElement e = new NavNotifyElement( | 32 final e = new NavNotifyElement(repository, notifyOnPause: true); |
30 new NotificationRepositoryMock(), notifyOnPause: true); | |
31 expect(e, isNotNull, reason: 'element correctly created'); | 33 expect(e, isNotNull, reason: 'element correctly created'); |
32 expect(e.notifyOnPause, isTrue, reason: 'notifyOnPause is the same'); | 34 expect(e.notifyOnPause, isTrue, reason: 'notifyOnPause is the same'); |
33 }); | 35 }); |
34 test('do not notify on pause', () { | 36 test('do not notify on pause', () { |
35 final NavNotifyElement e = new NavNotifyElement( | 37 final e = new NavNotifyElement(repository, notifyOnPause: false); |
36 new NotificationRepositoryMock(), notifyOnPause: false); | |
37 expect(e, isNotNull, reason: 'element correctly created'); | 38 expect(e, isNotNull, reason: 'element correctly created'); |
38 expect(e.notifyOnPause, isFalse, reason: 'notifyOnPause is the same'); | 39 expect(e.notifyOnPause, isFalse, reason: 'notifyOnPause is the same'); |
39 }); | 40 }); |
40 }); | 41 }); |
41 test('is correctly listening', () async { | 42 test('is correctly listening', () async { |
42 final NotificationRepositoryMock repository = | 43 final repository = new NotificationRepositoryMock(); |
43 new NotificationRepositoryMock(); | 44 final e = new NavNotifyElement(repository); |
44 final NavNotifyElement e = new NavNotifyElement(repository); | |
45 document.body.append(e); | 45 document.body.append(e); |
46 await e.onRendered.first; | 46 await e.onRendered.first; |
47 expect(repository.hasListeners, isTrue, reason: 'is listening'); | 47 expect(repository.hasListeners, isTrue, reason: 'is listening'); |
48 e.remove(); | 48 e.remove(); |
49 await e.onRendered.first; | 49 await e.onRendered.first; |
50 expect(repository.hasListeners, isFalse, reason: 'is no more listening'); | 50 expect(repository.hasListeners, isFalse, reason: 'is no more listening'); |
51 }); | 51 }); |
52 group('elements', () { | 52 group('elements', () { |
53 test('created after attachment', () async { | 53 test('created after attachment', () async { |
54 final NotificationRepositoryMock repository = | 54 final repository = |
55 new NotificationRepositoryMock(list: [ | 55 new NotificationRepositoryMock(list: [ |
56 new ExceptionNotificationMock(exception: new Exception("ex")), | 56 new ExceptionNotificationMock(exception: new Exception("ex")), |
57 const EventNotificationMock(event: const VMUpdateEventMock(vm: vm)), | 57 const EventNotificationMock(event: const VMUpdateEventMock(vm: vm)), |
58 const EventNotificationMock(event: const VMUpdateEventMock(vm: vm)) | 58 const EventNotificationMock(event: const VMUpdateEventMock(vm: vm)) |
59 ]); | 59 ]); |
60 final NavNotifyElement e = new NavNotifyElement(repository); | 60 final e = new NavNotifyElement(repository); |
61 document.body.append(e); | 61 document.body.append(e); |
62 await e.onRendered.first; | 62 await e.onRendered.first; |
63 expect(repository.listInvoked, isTrue, reason: 'should invoke list()'); | 63 expect(repository.listInvoked, isTrue, reason: 'should invoke list()'); |
64 expect(e.children.length, isNonZero, reason: 'has elements'); | 64 expect(e.children.length, isNonZero, reason: 'has elements'); |
65 expect(e.querySelectorAll(evTag).length, equals(2)); | 65 expect(e.querySelectorAll(evTag).length, equals(2)); |
66 expect(e.querySelectorAll(exTag).length, equals(1)); | 66 expect(e.querySelectorAll(exTag).length, equals(1)); |
67 e.remove(); | 67 e.remove(); |
68 await e.onRendered.first; | 68 await e.onRendered.first; |
69 expect(e.children.length, isZero, reason: 'is empty'); | 69 expect(e.children.length, isZero, reason: 'is empty'); |
70 }); | 70 }); |
71 test('react to notifyOnPause change', () async { | 71 test('react to notifyOnPause change', () async { |
72 final NotificationRepositoryMock repository = | 72 final NotificationRepositoryMock repository = |
73 new NotificationRepositoryMock(list: [ | 73 new NotificationRepositoryMock(list: [ |
74 new ExceptionNotificationMock(exception: new Exception("ex")), | 74 new ExceptionNotificationMock(exception: new Exception("ex")), |
75 const EventNotificationMock(event: const VMUpdateEventMock()), | 75 const EventNotificationMock(event: const VMUpdateEventMock()), |
76 const EventNotificationMock( | 76 const EventNotificationMock( |
77 event: const PauseStartEventMock(isolate: isolate)) | 77 event: const PauseStartEventMock(isolate: isolate)) |
78 ]); | 78 ]); |
79 final NavNotifyElement e = new NavNotifyElement(repository, | 79 final e = new NavNotifyElement(repository, notifyOnPause: true); |
80 notifyOnPause: true); | |
81 document.body.append(e); | 80 document.body.append(e); |
82 await e.onRendered.first; | 81 await e.onRendered.first; |
83 expect(e.querySelectorAll(evTag).length, equals(2)); | 82 expect(e.querySelectorAll(evTag).length, equals(2)); |
84 expect(e.querySelectorAll(exTag).length, equals(1)); | 83 expect(e.querySelectorAll(exTag).length, equals(1)); |
85 e.notifyOnPause = false; | 84 e.notifyOnPause = false; |
86 await e.onRendered.first; | 85 await e.onRendered.first; |
87 expect(e.querySelectorAll(evTag).length, equals(1)); | 86 expect(e.querySelectorAll(evTag).length, equals(1)); |
88 expect(e.querySelectorAll(exTag).length, equals(1)); | 87 expect(e.querySelectorAll(exTag).length, equals(1)); |
89 e.notifyOnPause = true; | 88 e.notifyOnPause = true; |
90 await e.onRendered.first; | 89 await e.onRendered.first; |
91 expect(e.querySelectorAll(evTag).length, equals(2)); | 90 expect(e.querySelectorAll(evTag).length, equals(2)); |
92 expect(e.querySelectorAll(exTag).length, equals(1)); | 91 expect(e.querySelectorAll(exTag).length, equals(1)); |
93 e.remove(); | 92 e.remove(); |
94 await e.onRendered.first; | 93 await e.onRendered.first; |
95 expect(e.children.length, isZero, reason: 'is empty'); | 94 expect(e.children.length, isZero, reason: 'is empty'); |
96 }); | 95 }); |
97 test('react to update event', () async { | 96 test('react to update event', () async { |
98 final List<M.Notification> list = [ | 97 final List<M.Notification> list = [ |
99 new ExceptionNotificationMock(exception: new Exception("ex")), | 98 new ExceptionNotificationMock(exception: new Exception("ex")), |
100 const EventNotificationMock(event: const VMUpdateEventMock()), | 99 const EventNotificationMock(event: const VMUpdateEventMock()), |
101 ]; | 100 ]; |
102 final NotificationRepositoryMock repository = | 101 final repository = new NotificationRepositoryMock(list: list); |
103 new NotificationRepositoryMock(list: list); | 102 final e = new NavNotifyElement(repository, notifyOnPause: true); |
104 final NavNotifyElement e = new NavNotifyElement(repository, | |
105 notifyOnPause: true); | |
106 document.body.append(e); | 103 document.body.append(e); |
107 await e.onRendered.first; | 104 await e.onRendered.first; |
108 expect(e.querySelectorAll(evTag).length, equals(1)); | 105 expect(e.querySelectorAll(evTag).length, equals(1)); |
109 expect(e.querySelectorAll(exTag).length, equals(1)); | 106 expect(e.querySelectorAll(exTag).length, equals(1)); |
110 list.add(const EventNotificationMock( | 107 list.add(const EventNotificationMock( |
111 event: const PauseStartEventMock(isolate: isolate))); | 108 event: const PauseStartEventMock(isolate: isolate))); |
112 repository.triggerChangeEvent(); | 109 repository.triggerChangeEvent(); |
113 await e.onRendered.first; | 110 await e.onRendered.first; |
114 expect(e.querySelectorAll(evTag).length, equals(2)); | 111 expect(e.querySelectorAll(evTag).length, equals(2)); |
115 expect(e.querySelectorAll(exTag).length, equals(1)); | 112 expect(e.querySelectorAll(exTag).length, equals(1)); |
116 e.remove(); | 113 e.remove(); |
117 await e.onRendered.first; | 114 await e.onRendered.first; |
118 expect(e.children.length, isZero, reason: 'is empty'); | 115 expect(e.children.length, isZero, reason: 'is empty'); |
119 }); | 116 }); |
120 }); | 117 }); |
121 } | 118 } |
OLD | NEW |