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
3 // found in the LICENSE file.
5 #include "chromeos/ime/xkeyboard.h"
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop/message_loop.h"
14 #include "testing/gtest/include/gtest/gtest.h"
19 namespace input_method
{
23 class XKeyboardTest
: public testing::Test
{
28 virtual void SetUp() {
29 xkey_
.reset(XKeyboard::Create());
32 virtual void TearDown() {
36 scoped_ptr
<XKeyboard
> xkey_
;
38 base::MessageLoopForUI message_loop_
;
41 // Returns true if X display is available.
42 bool DisplayAvailable() {
43 return (base::MessagePumpForUI::GetDefaultXDisplay() != NULL
);
48 // Tests CheckLayoutName() function.
49 TEST_F(XKeyboardTest
, TestCheckLayoutName
) {
50 // CheckLayoutName should not accept non-alphanumeric characters
52 EXPECT_FALSE(XKeyboard::CheckLayoutNameForTesting("us!"));
53 EXPECT_FALSE(XKeyboard::CheckLayoutNameForTesting("us; /bin/sh"));
54 EXPECT_TRUE(XKeyboard::CheckLayoutNameForTesting("ab-c_12"));
56 // CheckLayoutName should not accept upper-case ascii characters.
57 EXPECT_FALSE(XKeyboard::CheckLayoutNameForTesting("US"));
59 // CheckLayoutName should accept lower-case ascii characters.
60 for (int c
= 'a'; c
<= 'z'; ++c
) {
61 EXPECT_TRUE(XKeyboard::CheckLayoutNameForTesting(std::string(3, c
)));
64 // CheckLayoutName should accept numbers.
65 for (int c
= '0'; c
<= '9'; ++c
) {
66 EXPECT_TRUE(XKeyboard::CheckLayoutNameForTesting(std::string(3, c
)));
69 // CheckLayoutName should accept a layout with a variant name.
70 EXPECT_TRUE(XKeyboard::CheckLayoutNameForTesting("us(dvorak)"));
71 EXPECT_TRUE(XKeyboard::CheckLayoutNameForTesting("jp"));
74 TEST_F(XKeyboardTest
, TestSetCapsLockEnabled
) {
75 if (!DisplayAvailable()) {
76 // 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
78 // an X server for running browser_tests.
79 DVLOG(1) << "X server is not available. Skip the test.";
82 const bool initial_lock_state
= xkey_
->CapsLockIsEnabled();
83 xkey_
->SetCapsLockEnabled(true);
84 EXPECT_TRUE(xkey_
->CapsLockIsEnabled());
85 xkey_
->SetCapsLockEnabled(false);
86 EXPECT_FALSE(xkey_
->CapsLockIsEnabled());
87 xkey_
->SetCapsLockEnabled(true);
88 EXPECT_TRUE(xkey_
->CapsLockIsEnabled());
89 xkey_
->SetCapsLockEnabled(false);
90 EXPECT_FALSE(xkey_
->CapsLockIsEnabled());
91 xkey_
->SetCapsLockEnabled(initial_lock_state
);
94 TEST_F(XKeyboardTest
, TestSetAutoRepeatEnabled
) {
95 if (!DisplayAvailable()) {
96 DVLOG(1) << "X server is not available. Skip the test.";
99 const bool state
= XKeyboard::GetAutoRepeatEnabledForTesting();
100 xkey_
->SetAutoRepeatEnabled(!state
);
101 EXPECT_EQ(!state
, XKeyboard::GetAutoRepeatEnabledForTesting());
102 // Restore the initial state.
103 xkey_
->SetAutoRepeatEnabled(state
);
104 EXPECT_EQ(state
, XKeyboard::GetAutoRepeatEnabledForTesting());
107 TEST_F(XKeyboardTest
, TestSetAutoRepeatRate
) {
108 if (!DisplayAvailable()) {
109 DVLOG(1) << "X server is not available. Skip the test.";
113 EXPECT_TRUE(XKeyboard::GetAutoRepeatRateForTesting(&rate
));
115 AutoRepeatRate
tmp(rate
);
116 ++tmp
.initial_delay_in_ms
;
117 ++tmp
.repeat_interval_in_ms
;
118 EXPECT_TRUE(xkey_
->SetAutoRepeatRate(tmp
));
119 EXPECT_TRUE(XKeyboard::GetAutoRepeatRateForTesting(&tmp
));
120 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 // Restore the initial state.
124 EXPECT_TRUE(xkey_
->SetAutoRepeatRate(rate
));
125 EXPECT_TRUE(XKeyboard::GetAutoRepeatRateForTesting(&tmp
));
126 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
);
130 } // namespace input_method
131 } // namespace chromeos