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

Side by Side Diff: client/tests/client/html/XMLElementTests.dart

Issue 9478021: Add some sweet XML classes. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 9 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 | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 void testXMLElement() {
6 XMLElement makeElement() => new XMLElement.xml("<xml><foo/><bar/></xml>");
7
8 makeElementWithParent() {
9 final parent = new XMLElement.xml(
10 "<parent><before/><xml><foo/><bar/></xml><after/></parent>");
11 return parent.elements[1];
12 }
13
14 group('constructors', () {
15 group('.xml', () {
16 test('with a well-formed document', () {
17 final el = makeElement();
18 Expect.isTrue(el is XMLElement);
19 Expect.equals('foo', el.elements[0].tagName);
20 Expect.equals('bar', el.elements[1].tagName);
21 });
22
23 test('with too many nodes', () {
24 Expect.throws(() => new XMLElement.xml("<xml></xml>foo"),
25 (e) => e is IllegalArgumentException);
26 });
27
28 test('with a parse error', () {
29 Expect.throws(() => new XMLElement.xml("<xml></xml>>"),
30 (e) => e is IllegalArgumentException);
31 });
32
33 test('with a PARSERERROR tag', () {
34 final el = new XMLElement.xml("<xml><parsererror /></xml>");
35 Expect.equals('parsererror', el.elements[0].tagName);
36 });
37
38 test('has no parent', () =>
39 Expect.isNull(new XMLElement.xml('<foo/>').parent));
40 });
41
42 test('.tag', () {
43 final el = new XMLElement.tag('foo');
44 Expect.isTrue(el is XMLElement);
45 Expect.equals('foo', el.tagName);
46 });
47 });
48
49 // FilteredElementList is tested more thoroughly in DocumentFragmentTests.
50 group('elements', () {
51 test('filters out non-element nodes', () {
52 final el = new XMLElement.xml("<xml>1<a/><b/>2<c/>3<d/></xml>");
53 Expect.listEquals(["a", "b", "c", "d"], el.elements.map((e) => e.tagName)) ;
54 });
55
56 test('overwrites nodes when set', () {
57 final el = new XMLElement.xml("<xml>1<a/><b/>2<c/>3<d/></xml>");
58 el.elements = [new XMLElement.tag('x'), new XMLElement.tag('y')];
59 Expect.equals("<xml><x></x><y></y></xml>", el.outerHTML);
60 });
61 });
62
63 group('classes', () {
64 XMLElement makeElementWithClasses() =>
65 new XMLElement.xml("<xml class='foo bar baz'></xml>");
66
67 Set<String> makeClassSet() => makeElementWithClasses().classes;
68
69 test('affects the "class" attribute', () {
70 final el = makeElementWithClasses();
71 el.classes.add('qux');
72 Expect.setEquals(['foo', 'bar', 'baz', 'qux'],
73 el.attributes['class'].split(' '));
74 });
75
76 test('is affected by the "class" attribute', () {
77 final el = makeElementWithClasses();
78 el.attributes['class'] = 'foo qux';
79 Expect.setEquals(['foo', 'qux'], el.classes);
80 });
81
82 test('classes=', () {
83 final el = makeElementWithClasses();
84 el.classes = ['foo', 'qux'];
85 Expect.setEquals(['foo', 'qux'], el.classes);
86 Expect.setEquals(['foo', 'qux'], el.attributes['class'].split(' '));
87 });
88
89 test('toString', () {
90 Expect.setEquals(['foo', 'bar', 'baz'],
91 makeClassSet().toString().split(' '));
92 Expect.equals('', makeElement().classes.toString());
93 });
94
95 test('forEach', () {
96 final classes = <String>[];
97 makeClassSet().forEach(classes.add);
98 Expect.setEquals(['foo', 'bar', 'baz'], classes);
99 });
100
101 test('iterator', () {
102 final classes = <String>[];
103 for (var el in makeClassSet()) {
104 classes.add(el);
105 }
106 Expect.setEquals(['foo', 'bar', 'baz'], classes);
107 });
108
109 test('map', () {
110 Expect.setEquals(['FOO', 'BAR', 'BAZ'],
111 makeClassSet().map((c) => c.toUpperCase()));
112 });
113
114 test('filter', () {
115 Expect.setEquals(['bar', 'baz'],
116 makeClassSet().filter((c) => c.contains('a')));
117 });
118
119 test('every', () {
120 Expect.isTrue(makeClassSet().every((c) => c is String));
121 Expect.isFalse(
122 makeClassSet().every((c) => c.contains('a')));
123 });
124
125 test('some', () {
126 Expect.isTrue(
127 makeClassSet().some((c) => c.contains('a')));
128 Expect.isFalse(makeClassSet().some((c) => c is num));
129 });
130
131 test('isEmpty', () {
132 Expect.isFalse(makeClassSet().isEmpty());
133 Expect.isTrue(makeElement().classes.isEmpty());
134 });
135
136 test('length', () {
137 Expect.equals(3, makeClassSet().length);
138 Expect.equals(0, makeElement().classes.length);
139 });
140
141 test('contains', () {
142 Expect.isTrue(makeClassSet().contains('foo'));
143 Expect.isFalse(makeClassSet().contains('qux'));
144 });
145
146 test('add', () {
147 final classes = makeClassSet();
148 classes.add('qux');
149 Expect.setEquals(['foo', 'bar', 'baz', 'qux'], classes);
150
151 classes.add('qux');
152 final list = new List.from(classes);
153 list.sort((a, b) => a.compareTo(b));
154 Expect.listEquals(['bar', 'baz', 'foo', 'qux'], list,
155 "The class set shouldn't have duplicate elements.");
156 });
157
158 test('remove', () {
159 final classes = makeClassSet();
160 classes.remove('bar');
161 Expect.setEquals(['foo', 'baz'], classes);
162 classes.remove('qux');
163 Expect.setEquals(['foo', 'baz'], classes);
164 });
165
166 test('addAll', () {
167 final classes = makeClassSet();
168 classes.addAll(['bar', 'qux', 'bip']);
169 Expect.setEquals(['foo', 'bar', 'baz', 'qux', 'bip'], classes);
170 });
171
172 test('removeAll', () {
173 final classes = makeClassSet();
174 classes.removeAll(['bar', 'baz', 'qux']);
175 Expect.setEquals(['foo'], classes);
176 });
177
178 test('isSubsetOf', () {
179 final classes = makeClassSet();
180 Expect.isTrue(classes.isSubsetOf(['foo', 'bar', 'baz']));
181 Expect.isTrue(classes.isSubsetOf(['foo', 'bar', 'baz', 'qux']));
182 Expect.isFalse(classes.isSubsetOf(['foo', 'bar', 'qux']));
183 });
184
185 test('containsAll', () {
186 final classes = makeClassSet();
187 Expect.isTrue(classes.containsAll(['foo', 'baz']));
188 Expect.isFalse(classes.containsAll(['foo', 'qux']));
189 });
190
191 test('intersection', () {
192 final classes = makeClassSet();
193 Expect.setEquals(['foo', 'baz'],
194 classes.intersection(['foo', 'qux', 'baz']));
195 });
196
197 test('clear', () {
198 final classes = makeClassSet();
199 classes.clear();
200 Expect.setEquals([], classes);
201 });
202 });
203
204 test("no-op methods don't throw errors", () {
205 final el = makeElement();
206 el.on.click.add((e) => null);
207 el.blur();
208 el.focus();
209 el.scrollByLines(2);
210 el.scrollByPages(2);
211 el.scrollIntoView();
212 });
213
214 group('properties that map to attributes', () {
215 group('contentEditable', () {
216 test('get', () {
217 final el = makeElement();
218 Expect.equals('inherit', el.contentEditable);
219 el.attributes['contentEditable'] = 'foo';
220 Expect.equals('foo', el.contentEditable);
221 });
222
223 test('set', () {
224 final el = makeElement();
225 el.contentEditable = 'foo';
226 Expect.equals('foo', el.attributes['contentEditable']);
227 });
228
229 test('isContentEditable', () {
230 final el = makeElement();
231 Expect.isFalse(el.isContentEditable);
232 el.contentEditable = 'true';
233 Expect.isFalse(el.isContentEditable);
234 });
235 });
236
237 group('draggable', () {
238 test('get', () {
239 final el = makeElement();
240 Expect.isFalse(el.draggable);
241 el.attributes['draggable'] = 'true';
242 Expect.isTrue(el.draggable);
243 el.attributes['draggable'] = 'foo';
244 Expect.isFalse(el.draggable);
245 });
246
247 test('set', () {
248 final el = makeElement();
249 el.draggable = true;
250 Expect.equals('true', el.attributes['draggable']);
251 el.draggable = false;
252 Expect.equals('false', el.attributes['draggable']);
253 });
254 });
255
256 group('spellcheck', () {
257 test('get', () {
258 final el = makeElement();
259 Expect.isFalse(el.spellcheck);
260 el.attributes['spellcheck'] = 'true';
261 Expect.isTrue(el.spellcheck);
262 el.attributes['spellcheck'] = 'foo';
263 Expect.isFalse(el.spellcheck);
264 });
265
266 test('set', () {
267 final el = makeElement();
268 el.spellcheck = true;
269 Expect.equals('true', el.attributes['spellcheck']);
270 el.spellcheck = false;
271 Expect.equals('false', el.attributes['spellcheck']);
272 });
273 });
274
275 group('hidden', () {
276 test('get', () {
277 final el = makeElement();
278 Expect.isFalse(el.hidden);
279 el.attributes['hidden'] = '';
280 Expect.isTrue(el.hidden);
281 });
282
283 test('set', () {
284 final el = makeElement();
285 el.hidden = true;
286 Expect.equals('', el.attributes['hidden']);
287 el.hidden = false;
288 Expect.isFalse(el.attributes.containsKey('hidden'));
289 });
290 });
291
292 group('tabIndex', () {
293 test('get', () {
294 final el = makeElement();
295 Expect.equals(0, el.tabIndex);
296 el.attributes['tabIndex'] = '2';
297 Expect.equals(2, el.tabIndex);
298 el.attributes['tabIndex'] = 'foo';
299 Expect.equals(0, el.tabIndex);
300 });
301
302 test('set', () {
303 final el = makeElement();
304 el.tabIndex = 15;
305 Expect.equals('15', el.attributes['tabIndex']);
306 });
307 });
308
309 group('id', () {
310 test('get', () {
311 final el = makeElement();
312 Expect.equals('', el.id);
313 el.attributes['id'] = 'foo';
314 Expect.equals('foo', el.id);
315 });
316
317 test('set', () {
318 final el = makeElement();
319 el.id = 'foo';
320 Expect.equals('foo', el.attributes['id']);
321 });
322 });
323
324 group('title', () {
325 test('get', () {
326 final el = makeElement();
327 Expect.equals('', el.title);
328 el.attributes['title'] = 'foo';
329 Expect.equals('foo', el.title);
330 });
331
332 test('set', () {
333 final el = makeElement();
334 el.title = 'foo';
335 Expect.equals('foo', el.attributes['title']);
336 });
337 });
338
339 group('webkitdropzone', () {
340 test('get', () {
341 final el = makeElement();
342 Expect.equals('', el.webkitdropzone);
343 el.attributes['webkitdropzone'] = 'foo';
344 Expect.equals('foo', el.webkitdropzone);
345 });
346
347 test('set', () {
348 final el = makeElement();
349 el.webkitdropzone = 'foo';
350 Expect.equals('foo', el.attributes['webkitdropzone']);
351 });
352 });
353
354 group('lang', () {
355 test('get', () {
356 final el = makeElement();
357 Expect.equals('', el.lang);
358 el.attributes['lang'] = 'foo';
359 Expect.equals('foo', el.lang);
360 });
361
362 test('set', () {
363 final el = makeElement();
364 el.lang = 'foo';
365 Expect.equals('foo', el.attributes['lang']);
366 });
367 });
368
369 group('dir', () {
370 test('get', () {
371 final el = makeElement();
372 Expect.equals('', el.dir);
373 el.attributes['dir'] = 'foo';
374 Expect.equals('foo', el.dir);
375 });
376
377 test('set', () {
378 final el = makeElement();
379 el.dir = 'foo';
380 Expect.equals('foo', el.attributes['dir']);
381 });
382 });
383 });
384
385 test('set innerHTML', () {
386 final el = makeElement();
387 el.innerHTML = "<foo>Bar<baz/></foo>";
388 Expect.equals(1, el.nodes.length);
389 final node = el.nodes[0];
390 Expect.isTrue(node is XMLElement);
391 Expect.equals('foo', node.tagName);
392 Expect.equals('Bar', node.nodes[0].text);
393 Expect.equals('baz', node.nodes[1].tagName);
394 });
395
396 test('get innerHTML/outerHTML', () {
397 final el = makeElement();
398 Expect.equals("<foo></foo><bar></bar>", el.innerHTML);
399 el.nodes.clear();
400 el.nodes.addAll([new Text("foo"), new XMLElement.xml("<a>bar</a>")]);
401 Expect.equals("foo<a>bar</a>", el.innerHTML);
402 Expect.equals("<xml>foo<a>bar</a></xml>", el.outerHTML);
403 });
404
405 test('query', () {
406 final el = makeElement();
407 Expect.equals("foo", el.query('foo').tagName);
408 Expect.isNull(el.query('baz'));
409 });
410
411 test('queryAll', () {
412 final el = new XMLElement.xml(
413 "<xml><foo id='f1' /><bar><foo id='f2' /></bar></xml>");
414 Expect.listEquals(["f1", "f2"], el.queryAll('foo').map((e) => e.id));
415 Expect.listEquals([], el.queryAll('baz'));
416 });
417
418 // TODO(nweiz): re-enable this when matchesSelector works cross-browser.
419 //
420 // test('matchesSelector', () {
421 // final el = makeElement();
422 // Expect.isTrue(el.matchesSelector('*'));
423 // Expect.isTrue(el.matchesSelector('xml'));
424 // Expect.isFalse(el.matchesSelector('html'));
425 // });
426
427 group('insertAdjacentElement', () {
428 test('beforeBegin with no parent does nothing', () {
429 final el = makeElement();
430 Expect.isNull(
431 el.insertAdjacentElement("beforeBegin", new XMLElement.tag("b")));
432 Expect.equals("<foo></foo><bar></bar>", el.innerHTML);
433 });
434
435 test('afterEnd with no parent does nothing', () {
436 final el = makeElement();
437 Expect.isNull(
438 el.insertAdjacentElement("afterEnd", new XMLElement.tag("b")));
439 Expect.equals("<foo></foo><bar></bar>", el.innerHTML);
440 });
441
442 test('beforeBegin with parent inserts the element', () {
443 final el = makeElementWithParent();
444 final newEl = new XMLElement.tag("b");
445 Expect.equals(newEl, el.insertAdjacentElement("beforeBegin", newEl));
446 Expect.equals("<foo></foo><bar></bar>", el.innerHTML);
447 Expect.equals("<before></before><b></b><xml><foo></foo><bar></bar>" +
448 "</xml><after></after>", el.parent.innerHTML);
449 });
450
451 test('afterEnd with parent inserts the element', () {
452 final el = makeElementWithParent();
453 final newEl = new XMLElement.tag("b");
454 Expect.equals(newEl, el.insertAdjacentElement("afterEnd", newEl));
455 Expect.equals("<foo></foo><bar></bar>", el.innerHTML);
456 Expect.equals("<before></before><xml><foo></foo><bar></bar></xml><b>" +
457 "</b><after></after>", el.parent.innerHTML);
458 });
459
460 test('afterBegin inserts the element', () {
461 final el = makeElement();
462 final newEl = new XMLElement.tag("b");
463 Expect.equals(newEl, el.insertAdjacentElement("afterBegin", newEl));
464 Expect.equals("<b></b><foo></foo><bar></bar>", el.innerHTML);
465 });
466
467 test('beforeEnd inserts the element', () {
468 final el = makeElement();
469 final newEl = new XMLElement.tag("b");
470 Expect.equals(newEl, el.insertAdjacentElement("beforeEnd", newEl));
471 Expect.equals("<foo></foo><bar></bar><b></b>", el.innerHTML);
472 });
473 });
474
475 group('insertAdjacentText', () {
476 test('beforeBegin with no parent does nothing', () {
477 final el = makeElement();
478 el.insertAdjacentText("beforeBegin", "foo");
479 Expect.equals("<foo></foo><bar></bar>", el.innerHTML);
480 });
481
482 test('afterEnd with no parent does nothing', () {
483 final el = makeElement();
484 el.insertAdjacentText("afterEnd", "foo");
485 Expect.equals("<foo></foo><bar></bar>", el.innerHTML);
486 });
487
488 test('beforeBegin with parent inserts the text', () {
489 final el = makeElementWithParent();
490 el.insertAdjacentText("beforeBegin", "foo");
491 Expect.equals("<foo></foo><bar></bar>", el.innerHTML);
492 Expect.equals("<before></before>foo<xml><foo></foo><bar></bar></xml>" +
493 "<after></after>", el.parent.innerHTML);
494 });
495
496 test('afterEnd with parent inserts the text', () {
497 final el = makeElementWithParent();
498 el.insertAdjacentText("afterEnd", "foo");
499 Expect.equals("<foo></foo><bar></bar>", el.innerHTML);
500 Expect.equals("<before></before><xml><foo></foo><bar></bar></xml>foo" +
501 "<after></after>", el.parent.innerHTML);
502 });
503
504 test('afterBegin inserts the text', () {
505 final el = makeElement();
506 el.insertAdjacentText("afterBegin", "foo");
507 Expect.equals("foo<foo></foo><bar></bar>", el.innerHTML);
508 });
509
510 test('beforeEnd inserts the text', () {
511 final el = makeElement();
512 el.insertAdjacentText("beforeEnd", "foo");
513 Expect.equals("<foo></foo><bar></bar>foo", el.innerHTML);
514 });
515 });
516
517 group('insertAdjacentHTML', () {
518 test('beforeBegin with no parent does nothing', () {
519 final el = makeElement();
520 el.insertAdjacentHTML("beforeBegin", "foo<b/>");
521 Expect.equals("<foo></foo><bar></bar>", el.innerHTML);
522 });
523
524 test('afterEnd with no parent does nothing', () {
525 final el = makeElement();
526 el.insertAdjacentHTML("afterEnd", "<b/>foo");
527 Expect.equals("<foo></foo><bar></bar>", el.innerHTML);
528 });
529
530 test('beforeBegin with parent inserts the HTML', () {
531 final el = makeElementWithParent();
532 el.insertAdjacentHTML("beforeBegin", "foo<b/>");
533 Expect.equals("<foo></foo><bar></bar>", el.innerHTML);
534 Expect.equals("<before></before>foo<b></b><xml><foo></foo><bar></bar>" +
535 "</xml><after></after>", el.parent.innerHTML);
536 });
537
538 test('afterEnd with parent inserts the HTML', () {
539 final el = makeElementWithParent();
540 el.insertAdjacentHTML("afterEnd", "foo<b/>");
541 Expect.equals("<foo></foo><bar></bar>", el.innerHTML);
542 Expect.equals("<before></before><xml><foo></foo><bar></bar></xml>foo" +
543 "<b></b><after></after>", el.parent.innerHTML);
544 });
545
546 test('afterBegin inserts the HTML', () {
547 final el = makeElement();
548 el.insertAdjacentHTML("afterBegin", "foo<b/>");
549 Expect.equals("foo<b></b><foo></foo><bar></bar>", el.innerHTML);
550 });
551
552 test('beforeEnd inserts the HTML', () {
553 final el = makeElement();
554 el.insertAdjacentHTML("beforeEnd", "<b/>foo");
555 Expect.equals("<foo></foo><bar></bar><b></b>foo", el.innerHTML);
556 });
557 });
558
559 asyncTest('default rect values', 1, () {
560 makeElement().rect.then((ElementRect rect) {
561 expectEmptyRect(rect.client);
562 expectEmptyRect(rect.offset);
563 expectEmptyRect(rect.scroll);
564 expectEmptyRect(rect.bounding);
565 Expect.isTrue(rect.clientRects.isEmpty());
566 callbackDone();
567 });
568 });
569 }
OLDNEW
« no previous file with comments | « client/tests/client/html/XMLDocumentTests.dart ('k') | client/tests/client/html/frog_DocumentFragmentTests.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698