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