Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chromeos / dbus / shill_client_unittest_base.h
blob9b324907a207958156026ac69124f4e18bef8b1b
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_
8 #include <string>
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/message_loop/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 "chromeos/dbus/shill_third_party_vpn_observer.h"
17 #include "dbus/mock_bus.h"
18 #include "dbus/mock_object_proxy.h"
19 #include "dbus/object_proxy.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 using ::testing::MatcherInterface;
23 using ::testing::MatchResultListener;
24 using ::testing::Matcher;
25 using ::testing::MakeMatcher;
27 namespace base {
29 class Value;
30 class DictionaryValue;
32 } // namespace base
34 namespace dbus {
36 class MessageReader;
38 } // namespace dbus
40 namespace chromeos {
42 // A gmock matcher for base::Value types, so we can match them in expectations.
43 class ValueMatcher : public MatcherInterface<const base::Value&> {
44 public:
45 explicit ValueMatcher(const base::Value& value);
47 // MatcherInterface overrides.
48 bool MatchAndExplain(const base::Value& value,
49 MatchResultListener* listener) const override;
50 void DescribeTo(::std::ostream* os) const override;
51 void DescribeNegationTo(::std::ostream* os) const override;
53 private:
54 scoped_ptr<base::Value> expected_value_;
57 inline Matcher<const base::Value&> ValueEq(const base::Value& expected_value) {
58 return MakeMatcher(new ValueMatcher(expected_value));
61 // A class to provide functionalities needed for testing Shill D-Bus clients.
62 class ShillClientUnittestBase : public testing::Test {
63 public:
64 // A mock Closure.
65 class MockClosure {
66 public:
67 MockClosure();
68 ~MockClosure();
69 MOCK_METHOD0(Run, void());
70 base::Closure GetCallback();
73 class MockListValueCallback {
74 public:
75 MockListValueCallback();
76 ~MockListValueCallback();
77 MOCK_METHOD1(Run, void(const base::ListValue& list));
78 ShillClientHelper::ListValueCallback GetCallback();
81 // A mock ErrorCallback.
82 class MockErrorCallback {
83 public:
84 MockErrorCallback();
85 ~MockErrorCallback();
86 MOCK_METHOD2(Run, void(const std::string& error_name,
87 const std::string& error_message));
88 ShillClientHelper::ErrorCallback GetCallback();
91 // A mock PropertyChangedObserver that can be used to check expected values.
92 class MockPropertyChangeObserver
93 : public ShillPropertyChangedObserver {
94 public:
95 MockPropertyChangeObserver();
96 ~MockPropertyChangeObserver();
97 MOCK_METHOD2(OnPropertyChanged, void(const std::string& name,
98 const base::Value& value));
101 explicit ShillClientUnittestBase(const std::string& interface_name,
102 const dbus::ObjectPath& object_path);
103 ~ShillClientUnittestBase() override;
105 void SetUp() override;
106 void TearDown() override;
108 protected:
109 // A callback to intercept and check the method call arguments.
110 typedef base::Callback<void(
111 dbus::MessageReader* reader)> ArgumentCheckCallback;
113 // Sets expectations for called method name and arguments, and sets response.
114 void PrepareForMethodCall(const std::string& method_name,
115 const ArgumentCheckCallback& argument_checker,
116 dbus::Response* response);
118 // Sends platform message signal to the tested client.
119 void SendPlatformMessageSignal(dbus::Signal* signal);
121 // Sends packet received signal to the tested client.
122 void SendPacketReceievedSignal(dbus::Signal* signal);
124 // Sends property changed signal to the tested client.
125 void SendPropertyChangedSignal(dbus::Signal* signal);
127 // Checks the name and the value which are sent by PropertyChanged signal.
128 static void ExpectPropertyChanged(const std::string& expected_name,
129 const base::Value* expected_value,
130 const std::string& name,
131 const base::Value& value);
133 // Expects the reader to be empty.
134 static void ExpectNoArgument(dbus::MessageReader* reader);
136 // Expects the reader to have a uint32_t
137 static void ExpectUint32Argument(uint32_t expected_value,
138 dbus::MessageReader* reader);
140 // Expects the reader to have an array of bytes
141 static void ExpectArrayOfBytesArgument(
142 const std::string& expected_bytes,
143 dbus::MessageReader* reader);
145 // Expects the reader to have a string.
146 static void ExpectStringArgument(const std::string& expected_string,
147 dbus::MessageReader* reader);
149 static void ExpectArrayOfStringsArgument(
150 const std::vector<std::string>& expected_strings,
151 dbus::MessageReader* reader);
153 // Expects the reader to have a Value.
154 static void ExpectValueArgument(const base::Value* expected_value,
155 dbus::MessageReader* reader);
157 // Expects the reader to have a string and a Value.
158 static void ExpectStringAndValueArguments(const std::string& expected_string,
159 const base::Value* expected_value,
160 dbus::MessageReader* reader);
162 // Expects the reader to have a string-to-variant dictionary.
163 static void ExpectDictionaryValueArgument(
164 const base::DictionaryValue* expected_dictionary,
165 bool string_valued,
166 dbus::MessageReader* reader);
168 // Creates a DictionaryValue with example Service properties. The caller owns
169 // the result.
170 static base::DictionaryValue* CreateExampleServiceProperties();
172 // Expects the call status to be SUCCESS.
173 static void ExpectNoResultValue(DBusMethodCallStatus call_status);
175 // Checks the result and expects the call status to be SUCCESS.
176 static void ExpectObjectPathResult(const dbus::ObjectPath& expected_result,
177 DBusMethodCallStatus call_status,
178 const dbus::ObjectPath& result);
180 static void ExpectObjectPathResultWithoutStatus(
181 const dbus::ObjectPath& expected_result,
182 const dbus::ObjectPath& result);
184 static void ExpectBoolResultWithoutStatus(
185 bool expected_result,
186 bool result);
188 static void ExpectStringResultWithoutStatus(
189 const std::string& expected_result,
190 const std::string& result);
192 // Checks the result and expects the call status to be SUCCESS.
193 static void ExpectDictionaryValueResult(
194 const base::DictionaryValue* expected_result,
195 DBusMethodCallStatus call_status,
196 const base::DictionaryValue& result);
198 // Expects the |expected_result| to match the |result|.
199 static void ExpectDictionaryValueResultWithoutStatus(
200 const base::DictionaryValue* expected_result,
201 const base::DictionaryValue& result);
203 // A message loop to emulate asynchronous behavior.
204 base::MessageLoop message_loop_;
205 // The mock bus.
206 scoped_refptr<dbus::MockBus> mock_bus_;
208 private:
209 // Checks the requested interface name and signal name.
210 // Used to implement the mock proxy.
211 void OnConnectToPlatformMessage(
212 const std::string& interface_name,
213 const std::string& signal_name,
214 const dbus::ObjectProxy::SignalCallback& signal_callback,
215 const dbus::ObjectProxy::OnConnectedCallback& on_connected_callback);
217 // Checks the requested interface name and signal name.
218 // Used to implement the mock proxy.
219 void OnConnectToPacketReceived(
220 const std::string& interface_name,
221 const std::string& signal_name,
222 const dbus::ObjectProxy::SignalCallback& signal_callback,
223 const dbus::ObjectProxy::OnConnectedCallback& on_connected_callback);
225 // Checks the requested interface name and signal name.
226 // Used to implement the mock proxy.
227 void OnConnectToPropertyChanged(
228 const std::string& interface_name,
229 const std::string& signal_name,
230 const dbus::ObjectProxy::SignalCallback& signal_callback,
231 const dbus::ObjectProxy::OnConnectedCallback& on_connected_callback);
233 // Checks the content of the method call and returns the response.
234 // Used to implement the mock proxy.
235 void OnCallMethod(
236 dbus::MethodCall* method_call,
237 int timeout_ms,
238 const dbus::ObjectProxy::ResponseCallback& response_callback);
240 // Checks the content of the method call and returns the response.
241 // Used to implement the mock proxy.
242 void OnCallMethodWithErrorCallback(
243 dbus::MethodCall* method_call,
244 int timeout_ms,
245 const dbus::ObjectProxy::ResponseCallback& response_callback,
246 const dbus::ObjectProxy::ErrorCallback& error_callback);
248 // The interface name.
249 const std::string interface_name_;
250 // The object path.
251 const dbus::ObjectPath object_path_;
252 // The mock object proxy.
253 scoped_refptr<dbus::MockObjectProxy> mock_proxy_;
254 // The PlatformMessage signal handler given by the tested client.
255 dbus::ObjectProxy::SignalCallback platform_message_handler_;
256 // The PacketReceived signal handler given by the tested client.
257 dbus::ObjectProxy::SignalCallback packet_receieved__handler_;
258 // The PropertyChanged signal handler given by the tested client.
259 dbus::ObjectProxy::SignalCallback property_changed_handler_;
260 // The name of the method which is expected to be called.
261 std::string expected_method_name_;
262 // The response which the mock object proxy returns.
263 dbus::Response* response_;
264 // A callback to intercept and check the method call arguments.
265 ArgumentCheckCallback argument_checker_;
268 } // namespace chromeos
270 #endif // CHROMEOS_DBUS_SHILL_CLIENT_UNITTEST_BASE_H_