Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROMEOS_DBUS_SHILL_CLIENT_UNITTEST_BASE_H_ | 5 #ifndef CHROMEOS_DBUS_SHILL_CLIENT_UNITTEST_BASE_H_ |
| 6 #define CHROMEOS_DBUS_SHILL_CLIENT_UNITTEST_BASE_H_ | 6 #define CHROMEOS_DBUS_SHILL_CLIENT_UNITTEST_BASE_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/json/json_writer.h" | |
| 10 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
| 14 #include "base/values.h" | |
| 13 #include "chromeos/dbus/dbus_method_call_status.h" | 15 #include "chromeos/dbus/dbus_method_call_status.h" |
| 14 #include "chromeos/dbus/shill_client_helper.h" | 16 #include "chromeos/dbus/shill_client_helper.h" |
| 15 #include "dbus/mock_bus.h" | 17 #include "dbus/mock_bus.h" |
| 16 #include "dbus/mock_object_proxy.h" | 18 #include "dbus/mock_object_proxy.h" |
| 17 #include "dbus/object_proxy.h" | 19 #include "dbus/object_proxy.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 21 |
| 22 using ::testing::MatcherInterface; | |
| 23 using ::testing::MatchResultListener; | |
| 24 using ::testing::Matcher; | |
| 25 using ::testing::MakeMatcher; | |
| 26 | |
| 20 namespace base { | 27 namespace base { |
| 21 | 28 |
| 22 class Value; | 29 class Value; |
| 23 class DictionaryValue; | 30 class DictionaryValue; |
| 24 | 31 |
| 25 } // namespace base | 32 } // namespace base |
| 26 | 33 |
| 27 namespace dbus { | 34 namespace dbus { |
| 28 | 35 |
| 29 class MessageReader; | 36 class MessageReader; |
| 30 | 37 |
| 31 } // namespace dbus | 38 } // namespace dbus |
| 32 | 39 |
| 33 namespace chromeos { | 40 namespace chromeos { |
| 34 | 41 |
| 42 // A gmock matcher for base::Value types, so we can match them in expectations. | |
| 43 class ValueMatcher | |
| 44 : public MatcherInterface<const base::Value&> { | |
| 45 public: | |
| 46 explicit ValueMatcher(const base::Value& value) | |
| 47 : expected_value_(value.DeepCopy()) {} | |
| 48 | |
| 49 virtual bool MatchAndExplain(const base::Value& value, | |
| 50 MatchResultListener* listener) const { | |
|
hashimoto
2012/09/21 11:52:01
Could you move these method definitions to .cc fil
Greg Spencer (Chromium)
2012/09/21 22:03:47
Done.
| |
| 51 return expected_value_->Equals(&value); | |
| 52 } | |
| 53 | |
| 54 virtual void DescribeTo(::std::ostream* os) const { | |
| 55 std::string expected_value_str; | |
| 56 base::JSONWriter::WriteWithOptions(expected_value_.get(), | |
| 57 base::JSONWriter::OPTIONS_PRETTY_PRINT, | |
| 58 &expected_value_str); | |
| 59 *os << "value equals " << expected_value_str; | |
| 60 } | |
| 61 | |
| 62 virtual void DescribeNegationTo(::std::ostream* os) const { | |
| 63 std::string expected_value_str; | |
| 64 base::JSONWriter::WriteWithOptions(expected_value_.get(), | |
| 65 base::JSONWriter::OPTIONS_PRETTY_PRINT, | |
| 66 &expected_value_str); | |
| 67 *os << "value does not equal " << expected_value_str; | |
| 68 } | |
| 69 private: | |
| 70 scoped_ptr<base::Value> expected_value_; | |
| 71 }; | |
| 72 | |
| 73 inline Matcher<const base::Value&> ValueEq(const base::Value& expected_value) { | |
| 74 return MakeMatcher(new ValueMatcher(expected_value)); | |
| 75 } | |
| 76 | |
| 35 // A class to provide functionalities needed for testing Shill D-Bus clients. | 77 // A class to provide functionalities needed for testing Shill D-Bus clients. |
| 36 class ShillClientUnittestBase : public testing::Test { | 78 class ShillClientUnittestBase : public testing::Test { |
| 37 public: | 79 public: |
| 38 // A mock Closure. | 80 // A mock Closure. |
| 39 class MockClosure { | 81 class MockClosure { |
| 40 public: | 82 public: |
| 41 MockClosure(); | 83 MockClosure(); |
| 42 ~MockClosure(); | 84 ~MockClosure(); |
| 43 MOCK_METHOD0(Run, void()); | 85 MOCK_METHOD0(Run, void()); |
| 44 base::Closure GetCallback(); | 86 base::Closure GetCallback(); |
| 45 }; | 87 }; |
| 46 | 88 |
| 47 // A mock ErrorCallback. | 89 // A mock ErrorCallback. |
| 48 class MockErrorCallback { | 90 class MockErrorCallback { |
| 49 public: | 91 public: |
| 50 MockErrorCallback(); | 92 MockErrorCallback(); |
| 51 ~MockErrorCallback(); | 93 ~MockErrorCallback(); |
| 52 MOCK_METHOD2(Run, void(const std::string& error_name, | 94 MOCK_METHOD2(Run, void(const std::string& error_name, |
| 53 const std::string& error_mesage)); | 95 const std::string& error_mesage)); |
| 54 ShillClientHelper::ErrorCallback GetCallback(); | 96 ShillClientHelper::ErrorCallback GetCallback(); |
| 55 }; | 97 }; |
| 56 | 98 |
| 99 // A mock PropertyChangedObserver that can be used to check expected values. | |
| 100 class MockPropertyChangeObserver | |
| 101 : public ShillClientHelper::PropertyChangedObserver { | |
| 102 public: | |
| 103 MockPropertyChangeObserver(); | |
| 104 ~MockPropertyChangeObserver(); | |
| 105 MOCK_METHOD2(OnPropertyChanged, void(const std::string& name, | |
| 106 const base::Value& value)); | |
| 107 }; | |
| 108 | |
| 57 explicit ShillClientUnittestBase(const std::string& interface_name, | 109 explicit ShillClientUnittestBase(const std::string& interface_name, |
| 58 const dbus::ObjectPath& object_path); | 110 const dbus::ObjectPath& object_path); |
| 59 virtual ~ShillClientUnittestBase(); | 111 virtual ~ShillClientUnittestBase(); |
| 60 | 112 |
| 61 virtual void SetUp() OVERRIDE; | 113 virtual void SetUp() OVERRIDE; |
| 62 virtual void TearDown() OVERRIDE; | 114 virtual void TearDown() OVERRIDE; |
| 63 | 115 |
| 64 protected: | 116 protected: |
| 65 // A callback to intercept and check the method call arguments. | 117 // A callback to intercept and check the method call arguments. |
| 66 typedef base::Callback<void( | 118 typedef base::Callback<void( |
| 67 dbus::MessageReader* reader)> ArgumentCheckCallback; | 119 dbus::MessageReader* reader)> ArgumentCheckCallback; |
| 68 | 120 |
| 69 // Sets expectations for called method name and arguments, and sets response. | 121 // Sets expectations for called method name and arguments, and sets response. |
| 70 void PrepareForMethodCall(const std::string& method_name, | 122 void PrepareForMethodCall(const std::string& method_name, |
| 71 const ArgumentCheckCallback& argument_checker, | 123 const ArgumentCheckCallback& argument_checker, |
| 72 dbus::Response* response); | 124 dbus::Response* response); |
| 73 | 125 |
| 74 // Sends property changed signal to the tested client. | 126 // Sends property changed signal to the tested client. |
| 75 void SendPropertyChangedSignal(dbus::Signal* signal); | 127 void SendPropertyChangedSignal(dbus::Signal* signal); |
| 76 | 128 |
| 77 // Checks the name and the value which are sent by PropertyChanged signal. | |
| 78 static void ExpectPropertyChanged(const std::string& expected_name, | |
| 79 const base::Value* expected_value, | |
| 80 const std::string& name, | |
| 81 const base::Value& value); | |
| 82 | |
| 83 // Expects the reader to be empty. | 129 // Expects the reader to be empty. |
| 84 static void ExpectNoArgument(dbus::MessageReader* reader); | 130 static void ExpectNoArgument(dbus::MessageReader* reader); |
| 85 | 131 |
| 86 // Expects the reader to have a string. | 132 // Expects the reader to have a string. |
| 87 static void ExpectStringArgument(const std::string& expected_string, | 133 static void ExpectStringArgument(const std::string& expected_string, |
| 88 dbus::MessageReader* reader); | 134 dbus::MessageReader* reader); |
| 89 | 135 |
| 90 // Expects the reader to have a Value. | 136 // Expects the reader to have a Value. |
| 91 static void ExpectValueArgument(const base::Value* expected_value, | 137 static void ExpectValueArgument(const base::Value* expected_value, |
| 92 dbus::MessageReader* reader); | 138 dbus::MessageReader* reader); |
| 93 | 139 |
| 94 // Expects the reader to have a string and a Value. | 140 // Expects the reader to have a string and a Value. |
| 95 static void ExpectStringAndValueArguments(const std::string& expected_string, | 141 static void ExpectStringAndValueArguments(const std::string& expected_string, |
| 96 const base::Value* expected_value, | 142 const base::Value* expected_value, |
| 97 dbus::MessageReader* reader); | 143 dbus::MessageReader* reader); |
| 98 | 144 |
| 99 // Expects the call status to be SUCCESS. | 145 // Expects the call status to be SUCCESS. |
| 100 static void ExpectNoResultValue(DBusMethodCallStatus call_status); | 146 static void ExpectNoResultValue(DBusMethodCallStatus call_status); |
| 101 | 147 |
| 102 // Checks the result and expects the call status to be SUCCESS. | 148 // Checks the result and expects the call status to be SUCCESS. |
| 103 static void ExpectObjectPathResult(const dbus::ObjectPath& expected_result, | 149 static void ExpectObjectPathResult(const dbus::ObjectPath& expected_result, |
| 104 DBusMethodCallStatus call_status, | 150 DBusMethodCallStatus call_status, |
| 105 const dbus::ObjectPath& result); | 151 const dbus::ObjectPath& result); |
| 106 | 152 |
| 107 // Checks the result and expects the call status to be SUCCESS. | 153 // Checks the result and expects the call status to be SUCCESS. |
| 108 static void ExpectDictionaryValueResult( | 154 static void ExpectDictionaryValueResult( |
| 109 const base::DictionaryValue* expected_result, | 155 const base::DictionaryValue* expected_result, |
| 110 DBusMethodCallStatus call_status, | 156 DBusMethodCallStatus call_status, |
| 111 const base::DictionaryValue& result); | 157 const base::DictionaryValue& result); |
| 158 static void ExpectDictionaryValueResultWithoutStatus( | |
| 159 const base::DictionaryValue* expected_result, | |
| 160 const base::DictionaryValue& result); | |
| 112 | 161 |
| 113 // A message loop to emulate asynchronous behavior. | 162 // A message loop to emulate asynchronous behavior. |
| 114 MessageLoop message_loop_; | 163 MessageLoop message_loop_; |
| 115 // The mock bus. | 164 // The mock bus. |
| 116 scoped_refptr<dbus::MockBus> mock_bus_; | 165 scoped_refptr<dbus::MockBus> mock_bus_; |
| 117 | 166 |
| 118 private: | 167 private: |
| 119 // Checks the requested interface name and signal name. | 168 // Checks the requested interface name and signal name. |
| 120 // Used to implement the mock proxy. | 169 // Used to implement the mock proxy. |
| 121 void OnConnectToSignal( | 170 void OnConnectToSignal( |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 156 std::string expected_method_name_; | 205 std::string expected_method_name_; |
| 157 // The response which the mock object proxy returns. | 206 // The response which the mock object proxy returns. |
| 158 dbus::Response* response_; | 207 dbus::Response* response_; |
| 159 // A callback to intercept and check the method call arguments. | 208 // A callback to intercept and check the method call arguments. |
| 160 ArgumentCheckCallback argument_checker_; | 209 ArgumentCheckCallback argument_checker_; |
| 161 }; | 210 }; |
| 162 | 211 |
| 163 } // namespace chromeos | 212 } // namespace chromeos |
| 164 | 213 |
| 165 #endif // CHROMEOS_DBUS_SHILL_CLIENT_UNITTEST_BASE_H_ | 214 #endif // CHROMEOS_DBUS_SHILL_CLIENT_UNITTEST_BASE_H_ |
| OLD | NEW |