OLD | NEW |
1 library KeyboardEventTest; | 1 library KeyboardEventTest; |
2 import '../../pkg/unittest/lib/unittest.dart'; | 2 import '../../pkg/unittest/lib/unittest.dart'; |
3 import '../../pkg/unittest/lib/html_config.dart'; | 3 import '../../pkg/unittest/lib/html_config.dart'; |
4 import 'dart:html'; | 4 import 'dart:html'; |
5 | 5 |
6 // Test that we are correctly determining keyCode and charCode uniformly across | 6 // Test that we are correctly determining keyCode and charCode uniformly across |
7 // browsers. | 7 // browsers. |
8 | 8 |
9 main() { | 9 main() { |
10 | 10 |
11 useHtmlConfiguration(); | 11 useHtmlConfiguration(); |
12 | 12 |
13 keydownHandlerTest(KeyEvent e) { | 13 keydownHandlerTest(KeyEvent e) { |
14 expect(e.charCode, 0); | 14 expect(e.charCode, 0); |
15 } | 15 } |
16 | 16 |
17 test('keyboardEvent constructor', () { | 17 test('keyboardEvent constructor', () { |
18 var event = new KeyboardEvent('keyup'); | 18 var event = new KeyboardEvent('keyup'); |
19 }); | 19 }); |
20 test('keys', () { | 20 test('keys', () { |
21 // This test currently is pretty much a no-op because we | 21 // This test currently is pretty much a no-op because we |
22 // can't (right now) construct KeyboardEvents with specific keycode/charcode | 22 // can't (right now) construct KeyboardEvents with specific keycode/charcode |
23 // values (a KeyboardEvent can be "init"-ed but not all the information can | 23 // values (a KeyboardEvent can be "init"-ed but not all the information can |
24 // be programmatically populated. It exists as an example for how to use | 24 // be programmatically populated. It exists as an example for how to use |
25 // KeyboardEventController more than anything else. | 25 // KeyboardEventController more than anything else. |
26 KeyboardEventStream.onKeyDown(document.body).listen( | 26 var subscription = KeyboardEventStream.onKeyDown(document.body).listen( |
27 keydownHandlerTest); | 27 keydownHandlerTest); |
28 KeyEvent.keyDownEvent.forTarget(document.body).listen(keydownHandlerTest); | 28 var subscription2 = KeyEvent.keyDownEvent.forTarget(document.body).listen( |
29 document.body.onKeyDown.listen((e) => print('regular listener')); | 29 keydownHandlerTest); |
| 30 var subscription3 = document.body.onKeyDown.listen( |
| 31 (e) => print('regular listener')); |
| 32 subscription.cancel(); |
| 33 subscription2.cancel(); |
| 34 subscription3.cancel(); |
| 35 }); |
| 36 |
| 37 test('constructKeyEvent', () { |
| 38 var stream = KeyEvent.keyPressEvent.forTarget(document.body); |
| 39 var subscription = stream.listen((keyEvent) { |
| 40 expect(keyEvent.charCode, 97); |
| 41 expect(keyEvent.keyCode, 65); |
| 42 }); |
| 43 var k = new KeyEvent('keypress', keyCode: 65, charCode: 97); |
| 44 stream.add(k); |
| 45 subscription.cancel(); |
| 46 // Capital "A": |
| 47 stream.add(new KeyEvent('keydown', keyCode: 16, charCode: 0)); |
| 48 |
| 49 subscription = stream.listen((keyEvent) { |
| 50 expect(keyEvent.charCode, 65); |
| 51 expect(keyEvent.keyCode, 65); |
| 52 }); |
| 53 stream.add(new KeyEvent('keypress', keyCode: 65, charCode: 65)); |
| 54 subscription.cancel(); |
| 55 }); |
| 56 |
| 57 test('KeyEventSequence', () { |
| 58 // Press "?" by simulating "shift" and then the key that has "/" and "?" on |
| 59 // it. |
| 60 var streamDown = KeyEvent.keyDownEvent.forTarget(document.body); |
| 61 var streamPress = KeyEvent.keyPressEvent.forTarget(document.body); |
| 62 var streamUp = KeyEvent.keyUpEvent.forTarget(document.body); |
| 63 |
| 64 var subscription = streamDown.listen((keyEvent) { |
| 65 expect(keyEvent.keyCode, isIn([16, 191])); |
| 66 expect(keyEvent.charCode, 0); |
| 67 }); |
| 68 |
| 69 var subscription2 = streamPress.listen((keyEvent) { |
| 70 expect(keyEvent.keyCode, 23); |
| 71 expect(keyEvent.charCode, 63); |
| 72 }); |
| 73 |
| 74 var subscription3 = streamUp.listen((keyEvent) { |
| 75 expect(keyEvent.keyCode, isIn([16, 191])); |
| 76 expect(keyEvent.charCode, 0); |
| 77 }); |
| 78 |
| 79 streamDown.add(new KeyEvent('keydown', keyCode: 16, charCode: 0)); |
| 80 streamDown.add(new KeyEvent('keydown', keyCode: 191, charCode: 0)); |
| 81 |
| 82 streamPress.add(new KeyEvent('keypress', keyCode: 23, charCode: 63)); |
| 83 |
| 84 streamUp.add(new KeyEvent('keyup', keyCode: 191, charCode: 0)); |
| 85 streamUp.add(new KeyEvent('keyup', keyCode: 16, charCode: 0)); |
| 86 subscription.cancel(); |
| 87 subscription2.cancel(); |
| 88 subscription3.cancel(); |
30 }); | 89 }); |
31 } | 90 } |
32 | 91 |
33 | 92 |
OLD | NEW |