Chromium Code Reviews| 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:logging/logging.dart'; | |
| 11 import 'package:polymer/polymer.dart'; | |
| 12 import 'package:template_binding/template_binding.dart'; | |
| 10 import 'package:unittest/unittest.dart'; | 13 import 'package:unittest/unittest.dart'; |
| 11 import 'package:unittest/html_config.dart'; | 14 import 'package:unittest/html_config.dart'; |
| 12 import 'package:polymer/polymer.dart'; | |
| 13 import 'package:template_binding/template_binding.dart'; | |
| 14 | 15 |
| 15 @CustomTag('x-test') | 16 @CustomTag('x-test') |
| 16 class XTest extends PolymerElement { | 17 class XTest extends PolymerElement { |
| 17 int _testCount = 0; | 18 int _testCount = 0; |
| 18 String _lastEvent; | 19 String _lastEvent; |
| 19 String _lastMessage; | 20 String _lastMessage; |
| 20 List list1 = []; | 21 List list1 = []; |
| 21 List list2 = []; | 22 List list2 = []; |
| 22 final _ready = new Completer(); | 23 final _onTestDone = new Completer(); |
| 23 Future _onTestDone; | |
| 24 | 24 |
| 25 XTest.created() : super.created() { | 25 XTest.created() : super.created(); |
| 26 _onTestDone = _ready.future.then(_runTest); | |
| 27 } | |
| 28 | 26 |
| 29 ready() { | 27 ready() { |
| 30 super.ready(); | 28 super.ready(); |
| 31 for (var i = 0; i < 10; i++) { | 29 for (var i = 0; i < 10; i++) { |
| 32 var model = new MiniModel(this, i); | 30 var model = new MiniModel(this, i); |
| 33 list1.add(model); | 31 list1.add(model); |
| 34 list2.add(model); | 32 list2.add(model); |
| 35 } | 33 } |
| 36 _ready.complete(); | 34 |
| 35 // TODO(jmesserly): is there a cleaner way to do this? | |
| 36 new Future.sync(_runTests).then((x) { _onTestDone.complete(x); }, | |
| 37 onError: (e, s) { _onTestDone.completeError(e, s); }); | |
|
Siggi Cherem (dart-lang)
2013/10/29 22:49:49
I'm still surprised by this. Especially that you n
Jennifer Messerly
2013/10/29 23:07:19
you're right. ready will be called sync from creat
| |
| 37 } | 38 } |
| 38 | 39 |
| 39 hostTapAction(event, detail, node) => _logEvent(event); | 40 hostTapAction(event, detail, node) => _logEvent(event); |
| 40 | 41 |
| 41 divTapAction(event, detail, node) => _logEvent(event); | 42 divTapAction(event, detail, node) => _logEvent(event); |
| 42 | 43 |
| 43 focusAction(event, detail, node) => _logEvent(event); | 44 focusAction(event, detail, node) => _logEvent(event); |
| 44 | 45 |
| 45 blurAction(event, detail, node) => _logEvent(event); | 46 blurAction(event, detail, node) => _logEvent(event); |
| 46 | 47 |
| 47 scrollAction(event, detail, node) => _logEvent(event); | 48 scrollAction(event, detail, node) => _logEvent(event); |
| 48 | 49 |
| 49 itemTapAction(event, detail, node) { | 50 itemTapAction(event, detail, node) { |
| 50 var model = nodeBind(event.target).templateInstance.model; | 51 var model = nodeBind(event.target).templateInstance.model; |
| 51 _logEvent(event, "x-test callback ${model['this']}"); | 52 _logEvent(event, "x-test callback ${model['this']}"); |
| 52 } | 53 } |
| 53 | 54 |
| 54 _logEvent(event, [message]) { | 55 _logEvent(event, [message]) { |
| 55 _testCount++; | 56 _testCount++; |
| 56 _lastEvent = event.type; | 57 _lastEvent = event.type; |
| 57 _lastMessage = message; | 58 _lastMessage = message; |
| 58 } | 59 } |
| 59 | 60 |
| 60 _runTest(_) { | 61 Future _runTests() { |
| 61 fire('tap', toNode: $['div']); | 62 fire('tap', toNode: $['div']); |
| 62 expect(_testCount, 2, reason: 'event heard at div and host'); | 63 expect(_testCount, 2, reason: 'event heard at div and host'); |
| 63 expect(_lastEvent, 'tap', reason: 'tap handled'); | 64 expect(_lastEvent, 'tap', reason: 'tap handled'); |
| 64 fire('focus', toNode: $['input'], canBubble: false); | 65 fire('focus', toNode: $['input'], canBubble: false); |
| 65 expect(_testCount, 3, reason: 'event heard by input'); | 66 expect(_testCount, 3, reason: 'event heard by input'); |
| 66 expect(_lastEvent, 'focus', reason: 'focus handled'); | 67 expect(_lastEvent, 'focus', reason: 'focus handled'); |
| 67 fire('blur', toNode: $['input'], canBubble: false); | 68 fire('blur', toNode: $['input'], canBubble: false); |
| 68 expect(_testCount, 4, reason: 'event heard by input'); | 69 expect(_testCount, 4, reason: 'event heard by input'); |
| 69 expect(_lastEvent, 'blur', reason: 'blur handled'); | 70 expect(_lastEvent, 'blur', reason: 'blur handled'); |
| 70 fire('scroll', toNode: $['list'], canBubble: false); | 71 fire('scroll', toNode: $['list'], canBubble: false); |
| 71 expect(_testCount, 5, reason: 'event heard by list'); | 72 expect(_testCount, 5, reason: 'event heard by list'); |
| 72 expect(_lastEvent, 'scroll', reason: 'scroll handled'); | 73 expect(_lastEvent, 'scroll', reason: 'scroll handled'); |
| 74 | |
| 73 return onMutation($['list']).then((_) { | 75 return onMutation($['list']).then((_) { |
| 74 var l1 = $['list'].querySelectorAll('.list1')[4]; | 76 var l1 = $['list'].querySelectorAll('.list1')[4]; |
| 75 fire('tap', toNode: l1, canBubble: false); | 77 fire('tap', toNode: l1, canBubble: false); |
| 76 expect(_testCount, 6, reason: 'event heard by list1 item'); | 78 expect(_testCount, 6, reason: 'event heard by list1 item'); |
| 77 expect(_lastEvent, 'tap', reason: 'tap handled'); | 79 expect(_lastEvent, 'tap', reason: 'tap handled'); |
| 78 expect(_lastMessage, 'x-test callback <mini-model 4>'); | 80 expect(_lastMessage, 'x-test callback <mini-model 4>'); |
| 79 | 81 |
| 80 var l2 = $['list'].querySelectorAll('.list2')[3]; | 82 var l2 = $['list'].querySelectorAll('.list2')[3]; |
| 81 fire('tap', toNode: l2, canBubble: false); | 83 fire('tap', toNode: l2, canBubble: false); |
| 82 expect(_testCount, 7, reason: 'event heard by list2 item'); | 84 expect(_testCount, 7, reason: 'event heard by list2 item'); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 96 } | 98 } |
| 97 MiniModel(this._element, this.index) { | 99 MiniModel(this._element, this.index) { |
| 98 // TODO(sigmund): remove this and reflect directly on the method. This is | 100 // TODO(sigmund): remove this and reflect directly on the method. This is |
| 99 // needed to work around bug 13002 | 101 // needed to work around bug 13002 |
| 100 itemTapAction = _itemTapAction; | 102 itemTapAction = _itemTapAction; |
| 101 } | 103 } |
| 102 String toString() => "<mini-model $index>"; | 104 String toString() => "<mini-model $index>"; |
| 103 } | 105 } |
| 104 | 106 |
| 105 main() { | 107 main() { |
| 108 Logger.root..level = Level.FINE | |
| 109 ..onRecord.listen((m) => print('${m.loggerName} ${m.message}')); | |
| 110 | |
| 106 initPolymer(); | 111 initPolymer(); |
| 107 } | 112 } |
| 108 | 113 |
| 109 @initMethod _init() { | 114 @initMethod _init() { |
| 110 useHtmlConfiguration(); | 115 useHtmlConfiguration(); |
| 111 | 116 |
| 112 setUp(() => Polymer.onReady); | 117 setUp(() => Polymer.onReady); |
| 113 test('events handled', () => (query('x-test') as XTest)._onTestDone); | 118 test('events handled', () => (query('x-test') as XTest)._onTestDone); |
|
Siggi Cherem (dart-lang)
2013/10/29 22:49:49
should this return _onTestDone.future?
Jennifer Messerly
2013/10/29 23:07:19
fixed. it's now a future. added an expect so we kn
| |
| 114 } | 119 } |
| 115 | 120 |
| OLD | NEW |