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

Side by Side Diff: pkg/template_binding/test/custom_element_bindings_test.dart

Issue 815843002: delete template binding from the repo (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years 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 template_binding.test.custom_element_bindings_test;
6
7 import 'dart:async';
8 import 'dart:html';
9 import 'dart:collection' show MapView;
10 import 'package:template_binding/template_binding.dart';
11 import 'package:observe/observe.dart';
12 import 'package:unittest/html_config.dart';
13 import 'package:unittest/unittest.dart';
14 import 'package:web_components/polyfill.dart';
15 import 'utils.dart';
16
17 Future _registered;
18
19 main() => dirtyCheckZone().run(() {
20 useHtmlConfiguration();
21
22 _registered = customElementsReady.then((_) {
23 document.registerElement('my-custom-element', MyCustomElement);
24 });
25
26 group('Custom Element Bindings', customElementBindingsTest);
27 });
28
29 customElementBindingsTest() {
30 setUp(() {
31 document.body.append(testDiv = new DivElement());
32 return _registered;
33 });
34
35 tearDown(() {
36 testDiv.remove();
37 testDiv = null;
38 });
39
40 test('override bind/bindFinished', () {
41 var element = new MyCustomElement();
42 var model = toObservable({'a': new Point(123, 444), 'b': new Monster(100)});
43
44 var pointBinding = nodeBind(element)
45 .bind('my-point', new PathObserver(model, 'a'));
46
47 var scaryBinding = nodeBind(element)
48 .bind('scary-monster', new PathObserver(model, 'b'));
49
50 expect(element.attributes, isNot(contains('my-point')));
51 expect(element.attributes, isNot(contains('scary-monster')));
52
53 expect(element.myPoint, model['a']);
54 expect(element.scaryMonster, model['b']);
55
56 model['a'] = null;
57 return new Future(() {
58 expect(element.myPoint, null);
59 expect(element.bindFinishedCalled, 0);
60 pointBinding.close();
61
62 model['a'] = new Point(1, 2);
63 model['b'] = new Monster(200);
64 }).then(endOfMicrotask).then((_) {
65 expect(element.scaryMonster, model['b']);
66 expect(element.myPoint, null, reason: 'a was unbound');
67
68 scaryBinding.close();
69 model['b'] = null;
70 }).then(endOfMicrotask).then((_) {
71 expect(element.scaryMonster.health, 200);
72 expect(element.bindFinishedCalled, 0);
73 });
74 });
75
76 test('template bind uses overridden custom element bind', () {
77
78 var model = toObservable({'a': new Point(123, 444), 'b': new Monster(100)});
79 var div = createTestHtml('<template bind>'
80 '<my-custom-element my-point="{{a}}" scary-monster="{{b}}">'
81 '</my-custom-element>'
82 '</template>');
83
84 templateBind(div.query('template')).model = model;
85 var element;
86 return new Future(() {
87 element = div.nodes[1];
88
89 expect(element is MyCustomElement, true,
90 reason: '$element should be a MyCustomElement');
91
92 expect(element.myPoint, model['a']);
93 expect(element.scaryMonster, model['b']);
94
95 expect(element.attributes, isNot(contains('my-point')));
96 expect(element.attributes, isNot(contains('scary-monster')));
97
98 expect(element.bindFinishedCalled, 1);
99
100 model['a'] = null;
101 }).then(endOfMicrotask).then((_) {
102 expect(element.myPoint, null);
103 expect(element.bindFinishedCalled, 1);
104
105
106 templateBind(div.query('template')).model = null;
107 }).then(endOfMicrotask).then((_) {
108 // Note: the detached element
109 expect(element.parentNode is DocumentFragment, true,
110 reason: 'removed element is added back to its document fragment');
111 expect(element.parentNode.parentNode, null,
112 reason: 'document fragment is detached');
113 expect(element.bindFinishedCalled, 1);
114
115 model['a'] = new Point(1, 2);
116 model['b'] = new Monster(200);
117 }).then(endOfMicrotask).then((_) {
118 expect(element.myPoint, null, reason: 'model was unbound');
119 expect(element.scaryMonster.health, 100, reason: 'model was unbound');
120 expect(element.bindFinishedCalled, 1);
121 });
122 });
123
124 }
125
126 class Monster {
127 int health;
128 Monster(this.health);
129 }
130
131 /** Demonstrates a custom element overriding bind/bindFinished. */
132 class MyCustomElement extends HtmlElement implements NodeBindExtension {
133 Point myPoint;
134 Monster scaryMonster;
135 int bindFinishedCalled = 0;
136
137 factory MyCustomElement() => new Element.tag('my-custom-element');
138
139 MyCustomElement.created() : super.created();
140
141 Bindable bind(String name, value, {oneTime: false}) {
142 switch (name) {
143 case 'my-point':
144 case 'scary-monster':
145 attributes.remove(name);
146 if (oneTime) {
147 _setProperty(name, value);
148 return null;
149 }
150 _setProperty(name, value.open((x) => _setProperty(name, x)));
151
152 if (!enableBindingsReflection) return value;
153 if (bindings == null) bindings = {};
154 var old = bindings[name];
155 if (old != null) old.close();
156 return bindings[name] = value;
157 }
158 return nodeBindFallback(this).bind(name, value, oneTime: oneTime);
159 }
160
161 void bindFinished() {
162 bindFinishedCalled++;
163 }
164
165 get bindings => nodeBindFallback(this).bindings;
166 set bindings(x) => nodeBindFallback(this).bindings = x;
167 get templateInstance => nodeBindFallback(this).templateInstance;
168
169 void _setProperty(String property, newValue) {
170 if (property == 'my-point') myPoint = newValue;
171 if (property == 'scary-monster') scaryMonster = newValue;
172 }
173 }
174
OLDNEW
« no previous file with comments | « pkg/template_binding/test/binding_syntax.dart ('k') | pkg/template_binding/test/custom_element_bindings_test.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698