OLD | NEW |
(Empty) | |
| 1 library streams_test; |
| 2 import '../../pkg/unittest/lib/unittest.dart'; |
| 3 import '../../pkg/unittest/lib/html_config.dart'; |
| 4 import 'dart:async'; |
| 5 import 'dart:html'; |
| 6 |
| 7 class StreamHelper { |
| 8 var _a; |
| 9 var _b; |
| 10 StreamHelper() { |
| 11 _a = new TextInputElement(); |
| 12 document.body.append(_a); |
| 13 _b = new TextInputElement(); |
| 14 document.body.append(_b); |
| 15 } |
| 16 |
| 17 Stream<Event> get stream => _a.onFocus; |
| 18 |
| 19 // Causes an event on a to be fired. |
| 20 void pulse() { |
| 21 _b.focus(); |
| 22 _a.focus(); |
| 23 } |
| 24 } |
| 25 |
| 26 main() { |
| 27 useHtmlConfiguration(); |
| 28 |
| 29 test('simple', () { |
| 30 var a = new TextInputElement(); |
| 31 document.body.append(a); |
| 32 |
| 33 var callCount = 0; |
| 34 a.onFocus.listen((Event e) { |
| 35 ++callCount; |
| 36 }); |
| 37 |
| 38 a.focus(); |
| 39 expect(callCount, 1); |
| 40 }); |
| 41 |
| 42 // Validates that capturing events fire on parent before child. |
| 43 test('capture', () { |
| 44 var parent = new DivElement(); |
| 45 document.body.append(parent); |
| 46 |
| 47 var child = new TextInputElement(); |
| 48 parent.append(child); |
| 49 |
| 50 var childCallCount = 0; |
| 51 var parentCallCount = 0; |
| 52 Element.focusEvent.forTarget(parent, useCapture: true).listen((Event e) { |
| 53 ++parentCallCount; |
| 54 expect(childCallCount, 0); |
| 55 }); |
| 56 |
| 57 Element.focusEvent.forTarget(child, useCapture: true).listen((Event e) { |
| 58 ++childCallCount; |
| 59 expect(parentCallCount, 1); |
| 60 }); |
| 61 |
| 62 child.focus(); |
| 63 expect(childCallCount, 1); |
| 64 expect(parentCallCount, 1); |
| 65 }); |
| 66 |
| 67 test('cancel', () { |
| 68 var helper = new StreamHelper(); |
| 69 |
| 70 var callCount = 0; |
| 71 var subscription = helper.stream.listen((_) { |
| 72 ++callCount; |
| 73 }); |
| 74 |
| 75 helper.pulse(); |
| 76 expect(callCount, 1); |
| 77 |
| 78 subscription.cancel(); |
| 79 helper.pulse(); |
| 80 expect(callCount, 1); |
| 81 |
| 82 expect(() { |
| 83 subscription.onData((_) {}); |
| 84 }, throws); |
| 85 |
| 86 expect(() { |
| 87 subscription.pause(); |
| 88 }, throws); |
| 89 |
| 90 expect(() { |
| 91 subscription.resume(); |
| 92 }, throws); |
| 93 }); |
| 94 |
| 95 test('pause/resume', () { |
| 96 var helper = new StreamHelper(); |
| 97 |
| 98 var callCount = 0; |
| 99 var subscription = helper.stream.listen((_) { |
| 100 ++callCount; |
| 101 }); |
| 102 |
| 103 helper.pulse(); |
| 104 expect(callCount, 1); |
| 105 |
| 106 subscription.pause(); |
| 107 helper.pulse(); |
| 108 expect(callCount, 1); |
| 109 |
| 110 subscription.resume(); |
| 111 helper.pulse(); |
| 112 expect(callCount, 2); |
| 113 |
| 114 var completer = new Completer<int>(); |
| 115 subscription.pause(completer.future); |
| 116 helper.pulse(); |
| 117 expect(callCount, 2); |
| 118 |
| 119 // Paused, should have no impact. |
| 120 subscription.pause(); |
| 121 helper.pulse(); |
| 122 subscription.resume(); |
| 123 helper.pulse(); |
| 124 expect(callCount, 2); |
| 125 |
| 126 completer.complete(0); |
| 127 helper.pulse(); |
| 128 expect(callCount, 3); |
| 129 |
| 130 // Not paused. |
| 131 expect(() { |
| 132 subscription.resume(); |
| 133 }, throws); |
| 134 }); |
| 135 |
| 136 test('onData', () { |
| 137 var helper = new StreamHelper(); |
| 138 |
| 139 var callCountOne = 0; |
| 140 var subscription = helper.stream.listen((_) { |
| 141 ++callCountOne; |
| 142 }); |
| 143 |
| 144 helper.pulse(); |
| 145 expect(callCountOne, 1); |
| 146 |
| 147 var callCountTwo = 0; |
| 148 subscription.onData((_) { |
| 149 ++callCountTwo; |
| 150 }); |
| 151 |
| 152 helper.pulse(); |
| 153 expect(callCountOne, 1); |
| 154 expect(callCountTwo, 1); |
| 155 }); |
| 156 |
| 157 test('null onData', () { |
| 158 var helper = new StreamHelper(); |
| 159 |
| 160 var subscription = helper.stream.listen(null); |
| 161 helper.pulse(); |
| 162 |
| 163 var callCountOne = 0; |
| 164 subscription.onData((_) { |
| 165 ++callCountOne; |
| 166 }); |
| 167 helper.pulse(); |
| 168 expect(callCountOne, 1); |
| 169 |
| 170 subscription.onData(null); |
| 171 helper.pulse(); |
| 172 expect(callCountOne, 1); |
| 173 }); |
| 174 } |
OLD | NEW |