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 "ui/base/ime/chromeos/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
,
25 public ImeKeyboard::Observer
{
30 void SetUp() override
{
31 xkey_
.reset(ImeKeyboard::Create());
32 xkey_
->AddObserver(this);
33 caps_changed_
= false;
36 void TearDown() override
{
37 xkey_
->RemoveObserver(this);
41 void OnCapsLockChanged(bool enabled
) override
{
45 void VerifyCapsLockChanged(bool changed
) {
46 EXPECT_EQ(changed
, caps_changed_
);
47 caps_changed_
= false;
50 scoped_ptr
<ImeKeyboard
> xkey_
;
51 base::MessageLoopForUI message_loop_
;
55 // Returns true if X display is available.
56 bool DisplayAvailable() {
57 return gfx::GetXDisplay() != NULL
;
62 // Tests CheckLayoutName() function.
63 TEST_F(ImeKeyboardTest
, TestCheckLayoutName
) {
64 // CheckLayoutName should not accept non-alphanumeric characters
66 EXPECT_FALSE(ImeKeyboard::CheckLayoutNameForTesting("us!"));
67 EXPECT_FALSE(ImeKeyboard::CheckLayoutNameForTesting("us; /bin/sh"));
68 EXPECT_TRUE(ImeKeyboard::CheckLayoutNameForTesting("ab-c_12"));
70 // CheckLayoutName should not accept upper-case ascii characters.
71 EXPECT_FALSE(ImeKeyboard::CheckLayoutNameForTesting("US"));
73 // CheckLayoutName should accept lower-case ascii characters.
74 for (int c
= 'a'; c
<= 'z'; ++c
) {
75 EXPECT_TRUE(ImeKeyboard::CheckLayoutNameForTesting(std::string(3, c
)));
78 // CheckLayoutName should accept numbers.
79 for (int c
= '0'; c
<= '9'; ++c
) {
80 EXPECT_TRUE(ImeKeyboard::CheckLayoutNameForTesting(std::string(3, c
)));
83 // CheckLayoutName should accept a layout with a variant name.
84 EXPECT_TRUE(ImeKeyboard::CheckLayoutNameForTesting("us(dvorak)"));
85 EXPECT_TRUE(ImeKeyboard::CheckLayoutNameForTesting("jp"));
88 TEST_F(ImeKeyboardTest
, TestSetCapsLockEnabled
) {
89 if (!DisplayAvailable()) {
90 // Do not fail the test to allow developers to run unit_tests without an X
91 // server (e.g. via ssh). Note that both try bots and waterfall always have
92 // an X server for running browser_tests.
93 DVLOG(1) << "X server is not available. Skip the test.";
96 const bool initial_lock_state
= xkey_
->CapsLockIsEnabled();
97 xkey_
->SetCapsLockEnabled(true);
98 EXPECT_TRUE(xkey_
->CapsLockIsEnabled());
100 xkey_
->SetCapsLockEnabled(false);
101 EXPECT_FALSE(xkey_
->CapsLockIsEnabled());
102 VerifyCapsLockChanged(true);
104 xkey_
->SetCapsLockEnabled(true);
105 EXPECT_TRUE(xkey_
->CapsLockIsEnabled());
106 VerifyCapsLockChanged(true);
108 xkey_
->SetCapsLockEnabled(false);
109 EXPECT_FALSE(xkey_
->CapsLockIsEnabled());
110 VerifyCapsLockChanged(true);
112 xkey_
->SetCapsLockEnabled(false);
113 EXPECT_FALSE(xkey_
->CapsLockIsEnabled());
114 VerifyCapsLockChanged(false);
116 xkey_
->SetCapsLockEnabled(initial_lock_state
);
119 TEST_F(ImeKeyboardTest
, TestSetAutoRepeatEnabled
) {
120 if (!DisplayAvailable()) {
121 DVLOG(1) << "X server is not available. Skip the test.";
124 const bool state
= xkey_
->GetAutoRepeatEnabled();
125 xkey_
->SetAutoRepeatEnabled(!state
);
126 EXPECT_EQ(!state
, xkey_
->GetAutoRepeatEnabled());
127 // Restore the initial state.
128 xkey_
->SetAutoRepeatEnabled(state
);
129 EXPECT_EQ(state
, xkey_
->GetAutoRepeatEnabled());
132 TEST_F(ImeKeyboardTest
, TestSetAutoRepeatRate
) {
133 if (!DisplayAvailable()) {
134 DVLOG(1) << "X server is not available. Skip the test.";
138 EXPECT_TRUE(ImeKeyboard::GetAutoRepeatRateForTesting(&rate
));
140 AutoRepeatRate
tmp(rate
);
141 ++tmp
.initial_delay_in_ms
;
142 ++tmp
.repeat_interval_in_ms
;
143 EXPECT_TRUE(xkey_
->SetAutoRepeatRate(tmp
));
144 EXPECT_TRUE(ImeKeyboard::GetAutoRepeatRateForTesting(&tmp
));
145 EXPECT_EQ(rate
.initial_delay_in_ms
+ 1, tmp
.initial_delay_in_ms
);
146 EXPECT_EQ(rate
.repeat_interval_in_ms
+ 1, tmp
.repeat_interval_in_ms
);
148 // Restore the initial state.
149 EXPECT_TRUE(xkey_
->SetAutoRepeatRate(rate
));
150 EXPECT_TRUE(ImeKeyboard::GetAutoRepeatRateForTesting(&tmp
));
151 EXPECT_EQ(rate
.initial_delay_in_ms
, tmp
.initial_delay_in_ms
);
152 EXPECT_EQ(rate
.repeat_interval_in_ms
, tmp
.repeat_interval_in_ms
);
155 } // namespace input_method
156 } // namespace chromeos