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/blocking_method_caller.h"
7 #include "base/logging.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "dbus/message.h"
11 #include "dbus/mock_bus.h"
12 #include "dbus/mock_object_proxy.h"
13 #include "dbus/object_path.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
18 using ::testing::Invoke
;
19 using ::testing::Return
;
23 class BlockingMethodCallerTest
: public testing::Test
{
25 BlockingMethodCallerTest() {
28 virtual void SetUp() {
30 dbus::Bus::Options options
;
31 options
.bus_type
= dbus::Bus::SYSTEM
;
32 mock_bus_
= new dbus::MockBus(options
);
34 // Create a mock proxy.
35 mock_proxy_
= new dbus::MockObjectProxy(
37 "org.chromium.TestService",
38 dbus::ObjectPath("/org/chromium/TestObject"));
40 // Set an expectation so mock_proxy's CallMethodAndBlock() will use
41 // CreateMockProxyResponse() to return responses.
42 EXPECT_CALL(*mock_proxy_
, MockCallMethodAndBlock(_
, _
))
43 .WillRepeatedly(Invoke(
44 this, &BlockingMethodCallerTest::CreateMockProxyResponse
));
46 // Set an expectation so mock_bus's GetObjectProxy() for the given
47 // service name and the object path will return mock_proxy_.
48 EXPECT_CALL(*mock_bus_
, GetObjectProxy(
49 "org.chromium.TestService",
50 dbus::ObjectPath("/org/chromium/TestObject")))
51 .WillOnce(Return(mock_proxy_
.get()));
53 // Set an expectation so mock_bus's PostTaskToDBusThread() will run the
55 EXPECT_CALL(*mock_bus_
, PostTaskToDBusThread(_
, _
))
56 .WillRepeatedly(Invoke(
57 this, &BlockingMethodCallerTest::RunTask
));
59 // ShutdownAndBlock() will be called in TearDown().
60 EXPECT_CALL(*mock_bus_
, ShutdownAndBlock()).WillOnce(Return());
63 virtual void TearDown() {
64 mock_bus_
->ShutdownAndBlock();
68 scoped_refptr
<dbus::MockBus
> mock_bus_
;
69 scoped_refptr
<dbus::MockObjectProxy
> mock_proxy_
;
72 // Returns a response for the given method call. Used to implement
73 // CallMethodAndBlock() for |mock_proxy_|.
74 dbus::Response
* CreateMockProxyResponse(dbus::MethodCall
* method_call
,
76 if (method_call
->GetInterface() == "org.chromium.TestInterface" &&
77 method_call
->GetMember() == "Echo") {
78 dbus::MessageReader
reader(method_call
);
79 std::string text_message
;
80 if (reader
.PopString(&text_message
)) {
81 scoped_ptr
<dbus::Response
> response
= dbus::Response::CreateEmpty();
82 dbus::MessageWriter
writer(response
.get());
83 writer
.AppendString(text_message
);
84 return response
.release();
88 LOG(ERROR
) << "Unexpected method call: " << method_call
->ToString();
92 // Runs the given task.
93 void RunTask(const tracked_objects::Location
& from_here
,
94 const base::Closure
& task
) {
99 TEST_F(BlockingMethodCallerTest
, Echo
) {
100 const char kHello
[] = "Hello";
101 // Get an object proxy from the mock bus.
102 dbus::ObjectProxy
* proxy
= mock_bus_
->GetObjectProxy(
103 "org.chromium.TestService",
104 dbus::ObjectPath("/org/chromium/TestObject"));
106 // Create a method call.
107 dbus::MethodCall
method_call("org.chromium.TestInterface", "Echo");
108 dbus::MessageWriter
writer(&method_call
);
109 writer
.AppendString(kHello
);
112 BlockingMethodCaller
blocking_method_caller(mock_bus_
.get(), proxy
);
113 scoped_ptr
<dbus::Response
> response(
114 blocking_method_caller
.CallMethodAndBlock(&method_call
));
116 // Check the response.
117 ASSERT_TRUE(response
.get());
118 dbus::MessageReader
reader(response
.get());
119 std::string text_message
;
120 ASSERT_TRUE(reader
.PopString(&text_message
));
121 // The text message should be echo'ed back.
122 EXPECT_EQ(kHello
, text_message
);
125 } // namespace chromeos