OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "apps/moterm/key_util.h" | 5 #include "apps/moterm/key_util.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "mojo/services/input_events/public/interfaces/input_events.mojom.h" | 9 #include "mojo/services/input_events/public/interfaces/input_events.mojom.h" |
10 #include "mojo/services/input_events/public/interfaces/input_key_codes.mojom.h" | 10 #include "mojo/services/input_events/public/interfaces/input_key_codes.mojom.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 | 12 |
13 namespace { | 13 namespace { |
14 | 14 |
15 // Makes a key event. Note that this cheats in a number of respects. | 15 // Makes a key event. Note that this cheats in a number of respects. |
16 // TODO(vtl): Add a |flags| argument when |GetInputSequenceForKeyPressedEvent()| | 16 // TODO(vtl): Add a |flags| argument when |GetInputSequenceForKeyPressedEvent()| |
17 // actually uses it. | 17 // actually uses it. |
18 mojo::EventPtr MakeKeyEvent(bool is_char, | 18 mojo::EventPtr MakeKeyEvent(bool is_char, |
19 char character, | 19 char character, |
20 mojo::KeyboardCode windows_key_code) { | 20 mojo::KeyboardCode windows_key_code) { |
21 uint16_t character16 = static_cast<unsigned char>(character); | 21 uint16_t character16 = static_cast<unsigned char>(character); |
22 | 22 |
23 mojo::EventPtr ev = mojo::Event::New(); | 23 mojo::EventPtr ev = mojo::Event::New(); |
24 ev->action = mojo::EVENT_TYPE_KEY_PRESSED; | 24 ev->action = mojo::EventType::KEY_PRESSED; |
25 ev->flags = mojo::EVENT_FLAGS_NONE; // Cheat. | 25 ev->flags = mojo::EventFlags::NONE; // Cheat. |
26 ev->time_stamp = 1234567890LL; | 26 ev->time_stamp = 1234567890LL; |
27 ev->key_data = mojo::KeyData::New(); | 27 ev->key_data = mojo::KeyData::New(); |
28 ev->key_data->key_code = 0; // Cheat. | 28 ev->key_data->key_code = 0; // Cheat. |
29 ev->key_data->is_char = is_char; | 29 ev->key_data->is_char = is_char; |
30 ev->key_data->character = character16; | 30 ev->key_data->character = character16; |
31 ev->key_data->windows_key_code = windows_key_code; | 31 ev->key_data->windows_key_code = windows_key_code; |
32 ev->key_data->native_key_code = 0; | 32 ev->key_data->native_key_code = 0; |
33 ev->key_data->text = character16; // Cheat. | 33 ev->key_data->text = character16; // Cheat. |
34 ev->key_data->unmodified_text = character16; // Cheat. | 34 ev->key_data->unmodified_text = character16; // Cheat. |
35 return ev; | 35 return ev; |
36 } | 36 } |
37 | 37 |
38 TEST(KeyUtilTest, RegularChars) { | 38 TEST(KeyUtilTest, RegularChars) { |
39 // Only handles character events. | 39 // Only handles character events. |
40 EXPECT_EQ(std::string(), | 40 EXPECT_EQ(std::string(), |
41 GetInputSequenceForKeyPressedEvent( | 41 GetInputSequenceForKeyPressedEvent( |
42 *MakeKeyEvent(false, 0, mojo::KEYBOARD_CODE_A), false)); | 42 *MakeKeyEvent(false, 0, mojo::KeyboardCode::A), false)); |
43 | 43 |
44 EXPECT_EQ("A", GetInputSequenceForKeyPressedEvent( | 44 EXPECT_EQ("A", GetInputSequenceForKeyPressedEvent( |
45 *MakeKeyEvent(true, 'A', mojo::KEYBOARD_CODE_A), false)); | 45 *MakeKeyEvent(true, 'A', mojo::KeyboardCode::A), false)); |
46 EXPECT_EQ("a", GetInputSequenceForKeyPressedEvent( | 46 EXPECT_EQ("a", GetInputSequenceForKeyPressedEvent( |
47 *MakeKeyEvent(true, 'a', mojo::KEYBOARD_CODE_A), false)); | 47 *MakeKeyEvent(true, 'a', mojo::KeyboardCode::A), false)); |
48 EXPECT_EQ("0", | 48 EXPECT_EQ("0", |
49 GetInputSequenceForKeyPressedEvent( | 49 GetInputSequenceForKeyPressedEvent( |
50 *MakeKeyEvent(true, '0', mojo::KEYBOARD_CODE_NUM_0), false)); | 50 *MakeKeyEvent(true, '0', mojo::KeyboardCode::NUM_0), false)); |
51 EXPECT_EQ("!", | 51 EXPECT_EQ("!", |
52 GetInputSequenceForKeyPressedEvent( | 52 GetInputSequenceForKeyPressedEvent( |
53 *MakeKeyEvent(true, '!', mojo::KEYBOARD_CODE_NUM_0), false)); | 53 *MakeKeyEvent(true, '!', mojo::KeyboardCode::NUM_0), false)); |
54 EXPECT_EQ("\t", | 54 EXPECT_EQ("\t", |
55 GetInputSequenceForKeyPressedEvent( | 55 GetInputSequenceForKeyPressedEvent( |
56 *MakeKeyEvent(true, '\t', mojo::KEYBOARD_CODE_TAB), false)); | 56 *MakeKeyEvent(true, '\t', mojo::KeyboardCode::TAB), false)); |
57 EXPECT_EQ("\r", | 57 EXPECT_EQ("\r", |
58 GetInputSequenceForKeyPressedEvent( | 58 GetInputSequenceForKeyPressedEvent( |
59 *MakeKeyEvent(true, '\r', mojo::KEYBOARD_CODE_RETURN), false)); | 59 *MakeKeyEvent(true, '\r', mojo::KeyboardCode::RETURN), false)); |
60 } | 60 } |
61 | 61 |
62 TEST(KeyUtilTest, SpecialChars) { | 62 TEST(KeyUtilTest, SpecialChars) { |
63 // Backspace should send DEL. | 63 // Backspace should send DEL. |
64 EXPECT_EQ("\x7f", | 64 EXPECT_EQ("\x7f", |
65 GetInputSequenceForKeyPressedEvent( | 65 GetInputSequenceForKeyPressedEvent( |
66 *MakeKeyEvent(true, 0, mojo::KEYBOARD_CODE_BACK), false)); | 66 *MakeKeyEvent(true, 0, mojo::KeyboardCode::BACK), false)); |
67 EXPECT_EQ("\x1b", | 67 EXPECT_EQ("\x1b", |
68 GetInputSequenceForKeyPressedEvent( | 68 GetInputSequenceForKeyPressedEvent( |
69 *MakeKeyEvent(true, 0, mojo::KEYBOARD_CODE_ESCAPE), false)); | 69 *MakeKeyEvent(true, 0, mojo::KeyboardCode::ESCAPE), false)); |
70 // Some multi-character results: | 70 // Some multi-character results: |
71 EXPECT_EQ("\x1b[5~", | 71 EXPECT_EQ("\x1b[5~", |
72 GetInputSequenceForKeyPressedEvent( | 72 GetInputSequenceForKeyPressedEvent( |
73 *MakeKeyEvent(true, 0, mojo::KEYBOARD_CODE_PRIOR), false)); | 73 *MakeKeyEvent(true, 0, mojo::KeyboardCode::PRIOR), false)); |
74 EXPECT_EQ("\x1b[H", | 74 EXPECT_EQ("\x1b[H", |
75 GetInputSequenceForKeyPressedEvent( | 75 GetInputSequenceForKeyPressedEvent( |
76 *MakeKeyEvent(true, 0, mojo::KEYBOARD_CODE_HOME), false)); | 76 *MakeKeyEvent(true, 0, mojo::KeyboardCode::HOME), false)); |
77 EXPECT_EQ("\x1b[C", | 77 EXPECT_EQ("\x1b[C", |
78 GetInputSequenceForKeyPressedEvent( | 78 GetInputSequenceForKeyPressedEvent( |
79 *MakeKeyEvent(true, 0, mojo::KEYBOARD_CODE_RIGHT), false)); | 79 *MakeKeyEvent(true, 0, mojo::KeyboardCode::RIGHT), false)); |
80 } | 80 } |
81 | 81 |
82 TEST(KeyUtilTest, KeypadApplicationMode) { | 82 TEST(KeyUtilTest, KeypadApplicationMode) { |
83 // Page-up should not be affected by keypad application mode. | 83 // Page-up should not be affected by keypad application mode. |
84 EXPECT_EQ("\x1b[5~", | 84 EXPECT_EQ("\x1b[5~", |
85 GetInputSequenceForKeyPressedEvent( | 85 GetInputSequenceForKeyPressedEvent( |
86 *MakeKeyEvent(true, 0, mojo::KEYBOARD_CODE_PRIOR), true)); | 86 *MakeKeyEvent(true, 0, mojo::KeyboardCode::PRIOR), true)); |
87 // But Home (and End) should be. | 87 // But Home (and End) should be. |
88 EXPECT_EQ("\x1bOH", | 88 EXPECT_EQ("\x1bOH", |
89 GetInputSequenceForKeyPressedEvent( | 89 GetInputSequenceForKeyPressedEvent( |
90 *MakeKeyEvent(true, 0, mojo::KEYBOARD_CODE_HOME), true)); | 90 *MakeKeyEvent(true, 0, mojo::KeyboardCode::HOME), true)); |
91 // As should the arroy keys. | 91 // As should the arroy keys. |
92 EXPECT_EQ("\x1bOC", | 92 EXPECT_EQ("\x1bOC", |
93 GetInputSequenceForKeyPressedEvent( | 93 GetInputSequenceForKeyPressedEvent( |
94 *MakeKeyEvent(true, 0, mojo::KEYBOARD_CODE_RIGHT), true)); | 94 *MakeKeyEvent(true, 0, mojo::KeyboardCode::RIGHT), true)); |
95 // TODO(vtl): Test this more thoroughly. Also other keypad keys (once that's | 95 // TODO(vtl): Test this more thoroughly. Also other keypad keys (once that's |
96 // implemented). | 96 // implemented). |
97 } | 97 } |
98 | 98 |
99 } // namespace | 99 } // namespace |
OLD | NEW |