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 import 'dart:async' show Future; | |
6 import 'dart:html'; | |
7 import 'dart:mirrors' show reflect, reflectClass, MirrorSystem; | |
8 import 'package:custom_element/custom_element.dart' show createElement; | |
9 import 'package:observe/src/microtask.dart' show performMicrotaskCheckpoint; | |
10 import 'package:polymer/polymer.dart'; | |
11 import 'package:polymer/platform.dart' as Platform; | |
12 import 'package:unittest/unittest.dart'; | |
13 import 'package:unittest/html_config.dart'; | |
14 | |
15 @CustomTag('x-test') | |
16 class XTest extends PolymerElement { | |
17 @observable var foo = ''; | |
18 @observable var bar; | |
19 | |
20 bool forceReady = true; | |
21 bool fooWasChanged = false; | |
22 var validBar; | |
23 | |
24 ready() {} | |
25 | |
26 fooChanged() { | |
27 fooWasChanged = true; | |
28 } | |
29 | |
30 barChanged() { | |
31 validBar = bar; | |
32 } | |
33 | |
34 bool get isBarValid => validBar == bar; | |
35 } | |
36 | |
37 main() { | |
38 useHtmlConfiguration(); | |
39 | |
40 test('unbind', unbindTests); | |
41 } | |
42 | |
43 Future testAsync(List<Function> tests, int delayMs, [List args]) { | |
44 if (tests.length == 0) return new Future.value(); | |
45 // TODO(jmesserly): CustomElements.takeRecords(); | |
46 return new Future.delayed(new Duration(milliseconds: delayMs), () { | |
47 if (args == null) args = []; | |
48 var lastArgs = Function.apply(tests.removeAt(0), args); | |
49 return testAsync(tests, delayMs, lastArgs); | |
50 }); | |
51 } | |
52 | |
53 unbindTests() { | |
54 | |
55 var xTest = document.query('x-test').xtag; | |
56 xTest.foo = 'bar'; | |
57 Platform.flush(); | |
58 | |
59 // TODO(jmesserly): fix this when it's easier to get a private symbol. | |
60 final unboundSymbol = reflectClass(PolymerElement).variables.keys | |
61 .firstWhere((s) => MirrorSystem.getName(s) == '_unbound'); | |
62 | |
63 _unbound(node) => reflect(node).getField(unboundSymbol).reflectee; | |
64 | |
65 return testAsync([ | |
66 () { | |
67 expect(_unbound(xTest), null, reason: | |
68 'element is bound when inserted'); | |
69 expect(xTest.fooWasChanged, true, reason: | |
70 'element is actually bound'); | |
71 xTest.remove(); | |
72 }, | |
Siggi Cherem (dart-lang)
2013/10/04 21:13:46
totally optional, but since every callback takes a
| |
73 () { | |
74 expect(_unbound(xTest), true, reason: | |
75 'element is unbound when removed'); | |
76 return [createElement('x-test').xtag]; | |
77 }, | |
78 (node) { | |
79 expect(_unbound(node), null, reason: | |
80 'element is bound when not inserted'); | |
81 node.foo = 'bar'; | |
82 Platform.flush(); | |
83 return [node]; | |
84 }, | |
85 (node) { | |
86 expect(node.fooWasChanged, true, reason: 'node is actually bound'); | |
87 var n = createElement('x-test').xtag; | |
88 n.cancelUnbindAll(); | |
89 return [n]; | |
90 }, | |
91 (node) { | |
92 expect(_unbound(node), null, reason: | |
93 'element is bound when cancelUnbindAll is called'); | |
94 node.unbindAll(); | |
95 expect(_unbound(node), true, reason: | |
96 'element is unbound when unbindAll is called'); | |
97 var n = createElement('x-test')..id = 'foobar!!!'; | |
98 document.body.append(n); | |
99 // TODO(jmesserly): this is used to force the CustomElement polyfill to | |
100 // recognize the inserted node. | |
101 performMicrotaskCheckpoint(); | |
102 return [n.xtag]; | |
103 }, | |
104 (node) { | |
105 expect(_unbound(node), null, reason: | |
106 'element is bound when manually inserted'); | |
107 node.remove(); | |
108 return [node]; | |
109 }, | |
110 (node) { | |
111 expect(_unbound(node), true, reason: | |
112 'element is unbound when manually removed is called'); | |
113 } | |
114 // TODO(sorvell): In IE, the unbind time is indeterminate, so wait an | |
115 // extra beat. | |
116 ], 50); | |
117 } | |
OLD | NEW |