OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/chromeos/input_method/xkeyboard.h" | 5 #include "chrome/browser/chromeos/input_method/xkeyboard.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <set> | 8 #include <set> |
9 #include <string> | 9 #include <string> |
10 | 10 |
(...skipping 16 matching lines...) Expand all Loading... |
27 DISABLED_TestCreateFullXkbLayoutNameKeepAlt | 27 DISABLED_TestCreateFullXkbLayoutNameKeepAlt |
28 #define TestCreateFullXkbLayoutNameKeepCapsLock \ | 28 #define TestCreateFullXkbLayoutNameKeepCapsLock \ |
29 DISABLED_TestCreateFullXkbLayoutNameKeepCapsLock | 29 DISABLED_TestCreateFullXkbLayoutNameKeepCapsLock |
30 #endif // USE_VIRTUAL_KEYBOARD | 30 #endif // USE_VIRTUAL_KEYBOARD |
31 | 31 |
32 namespace chromeos { | 32 namespace chromeos { |
33 namespace input_method { | 33 namespace input_method { |
34 | 34 |
35 namespace { | 35 namespace { |
36 | 36 |
37 class TestableXKeyboard : public XKeyboard { | |
38 public: | |
39 explicit TestableXKeyboard(const InputMethodUtil& util) : XKeyboard(util) { | |
40 } | |
41 | |
42 // Change access rights. | |
43 using XKeyboard::CreateFullXkbLayoutName; | |
44 using XKeyboard::ContainsModifierKeyAsReplacement; | |
45 using XKeyboard::GetAutoRepeatEnabled; | |
46 using XKeyboard::GetAutoRepeatRate; | |
47 }; | |
48 | |
49 class XKeyboardTest : public testing::Test { | 37 class XKeyboardTest : public testing::Test { |
50 public: | 38 public: |
51 XKeyboardTest() | 39 XKeyboardTest() |
52 : controller_(IBusController::Create()), | 40 : controller_(IBusController::Create()), |
53 util_(controller_->GetSupportedInputMethods()), | 41 util_(controller_->GetSupportedInputMethods()), |
54 ui_thread_(BrowserThread::UI, &message_loop_) { | 42 ui_thread_(BrowserThread::UI, &message_loop_) { |
55 } | 43 } |
56 | 44 |
57 virtual void SetUp() { | 45 virtual void SetUp() { |
58 xkey_.reset(new TestableXKeyboard(util_)); | 46 xkey_.reset(XKeyboard::Create(util_)); |
59 } | 47 } |
60 | 48 |
61 virtual void TearDown() { | 49 virtual void TearDown() { |
62 xkey_.reset(); | 50 xkey_.reset(); |
63 } | 51 } |
64 | 52 |
65 scoped_ptr<IBusController> controller_; | 53 scoped_ptr<IBusController> controller_; |
66 InputMethodUtil util_; | 54 InputMethodUtil util_; |
67 scoped_ptr<TestableXKeyboard> xkey_; | 55 scoped_ptr<XKeyboard> xkey_; |
68 | 56 |
69 MessageLoopForUI message_loop_; | 57 MessageLoopForUI message_loop_; |
70 content::TestBrowserThread ui_thread_; | 58 content::TestBrowserThread ui_thread_; |
71 }; | 59 }; |
72 | 60 |
73 // Returns a ModifierMap object that contains the following mapping: | 61 // Returns a ModifierMap object that contains the following mapping: |
74 // - kSearchKey is mapped to |search|. | 62 // - kSearchKey is mapped to |search|. |
75 // - kLeftControl key is mapped to |control|. | 63 // - kLeftControl key is mapped to |control|. |
76 // - kLeftAlt key is mapped to |alt|. | 64 // - kLeftAlt key is mapped to |alt|. |
77 ModifierMap GetMap(ModifierKey search, ModifierKey control, ModifierKey alt) { | 65 ModifierMap GetMap(ModifierKey search, ModifierKey control, ModifierKey alt) { |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 TEST_F(XKeyboardTest, TestSetCapsLockEnabled) { | 246 TEST_F(XKeyboardTest, TestSetCapsLockEnabled) { |
259 if (!DisplayAvailable()) { | 247 if (!DisplayAvailable()) { |
260 // Do not fail the test to allow developers to run unit_tests without an X | 248 // Do not fail the test to allow developers to run unit_tests without an X |
261 // server (e.g. via ssh). Note that both try bots and waterfall always have | 249 // server (e.g. via ssh). Note that both try bots and waterfall always have |
262 // an X server for running browser_tests. | 250 // an X server for running browser_tests. |
263 LOG(INFO) << "X server is not available. Skip the test."; | 251 LOG(INFO) << "X server is not available. Skip the test."; |
264 return; | 252 return; |
265 } | 253 } |
266 const bool initial_lock_state = xkey_->CapsLockIsEnabled(); | 254 const bool initial_lock_state = xkey_->CapsLockIsEnabled(); |
267 xkey_->SetCapsLockEnabled(true); | 255 xkey_->SetCapsLockEnabled(true); |
268 EXPECT_TRUE(TestableXKeyboard::CapsLockIsEnabled()); | 256 EXPECT_TRUE(xkey_->CapsLockIsEnabled()); |
269 xkey_->SetCapsLockEnabled(false); | 257 xkey_->SetCapsLockEnabled(false); |
270 EXPECT_FALSE(TestableXKeyboard::CapsLockIsEnabled()); | 258 EXPECT_FALSE(xkey_->CapsLockIsEnabled()); |
271 xkey_->SetCapsLockEnabled(true); | 259 xkey_->SetCapsLockEnabled(true); |
272 EXPECT_TRUE(TestableXKeyboard::CapsLockIsEnabled()); | 260 EXPECT_TRUE(xkey_->CapsLockIsEnabled()); |
273 xkey_->SetCapsLockEnabled(false); | 261 xkey_->SetCapsLockEnabled(false); |
274 EXPECT_FALSE(TestableXKeyboard::CapsLockIsEnabled()); | 262 EXPECT_FALSE(xkey_->CapsLockIsEnabled()); |
275 xkey_->SetCapsLockEnabled(initial_lock_state); | 263 xkey_->SetCapsLockEnabled(initial_lock_state); |
276 } | 264 } |
277 | 265 |
278 TEST_F(XKeyboardTest, TestSetNumLockEnabled) { | 266 TEST_F(XKeyboardTest, TestSetNumLockEnabled) { |
279 if (!DisplayAvailable()) { | 267 if (!DisplayAvailable()) { |
280 LOG(INFO) << "X server is not available. Skip the test."; | 268 LOG(INFO) << "X server is not available. Skip the test."; |
281 return; | 269 return; |
282 } | 270 } |
283 const unsigned int num_lock_mask = TestableXKeyboard::GetNumLockMask(); | 271 const unsigned int num_lock_mask = xkey_->GetNumLockMask(); |
284 ASSERT_NE(0U, num_lock_mask); | 272 ASSERT_NE(0U, num_lock_mask); |
285 | 273 |
286 const bool initial_lock_state = xkey_->NumLockIsEnabled(num_lock_mask); | 274 const bool initial_lock_state = xkey_->NumLockIsEnabled(); |
287 xkey_->SetNumLockEnabled(true); | 275 xkey_->SetNumLockEnabled(true); |
288 EXPECT_TRUE(TestableXKeyboard::NumLockIsEnabled(num_lock_mask)); | 276 EXPECT_TRUE(xkey_->NumLockIsEnabled()); |
289 xkey_->SetNumLockEnabled(false); | 277 xkey_->SetNumLockEnabled(false); |
290 EXPECT_FALSE(TestableXKeyboard::NumLockIsEnabled(num_lock_mask)); | 278 EXPECT_FALSE(xkey_->NumLockIsEnabled()); |
291 xkey_->SetNumLockEnabled(true); | 279 xkey_->SetNumLockEnabled(true); |
292 EXPECT_TRUE(TestableXKeyboard::NumLockIsEnabled(num_lock_mask)); | 280 EXPECT_TRUE(xkey_->NumLockIsEnabled()); |
293 xkey_->SetNumLockEnabled(false); | 281 xkey_->SetNumLockEnabled(false); |
294 EXPECT_FALSE(TestableXKeyboard::NumLockIsEnabled(num_lock_mask)); | 282 EXPECT_FALSE(xkey_->NumLockIsEnabled()); |
295 xkey_->SetNumLockEnabled(initial_lock_state); | 283 xkey_->SetNumLockEnabled(initial_lock_state); |
296 } | 284 } |
297 | 285 |
298 TEST_F(XKeyboardTest, TestSetCapsLockAndNumLockAtTheSameTime) { | 286 TEST_F(XKeyboardTest, TestSetCapsLockAndNumLockAtTheSameTime) { |
299 if (!DisplayAvailable()) { | 287 if (!DisplayAvailable()) { |
300 LOG(INFO) << "X server is not available. Skip the test."; | 288 LOG(INFO) << "X server is not available. Skip the test."; |
301 return; | 289 return; |
302 } | 290 } |
303 const unsigned int num_lock_mask = TestableXKeyboard::GetNumLockMask(); | 291 const unsigned int num_lock_mask = xkey_->GetNumLockMask(); |
304 ASSERT_NE(0U, num_lock_mask); | 292 ASSERT_NE(0U, num_lock_mask); |
305 | 293 |
306 const bool initial_caps_lock_state = xkey_->CapsLockIsEnabled(); | 294 const bool initial_caps_lock_state = xkey_->CapsLockIsEnabled(); |
307 const bool initial_num_lock_state = xkey_->NumLockIsEnabled(num_lock_mask); | 295 const bool initial_num_lock_state = xkey_->NumLockIsEnabled(); |
308 | 296 |
309 // Flip both. | 297 // Flip both. |
310 xkey_->SetLockedModifiers( | 298 xkey_->SetLockedModifiers( |
311 initial_caps_lock_state ? kDisableLock : kEnableLock, | 299 initial_caps_lock_state ? kDisableLock : kEnableLock, |
312 initial_num_lock_state ? kDisableLock : kEnableLock); | 300 initial_num_lock_state ? kDisableLock : kEnableLock); |
313 EXPECT_EQ(!initial_caps_lock_state, | 301 EXPECT_EQ(!initial_caps_lock_state, xkey_->CapsLockIsEnabled()); |
314 TestableXKeyboard::CapsLockIsEnabled()); | 302 EXPECT_EQ(!initial_num_lock_state, xkey_->NumLockIsEnabled()); |
315 EXPECT_EQ(!initial_num_lock_state, | |
316 TestableXKeyboard::NumLockIsEnabled(num_lock_mask)); | |
317 | 303 |
318 // Flip Caps Lock. | 304 // Flip Caps Lock. |
319 xkey_->SetLockedModifiers( | 305 xkey_->SetLockedModifiers( |
320 initial_caps_lock_state ? kEnableLock : kDisableLock, | 306 initial_caps_lock_state ? kEnableLock : kDisableLock, |
321 kDontChange); | 307 kDontChange); |
322 // Use GetLockedModifiers() for verifying the result. | 308 // Use GetLockedModifiers() for verifying the result. |
323 bool c, n; | 309 bool c, n; |
324 TestableXKeyboard::GetLockedModifiers(num_lock_mask, &c, &n); | 310 xkey_->GetLockedModifiers(&c, &n); |
325 EXPECT_EQ(initial_caps_lock_state, c); | 311 EXPECT_EQ(initial_caps_lock_state, c); |
326 EXPECT_EQ(!initial_num_lock_state, n); | 312 EXPECT_EQ(!initial_num_lock_state, n); |
327 | 313 |
328 // Flip both. | 314 // Flip both. |
329 xkey_->SetLockedModifiers( | 315 xkey_->SetLockedModifiers( |
330 initial_caps_lock_state ? kDisableLock : kEnableLock, | 316 initial_caps_lock_state ? kDisableLock : kEnableLock, |
331 initial_num_lock_state ? kEnableLock : kDisableLock); | 317 initial_num_lock_state ? kEnableLock : kDisableLock); |
332 EXPECT_EQ(!initial_caps_lock_state, | 318 EXPECT_EQ(!initial_caps_lock_state, xkey_->CapsLockIsEnabled()); |
333 TestableXKeyboard::CapsLockIsEnabled()); | 319 EXPECT_EQ(initial_num_lock_state, xkey_->NumLockIsEnabled()); |
334 EXPECT_EQ(initial_num_lock_state, | |
335 TestableXKeyboard::NumLockIsEnabled(num_lock_mask)); | |
336 | 320 |
337 // Flip Num Lock. | 321 // Flip Num Lock. |
338 xkey_->SetLockedModifiers( | 322 xkey_->SetLockedModifiers( |
339 kDontChange, | 323 kDontChange, |
340 initial_num_lock_state ? kDisableLock : kEnableLock); | 324 initial_num_lock_state ? kDisableLock : kEnableLock); |
341 TestableXKeyboard::GetLockedModifiers(num_lock_mask, &c, &n); | 325 xkey_->GetLockedModifiers(&c, &n); |
342 EXPECT_EQ(!initial_caps_lock_state, c); | 326 EXPECT_EQ(!initial_caps_lock_state, c); |
343 EXPECT_EQ(!initial_num_lock_state, n); | 327 EXPECT_EQ(!initial_num_lock_state, n); |
344 | 328 |
345 // Flip both to restore the initial state. | 329 // Flip both to restore the initial state. |
346 xkey_->SetLockedModifiers( | 330 xkey_->SetLockedModifiers( |
347 initial_caps_lock_state ? kEnableLock : kDisableLock, | 331 initial_caps_lock_state ? kEnableLock : kDisableLock, |
348 initial_num_lock_state ? kEnableLock : kDisableLock); | 332 initial_num_lock_state ? kEnableLock : kDisableLock); |
349 EXPECT_EQ(initial_caps_lock_state, | 333 EXPECT_EQ(initial_caps_lock_state, xkey_->CapsLockIsEnabled()); |
350 TestableXKeyboard::CapsLockIsEnabled()); | 334 EXPECT_EQ(initial_num_lock_state, xkey_->NumLockIsEnabled()); |
351 EXPECT_EQ(initial_num_lock_state, | |
352 TestableXKeyboard::NumLockIsEnabled(num_lock_mask)); | |
353 | 335 |
354 // No-op SetLockedModifiers call. | 336 // No-op SetLockedModifiers call. |
355 xkey_->SetLockedModifiers(kDontChange, kDontChange); | 337 xkey_->SetLockedModifiers(kDontChange, kDontChange); |
356 EXPECT_EQ(initial_caps_lock_state, | 338 EXPECT_EQ(initial_caps_lock_state, xkey_->CapsLockIsEnabled()); |
357 TestableXKeyboard::CapsLockIsEnabled()); | 339 EXPECT_EQ(initial_num_lock_state, xkey_->NumLockIsEnabled()); |
358 EXPECT_EQ(initial_num_lock_state, | |
359 TestableXKeyboard::NumLockIsEnabled(num_lock_mask)); | |
360 | 340 |
361 // No-op GetLockedModifiers call. Confirm it does not crash. | 341 // No-op GetLockedModifiers call. Confirm it does not crash. |
362 TestableXKeyboard::GetLockedModifiers(0, NULL, NULL); | 342 xkey_->GetLockedModifiers(NULL, NULL); |
363 } | 343 } |
364 | 344 |
365 TEST_F(XKeyboardTest, TestContainsModifierKeyAsReplacement) { | 345 TEST_F(XKeyboardTest, TestContainsModifierKeyAsReplacement) { |
366 EXPECT_FALSE(TestableXKeyboard::ContainsModifierKeyAsReplacement( | 346 EXPECT_FALSE(XKeyboard::ContainsModifierKeyAsReplacement( |
367 GetMap(kVoidKey, kVoidKey, kVoidKey), kCapsLockKey)); | 347 GetMap(kVoidKey, kVoidKey, kVoidKey), kCapsLockKey)); |
368 EXPECT_TRUE(TestableXKeyboard::ContainsModifierKeyAsReplacement( | 348 EXPECT_TRUE(XKeyboard::ContainsModifierKeyAsReplacement( |
369 GetMap(kCapsLockKey, kVoidKey, kVoidKey), kCapsLockKey)); | 349 GetMap(kCapsLockKey, kVoidKey, kVoidKey), kCapsLockKey)); |
370 EXPECT_TRUE(TestableXKeyboard::ContainsModifierKeyAsReplacement( | 350 EXPECT_TRUE(XKeyboard::ContainsModifierKeyAsReplacement( |
371 GetMap(kVoidKey, kCapsLockKey, kVoidKey), kCapsLockKey)); | 351 GetMap(kVoidKey, kCapsLockKey, kVoidKey), kCapsLockKey)); |
372 EXPECT_TRUE(TestableXKeyboard::ContainsModifierKeyAsReplacement( | 352 EXPECT_TRUE(XKeyboard::ContainsModifierKeyAsReplacement( |
373 GetMap(kVoidKey, kVoidKey, kCapsLockKey), kCapsLockKey)); | 353 GetMap(kVoidKey, kVoidKey, kCapsLockKey), kCapsLockKey)); |
374 EXPECT_TRUE(TestableXKeyboard::ContainsModifierKeyAsReplacement( | 354 EXPECT_TRUE(XKeyboard::ContainsModifierKeyAsReplacement( |
375 GetMap(kCapsLockKey, kCapsLockKey, kVoidKey), kCapsLockKey)); | 355 GetMap(kCapsLockKey, kCapsLockKey, kVoidKey), kCapsLockKey)); |
376 EXPECT_TRUE(TestableXKeyboard::ContainsModifierKeyAsReplacement( | 356 EXPECT_TRUE(XKeyboard::ContainsModifierKeyAsReplacement( |
377 GetMap(kCapsLockKey, kCapsLockKey, kCapsLockKey), kCapsLockKey)); | 357 GetMap(kCapsLockKey, kCapsLockKey, kCapsLockKey), kCapsLockKey)); |
378 EXPECT_TRUE(TestableXKeyboard::ContainsModifierKeyAsReplacement( | 358 EXPECT_TRUE(XKeyboard::ContainsModifierKeyAsReplacement( |
379 GetMap(kSearchKey, kVoidKey, kVoidKey), kSearchKey)); | 359 GetMap(kSearchKey, kVoidKey, kVoidKey), kSearchKey)); |
380 } | 360 } |
381 | 361 |
382 TEST_F(XKeyboardTest, TestSetAutoRepeatEnabled) { | 362 TEST_F(XKeyboardTest, TestSetAutoRepeatEnabled) { |
383 if (!DisplayAvailable()) { | 363 if (!DisplayAvailable()) { |
384 LOG(INFO) << "X server is not available. Skip the test."; | 364 LOG(INFO) << "X server is not available. Skip the test."; |
385 return; | 365 return; |
386 } | 366 } |
387 const bool state = TestableXKeyboard::GetAutoRepeatEnabled(); | 367 const bool state = XKeyboard::GetAutoRepeatEnabledForTesting(); |
388 TestableXKeyboard::SetAutoRepeatEnabled(!state); | 368 XKeyboard::SetAutoRepeatEnabled(!state); |
389 EXPECT_EQ(!state, TestableXKeyboard::GetAutoRepeatEnabled()); | 369 EXPECT_EQ(!state, XKeyboard::GetAutoRepeatEnabledForTesting()); |
390 // Restore the initial state. | 370 // Restore the initial state. |
391 TestableXKeyboard::SetAutoRepeatEnabled(state); | 371 XKeyboard::SetAutoRepeatEnabled(state); |
392 EXPECT_EQ(state, TestableXKeyboard::GetAutoRepeatEnabled()); | 372 EXPECT_EQ(state, XKeyboard::GetAutoRepeatEnabledForTesting()); |
393 } | 373 } |
394 | 374 |
395 TEST_F(XKeyboardTest, TestSetAutoRepeatRate) { | 375 TEST_F(XKeyboardTest, TestSetAutoRepeatRate) { |
396 if (!DisplayAvailable()) { | 376 if (!DisplayAvailable()) { |
397 LOG(INFO) << "X server is not available. Skip the test."; | 377 LOG(INFO) << "X server is not available. Skip the test."; |
398 return; | 378 return; |
399 } | 379 } |
400 AutoRepeatRate rate; | 380 AutoRepeatRate rate; |
401 EXPECT_TRUE(TestableXKeyboard::GetAutoRepeatRate(&rate)); | 381 EXPECT_TRUE(XKeyboard::GetAutoRepeatRateForTesting(&rate)); |
402 | 382 |
403 AutoRepeatRate tmp(rate); | 383 AutoRepeatRate tmp(rate); |
404 ++tmp.initial_delay_in_ms; | 384 ++tmp.initial_delay_in_ms; |
405 ++tmp.repeat_interval_in_ms; | 385 ++tmp.repeat_interval_in_ms; |
406 EXPECT_TRUE(TestableXKeyboard::SetAutoRepeatRate(tmp)); | 386 EXPECT_TRUE(XKeyboard::SetAutoRepeatRate(tmp)); |
407 EXPECT_TRUE(TestableXKeyboard::GetAutoRepeatRate(&tmp)); | 387 EXPECT_TRUE(XKeyboard::GetAutoRepeatRateForTesting(&tmp)); |
408 EXPECT_EQ(rate.initial_delay_in_ms + 1, tmp.initial_delay_in_ms); | 388 EXPECT_EQ(rate.initial_delay_in_ms + 1, tmp.initial_delay_in_ms); |
409 EXPECT_EQ(rate.repeat_interval_in_ms + 1, tmp.repeat_interval_in_ms); | 389 EXPECT_EQ(rate.repeat_interval_in_ms + 1, tmp.repeat_interval_in_ms); |
410 | 390 |
411 // Restore the initial state. | 391 // Restore the initial state. |
412 EXPECT_TRUE(TestableXKeyboard::SetAutoRepeatRate(rate)); | 392 EXPECT_TRUE(XKeyboard::SetAutoRepeatRate(rate)); |
413 EXPECT_TRUE(TestableXKeyboard::GetAutoRepeatRate(&tmp)); | 393 EXPECT_TRUE(XKeyboard::GetAutoRepeatRateForTesting(&tmp)); |
414 EXPECT_EQ(rate.initial_delay_in_ms, tmp.initial_delay_in_ms); | 394 EXPECT_EQ(rate.initial_delay_in_ms, tmp.initial_delay_in_ms); |
415 EXPECT_EQ(rate.repeat_interval_in_ms, tmp.repeat_interval_in_ms); | 395 EXPECT_EQ(rate.repeat_interval_in_ms, tmp.repeat_interval_in_ms); |
416 } | 396 } |
417 | 397 |
418 } // namespace input_method | 398 } // namespace input_method |
419 } // namespace chromeos | 399 } // namespace chromeos |
OLD | NEW |