OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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 library bindings_test; | 5 library bindings_test; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:html'; | 8 import 'dart:html'; |
9 | 9 |
10 import 'package:observe/observe.dart'; | 10 import 'package:observe/observe.dart'; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 }); | 75 }); |
76 return completer.future; | 76 return completer.future; |
77 }); | 77 }); |
78 | 78 |
79 test('should preserve the cursor position', () { | 79 test('should preserve the cursor position', () { |
80 var model = new NotifyModel('abcde'); | 80 var model = new NotifyModel('abcde'); |
81 var template = templateBind(new Element.html( | 81 var template = templateBind(new Element.html( |
82 '<template><input id="i1" value={{x}}></template>')); | 82 '<template><input id="i1" value={{x}}></template>')); |
83 testDiv.append(template.createInstance(model, new PolymerExpressions())); | 83 testDiv.append(template.createInstance(model, new PolymerExpressions())); |
84 | 84 |
| 85 var el; |
85 return new Future(() { | 86 return new Future(() { |
86 var el = testDiv.query("#i1"); | 87 el = testDiv.query("#i1"); |
87 var subscription = el.onInput.listen(expectAsync((_) {}, count: 1)); | 88 var subscription = el.onInput.listen(expectAsync((_) {}, count: 1)); |
88 el.focus(); | 89 el.focus(); |
89 | 90 |
90 expect(el.value, 'abcde'); | 91 expect(el.value, 'abcde'); |
91 expect(model.x, 'abcde'); | 92 expect(model.x, 'abcde'); |
92 | 93 |
93 el.selectionStart = 3; | 94 el.selectionStart = 3; |
94 el.selectionEnd = 3; | 95 el.selectionEnd = 3; |
95 expect(el.selectionStart, 3); | 96 expect(el.selectionStart, 3); |
96 expect(el.selectionEnd, 3); | 97 expect(el.selectionEnd, 3); |
97 | 98 |
98 el.value = 'abc de'; | 99 el.value = 'abc de'; |
99 // Updating the input value programatically (even to the same value in | 100 // Updating the input value programatically (even to the same value in |
100 // Chrome) loses the selection position. | 101 // Chrome) loses the selection position. |
101 expect(el.selectionStart, 6); | 102 expect(el.selectionStart, 6); |
102 expect(el.selectionEnd, 6); | 103 expect(el.selectionEnd, 6); |
103 | 104 |
104 el.selectionStart = 4; | 105 el.selectionStart = 4; |
105 el.selectionEnd = 4; | 106 el.selectionEnd = 4; |
106 | 107 |
107 expect(model.x, 'abcde'); | 108 expect(model.x, 'abcde'); |
108 el.dispatchEvent(new Event('input')); | 109 el.dispatchEvent(new Event('input')); |
109 expect(model.x, 'abc de'); | 110 expect(model.x, 'abc de'); |
110 expect(el.value, 'abc de'); | 111 expect(el.value, 'abc de'); |
111 | 112 |
112 // But propagating observable values through reassign the value and | 113 // But propagating observable values through reassign the value and |
113 // selection will be preserved. | 114 // selection will be preserved. |
114 expect(el.selectionStart, 4); | 115 expect(el.selectionStart, 4); |
115 expect(el.selectionEnd, 4); | 116 expect(el.selectionEnd, 4); |
116 | |
117 subscription.cancel(); | 117 subscription.cancel(); |
| 118 }).then(_nextMicrotask).then((_) { |
| 119 // Nothing changes on the next micro task. |
| 120 expect(el.selectionStart, 4); |
| 121 expect(el.selectionEnd, 4); |
| 122 }).then((_) => window.animationFrame).then((_) { |
| 123 // ... or on the next animation frame. |
| 124 expect(el.selectionStart, 4); |
| 125 expect(el.selectionEnd, 4); |
| 126 }).then(_afterTimeout).then((_) { |
| 127 // ... or later. |
| 128 expect(el.selectionStart, 4); |
| 129 expect(el.selectionEnd, 4); |
118 }); | 130 }); |
119 }); | 131 }); |
120 | 132 |
121 | 133 |
122 test('detects changes to ObservableMap keys/values', () { | 134 test('detects changes to ObservableMap keys/values', () { |
123 var map = new ObservableMap.from({'a': 1, 'b': 2}); | 135 var map = new ObservableMap.from({'a': 1, 'b': 2}); |
124 var template = templateBind(new Element.html('<template>' | 136 var template = templateBind(new Element.html('<template>' |
125 '<template repeat="{{k in x.keys}}">{{k}}:{{x[k]}},</template>' | 137 '<template repeat="{{k in x.keys}}">{{k}}:{{x[k]}},</template>' |
126 '</template>')); | 138 '</template>')); |
127 var model = new NotifyModel(map); | 139 var model = new NotifyModel(map); |
128 testDiv.append(template.createInstance(model, new PolymerExpressions())); | 140 testDiv.append(template.createInstance(model, new PolymerExpressions())); |
129 | 141 |
130 return new Future(() { | 142 return new Future(() { |
131 expect(testDiv.text, 'a:1,b:2,'); | 143 expect(testDiv.text, 'a:1,b:2,'); |
132 map.remove('b'); | 144 map.remove('b'); |
133 map['c'] = 3; | 145 map['c'] = 3; |
134 }).then(_nextMicrotask).then((_) { | 146 }).then(_nextMicrotask).then((_) { |
135 expect(testDiv.text, 'a:1,c:3,'); | 147 expect(testDiv.text, 'a:1,c:3,'); |
136 map['a'] = 4; | 148 map['a'] = 4; |
137 }).then(_nextMicrotask).then((_) { | 149 }).then(_nextMicrotask).then((_) { |
138 expect(testDiv.text, 'a:4,c:3,'); | 150 expect(testDiv.text, 'a:4,c:3,'); |
139 }); | 151 }); |
140 }); | 152 }); |
141 }); | 153 }); |
142 }); | 154 }); |
143 | 155 |
144 _nextMicrotask(_) => new Future(() {}); | 156 _nextMicrotask(_) => new Future(() {}); |
| 157 _afterTimeout(_) => new Future.delayed(new Duration(milliseconds: 30), () {}); |
145 | 158 |
146 @reflectable | 159 @reflectable |
147 class NotifyModel extends ChangeNotifier { | 160 class NotifyModel extends ChangeNotifier { |
148 var _x; | 161 var _x; |
149 NotifyModel([this._x]); | 162 NotifyModel([this._x]); |
150 | 163 |
151 get x => _x; | 164 get x => _x; |
152 set x(value) { | 165 set x(value) { |
153 _x = notifyPropertyChange(#x, _x, value); | 166 _x = notifyPropertyChange(#x, _x, value); |
154 } | 167 } |
155 } | 168 } |
OLD | NEW |