OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chromeos/ime/xkeyboard.h" | 5 #include "chromeos/ime/ime_keyboard.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <set> | 8 #include <set> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 | 15 |
16 #include <X11/Xlib.h> | 16 #include <X11/Xlib.h> |
17 | 17 |
18 namespace chromeos { | 18 namespace chromeos { |
19 namespace input_method { | 19 namespace input_method { |
20 | 20 |
21 namespace { | 21 namespace { |
22 | 22 |
23 class XKeyboardTest : public testing::Test { | 23 class ImeKeyboardTest : public testing::Test { |
24 public: | 24 public: |
25 XKeyboardTest() { | 25 ImeKeyboardTest() { |
26 } | 26 } |
27 | 27 |
28 virtual void SetUp() { | 28 virtual void SetUp() { |
29 xkey_.reset(XKeyboard::Create()); | 29 xkey_.reset(ImeKeyboard::Create()); |
30 } | 30 } |
31 | 31 |
32 virtual void TearDown() { | 32 virtual void TearDown() { |
33 xkey_.reset(); | 33 xkey_.reset(); |
34 } | 34 } |
35 | 35 |
36 scoped_ptr<XKeyboard> xkey_; | 36 scoped_ptr<ImeKeyboard> xkey_; |
37 | 37 |
38 base::MessageLoopForUI message_loop_; | 38 base::MessageLoopForUI message_loop_; |
39 }; | 39 }; |
40 | 40 |
41 // Returns true if X display is available. | 41 // Returns true if X display is available. |
42 bool DisplayAvailable() { | 42 bool DisplayAvailable() { |
43 return (base::MessagePumpForUI::GetDefaultXDisplay() != NULL); | 43 return (base::MessagePumpForUI::GetDefaultXDisplay() != NULL); |
44 } | 44 } |
45 | 45 |
46 } // namespace | 46 } // namespace |
47 | 47 |
48 // Tests CheckLayoutName() function. | 48 // Tests CheckLayoutName() function. |
49 TEST_F(XKeyboardTest, TestCheckLayoutName) { | 49 TEST_F(ImeKeyboardTest, TestCheckLayoutName) { |
50 // CheckLayoutName should not accept non-alphanumeric characters | 50 // CheckLayoutName should not accept non-alphanumeric characters |
51 // except "()-_". | 51 // except "()-_". |
52 EXPECT_FALSE(XKeyboard::CheckLayoutNameForTesting("us!")); | 52 EXPECT_FALSE(ImeKeyboard::CheckLayoutNameForTesting("us!")); |
53 EXPECT_FALSE(XKeyboard::CheckLayoutNameForTesting("us; /bin/sh")); | 53 EXPECT_FALSE(ImeKeyboard::CheckLayoutNameForTesting("us; /bin/sh")); |
54 EXPECT_TRUE(XKeyboard::CheckLayoutNameForTesting("ab-c_12")); | 54 EXPECT_TRUE(ImeKeyboard::CheckLayoutNameForTesting("ab-c_12")); |
55 | 55 |
56 // CheckLayoutName should not accept upper-case ascii characters. | 56 // CheckLayoutName should not accept upper-case ascii characters. |
57 EXPECT_FALSE(XKeyboard::CheckLayoutNameForTesting("US")); | 57 EXPECT_FALSE(ImeKeyboard::CheckLayoutNameForTesting("US")); |
58 | 58 |
59 // CheckLayoutName should accept lower-case ascii characters. | 59 // CheckLayoutName should accept lower-case ascii characters. |
60 for (int c = 'a'; c <= 'z'; ++c) { | 60 for (int c = 'a'; c <= 'z'; ++c) { |
61 EXPECT_TRUE(XKeyboard::CheckLayoutNameForTesting(std::string(3, c))); | 61 EXPECT_TRUE(ImeKeyboard::CheckLayoutNameForTesting(std::string(3, c))); |
62 } | 62 } |
63 | 63 |
64 // CheckLayoutName should accept numbers. | 64 // CheckLayoutName should accept numbers. |
65 for (int c = '0'; c <= '9'; ++c) { | 65 for (int c = '0'; c <= '9'; ++c) { |
66 EXPECT_TRUE(XKeyboard::CheckLayoutNameForTesting(std::string(3, c))); | 66 EXPECT_TRUE(ImeKeyboard::CheckLayoutNameForTesting(std::string(3, c))); |
67 } | 67 } |
68 | 68 |
69 // CheckLayoutName should accept a layout with a variant name. | 69 // CheckLayoutName should accept a layout with a variant name. |
70 EXPECT_TRUE(XKeyboard::CheckLayoutNameForTesting("us(dvorak)")); | 70 EXPECT_TRUE(ImeKeyboard::CheckLayoutNameForTesting("us(dvorak)")); |
71 EXPECT_TRUE(XKeyboard::CheckLayoutNameForTesting("jp")); | 71 EXPECT_TRUE(ImeKeyboard::CheckLayoutNameForTesting("jp")); |
72 } | 72 } |
73 | 73 |
74 TEST_F(XKeyboardTest, TestSetCapsLockEnabled) { | 74 TEST_F(ImeKeyboardTest, TestSetCapsLockEnabled) { |
75 if (!DisplayAvailable()) { | 75 if (!DisplayAvailable()) { |
76 // Do not fail the test to allow developers to run unit_tests without an X | 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 | 77 // server (e.g. via ssh). Note that both try bots and waterfall always have |
78 // an X server for running browser_tests. | 78 // an X server for running browser_tests. |
79 DVLOG(1) << "X server is not available. Skip the test."; | 79 DVLOG(1) << "X server is not available. Skip the test."; |
80 return; | 80 return; |
81 } | 81 } |
82 const bool initial_lock_state = xkey_->CapsLockIsEnabled(); | 82 const bool initial_lock_state = xkey_->CapsLockIsEnabled(); |
83 xkey_->SetCapsLockEnabled(true); | 83 xkey_->SetCapsLockEnabled(true); |
84 EXPECT_TRUE(xkey_->CapsLockIsEnabled()); | 84 EXPECT_TRUE(xkey_->CapsLockIsEnabled()); |
85 xkey_->SetCapsLockEnabled(false); | 85 xkey_->SetCapsLockEnabled(false); |
86 EXPECT_FALSE(xkey_->CapsLockIsEnabled()); | 86 EXPECT_FALSE(xkey_->CapsLockIsEnabled()); |
87 xkey_->SetCapsLockEnabled(true); | 87 xkey_->SetCapsLockEnabled(true); |
88 EXPECT_TRUE(xkey_->CapsLockIsEnabled()); | 88 EXPECT_TRUE(xkey_->CapsLockIsEnabled()); |
89 xkey_->SetCapsLockEnabled(false); | 89 xkey_->SetCapsLockEnabled(false); |
90 EXPECT_FALSE(xkey_->CapsLockIsEnabled()); | 90 EXPECT_FALSE(xkey_->CapsLockIsEnabled()); |
91 xkey_->SetCapsLockEnabled(initial_lock_state); | 91 xkey_->SetCapsLockEnabled(initial_lock_state); |
92 } | 92 } |
93 | 93 |
94 TEST_F(XKeyboardTest, TestSetAutoRepeatEnabled) { | 94 TEST_F(ImeKeyboardTest, TestSetAutoRepeatEnabled) { |
95 if (!DisplayAvailable()) { | 95 if (!DisplayAvailable()) { |
96 DVLOG(1) << "X server is not available. Skip the test."; | 96 DVLOG(1) << "X server is not available. Skip the test."; |
97 return; | 97 return; |
98 } | 98 } |
99 const bool state = XKeyboard::GetAutoRepeatEnabledForTesting(); | 99 const bool state = ImeKeyboard::GetAutoRepeatEnabledForTesting(); |
100 xkey_->SetAutoRepeatEnabled(!state); | 100 xkey_->SetAutoRepeatEnabled(!state); |
101 EXPECT_EQ(!state, XKeyboard::GetAutoRepeatEnabledForTesting()); | 101 EXPECT_EQ(!state, ImeKeyboard::GetAutoRepeatEnabledForTesting()); |
102 // Restore the initial state. | 102 // Restore the initial state. |
103 xkey_->SetAutoRepeatEnabled(state); | 103 xkey_->SetAutoRepeatEnabled(state); |
104 EXPECT_EQ(state, XKeyboard::GetAutoRepeatEnabledForTesting()); | 104 EXPECT_EQ(state, ImeKeyboard::GetAutoRepeatEnabledForTesting()); |
105 } | 105 } |
106 | 106 |
107 TEST_F(XKeyboardTest, TestSetAutoRepeatRate) { | 107 TEST_F(ImeKeyboardTest, TestSetAutoRepeatRate) { |
108 if (!DisplayAvailable()) { | 108 if (!DisplayAvailable()) { |
109 DVLOG(1) << "X server is not available. Skip the test."; | 109 DVLOG(1) << "X server is not available. Skip the test."; |
110 return; | 110 return; |
111 } | 111 } |
112 AutoRepeatRate rate; | 112 AutoRepeatRate rate; |
113 EXPECT_TRUE(XKeyboard::GetAutoRepeatRateForTesting(&rate)); | 113 EXPECT_TRUE(ImeKeyboard::GetAutoRepeatRateForTesting(&rate)); |
114 | 114 |
115 AutoRepeatRate tmp(rate); | 115 AutoRepeatRate tmp(rate); |
116 ++tmp.initial_delay_in_ms; | 116 ++tmp.initial_delay_in_ms; |
117 ++tmp.repeat_interval_in_ms; | 117 ++tmp.repeat_interval_in_ms; |
118 EXPECT_TRUE(xkey_->SetAutoRepeatRate(tmp)); | 118 EXPECT_TRUE(xkey_->SetAutoRepeatRate(tmp)); |
119 EXPECT_TRUE(XKeyboard::GetAutoRepeatRateForTesting(&tmp)); | 119 EXPECT_TRUE(ImeKeyboard::GetAutoRepeatRateForTesting(&tmp)); |
120 EXPECT_EQ(rate.initial_delay_in_ms + 1, tmp.initial_delay_in_ms); | 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); | 121 EXPECT_EQ(rate.repeat_interval_in_ms + 1, tmp.repeat_interval_in_ms); |
122 | 122 |
123 // Restore the initial state. | 123 // Restore the initial state. |
124 EXPECT_TRUE(xkey_->SetAutoRepeatRate(rate)); | 124 EXPECT_TRUE(xkey_->SetAutoRepeatRate(rate)); |
125 EXPECT_TRUE(XKeyboard::GetAutoRepeatRateForTesting(&tmp)); | 125 EXPECT_TRUE(ImeKeyboard::GetAutoRepeatRateForTesting(&tmp)); |
126 EXPECT_EQ(rate.initial_delay_in_ms, tmp.initial_delay_in_ms); | 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); | 127 EXPECT_EQ(rate.repeat_interval_in_ms, tmp.repeat_interval_in_ms); |
128 } | 128 } |
129 | 129 |
130 } // namespace input_method | 130 } // namespace input_method |
131 } // namespace chromeos | 131 } // namespace chromeos |
OLD | NEW |