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.
5 #include "chrome/browser/chromeos/input_method/xkeyboard.h"
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop.h"
14 #include "chrome/browser/chromeos/input_method/input_method_util.h"
15 #include "chrome/browser/chromeos/input_method/input_method_whitelist.h"
16 #include "content/public/test/test_browser_thread.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "ui/base/x/x11_util.h"
22 using content::BrowserThread
;
25 namespace input_method
{
29 class XKeyboardTest
: public testing::Test
{
32 : util_(whitelist_
.GetSupportedInputMethods()),
33 ui_thread_(BrowserThread::UI
, &message_loop_
) {
36 virtual void SetUp() {
37 xkey_
.reset(XKeyboard::Create(util_
));
40 virtual void TearDown() {
44 InputMethodWhitelist whitelist_
;
45 InputMethodUtil util_
;
46 scoped_ptr
<XKeyboard
> xkey_
;
48 MessageLoopForUI message_loop_
;
49 content::TestBrowserThread ui_thread_
;
52 // Returns true if X display is available.
53 bool DisplayAvailable() {
54 return ui::GetXDisplay() ? true : false;
59 // Tests CheckLayoutName() function.
60 TEST_F(XKeyboardTest
, TestCheckLayoutName
) {
61 // CheckLayoutName should not accept non-alphanumeric characters
63 EXPECT_FALSE(XKeyboard::CheckLayoutNameForTesting("us!"));
64 EXPECT_FALSE(XKeyboard::CheckLayoutNameForTesting("us; /bin/sh"));
65 EXPECT_TRUE(XKeyboard::CheckLayoutNameForTesting("ab-c_12"));
67 // CheckLayoutName should not accept upper-case ascii characters.
68 EXPECT_FALSE(XKeyboard::CheckLayoutNameForTesting("US"));
70 // CheckLayoutName should accept lower-case ascii characters.
71 for (int c
= 'a'; c
<= 'z'; ++c
) {
72 EXPECT_TRUE(XKeyboard::CheckLayoutNameForTesting(std::string(3, c
)));
75 // CheckLayoutName should accept numbers.
76 for (int c
= '0'; c
<= '9'; ++c
) {
77 EXPECT_TRUE(XKeyboard::CheckLayoutNameForTesting(std::string(3, c
)));
80 // CheckLayoutName should accept a layout with a variant name.
81 EXPECT_TRUE(XKeyboard::CheckLayoutNameForTesting("us(dvorak)"));
82 EXPECT_TRUE(XKeyboard::CheckLayoutNameForTesting("jp"));
85 TEST_F(XKeyboardTest
, TestSetCapsLockEnabled
) {
86 if (!DisplayAvailable()) {
87 // Do not fail the test to allow developers to run unit_tests without an X
88 // server (e.g. via ssh). Note that both try bots and waterfall always have
89 // an X server for running browser_tests.
90 DVLOG(1) << "X server is not available. Skip the test.";
93 const bool initial_lock_state
= xkey_
->CapsLockIsEnabled();
94 xkey_
->SetCapsLockEnabled(true);
95 EXPECT_TRUE(xkey_
->CapsLockIsEnabled());
96 xkey_
->SetCapsLockEnabled(false);
97 EXPECT_FALSE(xkey_
->CapsLockIsEnabled());
98 xkey_
->SetCapsLockEnabled(true);
99 EXPECT_TRUE(xkey_
->CapsLockIsEnabled());
100 xkey_
->SetCapsLockEnabled(false);
101 EXPECT_FALSE(xkey_
->CapsLockIsEnabled());
102 xkey_
->SetCapsLockEnabled(initial_lock_state
);
105 TEST_F(XKeyboardTest
, TestSetNumLockEnabled
) {
106 if (!DisplayAvailable()) {
107 DVLOG(1) << "X server is not available. Skip the test.";
110 const unsigned int num_lock_mask
= xkey_
->GetNumLockMask();
111 ASSERT_NE(0U, num_lock_mask
);
113 const bool initial_lock_state
= xkey_
->NumLockIsEnabled();
114 xkey_
->SetNumLockEnabled(true);
115 EXPECT_TRUE(xkey_
->NumLockIsEnabled());
116 xkey_
->SetNumLockEnabled(false);
117 EXPECT_FALSE(xkey_
->NumLockIsEnabled());
118 xkey_
->SetNumLockEnabled(true);
119 EXPECT_TRUE(xkey_
->NumLockIsEnabled());
120 xkey_
->SetNumLockEnabled(false);
121 EXPECT_FALSE(xkey_
->NumLockIsEnabled());
122 xkey_
->SetNumLockEnabled(initial_lock_state
);
125 TEST_F(XKeyboardTest
, TestSetCapsLockAndNumLockAtTheSameTime
) {
126 if (!DisplayAvailable()) {
127 DVLOG(1) << "X server is not available. Skip the test.";
130 const unsigned int num_lock_mask
= xkey_
->GetNumLockMask();
131 ASSERT_NE(0U, num_lock_mask
);
133 const bool initial_caps_lock_state
= xkey_
->CapsLockIsEnabled();
134 const bool initial_num_lock_state
= xkey_
->NumLockIsEnabled();
137 xkey_
->SetLockedModifiers(
138 initial_caps_lock_state
? kDisableLock
: kEnableLock
,
139 initial_num_lock_state
? kDisableLock
: kEnableLock
);
140 EXPECT_EQ(!initial_caps_lock_state
, xkey_
->CapsLockIsEnabled());
141 EXPECT_EQ(!initial_num_lock_state
, xkey_
->NumLockIsEnabled());
144 xkey_
->SetLockedModifiers(
145 initial_caps_lock_state
? kEnableLock
: kDisableLock
,
147 // Use GetLockedModifiers() for verifying the result.
149 xkey_
->GetLockedModifiers(&c
, &n
);
150 EXPECT_EQ(initial_caps_lock_state
, c
);
151 EXPECT_EQ(!initial_num_lock_state
, n
);
154 xkey_
->SetLockedModifiers(
155 initial_caps_lock_state
? kDisableLock
: kEnableLock
,
156 initial_num_lock_state
? kEnableLock
: kDisableLock
);
157 EXPECT_EQ(!initial_caps_lock_state
, xkey_
->CapsLockIsEnabled());
158 EXPECT_EQ(initial_num_lock_state
, xkey_
->NumLockIsEnabled());
161 xkey_
->SetLockedModifiers(
163 initial_num_lock_state
? kDisableLock
: kEnableLock
);
164 xkey_
->GetLockedModifiers(&c
, &n
);
165 EXPECT_EQ(!initial_caps_lock_state
, c
);
166 EXPECT_EQ(!initial_num_lock_state
, n
);
168 // Flip both to restore the initial state.
169 xkey_
->SetLockedModifiers(
170 initial_caps_lock_state
? kEnableLock
: kDisableLock
,
171 initial_num_lock_state
? kEnableLock
: kDisableLock
);
172 EXPECT_EQ(initial_caps_lock_state
, xkey_
->CapsLockIsEnabled());
173 EXPECT_EQ(initial_num_lock_state
, xkey_
->NumLockIsEnabled());
175 // No-op SetLockedModifiers call.
176 xkey_
->SetLockedModifiers(kDontChange
, kDontChange
);
177 EXPECT_EQ(initial_caps_lock_state
, xkey_
->CapsLockIsEnabled());
178 EXPECT_EQ(initial_num_lock_state
, xkey_
->NumLockIsEnabled());
180 // No-op GetLockedModifiers call. Confirm it does not crash.
181 xkey_
->GetLockedModifiers(NULL
, NULL
);
184 TEST_F(XKeyboardTest
, TestSetAutoRepeatEnabled
) {
185 if (!DisplayAvailable()) {
186 DVLOG(1) << "X server is not available. Skip the test.";
189 const bool state
= XKeyboard::GetAutoRepeatEnabledForTesting();
190 XKeyboard::SetAutoRepeatEnabled(!state
);
191 EXPECT_EQ(!state
, XKeyboard::GetAutoRepeatEnabledForTesting());
192 // Restore the initial state.
193 XKeyboard::SetAutoRepeatEnabled(state
);
194 EXPECT_EQ(state
, XKeyboard::GetAutoRepeatEnabledForTesting());
197 TEST_F(XKeyboardTest
, TestSetAutoRepeatRate
) {
198 if (!DisplayAvailable()) {
199 DVLOG(1) << "X server is not available. Skip the test.";
203 EXPECT_TRUE(XKeyboard::GetAutoRepeatRateForTesting(&rate
));
205 AutoRepeatRate
tmp(rate
);
206 ++tmp
.initial_delay_in_ms
;
207 ++tmp
.repeat_interval_in_ms
;
208 EXPECT_TRUE(XKeyboard::SetAutoRepeatRate(tmp
));
209 EXPECT_TRUE(XKeyboard::GetAutoRepeatRateForTesting(&tmp
));
210 EXPECT_EQ(rate
.initial_delay_in_ms
+ 1, tmp
.initial_delay_in_ms
);
211 EXPECT_EQ(rate
.repeat_interval_in_ms
+ 1, tmp
.repeat_interval_in_ms
);
213 // Restore the initial state.
214 EXPECT_TRUE(XKeyboard::SetAutoRepeatRate(rate
));
215 EXPECT_TRUE(XKeyboard::GetAutoRepeatRateForTesting(&tmp
));
216 EXPECT_EQ(rate
.initial_delay_in_ms
, tmp
.initial_delay_in_ms
);
217 EXPECT_EQ(rate
.repeat_interval_in_ms
, tmp
.repeat_interval_in_ms
);
220 } // namespace input_method
221 } // namespace chromeos