| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #ifndef CHROMEOS_DBUS_FLIMFLAM_CLIENT_UNITTEST_BASE_H_ | |
| 6 #define CHROMEOS_DBUS_FLIMFLAM_CLIENT_UNITTEST_BASE_H_ | |
| 7 | |
| 8 #include <string> | |
| 9 | |
| 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/flimflam_client_helper.h" | |
| 15 #include "dbus/mock_bus.h" | |
| 16 #include "dbus/mock_object_proxy.h" | |
| 17 #include "dbus/object_proxy.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | |
| 19 | |
| 20 namespace base { | |
| 21 | |
| 22 class Value; | |
| 23 class DictionaryValue; | |
| 24 | |
| 25 } // namespace base | |
| 26 | |
| 27 namespace dbus { | |
| 28 | |
| 29 class MessageReader; | |
| 30 | |
| 31 } // namespace dbus | |
| 32 | |
| 33 namespace chromeos { | |
| 34 | |
| 35 // A class to provide functionalities needed for testing Flimflam D-Bus clients. | |
| 36 class FlimflamClientUnittestBase : public testing::Test { | |
| 37 public: | |
| 38 // A mock Closure. | |
| 39 class MockClosure { | |
| 40 public: | |
| 41 MockClosure(); | |
| 42 ~MockClosure(); | |
| 43 MOCK_METHOD0(Run, void()); | |
| 44 base::Closure GetCallback(); | |
| 45 }; | |
| 46 | |
| 47 // A mock ErrorCallback. | |
| 48 class MockErrorCallback { | |
| 49 public: | |
| 50 MockErrorCallback(); | |
| 51 ~MockErrorCallback(); | |
| 52 MOCK_METHOD2(Run, void(const std::string& error_name, | |
| 53 const std::string& error_mesage)); | |
| 54 FlimflamClientHelper::ErrorCallback GetCallback(); | |
| 55 }; | |
| 56 | |
| 57 explicit FlimflamClientUnittestBase(const std::string& interface_name, | |
| 58 const dbus::ObjectPath& object_path); | |
| 59 virtual ~FlimflamClientUnittestBase(); | |
| 60 | |
| 61 virtual void SetUp() OVERRIDE; | |
| 62 virtual void TearDown() OVERRIDE; | |
| 63 | |
| 64 protected: | |
| 65 // A callback to intercept and check the method call arguments. | |
| 66 typedef base::Callback<void( | |
| 67 dbus::MessageReader* reader)> ArgumentCheckCallback; | |
| 68 | |
| 69 // Sets expectations for called method name and arguments, and sets response. | |
| 70 void PrepareForMethodCall(const std::string& method_name, | |
| 71 const ArgumentCheckCallback& argument_checker, | |
| 72 dbus::Response* response); | |
| 73 | |
| 74 // Sends property changed signal to the tested client. | |
| 75 void SendPropertyChangedSignal(dbus::Signal* signal); | |
| 76 | |
| 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. | |
| 84 static void ExpectNoArgument(dbus::MessageReader* reader); | |
| 85 | |
| 86 // Expects the reader to have a string. | |
| 87 static void ExpectStringArgument(const std::string& expected_string, | |
| 88 dbus::MessageReader* reader); | |
| 89 | |
| 90 // Expects the reader to have a Value. | |
| 91 static void ExpectValueArgument(const base::Value* expected_value, | |
| 92 dbus::MessageReader* reader); | |
| 93 | |
| 94 // Expects the reader to have a string and a Value. | |
| 95 static void ExpectStringAndValueArguments(const std::string& expected_string, | |
| 96 const base::Value* expected_value, | |
| 97 dbus::MessageReader* reader); | |
| 98 | |
| 99 // Expects the call status to be SUCCESS. | |
| 100 static void ExpectNoResultValue(DBusMethodCallStatus call_status); | |
| 101 | |
| 102 // Checks the result and expects the call status to be SUCCESS. | |
| 103 static void ExpectObjectPathResult(const dbus::ObjectPath& expected_result, | |
| 104 DBusMethodCallStatus call_status, | |
| 105 const dbus::ObjectPath& result); | |
| 106 | |
| 107 // Checks the result and expects the call status to be SUCCESS. | |
| 108 static void ExpectDictionaryValueResult( | |
| 109 const base::DictionaryValue* expected_result, | |
| 110 DBusMethodCallStatus call_status, | |
| 111 const base::DictionaryValue& result); | |
| 112 | |
| 113 // A message loop to emulate asynchronous behavior. | |
| 114 MessageLoop message_loop_; | |
| 115 // The mock bus. | |
| 116 scoped_refptr<dbus::MockBus> mock_bus_; | |
| 117 | |
| 118 private: | |
| 119 // Checks the requested interface name and signal name. | |
| 120 // Used to implement the mock proxy. | |
| 121 void OnConnectToSignal( | |
| 122 const std::string& interface_name, | |
| 123 const std::string& signal_name, | |
| 124 const dbus::ObjectProxy::SignalCallback& signal_callback, | |
| 125 const dbus::ObjectProxy::OnConnectedCallback& on_connected_callback); | |
| 126 | |
| 127 // Checks the content of the method call and returns the response. | |
| 128 // Used to implement the mock proxy. | |
| 129 void OnCallMethod( | |
| 130 dbus::MethodCall* method_call, | |
| 131 int timeout_ms, | |
| 132 const dbus::ObjectProxy::ResponseCallback& response_callback); | |
| 133 | |
| 134 // Checks the content of the method call and returns the response. | |
| 135 // Used to implement the mock proxy. | |
| 136 void OnCallMethodWithErrorCallback( | |
| 137 dbus::MethodCall* method_call, | |
| 138 int timeout_ms, | |
| 139 const dbus::ObjectProxy::ResponseCallback& response_callback, | |
| 140 const dbus::ObjectProxy::ErrorCallback& error_callback); | |
| 141 | |
| 142 // Checks the content of the method call and returns the response. | |
| 143 // Used to implement the mock proxy. | |
| 144 dbus::Response* OnCallMethodAndBlock(dbus::MethodCall* method_call, | |
| 145 int timeout_ms); | |
| 146 | |
| 147 // The interface name. | |
| 148 const std::string interface_name_; | |
| 149 // The object path. | |
| 150 const dbus::ObjectPath object_path_; | |
| 151 // The mock object proxy. | |
| 152 scoped_refptr<dbus::MockObjectProxy> mock_proxy_; | |
| 153 // The PropertyChanged signal handler given by the tested client. | |
| 154 dbus::ObjectProxy::SignalCallback property_changed_handler_; | |
| 155 // The name of the method which is expected to be called. | |
| 156 std::string expected_method_name_; | |
| 157 // The response which the mock object proxy returns. | |
| 158 dbus::Response* response_; | |
| 159 // A callback to intercept and check the method call arguments. | |
| 160 ArgumentCheckCallback argument_checker_; | |
| 161 }; | |
| 162 | |
| 163 } // namespace chromeos | |
| 164 | |
| 165 #endif // CHROMEOS_DBUS_FLIMFLAM_CLIENT_UNITTEST_BASE_H_ | |
| OLD | NEW |