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 CHROME_BROWSER_CHROMEOS_DBUS_SERVICE_PROVIDER_TEST_HELPER_H_ 6 #define CHROME_BROWSER_CHROMEOS_DBUS_SERVICE_PROVIDER_TEST_HELPER_H_ 7 8 #include <string> 9 10 #include "base/message_loop/message_loop.h" 11 #include "chrome/browser/chromeos/dbus/cros_dbus_service.h" 12 #include "dbus/mock_exported_object.h" 13 #include "dbus/mock_object_proxy.h" 14 #include "dbus/object_proxy.h" 15 #include "testing/gmock/include/gmock/gmock.h" 16 #include "testing/gtest/include/gtest/gtest.h" 17 18 namespace dbus { 19 20 class MockBus; 21 22 } // namespace dbus 23 24 namespace chromeos { 25 26 // Helps to implement |CrosDBusService::ServiceProviderInterface| unittests. 27 // Setups mocking of dbus classes. 28 // Class can test only one method call in time. SetUp() must be called before 29 // testing new call to the same method or different method. 30 // 31 // Sample usage: 32 // ServiceProviderTestHelper helper; 33 // helper.Setup(...); 34 // helper.SetUpReturnSignal(...); // optional. 35 // helper.CallMethod(...); 36 // helper.TearDown(); 37 class ServiceProviderTestHelper { 38 public: 39 ServiceProviderTestHelper(); 40 ~ServiceProviderTestHelper(); 41 42 // Sets up helper. Should be called before |CallMethod()|. 43 void SetUp(const std::string& exported_method_name, 44 CrosDBusService::ServiceProviderInterface* service_provider); 45 46 // Setups return signal callback. It's optional and don't need to be called 47 // if tested method doesn't use signal to return results. 48 void SetUpReturnSignal( 49 const std::string& interface_name, 50 const std::string& signal_name, 51 dbus::ObjectProxy::SignalCallback signal_callback, 52 dbus::ObjectProxy::OnConnectedCallback on_connected_callback); 53 54 // Calls tested dbus method. 55 scoped_ptr<dbus::Response> CallMethod(dbus::MethodCall* method_call); 56 57 // Cleanups helper. Should be called after |CallMethod()|. 58 void TearDown(); 59 60 private: 61 // Behaves as |mock_exported_object_|'s ExportMethod(). 62 void MockExportMethod( 63 const std::string& interface_name, 64 const std::string& method_name, 65 dbus::ExportedObject::MethodCallCallback method_callback, 66 dbus::ExportedObject::OnExportedCallback on_exported_callback); 67 68 // Calls exported method and waits for a response for |mock_object_proxy_|. 69 dbus::Response* MockCallMethodAndBlock( 70 dbus::MethodCall* method_call, 71 ::testing::Unused); 72 73 // Behaves as |mock_object_proxy_|'s ConnectToSignal(). 74 void MockConnectToSignal( 75 const std::string& interface_name, 76 const std::string& signal_name, 77 dbus::ObjectProxy::SignalCallback signal_callback, 78 dbus::ObjectProxy::OnConnectedCallback connected_callback); 79 80 // Behaves as |mock_exported_object_|'s SendSignal(). 81 void MockSendSignal(dbus::Signal* signal); 82 83 // Receives a response and makes it available to MockCallMethodAndBlock(). 84 void OnResponse(scoped_ptr<dbus::Response> response); 85 86 scoped_refptr<dbus::MockBus> mock_bus_; 87 scoped_refptr<dbus::MockExportedObject> mock_exported_object_; 88 scoped_refptr<dbus::MockObjectProxy> mock_object_proxy_; 89 dbus::ExportedObject::MethodCallCallback method_callback_; 90 dbus::ObjectProxy::SignalCallback on_signal_callback_; 91 base::MessageLoop message_loop_; 92 bool response_received_; 93 scoped_ptr<dbus::Response> response_; 94 }; 95 96 } // namespace chromeos 97 98 #endif // CHROME_BROWSER_CHROMEOS_DBUS_SERVICE_PROVIDER_TEST_HELPER_H_ 99