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 "components/arc/ime/arc_ime_service.h" | 5 #include "components/arc/ime/arc_ime_service.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
11 #include "base/strings/string16.h" | 11 #include "base/strings/string16.h" |
12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
13 #include "components/arc/test/fake_arc_bridge_service.h" | 13 #include "components/arc/test/fake_arc_bridge_service.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 #include "ui/base/ime/composition_text.h" | 15 #include "ui/base/ime/composition_text.h" |
16 #include "ui/base/ime/dummy_input_method.h" | 16 #include "ui/base/ime/dummy_input_method.h" |
| 17 #include "ui/events/event.h" |
| 18 #include "ui/events/keycodes/keyboard_codes.h" |
17 | 19 |
18 namespace arc { | 20 namespace arc { |
19 | 21 |
20 namespace { | 22 namespace { |
21 | 23 |
22 class FakeArcImeBridge : public ArcImeBridge { | 24 class FakeArcImeBridge : public ArcImeBridge { |
23 public: | 25 public: |
| 26 FakeArcImeBridge() : count_send_insert_text_(0) {} |
| 27 |
24 void SendSetCompositionText(const ui::CompositionText& composition) override { | 28 void SendSetCompositionText(const ui::CompositionText& composition) override { |
25 } | 29 } |
26 void SendConfirmCompositionText() override { | 30 void SendConfirmCompositionText() override { |
27 } | 31 } |
28 void SendInsertText(const base::string16& text) override { | 32 void SendInsertText(const base::string16& text) override { |
| 33 count_send_insert_text_++; |
29 } | 34 } |
| 35 |
| 36 int count_send_insert_text() const { return count_send_insert_text_; } |
| 37 |
| 38 private: |
| 39 int count_send_insert_text_; |
30 }; | 40 }; |
31 | 41 |
32 class FakeInputMethod : public ui::DummyInputMethod { | 42 class FakeInputMethod : public ui::DummyInputMethod { |
33 public: | 43 public: |
34 FakeInputMethod() : client_(nullptr), | 44 FakeInputMethod() : client_(nullptr), |
35 count_show_ime_if_needed_(0), | 45 count_show_ime_if_needed_(0), |
36 count_cancel_composition_(0) {} | 46 count_cancel_composition_(0) {} |
37 | 47 |
38 void SetFocusedTextInputClient(ui::TextInputClient* client) override { | 48 void SetFocusedTextInputClient(ui::TextInputClient* client) override { |
39 client_ = client; | 49 client_ = client; |
(...skipping 29 matching lines...) Expand all Loading... |
69 } // namespace | 79 } // namespace |
70 | 80 |
71 class ArcImeServiceTest : public testing::Test { | 81 class ArcImeServiceTest : public testing::Test { |
72 public: | 82 public: |
73 ArcImeServiceTest() {} | 83 ArcImeServiceTest() {} |
74 | 84 |
75 protected: | 85 protected: |
76 std::unique_ptr<FakeArcBridgeService> fake_arc_bridge_service_; | 86 std::unique_ptr<FakeArcBridgeService> fake_arc_bridge_service_; |
77 std::unique_ptr<FakeInputMethod> fake_input_method_; | 87 std::unique_ptr<FakeInputMethod> fake_input_method_; |
78 std::unique_ptr<ArcImeService> instance_; | 88 std::unique_ptr<ArcImeService> instance_; |
| 89 FakeArcImeBridge* fake_arc_ime_bridge_; // Owned by |instance_| |
79 | 90 |
80 private: | 91 private: |
81 void SetUp() override { | 92 void SetUp() override { |
82 fake_arc_bridge_service_.reset(new FakeArcBridgeService); | 93 fake_arc_bridge_service_.reset(new FakeArcBridgeService); |
83 instance_.reset(new ArcImeService(fake_arc_bridge_service_.get())); | 94 instance_.reset(new ArcImeService(fake_arc_bridge_service_.get())); |
84 instance_->SetImeBridgeForTesting(base::WrapUnique(new FakeArcImeBridge)); | 95 fake_arc_ime_bridge_ = new FakeArcImeBridge; |
| 96 instance_->SetImeBridgeForTesting(base::WrapUnique(fake_arc_ime_bridge_)); |
85 | 97 |
86 fake_input_method_.reset(new FakeInputMethod); | 98 fake_input_method_.reset(new FakeInputMethod); |
87 instance_->SetInputMethodForTesting(fake_input_method_.get()); | 99 instance_->SetInputMethodForTesting(fake_input_method_.get()); |
88 } | 100 } |
89 | 101 |
90 void TearDown() override { | 102 void TearDown() override { |
| 103 fake_arc_ime_bridge_ = nullptr; |
91 instance_.reset(); | 104 instance_.reset(); |
92 fake_arc_bridge_service_.reset(); | 105 fake_arc_bridge_service_.reset(); |
93 } | 106 } |
94 }; | 107 }; |
95 | 108 |
96 TEST_F(ArcImeServiceTest, HasCompositionText) { | 109 TEST_F(ArcImeServiceTest, HasCompositionText) { |
97 ui::CompositionText composition; | 110 ui::CompositionText composition; |
98 composition.text = base::UTF8ToUTF16("nonempty text"); | 111 composition.text = base::UTF8ToUTF16("nonempty text"); |
99 | 112 |
100 EXPECT_FALSE(instance_->HasCompositionText()); | 113 EXPECT_FALSE(instance_->HasCompositionText()); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 EXPECT_EQ(2, fake_input_method_->count_show_ime_if_needed()); | 153 EXPECT_EQ(2, fake_input_method_->count_show_ime_if_needed()); |
141 } | 154 } |
142 | 155 |
143 TEST_F(ArcImeServiceTest, CancelComposition) { | 156 TEST_F(ArcImeServiceTest, CancelComposition) { |
144 // The bridge should forward the cancel event to the input method. | 157 // The bridge should forward the cancel event to the input method. |
145 fake_input_method_->SetFocusedTextInputClient(instance_.get()); | 158 fake_input_method_->SetFocusedTextInputClient(instance_.get()); |
146 instance_->OnCancelComposition(); | 159 instance_->OnCancelComposition(); |
147 EXPECT_EQ(1, fake_input_method_->count_cancel_composition()); | 160 EXPECT_EQ(1, fake_input_method_->count_cancel_composition()); |
148 } | 161 } |
149 | 162 |
| 163 TEST_F(ArcImeServiceTest, InsertChar) { |
| 164 fake_input_method_->SetFocusedTextInputClient(instance_.get()); |
| 165 |
| 166 // When text input type is NONE, the event is not forwarded. |
| 167 instance_->OnTextInputTypeChanged(ui::TEXT_INPUT_TYPE_NONE); |
| 168 instance_->InsertChar(ui::KeyEvent('a', ui::VKEY_A, 0)); |
| 169 EXPECT_EQ(0, fake_arc_ime_bridge_->count_send_insert_text()); |
| 170 |
| 171 // When the bridge is accepting text inputs, forward the event. |
| 172 instance_->OnTextInputTypeChanged(ui::TEXT_INPUT_TYPE_TEXT); |
| 173 instance_->InsertChar(ui::KeyEvent('a', ui::VKEY_A, 0)); |
| 174 EXPECT_EQ(1, fake_arc_ime_bridge_->count_send_insert_text()); |
| 175 } |
| 176 |
150 } // namespace arc | 177 } // namespace arc |
OLD | NEW |