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 js_custom_test; |
| 6 |
| 7 import 'package:unittest/unittest.dart'; |
| 8 import 'package:unittest/html_config.dart'; |
| 9 import 'dart:html'; |
| 10 import '../utils.dart'; |
| 11 import 'dart:mirrors'; |
| 12 |
| 13 class A extends HtmlElement { |
| 14 static final tag = 'x-a'; |
| 15 factory A() => new Element.tag(tag); |
| 16 A.created() : super.created() { |
| 17 ncallbacks++; |
| 18 } |
| 19 |
| 20 static int ncallbacks = 0; |
| 21 } |
| 22 |
| 23 main() { |
| 24 useHtmlConfiguration(); |
| 25 |
| 26 // Adapted from Blink's |
| 27 // fast/dom/custom/constructor-calls-created-synchronously test. |
| 28 |
| 29 var registered = false; |
| 30 setUp(() { |
| 31 return customElementsReady.then((_) { |
| 32 if (!registered) { |
| 33 registered = true; |
| 34 document.registerElement(A.tag, A); |
| 35 } |
| 36 }); |
| 37 }); |
| 38 |
| 39 test('accessing custom Dart element from JS', () { |
| 40 var a = new A(); |
| 41 a.id = 'a'; |
| 42 document.body.append(a); |
| 43 |
| 44 var script = ''' |
| 45 document.querySelector('#a').setAttribute('fromJS', 'true'); |
| 46 '''; |
| 47 document.body.append(new ScriptElement()..text = script); |
| 48 |
| 49 expect(a.attributes['fromJS'], 'true'); |
| 50 }); |
| 51 |
| 52 test('accessing custom JS element from Dart', () { |
| 53 var script = ''' |
| 54 var Foo = document.registerElement('x-foo', { |
| 55 prototype: Object.create(HTMLElement.prototype, { |
| 56 createdCallback: { |
| 57 value: function() { |
| 58 this.setAttribute('fromJS', 'true'); |
| 59 } |
| 60 } |
| 61 })}); |
| 62 var foo = new Foo(); |
| 63 foo.id = 'b'; |
| 64 document.body.appendChild(foo); |
| 65 '''; |
| 66 |
| 67 document.body.append(new ScriptElement()..text = script); |
| 68 var custom = document.querySelector('#b'); |
| 69 expect(custom is HtmlElement, isTrue); |
| 70 expect(custom.attributes['fromJS'], 'true'); |
| 71 }); |
| 72 } |
OLD | NEW |