OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library polymer.test.event_handlers_test; | 5 library polymer.test.event_handlers_test; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:html'; | 8 import 'dart:html'; |
9 | 9 |
10 import 'package:polymer/polymer.dart'; | 10 import 'package:polymer/polymer.dart'; |
11 import 'package:template_binding/template_binding.dart'; | 11 import 'package:template_binding/template_binding.dart'; |
12 import 'package:unittest/unittest.dart'; | 12 import 'package:unittest/unittest.dart'; |
13 import 'package:unittest/html_config.dart'; | 13 import 'package:unittest/html_config.dart'; |
14 | 14 |
15 @CustomTag('x-test') | 15 @CustomTag('x-test') |
16 class XTest extends PolymerElement { | 16 class XTest extends PolymerElement { |
17 int _testCount = 0; | 17 int _testCount = 0; |
18 String _lastEvent; | 18 String _lastEvent; |
19 String _lastMessage; | 19 String _lastMessage; |
20 List list1 = toObservable([]); | 20 List list1 = toObservable([]); |
21 List list2 = toObservable([]); | 21 List list2 = toObservable([]); |
22 Future _onTestDone; | 22 Future _onTestDone; |
23 | 23 |
24 XTest.created() : super.created(); | 24 XTest.created() : super.created(); |
25 | 25 |
| 26 @override |
| 27 parseDeclaration(elementElement) { |
| 28 var template = fetchTemplate(elementElement); |
| 29 if (template != null) { |
| 30 lightFromTemplate(template); |
| 31 } |
| 32 } |
| 33 |
26 ready() { | 34 ready() { |
27 super.ready(); | 35 super.ready(); |
28 for (var i = 0; i < 10; i++) { | 36 for (var i = 0; i < 10; i++) { |
29 var model = new MiniModel(this, i); | 37 var model = new MiniModel(this, i); |
30 list1.add(model); | 38 list1.add(model); |
31 list2.add(model); | 39 list2.add(model); |
32 } | 40 } |
33 | 41 |
34 _onTestDone = new Future.sync(_runTests); | 42 _onTestDone = new Future.sync(_runTests); |
35 } | 43 } |
(...skipping 13 matching lines...) Expand all Loading... |
49 _logEvent(event, "x-test callback ${model['this']}"); | 57 _logEvent(event, "x-test callback ${model['this']}"); |
50 } | 58 } |
51 | 59 |
52 _logEvent(event, [message]) { | 60 _logEvent(event, [message]) { |
53 _testCount++; | 61 _testCount++; |
54 _lastEvent = event.type; | 62 _lastEvent = event.type; |
55 _lastMessage = message; | 63 _lastMessage = message; |
56 } | 64 } |
57 | 65 |
58 Future _runTests() { | 66 Future _runTests() { |
59 fire('tap', toNode: $['div']); | 67 fire('tap', onNode: $['div']); |
60 expect(_testCount, 2, reason: 'event heard at div and host'); | 68 expect(_testCount, 2, reason: 'event heard at div and host'); |
61 expect(_lastEvent, 'tap', reason: 'tap handled'); | 69 expect(_lastEvent, 'tap', reason: 'tap handled'); |
62 fire('focus', toNode: $['input'], canBubble: false); | 70 fire('focus', onNode: $['input'], canBubble: false); |
63 expect(_testCount, 3, reason: 'event heard by input'); | 71 expect(_testCount, 3, reason: 'event heard by input'); |
64 expect(_lastEvent, 'focus', reason: 'focus handled'); | 72 expect(_lastEvent, 'focus', reason: 'focus handled'); |
65 fire('blur', toNode: $['input'], canBubble: false); | 73 fire('blur', onNode: $['input'], canBubble: false); |
66 expect(_testCount, 4, reason: 'event heard by input'); | 74 expect(_testCount, 4, reason: 'event heard by input'); |
67 expect(_lastEvent, 'blur', reason: 'blur handled'); | 75 expect(_lastEvent, 'blur', reason: 'blur handled'); |
68 fire('scroll', toNode: $['list'], canBubble: false); | 76 fire('scroll', onNode: $['list'], canBubble: false); |
69 expect(_testCount, 5, reason: 'event heard by list'); | 77 expect(_testCount, 5, reason: 'event heard by list'); |
70 expect(_lastEvent, 'scroll', reason: 'scroll handled'); | 78 expect(_lastEvent, 'scroll', reason: 'scroll handled'); |
71 | 79 |
72 return onMutation($['list']).then((_) { | 80 return onMutation($['list']).then((_) { |
73 var l1 = $['list'].querySelectorAll('.list1')[4]; | 81 var l1 = $['list'].querySelectorAll('.list1')[4]; |
74 fire('tap', toNode: l1, canBubble: false); | 82 fire('tap', onNode: l1, canBubble: false); |
75 expect(_testCount, 6, reason: 'event heard by list1 item'); | 83 expect(_testCount, 6, reason: 'event heard by list1 item'); |
76 expect(_lastEvent, 'tap', reason: 'tap handled'); | 84 expect(_lastEvent, 'tap', reason: 'tap handled'); |
77 expect(_lastMessage, 'x-test callback <mini-model 4>'); | 85 expect(_lastMessage, 'x-test callback <mini-model 4>'); |
78 | 86 |
79 var l2 = $['list'].querySelectorAll('.list2')[3]; | 87 var l2 = $['list'].querySelectorAll('.list2')[3]; |
80 fire('tap', toNode: l2, canBubble: false); | 88 fire('tap', onNode: l2, canBubble: false); |
81 expect(_testCount, 7, reason: 'event heard by list2 item'); | 89 expect(_testCount, 7, reason: 'event heard by list2 item'); |
82 expect(_lastEvent, 'tap', reason: 'tap handled by model'); | 90 expect(_lastEvent, 'tap', reason: 'tap handled by model'); |
83 expect(_lastMessage, 'mini-model callback <mini-model 3>'); | 91 expect(_lastMessage, 'x-test callback x-test'); |
84 }); | 92 }); |
85 } | 93 } |
86 } | 94 } |
87 | 95 |
88 class MiniModel extends Observable { | 96 class MiniModel extends Observable { |
89 XTest _element; | 97 XTest _element; |
90 @observable final int index; | 98 @observable final int index; |
91 @reflectable void itemTapAction(e, d, n) { | 99 @reflectable void itemTapAction(e, d, n) { |
92 _element._logEvent(e, 'mini-model callback $this'); | 100 _element._logEvent(e, 'mini-model callback $this'); |
93 e.stopPropagation(); | 101 e.stopPropagation(); |
94 } | 102 } |
95 MiniModel(this._element, this.index); | 103 MiniModel(this._element, this.index); |
96 String toString() => "<mini-model $index>"; | 104 String toString() => "<mini-model $index>"; |
97 } | 105 } |
98 | 106 |
99 main() => initPolymer(); | 107 main() => initPolymer(); |
100 | 108 |
101 @initMethod init() { | 109 @initMethod init() { |
102 useHtmlConfiguration(); | 110 useHtmlConfiguration(); |
103 | 111 |
104 setUp(() => Polymer.onReady); | 112 setUp(() => Polymer.onReady); |
105 test('events handled', () { | 113 test('events handled', () { |
106 XTest test = querySelector('x-test'); | 114 XTest test = querySelector('x-test'); |
107 expect(test._onTestDone, isNotNull, reason: 'ready was called'); | 115 expect(test._onTestDone, isNotNull, reason: 'ready was called'); |
108 return test._onTestDone; | 116 return test._onTestDone; |
109 }); | 117 }); |
110 } | 118 } |
OLD | NEW |