Fix compilation error triggered with -Werror=sign-compare
[chromium-blink-merge.git] / chromeos / dbus / shill_client_unittest_base.h
blobb362e38d902b2796548717d3b4e8d1f77f8fd5e0
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 "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;
26 namespace base {
28 class Value;
29 class DictionaryValue;
31 } // namespace base
33 namespace dbus {
35 class MessageReader;
37 } // namespace dbus
39 namespace chromeos {
41 // A gmock matcher for base::Value types, so we can match them in expectations.
42 class ValueMatcher : public MatcherInterface<const base::Value&> {
43 public:
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;
52 private:
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 {
62 public:
63 // A mock Closure.
64 class MockClosure {
65 public:
66 MockClosure();
67 ~MockClosure();
68 MOCK_METHOD0(Run, void());
69 base::Closure GetCallback();
72 class MockListValueCallback {
73 public:
74 MockListValueCallback();
75 ~MockListValueCallback();
76 MOCK_METHOD1(Run, void(const base::ListValue& list));
77 ShillClientHelper::ListValueCallback GetCallback();
80 // A mock ErrorCallback.
81 class MockErrorCallback {
82 public:
83 MockErrorCallback();
84 ~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 {
93 public:
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;
107 protected:
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 reader to have a string-to-variant dictionary.
147 static void ExpectDictionaryValueArgument(
148 const base::DictionaryValue* expected_dictionary,
149 dbus::MessageReader* reader);
151 // Creates a DictionaryValue with example Service properties. The caller owns
152 // the result.
153 static base::DictionaryValue* CreateExampleServiceProperties();
155 // Expects the call status to be SUCCESS.
156 static void ExpectNoResultValue(DBusMethodCallStatus call_status);
158 // Checks the result and expects the call status to be SUCCESS.
159 static void ExpectObjectPathResult(const dbus::ObjectPath& expected_result,
160 DBusMethodCallStatus call_status,
161 const dbus::ObjectPath& result);
163 static void ExpectObjectPathResultWithoutStatus(
164 const dbus::ObjectPath& expected_result,
165 const dbus::ObjectPath& result);
167 static void ExpectBoolResultWithoutStatus(
168 bool expected_result,
169 bool result);
171 static void ExpectStringResultWithoutStatus(
172 const std::string& expected_result,
173 const std::string& result);
175 // Checks the result and expects the call status to be SUCCESS.
176 static void ExpectDictionaryValueResult(
177 const base::DictionaryValue* expected_result,
178 DBusMethodCallStatus call_status,
179 const base::DictionaryValue& result);
181 // Expects the |expected_result| to match the |result|.
182 static void ExpectDictionaryValueResultWithoutStatus(
183 const base::DictionaryValue* expected_result,
184 const base::DictionaryValue& result);
186 // A message loop to emulate asynchronous behavior.
187 base::MessageLoop message_loop_;
188 // The mock bus.
189 scoped_refptr<dbus::MockBus> mock_bus_;
191 private:
192 // Checks the requested interface name and signal name.
193 // Used to implement the mock proxy.
194 void OnConnectToSignal(
195 const std::string& interface_name,
196 const std::string& signal_name,
197 const dbus::ObjectProxy::SignalCallback& signal_callback,
198 const dbus::ObjectProxy::OnConnectedCallback& on_connected_callback);
200 // Checks the content of the method call and returns the response.
201 // Used to implement the mock proxy.
202 void OnCallMethod(
203 dbus::MethodCall* method_call,
204 int timeout_ms,
205 const dbus::ObjectProxy::ResponseCallback& response_callback);
207 // Checks the content of the method call and returns the response.
208 // Used to implement the mock proxy.
209 void OnCallMethodWithErrorCallback(
210 dbus::MethodCall* method_call,
211 int timeout_ms,
212 const dbus::ObjectProxy::ResponseCallback& response_callback,
213 const dbus::ObjectProxy::ErrorCallback& error_callback);
215 // The interface name.
216 const std::string interface_name_;
217 // The object path.
218 const dbus::ObjectPath object_path_;
219 // The mock object proxy.
220 scoped_refptr<dbus::MockObjectProxy> mock_proxy_;
221 // The PropertyChanged signal handler given by the tested client.
222 dbus::ObjectProxy::SignalCallback property_changed_handler_;
223 // The name of the method which is expected to be called.
224 std::string expected_method_name_;
225 // The response which the mock object proxy returns.
226 dbus::Response* response_;
227 // A callback to intercept and check the method call arguments.
228 ArgumentCheckCallback argument_checker_;
231 } // namespace chromeos
233 #endif // CHROMEOS_DBUS_SHILL_CLIENT_UNITTEST_BASE_H_