Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chromeos / ime / ime_keyboard_x11_unittest.cc
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.
4
5 #include "chromeos/ime/ime_keyboard.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 #include "ui/gfx/x/x11_types.h"
16
17 #include <X11/Xlib.h>
18
19 namespace chromeos {
20 namespace input_method {
21
22 namespace {
23
24 class ImeKeyboardTest : public testing::Test {
25  public:
26   ImeKeyboardTest() {
27   }
28
29   virtual void SetUp() {
30     xkey_.reset(ImeKeyboard::Create());
31   }
32
33   virtual void TearDown() {
34     xkey_.reset();
35   }
36
37   scoped_ptr<ImeKeyboard> xkey_;
38
39   base::MessageLoopForUI message_loop_;
40 };
41
42 // Returns true if X display is available.
43 bool DisplayAvailable() {
44   return gfx::GetXDisplay() != NULL;
45 }
46
47 }  // namespace
48
49 // Tests CheckLayoutName() function.
50 TEST_F(ImeKeyboardTest, TestCheckLayoutName) {
51   // CheckLayoutName should not accept non-alphanumeric characters
52   // except "()-_".
53   EXPECT_FALSE(ImeKeyboard::CheckLayoutNameForTesting("us!"));
54   EXPECT_FALSE(ImeKeyboard::CheckLayoutNameForTesting("us; /bin/sh"));
55   EXPECT_TRUE(ImeKeyboard::CheckLayoutNameForTesting("ab-c_12"));
56
57   // CheckLayoutName should not accept upper-case ascii characters.
58   EXPECT_FALSE(ImeKeyboard::CheckLayoutNameForTesting("US"));
59
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)));
63   }
64
65   // CheckLayoutName should accept numbers.
66   for (int c = '0'; c <= '9'; ++c) {
67     EXPECT_TRUE(ImeKeyboard::CheckLayoutNameForTesting(std::string(3, c)));
68   }
69
70   // CheckLayoutName should accept a layout with a variant name.
71   EXPECT_TRUE(ImeKeyboard::CheckLayoutNameForTesting("us(dvorak)"));
72   EXPECT_TRUE(ImeKeyboard::CheckLayoutNameForTesting("jp"));
73 }
74
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.";
81     return;
82   }
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);
93 }
94
95 TEST_F(ImeKeyboardTest, TestSetAutoRepeatEnabled) {
96   if (!DisplayAvailable()) {
97     DVLOG(1) << "X server is not available. Skip the test.";
98     return;
99   }
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());
106 }
107
108 TEST_F(ImeKeyboardTest, TestSetAutoRepeatRate) {
109   if (!DisplayAvailable()) {
110     DVLOG(1) << "X server is not available. Skip the test.";
111     return;
112   }
113   AutoRepeatRate rate;
114   EXPECT_TRUE(ImeKeyboard::GetAutoRepeatRateForTesting(&rate));
115
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);
123
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);
129 }
130
131 }  // namespace input_method
132 }  // namespace chromeos