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/services/service_provider_test_helper.h"
8 #include "dbus/message.h"
9 #include "dbus/mock_bus.h"
10 #include "third_party/cros_system_api/dbus/service_constants.h"
13 using ::testing::AllOf
;
14 using ::testing::Invoke
;
15 using ::testing::ResultOf
;
16 using ::testing::Return
;
17 using ::testing::Unused
;
21 ServiceProviderTestHelper::ServiceProviderTestHelper()
22 : response_received_(false) {
25 ServiceProviderTestHelper::~ServiceProviderTestHelper() {
28 void ServiceProviderTestHelper::SetUp(
29 const std::string
& exported_method_name
,
30 CrosDBusService::ServiceProviderInterface
* service_provider
) {
32 dbus::Bus::Options options
;
33 options
.bus_type
= dbus::Bus::SYSTEM
;
34 mock_bus_
= new dbus::MockBus(options
);
36 // ShutdownAndBlock() will be called in TearDown().
37 EXPECT_CALL(*mock_bus_
.get(), ShutdownAndBlock()).WillOnce(Return());
39 // Create a mock exported object that behaves as
40 // org.chromium.CrosDBusService.
41 mock_exported_object_
=
42 new dbus::MockExportedObject(mock_bus_
.get(),
43 dbus::ObjectPath(kLibCrosServicePath
));
45 // |mock_exported_object_|'s ExportMethod() will use
46 // |MockExportedObject().
48 *mock_exported_object_
.get(),
49 ExportMethod(kLibCrosServiceInterface
, exported_method_name
, _
, _
))
50 .WillOnce(Invoke(this, &ServiceProviderTestHelper::MockExportMethod
));
52 // Create a mock object proxy, with which we call a method of
53 // |mock_exported_object_|.
55 new dbus::MockObjectProxy(mock_bus_
.get(),
57 dbus::ObjectPath(kLibCrosServicePath
));
58 // |mock_object_proxy_|'s MockCallMethodAndBlock() will use
59 // MockCallMethodAndBlock() to return responses.
60 EXPECT_CALL(*mock_object_proxy_
.get(),
61 MockCallMethodAndBlock(
62 AllOf(ResultOf(std::mem_fun(&dbus::MethodCall::GetInterface
),
63 kLibCrosServiceInterface
),
64 ResultOf(std::mem_fun(&dbus::MethodCall::GetMember
),
65 exported_method_name
)),
68 Invoke(this, &ServiceProviderTestHelper::MockCallMethodAndBlock
));
70 service_provider
->Start(mock_exported_object_
.get());
73 void ServiceProviderTestHelper::TearDown() {
74 mock_bus_
->ShutdownAndBlock();
75 mock_exported_object_
= NULL
;
76 mock_object_proxy_
= NULL
;
80 void ServiceProviderTestHelper::SetUpReturnSignal(
81 const std::string
& interface_name
,
82 const std::string
& signal_name
,
83 dbus::ObjectProxy::SignalCallback signal_callback
,
84 dbus::ObjectProxy::OnConnectedCallback on_connected_callback
) {
85 // |mock_exported_object_|'s SendSignal() will use
87 EXPECT_CALL(*mock_exported_object_
.get(), SendSignal(_
))
88 .WillOnce(Invoke(this, &ServiceProviderTestHelper::MockSendSignal
));
90 // |mock_object_proxy_|'s ConnectToSignal will use
91 // MockConnectToSignal().
92 EXPECT_CALL(*mock_object_proxy_
.get(),
93 ConnectToSignal(interface_name
, signal_name
, _
, _
))
94 .WillOnce(Invoke(this, &ServiceProviderTestHelper::MockConnectToSignal
));
96 mock_object_proxy_
->ConnectToSignal(interface_name
, signal_name
,
97 signal_callback
, on_connected_callback
);
100 scoped_ptr
<dbus::Response
> ServiceProviderTestHelper::CallMethod(
101 dbus::MethodCall
* method_call
) {
102 return mock_object_proxy_
->CallMethodAndBlock(
104 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
);
107 void ServiceProviderTestHelper::MockExportMethod(
108 const std::string
& interface_name
,
109 const std::string
& method_name
,
110 dbus::ExportedObject::MethodCallCallback method_callback
,
111 dbus::ExportedObject::OnExportedCallback on_exported_callback
) {
112 // Tell the call back that the method is exported successfully.
113 on_exported_callback
.Run(interface_name
, method_name
, true);
114 // Capture the callback, so we can run this at a later time.
115 method_callback_
= method_callback
;
118 dbus::Response
* ServiceProviderTestHelper::MockCallMethodAndBlock(
119 dbus::MethodCall
* method_call
,
121 // Set the serial number to non-zero, so
122 // dbus_message_new_method_return() won't emit a warning.
123 method_call
->SetSerial(1);
124 // Run the callback captured in MockExportMethod(). In addition to returning
125 // a response that the caller will ignore, this will send a signal, which
126 // will be received by |on_signal_callback_|.
127 method_callback_
.Run(method_call
,
128 base::Bind(&ServiceProviderTestHelper::OnResponse
,
129 base::Unretained(this)));
130 // Check for a response.
131 if (!response_received_
)
134 return response_
.release();
137 void ServiceProviderTestHelper::MockConnectToSignal(
138 const std::string
& interface_name
,
139 const std::string
& signal_name
,
140 dbus::ObjectProxy::SignalCallback signal_callback
,
141 dbus::ObjectProxy::OnConnectedCallback connected_callback
) {
142 // Tell the callback that the object proxy is connected to the signal.
143 connected_callback
.Run(interface_name
, signal_name
, true);
144 // Capture the callback, so we can run this at a later time.
145 on_signal_callback_
= signal_callback
;
148 void ServiceProviderTestHelper::MockSendSignal(dbus::Signal
* signal
) {
149 // Run the callback captured in MockConnectToSignal(). This will call
150 // OnSignalReceived().
151 on_signal_callback_
.Run(signal
);
154 void ServiceProviderTestHelper::OnResponse(
155 scoped_ptr
<dbus::Response
> response
) {
156 response_
= response
.Pass();
157 response_received_
= true;
158 if (message_loop_
.is_running())
159 message_loop_
.Quit();
162 } // namespace chromeos