Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / chromeos / dbus / ibus / ibus_engine_factory_service_unittest.cc
blobc3c2aa6f5b387a9ab19659584303af6924b4b96e
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.
5 #include "chromeos/dbus/ibus/ibus_engine_factory_service.h"
7 #include <map>
8 #include <string>
9 #include "base/message_loop.h"
10 #include "chromeos/dbus/ibus/ibus_constants.h"
11 #include "dbus/message.h"
12 #include "dbus/mock_bus.h"
13 #include "dbus/mock_exported_object.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 using ::testing::Invoke;
18 using ::testing::Return;
19 using ::testing::StrEq;
20 using ::testing::_;
22 namespace chromeos {
24 class SynchronousCreateEngineHandler {
25 public:
26 explicit SynchronousCreateEngineHandler(const dbus::ObjectPath& path)
27 : path_(path) {}
29 void Run(const IBusEngineFactoryService::CreateEngineResponseSender& sender) {
30 sender.Run(path_);
33 private:
34 dbus::ObjectPath path_;
36 DISALLOW_COPY_AND_ASSIGN(SynchronousCreateEngineHandler);
39 class AsynchronousCreateEngineHandler {
40 public:
41 AsynchronousCreateEngineHandler(const dbus::ObjectPath& path,
42 MessageLoop* message_loop)
43 : path_(path),
44 message_loop_(message_loop) {}
46 void Run(const IBusEngineFactoryService::CreateEngineResponseSender& sender) {
47 message_loop_->PostTask(FROM_HERE, base::Bind(sender, path_));
50 private:
51 dbus::ObjectPath path_;
52 MessageLoop* message_loop_;
54 DISALLOW_COPY_AND_ASSIGN(AsynchronousCreateEngineHandler);
57 class MockCreateEngineResponseSender {
58 public:
59 explicit MockCreateEngineResponseSender(const dbus::ObjectPath& expected_path)
60 : expected_path_(expected_path) {}
61 // GMock doesn't support mocking methods which take scoped_ptr<>.
62 MOCK_METHOD1(MockRun, void(dbus::Response*));
63 void Run(scoped_ptr<dbus::Response> response) {
64 MockRun(response.get());
67 // Checks the given |response| meets expectation for the CreateEngine method.
68 void CheckCreateEngineResponsePtr(dbus::Response* response) {
69 dbus::MessageReader reader(response);
70 dbus::ObjectPath actual_path;
71 ASSERT_TRUE(reader.PopObjectPath(&actual_path));
72 EXPECT_EQ(expected_path_, actual_path);
74 void CheckCreateEngineResponse(scoped_ptr<dbus::Response> response) {
75 CheckCreateEngineResponsePtr(response.get());
78 private:
79 dbus::ObjectPath expected_path_;
82 class IBusEngineFactoryServiceTest : public testing::Test {
83 public:
84 IBusEngineFactoryServiceTest() {}
86 virtual void SetUp() OVERRIDE {
87 // Create a mock bus.
88 dbus::Bus::Options options;
89 options.bus_type = dbus::Bus::SYSTEM;
90 mock_bus_ = new dbus::MockBus(options);
92 // Create a mock exported object.
93 mock_exported_object_ = new dbus::MockExportedObject(
94 mock_bus_.get(),
95 dbus::ObjectPath(ibus::engine_factory::kServicePath));
97 EXPECT_CALL(*mock_bus_,
98 GetExportedObject(dbus::ObjectPath(
99 ibus::engine_factory::kServicePath)))
100 .WillOnce(Return(mock_exported_object_.get()));
102 EXPECT_CALL(*mock_bus_, AssertOnOriginThread())
103 .WillRepeatedly(Return());
105 EXPECT_CALL(*mock_exported_object_, ExportMethod(
106 ibus::engine_factory::kServiceInterface,
107 ibus::engine_factory::kCreateEngineMethod,
110 .WillRepeatedly(
111 Invoke(this, &IBusEngineFactoryServiceTest::OnMethodExported));
113 service_.reset(IBusEngineFactoryService::Create(
114 mock_bus_,
115 REAL_DBUS_CLIENT_IMPLEMENTATION));
118 protected:
119 // The service to be tested.
120 scoped_ptr<IBusEngineFactoryService> service_;
121 // The mock bus.
122 scoped_refptr<dbus::MockBus> mock_bus_;
123 // The mock exported object.
124 scoped_refptr<dbus::MockExportedObject> mock_exported_object_;
125 // The map from method name to method call handler.
126 std::map<std::string, dbus::ExportedObject::MethodCallCallback>
127 method_exported_map_;
128 // A message loop to emulate asynchronous behavior.
129 MessageLoop message_loop_;
131 private:
132 // Used to implement the method call exportation.
133 void OnMethodExported(
134 const std::string& interface_name,
135 const std::string& method_name,
136 const dbus::ExportedObject::MethodCallCallback& method_callback,
137 const dbus::ExportedObject::OnExportedCallback& on_exported_callback) {
138 method_exported_map_[method_name] = method_callback;
139 const bool success = true;
140 message_loop_.PostTask(FROM_HERE, base::Bind(on_exported_callback,
141 interface_name,
142 method_name,
143 success));
147 TEST_F(IBusEngineFactoryServiceTest, SyncCreateEngineTest) {
148 // Set expectations.
149 const char kSampleEngine[] = "Sample Engine";
150 const dbus::ObjectPath kObjectPath("/org/freedesktop/IBus/Engine/10");
151 MockCreateEngineResponseSender response_sender(kObjectPath);
152 EXPECT_CALL(response_sender, MockRun(_))
153 .WillOnce(
154 Invoke(&response_sender,
155 &MockCreateEngineResponseSender::
156 CheckCreateEngineResponsePtr));
158 SynchronousCreateEngineHandler handler(kObjectPath);
159 // Set handler expectations.
160 service_->SetCreateEngineHandler(
161 kSampleEngine,
162 base::Bind(&SynchronousCreateEngineHandler::Run,
163 base::Unretained(&handler)));
164 message_loop_.RunUntilIdle();
166 // Invoke method call.
167 dbus::MethodCall method_call(
168 ibus::engine_factory::kServiceInterface,
169 ibus::engine_factory::kCreateEngineMethod);
170 method_call.SetSerial(10);
171 dbus::MessageWriter writer(&method_call);
172 writer.AppendString(kSampleEngine);
173 ASSERT_FALSE(
174 method_exported_map_[ibus::engine_factory::kCreateEngineMethod]
175 .is_null());
176 method_exported_map_[ibus::engine_factory::kCreateEngineMethod].Run(
177 &method_call,
178 base::Bind(&MockCreateEngineResponseSender::Run,
179 base::Unretained(&response_sender)));
181 // Unset the handler so expect not calling handler.
182 service_->UnsetCreateEngineHandler(kSampleEngine);
183 method_exported_map_[ibus::engine_factory::kCreateEngineMethod].Run(
184 &method_call,
185 base::Bind(&MockCreateEngineResponseSender::CheckCreateEngineResponse,
186 base::Unretained(&response_sender)));
187 message_loop_.RunUntilIdle();
190 TEST_F(IBusEngineFactoryServiceTest, AsyncCreateEngineTest) {
191 // Set expectations.
192 const char kSampleEngine[] = "Sample Engine";
193 const dbus::ObjectPath kObjectPath("/org/freedesktop/IBus/Engine/10");
194 MockCreateEngineResponseSender response_sender(kObjectPath);
195 EXPECT_CALL(response_sender, MockRun(_))
196 .WillOnce(
197 Invoke(&response_sender,
198 &MockCreateEngineResponseSender::
199 CheckCreateEngineResponsePtr));
201 AsynchronousCreateEngineHandler handler(kObjectPath, &message_loop_);
202 // Set handler expectations.
203 service_->SetCreateEngineHandler(
204 kSampleEngine,
205 base::Bind(&AsynchronousCreateEngineHandler::Run,
206 base::Unretained(&handler)));
207 message_loop_.RunUntilIdle();
209 // Invoke method call.
210 dbus::MethodCall method_call(
211 ibus::engine_factory::kServiceInterface,
212 ibus::engine_factory::kCreateEngineMethod);
213 method_call.SetSerial(10);
214 dbus::MessageWriter writer(&method_call);
215 writer.AppendString(kSampleEngine);
216 ASSERT_FALSE(
217 method_exported_map_[ibus::engine_factory::kCreateEngineMethod]
218 .is_null());
219 method_exported_map_[ibus::engine_factory::kCreateEngineMethod].Run(
220 &method_call,
221 base::Bind(&MockCreateEngineResponseSender::Run,
222 base::Unretained(&response_sender)));
224 // Unset the handler so expect not calling handler.
225 service_->UnsetCreateEngineHandler(kSampleEngine);
226 method_exported_map_[ibus::engine_factory::kCreateEngineMethod].Run(
227 &method_call,
228 base::Bind(&MockCreateEngineResponseSender::CheckCreateEngineResponse,
229 base::Unretained(&response_sender)));
230 message_loop_.RunUntilIdle();
233 } // namespace chromeos