forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathime_controller_impl_unittest.cc
365 lines (293 loc) · 13 KB
/
ime_controller_impl_unittest.cc
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
// Copyright 2017 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 "ash/ime/ime_controller_impl.h"
#include <vector>
#include "ash/ime/mode_indicator_observer.h"
#include "ash/ime/test_ime_controller_client.h"
#include "ash/public/cpp/ime_info.h"
#include "ash/shell.h"
#include "ash/system/ime/ime_observer.h"
#include "ash/system/tray/system_tray_notifier.h"
#include "ash/test/ash_test_base.h"
#include "base/strings/utf_string_conversions.h"
#include "ui/base/accelerators/accelerator.h"
#include "ui/base/ime/chromeos/extension_ime_util.h"
#include "ui/display/manager/display_manager.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "ui/views/widget/widget.h"
namespace ash {
namespace {
// 43 is the designed size of the inner contents.
// This value corresponds with kMinSize defined in
// mode_indicator_delegate_view.cc.
const int kInnerSize = 43;
// Refreshes the IME list with fake IMEs and fake menu items.
void RefreshImesWithMenuItems(const std::string& current_ime_id,
const std::vector<std::string>& ime_ids,
const std::vector<std::string>& menu_item_keys) {
std::vector<ImeInfo> available_imes;
for (const std::string& ime_id : ime_ids) {
ImeInfo ime;
ime.id = ime_id;
available_imes.push_back(std::move(ime));
}
std::vector<ImeMenuItem> menu_items;
for (const std::string& menu_item_key : menu_item_keys) {
ImeMenuItem item;
item.key = menu_item_key;
menu_items.push_back(std::move(item));
}
Shell::Get()->ime_controller()->RefreshIme(
current_ime_id, std::move(available_imes), std::move(menu_items));
}
// Refreshes the IME list without adding any menu items.
void RefreshImes(const std::string& current_ime_id,
const std::vector<std::string>& ime_ids) {
const std::vector<std::string> empty_menu_items;
RefreshImesWithMenuItems(current_ime_id, ime_ids, empty_menu_items);
}
class TestImeObserver : public IMEObserver {
public:
TestImeObserver() = default;
~TestImeObserver() override = default;
// IMEObserver:
void OnIMERefresh() override { ++refresh_count_; }
void OnIMEMenuActivationChanged(bool is_active) override {
ime_menu_active_ = is_active;
}
int refresh_count_ = 0;
bool ime_menu_active_ = false;
};
class TestImeControllerObserver : public ImeControllerImpl::Observer {
public:
TestImeControllerObserver() = default;
// IMEController::Observer:
void OnCapsLockChanged(bool enabled) override { ++caps_lock_count_; }
void OnKeyboardLayoutNameChanged(const std::string& layout_name) override {
last_keyboard_layout_name_ = layout_name;
}
const std::string& last_keyboard_layout_name() const {
return last_keyboard_layout_name_;
}
private:
int caps_lock_count_ = 0;
std::string last_keyboard_layout_name_;
DISALLOW_COPY_AND_ASSIGN(TestImeControllerObserver);
};
using ImeControllerImplTest = AshTestBase;
TEST_F(ImeControllerImplTest, RefreshIme) {
ImeControllerImpl* controller = Shell::Get()->ime_controller();
TestImeObserver observer;
Shell::Get()->system_tray_notifier()->AddIMEObserver(&observer);
RefreshImesWithMenuItems("ime1", {"ime1", "ime2"}, {"menu1"});
// Cached data was updated.
EXPECT_EQ("ime1", controller->current_ime().id);
ASSERT_EQ(2u, controller->available_imes().size());
EXPECT_EQ("ime1", controller->available_imes()[0].id);
EXPECT_EQ("ime2", controller->available_imes()[1].id);
ASSERT_EQ(1u, controller->current_ime_menu_items().size());
EXPECT_EQ("menu1", controller->current_ime_menu_items()[0].key);
// Observer was notified.
EXPECT_EQ(1, observer.refresh_count_);
}
TEST_F(ImeControllerImplTest, NoCurrentIme) {
ImeControllerImpl* controller = Shell::Get()->ime_controller();
// Set up a single IME.
RefreshImes("ime1", {"ime1"});
EXPECT_EQ("ime1", controller->current_ime().id);
// When there is no current IME the cached current IME is empty.
const std::string empty_ime_id;
RefreshImes(empty_ime_id, {"ime1"});
EXPECT_TRUE(controller->current_ime().id.empty());
}
TEST_F(ImeControllerImplTest, SetImesManagedByPolicy) {
ImeControllerImpl* controller = Shell::Get()->ime_controller();
TestImeObserver observer;
Shell::Get()->system_tray_notifier()->AddIMEObserver(&observer);
// Defaults to false.
EXPECT_FALSE(controller->managed_by_policy());
// Setting the value notifies observers.
controller->SetImesManagedByPolicy(true);
EXPECT_TRUE(controller->managed_by_policy());
EXPECT_EQ(1, observer.refresh_count_);
}
TEST_F(ImeControllerImplTest, ShowImeMenuOnShelf) {
ImeControllerImpl* controller = Shell::Get()->ime_controller();
TestImeObserver observer;
Shell::Get()->system_tray_notifier()->AddIMEObserver(&observer);
controller->ShowImeMenuOnShelf(true);
EXPECT_TRUE(observer.ime_menu_active_);
}
TEST_F(ImeControllerImplTest, CanSwitchIme) {
ImeControllerImpl* controller = Shell::Get()->ime_controller();
// Can't switch IMEs when none are available.
ASSERT_EQ(0u, controller->available_imes().size());
EXPECT_FALSE(controller->CanSwitchIme());
// Can't switch with only 1 IME.
RefreshImes("ime1", {"ime1"});
EXPECT_FALSE(controller->CanSwitchIme());
// Can switch with more than 1 IME.
RefreshImes("ime1", {"ime1", "ime2"});
EXPECT_TRUE(controller->CanSwitchIme());
}
TEST_F(ImeControllerImplTest, SwitchIme) {
ImeControllerImpl* controller = Shell::Get()->ime_controller();
TestImeControllerClient client;
// Can't switch IME before the client is set.
controller->SwitchToNextIme();
EXPECT_EQ(0, client.next_ime_count_);
controller->SwitchToLastUsedIme();
EXPECT_EQ(0, client.last_used_ime_count_);
controller->SwitchImeById("ime1", true /* show_message */);
EXPECT_EQ(0, client.switch_ime_count_);
// After setting the client the requests are forwarded.
controller->SetClient(&client);
controller->SwitchToNextIme();
EXPECT_EQ(1, client.next_ime_count_);
controller->SwitchToLastUsedIme();
EXPECT_EQ(1, client.last_used_ime_count_);
controller->SwitchImeById("ime1", true /* show_message */);
EXPECT_EQ(1, client.switch_ime_count_);
EXPECT_EQ("ime1", client.last_switch_ime_id_);
EXPECT_TRUE(client.last_show_message_);
controller->SwitchImeById("ime2", false /* show_message */);
EXPECT_EQ(2, client.switch_ime_count_);
EXPECT_EQ("ime2", client.last_switch_ime_id_);
EXPECT_FALSE(client.last_show_message_);
}
TEST_F(ImeControllerImplTest, SwitchImeWithAccelerator) {
ImeControllerImpl* controller = Shell::Get()->ime_controller();
TestImeControllerClient client;
controller->SetClient(&client);
const ui::Accelerator convert(ui::VKEY_CONVERT, ui::EF_NONE);
const ui::Accelerator non_convert(ui::VKEY_NONCONVERT, ui::EF_NONE);
const ui::Accelerator wide_half_1(ui::VKEY_DBE_SBCSCHAR, ui::EF_NONE);
const ui::Accelerator wide_half_2(ui::VKEY_DBE_DBCSCHAR, ui::EF_NONE);
// When there are no IMEs available switching by accelerator does not work.
ASSERT_EQ(0u, controller->available_imes().size());
EXPECT_FALSE(controller->CanSwitchImeWithAccelerator(convert));
EXPECT_FALSE(controller->CanSwitchImeWithAccelerator(non_convert));
EXPECT_FALSE(controller->CanSwitchImeWithAccelerator(wide_half_1));
EXPECT_FALSE(controller->CanSwitchImeWithAccelerator(wide_half_2));
// With only test IMEs (and no Japanese IMEs) the special keys do not work.
RefreshImes("ime1", {"ime1", "ime2"});
EXPECT_FALSE(controller->CanSwitchImeWithAccelerator(convert));
EXPECT_FALSE(controller->CanSwitchImeWithAccelerator(non_convert));
EXPECT_FALSE(controller->CanSwitchImeWithAccelerator(wide_half_1));
EXPECT_FALSE(controller->CanSwitchImeWithAccelerator(wide_half_2));
// Install both a test IME and Japanese IMEs.
using chromeos::extension_ime_util::GetInputMethodIDByEngineID;
const std::string nacl_mozc_jp = GetInputMethodIDByEngineID("nacl_mozc_jp");
const std::string xkb_jp_jpn = GetInputMethodIDByEngineID("xkb:jp::jpn");
RefreshImes("ime1", {"ime1", nacl_mozc_jp, xkb_jp_jpn});
// Accelerator based switching now works.
EXPECT_TRUE(controller->CanSwitchImeWithAccelerator(convert));
EXPECT_TRUE(controller->CanSwitchImeWithAccelerator(non_convert));
EXPECT_TRUE(controller->CanSwitchImeWithAccelerator(wide_half_1));
EXPECT_TRUE(controller->CanSwitchImeWithAccelerator(wide_half_2));
// Convert keys jump directly to the requested IME.
controller->SwitchImeWithAccelerator(convert);
EXPECT_EQ(1, client.switch_ime_count_);
EXPECT_EQ(nacl_mozc_jp, client.last_switch_ime_id_);
controller->SwitchImeWithAccelerator(non_convert);
EXPECT_EQ(2, client.switch_ime_count_);
EXPECT_EQ(xkb_jp_jpn, client.last_switch_ime_id_);
// Switch from nacl_mozc_jp to xkb_jp_jpn.
RefreshImes(nacl_mozc_jp, {"ime1", nacl_mozc_jp, xkb_jp_jpn});
controller->SwitchImeWithAccelerator(wide_half_1);
EXPECT_EQ(3, client.switch_ime_count_);
EXPECT_EQ(xkb_jp_jpn, client.last_switch_ime_id_);
// Switch from xkb_jp_jpn to nacl_mozc_jp.
RefreshImes(xkb_jp_jpn, {"ime1", nacl_mozc_jp, xkb_jp_jpn});
controller->SwitchImeWithAccelerator(wide_half_2);
EXPECT_EQ(4, client.switch_ime_count_);
EXPECT_EQ(nacl_mozc_jp, client.last_switch_ime_id_);
}
TEST_F(ImeControllerImplTest, SetCapsLock) {
ImeControllerImpl* controller = Shell::Get()->ime_controller();
TestImeControllerClient client;
EXPECT_EQ(0, client.set_caps_lock_count_);
controller->SetCapsLockEnabled(true);
EXPECT_EQ(0, client.set_caps_lock_count_);
controller->SetClient(&client);
controller->SetCapsLockEnabled(true);
EXPECT_EQ(1, client.set_caps_lock_count_);
// Does not no-op when the state is the same. Should send all notifications.
controller->SetCapsLockEnabled(true);
EXPECT_EQ(2, client.set_caps_lock_count_);
controller->SetCapsLockEnabled(false);
EXPECT_EQ(3, client.set_caps_lock_count_);
controller->SetCapsLockEnabled(false);
EXPECT_EQ(4, client.set_caps_lock_count_);
EXPECT_FALSE(controller->IsCapsLockEnabled());
controller->UpdateCapsLockState(true);
EXPECT_TRUE(controller->IsCapsLockEnabled());
controller->UpdateCapsLockState(false);
EXPECT_FALSE(controller->IsCapsLockEnabled());
}
TEST_F(ImeControllerImplTest, OnKeyboardLayoutNameChanged) {
ImeControllerImpl* controller = Shell::Get()->ime_controller();
EXPECT_TRUE(controller->keyboard_layout_name().empty());
TestImeControllerObserver observer;
controller->AddObserver(&observer);
controller->OnKeyboardLayoutNameChanged("us(dvorak)");
EXPECT_EQ("us(dvorak)", controller->keyboard_layout_name());
EXPECT_EQ("us(dvorak)", observer.last_keyboard_layout_name());
}
TEST_F(ImeControllerImplTest, ShowModeIndicator) {
ImeControllerImpl* controller = Shell::Get()->ime_controller();
std::u16string text = u"US";
gfx::Rect cursor1_bounds(100, 100, 1, 20);
controller->ShowModeIndicator(cursor1_bounds, text);
views::Widget* widget1 =
controller->mode_indicator_observer()->active_widget();
EXPECT_TRUE(widget1);
// The widget bounds should be bigger than the inner size.
gfx::Rect bounds1 = widget1->GetWindowBoundsInScreen();
EXPECT_LE(kInnerSize, bounds1.width());
EXPECT_LE(kInnerSize, bounds1.height());
gfx::Rect cursor2_bounds(50, 200, 1, 20);
controller->ShowModeIndicator(cursor2_bounds, text);
views::Widget* widget2 =
controller->mode_indicator_observer()->active_widget();
EXPECT_TRUE(widget2);
EXPECT_NE(widget1, widget2);
// Check if the location of the mode indicator corresponds to the cursor
// bounds.
gfx::Rect bounds2 = widget2->GetWindowBoundsInScreen();
EXPECT_EQ(cursor1_bounds.x() - cursor2_bounds.x(), bounds1.x() - bounds2.x());
EXPECT_EQ(cursor1_bounds.y() - cursor2_bounds.y(), bounds1.y() - bounds2.y());
EXPECT_EQ(bounds1.width(), bounds2.width());
EXPECT_EQ(bounds1.height(), bounds2.height());
const gfx::Rect screen_bounds = display::Screen::GetScreen()
->GetDisplayMatching(cursor1_bounds)
.work_area();
const gfx::Rect cursor3_bounds(100, screen_bounds.bottom() - 25, 1, 20);
controller->ShowModeIndicator(cursor3_bounds, text);
views::Widget* widget3 =
controller->mode_indicator_observer()->active_widget();
EXPECT_TRUE(widget3);
EXPECT_NE(widget2, widget3);
// Check if the location of the mode indicator is considered with the screen
// size.
gfx::Rect bounds3 = widget3->GetWindowBoundsInScreen();
EXPECT_LT(bounds3.bottom(), screen_bounds.bottom());
}
TEST_F(ImeControllerImplTest, MirroringChanged) {
UpdateDisplay("500x500,500x500");
// The controller is already an observer of the display_manager
ImeControllerImpl* controller = Shell::Get()->ime_controller();
TestImeControllerClient client;
controller->SetClient(&client);
display::DisplayManager* display_manager = Shell::Get()->display_manager();
display_manager->SetMultiDisplayMode(display::DisplayManager::MIRRORING);
display_manager->UpdateDisplays();
EXPECT_TRUE(client.is_mirroring_);
UpdateDisplay("500x500");
EXPECT_FALSE(client.is_mirroring_);
UpdateDisplay("500x500,500x500");
EXPECT_TRUE(client.is_mirroring_);
}
} // namespace
} // namespace ash