Chromium Code Reviews| Index: pkg/polymer/test/unbind_test.dart |
| diff --git a/pkg/polymer/test/unbind_test.dart b/pkg/polymer/test/unbind_test.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4a3f9e1c0058155dd1ef6cca5a0a63e38f7b6641 |
| --- /dev/null |
| +++ b/pkg/polymer/test/unbind_test.dart |
| @@ -0,0 +1,117 @@ |
| +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +import 'dart:async' show Future; |
| +import 'dart:html'; |
| +import 'dart:mirrors' show reflect, reflectClass, MirrorSystem; |
| +import 'package:custom_element/custom_element.dart' show createElement; |
| +import 'package:observe/src/microtask.dart' show performMicrotaskCheckpoint; |
| +import 'package:polymer/polymer.dart'; |
| +import 'package:polymer/platform.dart' as Platform; |
| +import 'package:unittest/unittest.dart'; |
| +import 'package:unittest/html_config.dart'; |
| + |
| +@CustomTag('x-test') |
| +class XTest extends PolymerElement { |
| + @observable var foo = ''; |
| + @observable var bar; |
| + |
| + bool forceReady = true; |
| + bool fooWasChanged = false; |
| + var validBar; |
| + |
| + ready() {} |
| + |
| + fooChanged() { |
| + fooWasChanged = true; |
| + } |
| + |
| + barChanged() { |
| + validBar = bar; |
| + } |
| + |
| + bool get isBarValid => validBar == bar; |
| +} |
| + |
| +main() { |
| + useHtmlConfiguration(); |
| + |
| + test('unbind', unbindTests); |
| +} |
| + |
| +Future testAsync(List<Function> tests, int delayMs, [List args]) { |
| + if (tests.length == 0) return new Future.value(); |
| + // TODO(jmesserly): CustomElements.takeRecords(); |
| + return new Future.delayed(new Duration(milliseconds: delayMs), () { |
| + if (args == null) args = []; |
| + var lastArgs = Function.apply(tests.removeAt(0), args); |
| + return testAsync(tests, delayMs, lastArgs); |
| + }); |
| +} |
| + |
| +unbindTests() { |
| + |
| + var xTest = document.query('x-test').xtag; |
| + xTest.foo = 'bar'; |
| + Platform.flush(); |
| + |
| + // TODO(jmesserly): fix this when it's easier to get a private symbol. |
| + final unboundSymbol = reflectClass(PolymerElement).variables.keys |
| + .firstWhere((s) => MirrorSystem.getName(s) == '_unbound'); |
| + |
| + _unbound(node) => reflect(node).getField(unboundSymbol).reflectee; |
| + |
| + return testAsync([ |
| + () { |
| + expect(_unbound(xTest), null, reason: |
| + 'element is bound when inserted'); |
| + expect(xTest.fooWasChanged, true, reason: |
| + 'element is actually bound'); |
| + xTest.remove(); |
| + }, |
|
Siggi Cherem (dart-lang)
2013/10/04 21:13:46
totally optional, but since every callback takes a
|
| + () { |
| + expect(_unbound(xTest), true, reason: |
| + 'element is unbound when removed'); |
| + return [createElement('x-test').xtag]; |
| + }, |
| + (node) { |
| + expect(_unbound(node), null, reason: |
| + 'element is bound when not inserted'); |
| + node.foo = 'bar'; |
| + Platform.flush(); |
| + return [node]; |
| + }, |
| + (node) { |
| + expect(node.fooWasChanged, true, reason: 'node is actually bound'); |
| + var n = createElement('x-test').xtag; |
| + n.cancelUnbindAll(); |
| + return [n]; |
| + }, |
| + (node) { |
| + expect(_unbound(node), null, reason: |
| + 'element is bound when cancelUnbindAll is called'); |
| + node.unbindAll(); |
| + expect(_unbound(node), true, reason: |
| + 'element is unbound when unbindAll is called'); |
| + var n = createElement('x-test')..id = 'foobar!!!'; |
| + document.body.append(n); |
| + // TODO(jmesserly): this is used to force the CustomElement polyfill to |
| + // recognize the inserted node. |
| + performMicrotaskCheckpoint(); |
| + return [n.xtag]; |
| + }, |
| + (node) { |
| + expect(_unbound(node), null, reason: |
| + 'element is bound when manually inserted'); |
| + node.remove(); |
| + return [node]; |
| + }, |
| + (node) { |
| + expect(_unbound(node), true, reason: |
| + 'element is unbound when manually removed is called'); |
| + } |
| + // TODO(sorvell): In IE, the unbind time is indeterminate, so wait an |
| + // extra beat. |
| + ], 50); |
| +} |