| 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 template_wrappers_test; | |
| 6 | |
| 7 import 'dart:html'; | |
| 8 import 'dart:async'; | |
| 9 import 'dart:js' show context, JsObject; | |
| 10 import 'package:unittest/html_config.dart'; | |
| 11 import 'package:unittest/unittest.dart'; | |
| 12 import 'package:web_components/interop.dart'; | |
| 13 import 'package:web_components/polyfill.dart'; | |
| 14 | |
| 15 main() { | |
| 16 useHtmlConfiguration(); | |
| 17 setUp(() => customElementsReady); | |
| 18 | |
| 19 test('interop is supported', () { | |
| 20 expect(isSupported, isTrue); | |
| 21 }); | |
| 22 | |
| 23 test('previously created elements are not upgraded', () { | |
| 24 var a = document.querySelector('x-a'); | |
| 25 expect(a is HtmlElement, isTrue, reason: 'x-a is HtmlElement'); | |
| 26 expect(a is XAWrapper, isFalse, reason: 'x-a should not be upgraded yet'); | |
| 27 expect(_readX(a), 0); | |
| 28 | |
| 29 var b = document.querySelector('[is=x-b]'); | |
| 30 expect(b is DivElement, isTrue, reason: 'x-b is DivElement'); | |
| 31 expect(b is XBWrapper, isFalse, reason: 'x-b should not be upgraded yet'); | |
| 32 expect(_readX(b), 1); | |
| 33 | |
| 34 var d = document.querySelector('x-d'); | |
| 35 expect(d is HtmlElement, isTrue, reason: 'x-d is HtmlElement'); | |
| 36 expect(d is XDWrapper, isFalse, reason: 'x-d should not be upgraded yet'); | |
| 37 expect(_readX(d), 2); | |
| 38 | |
| 39 /// Note: this registration has a global side-effect and is assumed in the | |
| 40 /// following tests. | |
| 41 registerDartType('x-a', XAWrapper); | |
| 42 registerDartType('x-b', XBWrapper, extendsTag: 'div'); | |
| 43 registerDartType('x-c', XCWrapper); | |
| 44 onlyUpgradeNewElements(); | |
| 45 registerDartType('x-d', XDWrapper); // late on purpose. | |
| 46 | |
| 47 a = document.querySelector('x-a'); | |
| 48 expect(a is HtmlElement, isTrue, reason: 'x-a is HtmlElement'); | |
| 49 expect(a is XAWrapper, isTrue, reason: 'x-a is upgraded to XAWrapper'); | |
| 50 expect(a.x, 0); | |
| 51 expect(a.wrapperCount, 0); | |
| 52 | |
| 53 b = document.querySelector('[is=x-b]'); | |
| 54 expect(b is DivElement, isTrue, reason: 'x-b is DivElement'); | |
| 55 expect(b is XBWrapper, isTrue, reason: 'x-b is upgraded to XBWrapper'); | |
| 56 expect(b.x, 1); | |
| 57 expect(b.wrapperCount, 1); | |
| 58 | |
| 59 // x-d was not upgraded because its registration came after we stopped | |
| 60 // upgrading old elements: | |
| 61 d = document.querySelector('x-d'); | |
| 62 expect(d is HtmlElement, isTrue, reason: 'x-d is HtmlElement'); | |
| 63 expect(d is XDWrapper, isFalse, reason: 'x-d should not be upgraded yet'); | |
| 64 expect(_readX(d), 2); | |
| 65 | |
| 66 var c = document.querySelector('x-c'); | |
| 67 expect(c is HtmlElement, isTrue, reason: 'x-c is HtmlElement'); | |
| 68 expect(c is XCWrapper, isFalse, reason: 'x-c should not be upgraded yet'); | |
| 69 expect(_readX(c), null, reason: 'x-c has not been registered in JS yet'); | |
| 70 }); | |
| 71 | |
| 72 test('anything created after registering Dart type is upgraded', () { | |
| 73 context.callMethod('addA'); | |
| 74 var list = document.querySelectorAll('x-a'); | |
| 75 expect(list.length, 2); | |
| 76 var a = list[1]; | |
| 77 expect(a is HtmlElement, isTrue, reason: 'x-a is HtmlElement'); | |
| 78 expect(a is XAWrapper, isTrue, reason: 'x-a is upgraded to XAWrapper'); | |
| 79 expect(a.x, 3); | |
| 80 expect(a.wrapperCount, 2); | |
| 81 | |
| 82 context.callMethod('addB'); | |
| 83 list = document.querySelectorAll('[is=x-b]'); | |
| 84 expect(list.length, 2); | |
| 85 var b = list[1]; | |
| 86 expect(b is DivElement, isTrue, reason: 'x-b is DivElement'); | |
| 87 expect(b is XBWrapper, isTrue, reason: 'x-b is upgraded to XBWrapper'); | |
| 88 expect(b.x, 4); | |
| 89 expect(b.wrapperCount, 3); | |
| 90 | |
| 91 // New instances of x-d should be upgraded regardless. | |
| 92 context.callMethod('addD'); | |
| 93 list = document.querySelectorAll('x-d'); | |
| 94 expect(list.length, 2); | |
| 95 var d = list[1]; | |
| 96 expect(d is HtmlElement, isTrue, reason: 'x-d is HtmlElement'); | |
| 97 expect(d is XDWrapper, isTrue, reason: 'x-d is upgraded to XDWrapper'); | |
| 98 expect(d.x, 5); | |
| 99 expect(d.wrapperCount, 4); | |
| 100 }); | |
| 101 | |
| 102 test('events seen if Dart type is registered before registerElement', () { | |
| 103 var c = document.querySelector('x-c'); | |
| 104 expect(c is XCWrapper, isFalse); | |
| 105 expect(_readX(c), null, reason: 'x-c has not been registered in JS yet'); | |
| 106 | |
| 107 context.callMethod('registerC'); | |
| 108 c = document.querySelector('x-c'); | |
| 109 expect(c is XCWrapper, isTrue); | |
| 110 expect(c.x, 6); | |
| 111 expect(c.wrapperCount, 5); | |
| 112 | |
| 113 context.callMethod('addC'); | |
| 114 var list = document.querySelectorAll('x-c'); | |
| 115 expect(list.length, 2); | |
| 116 expect(list[0], c); | |
| 117 c = list[1]; | |
| 118 expect(c is HtmlElement, isTrue, reason: 'x-c is HtmlElement'); | |
| 119 expect(c is XCWrapper, isTrue, reason: 'x-c is upgraded to XCWrapper'); | |
| 120 expect(c.x, 7); | |
| 121 expect(c.wrapperCount, 6); | |
| 122 }); | |
| 123 | |
| 124 test('element can extend another element', () { | |
| 125 registerDartType('x-e', XEWrapper); | |
| 126 context.callMethod('addE'); | |
| 127 | |
| 128 var e = document.querySelector('x-e'); | |
| 129 expect(e is XEWrapper, isTrue); | |
| 130 expect(e.x, 8); | |
| 131 expect(e.y, 9); | |
| 132 }); | |
| 133 } | |
| 134 int _count = 0; | |
| 135 | |
| 136 abstract class Wrapper { | |
| 137 int wrapperCount = _count++; | |
| 138 int get x => _readX(this); | |
| 139 } | |
| 140 | |
| 141 _readX(e) => new JsObject.fromBrowserObject(e)['x']; | |
| 142 | |
| 143 class XAWrapper extends HtmlElement with Wrapper { | |
| 144 XAWrapper.created() : super.created(); | |
| 145 } | |
| 146 | |
| 147 class XBWrapper extends DivElement with Wrapper { | |
| 148 XBWrapper.created() : super.created(); | |
| 149 } | |
| 150 | |
| 151 class XCWrapper extends HtmlElement with Wrapper { | |
| 152 XCWrapper.created() : super.created(); | |
| 153 } | |
| 154 | |
| 155 class XDWrapper extends HtmlElement with Wrapper { | |
| 156 XDWrapper.created() : super.created(); | |
| 157 } | |
| 158 | |
| 159 class XEWrapper extends HtmlElement with Wrapper { | |
| 160 XEWrapper.created() : super.created(); | |
| 161 | |
| 162 int get y => new JsObject.fromBrowserObject(this)['y']; | |
| 163 } | |
| OLD | NEW |