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

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

Issue 23232005: Port custom element layout tests to Dart (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 4 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) 2013, 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 library custom_element_document_register_test;
6 import '../../pkg/unittest/lib/unittest.dart';
7 import '../../pkg/unittest/lib/html_individual_config.dart';
8 import 'dart:html';
9
10 class CustomFoo extends HtmlElement {
11 static final tag = 'x-foo';
12 factory CustomFoo() => new Element.tag(tag);
13
14 get thisIsACustomClass => true;
15 }
16
17 class CustomBar extends HtmlElement {
18 static final tag = 'x-bar';
19 factory CustomBar() => new Element.tag(tag);
20
21 get thisIsACustomClass => true;
22 }
23
24 class CustomBaz extends CustomFoo {
25 static final tag = 'x-baz';
26 factory CustomBaz() => new Element.tag(tag);
27
28 get thisIsAlsoACustomClass => true;
29 }
30
31 class CustomBadB {
32 }
33
34 class CustomBadE implements HtmlElement {
35 static final tag = 'x-tag-e';
36 factory CustomBadE() => new Element.tag(tag);
37 }
38
39 class CustomBar2 extends InputElement {
40 static final tag = 'x-bar';
41 factory CustomBar2() => document.$dom_createElement('input', tag);
42 }
43
44 class CustomQux extends CustomBar2 {
45 static final tag = 'x-qux';
46 factory CustomQux() => document.$dom_createElement('input', tag);
47 }
48
49 class CustomFooBad extends DivElement {
50 static final tag = 'x-foo';
51 factory CustomFooBad() => document.$dom_createElement('div', tag);
52 }
53
54 main() {
55 useHtmlIndividualConfiguration();
56
57 // Adapted from Blink's fast/dom/custom/document-register-basic test.
58 group('basic', () {
59 test('Testing document.register() basic behaviors', () {
60 document.register(CustomFoo.tag, CustomFoo);
61
62 // Cannot register an existing dart:html type.
63 expect(() => document.register('x-bad-a', HtmlElement), throws);
64
65 // Invalid user type. Doesn't inherit from HtmlElement.
66 expect(() => document.register('x-bad-b', CustomBadB), throws);
67
68 // Not a type.
69 expect(() => document.register('x-bad-c', null), throws);
70
71 // Cannot register system type.
72 expect(() => document.register('x-bad-d', Object), throws);
73
74 // Must extend HtmlElement, not just implement it.
75 expect(() => document.register(CustomBadE.tag, CustomBadE), throws);
76
77 // Constructor initiated instantiation
78 var createdFoo = new CustomFoo();
79 expect(createdFoo.thisIsACustomClass, isTrue);
80
81 // Dart type correctness
82 expect(createdFoo is HtmlElement, isTrue);
83 expect(createdFoo is CustomFoo, isTrue);
84 expect(createdFoo.runtimeType, CustomFoo);
85
86 // Native getter
87 expect(createdFoo.tagName, "X-FOO");
88
89 // Native setter
90 createdFoo.innerHtml = "Hello";
91 expect(createdFoo.text, "Hello");
92
93 // Native method
94 var childDiv = new DivElement();
95 createdFoo.append(childDiv);
96 expect(createdFoo.lastChild, childDiv);
97
98 // Parser initiated instantiation
99 var container = new DivElement()..id = "container";
100 document.body.append(container);
101 container.innerHtml = "<x-foo></x-foo>";
102 var parsedFoo = container.firstChild;
103
104 expect(parsedFoo is CustomFoo, isTrue);
105 expect(parsedFoo.tagName, "X-FOO");
106
107 // Ensuring the wrapper is retained
108 var someProperty = new Expando();
109 someProperty[parsedFoo] = "hello";
110 // TODO(vsm): Fix this in Dartium.
111 // expect(someProperty[container.firstChild], someProperty[parsedFoo]);
112 // expect(container.firstChild, parsedFoo);
113
114 // Having another constructor
115 document.register(CustomBar.tag, CustomBar);
116 var createdBar = new CustomBar();
117 expect(createdBar is CustomBar, isTrue);
118 expect(createdBar is CustomFoo, isFalse);
119 expect(createdBar.tagName, "X-BAR");
120
121 // Having a subclass
122 document.register(CustomBaz.tag, CustomBaz);
123 var createdBaz = new CustomBaz();
124 expect(createdBaz.tagName, "X-BAZ");
125 expect(createdBaz.thisIsACustomClass, isTrue);
126 expect(createdBaz.thisIsAlsoACustomClass, isTrue);
127
128 // With irregular cases
129 var createdUpperBar = new Element.tag("X-BAR");
130 var createdMixedBar = new Element.tag("X-Bar");
131 expect(createdUpperBar is CustomBar, isTrue);
132 expect(createdUpperBar.tagName, "X-BAR");
133 expect(createdMixedBar is CustomBar, isTrue);
134 expect(createdMixedBar.tagName, "X-BAR");
135
136 container.innerHtml = "<X-BAR></X-BAR><X-Bar></X-Bar>";
137 expect(container.firstChild is CustomBar, isTrue);
138 expect(container.firstChild.tagName, "X-BAR");
139 expect(container.lastChild is CustomBar, isTrue);
140 expect(container.lastChild.tagName, "X-BAR");
141
142 // Constructors shouldn't interfere with each other
143 expect((new CustomFoo()).tagName, "X-FOO");
144 expect((new CustomBar()).tagName, "X-BAR");
145 expect((new CustomBaz()).tagName, "X-BAZ");
146 });
147 });
148
149 // Adapted from Blink's fast/dom/custom/document-register-type-extension test.
150 group('type_extensions', () {
151 var testForm = new FormElement()..id = 'testForm';
152 document.body.append(testForm);
153
154 bool isFormElement(element) {
155 testForm.append(element);
156 return element.form == testForm;
157 }
158
159 test('construction', () {
160 document.register(CustomFoo.tag, CustomFoo);
161 document.register(CustomBar2.tag, CustomBar2);
162 document.register(CustomBaz.tag, CustomBaz);
163 document.register(CustomQux.tag, CustomQux);
164
165 expect(() => document.register(CustomFooBad.tag, CustomFoo), throws);
166
167 // Constructors
168
169 var fooNewed = new CustomFoo();
170 var fooOuterHtml = "<x-foo></x-foo>";
171 expect(fooNewed.outerHtml, fooOuterHtml);
172 expect(fooNewed is CustomFoo, isTrue);
173 expect(fooNewed is HtmlElement, isTrue);
174 expect(fooNewed is UnknownElement, isFalse);
175
176 var barNewed = new CustomBar2();
177 var barOuterHtml = '<input is="x-bar"></input>';
178 expect(barNewed.outerHtml, barOuterHtml);
179 expect(barNewed is CustomBar, isTrue);
180 expect(barNewed is InputElement, isTrue);
181 expect(isFormElement(barNewed), isTrue);
182
183 var bazNewed = new CustomBaz();
184 var bazOuterHtml = "<x-baz></x-baz>";
185 expect(bazNewed.outerHtml, bazOuterHtml);
186 expect(bazNewed is CustomBaz, isTrue);
187 expect(bazNewed is HtmlElement, isTrue);
188 expect(bazNewed is UnknownElement, isFalse);
189
190 var quxNewed = new CustomQux();
191 var quxOuterHtml = '<input is="x-qux"></input>';
192 expect(quxNewed.outerHtml, quxOuterHtml);
193 expect(quxNewed is CustomQux, isTrue);
194 expect(quxNewed is InputElement, isTrue);
195 expect(isFormElement(quxNewed), isTrue);
196
197 // new Element.tag
198
199 var fooCreated = new Element.tag('x-foo');
200 expect(fooCreated.outerHtml, fooOuterHtml);
201 expect(fooCreated is CustomFoo, isTrue);
202
203 var barCreated = new Element.tag('x-bar');
204 expect(barCreated.outerHtml, "<x-bar></x-bar>");
205 expect(barCreated is CustomBar2, isFalse);
206 expect(barCreated is UnknownElement, isFalse);
207 expect(barCreated is HtmlElement, isTrue);
208
209 var bazCreated = new Element.tag('x-baz');
210 expect(bazCreated.outerHtml, bazOuterHtml);
211 expect(bazCreated is CustomBaz, isTrue);
212 expect(bazCreated is UnknownElement, isFalse);
213
214 var quxCreated = new Element.tag('x-qux');
215 expect(quxCreated.outerHtml, "<x-qux></x-qux>");
216 expect(quxCreated is CustomQux, isFalse);
217 expect(quxCreated is UnknownElement, isFalse);
218 expect(quxCreated is HtmlElement, isTrue);
219
220 // create with type extensions
221 // TODO(vsm): How should we expose this?
222
223 var divFooCreated = document.$dom_createElement("div", CustomFoo.tag);
224 expect(divFooCreated.outerHtml, '<div is="x-foo"></div>');
225 expect(divFooCreated is CustomFoo, isFalse);
226 expect(divFooCreated is DivElement, isTrue);
227
228 var inputBarCreated =
229 document.$dom_createElement("input", CustomBar2.tag);
230 expect(inputBarCreated.outerHtml, barOuterHtml);
231 expect(inputBarCreated is CustomBar2, isTrue);
232 expect(inputBarCreated is UnknownElement, isFalse);
233 expect(isFormControl(inputBarCreated), isTrue);
234
235 var divBarCreated = document.$dom_createElement("div", CustomBar2.tag);
236 expect(divBarCreated.outerHtml, '<div is="x-bar"></div>');
237 expect(divBarCreated is CustomBar2, isFalse);
238 expect(divBarCreated is DivElement, isTrue);
239
240 var fooBarCreated =
241 document.$dom_createElement(CustomFoo.tag, CustomBar2.tag);
242 expect(fooBasedCreated.outerHtml, '<x-foo is="x-bar"></x-foo>');
243 expect(fooBarCreated is CustomFoo, isTrue);
244
245 var barFooCreated = document.$dom_createElement(CustomBar2.tag, CustomFoo. tag);
Siggi Cherem (dart-lang) 2013/08/16 23:02:19 long line
vsm 2013/08/19 17:00:10 Done.
246 expect(barFooCreated.outerHtml, '<x-bar is="x-foo"></x-bar>');
247 expect(barFooCreated is UnknownElement, isFalse);
248 expect(barFooCreated is HtmlElement, isTrue);
249
250 var fooCreatedNull = document.$dom_createElement(CustomFoo.tag, null);
251 expect(fooCreatedNull.outerHtml, fooOuterHtml);
252 expect(fooCreatedNull is CustomFoo, isTrue);
253
254 var fooCreatedEmpty = document.$dom_createElement(CustomFoo.tag, "");
255 expect(fooCreatedEmpty.outerHtml, fooOuterHtml);
256 expect(fooCreatedEmpty is CustomFoo, isTrue);
257
258 expect(() => document.$dom_createElement('@invalid', 'x-bar'), throws);
259
260 // Create NS with type extensions
261
262 var fooCreatedNS =
263 document.$dom_createElementNS("http://www.w3.org/1999/xhtml",
264 CustomFoo.tag, null);
265 expect(fooCreatedNS.outerHtml, fooOuterHtml);
266 expect(fooCreatedNS is CustomFoo, isTrue);
267
268 var barCreatedNS =
269 document.$dom_createElementNS("http://www.w3.org/1999/xhtml", "input",
270 CustomBar2.tag);
271 expect(barCreatedNS.outerHtml, barOuterHtml);
272 expect(barCreatedNS is CustomBar2, isTrue);
273 expect(isFormControl(barCreatedNS), isTrue);
274
275 expect(() =>
276 document.$dom_createElementNS('http://example.com/2013/no-such-name space',
Siggi Cherem (dart-lang) 2013/08/16 23:02:19 here too?
vsm 2013/08/19 17:00:10 Done.
277 'xml:lang', 'x-bar'), throws);
278
279 // Parser
280
281 createElementFromHtml(html) {
282 var container = new DivElement()..innerHtml = html;
283 return container.firstChild;
284 }
285
286 var fooParsed = createElementFromHtml('<x-foo>');
287 expect(fooParsed is CustomFoo, isTrue);
288
289 var barParsed = createElementFromHtml('<input is=x-bar>');
290 expect(barParsed is CustomBar2, isTrue);
291 expect(isFormControl(barParsed), isTrue);
292
293 var divFooParsed = createElementFromHtml('<div is=x-foo>');
294 expect(divFooParsed is CustomFoo, isFalse);
295 expect(divFooParsed is DivElement, isTrue);
296
297 var namedBarParsed = createElementFromHtml('<x-bar>');
298 expect(namedBarParsed is CustomBar2, isFalse);
299 expect(namedBarParsed is UnknownElement, isFalse);
300 expect(namedBarParsed is HtmlElement, isTrue);
301
302 var divBarParsed = createElementFromHtml('<div is=x-bar>');
303 expect(divBarParsed is CustomBar2, isFalse);
304 expect(divBarParsed is DivElement, isTrue);
305 });
306 });
307 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698