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