| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 import 'dart:async'; |
| 5 import 'dart:html'; | 6 import 'dart:html'; |
| 6 import 'package:unittest/unittest.dart'; | 7 import 'package:unittest/unittest.dart'; |
| 7 import 'package:unittest/html_config.dart'; | 8 import 'package:unittest/html_config.dart'; |
| 8 import 'package:polymer/platform.dart' as Platform; | 9 import 'package:polymer/platform.dart' as Platform; |
| 9 import 'package:polymer/polymer.dart'; | 10 import 'package:polymer/polymer.dart'; |
| 10 | 11 |
| 11 class XFoo extends PolymerElement { | 12 class XFoo extends PolymerElement { |
| 12 XFoo.created() : super.created(); | 13 XFoo.created() : super.created(); |
| 13 | 14 |
| 14 @observable var foo = ''; | 15 @observable var foo = ''; |
| 15 @observable String baz = ''; | 16 @observable String baz = ''; |
| 16 } | 17 } |
| 17 | 18 |
| 18 class XBar extends XFoo { | 19 class XBar extends XFoo { |
| 19 XBar.created() : super.created(); | 20 XBar.created() : super.created(); |
| 20 | 21 |
| 21 @observable int zot = 3; | 22 @observable int zot = 3; |
| 22 @observable bool zim = false; | 23 @observable bool zim = false; |
| 23 @observable String str = 'str'; | 24 @observable String str = 'str'; |
| 24 @observable Object obj; | 25 @observable Object obj; |
| 25 } | 26 } |
| 26 | 27 |
| 27 class XCompose extends PolymerElement { | 28 class XCompose extends PolymerElement { |
| 28 XCompose.created() : super.created(); | 29 XCompose.created() : super.created(); |
| 29 | 30 |
| 30 @observable bool zim = false; | 31 @observable bool zim = false; |
| 31 } | 32 } |
| 32 | 33 |
| 34 Future onAttributeChange(Element node) { |
| 35 var completer = new Completer(); |
| 36 new MutationObserver((records, observer) { |
| 37 observer.disconnect(); |
| 38 completer.complete(); |
| 39 })..observe(node, attributes: true); |
| 40 return completer.future; |
| 41 } |
| 42 |
| 33 @initMethod _main() { | 43 @initMethod _main() { |
| 34 useHtmlConfiguration(); | 44 useHtmlConfiguration(); |
| 35 | 45 |
| 46 setUp(() => Polymer.onReady); |
| 47 |
| 36 // Most tests use @CustomTag, here we test out the impertive register: | 48 // Most tests use @CustomTag, here we test out the impertive register: |
| 37 Polymer.register('x-foo', XFoo); | 49 Polymer.register('x-foo', XFoo); |
| 38 Polymer.register('x-bar', XBar); | 50 Polymer.register('x-bar', XBar); |
| 39 Polymer.register('x-compose', XCompose); | 51 Polymer.register('x-compose', XCompose); |
| 40 | 52 |
| 41 test('property attribute reflection', () { | 53 test('property attribute reflection', () { |
| 42 var xcompose = query('x-compose').xtag; | 54 var xcompose = query('x-compose').xtag; |
| 43 var xfoo = query('x-foo').xtag; | 55 var xfoo = query('x-foo').xtag; |
| 56 var xbar = query('x-bar').xtag; |
| 44 xfoo.foo = 5; | 57 xfoo.foo = 5; |
| 45 Platform.flush(); | 58 return onAttributeChange(xfoo).then((_) { |
| 46 Platform.endOfMicrotask(expectAsync0(() { | |
| 47 expect(xcompose.$['bar'].attributes.containsKey('zim'), false, | 59 expect(xcompose.$['bar'].attributes.containsKey('zim'), false, |
| 48 reason: 'attribute bound to property updates when binding is made'); | 60 reason: 'attribute bound to property updates when binding is made'); |
| 49 | 61 |
| 50 expect('${xfoo.foo}', xfoo.attributes['foo'], | 62 expect('${xfoo.foo}', xfoo.attributes['foo'], |
| 51 reason: 'attribute reflects property as string'); | 63 reason: 'attribute reflects property as string'); |
| 52 xfoo.attributes['foo'] = '27'; | 64 xfoo.attributes['foo'] = '27'; |
| 53 // TODO(jmesserly): why is JS leaving this as a String? From the code it | 65 // TODO(jmesserly): why is JS leaving this as a String? From the code it |
| 54 // looks like it should use the type of 5 and parse it as a number. | 66 // looks like it should use the type of 5 and parse it as a number. |
| 55 expect('${xfoo.foo}', xfoo.attributes['foo'], | 67 expect('${xfoo.foo}', xfoo.attributes['foo'], |
| 56 reason: 'property reflects attribute'); | 68 reason: 'property reflects attribute'); |
| 57 // | 69 // |
| 58 xfoo.baz = 'Hello'; | 70 xfoo.baz = 'Hello'; |
| 59 Platform.flush(); | 71 }).then((_) => onAttributeChange(xfoo)).then((_) { |
| 60 Platform.endOfMicrotask(expectAsync0(() { | 72 expect(xfoo.baz, xfoo.attributes['baz'], |
| 61 expect(xfoo.baz, xfoo.attributes['baz'], | 73 reason: 'attribute reflects property'); |
| 62 reason: 'attribute reflects property'); | 74 // |
| 63 // | 75 xbar.foo = 'foo!'; |
| 64 var xbar = query('x-bar').xtag; | 76 xbar.zot = 27; |
| 65 // | 77 xbar.zim = true; |
| 66 xbar.foo = 'foo!'; | 78 xbar.str = 'str!'; |
| 67 xbar.zot = 27; | 79 xbar.obj = {'hello': 'world'}; |
| 68 xbar.zim = true; | 80 }).then((_) => onAttributeChange(xbar)).then((_) { |
| 69 xbar.str = 'str!'; | 81 expect(xbar.foo, xbar.attributes['foo'], |
| 70 xbar.obj = {'hello': 'world'}; | 82 reason: 'inherited published property is reflected'); |
| 71 Platform.flush(); | 83 expect('${xbar.zot}', xbar.attributes['zot'], |
| 72 Platform.endOfMicrotask(expectAsync0(() { | 84 reason: 'attribute reflects property as number'); |
| 73 expect(xbar.foo, xbar.attributes['foo'], | 85 expect(xbar.attributes['zim'], '', reason: |
| 74 reason: 'inherited published property is reflected'); | 86 'attribute reflects true valued boolean property as ' |
| 75 expect('${xbar.zot}', xbar.attributes['zot'], | 87 'having attribute'); |
| 76 reason: 'attribute reflects property as number'); | 88 expect(xbar.str, xbar.attributes['str'], |
| 77 expect(xbar.attributes['zim'], '', reason: | 89 reason: 'attribute reflects property as published string'); |
| 78 'attribute reflects true valued boolean property as ' | 90 expect(xbar.attributes.containsKey('obj'), false, |
| 79 'having attribute'); | 91 reason: 'attribute does not reflect object property'); |
| 80 expect(xbar.str, xbar.attributes['str'], | 92 xbar.attributes['zim'] = 'false'; |
| 81 reason: 'attribute reflects property as published string'); | 93 xbar.attributes['foo'] = 'foo!!'; |
| 82 expect(xbar.attributes.containsKey('obj'), false, | 94 xbar.attributes['zot'] = '54'; |
| 83 reason: 'attribute does not reflect object property'); | 95 xbar.attributes['str'] = 'str!!'; |
| 84 xbar.attributes['zim'] = 'false'; | 96 xbar.attributes['obj'] = "{'hello': 'world'}"; |
| 85 xbar.attributes['foo'] = 'foo!!'; | 97 expect(xbar.foo, xbar.attributes['foo'], |
| 86 xbar.attributes['zot'] = '54'; | 98 reason: 'property reflects attribute as string'); |
| 87 xbar.attributes['str'] = 'str!!'; | 99 expect(xbar.zot, 54, |
| 88 xbar.attributes['obj'] = "{'hello': 'world'}"; | 100 reason: 'property reflects attribute as number'); |
| 89 expect(xbar.foo, xbar.attributes['foo'], | 101 expect(xbar.zim, false, |
| 90 reason: 'property reflects attribute as string'); | 102 reason: 'property reflects attribute as boolean'); |
| 91 expect(xbar.zot, 54, | 103 expect(xbar.str, 'str!!', |
| 92 reason: 'property reflects attribute as number'); | 104 reason: 'property reflects attribute as published string'); |
| 93 expect(xbar.zim, false, | 105 expect(xbar.obj, {'hello': 'world'}, |
| 94 reason: 'property reflects attribute as boolean'); | 106 reason: 'property reflects attribute as object'); |
| 95 expect(xbar.str, 'str!!', | 107 xbar.zim = false; |
| 96 reason: 'property reflects attribute as published string'); | 108 }).then((_) => onAttributeChange(xbar)).then((_) { |
| 97 expect(xbar.obj, {'hello': 'world'}, | 109 expect(xbar.attributes.containsKey('zim'), false, reason: |
| 98 reason: 'property reflects attribute as object'); | 110 'attribute reflects false valued boolean property as NOT ' |
| 99 xbar.zim = false; | 111 'having attribute'); |
| 100 Platform.flush(); | 112 xbar.obj = 'hi'; |
| 101 Platform.endOfMicrotask(expectAsync0(() { | 113 }).then((_) => onAttributeChange(xbar)).then((_) { |
| 102 expect(xbar.attributes.containsKey('zim'), false, reason: | 114 expect(xbar.attributes['obj'], 'hi', reason: |
| 103 'attribute reflects false valued boolean property as NOT ' | 115 'reflect property based on current type'); |
| 104 'having attribute'); | 116 }); |
| 105 var objAttr = xbar.attributes['obj']; | |
| 106 xbar.obj = 'hi'; | |
| 107 Platform.endOfMicrotask(expectAsync0(() { | |
| 108 expect(xbar.attributes['obj'], objAttr, reason: | |
| 109 'do not reflect property with default type of object'); | |
| 110 })); | |
| 111 })); | |
| 112 })); | |
| 113 })); | |
| 114 })); | |
| 115 }); | 117 }); |
| 116 } | 118 } |
| OLD | NEW |