// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#library('ElementTest');
#import('../../pkg/unittest/unittest.dart');
#import('../../pkg/unittest/html_config.dart');
#import('dart:html');
expectLargeRect(ClientRect rect) {
Expect.equals(rect.top, 0);
Expect.equals(rect.left, 0);
Expect.isTrue(rect.width > 100);
Expect.isTrue(rect.height > 100);
Expect.equals(rect.bottom, rect.top + rect.height);
Expect.equals(rect.right, rect.left + rect.width);
}
void testEventHelper(EventListenerList listenerList, String type,
[Function registerOnEventListener = null]) {
bool firedWhenAddedToListenerList = false;
bool firedOnEvent = false;
listenerList.add((e) {
firedWhenAddedToListenerList = true;
});
if (registerOnEventListener != null) {
registerOnEventListener((e) {
firedOnEvent = true;
});
}
final event = new Event(type);
listenerList.dispatch(event);
Expect.isTrue(firedWhenAddedToListenerList);
if (registerOnEventListener != null) {
Expect.isTrue(firedOnEvent);
}
}
void testConstructorHelper(String tag, String htmlSnippet,
String expectedText, Function isExpectedClass) {
Expect.isTrue(isExpectedClass(new Element.tag(tag)));
final elementFromSnippet = new Element.html(htmlSnippet);
Expect.isTrue(isExpectedClass(elementFromSnippet));
Expect.equals(expectedText, elementFromSnippet.text);
}
main() {
useHtmlConfiguration();
Element makeElement() => new Element.tag('div');
Element makeElementWithChildren() =>
new Element.html("
");
test('computedStyle', () {
final element = document.body;
element.computedStyle.then(expectAsync1((style) {
Expect.equals(style.getPropertyValue('left'), 'auto');
}));
});
test('rect', () {
final container = new Element.tag("div");
container.style.position = 'absolute';
container.style.top = '8px';
container.style.left = '8px';
final element = new Element.tag("div");
element.style.width = '200px';
element.style.height = '200px';
container.elements.add(element);
document.body.elements.add(container);
element.rect.then(expectAsync1((rect) {
expectLargeRect(rect.client);
expectLargeRect(rect.offset);
expectLargeRect(rect.scroll);
Expect.equals(rect.bounding.left, 8);
Expect.equals(rect.bounding.top, 8);
Expect.isTrue(rect.clientRects.length > 0);
container.remove();
}));
});
group('constructors', () {
test('error', () {
Expect.throws(() => new Element.html('
'),
(e) => e is IllegalArgumentException);
});
test('.html has no parent', () =>
Expect.isNull(new Element.html(' ').parent));
test('a', () => testConstructorHelper('a', 'foo', 'foo',
(element) => element is AnchorElement));
test('area', () => testConstructorHelper('area', 'foo', '',
(element) => element is AreaElement));
// TODO(jacobr): audio tags cause tests to segfault when using dartium.
// b/5522106.
// test('audio', () => testConstructorHelper('audio',
// '', 'foo',
// (element) => element is AudioElement));
test('br', () => testConstructorHelper('br', ' ', '',
(element) => element is BRElement));
test('base', () => testConstructorHelper('base', 'foo', '',
(element) => element is BaseElement));
test('blockquote', () => testConstructorHelper('blockquote',
'
foo
', 'foo',
(element) => element is QuoteElement));
test('body', () => testConstructorHelper('body',
'
foo
', 'foo',
(element) => element is BodyElement));
test('button', () => testConstructorHelper('button',
'', 'foo',
(element) => element is ButtonElement));
test('canvas', () => testConstructorHelper('canvas',
'', 'foo',
(element) => element is CanvasElement));
test('dl', () => testConstructorHelper('dl', '
foo
', 'foo',
(element) => element is DListElement));
// TODO(jacobr): WebKit doesn't yet support the DataList class.
// test('datalist', () => testConstructorHelper('datalist',
// '', 'foo',
// (element) => element is DataListElement));
test('details', () => testConstructorHelper('details',
'foo', 'foo',
(element) => element is DetailsElement));
test('div', () => testConstructorHelper('div', '
foo
', 'foo',
(element) => element is DivElement));
test('embed', () => testConstructorHelper('embed',
'', '',
(element) => element is EmbedElement));
test('fieldset', () => testConstructorHelper('fieldset',
'', 'foo',
(element) => element is FieldSetElement));
test('font', () => testConstructorHelper('font', 'foo', 'foo',
(element) => element is FontElement));
test('form', () => testConstructorHelper('form', '', 'foo',
(element) => element is FormElement));
test('hr', () => testConstructorHelper('hr', '', '',
(element) => element is HRElement));
test('head', () => testConstructorHelper('head',
'', 'foo;',
(element) => element is HeadElement));
test('h1', () => testConstructorHelper('h1', '
foo
', 'foo',
(element) => element is HeadingElement));
test('h2', () => testConstructorHelper('h2', '
foo
', 'foo',
(element) => element is HeadingElement));
test('h3', () => testConstructorHelper('h3', '
foo
', 'foo',
(element) => element is HeadingElement));
test('h4', () => testConstructorHelper('h4', '
foo
', 'foo',
(element) => element is HeadingElement));
test('h5', () => testConstructorHelper('h5', '
foo
', 'foo',
(element) => element is HeadingElement));
test('h6', () => testConstructorHelper('h6', '
foo
', 'foo',
(element) => element is HeadingElement));
test('iframe', () => testConstructorHelper('iframe',
'', 'foo',
(element) => element is IFrameElement));
test('img', () => testConstructorHelper('img', '', '',
(element) => element is ImageElement));
test('input', () => testConstructorHelper('input', '', '',
(element) => element is InputElement));
test('keygen', () => testConstructorHelper('keygen', '', '',
(element) => element is KeygenElement));
test('li', () => testConstructorHelper('li', '
foo
', 'foo',
(element) => element is LIElement));
test('label', () => testConstructorHelper('label',
'', 'foo',
(element) => element is LabelElement));
test('legend', () => testConstructorHelper('legend',
'', 'foo',
(element) => element is LegendElement));
test('link', () => testConstructorHelper('link', '', '',
(element) => element is LinkElement));
test('map', () => testConstructorHelper('map', '', 'foo',
(element) => element is MapElement));
test('marquee', () => testConstructorHelper('marquee',
'', 'foo',
(element) => element is MarqueeElement));
test('menu', () => testConstructorHelper('menu', '', 'foo',
(element) => element is MenuElement));
test('meta', () => testConstructorHelper('meta', '', '',
(element) => element is MetaElement));
test('meter', () => testConstructorHelper('meter',
'foo', 'foo',
(element) => element is MeterElement));
test('del', () => testConstructorHelper('del', 'foo', 'foo',
(element) => element is ModElement));
test('ins', () => testConstructorHelper('ins', 'foo', 'foo',
(element) => element is ModElement));
test('ol', () => testConstructorHelper('ol', 'foo', 'foo',
(element) => element is OListElement));
test('object', () => testConstructorHelper('object',
'', 'foo',
(element) => element is ObjectElement));
test('optgroup', () => testConstructorHelper('optgroup',
'', 'foo',
(element) => element is OptGroupElement));
test('option', () => testConstructorHelper('option',
'', 'foo',
(element) => element is OptionElement));
test('output', () => testConstructorHelper('output',
'', 'foo',
(element) => element is OutputElement));
test('p', () => testConstructorHelper('p', '
foo
', 'foo',
(element) => element is ParagraphElement));
test('param', () => testConstructorHelper('param', '', '',
(element) => element is ParamElement));
test('pre', () => testConstructorHelper('pre', '
foo
', 'foo',
(element) => element is PreElement));
test('progress', () => testConstructorHelper('progress',
'', 'foo',
(element) => element is ProgressElement));
test('q', () => testConstructorHelper('q', 'foo', 'foo',
(element) => element is QuoteElement));
test('script', () => testConstructorHelper('script',
'', 'foo',
(element) => element is ScriptElement));
test('select', () => testConstructorHelper('select',
'', 'foo',
(element) => element is SelectElement));
// TODO(jacobr): audio tags cause tests to segfault when using dartium.
// b/5522106.
// test('source', () => testConstructorHelper('source', '', '',
// (element) => element is SourceElement));
test('span', () => testConstructorHelper('span', 'foo', 'foo',
(element) => element is SpanElement));
test('style', () => testConstructorHelper('style',
'', 'foo',
(element) => element is StyleElement));
test('caption', () => testConstructorHelper('caption',
'
foo
', 'foo',
(element) => element is TableCaptionElement));
test('td', () => testConstructorHelper('td', '
foo
', 'foo',
(element) => element is TableCellElement));
test('colgroup', () => testConstructorHelper('colgroup',
'
', '',
(element) => element is TableColElement));
test('col', () => testConstructorHelper('col', '
', '',
(element) => element is TableColElement));
test('table', () => testConstructorHelper('table',
'
foo
', 'foo',
(element) => element is TableElement));
test('tr', () => testConstructorHelper('tr',
'
foo
', 'foo',
(element) => element is TableRowElement));
test('tbody', () => testConstructorHelper('tbody',
'
foo
', 'foo',
(element) => element is TableSectionElement));
test('tfoot', () => testConstructorHelper('tfoot',
'
foo
', 'foo',
(element) => element is TableSectionElement));
test('thead', () => testConstructorHelper('thead',
'
foo
', 'foo',
(element) => element is TableSectionElement));
test('textarea', () => testConstructorHelper('textarea',
'', 'foo',
(element) => element is TextAreaElement));
test('title', () => testConstructorHelper('title',
'foo', 'foo',
(element) => element is TitleElement));
// TODO(jacobr): audio tags cause tests to segfault when using dartium.
// b/5522106.
// test('track', () => testConstructorHelper('track', '