OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "apps/moterm/key_util.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 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" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 namespace { |
| 14 |
| 15 // Makes a key event. Note that this cheats in a number of respects. |
| 16 // TODO(vtl): Add a |flags| argument when |GetInputSequenceForKeyPressedEvent()| |
| 17 // actually uses it. |
| 18 mojo::EventPtr MakeKeyEvent(bool is_char, |
| 19 char character, |
| 20 mojo::KeyboardCode windows_key_code) { |
| 21 uint16_t character16 = static_cast<unsigned char>(character); |
| 22 |
| 23 mojo::EventPtr ev = mojo::Event::New(); |
| 24 ev->action = mojo::EVENT_TYPE_KEY_PRESSED; |
| 25 ev->flags = mojo::EVENT_FLAGS_NONE; // Cheat. |
| 26 ev->time_stamp = 1234567890LL; |
| 27 ev->key_data = mojo::KeyData::New(); |
| 28 ev->key_data->key_code = 0; // Cheat. |
| 29 ev->key_data->is_char = is_char; |
| 30 ev->key_data->character = character16; |
| 31 ev->key_data->windows_key_code = windows_key_code; |
| 32 ev->key_data->native_key_code = 0; |
| 33 ev->key_data->text = character16; // Cheat. |
| 34 ev->key_data->unmodified_text = character16; // Cheat. |
| 35 return ev; |
| 36 } |
| 37 |
| 38 TEST(KeyUtilTest, RegularChars) { |
| 39 // Only handles character events. |
| 40 EXPECT_EQ(std::string(), GetInputSequenceForKeyPressedEvent( |
| 41 *MakeKeyEvent(false, 0, mojo::KEYBOARD_CODE_A))); |
| 42 |
| 43 EXPECT_EQ("A", GetInputSequenceForKeyPressedEvent( |
| 44 *MakeKeyEvent(true, 'A', mojo::KEYBOARD_CODE_A))); |
| 45 EXPECT_EQ("a", GetInputSequenceForKeyPressedEvent( |
| 46 *MakeKeyEvent(true, 'a', mojo::KEYBOARD_CODE_A))); |
| 47 EXPECT_EQ("0", GetInputSequenceForKeyPressedEvent( |
| 48 *MakeKeyEvent(true, '0', mojo::KEYBOARD_CODE_NUM_0))); |
| 49 EXPECT_EQ("!", GetInputSequenceForKeyPressedEvent( |
| 50 *MakeKeyEvent(true, '!', mojo::KEYBOARD_CODE_NUM_0))); |
| 51 EXPECT_EQ("\t", GetInputSequenceForKeyPressedEvent( |
| 52 *MakeKeyEvent(true, '\t', mojo::KEYBOARD_CODE_TAB))); |
| 53 EXPECT_EQ("\r", GetInputSequenceForKeyPressedEvent( |
| 54 *MakeKeyEvent(true, '\r', mojo::KEYBOARD_CODE_RETURN))); |
| 55 } |
| 56 |
| 57 TEST(KeyUtilTest, SpecialChars) { |
| 58 // Backspace should send DEL. |
| 59 EXPECT_EQ("\x7f", GetInputSequenceForKeyPressedEvent( |
| 60 *MakeKeyEvent(true, 0, mojo::KEYBOARD_CODE_BACK))); |
| 61 EXPECT_EQ("\x1b", GetInputSequenceForKeyPressedEvent( |
| 62 *MakeKeyEvent(true, 0, mojo::KEYBOARD_CODE_ESCAPE))); |
| 63 // Some multi-character results: |
| 64 EXPECT_EQ("\x1b[5~", GetInputSequenceForKeyPressedEvent( |
| 65 *MakeKeyEvent(true, 0, mojo::KEYBOARD_CODE_PRIOR))); |
| 66 EXPECT_EQ("\x1b[C", GetInputSequenceForKeyPressedEvent( |
| 67 *MakeKeyEvent(true, 0, mojo::KEYBOARD_CODE_RIGHT))); |
| 68 } |
| 69 |
| 70 } // namespace |
OLD | NEW |