1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <glib-object.h>
#include <cstring>
#include "base/memory/scoped_ptr.h"
#include "ui/base/ime/input_method_delegate.h"
#include "ui/base/ime/input_method_ibus.h"
#include "ui/base/ime/mock_ibus_client.h"
#include "ui/base/ime/text_input_client.h"
#include "ui/gfx/rect.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace ui {
class TestableInputMethodIBus : public InputMethodIBus {
public:
TestableInputMethodIBus(internal::InputMethodDelegate* delegate,
scoped_ptr<internal::IBusClient> new_client)
: InputMethodIBus(delegate) {
set_ibus_client(new_client.Pass());
}
// Change access rights.
using InputMethodIBus::GetBus;
};
class InputMethodIBusTest : public internal::InputMethodDelegate,
public testing::Test,
public TextInputClient {
public:
InputMethodIBusTest() {
ResetFlags();
}
virtual ~InputMethodIBusTest() {
}
// testing::Test overrides:
virtual void SetUp() OVERRIDE {
scoped_ptr<internal::IBusClient> client(new internal::MockIBusClient);
ime_.reset(new TestableInputMethodIBus(this, client.Pass()));
ime_->SetFocusedTextInputClient(this);
}
virtual void TearDown() OVERRIDE {
if (ime_.get())
ime_->SetFocusedTextInputClient(NULL);
ime_.reset();
}
// ui::internal::InputMethodDelegate overrides:
virtual void DispatchKeyEventPostIME(
const base::NativeEvent& native_key_event) OVERRIDE {
dispatched_native_event_ = native_key_event;
}
virtual void DispatchFabricatedKeyEventPostIME(ui::EventType type,
ui::KeyboardCode key_code,
int flags) OVERRIDE {
dispatched_fabricated_event_type_ = type;
dispatched_fabricated_event_key_code_ = key_code;
dispatched_fabricated_event_flags_ = flags;
}
// ui::TextInputClient overrides:
virtual void SetCompositionText(
const CompositionText& composition) OVERRIDE {
composition_text_ = composition;
}
virtual void ConfirmCompositionText() OVERRIDE {
confirmed_text_ = composition_text_;
composition_text_.Clear();
}
virtual void ClearCompositionText() OVERRIDE {
composition_text_.Clear();
}
virtual void InsertText(const string16& text) OVERRIDE {
inserted_text_ = text;
}
virtual void InsertChar(char16 ch, int flags) OVERRIDE {
inserted_char_ = ch;
inserted_char_flags_ = flags;
}
virtual TextInputType GetTextInputType() const OVERRIDE {
return input_type_;
}
virtual bool CanComposeInline() const OVERRIDE {
return can_compose_inline_;
}
virtual gfx::Rect GetCaretBounds() OVERRIDE {
return caret_bounds_;
}
virtual bool HasCompositionText() OVERRIDE {
CompositionText empty;
return composition_text_ != empty;
}
virtual bool GetTextRange(Range* range) OVERRIDE { return false; }
virtual bool GetCompositionTextRange(Range* range) OVERRIDE { return false; }
virtual bool GetSelectionRange(Range* range) OVERRIDE { return false; }
virtual bool SetSelectionRange(const Range& range) OVERRIDE { return false; }
virtual bool DeleteRange(const Range& range) OVERRIDE { return false; }
virtual bool GetTextFromRange(const Range& range,
string16* text) OVERRIDE { return false; }
virtual void OnInputMethodChanged() OVERRIDE {
++on_input_method_changed_call_count_;
}
virtual bool ChangeTextDirectionAndLayoutAlignment(
base::i18n::TextDirection direction) OVERRIDE { return false; }
IBusBus* GetBus() {
return ime_->GetBus();
}
internal::MockIBusClient* GetClient() {
return static_cast<internal::MockIBusClient*>(ime_->ibus_client());
}
bool HasNativeEvent() const {
base::NativeEvent empty;
std::memset(&empty, 0, sizeof(empty));
return !!std::memcmp(&dispatched_native_event_,
&empty,
sizeof(dispatched_native_event_));
}
void ResetFlags() {
std::memset(&dispatched_native_event_, 0, sizeof(dispatched_native_event_));
DCHECK(!HasNativeEvent());
dispatched_fabricated_event_type_ = ET_UNKNOWN;
dispatched_fabricated_event_key_code_ = VKEY_UNKNOWN;
dispatched_fabricated_event_flags_ = 0;
composition_text_.Clear();
confirmed_text_.Clear();
inserted_text_.clear();
inserted_char_ = 0;
inserted_char_flags_ = 0;
on_input_method_changed_call_count_ = 0;
input_type_ = TEXT_INPUT_TYPE_NONE;
can_compose_inline_ = true;
caret_bounds_ = gfx::Rect();
}
scoped_ptr<TestableInputMethodIBus> ime_;
// Variables for remembering the parameters that are passed to
// ui::internal::InputMethodDelegate functions.
base::NativeEvent dispatched_native_event_;
ui::EventType dispatched_fabricated_event_type_;
ui::KeyboardCode dispatched_fabricated_event_key_code_;
int dispatched_fabricated_event_flags_;
// Variables for remembering the parameters that are passed to
// ui::TextInputClient functions.
CompositionText composition_text_;
CompositionText confirmed_text_;
string16 inserted_text_;
char16 inserted_char_;
unsigned int on_input_method_changed_call_count_;
int inserted_char_flags_;
// Variables that will be returned from the ui::TextInputClient functions.
TextInputType input_type_;
bool can_compose_inline_;
gfx::Rect caret_bounds_;
};
// Tests public APIs in ui::InputMethod first.
TEST_F(InputMethodIBusTest, GetInputLocale) {
// ui::InputMethodIBus does not support the API.
ime_->Init(true);
EXPECT_EQ("", ime_->GetInputLocale());
}
TEST_F(InputMethodIBusTest, GetInputTextDirection) {
// ui::InputMethodIBus does not support the API.
ime_->Init(true);
EXPECT_EQ(base::i18n::UNKNOWN_DIRECTION, ime_->GetInputTextDirection());
}
TEST_F(InputMethodIBusTest, IsActive) {
ime_->Init(true);
// ui::InputMethodIBus always returns true.
EXPECT_TRUE(ime_->IsActive());
}
TEST_F(InputMethodIBusTest, GetInputTextType) {
ime_->Init(true);
EXPECT_EQ(TEXT_INPUT_TYPE_NONE, ime_->GetTextInputType());
input_type_ = TEXT_INPUT_TYPE_PASSWORD;
ime_->OnTextInputTypeChanged(this);
EXPECT_EQ(TEXT_INPUT_TYPE_PASSWORD, ime_->GetTextInputType());
input_type_ = TEXT_INPUT_TYPE_TEXT;
ime_->OnTextInputTypeChanged(this);
EXPECT_EQ(TEXT_INPUT_TYPE_TEXT, ime_->GetTextInputType());
}
TEST_F(InputMethodIBusTest, CanComposeInline) {
ime_->Init(true);
EXPECT_EQ(true, ime_->CanComposeInline());
can_compose_inline_ = false;
ime_->OnTextInputTypeChanged(this);
EXPECT_EQ(false, ime_->CanComposeInline());
}
TEST_F(InputMethodIBusTest, GetTextInputClient) {
ime_->Init(true);
EXPECT_EQ(this, ime_->GetTextInputClient());
ime_->SetFocusedTextInputClient(NULL);
EXPECT_EQ(NULL, ime_->GetTextInputClient());
}
TEST_F(InputMethodIBusTest, GetInputTextType_WithoutFocusedClient) {
ime_->Init(true);
EXPECT_EQ(TEXT_INPUT_TYPE_NONE, ime_->GetTextInputType());
ime_->SetFocusedTextInputClient(NULL);
input_type_ = TEXT_INPUT_TYPE_PASSWORD;
ime_->OnTextInputTypeChanged(this);
// The OnTextInputTypeChanged() call above should be ignored since |this| is
// not the current focused client.
EXPECT_EQ(TEXT_INPUT_TYPE_NONE, ime_->GetTextInputType());
ime_->SetFocusedTextInputClient(this);
ime_->OnTextInputTypeChanged(this);
EXPECT_EQ(TEXT_INPUT_TYPE_PASSWORD, ime_->GetTextInputType());
}
TEST_F(InputMethodIBusTest, GetInputTextType_WithoutFocusedWindow) {
ime_->Init(true);
EXPECT_EQ(TEXT_INPUT_TYPE_NONE, ime_->GetTextInputType());
ime_->OnBlur();
input_type_ = TEXT_INPUT_TYPE_PASSWORD;
ime_->OnTextInputTypeChanged(this);
// The OnTextInputTypeChanged() call above should be ignored since the top-
// level window which the ime_ is attached to is not currently focused.
EXPECT_EQ(TEXT_INPUT_TYPE_NONE, ime_->GetTextInputType());
ime_->OnFocus();
ime_->OnTextInputTypeChanged(this);
EXPECT_EQ(TEXT_INPUT_TYPE_PASSWORD, ime_->GetTextInputType());
}
TEST_F(InputMethodIBusTest, GetInputTextType_WithoutFocusedWindow2) {
ime_->Init(false); // the top-level is initially unfocused.
EXPECT_EQ(TEXT_INPUT_TYPE_NONE, ime_->GetTextInputType());
input_type_ = TEXT_INPUT_TYPE_PASSWORD;
ime_->OnTextInputTypeChanged(this);
EXPECT_EQ(TEXT_INPUT_TYPE_NONE, ime_->GetTextInputType());
ime_->OnFocus();
ime_->OnTextInputTypeChanged(this);
EXPECT_EQ(TEXT_INPUT_TYPE_PASSWORD, ime_->GetTextInputType());
}
// Then, tests internal behavior of ui::InputMethodIBus, especially if the input
// method implementation calls ui::internal::IBusClient APIs as expected.
// Start ibus-daemon first, then create ui::InputMethodIBus. Check if a new
// input context is created.
TEST_F(InputMethodIBusTest, InitiallyConnected) {
GetClient()->is_connected_ = true;
ime_->Init(true);
// An input context should be created immediately since is_connected_ is true.
EXPECT_EQ(1U, GetClient()->create_context_call_count_);
EXPECT_EQ(1U, GetClient()->set_capabilities_call_count_);
// However, since the current text input type is 'NONE' (the default), FocusIn
// shouldn't be called.
EXPECT_EQ(0U, GetClient()->focus_in_call_count_);
}
// Create ui::InputMethodIBus, then start ibus-daemon.
TEST_F(InputMethodIBusTest, InitiallyDisconnected) {
ime_->Init(true);
// A context shouldn't be created since the daemon is not running.
EXPECT_EQ(0U, GetClient()->create_context_call_count_);
// Start the daemon.
GetClient()->is_connected_ = true;
g_signal_emit_by_name(GetBus(), "connected");
// A context should be created upon the signal delivery.
EXPECT_EQ(1U, GetClient()->create_context_call_count_);
EXPECT_EQ(1U, GetClient()->set_capabilities_call_count_);
EXPECT_EQ(0U, GetClient()->focus_in_call_count_);
}
// Confirm that ui::InputMethodIBus does not crash on "disconnected" signal
// delivery.
TEST_F(InputMethodIBusTest, Disconnect) {
GetClient()->is_connected_ = true;
ime_->Init(true);
EXPECT_EQ(1U, GetClient()->create_context_call_count_);
GetClient()->is_connected_ = false;
g_signal_emit_by_name(GetBus(), "disconnected");
}
// Confirm that ui::InputMethodIBus re-creates an input context when ibus-daemon
// restarts.
TEST_F(InputMethodIBusTest, DisconnectThenReconnect) {
GetClient()->is_connected_ = true;
ime_->Init(true);
EXPECT_EQ(1U, GetClient()->create_context_call_count_);
EXPECT_EQ(1U, GetClient()->set_capabilities_call_count_);
EXPECT_EQ(0U, GetClient()->destroy_proxy_call_count_);
GetClient()->is_connected_ = false;
g_signal_emit_by_name(GetBus(), "disconnected");
GetClient()->is_connected_ = true;
g_signal_emit_by_name(GetBus(), "connected");
// Check if the old context is deleted.
EXPECT_EQ(1U, GetClient()->destroy_proxy_call_count_);
// Check if a new context is created.
EXPECT_EQ(2U, GetClient()->create_context_call_count_);
EXPECT_EQ(2U, GetClient()->set_capabilities_call_count_);
}
// Confirm that ui::InputMethodIBus does not crash even if NULL context is
// passed.
// TODO(yusukes): Currently, ui::InputMethodIBus does not try to create ic once
// it fails (unless ibus sends the "connected" signal to Chrome again). It might
// be better to add some retry logic. Will revisit later.
TEST_F(InputMethodIBusTest, CreateContextFail) {
GetClient()->is_connected_ = true;
GetClient()->create_context_result_ =
internal::MockIBusClient::kCreateContextFail;
ime_->Init(true);
EXPECT_EQ(1U, GetClient()->create_context_call_count_);
// |set_capabilities_call_count_| should be zero since a context is not
// created yet.
EXPECT_EQ(0U, GetClient()->set_capabilities_call_count_);
}
// Confirm that ui::InputMethodIBus does not crash even if ibus-daemon does not
// respond.
TEST_F(InputMethodIBusTest, CreateContextNoResp) {
GetClient()->is_connected_ = true;
GetClient()->create_context_result_ =
internal::MockIBusClient::kCreateContextNoResponse;
ime_->Init(true);
EXPECT_EQ(1U, GetClient()->create_context_call_count_);
EXPECT_EQ(0U, GetClient()->set_capabilities_call_count_);
}
// Confirm that ui::InputMethodIBus does not crash even if ibus-daemon responds
// after ui::InputMethodIBus is deleted. See comments in ~MockIBusClient() as
// well.
TEST_F(InputMethodIBusTest, CreateContextDelayed) {
GetClient()->is_connected_ = true;
GetClient()->create_context_result_ =
internal::MockIBusClient::kCreateContextDelayed;
ime_->Init(true);
EXPECT_EQ(1U, GetClient()->create_context_call_count_);
EXPECT_EQ(0U, GetClient()->set_capabilities_call_count_);
// After this line, the destructor for |ime_| will run first. Then, the
// destructor for the client will run. In the latter function, a new input
// context will be created and passed to StoreOrAbandonInputContext().
}
// Confirm that IBusClient::FocusIn is called on "connected" if input_type_ is
// TEXT.
TEST_F(InputMethodIBusTest, FocusIn_Text) {
ime_->Init(true);
// A context shouldn't be created since the daemon is not running.
EXPECT_EQ(0U, GetClient()->create_context_call_count_);
EXPECT_EQ(0U, on_input_method_changed_call_count_);
// Click a text input form.
input_type_ = TEXT_INPUT_TYPE_TEXT;
ime_->OnTextInputTypeChanged(this);
// Start the daemon.
GetClient()->is_connected_ = true;
g_signal_emit_by_name(GetBus(), "connected");
// A context should be created upon the signal delivery.
EXPECT_EQ(1U, GetClient()->create_context_call_count_);
// Since a form has focus, IBusClient::FocusIn() should be called.
EXPECT_EQ(1U, GetClient()->focus_in_call_count_);
EXPECT_EQ(1U, GetClient()->set_cursor_location_call_count_);
// ui::TextInputClient::OnInputMethodChanged() should be called when
// ui::InputMethodIBus connects/disconnects to/from ibus-daemon and the
// current text input type is not NONE.
EXPECT_EQ(1U, on_input_method_changed_call_count_);
}
// Confirm that IBusClient::FocusIn is NOT called on "connected" if input_type_
// is PASSWORD.
TEST_F(InputMethodIBusTest, FocusIn_Password) {
ime_->Init(true);
EXPECT_EQ(0U, GetClient()->create_context_call_count_);
EXPECT_EQ(0U, on_input_method_changed_call_count_);
input_type_ = TEXT_INPUT_TYPE_PASSWORD;
ime_->OnTextInputTypeChanged(this);
GetClient()->is_connected_ = true;
g_signal_emit_by_name(GetBus(), "connected");
EXPECT_EQ(1U, GetClient()->create_context_call_count_);
// Since a form has focus, IBusClient::FocusIn() should NOT be called.
EXPECT_EQ(0U, GetClient()->focus_in_call_count_);
EXPECT_EQ(1U, on_input_method_changed_call_count_);
}
// Confirm that IBusClient::FocusOut is called as expected.
TEST_F(InputMethodIBusTest, FocusOut_None) {
input_type_ = TEXT_INPUT_TYPE_TEXT;
GetClient()->is_connected_ = true;
ime_->Init(true);
EXPECT_EQ(1U, GetClient()->create_context_call_count_);
EXPECT_EQ(1U, GetClient()->focus_in_call_count_);
EXPECT_EQ(0U, GetClient()->focus_out_call_count_);
input_type_ = TEXT_INPUT_TYPE_NONE;
ime_->OnTextInputTypeChanged(this);
EXPECT_EQ(1U, GetClient()->focus_in_call_count_);
EXPECT_EQ(1U, GetClient()->focus_out_call_count_);
}
// Confirm that IBusClient::FocusOut is called as expected.
TEST_F(InputMethodIBusTest, FocusOut_Password) {
input_type_ = TEXT_INPUT_TYPE_TEXT;
GetClient()->is_connected_ = true;
ime_->Init(true);
EXPECT_EQ(1U, GetClient()->create_context_call_count_);
EXPECT_EQ(1U, GetClient()->focus_in_call_count_);
EXPECT_EQ(0U, GetClient()->focus_out_call_count_);
input_type_ = TEXT_INPUT_TYPE_PASSWORD;
ime_->OnTextInputTypeChanged(this);
EXPECT_EQ(1U, GetClient()->focus_in_call_count_);
EXPECT_EQ(1U, GetClient()->focus_out_call_count_);
}
// Confirm that IBusClient::FocusOut is NOT called.
TEST_F(InputMethodIBusTest, FocusOut_Url) {
input_type_ = TEXT_INPUT_TYPE_TEXT;
GetClient()->is_connected_ = true;
ime_->Init(true);
EXPECT_EQ(1U, GetClient()->create_context_call_count_);
EXPECT_EQ(1U, GetClient()->focus_in_call_count_);
EXPECT_EQ(0U, GetClient()->focus_out_call_count_);
input_type_ = TEXT_INPUT_TYPE_URL;
ime_->OnTextInputTypeChanged(this);
EXPECT_EQ(1U, GetClient()->focus_in_call_count_);
EXPECT_EQ(0U, GetClient()->focus_out_call_count_);
}
// Test if the new |caret_bounds_| is correctly sent to ibus-daemon.
TEST_F(InputMethodIBusTest, OnCaretBoundsChanged) {
GetClient()->is_connected_ = true;
input_type_ = TEXT_INPUT_TYPE_TEXT;
ime_->Init(true);
EXPECT_EQ(0U, GetClient()->set_cursor_location_call_count_);
caret_bounds_ = gfx::Rect(1, 2, 3, 4);
ime_->OnCaretBoundsChanged(this);
EXPECT_EQ(1U, GetClient()->set_cursor_location_call_count_);
caret_bounds_ = gfx::Rect(0, 2, 3, 4);
ime_->OnCaretBoundsChanged(this);
EXPECT_EQ(2U, GetClient()->set_cursor_location_call_count_);
caret_bounds_ = gfx::Rect(0, 2, 3, 4); // unchanged
ime_->OnCaretBoundsChanged(this);
// Current InputMethodIBus implementation performs the IPC regardless of the
// bounds are changed or not.
EXPECT_EQ(3U, GetClient()->set_cursor_location_call_count_);
}
// TODO(yusukes): Write more tests, especially for key event functions.
} // namespace ui
|