OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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 "base/logging.h" |
| 6 #include "base/memory/scoped_ptr.h" |
| 7 #include "chrome/browser/chromeos/input_method/ibus_controller_base.h" |
| 8 #include "chrome/browser/chromeos/input_method/input_method_config.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 |
| 11 namespace chromeos { |
| 12 namespace input_method { |
| 13 |
| 14 namespace { |
| 15 |
| 16 // A mock class for testing SetInputMethodConfig(), AddObserver(), and |
| 17 // RemoveObserver() methods in IBusControllerBase. |
| 18 class TestIBusController : public IBusControllerBase { |
| 19 public: |
| 20 TestIBusController() : set_input_method_config_internal_count_(0) { |
| 21 } |
| 22 virtual ~TestIBusController() { |
| 23 } |
| 24 |
| 25 // IBusController overrides: |
| 26 virtual bool Start(const std::vector<std::string>& ids) OVERRIDE { |
| 27 return true; |
| 28 } |
| 29 virtual bool Stop() OVERRIDE { |
| 30 return true; |
| 31 } |
| 32 virtual bool ChangeInputMethod(const std::string& id) OVERRIDE { |
| 33 return true; |
| 34 } |
| 35 virtual bool ActivateInputMethodProperty(const std::string& key) OVERRIDE { |
| 36 return true; |
| 37 } |
| 38 #if defined(USE_VIRTUAL_KEYBOARD) |
| 39 virtual void SendHandwritingStroke(const HandwritingStroke& stroke) OVERRIDE { |
| 40 } |
| 41 virtual void CancelHandwriting(int n_strokes) OVERRIDE { |
| 42 } |
| 43 #endif |
| 44 |
| 45 size_t GetObserverCount() const { |
| 46 return observers_.size(); |
| 47 } |
| 48 |
| 49 int set_input_method_config_internal_count() const { |
| 50 return set_input_method_config_internal_count_; |
| 51 }; |
| 52 const ConfigKeyType& last_key() const { return last_key_; } |
| 53 |
| 54 protected: |
| 55 virtual bool SetInputMethodConfigInternal( |
| 56 const ConfigKeyType& key, |
| 57 const InputMethodConfigValue& value) OVERRIDE { |
| 58 ++set_input_method_config_internal_count_; |
| 59 last_key_ = key; |
| 60 return true; |
| 61 } |
| 62 |
| 63 private: |
| 64 int set_input_method_config_internal_count_; |
| 65 ConfigKeyType last_key_; |
| 66 |
| 67 DISALLOW_COPY_AND_ASSIGN(TestIBusController); |
| 68 }; |
| 69 |
| 70 class TestObserver : public IBusController::Observer { |
| 71 public: |
| 72 // IBusController::Observer overrides: |
| 73 virtual void PropertyChanged() OVERRIDE {} |
| 74 }; |
| 75 |
| 76 class IBusControllerBaseTest : public testing::Test { |
| 77 public: |
| 78 virtual void SetUp() { |
| 79 controller_.reset(new TestIBusController); |
| 80 } |
| 81 virtual void TearDown() { |
| 82 controller_.reset(); |
| 83 } |
| 84 |
| 85 protected: |
| 86 scoped_ptr<TestIBusController> controller_; |
| 87 }; |
| 88 |
| 89 } // namespace |
| 90 |
| 91 TEST_F(IBusControllerBaseTest, TestSetInputMethodConfig) { |
| 92 InputMethodConfigValue value_set; |
| 93 InputMethodConfigValue value_get; |
| 94 EXPECT_FALSE(controller_->GetInputMethodConfigForTesting("section", |
| 95 "name", |
| 96 &value_get)); |
| 97 EXPECT_EQ(0, controller_->set_input_method_config_internal_count()); |
| 98 |
| 99 // Set a value. |
| 100 value_set.type = InputMethodConfigValue::kValueTypeInt; |
| 101 value_set.int_value = 12345; |
| 102 EXPECT_TRUE(controller_->SetInputMethodConfig("section", |
| 103 "name", |
| 104 value_set)); |
| 105 EXPECT_EQ(1, controller_->set_input_method_config_internal_count()); |
| 106 EXPECT_EQ("section", controller_->last_key().first); |
| 107 EXPECT_EQ("name", controller_->last_key().second); |
| 108 |
| 109 // Get the value. |
| 110 EXPECT_TRUE(controller_->GetInputMethodConfigForTesting("section", |
| 111 "name", |
| 112 &value_get)); |
| 113 EXPECT_EQ(value_set.type, value_get.type); |
| 114 EXPECT_EQ(value_set.int_value, value_get.int_value); |
| 115 |
| 116 // Set another value. |
| 117 value_set.type = InputMethodConfigValue::kValueTypeBool; |
| 118 value_set.bool_value = true; |
| 119 EXPECT_TRUE(controller_->SetInputMethodConfig("section/2", |
| 120 "name2", |
| 121 value_set)); |
| 122 EXPECT_EQ(2, controller_->set_input_method_config_internal_count()); |
| 123 EXPECT_EQ("section/2", controller_->last_key().first); |
| 124 EXPECT_EQ("name2", controller_->last_key().second); |
| 125 |
| 126 // Overwrite the first value. |
| 127 value_set.type = InputMethodConfigValue::kValueTypeInt; |
| 128 value_set.int_value = 54321; |
| 129 EXPECT_TRUE(controller_->SetInputMethodConfig("section", |
| 130 "name", |
| 131 value_set)); |
| 132 EXPECT_EQ(3, controller_->set_input_method_config_internal_count()); |
| 133 EXPECT_EQ("section", controller_->last_key().first); |
| 134 EXPECT_EQ("name", controller_->last_key().second); |
| 135 |
| 136 // Get the value. |
| 137 EXPECT_TRUE(controller_->GetInputMethodConfigForTesting("section", |
| 138 "name", |
| 139 &value_get)); |
| 140 EXPECT_EQ(value_set.type, value_get.type); |
| 141 EXPECT_EQ(value_set.int_value, value_get.int_value); |
| 142 |
| 143 // Get a non existent value. |
| 144 EXPECT_FALSE(controller_->GetInputMethodConfigForTesting("sectionX", |
| 145 "name", |
| 146 &value_get)); |
| 147 EXPECT_FALSE(controller_->GetInputMethodConfigForTesting("section", |
| 148 "nameX", |
| 149 &value_get)); |
| 150 } |
| 151 |
| 152 TEST_F(IBusControllerBaseTest, TestAddRemoveObserver) { |
| 153 TestObserver observer1; |
| 154 TestObserver observer2; |
| 155 TestObserver observer3; |
| 156 EXPECT_EQ(0U, controller_->GetObserverCount()); |
| 157 controller_->AddObserver(&observer1); |
| 158 EXPECT_EQ(1U, controller_->GetObserverCount()); |
| 159 controller_->AddObserver(&observer2); |
| 160 EXPECT_EQ(2U, controller_->GetObserverCount()); |
| 161 controller_->RemoveObserver(&observer3); // nop |
| 162 EXPECT_EQ(2U, controller_->GetObserverCount()); |
| 163 controller_->RemoveObserver(&observer1); |
| 164 EXPECT_EQ(1U, controller_->GetObserverCount()); |
| 165 controller_->RemoveObserver(&observer1); // nop |
| 166 EXPECT_EQ(1U, controller_->GetObserverCount()); |
| 167 controller_->RemoveObserver(&observer2); |
| 168 EXPECT_EQ(0U, controller_->GetObserverCount()); |
| 169 } |
| 170 |
| 171 TEST_F(IBusControllerBaseTest, TestGetCurrentProperties) { |
| 172 EXPECT_EQ(0U, controller_->GetCurrentProperties().size()); |
| 173 } |
| 174 |
| 175 } // namespace input_method |
| 176 } // namespace chromeos |
OLD | NEW |