OLD | NEW |
(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 document_register_basic_test; |
| 6 import 'package:unittest/unittest.dart'; |
| 7 import 'package:unittest/html_config.dart'; |
| 8 import 'dart:html'; |
| 9 import '../utils.dart'; |
| 10 |
| 11 class Foo extends HtmlElement { |
| 12 static final tag = 'x-foo'; |
| 13 factory Foo() => new Element.tag(tag); |
| 14 Foo.created() : super.created(); |
| 15 |
| 16 get thisIsACustomClass => true; |
| 17 } |
| 18 |
| 19 class Bar extends HtmlElement { |
| 20 static final tag = 'x-bar'; |
| 21 factory Bar() => new Element.tag(tag); |
| 22 Bar.created() : super.created(); |
| 23 |
| 24 get thisIsACustomClass => true; |
| 25 } |
| 26 |
| 27 class Baz extends Foo { |
| 28 static final tag = 'x-baz'; |
| 29 factory Baz() => new Element.tag(tag); |
| 30 Baz.created() : super.created(); |
| 31 |
| 32 get thisIsAlsoACustomClass => true; |
| 33 } |
| 34 |
| 35 class BadB { |
| 36 } |
| 37 |
| 38 abstract class BadC extends HtmlElement { |
| 39 BadC.created() : super.created(); |
| 40 } |
| 41 |
| 42 class BadF implements HtmlElement { |
| 43 static final tag = 'x-tag-f'; |
| 44 factory BadF() => new Element.tag(tag); |
| 45 } |
| 46 |
| 47 main() { |
| 48 useHtmlConfiguration(); |
| 49 |
| 50 // Adapted from Blink's fast/dom/custom/document-register-basic test. |
| 51 |
| 52 setUp(() => customElementsReady); |
| 53 |
| 54 test('Testing document.registerElement() basic behaviors', () { |
| 55 document.registerElement(Foo.tag, Foo); |
| 56 |
| 57 // Cannot register an existing dart:html type. |
| 58 expect(() => document.registerElement('x-bad-a', HtmlElement), throws); |
| 59 |
| 60 // Invalid user type. Doesn't inherit from HtmlElement. |
| 61 expect(() => document.registerElement('x-bad-b', BadB), throws); |
| 62 |
| 63 // Cannot register abstract class. |
| 64 expect(() => document.registerElement('x-bad-c', BadC), throws); |
| 65 |
| 66 // Not a type. |
| 67 expect(() => document.registerElement('x-bad-d', null), throws); |
| 68 |
| 69 // Cannot register system type. |
| 70 expect(() => document.registerElement('x-bad-e', Object), throws); |
| 71 |
| 72 // Must extend HtmlElement, not just implement it. |
| 73 expect(() => document.registerElement(BadF.tag, BadF), throws); |
| 74 |
| 75 // Constructor initiated instantiation |
| 76 var createdFoo = new Foo(); |
| 77 expect(createdFoo.thisIsACustomClass, isTrue); |
| 78 |
| 79 // Dart type correctness |
| 80 expect(createdFoo is HtmlElement, isTrue); |
| 81 expect(createdFoo is Foo, isTrue); |
| 82 expect(createdFoo.runtimeType, Foo); |
| 83 |
| 84 // Native getter |
| 85 expect(createdFoo.tagName, "X-FOO"); |
| 86 |
| 87 // Native setter |
| 88 createdFoo.innerHtml = "Hello"; |
| 89 expect(createdFoo.text, "Hello"); |
| 90 |
| 91 // Native method |
| 92 var childDiv = new DivElement(); |
| 93 createdFoo.append(childDiv); |
| 94 expect(createdFoo.lastChild, childDiv); |
| 95 |
| 96 // Parser initiated instantiation |
| 97 var container = new DivElement()..id = "container"; |
| 98 document.body.append(container); |
| 99 container.setInnerHtml("<x-foo></x-foo>", |
| 100 treeSanitizer: new NullTreeSanitizer()); |
| 101 upgradeCustomElements(container); |
| 102 var parsedFoo = container.firstChild; |
| 103 |
| 104 expect(parsedFoo is Foo, isTrue); |
| 105 expect(parsedFoo.tagName, "X-FOO"); |
| 106 |
| 107 // Ensuring the wrapper is retained |
| 108 var someProperty = new Expando(); |
| 109 someProperty[parsedFoo] = "hello"; |
| 110 expect(container.firstChild, parsedFoo); |
| 111 expect(someProperty[container.firstChild], someProperty[parsedFoo]); |
| 112 |
| 113 // Having another constructor |
| 114 document.registerElement(Bar.tag, Bar); |
| 115 var createdBar = new Bar(); |
| 116 expect(createdBar is Bar, isTrue); |
| 117 expect(createdBar is Foo, isFalse); |
| 118 expect(createdBar.tagName, "X-BAR"); |
| 119 |
| 120 // Having a subclass |
| 121 document.registerElement(Baz.tag, Baz); |
| 122 var createdBaz = new Baz(); |
| 123 expect(createdBaz.tagName, "X-BAZ"); |
| 124 expect(createdBaz.thisIsACustomClass, isTrue); |
| 125 expect(createdBaz.thisIsAlsoACustomClass, isTrue); |
| 126 |
| 127 // With irregular cases |
| 128 var createdUpperBar = new Element.tag("X-BAR"); |
| 129 var createdMixedBar = new Element.tag("X-Bar"); |
| 130 expect(createdUpperBar is Bar, isTrue); |
| 131 expect(createdUpperBar.tagName, "X-BAR"); |
| 132 expect(createdMixedBar is Bar, isTrue); |
| 133 expect(createdMixedBar.tagName, "X-BAR"); |
| 134 |
| 135 container.setInnerHtml("<X-BAR></X-BAR><X-Bar></X-Bar>", |
| 136 treeSanitizer: new NullTreeSanitizer()); |
| 137 upgradeCustomElements(container); |
| 138 expect(container.firstChild is Bar, isTrue); |
| 139 expect(container.firstChild.tagName, "X-BAR"); |
| 140 expect(container.lastChild is Bar, isTrue); |
| 141 expect(container.lastChild.tagName, "X-BAR"); |
| 142 |
| 143 // Constructors shouldn't interfere with each other |
| 144 expect((new Foo()).tagName, "X-FOO"); |
| 145 expect((new Bar()).tagName, "X-BAR"); |
| 146 expect((new Baz()).tagName, "X-BAZ"); |
| 147 }); |
| 148 } |
OLD | NEW |