| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "chromeos/ime/xkeyboard.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <set> | |
| 9 #include <string> | |
| 10 | |
| 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 | |
| 16 #include <X11/Xlib.h> | |
| 17 | |
| 18 namespace chromeos { | |
| 19 namespace input_method { | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 class XKeyboardTest : public testing::Test { | |
| 24 public: | |
| 25 XKeyboardTest() { | |
| 26 } | |
| 27 | |
| 28 virtual void SetUp() { | |
| 29 xkey_.reset(XKeyboard::Create()); | |
| 30 } | |
| 31 | |
| 32 virtual void TearDown() { | |
| 33 xkey_.reset(); | |
| 34 } | |
| 35 | |
| 36 scoped_ptr<XKeyboard> xkey_; | |
| 37 | |
| 38 base::MessageLoopForUI message_loop_; | |
| 39 }; | |
| 40 | |
| 41 // Returns true if X display is available. | |
| 42 bool DisplayAvailable() { | |
| 43 return (base::MessagePumpForUI::GetDefaultXDisplay() != NULL); | |
| 44 } | |
| 45 | |
| 46 } // namespace | |
| 47 | |
| 48 // Tests CheckLayoutName() function. | |
| 49 TEST_F(XKeyboardTest, TestCheckLayoutName) { | |
| 50 // CheckLayoutName should not accept non-alphanumeric characters | |
| 51 // except "()-_". | |
| 52 EXPECT_FALSE(XKeyboard::CheckLayoutNameForTesting("us!")); | |
| 53 EXPECT_FALSE(XKeyboard::CheckLayoutNameForTesting("us; /bin/sh")); | |
| 54 EXPECT_TRUE(XKeyboard::CheckLayoutNameForTesting("ab-c_12")); | |
| 55 | |
| 56 // CheckLayoutName should not accept upper-case ascii characters. | |
| 57 EXPECT_FALSE(XKeyboard::CheckLayoutNameForTesting("US")); | |
| 58 | |
| 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))); | |
| 62 } | |
| 63 | |
| 64 // CheckLayoutName should accept numbers. | |
| 65 for (int c = '0'; c <= '9'; ++c) { | |
| 66 EXPECT_TRUE(XKeyboard::CheckLayoutNameForTesting(std::string(3, c))); | |
| 67 } | |
| 68 | |
| 69 // CheckLayoutName should accept a layout with a variant name. | |
| 70 EXPECT_TRUE(XKeyboard::CheckLayoutNameForTesting("us(dvorak)")); | |
| 71 EXPECT_TRUE(XKeyboard::CheckLayoutNameForTesting("jp")); | |
| 72 } | |
| 73 | |
| 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."; | |
| 80 return; | |
| 81 } | |
| 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); | |
| 92 } | |
| 93 | |
| 94 TEST_F(XKeyboardTest, TestSetAutoRepeatEnabled) { | |
| 95 if (!DisplayAvailable()) { | |
| 96 DVLOG(1) << "X server is not available. Skip the test."; | |
| 97 return; | |
| 98 } | |
| 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()); | |
| 105 } | |
| 106 | |
| 107 TEST_F(XKeyboardTest, TestSetAutoRepeatRate) { | |
| 108 if (!DisplayAvailable()) { | |
| 109 DVLOG(1) << "X server is not available. Skip the test."; | |
| 110 return; | |
| 111 } | |
| 112 AutoRepeatRate rate; | |
| 113 EXPECT_TRUE(XKeyboard::GetAutoRepeatRateForTesting(&rate)); | |
| 114 | |
| 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); | |
| 122 | |
| 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); | |
| 128 } | |
| 129 | |
| 130 } // namespace input_method | |
| 131 } // namespace chromeos | |
| OLD | NEW |