Home | History | Annotate | Download | only in ime
      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 ImeKeyboard::Observer {
     26  public:
     27   ImeKeyboardTest() {
     28   }
     29 
     30   virtual void SetUp() {
     31     xkey_.reset(ImeKeyboard::Create());
     32     xkey_->AddObserver(this);
     33     caps_changed_ = false;
     34   }
     35 
     36   virtual void TearDown() {
     37     xkey_->RemoveObserver(this);
     38     xkey_.reset();
     39   }
     40 
     41   virtual void OnCapsLockChanged(bool enabled) OVERRIDE {
     42     caps_changed_ = true;
     43   }
     44 
     45   void VerifyCapsLockChanged(bool changed) {
     46     EXPECT_EQ(changed, caps_changed_);
     47     caps_changed_ = false;
     48   }
     49 
     50   scoped_ptr<ImeKeyboard> xkey_;
     51   base::MessageLoopForUI message_loop_;
     52   bool caps_changed_;
     53 };
     54 
     55 // Returns true if X display is available.
     56 bool DisplayAvailable() {
     57   return gfx::GetXDisplay() != NULL;
     58 }
     59 
     60 }  // namespace
     61 
     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"));
     69 
     70   // CheckLayoutName should not accept upper-case ascii characters.
     71   EXPECT_FALSE(ImeKeyboard::CheckLayoutNameForTesting("US"));
     72 
     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)));
     76   }
     77 
     78   // CheckLayoutName should accept numbers.
     79   for (int c = '0'; c <= '9'; ++c) {
     80     EXPECT_TRUE(ImeKeyboard::CheckLayoutNameForTesting(std::string(3, c)));
     81   }
     82 
     83   // CheckLayoutName should accept a layout with a variant name.
     84   EXPECT_TRUE(ImeKeyboard::CheckLayoutNameForTesting("us(dvorak)"));
     85   EXPECT_TRUE(ImeKeyboard::CheckLayoutNameForTesting("jp"));
     86 }
     87 
     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;
     95   }
     96   const bool initial_lock_state = xkey_->CapsLockIsEnabled();
     97   xkey_->SetCapsLockEnabled(true);
     98   EXPECT_TRUE(xkey_->CapsLockIsEnabled());
     99 
    100   xkey_->SetCapsLockEnabled(false);
    101   EXPECT_FALSE(xkey_->CapsLockIsEnabled());
    102   VerifyCapsLockChanged(true);
    103 
    104   xkey_->SetCapsLockEnabled(true);
    105   EXPECT_TRUE(xkey_->CapsLockIsEnabled());
    106   VerifyCapsLockChanged(true);
    107 
    108   xkey_->SetCapsLockEnabled(false);
    109   EXPECT_FALSE(xkey_->CapsLockIsEnabled());
    110   VerifyCapsLockChanged(true);
    111 
    112   xkey_->SetCapsLockEnabled(false);
    113   EXPECT_FALSE(xkey_->CapsLockIsEnabled());
    114   VerifyCapsLockChanged(false);
    115 
    116   xkey_->SetCapsLockEnabled(initial_lock_state);
    117 }
    118 
    119 TEST_F(ImeKeyboardTest, TestSetAutoRepeatEnabled) {
    120   if (!DisplayAvailable()) {
    121     DVLOG(1) << "X server is not available. Skip the test.";
    122     return;
    123   }
    124   const bool state = ImeKeyboard::GetAutoRepeatEnabledForTesting();
    125   xkey_->SetAutoRepeatEnabled(!state);
    126   EXPECT_EQ(!state, ImeKeyboard::GetAutoRepeatEnabledForTesting());
    127   // Restore the initial state.
    128   xkey_->SetAutoRepeatEnabled(state);
    129   EXPECT_EQ(state, ImeKeyboard::GetAutoRepeatEnabledForTesting());
    130 }
    131 
    132 TEST_F(ImeKeyboardTest, TestSetAutoRepeatRate) {
    133   if (!DisplayAvailable()) {
    134     DVLOG(1) << "X server is not available. Skip the test.";
    135     return;
    136   }
    137   AutoRepeatRate rate;
    138   EXPECT_TRUE(ImeKeyboard::GetAutoRepeatRateForTesting(&rate));
    139 
    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);
    147 
    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);
    153 }
    154 
    155 }  // namespace input_method
    156 }  // namespace chromeos
    157