| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 <stdint.h> | 5 #include <stdint.h> |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "mojo/public/cpp/bindings/interface_request.h" | 10 #include "mojo/public/cpp/bindings/interface_request.h" |
| 11 #include "services/service_manager/public/cpp/service_context.h" | 11 #include "services/service_manager/public/cpp/service_context.h" |
| 12 #include "services/service_manager/public/cpp/service_test.h" | 12 #include "services/service_manager/public/cpp/service_test.h" |
| 13 #include "services/ui/public/interfaces/ime.mojom.h" | 13 #include "services/ui/public/interfaces/ime.mojom.h" |
| 14 #include "ui/events/event.h" | 14 #include "ui/events/event.h" |
| 15 | 15 |
| 16 class TestTextInputClient : public ui::mojom::TextInputClient { | 16 class TestTextInputClient : public ui::mojom::TextInputClient { |
| 17 public: | 17 public: |
| 18 explicit TestTextInputClient(ui::mojom::TextInputClientRequest request) | 18 explicit TestTextInputClient(ui::mojom::TextInputClientRequest request) |
| 19 : binding_(this, std::move(request)) {} | 19 : binding_(this, std::move(request)) {} |
| 20 | 20 |
| 21 ui::mojom::CompositionEventPtr WaitUntilCompositionEvent() { | 21 ui::mojom::CompositionEventPtr WaitUntilCompositionEvent() { |
| 22 run_loop_.reset(new base::RunLoop); | 22 if (!receieved_composition_event_) { |
| 23 run_loop_->Run(); | 23 run_loop_.reset(new base::RunLoop); |
| 24 run_loop_.reset(); | 24 run_loop_->Run(); |
| 25 run_loop_.reset(); |
| 26 } |
| 25 | 27 |
| 26 return std::move(receieved_composition_event_); | 28 return std::move(receieved_composition_event_); |
| 27 } | 29 } |
| 28 | 30 |
| 29 ui::Event* WaitUntilUnhandledEvent() { | |
| 30 run_loop_.reset(new base::RunLoop); | |
| 31 run_loop_->Run(); | |
| 32 run_loop_.reset(); | |
| 33 | |
| 34 return unhandled_event_.get(); | |
| 35 } | |
| 36 | |
| 37 private: | 31 private: |
| 38 void OnCompositionEvent(ui::mojom::CompositionEventPtr event) override { | 32 void OnCompositionEvent(ui::mojom::CompositionEventPtr event) override { |
| 39 receieved_composition_event_ = std::move(event); | 33 receieved_composition_event_ = std::move(event); |
| 40 run_loop_->Quit(); | 34 if (run_loop_) |
| 41 } | 35 run_loop_->Quit(); |
| 42 void OnUnhandledEvent(std::unique_ptr<ui::Event> char_event) override { | |
| 43 unhandled_event_ = std::move(char_event); | |
| 44 run_loop_->Quit(); | |
| 45 } | 36 } |
| 46 | 37 |
| 47 mojo::Binding<ui::mojom::TextInputClient> binding_; | 38 mojo::Binding<ui::mojom::TextInputClient> binding_; |
| 48 std::unique_ptr<base::RunLoop> run_loop_; | 39 std::unique_ptr<base::RunLoop> run_loop_; |
| 49 ui::mojom::CompositionEventPtr receieved_composition_event_; | 40 ui::mojom::CompositionEventPtr receieved_composition_event_; |
| 50 std::unique_ptr<ui::Event> unhandled_event_; | |
| 51 | 41 |
| 52 DISALLOW_COPY_AND_ASSIGN(TestTextInputClient); | 42 DISALLOW_COPY_AND_ASSIGN(TestTextInputClient); |
| 53 }; | 43 }; |
| 54 | 44 |
| 55 class IMEAppTest : public service_manager::test::ServiceTest { | 45 class IMEAppTest : public service_manager::test::ServiceTest { |
| 56 public: | 46 public: |
| 57 IMEAppTest() : ServiceTest("exe:mus_ime_unittests") {} | 47 IMEAppTest() : ServiceTest("exe:mus_ime_unittests") {} |
| 58 ~IMEAppTest() override {} | 48 ~IMEAppTest() override {} |
| 59 | 49 |
| 60 // service_manager::test::ServiceTest: | 50 // service_manager::test::ServiceTest: |
| 61 void SetUp() override { | 51 void SetUp() override { |
| 62 ServiceTest::SetUp(); | 52 ServiceTest::SetUp(); |
| 63 // test_ime_driver will register itself as the current IMEDriver. | 53 // test_ime_driver will register itself as the current IMEDriver. |
| 64 connector()->Connect("service:test_ime_driver"); | 54 connector()->Connect("service:test_ime_driver"); |
| 65 connector()->ConnectToInterface("service:ui", &ime_server_); | 55 connector()->ConnectToInterface("service:ui", &ime_server_); |
| 66 } | 56 } |
| 67 | 57 |
| 58 bool ProcessKeyEvent(ui::mojom::InputMethodPtr* input_method, |
| 59 std::unique_ptr<ui::Event> event) { |
| 60 (*input_method) |
| 61 ->ProcessKeyEvent(std::move(event), |
| 62 base::Bind(&IMEAppTest::ProcessKeyEventCallback, |
| 63 base::Unretained(this))); |
| 64 |
| 65 run_loop_.reset(new base::RunLoop); |
| 66 run_loop_->Run(); |
| 67 run_loop_.reset(); |
| 68 |
| 69 return handled_; |
| 70 } |
| 71 |
| 68 protected: | 72 protected: |
| 73 void ProcessKeyEventCallback(bool handled) { |
| 74 handled_ = handled; |
| 75 run_loop_->Quit(); |
| 76 } |
| 77 |
| 69 ui::mojom::IMEServerPtr ime_server_; | 78 ui::mojom::IMEServerPtr ime_server_; |
| 70 std::unique_ptr<base::RunLoop> run_loop_; | 79 std::unique_ptr<base::RunLoop> run_loop_; |
| 80 bool handled_; |
| 71 | 81 |
| 72 DISALLOW_COPY_AND_ASSIGN(IMEAppTest); | 82 DISALLOW_COPY_AND_ASSIGN(IMEAppTest); |
| 73 }; | 83 }; |
| 74 | 84 |
| 75 // Tests sending a KeyEvent to the IMEDriver through the Mus IMEServer. | 85 // Tests sending a KeyEvent to the IMEDriver through the Mus IMEServer. |
| 76 TEST_F(IMEAppTest, ProcessKeyEvent) { | 86 TEST_F(IMEAppTest, ProcessKeyEvent) { |
| 77 ui::mojom::TextInputClientPtr client_ptr; | 87 ui::mojom::TextInputClientPtr client_ptr; |
| 78 TestTextInputClient client(GetProxy(&client_ptr)); | 88 TestTextInputClient client(GetProxy(&client_ptr)); |
| 79 | 89 |
| 80 ui::mojom::InputMethodPtr input_method; | 90 ui::mojom::InputMethodPtr input_method; |
| 81 ime_server_->StartSession(std::move(client_ptr), GetProxy(&input_method)); | 91 ime_server_->StartSession(std::move(client_ptr), GetProxy(&input_method)); |
| 82 | 92 |
| 83 // Send character key event. | 93 // Send character key event. |
| 84 ui::KeyEvent char_event('A', ui::VKEY_A, 0); | 94 ui::KeyEvent char_event('A', ui::VKEY_A, 0); |
| 85 input_method->ProcessKeyEvent(ui::Event::Clone(char_event)); | 95 EXPECT_TRUE(ProcessKeyEvent(&input_method, ui::Event::Clone(char_event))); |
| 86 | 96 |
| 87 ui::mojom::CompositionEventPtr composition_event = | 97 ui::mojom::CompositionEventPtr composition_event = |
| 88 client.WaitUntilCompositionEvent(); | 98 client.WaitUntilCompositionEvent(); |
| 89 EXPECT_EQ(ui::mojom::CompositionEventType::INSERT_CHAR, | 99 EXPECT_EQ(ui::mojom::CompositionEventType::INSERT_CHAR, |
| 90 composition_event->type); | 100 composition_event->type); |
| 91 EXPECT_TRUE(composition_event->key_event); | 101 EXPECT_TRUE(composition_event->key_event); |
| 92 EXPECT_TRUE(composition_event->key_event.value()->IsKeyEvent()); | 102 EXPECT_TRUE(composition_event->key_event.value()->IsKeyEvent()); |
| 93 | 103 |
| 94 ui::KeyEvent* received_key_event = | 104 ui::KeyEvent* received_key_event = |
| 95 composition_event->key_event.value()->AsKeyEvent(); | 105 composition_event->key_event.value()->AsKeyEvent(); |
| 96 EXPECT_EQ(ui::ET_KEY_PRESSED, received_key_event->type()); | 106 EXPECT_EQ(ui::ET_KEY_PRESSED, received_key_event->type()); |
| 97 EXPECT_TRUE(received_key_event->is_char()); | 107 EXPECT_TRUE(received_key_event->is_char()); |
| 98 EXPECT_EQ(char_event.GetCharacter(), received_key_event->GetCharacter()); | 108 EXPECT_EQ(char_event.GetCharacter(), received_key_event->GetCharacter()); |
| 99 | 109 |
| 100 // Send non-character key event. | 110 // Send non-character key event. |
| 101 ui::KeyEvent nonchar_event(ui::ET_KEY_PRESSED, ui::VKEY_LEFT, 0); | 111 ui::KeyEvent nonchar_event(ui::ET_KEY_PRESSED, ui::VKEY_LEFT, 0); |
| 102 input_method->ProcessKeyEvent(ui::Event::Clone(nonchar_event)); | 112 EXPECT_FALSE(ProcessKeyEvent(&input_method, ui::Event::Clone(nonchar_event))); |
| 103 | |
| 104 ui::Event* unhandled_event = client.WaitUntilUnhandledEvent(); | |
| 105 EXPECT_TRUE(unhandled_event); | |
| 106 EXPECT_TRUE(unhandled_event->IsKeyEvent()); | |
| 107 EXPECT_FALSE(unhandled_event->AsKeyEvent()->is_char()); | |
| 108 EXPECT_EQ(ui::ET_KEY_PRESSED, unhandled_event->type()); | |
| 109 EXPECT_EQ(nonchar_event.key_code(), | |
| 110 unhandled_event->AsKeyEvent()->key_code()); | |
| 111 } | 113 } |
| OLD | NEW |