1 // Copyright 2014 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.
5 #include "base/logging.h"
6 #include "base/metrics/histogram.h"
7 #include "base/metrics/histogram_samples.h"
8 #include "base/metrics/statistics_recorder.h"
9 #include "base/test/histogram_tester.h"
10 #include "chrome/browser/chromeos/input_method/input_method_configuration.h"
11 #include "chrome/browser/chromeos/input_method/input_method_engine.h"
12 #include "chrome/browser/chromeos/input_method/input_method_engine_interface.h"
13 #include "chrome/browser/chromeos/input_method/mock_input_method_manager.h"
14 #include "chrome/browser/profiles/profile_manager.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "ui/base/ime/chromeos/extension_ime_util.h"
17 #include "ui/base/ime/chromeos/mock_component_extension_ime_manager_delegate.h"
18 #include "ui/base/ime/chromeos/mock_ime_input_context_handler.h"
19 #include "ui/base/ime/text_input_flags.h"
20 #include "ui/gfx/geometry/rect.h"
24 namespace input_method
{
27 const char kTestExtensionId
[] = "mppnpdlheglhdfmldimlhpnegondlapf";
28 const char kTestExtensionId2
[] = "dmpipdbjkoajgdeppkffbjhngfckdloi";
29 const char kTestImeComponentId
[] = "test_engine_id";
37 ONCOMPOSITIONBOUNDSCHANGED
= 16U
40 void InitInputMethod() {
41 ComponentExtensionIMEManager
* comp_ime_manager
=
42 new ComponentExtensionIMEManager
;
43 MockComponentExtIMEManagerDelegate
* delegate
=
44 new MockComponentExtIMEManagerDelegate
;
46 ComponentExtensionIME ext1
;
47 ext1
.id
= kTestExtensionId
;
49 ComponentExtensionEngine ext1_engine1
;
50 ext1_engine1
.engine_id
= kTestImeComponentId
;
51 ext1_engine1
.language_codes
.push_back("en-US");
52 ext1_engine1
.layouts
.push_back("us");
53 ext1
.engines
.push_back(ext1_engine1
);
55 std::vector
<ComponentExtensionIME
> ime_list
;
56 ime_list
.push_back(ext1
);
57 delegate
->set_ime_list(ime_list
);
58 comp_ime_manager
->Initialize(
59 scoped_ptr
<ComponentExtensionIMEManagerDelegate
>(delegate
).Pass());
61 MockInputMethodManager
* manager
= new MockInputMethodManager
;
62 manager
->SetComponentExtensionIMEManager(
63 scoped_ptr
<ComponentExtensionIMEManager
>(comp_ime_manager
).Pass());
64 InitializeForTesting(manager
);
67 class TestObserver
: public InputMethodEngineInterface::Observer
{
69 TestObserver() : calls_bitmap_(NONE
) {}
70 ~TestObserver() override
{}
72 void OnActivate(const std::string
& engine_id
) override
{
73 calls_bitmap_
|= ACTIVATE
;
75 void OnDeactivated(const std::string
& engine_id
) override
{
76 calls_bitmap_
|= DEACTIVATED
;
79 const InputMethodEngineInterface::InputContext
& context
) override
{
80 calls_bitmap_
|= ONFOCUS
;
82 void OnBlur(int context_id
) override
{ calls_bitmap_
|= ONBLUR
; }
83 bool IsInterestedInKeyEvent() const override
{ return true; }
84 void OnKeyEvent(const std::string
& engine_id
,
85 const InputMethodEngineInterface::KeyboardEvent
& event
,
86 input_method::KeyEventHandle
* key_data
) override
{}
87 void OnInputContextUpdate(
88 const InputMethodEngineInterface::InputContext
& context
) override
{}
89 void OnCandidateClicked(
90 const std::string
& engine_id
,
92 InputMethodEngineInterface::MouseButtonEvent button
) override
{}
93 void OnMenuItemActivated(const std::string
& engine_id
,
94 const std::string
& menu_id
) override
{}
95 void OnSurroundingTextChanged(const std::string
& engine_id
,
96 const std::string
& text
,
98 int anchor_pos
) override
{}
99 void OnCompositionBoundsChanged(
100 const std::vector
<gfx::Rect
>& bounds
) override
{
101 calls_bitmap_
|= ONCOMPOSITIONBOUNDSCHANGED
;
103 void OnReset(const std::string
& engine_id
) override
{}
105 unsigned char GetCallsBitmapAndReset() {
106 unsigned char ret
= calls_bitmap_
;
107 calls_bitmap_
= NONE
;
112 unsigned char calls_bitmap_
;
114 DISALLOW_COPY_AND_ASSIGN(TestObserver
);
117 class InputMethodEngineTest
: public testing::Test
{
119 InputMethodEngineTest() : observer_(NULL
), input_view_("inputview.html") {
120 languages_
.push_back("en-US");
121 layouts_
.push_back("us");
123 IMEBridge::Initialize();
124 mock_ime_input_context_handler_
.reset(new MockIMEInputContextHandler());
125 IMEBridge::Get()->SetInputContextHandler(
126 mock_ime_input_context_handler_
.get());
128 ~InputMethodEngineTest() override
{
129 IMEBridge::Get()->SetInputContextHandler(NULL
);
135 void CreateEngine(bool whitelisted
) {
136 engine_
.reset(new InputMethodEngine());
137 observer_
= new TestObserver();
138 scoped_ptr
<InputMethodEngineInterface::Observer
> observer_ptr(observer_
);
139 engine_
->Initialize(observer_ptr
.Pass(),
140 whitelisted
? kTestExtensionId
: kTestExtensionId2
,
141 ProfileManager::GetActiveUserProfile());
144 void FocusIn(ui::TextInputType input_type
) {
145 IMEEngineHandlerInterface::InputContext
input_context(
146 input_type
, ui::TEXT_INPUT_MODE_DEFAULT
, ui::TEXT_INPUT_FLAG_NONE
);
147 engine_
->FocusIn(input_context
);
148 IMEBridge::Get()->SetCurrentInputContext(input_context
);
151 scoped_ptr
<InputMethodEngine
> engine_
;
153 TestObserver
* observer_
;
154 std::vector
<std::string
> languages_
;
155 std::vector
<std::string
> layouts_
;
159 scoped_ptr
<MockIMEInputContextHandler
> mock_ime_input_context_handler_
;
162 DISALLOW_COPY_AND_ASSIGN(InputMethodEngineTest
);
167 TEST_F(InputMethodEngineTest
, TestSwitching
) {
169 // Enable/disable with focus.
170 FocusIn(ui::TEXT_INPUT_TYPE_URL
);
171 EXPECT_EQ(NONE
, observer_
->GetCallsBitmapAndReset());
172 engine_
->Enable(kTestImeComponentId
);
173 EXPECT_EQ(ACTIVATE
| ONFOCUS
, observer_
->GetCallsBitmapAndReset());
175 EXPECT_EQ(DEACTIVATED
, observer_
->GetCallsBitmapAndReset());
176 // Enable/disable without focus.
178 EXPECT_EQ(NONE
, observer_
->GetCallsBitmapAndReset());
179 engine_
->Enable(kTestImeComponentId
);
180 EXPECT_EQ(ACTIVATE
| ONFOCUS
, observer_
->GetCallsBitmapAndReset());
182 EXPECT_EQ(DEACTIVATED
, observer_
->GetCallsBitmapAndReset());
183 // Focus change when enabled.
184 engine_
->Enable(kTestImeComponentId
);
185 EXPECT_EQ(ACTIVATE
| ONFOCUS
, observer_
->GetCallsBitmapAndReset());
187 EXPECT_EQ(ONBLUR
, observer_
->GetCallsBitmapAndReset());
188 // Focus change when disabled.
190 EXPECT_EQ(DEACTIVATED
, observer_
->GetCallsBitmapAndReset());
191 FocusIn(ui::TEXT_INPUT_TYPE_TEXT
);
192 EXPECT_EQ(NONE
, observer_
->GetCallsBitmapAndReset());
194 EXPECT_EQ(NONE
, observer_
->GetCallsBitmapAndReset());
197 TEST_F(InputMethodEngineTest
, TestSwitching_Password_3rd_Party
) {
199 // Enable/disable with focus.
200 FocusIn(ui::TEXT_INPUT_TYPE_PASSWORD
);
201 EXPECT_EQ(NONE
, observer_
->GetCallsBitmapAndReset());
202 engine_
->Enable(kTestImeComponentId
);
203 EXPECT_EQ(ACTIVATE
| ONFOCUS
, observer_
->GetCallsBitmapAndReset());
205 EXPECT_EQ(DEACTIVATED
, observer_
->GetCallsBitmapAndReset());
206 // Focus change when enabled.
207 engine_
->Enable(kTestImeComponentId
);
208 EXPECT_EQ(ACTIVATE
| ONFOCUS
, observer_
->GetCallsBitmapAndReset());
210 EXPECT_EQ(ONBLUR
, observer_
->GetCallsBitmapAndReset());
211 FocusIn(ui::TEXT_INPUT_TYPE_PASSWORD
);
212 EXPECT_EQ(ONFOCUS
, observer_
->GetCallsBitmapAndReset());
214 EXPECT_EQ(DEACTIVATED
, observer_
->GetCallsBitmapAndReset());
217 TEST_F(InputMethodEngineTest
, TestSwitching_Password_Whitelisted
) {
219 // Enable/disable with focus.
220 FocusIn(ui::TEXT_INPUT_TYPE_PASSWORD
);
221 EXPECT_EQ(NONE
, observer_
->GetCallsBitmapAndReset());
222 engine_
->Enable(kTestImeComponentId
);
223 EXPECT_EQ(ACTIVATE
| ONFOCUS
, observer_
->GetCallsBitmapAndReset());
225 EXPECT_EQ(DEACTIVATED
, observer_
->GetCallsBitmapAndReset());
226 // Focus change when enabled.
227 engine_
->Enable(kTestImeComponentId
);
228 EXPECT_EQ(ACTIVATE
| ONFOCUS
, observer_
->GetCallsBitmapAndReset());
230 EXPECT_EQ(ONBLUR
, observer_
->GetCallsBitmapAndReset());
231 FocusIn(ui::TEXT_INPUT_TYPE_PASSWORD
);
232 EXPECT_EQ(ONFOCUS
, observer_
->GetCallsBitmapAndReset());
234 EXPECT_EQ(DEACTIVATED
, observer_
->GetCallsBitmapAndReset());
237 TEST_F(InputMethodEngineTest
, TestHistograms
) {
239 FocusIn(ui::TEXT_INPUT_TYPE_TEXT
);
240 engine_
->Enable(kTestImeComponentId
);
241 std::vector
<InputMethodEngineInterface::SegmentInfo
> segments
;
242 engine_
->SetComposition(
243 engine_
->GetCotextIdForTesting(), "test", 0, 0, 0, segments
, NULL
);
245 base::HistogramTester histograms
;
246 engine_
->CommitText(1, "input", &error
);
247 engine_
->CommitText(1,
248 "\xE5\x85\xA5\xE5\x8A\x9B", // 2 UTF-8 characters
250 engine_
->CommitText(1, "input\xE5\x85\xA5\xE5\x8A\x9B", &error
);
251 histograms
.ExpectTotalCount("InputMethod.CommitLength", 3);
252 histograms
.ExpectBucketCount("InputMethod.CommitLength", 5, 1);
253 histograms
.ExpectBucketCount("InputMethod.CommitLength", 2, 1);
254 histograms
.ExpectBucketCount("InputMethod.CommitLength", 7, 1);
257 TEST_F(InputMethodEngineTest
, TestCompositionBoundsChanged
) {
259 // Enable/disable with focus.
260 std::vector
<gfx::Rect
> rects
;
261 rects
.push_back(gfx::Rect());
262 engine_
->SetCompositionBounds(rects
);
263 EXPECT_EQ(ONCOMPOSITIONBOUNDSCHANGED
,
264 observer_
->GetCallsBitmapAndReset());
267 } // namespace input_method
268 } // namespace chromeos