dbus_test_utils.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //
  2. // Copyright (C) 2015 The Android Open Source Project
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. #ifndef UPDATE_ENGINE_DBUS_TEST_UTILS_H_
  17. #define UPDATE_ENGINE_DBUS_TEST_UTILS_H_
  18. #include <set>
  19. #include <string>
  20. #include <base/bind.h>
  21. #include <brillo/message_loops/message_loop.h>
  22. #include <gmock/gmock.h>
  23. namespace chromeos_update_engine {
  24. namespace dbus_test_utils {
  25. #define MOCK_SIGNAL_HANDLER_EXPECT_SIGNAL_HANDLER( \
  26. mock_signal_handler, mock_proxy, signal) \
  27. do { \
  28. EXPECT_CALL((mock_proxy), \
  29. Register##signal##SignalHandler(::testing::_, ::testing::_)) \
  30. .WillOnce(::chromeos_update_engine::dbus_test_utils::GrabCallbacks( \
  31. &(mock_signal_handler))); \
  32. } while (false)
  33. template <typename T>
  34. class MockSignalHandler {
  35. public:
  36. MockSignalHandler() = default;
  37. ~MockSignalHandler() {
  38. if (callback_connected_task_ != brillo::MessageLoop::kTaskIdNull)
  39. brillo::MessageLoop::current()->CancelTask(callback_connected_task_);
  40. }
  41. // Returns whether the signal handler is registered.
  42. bool IsHandlerRegistered() const { return signal_callback_ != nullptr; }
  43. const base::Callback<T>& signal_callback() { return *signal_callback_.get(); }
  44. void GrabCallbacks(
  45. const base::Callback<T>& signal_callback,
  46. dbus::ObjectProxy::OnConnectedCallback on_connected_callback) {
  47. signal_callback_.reset(new base::Callback<T>(signal_callback));
  48. on_connected_callback_.reset(
  49. new dbus::ObjectProxy::OnConnectedCallback(on_connected_callback));
  50. // Notify from the main loop that the callback was connected.
  51. callback_connected_task_ = brillo::MessageLoop::current()->PostTask(
  52. FROM_HERE,
  53. base::Bind(&MockSignalHandler<T>::OnCallbackConnected,
  54. base::Unretained(this)));
  55. }
  56. private:
  57. void OnCallbackConnected() {
  58. callback_connected_task_ = brillo::MessageLoop::kTaskIdNull;
  59. on_connected_callback_->Run("", "", true);
  60. }
  61. brillo::MessageLoop::TaskId callback_connected_task_{
  62. brillo::MessageLoop::kTaskIdNull};
  63. std::unique_ptr<base::Callback<T>> signal_callback_;
  64. std::unique_ptr<dbus::ObjectProxy::OnConnectedCallback>
  65. on_connected_callback_;
  66. };
  67. // Defines the action that will call MockSignalHandler<T>::GrabCallbacks for the
  68. // right type.
  69. ACTION_P(GrabCallbacks, mock_signal_handler) {
  70. mock_signal_handler->GrabCallbacks(arg0, arg1);
  71. }
  72. } // namespace dbus_test_utils
  73. } // namespace chromeos_update_engine
  74. #endif // UPDATE_ENGINE_DBUS_TEST_UTILS_H_