OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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. | |
4 | |
5 #include "chrome/browser/chromeos/input_method/xkeyboard.h" | |
6 | |
7 #include <algorithm> | |
8 #include <set> | |
9 #include <string> | |
10 | |
11 #include "base/logging.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "base/message_loop.h" | |
14 #include "chromeos/ime/input_method_whitelist.h" | |
15 #include "chromeos/ime/mock_input_method_delegate.h" | |
16 #include "testing/gtest/include/gtest/gtest.h" | |
17 | |
18 #include <X11/Xlib.h> | |
19 | |
20 namespace chromeos { | |
21 namespace input_method { | |
22 | |
23 namespace { | |
24 | |
25 class XKeyboardTest : public testing::Test { | |
26 public: | |
27 XKeyboardTest() { | |
28 } | |
29 | |
30 virtual void SetUp() { | |
31 xkey_.reset(XKeyboard::Create()); | |
32 } | |
33 | |
34 virtual void TearDown() { | |
35 xkey_.reset(); | |
36 } | |
37 | |
38 InputMethodWhitelist whitelist_; | |
39 scoped_ptr<XKeyboard> xkey_; | |
40 | |
41 MessageLoopForUI message_loop_; | |
42 }; | |
43 | |
44 // Returns true if X display is available. | |
45 bool DisplayAvailable() { | |
46 return (base::MessagePumpForUI::GetDefaultXDisplay() != NULL); | |
47 } | |
48 | |
49 } // namespace | |
50 | |
51 // Tests CheckLayoutName() function. | |
52 TEST_F(XKeyboardTest, TestCheckLayoutName) { | |
53 // CheckLayoutName should not accept non-alphanumeric characters | |
54 // except "()-_". | |
55 EXPECT_FALSE(XKeyboard::CheckLayoutNameForTesting("us!")); | |
56 EXPECT_FALSE(XKeyboard::CheckLayoutNameForTesting("us; /bin/sh")); | |
57 EXPECT_TRUE(XKeyboard::CheckLayoutNameForTesting("ab-c_12")); | |
58 | |
59 // CheckLayoutName should not accept upper-case ascii characters. | |
60 EXPECT_FALSE(XKeyboard::CheckLayoutNameForTesting("US")); | |
61 | |
62 // CheckLayoutName should accept lower-case ascii characters. | |
63 for (int c = 'a'; c <= 'z'; ++c) { | |
64 EXPECT_TRUE(XKeyboard::CheckLayoutNameForTesting(std::string(3, c))); | |
65 } | |
66 | |
67 // CheckLayoutName should accept numbers. | |
68 for (int c = '0'; c <= '9'; ++c) { | |
69 EXPECT_TRUE(XKeyboard::CheckLayoutNameForTesting(std::string(3, c))); | |
70 } | |
71 | |
72 // CheckLayoutName should accept a layout with a variant name. | |
73 EXPECT_TRUE(XKeyboard::CheckLayoutNameForTesting("us(dvorak)")); | |
74 EXPECT_TRUE(XKeyboard::CheckLayoutNameForTesting("jp")); | |
75 } | |
76 | |
77 TEST_F(XKeyboardTest, TestSetCapsLockEnabled) { | |
78 if (!DisplayAvailable()) { | |
79 // Do not fail the test to allow developers to run unit_tests without an X | |
80 // server (e.g. via ssh). Note that both try bots and waterfall always have | |
81 // an X server for running browser_tests. | |
82 DVLOG(1) << "X server is not available. Skip the test."; | |
83 return; | |
84 } | |
85 const bool initial_lock_state = xkey_->CapsLockIsEnabled(); | |
86 xkey_->SetCapsLockEnabled(true); | |
87 EXPECT_TRUE(xkey_->CapsLockIsEnabled()); | |
88 xkey_->SetCapsLockEnabled(false); | |
89 EXPECT_FALSE(xkey_->CapsLockIsEnabled()); | |
90 xkey_->SetCapsLockEnabled(true); | |
91 EXPECT_TRUE(xkey_->CapsLockIsEnabled()); | |
92 xkey_->SetCapsLockEnabled(false); | |
93 EXPECT_FALSE(xkey_->CapsLockIsEnabled()); | |
94 xkey_->SetCapsLockEnabled(initial_lock_state); | |
95 } | |
96 | |
97 TEST_F(XKeyboardTest, TestSetNumLockEnabled) { | |
98 if (!DisplayAvailable()) { | |
99 DVLOG(1) << "X server is not available. Skip the test."; | |
100 return; | |
101 } | |
102 const unsigned int num_lock_mask = xkey_->GetNumLockMask(); | |
103 ASSERT_NE(0U, num_lock_mask); | |
104 | |
105 const bool initial_lock_state = xkey_->NumLockIsEnabled(); | |
106 xkey_->SetNumLockEnabled(true); | |
107 EXPECT_TRUE(xkey_->NumLockIsEnabled()); | |
108 xkey_->SetNumLockEnabled(false); | |
109 EXPECT_FALSE(xkey_->NumLockIsEnabled()); | |
110 xkey_->SetNumLockEnabled(true); | |
111 EXPECT_TRUE(xkey_->NumLockIsEnabled()); | |
112 xkey_->SetNumLockEnabled(false); | |
113 EXPECT_FALSE(xkey_->NumLockIsEnabled()); | |
114 xkey_->SetNumLockEnabled(initial_lock_state); | |
115 } | |
116 | |
117 TEST_F(XKeyboardTest, TestSetCapsLockAndNumLockAtTheSameTime) { | |
118 if (!DisplayAvailable()) { | |
119 DVLOG(1) << "X server is not available. Skip the test."; | |
120 return; | |
121 } | |
122 const unsigned int num_lock_mask = xkey_->GetNumLockMask(); | |
123 ASSERT_NE(0U, num_lock_mask); | |
124 | |
125 const bool initial_caps_lock_state = xkey_->CapsLockIsEnabled(); | |
126 const bool initial_num_lock_state = xkey_->NumLockIsEnabled(); | |
127 | |
128 // Flip both. | |
129 xkey_->SetLockedModifiers( | |
130 initial_caps_lock_state ? kDisableLock : kEnableLock, | |
131 initial_num_lock_state ? kDisableLock : kEnableLock); | |
132 EXPECT_EQ(!initial_caps_lock_state, xkey_->CapsLockIsEnabled()); | |
133 EXPECT_EQ(!initial_num_lock_state, xkey_->NumLockIsEnabled()); | |
134 | |
135 // Flip Caps Lock. | |
136 xkey_->SetLockedModifiers( | |
137 initial_caps_lock_state ? kEnableLock : kDisableLock, | |
138 kDontChange); | |
139 // Use GetLockedModifiers() for verifying the result. | |
140 bool c, n; | |
141 xkey_->GetLockedModifiers(&c, &n); | |
142 EXPECT_EQ(initial_caps_lock_state, c); | |
143 EXPECT_EQ(!initial_num_lock_state, n); | |
144 | |
145 // Flip both. | |
146 xkey_->SetLockedModifiers( | |
147 initial_caps_lock_state ? kDisableLock : kEnableLock, | |
148 initial_num_lock_state ? kEnableLock : kDisableLock); | |
149 EXPECT_EQ(!initial_caps_lock_state, xkey_->CapsLockIsEnabled()); | |
150 EXPECT_EQ(initial_num_lock_state, xkey_->NumLockIsEnabled()); | |
151 | |
152 // Flip Num Lock. | |
153 xkey_->SetLockedModifiers( | |
154 kDontChange, | |
155 initial_num_lock_state ? kDisableLock : kEnableLock); | |
156 xkey_->GetLockedModifiers(&c, &n); | |
157 EXPECT_EQ(!initial_caps_lock_state, c); | |
158 EXPECT_EQ(!initial_num_lock_state, n); | |
159 | |
160 // Flip both to restore the initial state. | |
161 xkey_->SetLockedModifiers( | |
162 initial_caps_lock_state ? kEnableLock : kDisableLock, | |
163 initial_num_lock_state ? kEnableLock : kDisableLock); | |
164 EXPECT_EQ(initial_caps_lock_state, xkey_->CapsLockIsEnabled()); | |
165 EXPECT_EQ(initial_num_lock_state, xkey_->NumLockIsEnabled()); | |
166 | |
167 // No-op SetLockedModifiers call. | |
168 xkey_->SetLockedModifiers(kDontChange, kDontChange); | |
169 EXPECT_EQ(initial_caps_lock_state, xkey_->CapsLockIsEnabled()); | |
170 EXPECT_EQ(initial_num_lock_state, xkey_->NumLockIsEnabled()); | |
171 | |
172 // No-op GetLockedModifiers call. Confirm it does not crash. | |
173 xkey_->GetLockedModifiers(NULL, NULL); | |
174 } | |
175 | |
176 TEST_F(XKeyboardTest, TestSetAutoRepeatEnabled) { | |
177 if (!DisplayAvailable()) { | |
178 DVLOG(1) << "X server is not available. Skip the test."; | |
179 return; | |
180 } | |
181 const bool state = XKeyboard::GetAutoRepeatEnabledForTesting(); | |
182 XKeyboard::SetAutoRepeatEnabled(!state); | |
183 EXPECT_EQ(!state, XKeyboard::GetAutoRepeatEnabledForTesting()); | |
184 // Restore the initial state. | |
185 XKeyboard::SetAutoRepeatEnabled(state); | |
186 EXPECT_EQ(state, XKeyboard::GetAutoRepeatEnabledForTesting()); | |
187 } | |
188 | |
189 TEST_F(XKeyboardTest, TestSetAutoRepeatRate) { | |
190 if (!DisplayAvailable()) { | |
191 DVLOG(1) << "X server is not available. Skip the test."; | |
192 return; | |
193 } | |
194 AutoRepeatRate rate; | |
195 EXPECT_TRUE(XKeyboard::GetAutoRepeatRateForTesting(&rate)); | |
196 | |
197 AutoRepeatRate tmp(rate); | |
198 ++tmp.initial_delay_in_ms; | |
199 ++tmp.repeat_interval_in_ms; | |
200 EXPECT_TRUE(XKeyboard::SetAutoRepeatRate(tmp)); | |
201 EXPECT_TRUE(XKeyboard::GetAutoRepeatRateForTesting(&tmp)); | |
202 EXPECT_EQ(rate.initial_delay_in_ms + 1, tmp.initial_delay_in_ms); | |
203 EXPECT_EQ(rate.repeat_interval_in_ms + 1, tmp.repeat_interval_in_ms); | |
204 | |
205 // Restore the initial state. | |
206 EXPECT_TRUE(XKeyboard::SetAutoRepeatRate(rate)); | |
207 EXPECT_TRUE(XKeyboard::GetAutoRepeatRateForTesting(&tmp)); | |
208 EXPECT_EQ(rate.initial_delay_in_ms, tmp.initial_delay_in_ms); | |
209 EXPECT_EQ(rate.repeat_interval_in_ms, tmp.repeat_interval_in_ms); | |
210 } | |
211 | |
212 } // namespace input_method | |
213 } // namespace chromeos | |
OLD | NEW |