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 polymer.test.unbind_test; |
| 6 |
| 7 import 'dart:async' show Future, scheduleMicrotask; |
| 8 import 'dart:html'; |
| 9 |
| 10 @MirrorsUsed(targets: const [Polymer], override: 'polymer.test.unbind_test') |
| 11 import 'dart:mirrors' show reflect, reflectClass, MirrorSystem, MirrorsUsed; |
| 12 |
| 13 import 'package:polymer/polymer.dart'; |
| 14 import 'package:unittest/unittest.dart'; |
| 15 import 'package:unittest/html_config.dart'; |
| 16 |
| 17 @CustomTag('x-test') |
| 18 class XTest extends PolymerElement { |
| 19 @observable var foo = ''; |
| 20 @observable var bar; |
| 21 |
| 22 bool fooWasChanged = false; |
| 23 var validBar; |
| 24 |
| 25 factory XTest() => new Element.tag('x-test'); |
| 26 XTest.created() : super.created(); |
| 27 |
| 28 ready() {} |
| 29 |
| 30 fooChanged() { |
| 31 fooWasChanged = true; |
| 32 } |
| 33 |
| 34 barChanged() { |
| 35 validBar = bar; |
| 36 } |
| 37 |
| 38 bool get isBarValid => validBar == bar; |
| 39 } |
| 40 |
| 41 main() => initPolymer().then((zone) => zone.run(() { |
| 42 useHtmlConfiguration(); |
| 43 |
| 44 setUp(() => Polymer.onReady); |
| 45 |
| 46 test('unbind', unbindTests); |
| 47 })); |
| 48 |
| 49 Future testAsync(List<Function> tests, int delayMs, [List args]) { |
| 50 if (tests.length == 0) return new Future.value(); |
| 51 // TODO(jmesserly): CustomElements.takeRecords(); |
| 52 return new Future.delayed(new Duration(milliseconds: delayMs), () { |
| 53 if (args == null) args = []; |
| 54 var lastArgs = Function.apply(tests.removeAt(0), args); |
| 55 return testAsync(tests, delayMs, lastArgs); |
| 56 }); |
| 57 } |
| 58 |
| 59 // TODO(sorvell): In IE, the unbind time is indeterminate, so wait an |
| 60 // extra beat. |
| 61 delay(x) => new Future.delayed(new Duration(milliseconds: 50), () => x); |
| 62 |
| 63 // TODO(jmesserly): fix this when it's easier to get a private symbol. |
| 64 final unboundSymbol = reflectClass(Polymer).declarations.keys |
| 65 .firstWhere((s) => MirrorSystem.getName(s) == '_unbound'); |
| 66 final observersSymbol = reflectClass(Polymer).declarations.keys |
| 67 .firstWhere((s) => MirrorSystem.getName(s) == '_observers'); |
| 68 |
| 69 _unbound(node) => reflect(node).getField(unboundSymbol).reflectee; |
| 70 _observerCount(node) => |
| 71 reflect(node).getField(observersSymbol).reflectee.length; |
| 72 |
| 73 unbindTests() { |
| 74 var xTest = document.querySelector('x-test'); |
| 75 xTest.foo = 'bar'; |
| 76 scheduleMicrotask(Observable.dirtyCheck); |
| 77 |
| 78 return delay(null).then((_) { |
| 79 expect(_unbound(xTest), null, reason: 'element is bound when inserted'); |
| 80 expect(xTest.fooWasChanged, true, reason: 'element is actually bound'); |
| 81 // Dart note: we don't have a way to check for the global count of |
| 82 // observables/bindables, so we just check the count in the node. |
| 83 expect(_observerCount(xTest), greaterThan(0)); |
| 84 xTest.remove(); |
| 85 }).then(delay).then((_) { |
| 86 expect(_unbound(xTest), true, reason: 'element is unbound when removed'); |
| 87 expect(_observerCount(xTest), 0); |
| 88 return new XTest(); |
| 89 }).then(delay).then((node) { |
| 90 expect(_unbound(node), null, reason: 'element is bound when not inserted'); |
| 91 node.foo = 'bar'; |
| 92 expect(_observerCount(node), greaterThan(0)); |
| 93 scheduleMicrotask(Observable.dirtyCheck); |
| 94 return node; |
| 95 }).then(delay).then((node) { |
| 96 expect(node.fooWasChanged, true, reason: 'node is actually bound'); |
| 97 node.unbindAll(); |
| 98 var n = new XTest(); |
| 99 n.cancelUnbindAll(); |
| 100 return n; |
| 101 }).then(delay).then((node) { |
| 102 expect(_unbound(node), null, |
| 103 reason: 'element is bound when cancelUnbindAll is called'); |
| 104 expect(_observerCount(node), greaterThan(0)); |
| 105 node.unbindAll(); |
| 106 expect(_unbound(node), true, |
| 107 reason: 'element is unbound when unbindAll is called'); |
| 108 expect(_observerCount(node), 0); |
| 109 var n = new XTest()..id = 'foobar!!!'; |
| 110 document.body.append(n); |
| 111 return n; |
| 112 }).then(delay).then((node) { |
| 113 expect(_unbound(node), null, |
| 114 reason: 'element is bound when manually inserted'); |
| 115 expect(_observerCount(node), greaterThan(0)); |
| 116 node.remove(); |
| 117 return node; |
| 118 }).then(delay).then((node) { |
| 119 expect(_unbound(node), true, |
| 120 reason: 'element is unbound when manually removed is called'); |
| 121 expect(_observerCount(node), 0); |
| 122 }); |
| 123 } |
OLD | NEW |