OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 <stdint.h> |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/macros.h" |
| 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/run_loop.h" |
| 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "chrome/browser/ui/views/ime_driver/input_method_bridge_chromeos.h" |
| 13 #include "content/public/test/test_browser_thread_bundle.h" |
| 14 #include "mojo/public/cpp/bindings/binding.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "ui/base/ime/ime_bridge.h" |
| 17 #include "ui/events/event.h" |
| 18 #include "ui/events/event_constants.h" |
| 19 #include "ui/events/event_utils.h" |
| 20 #include "ui/events/keycodes/dom/dom_code.h" |
| 21 #include "ui/events/keycodes/dom/dom_key.h" |
| 22 #include "ui/events/keycodes/keyboard_code_conversion.h" |
| 23 #include "ui/events/keycodes/keyboard_codes.h" |
| 24 |
| 25 enum class CompositionEventType { |
| 26 SET, |
| 27 CONFIRM, |
| 28 CLEAR, |
| 29 INSERT_TEXT, |
| 30 INSERT_CHAR |
| 31 }; |
| 32 |
| 33 struct CompositionEvent { |
| 34 CompositionEventType type; |
| 35 base::string16 text_data; |
| 36 base::char16 char_data; |
| 37 }; |
| 38 |
| 39 class TestTextInputClient : public ui::mojom::TextInputClient { |
| 40 public: |
| 41 explicit TestTextInputClient(ui::mojom::TextInputClientRequest request) |
| 42 : binding_(this, std::move(request)) {} |
| 43 |
| 44 CompositionEvent WaitUntilCompositionEvent() { |
| 45 if (!receieved_event_.has_value()) { |
| 46 run_loop_ = base::MakeUnique<base::RunLoop>(); |
| 47 run_loop_->Run(); |
| 48 run_loop_.reset(); |
| 49 } |
| 50 CompositionEvent result = receieved_event_.value(); |
| 51 receieved_event_.reset(); |
| 52 return result; |
| 53 } |
| 54 |
| 55 private: |
| 56 void SetCompositionText(const ui::CompositionText& composition) override { |
| 57 CompositionEvent ev = {CompositionEventType::SET, composition.text, 0}; |
| 58 receieved_event_ = ev; |
| 59 if (run_loop_) |
| 60 run_loop_->Quit(); |
| 61 } |
| 62 void ConfirmCompositionText() override { |
| 63 CompositionEvent ev = {CompositionEventType::CONFIRM, base::string16(), 0}; |
| 64 receieved_event_ = ev; |
| 65 if (run_loop_) |
| 66 run_loop_->Quit(); |
| 67 } |
| 68 void ClearCompositionText() override { |
| 69 CompositionEvent ev = {CompositionEventType::CLEAR, base::string16(), 0}; |
| 70 receieved_event_ = ev; |
| 71 if (run_loop_) |
| 72 run_loop_->Quit(); |
| 73 } |
| 74 void InsertText(const std::string& text) override { |
| 75 CompositionEvent ev = {CompositionEventType::INSERT_TEXT, |
| 76 base::UTF8ToUTF16(text), 0}; |
| 77 receieved_event_ = ev; |
| 78 if (run_loop_) |
| 79 run_loop_->Quit(); |
| 80 } |
| 81 void InsertChar(std::unique_ptr<ui::Event> event) override { |
| 82 ASSERT_TRUE(event->IsKeyEvent()); |
| 83 CompositionEvent ev = {CompositionEventType::INSERT_CHAR, base::string16(), |
| 84 event->AsKeyEvent()->GetCharacter()}; |
| 85 receieved_event_ = ev; |
| 86 if (run_loop_) |
| 87 run_loop_->Quit(); |
| 88 } |
| 89 |
| 90 mojo::Binding<ui::mojom::TextInputClient> binding_; |
| 91 std::unique_ptr<base::RunLoop> run_loop_; |
| 92 base::Optional<CompositionEvent> receieved_event_; |
| 93 |
| 94 DISALLOW_COPY_AND_ASSIGN(TestTextInputClient); |
| 95 }; |
| 96 |
| 97 class InputMethodBridgeChromeOSTest : public testing::Test { |
| 98 public: |
| 99 InputMethodBridgeChromeOSTest() |
| 100 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {} |
| 101 ~InputMethodBridgeChromeOSTest() override {} |
| 102 |
| 103 void SetUp() override { |
| 104 ui::IMEBridge::Initialize(); |
| 105 |
| 106 ui::mojom::TextInputClientPtr client_ptr; |
| 107 client_ = base::MakeUnique<TestTextInputClient>(GetProxy(&client_ptr)); |
| 108 input_method_ = base::MakeUnique<InputMethodBridge>(std::move(client_ptr)); |
| 109 } |
| 110 |
| 111 bool ProcessKeyEvent(std::unique_ptr<ui::Event> event) { |
| 112 handled_.reset(); |
| 113 |
| 114 input_method_->ProcessKeyEvent( |
| 115 std::move(event), |
| 116 base::Bind(&InputMethodBridgeChromeOSTest::ProcessKeyEventCallback, |
| 117 base::Unretained(this))); |
| 118 |
| 119 if (!handled_.has_value()) { |
| 120 run_loop_ = base::MakeUnique<base::RunLoop>(); |
| 121 run_loop_->Run(); |
| 122 run_loop_.reset(); |
| 123 } |
| 124 |
| 125 return handled_.value(); |
| 126 } |
| 127 |
| 128 std::unique_ptr<ui::Event> UnicodeKeyPress(ui::KeyboardCode vkey, |
| 129 ui::DomCode code, |
| 130 int flags, |
| 131 base::char16 character) const { |
| 132 return base::MakeUnique<ui::KeyEvent>(ui::ET_KEY_PRESSED, vkey, code, flags, |
| 133 ui::DomKey::FromCharacter(character), |
| 134 ui::EventTimeForNow()); |
| 135 } |
| 136 |
| 137 protected: |
| 138 void ProcessKeyEventCallback(bool handled) { |
| 139 handled_ = handled; |
| 140 if (run_loop_) |
| 141 run_loop_->Quit(); |
| 142 } |
| 143 |
| 144 content::TestBrowserThreadBundle thread_bundle_; |
| 145 std::unique_ptr<TestTextInputClient> client_; |
| 146 std::unique_ptr<InputMethodBridge> input_method_; |
| 147 std::unique_ptr<base::RunLoop> run_loop_; |
| 148 base::Optional<bool> handled_; |
| 149 |
| 150 DISALLOW_COPY_AND_ASSIGN(InputMethodBridgeChromeOSTest); |
| 151 }; |
| 152 |
| 153 // Tests if hexadecimal composition provided by ui::CharacterComposer works |
| 154 // correctly. ui::CharacterComposer is tried if no input method extensions |
| 155 // have been registered yet. |
| 156 TEST_F(InputMethodBridgeChromeOSTest, HexadecimalComposition) { |
| 157 struct { |
| 158 ui::KeyboardCode vkey; |
| 159 ui::DomCode code; |
| 160 int flags; |
| 161 base::char16 character; |
| 162 std::string composition_text; |
| 163 } kTestSequence[] = { |
| 164 {ui::VKEY_U, ui::DomCode::US_U, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN, |
| 165 'U', "u"}, |
| 166 {ui::VKEY_3, ui::DomCode::DIGIT3, 0, '3', "u3"}, |
| 167 {ui::VKEY_0, ui::DomCode::DIGIT0, 0, '0', "u30"}, |
| 168 {ui::VKEY_4, ui::DomCode::DIGIT4, 0, '4', "u304"}, |
| 169 {ui::VKEY_2, ui::DomCode::DIGIT2, 0, '2', "u3042"}, |
| 170 }; |
| 171 |
| 172 // Send the Ctrl-Shift-U,3,4,0,2 sequence. |
| 173 for (size_t i = 0; i < arraysize(kTestSequence); i++) { |
| 174 EXPECT_TRUE(ProcessKeyEvent( |
| 175 UnicodeKeyPress(kTestSequence[i].vkey, kTestSequence[i].code, |
| 176 kTestSequence[i].flags, kTestSequence[i].character))); |
| 177 CompositionEvent ev = client_->WaitUntilCompositionEvent(); |
| 178 EXPECT_EQ(CompositionEventType::SET, ev.type); |
| 179 EXPECT_EQ(base::UTF8ToUTF16(kTestSequence[i].composition_text), |
| 180 ev.text_data); |
| 181 } |
| 182 |
| 183 // Press the return key and verify that the composition text was converted |
| 184 // to the desired text. |
| 185 EXPECT_TRUE(ProcessKeyEvent( |
| 186 UnicodeKeyPress(ui::VKEY_RETURN, ui::DomCode::ENTER, 0, '\r'))); |
| 187 CompositionEvent ev = client_->WaitUntilCompositionEvent(); |
| 188 EXPECT_EQ(CompositionEventType::INSERT_TEXT, ev.type); |
| 189 EXPECT_EQ(base::string16(1, 0x3042), ev.text_data); |
| 190 } |
| 191 |
| 192 // Test that Ctrl-C, Ctrl-X, and Ctrl-V are not handled. |
| 193 TEST_F(InputMethodBridgeChromeOSTest, ClipboardAccelerators) { |
| 194 EXPECT_FALSE(ProcessKeyEvent(UnicodeKeyPress(ui::VKEY_C, ui::DomCode::US_C, |
| 195 ui::EF_CONTROL_DOWN, 'C'))); |
| 196 EXPECT_FALSE(ProcessKeyEvent(UnicodeKeyPress(ui::VKEY_X, ui::DomCode::US_X, |
| 197 ui::EF_CONTROL_DOWN, 'X'))); |
| 198 EXPECT_FALSE(ProcessKeyEvent(UnicodeKeyPress(ui::VKEY_V, ui::DomCode::US_V, |
| 199 ui::EF_CONTROL_DOWN, 'V'))); |
| 200 } |
OLD | NEW |