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 #ifndef CHROMEOS_DBUS_SHILL_CLIENT_UNITTEST_BASE_H_
6 #define CHROMEOS_DBUS_SHILL_CLIENT_UNITTEST_BASE_H_
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/message_loop.h"
13 #include "chromeos/dbus/dbus_method_call_status.h"
14 #include "chromeos/dbus/shill_client_helper.h"
15 #include "chromeos/dbus/shill_property_changed_observer.h"
16 #include "dbus/mock_bus.h"
17 #include "dbus/mock_object_proxy.h"
18 #include "dbus/object_proxy.h"
19 #include "testing/gtest/include/gtest/gtest.h"
21 using ::testing::MatcherInterface
;
22 using ::testing::MatchResultListener
;
23 using ::testing::Matcher
;
24 using ::testing::MakeMatcher
;
29 class DictionaryValue
;
41 // A gmock matcher for base::Value types, so we can match them in expectations.
42 class ValueMatcher
: public MatcherInterface
<const base::Value
&> {
44 explicit ValueMatcher(const base::Value
& value
);
46 // MatcherInterface overrides.
47 virtual bool MatchAndExplain(const base::Value
& value
,
48 MatchResultListener
* listener
) const OVERRIDE
;
49 virtual void DescribeTo(::std::ostream
* os
) const OVERRIDE
;
50 virtual void DescribeNegationTo(::std::ostream
* os
) const OVERRIDE
;
53 scoped_ptr
<base::Value
> expected_value_
;
56 inline Matcher
<const base::Value
&> ValueEq(const base::Value
& expected_value
) {
57 return MakeMatcher(new ValueMatcher(expected_value
));
60 // A class to provide functionalities needed for testing Shill D-Bus clients.
61 class ShillClientUnittestBase
: public testing::Test
{
68 MOCK_METHOD0(Run
, void());
69 base::Closure
GetCallback();
72 class MockListValueCallback
{
74 MockListValueCallback();
75 ~MockListValueCallback();
76 MOCK_METHOD1(Run
, void(const base::ListValue
& list
));
77 ShillClientHelper::ListValueCallback
GetCallback();
80 // A mock ErrorCallback.
81 class MockErrorCallback
{
85 MOCK_METHOD2(Run
, void(const std::string
& error_name
,
86 const std::string
& error_message
));
87 ShillClientHelper::ErrorCallback
GetCallback();
90 // A mock PropertyChangedObserver that can be used to check expected values.
91 class MockPropertyChangeObserver
92 : public ShillPropertyChangedObserver
{
94 MockPropertyChangeObserver();
95 ~MockPropertyChangeObserver();
96 MOCK_METHOD2(OnPropertyChanged
, void(const std::string
& name
,
97 const base::Value
& value
));
100 explicit ShillClientUnittestBase(const std::string
& interface_name
,
101 const dbus::ObjectPath
& object_path
);
102 virtual ~ShillClientUnittestBase();
104 virtual void SetUp() OVERRIDE
;
105 virtual void TearDown() OVERRIDE
;
108 // A callback to intercept and check the method call arguments.
109 typedef base::Callback
<void(
110 dbus::MessageReader
* reader
)> ArgumentCheckCallback
;
112 // Sets expectations for called method name and arguments, and sets response.
113 void PrepareForMethodCall(const std::string
& method_name
,
114 const ArgumentCheckCallback
& argument_checker
,
115 dbus::Response
* response
);
117 // Sends property changed signal to the tested client.
118 void SendPropertyChangedSignal(dbus::Signal
* signal
);
120 // Checks the name and the value which are sent by PropertyChanged signal.
121 static void ExpectPropertyChanged(const std::string
& expected_name
,
122 const base::Value
* expected_value
,
123 const std::string
& name
,
124 const base::Value
& value
);
126 // Expects the reader to be empty.
127 static void ExpectNoArgument(dbus::MessageReader
* reader
);
129 // Expects the reader to have a string.
130 static void ExpectStringArgument(const std::string
& expected_string
,
131 dbus::MessageReader
* reader
);
133 static void ExpectArrayOfStringsArgument(
134 const std::vector
<std::string
>& expected_strings
,
135 dbus::MessageReader
* reader
);
137 // Expects the reader to have a Value.
138 static void ExpectValueArgument(const base::Value
* expected_value
,
139 dbus::MessageReader
* reader
);
141 // Expects the reader to have a string and a Value.
142 static void ExpectStringAndValueArguments(const std::string
& expected_string
,
143 const base::Value
* expected_value
,
144 dbus::MessageReader
* reader
);
146 // Expects the call status to be SUCCESS.
147 static void ExpectNoResultValue(DBusMethodCallStatus call_status
);
149 // Checks the result and expects the call status to be SUCCESS.
150 static void ExpectObjectPathResult(const dbus::ObjectPath
& expected_result
,
151 DBusMethodCallStatus call_status
,
152 const dbus::ObjectPath
& result
);
154 static void ExpectObjectPathResultWithoutStatus(
155 const dbus::ObjectPath
& expected_result
,
156 const dbus::ObjectPath
& result
);
158 static void ExpectBoolResultWithoutStatus(
159 bool expected_result
,
162 static void ExpectStringResultWithoutStatus(
163 const std::string
& expected_result
,
164 const std::string
& result
);
166 // Checks the result and expects the call status to be SUCCESS.
167 static void ExpectDictionaryValueResult(
168 const base::DictionaryValue
* expected_result
,
169 DBusMethodCallStatus call_status
,
170 const base::DictionaryValue
& result
);
172 // Expects the |expected_result| to match the |result|.
173 static void ExpectDictionaryValueResultWithoutStatus(
174 const base::DictionaryValue
* expected_result
,
175 const base::DictionaryValue
& result
);
177 // A message loop to emulate asynchronous behavior.
178 MessageLoop message_loop_
;
180 scoped_refptr
<dbus::MockBus
> mock_bus_
;
183 // Checks the requested interface name and signal name.
184 // Used to implement the mock proxy.
185 void OnConnectToSignal(
186 const std::string
& interface_name
,
187 const std::string
& signal_name
,
188 const dbus::ObjectProxy::SignalCallback
& signal_callback
,
189 const dbus::ObjectProxy::OnConnectedCallback
& on_connected_callback
);
191 // Checks the content of the method call and returns the response.
192 // Used to implement the mock proxy.
194 dbus::MethodCall
* method_call
,
196 const dbus::ObjectProxy::ResponseCallback
& response_callback
);
198 // Checks the content of the method call and returns the response.
199 // Used to implement the mock proxy.
200 void OnCallMethodWithErrorCallback(
201 dbus::MethodCall
* method_call
,
203 const dbus::ObjectProxy::ResponseCallback
& response_callback
,
204 const dbus::ObjectProxy::ErrorCallback
& error_callback
);
206 // Checks the content of the method call and returns the response.
207 // Used to implement the mock proxy.
208 dbus::Response
* OnCallMethodAndBlock(dbus::MethodCall
* method_call
,
211 // The interface name.
212 const std::string interface_name_
;
214 const dbus::ObjectPath object_path_
;
215 // The mock object proxy.
216 scoped_refptr
<dbus::MockObjectProxy
> mock_proxy_
;
217 // The PropertyChanged signal handler given by the tested client.
218 dbus::ObjectProxy::SignalCallback property_changed_handler_
;
219 // The name of the method which is expected to be called.
220 std::string expected_method_name_
;
221 // The response which the mock object proxy returns.
222 dbus::Response
* response_
;
223 // A callback to intercept and check the method call arguments.
224 ArgumentCheckCallback argument_checker_
;
227 } // namespace chromeos
229 #endif // CHROMEOS_DBUS_SHILL_CLIENT_UNITTEST_BASE_H_