OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chromeos/ime/xkeyboard.h" | 5 #include "chromeos/ime/keyboard_controller.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <set> | 8 #include <set> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 | 15 |
16 #include <X11/Xlib.h> | 16 #include <X11/Xlib.h> |
17 | 17 |
18 namespace chromeos { | 18 namespace chromeos { |
19 namespace input_method { | 19 namespace input_method { |
20 | 20 |
21 namespace { | 21 namespace { |
22 | 22 |
23 class XKeyboardTest : public testing::Test { | 23 class KeyboardControllerTest : public testing::Test { |
24 public: | 24 public: |
25 XKeyboardTest() { | 25 KeyboardControllerTest() { |
26 } | 26 } |
27 | 27 |
28 virtual void SetUp() { | 28 virtual void SetUp() { |
29 xkey_.reset(XKeyboard::Create()); | 29 xkey_.reset(KeyboardController::Create()); |
30 } | 30 } |
31 | 31 |
32 virtual void TearDown() { | 32 virtual void TearDown() { |
33 xkey_.reset(); | 33 xkey_.reset(); |
34 } | 34 } |
35 | 35 |
36 scoped_ptr<XKeyboard> xkey_; | 36 scoped_ptr<KeyboardController> xkey_; |
37 | 37 |
38 base::MessageLoopForUI message_loop_; | 38 base::MessageLoopForUI message_loop_; |
39 }; | 39 }; |
40 | 40 |
41 // Returns true if X display is available. | 41 // Returns true if X display is available. |
42 bool DisplayAvailable() { | 42 bool DisplayAvailable() { |
43 return (base::MessagePumpForUI::GetDefaultXDisplay() != NULL); | 43 return (base::MessagePumpForUI::GetDefaultXDisplay() != NULL); |
44 } | 44 } |
45 | 45 |
46 } // namespace | 46 } // namespace |
47 | 47 |
48 // Tests CheckLayoutName() function. | 48 // Tests CheckLayoutName() function. |
49 TEST_F(XKeyboardTest, TestCheckLayoutName) { | 49 TEST_F(KeyboardControllerTest, TestCheckLayoutName) { |
50 // CheckLayoutName should not accept non-alphanumeric characters | 50 // CheckLayoutName should not accept non-alphanumeric characters |
51 // except "()-_". | 51 // except "()-_". |
52 EXPECT_FALSE(XKeyboard::CheckLayoutNameForTesting("us!")); | 52 EXPECT_FALSE(KeyboardController::CheckLayoutNameForTesting("us!")); |
53 EXPECT_FALSE(XKeyboard::CheckLayoutNameForTesting("us; /bin/sh")); | 53 EXPECT_FALSE(KeyboardController::CheckLayoutNameForTesting("us; /bin/sh")); |
54 EXPECT_TRUE(XKeyboard::CheckLayoutNameForTesting("ab-c_12")); | 54 EXPECT_TRUE(KeyboardController::CheckLayoutNameForTesting("ab-c_12")); |
55 | 55 |
56 // CheckLayoutName should not accept upper-case ascii characters. | 56 // CheckLayoutName should not accept upper-case ascii characters. |
57 EXPECT_FALSE(XKeyboard::CheckLayoutNameForTesting("US")); | 57 EXPECT_FALSE(KeyboardController::CheckLayoutNameForTesting("US")); |
58 | 58 |
59 // CheckLayoutName should accept lower-case ascii characters. | 59 // CheckLayoutName should accept lower-case ascii characters. |
60 for (int c = 'a'; c <= 'z'; ++c) { | 60 for (int c = 'a'; c <= 'z'; ++c) { |
61 EXPECT_TRUE(XKeyboard::CheckLayoutNameForTesting(std::string(3, c))); | 61 EXPECT_TRUE( |
| 62 KeyboardController::CheckLayoutNameForTesting(std::string(3, c))); |
62 } | 63 } |
63 | 64 |
64 // CheckLayoutName should accept numbers. | 65 // CheckLayoutName should accept numbers. |
65 for (int c = '0'; c <= '9'; ++c) { | 66 for (int c = '0'; c <= '9'; ++c) { |
66 EXPECT_TRUE(XKeyboard::CheckLayoutNameForTesting(std::string(3, c))); | 67 EXPECT_TRUE( |
| 68 KeyboardController::CheckLayoutNameForTesting(std::string(3, c))); |
67 } | 69 } |
68 | 70 |
69 // CheckLayoutName should accept a layout with a variant name. | 71 // CheckLayoutName should accept a layout with a variant name. |
70 EXPECT_TRUE(XKeyboard::CheckLayoutNameForTesting("us(dvorak)")); | 72 EXPECT_TRUE(KeyboardController::CheckLayoutNameForTesting("us(dvorak)")); |
71 EXPECT_TRUE(XKeyboard::CheckLayoutNameForTesting("jp")); | 73 EXPECT_TRUE(KeyboardController::CheckLayoutNameForTesting("jp")); |
72 } | 74 } |
73 | 75 |
74 TEST_F(XKeyboardTest, TestSetCapsLockEnabled) { | 76 TEST_F(KeyboardControllerTest, TestSetCapsLockEnabled) { |
75 if (!DisplayAvailable()) { | 77 if (!DisplayAvailable()) { |
76 // Do not fail the test to allow developers to run unit_tests without an X | 78 // Do not fail the test to allow developers to run unit_tests without an X |
77 // server (e.g. via ssh). Note that both try bots and waterfall always have | 79 // server (e.g. via ssh). Note that both try bots and waterfall always have |
78 // an X server for running browser_tests. | 80 // an X server for running browser_tests. |
79 DVLOG(1) << "X server is not available. Skip the test."; | 81 DVLOG(1) << "X server is not available. Skip the test."; |
80 return; | 82 return; |
81 } | 83 } |
82 const bool initial_lock_state = xkey_->CapsLockIsEnabled(); | 84 const bool initial_lock_state = xkey_->CapsLockIsEnabled(); |
83 xkey_->SetCapsLockEnabled(true); | 85 xkey_->SetCapsLockEnabled(true); |
84 EXPECT_TRUE(xkey_->CapsLockIsEnabled()); | 86 EXPECT_TRUE(xkey_->CapsLockIsEnabled()); |
85 xkey_->SetCapsLockEnabled(false); | 87 xkey_->SetCapsLockEnabled(false); |
86 EXPECT_FALSE(xkey_->CapsLockIsEnabled()); | 88 EXPECT_FALSE(xkey_->CapsLockIsEnabled()); |
87 xkey_->SetCapsLockEnabled(true); | 89 xkey_->SetCapsLockEnabled(true); |
88 EXPECT_TRUE(xkey_->CapsLockIsEnabled()); | 90 EXPECT_TRUE(xkey_->CapsLockIsEnabled()); |
89 xkey_->SetCapsLockEnabled(false); | 91 xkey_->SetCapsLockEnabled(false); |
90 EXPECT_FALSE(xkey_->CapsLockIsEnabled()); | 92 EXPECT_FALSE(xkey_->CapsLockIsEnabled()); |
91 xkey_->SetCapsLockEnabled(initial_lock_state); | 93 xkey_->SetCapsLockEnabled(initial_lock_state); |
92 } | 94 } |
93 | 95 |
94 TEST_F(XKeyboardTest, TestSetAutoRepeatEnabled) { | 96 TEST_F(KeyboardControllerTest, TestSetAutoRepeatEnabled) { |
95 if (!DisplayAvailable()) { | 97 if (!DisplayAvailable()) { |
96 DVLOG(1) << "X server is not available. Skip the test."; | 98 DVLOG(1) << "X server is not available. Skip the test."; |
97 return; | 99 return; |
98 } | 100 } |
99 const bool state = XKeyboard::GetAutoRepeatEnabledForTesting(); | 101 const bool state = KeyboardController::GetAutoRepeatEnabledForTesting(); |
100 xkey_->SetAutoRepeatEnabled(!state); | 102 xkey_->SetAutoRepeatEnabled(!state); |
101 EXPECT_EQ(!state, XKeyboard::GetAutoRepeatEnabledForTesting()); | 103 EXPECT_EQ(!state, KeyboardController::GetAutoRepeatEnabledForTesting()); |
102 // Restore the initial state. | 104 // Restore the initial state. |
103 xkey_->SetAutoRepeatEnabled(state); | 105 xkey_->SetAutoRepeatEnabled(state); |
104 EXPECT_EQ(state, XKeyboard::GetAutoRepeatEnabledForTesting()); | 106 EXPECT_EQ(state, KeyboardController::GetAutoRepeatEnabledForTesting()); |
105 } | 107 } |
106 | 108 |
107 TEST_F(XKeyboardTest, TestSetAutoRepeatRate) { | 109 TEST_F(KeyboardControllerTest, TestSetAutoRepeatRate) { |
108 if (!DisplayAvailable()) { | 110 if (!DisplayAvailable()) { |
109 DVLOG(1) << "X server is not available. Skip the test."; | 111 DVLOG(1) << "X server is not available. Skip the test."; |
110 return; | 112 return; |
111 } | 113 } |
112 AutoRepeatRate rate; | 114 AutoRepeatRate rate; |
113 EXPECT_TRUE(XKeyboard::GetAutoRepeatRateForTesting(&rate)); | 115 EXPECT_TRUE(KeyboardController::GetAutoRepeatRateForTesting(&rate)); |
114 | 116 |
115 AutoRepeatRate tmp(rate); | 117 AutoRepeatRate tmp(rate); |
116 ++tmp.initial_delay_in_ms; | 118 ++tmp.initial_delay_in_ms; |
117 ++tmp.repeat_interval_in_ms; | 119 ++tmp.repeat_interval_in_ms; |
118 EXPECT_TRUE(xkey_->SetAutoRepeatRate(tmp)); | 120 EXPECT_TRUE(xkey_->SetAutoRepeatRate(tmp)); |
119 EXPECT_TRUE(XKeyboard::GetAutoRepeatRateForTesting(&tmp)); | 121 EXPECT_TRUE(KeyboardController::GetAutoRepeatRateForTesting(&tmp)); |
120 EXPECT_EQ(rate.initial_delay_in_ms + 1, tmp.initial_delay_in_ms); | 122 EXPECT_EQ(rate.initial_delay_in_ms + 1, tmp.initial_delay_in_ms); |
121 EXPECT_EQ(rate.repeat_interval_in_ms + 1, tmp.repeat_interval_in_ms); | 123 EXPECT_EQ(rate.repeat_interval_in_ms + 1, tmp.repeat_interval_in_ms); |
122 | 124 |
123 // Restore the initial state. | 125 // Restore the initial state. |
124 EXPECT_TRUE(xkey_->SetAutoRepeatRate(rate)); | 126 EXPECT_TRUE(xkey_->SetAutoRepeatRate(rate)); |
125 EXPECT_TRUE(XKeyboard::GetAutoRepeatRateForTesting(&tmp)); | 127 EXPECT_TRUE(KeyboardController::GetAutoRepeatRateForTesting(&tmp)); |
126 EXPECT_EQ(rate.initial_delay_in_ms, tmp.initial_delay_in_ms); | 128 EXPECT_EQ(rate.initial_delay_in_ms, tmp.initial_delay_in_ms); |
127 EXPECT_EQ(rate.repeat_interval_in_ms, tmp.repeat_interval_in_ms); | 129 EXPECT_EQ(rate.repeat_interval_in_ms, tmp.repeat_interval_in_ms); |
128 } | 130 } |
129 | 131 |
130 } // namespace input_method | 132 } // namespace input_method |
131 } // namespace chromeos | 133 } // namespace chromeos |
OLD | NEW |