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
17 #ifndef UPDATE_ENGINE_CROS_DBUS_TEST_UTILS_H_
18 #define UPDATE_ENGINE_CROS_DBUS_TEST_UTILS_H_
19
20 #include <memory>
21 #include <set>
22 #include <string>
23 #include <utility>
24
25 #include <base/bind.h>
26 #include <brillo/message_loops/message_loop.h>
27 #include <gmock/gmock.h>
28
29 namespace chromeos_update_engine {
30 namespace dbus_test_utils {
31
32 #define MOCK_SIGNAL_HANDLER_EXPECT_SIGNAL_HANDLER( \
33 mock_signal_handler, mock_proxy, signal) \
34 do { \
35 EXPECT_CALL((mock_proxy), \
36 DoRegister##signal##SignalHandler(::testing::_, ::testing::_)) \
37 .WillOnce(::chromeos_update_engine::dbus_test_utils::GrabCallbacks( \
38 &(mock_signal_handler))); \
39 } while (false)
40
41 template <typename T>
42 class MockSignalHandler {
43 public:
44 MockSignalHandler() = default;
~MockSignalHandler()45 ~MockSignalHandler() {
46 if (callback_connected_task_ != brillo::MessageLoop::kTaskIdNull)
47 brillo::MessageLoop::current()->CancelTask(callback_connected_task_);
48 }
49
50 // Returns whether the signal handler is registered.
IsHandlerRegistered()51 bool IsHandlerRegistered() const { return signal_callback_ != nullptr; }
52
signal_callback()53 const base::Callback<T>& signal_callback() { return *signal_callback_.get(); }
54
GrabCallbacks(const base::Callback<T> & signal_callback,dbus::ObjectProxy::OnConnectedCallback * on_connected_callback)55 void GrabCallbacks(
56 const base::Callback<T>& signal_callback,
57 dbus::ObjectProxy::OnConnectedCallback* on_connected_callback) {
58 signal_callback_.reset(new base::Callback<T>(signal_callback));
59 on_connected_callback_.reset(new dbus::ObjectProxy::OnConnectedCallback(
60 std::move(*on_connected_callback)));
61 // Notify from the main loop that the callback was connected.
62 callback_connected_task_ = brillo::MessageLoop::current()->PostTask(
63 FROM_HERE,
64 base::Bind(&MockSignalHandler<T>::OnCallbackConnected,
65 base::Unretained(this)));
66 }
67
68 private:
OnCallbackConnected()69 void OnCallbackConnected() {
70 callback_connected_task_ = brillo::MessageLoop::kTaskIdNull;
71 std::move(*on_connected_callback_).Run("", "", true);
72 }
73
74 brillo::MessageLoop::TaskId callback_connected_task_{
75 brillo::MessageLoop::kTaskIdNull};
76
77 std::unique_ptr<base::Callback<T>> signal_callback_;
78 std::unique_ptr<dbus::ObjectProxy::OnConnectedCallback>
79 on_connected_callback_;
80 };
81
82 // Defines the action that will call MockSignalHandler<T>::GrabCallbacks for the
83 // right type.
ACTION_P(GrabCallbacks,mock_signal_handler)84 ACTION_P(GrabCallbacks, mock_signal_handler) {
85 mock_signal_handler->GrabCallbacks(arg0, arg1);
86 }
87
88 } // namespace dbus_test_utils
89 } // namespace chromeos_update_engine
90
91 #endif // UPDATE_ENGINE_CROS_DBUS_TEST_UTILS_H_
92