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
|
// 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 "chromeos/dbus/services/service_provider_test_helper.h"
#include "base/bind.h"
#include "dbus/message.h"
#include "dbus/mock_bus.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
using ::testing::_;
using ::testing::AllOf;
using ::testing::Invoke;
using ::testing::ResultOf;
using ::testing::Return;
using ::testing::Unused;
namespace chromeos {
ServiceProviderTestHelper::ServiceProviderTestHelper()
: response_received_(false) {
}
ServiceProviderTestHelper::~ServiceProviderTestHelper() {
}
void ServiceProviderTestHelper::SetUp(
const std::string& exported_method_name,
CrosDBusService::ServiceProviderInterface* service_provider) {
// Create a mock bus.
dbus::Bus::Options options;
options.bus_type = dbus::Bus::SYSTEM;
mock_bus_ = new dbus::MockBus(options);
// ShutdownAndBlock() will be called in TearDown().
EXPECT_CALL(*mock_bus_.get(), ShutdownAndBlock()).WillOnce(Return());
// Create a mock exported object that behaves as
// org.chromium.CrosDBusService.
mock_exported_object_ =
new dbus::MockExportedObject(mock_bus_.get(),
dbus::ObjectPath(kLibCrosServicePath));
// |mock_exported_object_|'s ExportMethod() will use
// |MockExportedObject().
EXPECT_CALL(
*mock_exported_object_.get(),
ExportMethod(kLibCrosServiceInterface, exported_method_name, _, _))
.WillOnce(Invoke(this, &ServiceProviderTestHelper::MockExportMethod));
// Create a mock object proxy, with which we call a method of
// |mock_exported_object_|.
mock_object_proxy_ =
new dbus::MockObjectProxy(mock_bus_.get(),
kLibCrosServiceName,
dbus::ObjectPath(kLibCrosServicePath));
// |mock_object_proxy_|'s MockCallMethodAndBlock() will use
// MockCallMethodAndBlock() to return responses.
EXPECT_CALL(*mock_object_proxy_.get(),
MockCallMethodAndBlock(
AllOf(ResultOf(std::mem_fun(&dbus::MethodCall::GetInterface),
kLibCrosServiceInterface),
ResultOf(std::mem_fun(&dbus::MethodCall::GetMember),
exported_method_name)),
_))
.WillOnce(
Invoke(this, &ServiceProviderTestHelper::MockCallMethodAndBlock));
service_provider->Start(mock_exported_object_.get());
}
void ServiceProviderTestHelper::TearDown() {
mock_bus_->ShutdownAndBlock();
mock_exported_object_ = NULL;
mock_object_proxy_ = NULL;
mock_bus_ = NULL;
}
void ServiceProviderTestHelper::SetUpReturnSignal(
const std::string& interface_name,
const std::string& signal_name,
dbus::ObjectProxy::SignalCallback signal_callback,
dbus::ObjectProxy::OnConnectedCallback on_connected_callback) {
// |mock_exported_object_|'s SendSignal() will use
// MockSendSignal().
EXPECT_CALL(*mock_exported_object_.get(), SendSignal(_))
.WillOnce(Invoke(this, &ServiceProviderTestHelper::MockSendSignal));
// |mock_object_proxy_|'s ConnectToSignal will use
// MockConnectToSignal().
EXPECT_CALL(*mock_object_proxy_.get(),
ConnectToSignal(interface_name, signal_name, _, _))
.WillOnce(Invoke(this, &ServiceProviderTestHelper::MockConnectToSignal));
mock_object_proxy_->ConnectToSignal(interface_name, signal_name,
signal_callback, on_connected_callback);
}
scoped_ptr<dbus::Response> ServiceProviderTestHelper::CallMethod(
dbus::MethodCall* method_call) {
return mock_object_proxy_->CallMethodAndBlock(
method_call,
dbus::ObjectProxy::TIMEOUT_USE_DEFAULT);
}
void ServiceProviderTestHelper::MockExportMethod(
const std::string& interface_name,
const std::string& method_name,
dbus::ExportedObject::MethodCallCallback method_callback,
dbus::ExportedObject::OnExportedCallback on_exported_callback) {
// Tell the call back that the method is exported successfully.
on_exported_callback.Run(interface_name, method_name, true);
// Capture the callback, so we can run this at a later time.
method_callback_ = method_callback;
}
dbus::Response* ServiceProviderTestHelper::MockCallMethodAndBlock(
dbus::MethodCall* method_call,
Unused) {
// Set the serial number to non-zero, so
// dbus_message_new_method_return() won't emit a warning.
method_call->SetSerial(1);
// Run the callback captured in MockExportMethod(). In addition to returning
// a response that the caller will ignore, this will send a signal, which
// will be received by |on_signal_callback_|.
method_callback_.Run(method_call,
base::Bind(&ServiceProviderTestHelper::OnResponse,
base::Unretained(this)));
// Check for a response.
if (!response_received_)
message_loop_.Run();
// Return response.
return response_.release();
}
void ServiceProviderTestHelper::MockConnectToSignal(
const std::string& interface_name,
const std::string& signal_name,
dbus::ObjectProxy::SignalCallback signal_callback,
dbus::ObjectProxy::OnConnectedCallback connected_callback) {
// Tell the callback that the object proxy is connected to the signal.
connected_callback.Run(interface_name, signal_name, true);
// Capture the callback, so we can run this at a later time.
on_signal_callback_ = signal_callback;
}
void ServiceProviderTestHelper::MockSendSignal(dbus::Signal* signal) {
// Run the callback captured in MockConnectToSignal(). This will call
// OnSignalReceived().
on_signal_callback_.Run(signal);
}
void ServiceProviderTestHelper::OnResponse(
scoped_ptr<dbus::Response> response) {
response_ = response.Pass();
response_received_ = true;
if (message_loop_.is_running())
message_loop_.Quit();
}
} // namespace chromeos
|