1 // Copyright 2014 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/ime_keyboard.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"
15 #include "ui/gfx/x/x11_types.h"
20 namespace input_method
{
24 class ImeKeyboardTest
: public testing::Test
{
29 virtual void SetUp() {
30 xkey_
.reset(ImeKeyboard::Create());
33 virtual void TearDown() {
37 scoped_ptr
<ImeKeyboard
> xkey_
;
39 base::MessageLoopForUI message_loop_
;
42 // Returns true if X display is available.
43 bool DisplayAvailable() {
44 return gfx::GetXDisplay() != NULL
;
49 // Tests CheckLayoutName() function.
50 TEST_F(ImeKeyboardTest
, TestCheckLayoutName
) {
51 // CheckLayoutName should not accept non-alphanumeric characters
53 EXPECT_FALSE(ImeKeyboard::CheckLayoutNameForTesting("us!"));
54 EXPECT_FALSE(ImeKeyboard::CheckLayoutNameForTesting("us; /bin/sh"));
55 EXPECT_TRUE(ImeKeyboard::CheckLayoutNameForTesting("ab-c_12"));
57 // CheckLayoutName should not accept upper-case ascii characters.
58 EXPECT_FALSE(ImeKeyboard::CheckLayoutNameForTesting("US"));
60 // CheckLayoutName should accept lower-case ascii characters.
61 for (int c
= 'a'; c
<= 'z'; ++c
) {
62 EXPECT_TRUE(ImeKeyboard::CheckLayoutNameForTesting(std::string(3, c
)));
65 // CheckLayoutName should accept numbers.
66 for (int c
= '0'; c
<= '9'; ++c
) {
67 EXPECT_TRUE(ImeKeyboard::CheckLayoutNameForTesting(std::string(3, c
)));
70 // CheckLayoutName should accept a layout with a variant name.
71 EXPECT_TRUE(ImeKeyboard::CheckLayoutNameForTesting("us(dvorak)"));
72 EXPECT_TRUE(ImeKeyboard::CheckLayoutNameForTesting("jp"));
75 TEST_F(ImeKeyboardTest
, TestSetCapsLockEnabled
) {
76 if (!DisplayAvailable()) {
77 // Do not fail the test to allow developers to run unit_tests without an X
78 // server (e.g. via ssh). Note that both try bots and waterfall always have
79 // an X server for running browser_tests.
80 DVLOG(1) << "X server is not available. Skip the test.";
83 const bool initial_lock_state
= xkey_
->CapsLockIsEnabled();
84 xkey_
->SetCapsLockEnabled(true);
85 EXPECT_TRUE(xkey_
->CapsLockIsEnabled());
86 xkey_
->SetCapsLockEnabled(false);
87 EXPECT_FALSE(xkey_
->CapsLockIsEnabled());
88 xkey_
->SetCapsLockEnabled(true);
89 EXPECT_TRUE(xkey_
->CapsLockIsEnabled());
90 xkey_
->SetCapsLockEnabled(false);
91 EXPECT_FALSE(xkey_
->CapsLockIsEnabled());
92 xkey_
->SetCapsLockEnabled(initial_lock_state
);
95 TEST_F(ImeKeyboardTest
, TestSetAutoRepeatEnabled
) {
96 if (!DisplayAvailable()) {
97 DVLOG(1) << "X server is not available. Skip the test.";
100 const bool state
= ImeKeyboard::GetAutoRepeatEnabledForTesting();
101 xkey_
->SetAutoRepeatEnabled(!state
);
102 EXPECT_EQ(!state
, ImeKeyboard::GetAutoRepeatEnabledForTesting());
103 // Restore the initial state.
104 xkey_
->SetAutoRepeatEnabled(state
);
105 EXPECT_EQ(state
, ImeKeyboard::GetAutoRepeatEnabledForTesting());
108 TEST_F(ImeKeyboardTest
, TestSetAutoRepeatRate
) {
109 if (!DisplayAvailable()) {
110 DVLOG(1) << "X server is not available. Skip the test.";
114 EXPECT_TRUE(ImeKeyboard::GetAutoRepeatRateForTesting(&rate
));
116 AutoRepeatRate
tmp(rate
);
117 ++tmp
.initial_delay_in_ms
;
118 ++tmp
.repeat_interval_in_ms
;
119 EXPECT_TRUE(xkey_
->SetAutoRepeatRate(tmp
));
120 EXPECT_TRUE(ImeKeyboard::GetAutoRepeatRateForTesting(&tmp
));
121 EXPECT_EQ(rate
.initial_delay_in_ms
+ 1, tmp
.initial_delay_in_ms
);
122 EXPECT_EQ(rate
.repeat_interval_in_ms
+ 1, tmp
.repeat_interval_in_ms
);
124 // Restore the initial state.
125 EXPECT_TRUE(xkey_
->SetAutoRepeatRate(rate
));
126 EXPECT_TRUE(ImeKeyboard::GetAutoRepeatRateForTesting(&tmp
));
127 EXPECT_EQ(rate
.initial_delay_in_ms
, tmp
.initial_delay_in_ms
);
128 EXPECT_EQ(rate
.repeat_interval_in_ms
, tmp
.repeat_interval_in_ms
);
131 } // namespace input_method
132 } // namespace chromeos