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_client.h"
8 #include "base/message_loop.h"
9 #include "base/values.h"
10 #include "chromeos/dbus/ibus/ibus_constants.h"
11 #include "dbus/message.h"
12 #include "dbus/mock_bus.h"
13 #include "dbus/mock_object_proxy.h"
14 #include "dbus/object_path.h"
15 #include "dbus/values_util.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 using testing::Invoke
;
20 using testing::Return
;
26 const char kClientName
[] = "chrome";
27 const char kEngineName
[] = "engine";
28 const bool kRestartFlag
= true;
31 class MockCreateInputContextCallback
{
33 MOCK_METHOD1(Run
, void(const dbus::ObjectPath
& object_path
));
36 class MockErrorCallback
{
38 MOCK_METHOD0(Run
, void());
41 class IBusClientTest
: public testing::Test
{
43 IBusClientTest() : response_(NULL
) {}
45 // Handles CreateInputContext method call.
46 void OnCreateInputContext(
47 dbus::MethodCall
* method_call
,
49 const dbus::ObjectProxy::ResponseCallback
& callback
,
50 const dbus::ObjectProxy::ErrorCallback
& error_callback
) {
51 dbus::MessageReader
reader(method_call
);
52 std::string client_name
;
53 EXPECT_TRUE(reader
.PopString(&client_name
));
54 EXPECT_EQ(kClientName
, client_name
);
55 EXPECT_FALSE(reader
.HasMoreData());
57 message_loop_
.PostTask(FROM_HERE
, base::Bind(callback
, response_
));
60 // Handles fail case of CreateInputContext method call.
61 void OnCreateInputContextFail(
62 dbus::MethodCall
* method_call
,
64 const dbus::ObjectProxy::ResponseCallback
& callback
,
65 const dbus::ObjectProxy::ErrorCallback
& error_callback
) {
66 dbus::MessageReader
reader(method_call
);
67 std::string client_name
;
68 EXPECT_TRUE(reader
.PopString(&client_name
));
69 EXPECT_EQ(kClientName
, client_name
);
70 EXPECT_FALSE(reader
.HasMoreData());
72 message_loop_
.PostTask(FROM_HERE
, base::Bind(error_callback
,
76 // Handles SetGlobalEngine method call.
77 void OnSetGlobalEngine(
78 dbus::MethodCall
* method_call
,
80 const dbus::ObjectProxy::ResponseCallback
& callback
,
81 const dbus::ObjectProxy::ErrorCallback
& error_callback
) {
82 dbus::MessageReader
reader(method_call
);
83 std::string engine_name
;
84 EXPECT_TRUE(reader
.PopString(&engine_name
));
85 EXPECT_EQ(kEngineName
, engine_name
);
86 EXPECT_FALSE(reader
.HasMoreData());
87 message_loop_
.PostTask(FROM_HERE
, base::Bind(callback
, response_
));
90 // Handles fail case of SetGlobalEngine method call.
91 void OnSetGlobalEngineFail(
92 dbus::MethodCall
* method_call
,
94 const dbus::ObjectProxy::ResponseCallback
& callback
,
95 const dbus::ObjectProxy::ErrorCallback
& error_callback
) {
96 dbus::MessageReader
reader(method_call
);
97 std::string engine_name
;
98 EXPECT_TRUE(reader
.PopString(&engine_name
));
99 EXPECT_EQ(kEngineName
, engine_name
);
100 EXPECT_FALSE(reader
.HasMoreData());
101 message_loop_
.PostTask(FROM_HERE
, base::Bind(error_callback
,
105 // Handles Exit method call.
106 void OnExit(dbus::MethodCall
* method_call
,
108 const dbus::ObjectProxy::ResponseCallback
& callback
,
109 const dbus::ObjectProxy::ErrorCallback
& error_callback
) {
110 dbus::MessageReader
reader(method_call
);
111 bool restart
= false;
112 EXPECT_TRUE(reader
.PopBool(&restart
));
113 EXPECT_EQ(kRestartFlag
, restart
);
114 EXPECT_FALSE(reader
.HasMoreData());
115 message_loop_
.PostTask(FROM_HERE
, base::Bind(callback
, response_
));
118 // Handles fail case of Exit method call.
119 void OnExitFail(dbus::MethodCall
* method_call
,
121 const dbus::ObjectProxy::ResponseCallback
& callback
,
122 const dbus::ObjectProxy::ErrorCallback
& error_callback
) {
123 dbus::MessageReader
reader(method_call
);
124 bool restart
= false;
125 EXPECT_TRUE(reader
.PopBool(&restart
));
126 EXPECT_EQ(kRestartFlag
, restart
);
127 EXPECT_FALSE(reader
.HasMoreData());
128 message_loop_
.PostTask(FROM_HERE
, base::Bind(error_callback
,
133 virtual void SetUp() OVERRIDE
{
134 dbus::Bus::Options options
;
135 mock_bus_
= new dbus::MockBus(options
);
136 mock_proxy_
= new dbus::MockObjectProxy(mock_bus_
.get(),
139 ibus::bus::kServicePath
));
140 EXPECT_CALL(*mock_bus_
, GetObjectProxy(ibus::kServiceName
,
142 ibus::bus::kServicePath
)))
143 .WillOnce(Return(mock_proxy_
.get()));
145 EXPECT_CALL(*mock_bus_
, ShutdownAndBlock());
146 client_
.reset(IBusClient::Create(REAL_DBUS_CLIENT_IMPLEMENTATION
,
150 virtual void TearDown() OVERRIDE
{
151 mock_bus_
->ShutdownAndBlock();
154 // The IBus client to be tested.
155 scoped_ptr
<IBusClient
> client_
;
157 // A message loop to emulate asynchronous behavior.
158 MessageLoop message_loop_
;
159 scoped_refptr
<dbus::MockBus
> mock_bus_
;
160 scoped_refptr
<dbus::MockObjectProxy
> mock_proxy_
;
162 // Response returned by mock methods.
163 dbus::Response
* response_
;
164 dbus::ErrorResponse
* error_response_
;
167 TEST_F(IBusClientTest
, CreateInputContextTest
) {
169 const dbus::ObjectPath kInputContextObjectPath
=
170 dbus::ObjectPath("/some/object/path");
171 EXPECT_CALL(*mock_proxy_
, CallMethodWithErrorCallback(_
, _
, _
, _
))
172 .WillOnce(Invoke(this, &IBusClientTest::OnCreateInputContext
));
173 MockCreateInputContextCallback callback
;
174 EXPECT_CALL(callback
, Run(kInputContextObjectPath
));
175 MockErrorCallback error_callback
;
176 EXPECT_CALL(error_callback
, Run()).Times(0);
179 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
180 dbus::MessageWriter
writer(response
.get());
181 writer
.AppendObjectPath(kInputContextObjectPath
);
182 response_
= response
.get();
184 // Call CreateInputContext.
185 client_
->CreateInputContext(
187 base::Bind(&MockCreateInputContextCallback::Run
,
188 base::Unretained(&callback
)),
189 base::Bind(&MockErrorCallback::Run
,
190 base::Unretained(&error_callback
)));
192 // Run the message loop.
193 message_loop_
.RunUntilIdle();
196 TEST_F(IBusClientTest
, CreateInputContext_NullResponseFail
) {
198 EXPECT_CALL(*mock_proxy_
, CallMethodWithErrorCallback(_
, _
, _
, _
))
199 .WillOnce(Invoke(this, &IBusClientTest::OnCreateInputContext
));
200 MockCreateInputContextCallback callback
;
201 EXPECT_CALL(callback
, Run(_
)).Times(0);
202 MockErrorCallback error_callback
;
203 EXPECT_CALL(error_callback
, Run());
205 // Set NULL response.
208 // Call CreateInputContext.
209 client_
->CreateInputContext(
211 base::Bind(&MockCreateInputContextCallback::Run
,
212 base::Unretained(&callback
)),
213 base::Bind(&MockErrorCallback::Run
,
214 base::Unretained(&error_callback
)));
216 // Run the message loop.
217 message_loop_
.RunUntilIdle();
220 TEST_F(IBusClientTest
, CreateInputContext_InvalidResponseFail
) {
222 EXPECT_CALL(*mock_proxy_
, CallMethodWithErrorCallback(_
, _
, _
, _
))
223 .WillOnce(Invoke(this, &IBusClientTest::OnCreateInputContext
));
224 MockCreateInputContextCallback callback
;
225 EXPECT_CALL(callback
, Run(_
)).Times(0);
226 MockErrorCallback error_callback
;
227 EXPECT_CALL(error_callback
, Run());
229 // Create invalid(empty) response.
230 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
231 response_
= response
.get();
233 // Call CreateInputContext.
234 client_
->CreateInputContext(
236 base::Bind(&MockCreateInputContextCallback::Run
,
237 base::Unretained(&callback
)),
238 base::Bind(&MockErrorCallback::Run
,
239 base::Unretained(&error_callback
)));
241 // Run the message loop.
242 message_loop_
.RunUntilIdle();
245 TEST_F(IBusClientTest
, CreateInputContext_MethodCallFail
) {
247 EXPECT_CALL(*mock_proxy_
, CallMethodWithErrorCallback(_
, _
, _
, _
))
248 .WillOnce(Invoke(this, &IBusClientTest::OnCreateInputContextFail
));
249 MockCreateInputContextCallback callback
;
250 EXPECT_CALL(callback
, Run(_
)).Times(0);
251 MockErrorCallback error_callback
;
252 EXPECT_CALL(error_callback
, Run());
254 // The error response is not used in CreateInputContext.
255 error_response_
= NULL
;
257 // Call CreateInputContext.
258 client_
->CreateInputContext(
260 base::Bind(&MockCreateInputContextCallback::Run
,
261 base::Unretained(&callback
)),
262 base::Bind(&MockErrorCallback::Run
,
263 base::Unretained(&error_callback
)));
265 // Run the message loop.
266 message_loop_
.RunUntilIdle();
269 TEST_F(IBusClientTest
, SetGlobalEngineTest
) {
271 EXPECT_CALL(*mock_proxy_
, CallMethodWithErrorCallback(_
, _
, _
, _
))
272 .WillOnce(Invoke(this, &IBusClientTest::OnSetGlobalEngine
));
273 MockErrorCallback error_callback
;
274 EXPECT_CALL(error_callback
, Run()).Times(0);
276 // Create empty response.
277 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
278 response_
= response
.get();
280 // The error response is not used in SetGLobalEngine.
281 error_response_
= NULL
;
283 // Call CreateInputContext.
284 client_
->SetGlobalEngine(
286 base::Bind(&MockErrorCallback::Run
,
287 base::Unretained(&error_callback
)));
289 // Run the message loop.
290 message_loop_
.RunUntilIdle();
293 TEST_F(IBusClientTest
, SetGlobalEngineTest_InvalidResponse
) {
295 EXPECT_CALL(*mock_proxy_
, CallMethodWithErrorCallback(_
, _
, _
, _
))
296 .WillOnce(Invoke(this, &IBusClientTest::OnSetGlobalEngineFail
));
297 MockErrorCallback error_callback
;
298 EXPECT_CALL(error_callback
, Run());
300 // Set invalid response.
303 // The error response is not used in SetGLobalEngine.
304 error_response_
= NULL
;
306 // Call CreateInputContext.
307 client_
->SetGlobalEngine(
309 base::Bind(&MockErrorCallback::Run
,
310 base::Unretained(&error_callback
)));
312 // Run the message loop.
313 message_loop_
.RunUntilIdle();
316 TEST_F(IBusClientTest
, SetGlobalEngineTest_MethodCallFail
) {
318 EXPECT_CALL(*mock_proxy_
, CallMethodWithErrorCallback(_
, _
, _
, _
))
319 .WillOnce(Invoke(this, &IBusClientTest::OnSetGlobalEngineFail
));
320 MockErrorCallback error_callback
;
321 EXPECT_CALL(error_callback
, Run());
323 // Create empty response.
324 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
325 response_
= response
.get();
327 // The error response is not used in SetGLobalEngine.
328 error_response_
= NULL
;
330 // Call CreateInputContext.
331 client_
->SetGlobalEngine(
333 base::Bind(&MockErrorCallback::Run
,
334 base::Unretained(&error_callback
)));
336 // Run the message loop.
337 message_loop_
.RunUntilIdle();
340 TEST_F(IBusClientTest
, ExitTest
) {
342 EXPECT_CALL(*mock_proxy_
, CallMethodWithErrorCallback(_
, _
, _
, _
))
343 .WillOnce(Invoke(this, &IBusClientTest::OnExit
));
344 MockErrorCallback error_callback
;
345 EXPECT_CALL(error_callback
, Run()).Times(0);
347 // Create empty response.
348 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
349 response_
= response
.get();
351 // The error response is not used in SetGLobalEngine.
352 error_response_
= NULL
;
354 // Call CreateInputContext.
356 IBusClient::RESTART_IBUS_DAEMON
,
357 base::Bind(&MockErrorCallback::Run
,
358 base::Unretained(&error_callback
)));
360 // Run the message loop.
361 message_loop_
.RunUntilIdle();
364 TEST_F(IBusClientTest
, ExitTest_InvalidResponse
) {
366 EXPECT_CALL(*mock_proxy_
, CallMethodWithErrorCallback(_
, _
, _
, _
))
367 .WillOnce(Invoke(this, &IBusClientTest::OnExit
));
368 MockErrorCallback error_callback
;
369 EXPECT_CALL(error_callback
, Run());
371 // Set invalid response.
374 // The error response is not used in SetGLobalEngine.
375 error_response_
= NULL
;
377 // Call CreateInputContext.
379 IBusClient::RESTART_IBUS_DAEMON
,
380 base::Bind(&MockErrorCallback::Run
,
381 base::Unretained(&error_callback
)));
383 // Run the message loop.
384 message_loop_
.RunUntilIdle();
387 TEST_F(IBusClientTest
, ExitTest_MethodCallFail
) {
389 EXPECT_CALL(*mock_proxy_
, CallMethodWithErrorCallback(_
, _
, _
, _
))
390 .WillOnce(Invoke(this, &IBusClientTest::OnExitFail
));
391 MockErrorCallback error_callback
;
392 EXPECT_CALL(error_callback
, Run());
394 // Create empty response.
395 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
396 response_
= response
.get();
398 // The error response is not used in SetGLobalEngine.
399 error_response_
= NULL
;
401 // Call CreateInputContext.
403 IBusClient::RESTART_IBUS_DAEMON
,
404 base::Bind(&MockErrorCallback::Run
,
405 base::Unretained(&error_callback
)));
407 // Run the message loop.
408 message_loop_
.RunUntilIdle();
411 } // namespace chromeos