summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/input_method/ibus_controller_base_unittest.cc
blob: 611cf3de378530ccc7b6a900e0c980adbf52eff0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "chrome/browser/chromeos/input_method/ibus_controller_base.h"
#include "chromeos/ime/input_method_config.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace chromeos {
namespace input_method {

namespace {

// A mock class for testing SetInputMethodConfig(), AddObserver(), and
// RemoveObserver() methods in IBusControllerBase.
class TestIBusController : public IBusControllerBase {
 public:
  TestIBusController()
      : set_input_method_config_internal_count_(0),
        set_input_method_config_internal_return_(true) {
  }
  virtual ~TestIBusController() {
  }

  // IBusController overrides:
  virtual void ClearProperties() OVERRIDE {}
  virtual bool ActivateInputMethodProperty(const std::string& key) OVERRIDE {
    return true;
  }

  size_t GetObserverCount() const {
    return observers_.size();
  }

  int set_input_method_config_internal_count() const {
    return set_input_method_config_internal_count_;
  }
  void set_set_input_method_config_internal_return(bool new_value) {
    set_input_method_config_internal_return_ = new_value;
  }
  const ConfigKeyType& last_key() const { return last_key_; }

 protected:
  virtual bool SetInputMethodConfigInternal(
      const ConfigKeyType& key,
      const InputMethodConfigValue& value) OVERRIDE {
    ++set_input_method_config_internal_count_;
    last_key_ = key;
    return set_input_method_config_internal_return_;
  }

 private:
  int set_input_method_config_internal_count_;
  bool set_input_method_config_internal_return_;
  ConfigKeyType last_key_;

  DISALLOW_COPY_AND_ASSIGN(TestIBusController);
};

class TestObserver : public IBusController::Observer {
 public:
  // IBusController::Observer overrides:
  virtual void PropertyChanged() OVERRIDE {}
};

class IBusControllerBaseTest : public testing::Test {
 public:
  virtual void SetUp() {
    controller_.reset(new TestIBusController);
  }
  virtual void TearDown() {
    controller_.reset();
  }

 protected:
  scoped_ptr<TestIBusController> controller_;
};

}  // namespace

TEST_F(IBusControllerBaseTest, TestSetInputMethodConfig) {
  InputMethodConfigValue value_set;
  InputMethodConfigValue value_get;
  EXPECT_FALSE(controller_->GetInputMethodConfigForTesting("section",
                                                           "name",
                                                           &value_get));
  EXPECT_EQ(0, controller_->set_input_method_config_internal_count());

  // Set a value.
  value_set.type = InputMethodConfigValue::kValueTypeInt;
  value_set.int_value = 12345;
  EXPECT_TRUE(controller_->SetInputMethodConfig("section",
                                                "name",
                                                value_set));
  EXPECT_EQ(1, controller_->set_input_method_config_internal_count());
  EXPECT_EQ("section", controller_->last_key().first);
  EXPECT_EQ("name", controller_->last_key().second);

  // Get the value.
  EXPECT_TRUE(controller_->GetInputMethodConfigForTesting("section",
                                                          "name",
                                                          &value_get));
  EXPECT_EQ(value_set.type, value_get.type);
  EXPECT_EQ(value_set.int_value, value_get.int_value);

  // Set another value.
  value_set.type = InputMethodConfigValue::kValueTypeBool;
  value_set.bool_value = true;
  EXPECT_TRUE(controller_->SetInputMethodConfig("section/2",
                                                "name2",
                                                value_set));
  EXPECT_EQ(2, controller_->set_input_method_config_internal_count());
  EXPECT_EQ("section/2", controller_->last_key().first);
  EXPECT_EQ("name2", controller_->last_key().second);

  // Overwrite the first value.
  value_set.type = InputMethodConfigValue::kValueTypeInt;
  value_set.int_value = 54321;
  EXPECT_TRUE(controller_->SetInputMethodConfig("section",
                                                "name",
                                                value_set));
  EXPECT_EQ(3, controller_->set_input_method_config_internal_count());
  EXPECT_EQ("section", controller_->last_key().first);
  EXPECT_EQ("name", controller_->last_key().second);

  // Get the value.
  EXPECT_TRUE(controller_->GetInputMethodConfigForTesting("section",
                                                          "name",
                                                          &value_get));
  EXPECT_EQ(value_set.type, value_get.type);
  EXPECT_EQ(value_set.int_value, value_get.int_value);

  // Get a non existent value.
  EXPECT_FALSE(controller_->GetInputMethodConfigForTesting("sectionX",
                                                           "name",
                                                           &value_get));
  EXPECT_FALSE(controller_->GetInputMethodConfigForTesting("section",
                                                           "nameX",
                                                           &value_get));
}

TEST_F(IBusControllerBaseTest, TestSetInputMethodConfigInternal) {
  InputMethodConfigValue value_set;
  InputMethodConfigValue value_get;
  // Set a value. In this case, SetInputMethodConfigInternal returns false.
  controller_->set_set_input_method_config_internal_return(false);
  value_set.type = InputMethodConfigValue::kValueTypeInt;
  value_set.int_value = 12345;
  EXPECT_FALSE(controller_->SetInputMethodConfig("section",
                                                 "name",
                                                 value_set));
  EXPECT_EQ(1, controller_->set_input_method_config_internal_count());

  // Getting the value should fail.
  EXPECT_FALSE(controller_->GetInputMethodConfigForTesting("section",
                                                           "name",
                                                           &value_get));
}

TEST_F(IBusControllerBaseTest, TestAddRemoveObserver) {
  TestObserver observer1;
  TestObserver observer2;
  TestObserver observer3;
  EXPECT_EQ(0U, controller_->GetObserverCount());
  controller_->AddObserver(&observer1);
  EXPECT_EQ(1U, controller_->GetObserverCount());
  controller_->AddObserver(&observer2);
  EXPECT_EQ(2U, controller_->GetObserverCount());
  controller_->RemoveObserver(&observer3);  // nop
  EXPECT_EQ(2U, controller_->GetObserverCount());
  controller_->RemoveObserver(&observer1);
  EXPECT_EQ(1U, controller_->GetObserverCount());
  controller_->RemoveObserver(&observer1);  // nop
  EXPECT_EQ(1U, controller_->GetObserverCount());
  controller_->RemoveObserver(&observer2);
  EXPECT_EQ(0U, controller_->GetObserverCount());
}

TEST_F(IBusControllerBaseTest, TestGetCurrentProperties) {
  EXPECT_EQ(0U, controller_->GetCurrentProperties().size());
}

}  // namespace input_method
}  // namespace chromeos