| OLD | NEW |
| 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 #import('../../pkg/unittest/unittest.dart'); | 6 #import('../../pkg/unittest/unittest.dart'); |
| 7 #import('../../pkg/unittest/html_config.dart'); | 7 #import('../../pkg/unittest/html_config.dart'); |
| 8 #import('dart:html'); | 8 #import('dart:html'); |
| 9 | 9 |
| 10 expectLargeRect(ClientRect rect) { | 10 expectLargeRect(ClientRect rect) { |
| 11 Expect.equals(rect.top, 0); | 11 Expect.equals(rect.top, 0); |
| 12 Expect.equals(rect.left, 0); | 12 Expect.equals(rect.left, 0); |
| 13 Expect.isTrue(rect.width > 100); | 13 Expect.isTrue(rect.width > 100); |
| 14 Expect.isTrue(rect.height > 100); | 14 Expect.isTrue(rect.height > 100); |
| 15 Expect.equals(rect.bottom, rect.top + rect.height); | 15 Expect.equals(rect.bottom, rect.top + rect.height); |
| 16 Expect.equals(rect.right, rect.left + rect.width); | 16 Expect.equals(rect.right, rect.left + rect.width); |
| 17 } | 17 } |
| 18 | 18 |
| 19 void testEventHelper(EventListenerList listenerList, String type, | 19 void testEventHelper(EventListenerList listenerList, String type, |
| 20 [Function registerOnEventListener = null]) { | 20 [Function registerOnEventListener = null]) { |
| 21 testMultipleEventHelper(listenerList, [type], registerOnEventListener); |
| 22 } |
| 23 |
| 24 // Allows testing where we polyfill different browsers firing different events. |
| 25 void testMultipleEventHelper(EventListenerList listenerList, List<String> types, |
| 26 [Function registerOnEventListener = null]) { |
| 21 bool firedWhenAddedToListenerList = false; | 27 bool firedWhenAddedToListenerList = false; |
| 22 bool firedOnEvent = false; | 28 bool firedOnEvent = false; |
| 23 listenerList.add((e) { | 29 listenerList.add((e) { |
| 24 firedWhenAddedToListenerList = true; | 30 firedWhenAddedToListenerList = true; |
| 25 }); | 31 }); |
| 26 if (registerOnEventListener != null) { | 32 if (registerOnEventListener != null) { |
| 27 registerOnEventListener((e) { | 33 registerOnEventListener((e) { |
| 28 firedOnEvent = true; | 34 firedOnEvent = true; |
| 29 }); | 35 }); |
| 30 } | 36 } |
| 31 final event = new Event(type); | 37 for (var type in types) { |
| 32 listenerList.dispatch(event); | 38 final event = new Event(type); |
| 39 listenerList.dispatch(event); |
| 40 } |
| 33 | 41 |
| 34 Expect.isTrue(firedWhenAddedToListenerList); | 42 Expect.isTrue(firedWhenAddedToListenerList); |
| 35 if (registerOnEventListener != null) { | 43 if (registerOnEventListener != null) { |
| 36 Expect.isTrue(firedOnEvent); | 44 Expect.isTrue(firedOnEvent); |
| 37 } | 45 } |
| 38 } | 46 } |
| 39 | 47 |
| 40 void testConstructorHelper(String tag, String htmlSnippet, | 48 void testConstructorHelper(String tag, String htmlSnippet, |
| 41 String expectedText, Function isExpectedClass) { | 49 String expectedText, Function isExpectedClass) { |
| 42 Expect.isTrue(isExpectedClass(new Element.tag(tag))); | 50 Expect.isTrue(isExpectedClass(new Element.tag(tag))); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 68 final element = new Element.tag("div"); | 76 final element = new Element.tag("div"); |
| 69 element.style.width = '200px'; | 77 element.style.width = '200px'; |
| 70 element.style.height = '200px'; | 78 element.style.height = '200px'; |
| 71 container.elements.add(element); | 79 container.elements.add(element); |
| 72 document.body.elements.add(container); | 80 document.body.elements.add(container); |
| 73 | 81 |
| 74 element.rect.then(expectAsync1((rect) { | 82 element.rect.then(expectAsync1((rect) { |
| 75 expectLargeRect(rect.client); | 83 expectLargeRect(rect.client); |
| 76 expectLargeRect(rect.offset); | 84 expectLargeRect(rect.offset); |
| 77 expectLargeRect(rect.scroll); | 85 expectLargeRect(rect.scroll); |
| 78 Expect.equals(rect.bounding.left, 8); | 86 expect(rect.bounding.left, 8); |
| 79 Expect.equals(rect.bounding.top, 8); | 87 expect(rect.bounding.top, 8); |
| 80 Expect.isTrue(rect.clientRects.length > 0); | 88 expect(rect.clientRects.length, greaterThan(0)); |
| 81 container.remove(); | 89 container.remove(); |
| 82 })); | 90 })); |
| 83 }); | 91 }); |
| 84 | 92 |
| 93 test('rect synchronous', () { |
| 94 final container = new Element.tag("div"); |
| 95 container.style.position = 'absolute'; |
| 96 container.style.top = '8px'; |
| 97 container.style.left = '8px'; |
| 98 final element = new Element.tag("div"); |
| 99 element.style.width = '200px'; |
| 100 element.style.height = '200px'; |
| 101 container.elements.add(element); |
| 102 document.body.elements.add(container); |
| 103 |
| 104 expectLargeRect(element.client); |
| 105 expectLargeRect(element.offset); |
| 106 expectLargeRect(element.scroll); |
| 107 expect(element.boundingClientRect.left, 8); |
| 108 expect(element.boundingClientRect.top, 8); |
| 109 container.remove(); |
| 110 }); |
| 111 |
| 85 group('constructors', () { | 112 group('constructors', () { |
| 86 test('error', () { | 113 test('error', () { |
| 87 Expect.throws(() => new Element.html('<br/><br/>'), | 114 Expect.throws(() => new Element.html('<br/><br/>'), |
| 88 (e) => e is ArgumentError); | 115 (e) => e is ArgumentError); |
| 89 }); | 116 }); |
| 90 | 117 |
| 91 test('.html has no parent', () => | 118 test('.html has no parent', () => |
| 92 Expect.isNull(new Element.html('<br/>').parent)); | 119 Expect.isNull(new Element.html('<br/>').parent)); |
| 93 | 120 |
| 94 test('a', () => testConstructorHelper('a', '<a>foo</a>', 'foo', | 121 test('a', () => testConstructorHelper('a', '<a>foo</a>', 'foo', |
| (...skipping 20 matching lines...) Expand all Loading... |
| 115 (element) => element is ButtonElement)); | 142 (element) => element is ButtonElement)); |
| 116 test('canvas', () => testConstructorHelper('canvas', | 143 test('canvas', () => testConstructorHelper('canvas', |
| 117 '<canvas>foo</canvas>', 'foo', | 144 '<canvas>foo</canvas>', 'foo', |
| 118 (element) => element is CanvasElement)); | 145 (element) => element is CanvasElement)); |
| 119 test('dl', () => testConstructorHelper('dl', '<dl>foo</dl>', 'foo', | 146 test('dl', () => testConstructorHelper('dl', '<dl>foo</dl>', 'foo', |
| 120 (element) => element is DListElement)); | 147 (element) => element is DListElement)); |
| 121 // TODO(jacobr): WebKit doesn't yet support the DataList class. | 148 // TODO(jacobr): WebKit doesn't yet support the DataList class. |
| 122 // test('datalist', () => testConstructorHelper('datalist', | 149 // test('datalist', () => testConstructorHelper('datalist', |
| 123 // '<datalist>foo</datalist>', 'foo', | 150 // '<datalist>foo</datalist>', 'foo', |
| 124 // (element) => element is DataListElement)); | 151 // (element) => element is DataListElement)); |
| 125 test('details', () => testConstructorHelper('details', | |
| 126 '<details>foo</details>', 'foo', | |
| 127 (element) => element is DetailsElement)); | |
| 128 test('div', () => testConstructorHelper('div', '<div>foo</div>', 'foo', | 152 test('div', () => testConstructorHelper('div', '<div>foo</div>', 'foo', |
| 129 (element) => element is DivElement)); | 153 (element) => element is DivElement)); |
| 130 test('embed', () => testConstructorHelper('embed', | |
| 131 '<embed>foo</embed>', '', | |
| 132 (element) => element is EmbedElement)); | |
| 133 test('fieldset', () => testConstructorHelper('fieldset', | 154 test('fieldset', () => testConstructorHelper('fieldset', |
| 134 '<fieldset>foo</fieldset>', 'foo', | 155 '<fieldset>foo</fieldset>', 'foo', |
| 135 (element) => element is FieldSetElement)); | 156 (element) => element is FieldSetElement)); |
| 136 test('font', () => testConstructorHelper('font', '<font>foo</font>', 'foo', | 157 test('font', () => testConstructorHelper('font', '<font>foo</font>', 'foo', |
| 137 (element) => element is FontElement)); | 158 (element) => element is FontElement)); |
| 138 test('form', () => testConstructorHelper('form', '<form>foo</form>', 'foo', | 159 test('form', () => testConstructorHelper('form', '<form>foo</form>', 'foo', |
| 139 (element) => element is FormElement)); | 160 (element) => element is FormElement)); |
| 140 test('hr', () => testConstructorHelper('hr', '<hr>', '', | 161 test('hr', () => testConstructorHelper('hr', '<hr>', '', |
| 141 (element) => element is HRElement)); | 162 (element) => element is HRElement)); |
| 142 test('head', () => testConstructorHelper('head', | 163 test('head', () => testConstructorHelper('head', |
| (...skipping 11 matching lines...) Expand all Loading... |
| 154 (element) => element is HeadingElement)); | 175 (element) => element is HeadingElement)); |
| 155 test('h6', () => testConstructorHelper('h6', '<h6>foo</h6>', 'foo', | 176 test('h6', () => testConstructorHelper('h6', '<h6>foo</h6>', 'foo', |
| 156 (element) => element is HeadingElement)); | 177 (element) => element is HeadingElement)); |
| 157 test('iframe', () => testConstructorHelper('iframe', | 178 test('iframe', () => testConstructorHelper('iframe', |
| 158 '<iframe>foo</iframe>', 'foo', | 179 '<iframe>foo</iframe>', 'foo', |
| 159 (element) => element is IFrameElement)); | 180 (element) => element is IFrameElement)); |
| 160 test('img', () => testConstructorHelper('img', '<img>', '', | 181 test('img', () => testConstructorHelper('img', '<img>', '', |
| 161 (element) => element is ImageElement)); | 182 (element) => element is ImageElement)); |
| 162 test('input', () => testConstructorHelper('input', '<input/>', '', | 183 test('input', () => testConstructorHelper('input', '<input/>', '', |
| 163 (element) => element is InputElement)); | 184 (element) => element is InputElement)); |
| 164 test('keygen', () => testConstructorHelper('keygen', '<keygen>', '', | |
| 165 (element) => element is KeygenElement)); | |
| 166 test('li', () => testConstructorHelper('li', '<li>foo</li>', 'foo', | 185 test('li', () => testConstructorHelper('li', '<li>foo</li>', 'foo', |
| 167 (element) => element is LIElement)); | 186 (element) => element is LIElement)); |
| 168 test('label', () => testConstructorHelper('label', | 187 test('label', () => testConstructorHelper('label', |
| 169 '<label>foo</label>', 'foo', | 188 '<label>foo</label>', 'foo', |
| 170 (element) => element is LabelElement)); | 189 (element) => element is LabelElement)); |
| 171 test('legend', () => testConstructorHelper('legend', | 190 test('legend', () => testConstructorHelper('legend', |
| 172 '<legend>foo</legend>', 'foo', | 191 '<legend>foo</legend>', 'foo', |
| 173 (element) => element is LegendElement)); | 192 (element) => element is LegendElement)); |
| 174 test('link', () => testConstructorHelper('link', '<link>', '', | 193 test('link', () => testConstructorHelper('link', '<link>', '', |
| 175 (element) => element is LinkElement)); | 194 (element) => element is LinkElement)); |
| 176 test('map', () => testConstructorHelper('map', '<map>foo</map>', 'foo', | 195 test('map', () => testConstructorHelper('map', '<map>foo</map>', 'foo', |
| 177 (element) => element is MapElement)); | 196 (element) => element is MapElement)); |
| 178 test('marquee', () => testConstructorHelper('marquee', | |
| 179 '<marquee>foo</marquee>', 'foo', | |
| 180 (element) => element is MarqueeElement)); | |
| 181 test('menu', () => testConstructorHelper('menu', '<menu>foo</menu>', 'foo', | 197 test('menu', () => testConstructorHelper('menu', '<menu>foo</menu>', 'foo', |
| 182 (element) => element is MenuElement)); | 198 (element) => element is MenuElement)); |
| 183 test('meta', () => testConstructorHelper('meta', '<meta>', '', | 199 test('meta', () => testConstructorHelper('meta', '<meta>', '', |
| 184 (element) => element is MetaElement)); | 200 (element) => element is MetaElement)); |
| 185 test('meter', () => testConstructorHelper('meter', | |
| 186 '<meter>foo</meter>', 'foo', | |
| 187 (element) => element is MeterElement)); | |
| 188 test('del', () => testConstructorHelper('del', '<del>foo</del>', 'foo', | 201 test('del', () => testConstructorHelper('del', '<del>foo</del>', 'foo', |
| 189 (element) => element is ModElement)); | 202 (element) => element is ModElement)); |
| 190 test('ins', () => testConstructorHelper('ins', '<ins>foo</ins>', 'foo', | 203 test('ins', () => testConstructorHelper('ins', '<ins>foo</ins>', 'foo', |
| 191 (element) => element is ModElement)); | 204 (element) => element is ModElement)); |
| 192 test('ol', () => testConstructorHelper('ol', '<ol>foo</ol>', 'foo', | 205 test('ol', () => testConstructorHelper('ol', '<ol>foo</ol>', 'foo', |
| 193 (element) => element is OListElement)); | 206 (element) => element is OListElement)); |
| 194 test('object', () => testConstructorHelper('object', | |
| 195 '<object>foo</object>', 'foo', | |
| 196 (element) => element is ObjectElement)); | |
| 197 test('optgroup', () => testConstructorHelper('optgroup', | 207 test('optgroup', () => testConstructorHelper('optgroup', |
| 198 '<optgroup>foo</optgroup>', 'foo', | 208 '<optgroup>foo</optgroup>', 'foo', |
| 199 (element) => element is OptGroupElement)); | 209 (element) => element is OptGroupElement)); |
| 200 test('option', () => testConstructorHelper('option', | 210 test('option', () => testConstructorHelper('option', |
| 201 '<option>foo</option>', 'foo', | 211 '<option>foo</option>', 'foo', |
| 202 (element) => element is OptionElement)); | 212 (element) => element is OptionElement)); |
| 203 test('output', () => testConstructorHelper('output', | 213 test('output', () => testConstructorHelper('output', |
| 204 '<output>foo</output>', 'foo', | 214 '<output>foo</output>', 'foo', |
| 205 (element) => element is OutputElement)); | 215 (element) => element is OutputElement)); |
| 206 test('p', () => testConstructorHelper('p', '<p>foo</p>', 'foo', | 216 test('p', () => testConstructorHelper('p', '<p>foo</p>', 'foo', |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 367 element, 'mousemove', listener, true)); | 377 element, 'mousemove', listener, true)); |
| 368 testEventHelper(on.mouseOut, 'mouseout', | 378 testEventHelper(on.mouseOut, 'mouseout', |
| 369 (listener) => Testing.addEventListener( | 379 (listener) => Testing.addEventListener( |
| 370 element, 'mouseout', listener, true)); | 380 element, 'mouseout', listener, true)); |
| 371 testEventHelper(on.mouseOver, 'mouseover', | 381 testEventHelper(on.mouseOver, 'mouseover', |
| 372 (listener) => Testing.addEventListener( | 382 (listener) => Testing.addEventListener( |
| 373 element, 'mouseover', listener, true)); | 383 element, 'mouseover', listener, true)); |
| 374 testEventHelper(on.mouseUp, 'mouseup', | 384 testEventHelper(on.mouseUp, 'mouseup', |
| 375 (listener) => Testing.addEventListener( | 385 (listener) => Testing.addEventListener( |
| 376 element, 'mouseup', listener, true)); | 386 element, 'mouseup', listener, true)); |
| 377 testEventHelper(on.mouseWheel, 'mousewheel', | 387 // Browsers have different events that they use, so fire all variants. |
| 388 testMultipleEventHelper(on.mouseWheel, |
| 389 ['mousewheel', 'wheel', 'DOMMouseScroll'], |
| 378 (listener) => Testing.addEventListener( | 390 (listener) => Testing.addEventListener( |
| 379 element, 'mousewheel', listener, true)); | 391 element, 'mousewheel', listener, true)); |
| 380 testEventHelper(on.paste, 'paste', | 392 testEventHelper(on.paste, 'paste', |
| 381 (listener) => Testing.addEventListener( | 393 (listener) => Testing.addEventListener( |
| 382 element, 'paste', listener, true)); | 394 element, 'paste', listener, true)); |
| 383 testEventHelper(on.reset, 'reset', | 395 testEventHelper(on.reset, 'reset', |
| 384 (listener) => Testing.addEventListener( | 396 (listener) => Testing.addEventListener( |
| 385 element, 'reset', listener, true)); | 397 element, 'reset', listener, true)); |
| 386 testEventHelper(on.scroll, 'scroll', | 398 testEventHelper(on.scroll, 'scroll', |
| 387 (listener) => Testing.addEventListener( | 399 (listener) => Testing.addEventListener( |
| (...skipping 16 matching lines...) Expand all Loading... |
| 404 testEventHelper(on.touchEnd, 'touchend', | 416 testEventHelper(on.touchEnd, 'touchend', |
| 405 (listener) => Testing.addEventListener( | 417 (listener) => Testing.addEventListener( |
| 406 element, 'touchend', listener, true)); | 418 element, 'touchend', listener, true)); |
| 407 testEventHelper(on.touchLeave, 'touchleave'); | 419 testEventHelper(on.touchLeave, 'touchleave'); |
| 408 testEventHelper(on.touchMove, 'touchmove', | 420 testEventHelper(on.touchMove, 'touchmove', |
| 409 (listener) => Testing.addEventListener( | 421 (listener) => Testing.addEventListener( |
| 410 element, 'touchmove', listener, true)); | 422 element, 'touchmove', listener, true)); |
| 411 testEventHelper(on.touchStart, 'touchstart', | 423 testEventHelper(on.touchStart, 'touchstart', |
| 412 (listener) => Testing.addEventListener( | 424 (listener) => Testing.addEventListener( |
| 413 element, 'touchstart', listener, true)); | 425 element, 'touchstart', listener, true)); |
| 414 testEventHelper(on.transitionEnd, 'webkitTransitionEnd'); | |
| 415 testEventHelper(on.fullscreenChange, 'webkitfullscreenchange', | |
| 416 (listener) => Testing.addEventListener(element, | |
| 417 'webkitfullscreenchange', listener, true)); | |
| 418 }); | 426 }); |
| 419 | 427 |
| 420 group('attributes', () { | 428 group('attributes', () { |
| 421 test('manipulation', () { | 429 test('manipulation', () { |
| 422 final element = new Element.html( | 430 final element = new Element.html( |
| 423 '''<div class="foo" style="overflow: hidden" data-foo="bar" | 431 '''<div class="foo" style="overflow: hidden" data-foo="bar" |
| 424 data-foo2="bar2" dir="rtl"> | 432 data-foo2="bar2" dir="rtl"> |
| 425 </div>'''); | 433 </div>'''); |
| 426 final attributes = element.attributes; | 434 final attributes = element.attributes; |
| 427 Expect.equals(attributes['class'], 'foo'); | 435 Expect.equals(attributes['class'], 'foo'); |
| (...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 729 }); | 737 }); |
| 730 | 738 |
| 731 test('getRange', () { | 739 test('getRange', () { |
| 732 var range = makeElList().getRange(1, 2); | 740 var range = makeElList().getRange(1, 2); |
| 733 Expect.isTrue(range is List<Element>); | 741 Expect.isTrue(range is List<Element>); |
| 734 Expect.isTrue(range[0] is ImageElement); | 742 Expect.isTrue(range[0] is ImageElement); |
| 735 Expect.isTrue(range[1] is InputElement); | 743 Expect.isTrue(range[1] is InputElement); |
| 736 }); | 744 }); |
| 737 }); | 745 }); |
| 738 } | 746 } |
| OLD | NEW |