Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / base / ime / chromeos / ime_keyboard_x11_unittest.cc
blobd746b639ae66eb42ac486b012ebd9a9e1c961fc1
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"
7 #include <algorithm>
8 #include <set>
9 #include <string>
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"
17 #include <X11/Xlib.h>
19 namespace chromeos {
20 namespace input_method {
22 namespace {
24 class ImeKeyboardTest : public testing::Test,
25 public ImeKeyboard::Observer {
26 public:
27 ImeKeyboardTest() {
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);
38 xkey_.reset();
41 void OnCapsLockChanged(bool enabled) override {
42 caps_changed_ = true;
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_;
52 bool caps_changed_;
55 // Returns true if X display is available.
56 bool DisplayAvailable() {
57 return gfx::GetXDisplay() != NULL;
60 } // namespace
62 // Tests CheckLayoutName() function.
63 TEST_F(ImeKeyboardTest, TestCheckLayoutName) {
64 // CheckLayoutName should not accept non-alphanumeric characters
65 // except "()-_".
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.";
94 return;
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.";
122 return;
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.";
135 return;
137 AutoRepeatRate rate;
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