Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(232)

Side by Side Diff: tests/html/element_test.dart

Issue 1894713002: Strong html (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: ptal Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « sdk/lib/html/html_common/filtered_element_list.dart ('k') | tests/html/events_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 ElementTest; 5 library ElementTest;
6
6 import 'package:unittest/unittest.dart'; 7 import 'package:unittest/unittest.dart';
7 import 'package:unittest/html_individual_config.dart'; 8 import 'package:unittest/html_individual_config.dart';
8 import 'dart:async'; 9 import 'dart:async';
9 import 'dart:html'; 10 import 'dart:html';
10 import 'dart:svg' as svg; 11 import 'dart:svg' as svg;
11 import 'utils.dart'; 12 import 'utils.dart';
12 13
13 expectLargeRect(Rectangle rect) { 14 expectLargeRect(Rectangle rect) {
14 expect(rect.top, 0); 15 expect(rect.top, 0);
15 expect(rect.left, 0); 16 expect(rect.left, 0);
(...skipping 24 matching lines...) Expand all
40 var isElementList = 41 var isElementList =
41 predicate((x) => x is List<Element>, 'is a List<Element>'); 42 predicate((x) => x is List<Element>, 'is a List<Element>');
42 var isElementIterable = 43 var isElementIterable =
43 predicate((x) => x is Iterable<Element>, 'is an Iterable<Element>'); 44 predicate((x) => x is Iterable<Element>, 'is an Iterable<Element>');
44 var isHeadingElement = 45 var isHeadingElement =
45 predicate((x) => x is HeadingElement, 'is a HeadingElement'); 46 predicate((x) => x is HeadingElement, 'is a HeadingElement');
46 47
47 Element makeElement() => new Element.tag('div'); 48 Element makeElement() => new Element.tag('div');
48 49
49 Element makeElementWithChildren() => 50 Element makeElementWithChildren() =>
50 new Element.html("<div><br/><img/><input/></div>"); 51 new Element.html("<div><br/><img/><input/></div>");
51 52
52 group('position', () { 53 group('position', () {
53 test('computedStyle', () { 54 test('computedStyle', () {
54 final element = document.body; 55 final element = document.body;
55 var style = element.getComputedStyle(); 56 var style = element.getComputedStyle();
56 expect(style.getPropertyValue('left'), 'auto'); 57 expect(style.getPropertyValue('left'), 'auto');
57 }); 58 });
58 59
59 test('client position synchronous', () { 60 test('client position synchronous', () {
60 final container = new Element.tag("div"); 61 final container = new Element.tag("div");
(...skipping 19 matching lines...) Expand all
80 expect(element.documentOffset.y, 8); 81 expect(element.documentOffset.y, 8);
81 container.remove(); 82 container.remove();
82 }); 83 });
83 }); 84 });
84 85
85 group('constructors', () { 86 group('constructors', () {
86 test('error', () { 87 test('error', () {
87 expect(() => new Element.html('<br/><br/>'), throwsStateError); 88 expect(() => new Element.html('<br/><br/>'), throwsStateError);
88 }); 89 });
89 90
90 test('.html has no parent', () => 91 test('.html has no parent',
91 expect(new Element.html('<br/>').parent, isNull)); 92 () => expect(new Element.html('<br/>').parent, isNull));
92 93
93 test('.html table', () { 94 test('.html table', () {
94 // http://developers.whatwg.org/tabular-data.html#tabular-data 95 // http://developers.whatwg.org/tabular-data.html#tabular-data
95 var node = new Element.html(''' 96 var node = new Element.html('''
96 <table> 97 <table>
97 <caption>Characteristics with positive and negative sides</caption> 98 <caption>Characteristics with positive and negative sides</caption>
98 <thead> 99 <thead>
99 <tr> 100 <tr>
100 <th id="n"> Negative 101 <th id="n"> Negative
101 <th> Characteristic 102 <th> Characteristic
(...skipping 17 matching lines...) Expand all
119 expect(node.tHead.rows[0].cells.length, 3); 120 expect(node.tHead.rows[0].cells.length, 3);
120 expect(node.tBodies.length, 1); 121 expect(node.tBodies.length, 1);
121 expect(node.tBodies[0].rows.length, 2); 122 expect(node.tBodies[0].rows.length, 2);
122 expect(node.tBodies[0].rows[1].cells.map((c) => c.innerHtml), 123 expect(node.tBodies[0].rows[1].cells.map((c) => c.innerHtml),
123 [' Failing\n ', ' Grade\n ', ' Passing\n']); 124 [' Failing\n ', ' Grade\n ', ' Passing\n']);
124 }); 125 });
125 126
126 test('.html caption', () { 127 test('.html caption', () {
127 var table = new TableElement(); 128 var table = new TableElement();
128 var node = table.createFragment('<caption><p>Table 1.').nodes.single; 129 var node = table.createFragment('<caption><p>Table 1.').nodes.single;
129 expect(node, predicate((x) => x is TableCaptionElement, 130 expect(
130 'is a TableCaptionElement')); 131 node,
132 predicate(
133 (x) => x is TableCaptionElement, 'is a TableCaptionElement'));
131 expect(node.tagName, 'CAPTION'); 134 expect(node.tagName, 'CAPTION');
132 expect(node.parent, isNull); 135 expect(node.parent, isNull);
133 expect(node.innerHtml, '<p>Table 1.</p>'); 136 expect(node.innerHtml, '<p>Table 1.</p>');
134 }); 137 });
135 138
136 test('.html colgroup', () { 139 test('.html colgroup', () {
137 var table = new TableElement(); 140 var table = new TableElement();
138 var node = 141 var node =
139 table.createFragment('<colgroup> <col> <col> <col>').nodes.single; 142 table.createFragment('<colgroup> <col> <col> <col>').nodes.single;
140 expect(node, predicate((x) => x is TableColElement, 143 expect(
141 'is a TableColElement')); 144 node, predicate((x) => x is TableColElement, 'is a TableColElement'));
142 expect(node.tagName, 'COLGROUP'); 145 expect(node.tagName, 'COLGROUP');
143 expect(node.parent, isNull); 146 expect(node.parent, isNull);
144 expect(node.innerHtml, ' <col> <col> <col>'); 147 expect(node.innerHtml, ' <col> <col> <col>');
145 }); 148 });
146 149
147 test('.html tbody', () { 150 test('.html tbody', () {
148 var innerHtml = '<tr><td headers="n r1">Sad</td><td>Happy</td></tr>'; 151 var innerHtml = '<tr><td headers="n r1">Sad</td><td>Happy</td></tr>';
149 var table = new TableElement(); 152 var table = new TableElement();
150 var node = table.createFragment('<tbody>$innerHtml').nodes.single; 153 var node = table.createFragment('<tbody>$innerHtml').nodes.single;
151 expect(node, predicate((x) => x is TableSectionElement, 154 expect(
152 'is a TableSectionElement')); 155 node,
156 predicate(
157 (x) => x is TableSectionElement, 'is a TableSectionElement'));
153 expect(node.tagName, 'TBODY'); 158 expect(node.tagName, 'TBODY');
154 expect(node.parent, isNull); 159 expect(node.parent, isNull);
155 expect(node.rows.length, 1); 160 expect(node.rows.length, 1);
156 expect(node.rows[0].cells.length, 2); 161 expect(node.rows[0].cells.length, 2);
157 expect(node.innerHtml, innerHtml); 162 expect(node.innerHtml, innerHtml);
158 }); 163 });
159 164
160 test('.html thead', () { 165 test('.html thead', () {
161 var innerHtml = '<tr><th id="n">Negative</th><th>Positive</th></tr>'; 166 var innerHtml = '<tr><th id="n">Negative</th><th>Positive</th></tr>';
162 var table = new TableElement(); 167 var table = new TableElement();
163 var node = table.createFragment('<thead>$innerHtml').nodes.single; 168 var node = table.createFragment('<thead>$innerHtml').nodes.single;
164 expect(node, predicate((x) => x is TableSectionElement, 169 expect(
165 'is a TableSectionElement')); 170 node,
171 predicate(
172 (x) => x is TableSectionElement, 'is a TableSectionElement'));
166 expect(node.tagName, 'THEAD'); 173 expect(node.tagName, 'THEAD');
167 expect(node.parent, isNull); 174 expect(node.parent, isNull);
168 expect(node.rows.length, 1); 175 expect(node.rows.length, 1);
169 expect(node.rows[0].cells.length, 2); 176 expect(node.rows[0].cells.length, 2);
170 expect(node.innerHtml, innerHtml); 177 expect(node.innerHtml, innerHtml);
171 }); 178 });
172 179
173 test('.html tfoot', () { 180 test('.html tfoot', () {
174 var innerHtml = '<tr><th>percentage</th><td>34.3%</td></tr>'; 181 var innerHtml = '<tr><th>percentage</th><td>34.3%</td></tr>';
175 var table = new TableElement(); 182 var table = new TableElement();
176 var node = table.createFragment('<tfoot>$innerHtml').nodes.single; 183 var node = table.createFragment('<tfoot>$innerHtml').nodes.single;
177 expect(node, predicate((x) => x is TableSectionElement, 184 expect(
178 'is a TableSectionElement')); 185 node,
186 predicate(
187 (x) => x is TableSectionElement, 'is a TableSectionElement'));
179 expect(node.tagName, 'TFOOT'); 188 expect(node.tagName, 'TFOOT');
180 expect(node.parent, isNull); 189 expect(node.parent, isNull);
181 expect(node.rows.length, 1); 190 expect(node.rows.length, 1);
182 expect(node.rows[0].cells.length, 2); 191 expect(node.rows[0].cells.length, 2);
183 expect(node.innerHtml, innerHtml); 192 expect(node.innerHtml, innerHtml);
184 }); 193 });
185 194
186 test('.html tr', () { 195 test('.html tr', () {
187 var table = new TableElement(); 196 var table = new TableElement();
188 document.body.append(table); 197 document.body.append(table);
189 var tBody = table.createTBody(); 198 var tBody = table.createTBody();
190 var node = tBody.createFragment('<tr><td>foo<td>bar').nodes.single; 199 var node = tBody.createFragment('<tr><td>foo<td>bar').nodes.single;
191 expect(node, predicate((x) => x is TableRowElement, 200 expect(
192 'is a TableRowElement')); 201 node, predicate((x) => x is TableRowElement, 'is a TableRowElement'));
193 expect(node.tagName, 'TR'); 202 expect(node.tagName, 'TR');
194 expect(node.parent, isNull); 203 expect(node.parent, isNull);
195 expect(node.cells.map((c) => c.innerHtml), ['foo', 'bar']); 204 expect(node.cells.map((c) => c.innerHtml), ['foo', 'bar']);
196 }); 205 });
197 206
198 test('.html td', () { 207 test('.html td', () {
199 var table = new TableElement(); 208 var table = new TableElement();
200 document.body.append(table); 209 document.body.append(table);
201 var tBody = table.createTBody(); 210 var tBody = table.createTBody();
202 var tRow = tBody.addRow(); 211 var tRow = tBody.addRow();
203 var node = tRow.createFragment('<td>foobar').nodes.single; 212 var node = tRow.createFragment('<td>foobar').nodes.single;
204 expect(node, predicate((x) => x is TableCellElement, 213 expect(node,
205 'is a TableCellElement')); 214 predicate((x) => x is TableCellElement, 'is a TableCellElement'));
206 expect(node.tagName, 'TD'); 215 expect(node.tagName, 'TD');
207 expect(node.parent, isNull); 216 expect(node.parent, isNull);
208 expect(node.innerHtml, 'foobar'); 217 expect(node.innerHtml, 'foobar');
209 }); 218 });
210 219
211 test('.html th', () { 220 test('.html th', () {
212 var table = new TableElement(); 221 var table = new TableElement();
213 document.body.append(table); 222 document.body.append(table);
214 var tBody = table.createTBody(); 223 var tBody = table.createTBody();
215 var tRow = tBody.addRow(); 224 var tRow = tBody.addRow();
216 var node = tRow.createFragment('<th>foobar').nodes.single; 225 var node = tRow.createFragment('<th>foobar').nodes.single;
217 expect(node, predicate((x) => x is TableCellElement, 226 expect(node,
218 'is a TableCellElement')); 227 predicate((x) => x is TableCellElement, 'is a TableCellElement'));
219 expect(node.tagName, 'TH'); 228 expect(node.tagName, 'TH');
220 expect(node.parent, isNull); 229 expect(node.parent, isNull);
221 expect(node.innerHtml, 'foobar'); 230 expect(node.innerHtml, 'foobar');
222 }); 231 });
223 232
224 test('.html can fire events', () { 233 test('.html can fire events', () {
225 var e = new Element.html('<button>aha</button>'); 234 var e = new Element.html('<button>aha</button>');
226 var gotEvent = false; 235 var gotEvent = false;
227 e.onClick.listen((_) { 236 e.onClick.listen((_) {
228 gotEvent = true; 237 gotEvent = true;
229 }); 238 });
230 e.click(); 239 e.click();
231 expect(gotEvent, isTrue, reason: 'click should have raised click event'); 240 expect(gotEvent, isTrue, reason: 'click should have raised click event');
232 }); 241 });
233 }); 242 });
234 243
235 group('eventListening', () { 244 group('eventListening', () {
236 test('streams', () { 245 test('streams', () {
237 final target = new Element.tag('div'); 246 final target = new TextAreaElement();
238 247
239 void testEvent(Stream stream, String type) { 248 void testEvent(Stream stream, String type, [createEvent(String type)]) {
240 var firedOnEvent = false; 249 var firedOnEvent = false;
241 stream.listen((e) { 250 stream.listen((e) {
242 firedOnEvent = true; 251 firedOnEvent = true;
243 }); 252 });
244 expect(firedOnEvent, isFalse); 253 expect(firedOnEvent, isFalse);
245 var event = new Event(type); 254 var event = createEvent != null ? createEvent(type) : new Event(type);
246 target.dispatchEvent(event); 255 target.dispatchEvent(event);
247 256
248 expect(firedOnEvent, isTrue); 257 expect(firedOnEvent, isTrue);
249 } 258 }
250 259
251 testEvent(target.onAbort, 'abort'); 260 testEvent(target.onAbort, 'abort');
252 testEvent(target.onBeforeCopy, 'beforecopy'); 261 testEvent(target.onBeforeCopy, 'beforecopy');
253 testEvent(target.onBeforeCut, 'beforecut'); 262 testEvent(target.onBeforeCut, 'beforecut');
254 testEvent(target.onBeforePaste, 'beforepaste'); 263 testEvent(target.onBeforePaste, 'beforepaste');
255 testEvent(target.onBlur, 'blur'); 264 testEvent(target.onBlur, 'blur');
256 testEvent(target.onChange, 'change'); 265 testEvent(target.onChange, 'change');
257 testEvent(target.onContextMenu, 'contextmenu'); 266 testEvent(
267 target.onContextMenu, 'contextmenu', (type) => new MouseEvent(type));
268 // We cannot test dispatching a true ClipboardEvent as the DOM does not
269 // provide a way to create a fake ClipboardEvent.
258 testEvent(target.onCopy, 'copy'); 270 testEvent(target.onCopy, 'copy');
259 testEvent(target.onCut, 'cut'); 271 testEvent(target.onCut, 'cut');
260 testEvent(target.onDoubleClick, 'dblclick'); 272 testEvent(target.onPaste, 'paste');
261 testEvent(target.onDrag, 'drag'); 273
262 testEvent(target.onDragEnd, 'dragend'); 274 testEvent(
263 testEvent(target.onDragEnter, 'dragenter'); 275 target.onDoubleClick, 'dblclick', (type) => new MouseEvent(type));
264 testEvent(target.onDragLeave, 'dragleave'); 276 testEvent(target.onDrag, 'drag', (type) => new MouseEvent(type));
265 testEvent(target.onDragOver, 'dragover'); 277 testEvent(target.onDragEnd, 'dragend', (type) => new MouseEvent(type));
266 testEvent(target.onDragStart, 'dragstart'); 278 testEvent(
267 testEvent(target.onDrop, 'drop'); 279 target.onDragEnter, 'dragenter', (type) => new MouseEvent(type));
280 testEvent(
281 target.onDragLeave, 'dragleave', (type) => new MouseEvent(type));
282 testEvent(target.onDragOver, 'dragover', (type) => new MouseEvent(type));
283 testEvent(
284 target.onDragStart, 'dragstart', (type) => new MouseEvent(type));
285 testEvent(target.onDrop, 'drop', (type) => new MouseEvent(type));
268 testEvent(target.onError, 'error'); 286 testEvent(target.onError, 'error');
269 testEvent(target.onFocus, 'focus'); 287 testEvent(target.onFocus, 'focus');
270 testEvent(target.onFullscreenChange, 'webkitfullscreenchange'); 288 testEvent(target.onFullscreenChange, 'webkitfullscreenchange');
271 testEvent(target.onInput, 'input'); 289 testEvent(target.onInput, 'input');
272 testEvent(target.onInvalid, 'invalid'); 290 testEvent(target.onInvalid, 'invalid');
273 testEvent(target.onKeyDown, 'keydown'); 291 testEvent(target.onKeyDown, 'keydown', (type) => new KeyboardEvent(type));
274 testEvent(target.onKeyPress, 'keypress'); 292 testEvent(
275 testEvent(target.onKeyUp, 'keyup'); 293 target.onKeyPress, 'keypress', (type) => new KeyboardEvent(type));
294 testEvent(target.onKeyUp, 'keyup', (type) => new KeyboardEvent(type));
276 testEvent(target.onLoad, 'load'); 295 testEvent(target.onLoad, 'load');
277 testEvent(target.onMouseDown, 'mousedown'); 296 testEvent(
278 testEvent(target.onMouseMove, 'mousemove'); 297 target.onMouseDown, 'mousedown', (type) => new MouseEvent(type));
279 testEvent(target.onMouseOut, 'mouseout'); 298 testEvent(
280 testEvent(target.onMouseOver, 'mouseover'); 299 target.onMouseMove, 'mousemove', (type) => new MouseEvent(type));
281 testEvent(target.onMouseUp, 'mouseup'); 300 testEvent(target.onMouseOut, 'mouseout', (type) => new MouseEvent(type));
282 testEvent(target.onPaste, 'paste'); 301 testEvent(
302 target.onMouseOver, 'mouseover', (type) => new MouseEvent(type));
303 testEvent(target.onMouseUp, 'mouseup', (type) => new MouseEvent(type));
283 testEvent(target.onReset, 'reset'); 304 testEvent(target.onReset, 'reset');
284 testEvent(target.onScroll, 'scroll'); 305 testEvent(target.onScroll, 'scroll');
285 testEvent(target.onSearch, 'search'); 306 testEvent(target.onSearch, 'search');
286 testEvent(target.onSelect, 'select'); 307 testEvent(target.onSelect, 'select');
287 testEvent(target.onSelectStart, 'selectstart'); 308 testEvent(target.onSelectStart, 'selectstart');
288 testEvent(target.onSubmit, 'submit'); 309 testEvent(target.onSubmit, 'submit');
310 // We would prefer to create new touch events for this test via
311 // new TouchEvent(null, null, null, type)
312 // but that fails on desktop browsers as touch is not enabled.
289 testEvent(target.onTouchCancel, 'touchcancel'); 313 testEvent(target.onTouchCancel, 'touchcancel');
290 testEvent(target.onTouchEnd, 'touchend'); 314 testEvent(target.onTouchEnd, 'touchend');
291 testEvent(target.onTouchLeave, 'touchleave'); 315 testEvent(target.onTouchLeave, 'touchleave');
292 testEvent(target.onTouchMove, 'touchmove'); 316 testEvent(target.onTouchMove, 'touchmove');
293 testEvent(target.onTouchStart, 'touchstart'); 317 testEvent(target.onTouchStart, 'touchstart');
294 }); 318 });
295 }); 319 });
296 320
297 group('click', () { 321 group('click', () {
298 test('clickEvent', () { 322 test('clickEvent', () {
(...skipping 13 matching lines...) Expand all
312 }); 336 });
313 e2.click(); 337 e2.click();
314 expect(firedEvent2, false); 338 expect(firedEvent2, false);
315 e2.classes.add('foo'); 339 e2.classes.add('foo');
316 e2.click(); 340 e2.click();
317 expect(firedEvent2, true); 341 expect(firedEvent2, true);
318 }); 342 });
319 }); 343 });
320 344
321 group('attributes', () { 345 group('attributes', () {
322 test('manipulation', () { 346 test('manipulation', () {
323 final element = new Element.html( 347 final element = new Element.html(
324 '''<div class="foo" style="overflow: hidden" data-foo="bar" 348 '''<div class="foo" style="overflow: hidden" data-foo="bar"
325 data-foo2="bar2" dir="rtl"> 349 data-foo2="bar2" dir="rtl">
326 </div>''', treeSanitizer: new NullTreeSanitizer()); 350 </div>''',
327 final attributes = element.attributes; 351 treeSanitizer: new NullTreeSanitizer());
328 expect(attributes['class'], 'foo'); 352 final attributes = element.attributes;
329 expect(attributes['style'], startsWith('overflow: hidden')); 353 expect(attributes['class'], 'foo');
330 expect(attributes['data-foo'], 'bar'); 354 expect(attributes['style'], startsWith('overflow: hidden'));
331 expect(attributes['data-foo2'], 'bar2'); 355 expect(attributes['data-foo'], 'bar');
332 expect(attributes.length, 5); 356 expect(attributes['data-foo2'], 'bar2');
333 expect(element.dataset.length, 2); 357 expect(attributes.length, 5);
334 element.dataset['foo'] = 'baz'; 358 expect(element.dataset.length, 2);
335 expect(element.dataset['foo'], 'baz'); 359 element.dataset['foo'] = 'baz';
336 expect(attributes['data-foo'], 'baz'); 360 expect(element.dataset['foo'], 'baz');
337 attributes['data-foo2'] = 'baz2'; 361 expect(attributes['data-foo'], 'baz');
338 expect(attributes['data-foo2'], 'baz2'); 362 attributes['data-foo2'] = 'baz2';
339 expect(element.dataset['foo2'], 'baz2'); 363 expect(attributes['data-foo2'], 'baz2');
340 expect(attributes['dir'], 'rtl'); 364 expect(element.dataset['foo2'], 'baz2');
365 expect(attributes['dir'], 'rtl');
341 366
342 final dataset = element.dataset; 367 final dataset = element.dataset;
343 dataset.remove('foo2'); 368 dataset.remove('foo2');
344 expect(attributes.length, 4); 369 expect(attributes.length, 4);
345 expect(dataset.length, 1); 370 expect(dataset.length, 1);
346 attributes.remove('style'); 371 attributes.remove('style');
347 expect(attributes.length, 3); 372 expect(attributes.length, 3);
348 dataset['foo3'] = 'baz3'; 373 dataset['foo3'] = 'baz3';
349 expect(dataset.length, 2); 374 expect(dataset.length, 2);
350 expect(attributes.length, 4); 375 expect(attributes.length, 4);
351 attributes['style'] = 'width: 300px;'; 376 attributes['style'] = 'width: 300px;';
352 expect(attributes.length, 5); 377 expect(attributes.length, 5);
353 }); 378 });
354 379
355 test('namespaces', () { 380 test('namespaces', () {
356 var element = new svg.SvgElement.svg( 381 var element =
357 '''<svg xmlns="http://www.w3.org/2000/svg" 382 new svg.SvgElement.svg('''<svg xmlns="http://www.w3.org/2000/svg"
358 xmlns:xlink="http://www.w3.org/1999/xlink"> 383 xmlns:xlink="http://www.w3.org/1999/xlink">
359 <image xlink:href="foo" data-foo="bar"/> 384 <image xlink:href="foo" data-foo="bar"/>
360 </svg>''').children[0]; 385 </svg>''').children[0];
361 386
362 var attributes = element.attributes; 387 var attributes = element.attributes;
363 expect(attributes.length, 1); 388 expect(attributes.length, 1);
364 expect(attributes['data-foo'], 'bar'); 389 expect(attributes['data-foo'], 'bar');
365 390
366 var xlinkAttrs = 391 var xlinkAttrs =
367 element.getNamespacedAttributes('http://www.w3.org/1999/xlink'); 392 element.getNamespacedAttributes('http://www.w3.org/1999/xlink');
368 expect(xlinkAttrs.length, 1); 393 expect(xlinkAttrs.length, 1);
369 expect(xlinkAttrs['href'], 'foo'); 394 expect(xlinkAttrs['href'], 'foo');
370 395
371 xlinkAttrs.remove('href'); 396 xlinkAttrs.remove('href');
372 expect(xlinkAttrs.length, 0); 397 expect(xlinkAttrs.length, 0);
373 398
374 xlinkAttrs['href'] = 'bar'; 399 xlinkAttrs['href'] = 'bar';
375 expect(xlinkAttrs['href'], 'bar'); 400 expect(xlinkAttrs['href'], 'bar');
376 401
377 var randomAttrs = element.getNamespacedAttributes('http://example.com'); 402 var randomAttrs = element.getNamespacedAttributes('http://example.com');
378 expect(randomAttrs.length, 0); 403 expect(randomAttrs.length, 0);
379 randomAttrs['href'] = 'bar'; 404 randomAttrs['href'] = 'bar';
380 expect(randomAttrs.length, 1); 405 expect(randomAttrs.length, 1);
381 }); 406 });
382 }); 407 });
383 408
384 group('children', () { 409 group('children', () {
385 test('is a subset of nodes', () { 410 test('is a subset of nodes', () {
386 var el = new Element.html("<div>Foo<br/><img/></div>"); 411 var el = new Element.html("<div>Foo<br/><img/></div>");
387 expect(el.nodes.length, 3); 412 expect(el.nodes.length, 3);
388 expect(el.children.length, 2); 413 expect(el.children.length, 2);
389 expect(el.nodes[1], el.children[0]); 414 expect(el.nodes[1], el.children[0]);
390 expect(el.nodes[2], el.children[1]); 415 expect(el.nodes[2], el.children[1]);
391 }); 416 });
(...skipping 22 matching lines...) Expand all
414 test('forEach', () { 439 test('forEach', () {
415 var els = []; 440 var els = [];
416 var el = makeElementWithChildren(); 441 var el = makeElementWithChildren();
417 el.children.forEach((n) => els.add(n)); 442 el.children.forEach((n) => els.add(n));
418 expect(els[0], isBRElement); 443 expect(els[0], isBRElement);
419 expect(els[1], isImageElement); 444 expect(els[1], isImageElement);
420 expect(els[2], isInputElement); 445 expect(els[2], isInputElement);
421 }); 446 });
422 447
423 test('where', () { 448 test('where', () {
424 var filtered = makeElementWithChildren().children. 449 var filtered =
425 where((n) => n is ImageElement); 450 makeElementWithChildren().children.where((n) => n is ImageElement);
426 expect(1, filtered.length); 451 expect(1, filtered.length);
427 expect(filtered.first, isImageElement); 452 expect(filtered.first, isImageElement);
428 expect(filtered, isElementIterable); 453 expect(filtered, isElementIterable);
429 }); 454 });
430 455
431 test('every', () { 456 test('every', () {
432 var el = makeElementWithChildren(); 457 var el = makeElementWithChildren();
433 expect(el.children.every((n) => n is Element), isTrue); 458 expect(el.children.every((n) => n is Element), isTrue);
434 expect(el.children.every((n) => n is InputElement), isFalse); 459 expect(el.children.every((n) => n is InputElement), isFalse);
435 }); 460 });
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after
757 expect(range, isElementList); 782 expect(range, isElementList);
758 expect(range[0], isImageElement); 783 expect(range[0], isImageElement);
759 expect(range[1], isInputElement); 784 expect(range[1], isInputElement);
760 }); 785 });
761 }); 786 });
762 787
763 group('eventDelegation', () { 788 group('eventDelegation', () {
764 test('matches', () { 789 test('matches', () {
765 Element clickOne = new Element.a(); 790 Element clickOne = new Element.a();
766 Element selectorOne = new Element.div() 791 Element selectorOne = new Element.div()
767 ..classes.add('selector') 792 ..classes.add('selector')
768 ..children.add(clickOne); 793 ..children.add(clickOne);
769 794
770 Element clickTwo = new Element.a(); 795 Element clickTwo = new Element.a();
771 Element selectorTwo = new Element.div() 796 Element selectorTwo = new Element.div()
772 ..classes.add('selector') 797 ..classes.add('selector')
773 ..children.add(clickTwo); 798 ..children.add(clickTwo);
774 document.body.append(selectorOne); 799 document.body.append(selectorOne);
775 document.body.append(selectorTwo); 800 document.body.append(selectorTwo);
776 801
777 document.body.onClick.matches('.selector').listen(expectAsync( 802 document.body.onClick
778 (Event event) { 803 .matches('.selector')
804 .listen(expectAsync((Event event) {
779 expect(event.currentTarget, document.body); 805 expect(event.currentTarget, document.body);
780 expect(event.target, clickOne); 806 expect(event.target, clickOne);
781 expect(event.matchingTarget, selectorOne); 807 expect(event.matchingTarget, selectorOne);
782 })); 808 }));
783 809
784 selectorOne.onClick.matches('.selector').listen(expectAsync( 810 selectorOne.onClick
785 (Event event) { 811 .matches('.selector')
812 .listen(expectAsync((Event event) {
786 expect(event.currentTarget, selectorOne); 813 expect(event.currentTarget, selectorOne);
787 expect(event.target, clickOne); 814 expect(event.target, clickOne);
788 expect(event.matchingTarget, selectorOne); 815 expect(event.matchingTarget, selectorOne);
789 })); 816 }));
790 clickOne.click(); 817 clickOne.click();
791 818
792 Element elem = new Element.div()..classes.addAll(['a', 'b']); 819 Element elem = new Element.div()..classes.addAll(['a', 'b']);
793 Element img = new Element.img() 820 Element img = new Element.img()
794 ..classes.addAll(['b', 'a', 'd']) 821 ..classes.addAll(['b', 'a', 'd'])
795 ..id = 'cookie'; 822 ..id = 'cookie';
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
889 }); 916 });
890 917
891 b.on['custom_event'].capture((_) { 918 b.on['custom_event'].capture((_) {
892 eventOrder.add('b capture'); 919 eventOrder.add('b capture');
893 }); 920 });
894 921
895 document.body.append(a); 922 document.body.append(a);
896 923
897 var event = new Event('custom_event', canBubble: true); 924 var event = new Event('custom_event', canBubble: true);
898 c.dispatchEvent(event); 925 c.dispatchEvent(event);
899 expect(eventOrder, [ 926 expect(eventOrder,
900 'a capture', 927 ['a capture', 'b capture', 'b no-capture', 'a no-capture']);
901 'b capture',
902 'b no-capture',
903 'a no-capture'
904 ]);
905 }); 928 });
906 }); 929 });
907 930
908 group('ElementList', () { 931 group('ElementList', () {
909 // Tests for methods on the DOM class 'NodeList'. 932 // Tests for methods on the DOM class 'NodeList'.
910 // 933 //
911 // There are two interesting things that are checked here from the viewpoint 934 // There are two interesting things that are checked here from the viewpoint
912 // of the dart2js implementation of a 'native' class: 935 // of the dart2js implementation of a 'native' class:
913 // 936 //
914 // 1. Some methods are implementated from by 'Object' or 'Interceptor'; 937 // 1. Some methods are implementated from by 'Object' or 'Interceptor';
915 // some of these tests simply check that a method can be called. 938 // some of these tests simply check that a method can be called.
916 // 2. Some methods are implemented by mixins. 939 // 2. Some methods are implemented by mixins.
917 940
918 ElementList<Element> makeElementList() => 941 ElementList<Element> makeElementList() =>
919 (new Element.html("<div>Foo<br/><!--baz--><br/><br/></div>")) 942 (new Element.html("<div>Foo<br/><!--baz--><br/><br/></div>"))
920 .queryAll('br'); 943 .queryAll('br');
921 944
922 test('hashCode', () { 945 test('hashCode', () {
923 var nodes = makeElementList(); 946 var nodes = makeElementList();
924 var hash = nodes.hashCode; 947 var hash = nodes.hashCode;
925 final int N = 1000; 948 final int N = 1000;
926 int matchCount = 0; 949 int matchCount = 0;
927 for (int i = 0; i < N; i++) { 950 for (int i = 0; i < N; i++) {
928 if (makeElementList().hashCode == hash) matchCount++; 951 if (makeElementList().hashCode == hash) matchCount++;
929 } 952 }
930 expect(matchCount, lessThan(N)); 953 expect(matchCount, lessThan(N));
931 }); 954 });
932 955
933 test('operator==', () { 956 test('operator==', () {
934 var a = [makeElementList(), makeElementList(), null]; 957 var a = [makeElementList(), makeElementList(), null];
935 for (int i = 0; i < a.length; i++) { 958 for (int i = 0; i < a.length; i++) {
936 for (int j = 0; j < a.length; j++) { 959 for (int j = 0; j < a.length; j++) {
937 expect(i == j, a[i] == a[j]); 960 expect(i == j, a[i] == a[j]);
938 } 961 }
939 } 962 }
940 }); 963 });
941 964
942 test('runtimeType', () { 965 test('runtimeType', () {
943 var nodes1 = makeElementList(); 966 var nodes1 = makeElementList();
944 var nodes2 = makeElementList(); 967 var nodes2 = makeElementList();
945 var type1 = nodes1.runtimeType; 968 var type1 = nodes1.runtimeType;
946 var type2 = nodes2.runtimeType; 969 var type2 = nodes2.runtimeType;
947 expect(type1 == type2, true); 970 expect(type1 == type2, true);
(...skipping 18 matching lines...) Expand all
966 expect(filtered.length, 3); 989 expect(filtered.length, 3);
967 expect(filtered[0], isBRElement); 990 expect(filtered[0], isBRElement);
968 }); 991 });
969 992
970 test('sublist', () { 993 test('sublist', () {
971 var range = makeElementList().sublist(1, 3); 994 var range = makeElementList().sublist(1, 3);
972 expect(range.length, 2); 995 expect(range.length, 2);
973 expect(range[0], isBRElement); 996 expect(range[0], isBRElement);
974 expect(range[1], isBRElement); 997 expect(range[1], isBRElement);
975 }); 998 });
976
977 }); 999 });
978 } 1000 }
OLDNEW
« no previous file with comments | « sdk/lib/html/html_common/filtered_element_list.dart ('k') | tests/html/events_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698