blob: b3748ceafc277ba7373dabdbdd7fc1df7b949f4a [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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//
Alex Deymo30534502015-07-20 15:06:33 -070016
17#ifndef UPDATE_ENGINE_DBUS_TEST_UTILS_H_
18#define UPDATE_ENGINE_DBUS_TEST_UTILS_H_
19
20#include <set>
21#include <string>
22
23#include <base/bind.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070024#include <brillo/message_loops/message_loop.h>
Alex Deymo30534502015-07-20 15:06:33 -070025#include <gmock/gmock.h>
26
27namespace chromeos_update_engine {
28namespace dbus_test_utils {
29
30#define MOCK_SIGNAL_HANDLER_EXPECT_SIGNAL_HANDLER( \
31 mock_signal_handler, mock_proxy, signal) \
32 do { \
33 EXPECT_CALL((mock_proxy), \
34 Register##signal##SignalHandler(::testing::_, ::testing::_)) \
35 .WillOnce(::chromeos_update_engine::dbus_test_utils::GrabCallbacks( \
36 &(mock_signal_handler))); \
37 } while (false)
38
39template <typename T>
40class MockSignalHandler {
41 public:
42 MockSignalHandler() = default;
43 ~MockSignalHandler() {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070044 if (callback_connected_task_ != brillo::MessageLoop::kTaskIdNull)
45 brillo::MessageLoop::current()->CancelTask(callback_connected_task_);
Alex Deymo30534502015-07-20 15:06:33 -070046 }
47
48 // Returns whether the signal handler is registered.
49 bool IsHandlerRegistered() const { return signal_callback_ != nullptr; }
50
51 const base::Callback<T>& signal_callback() { return *signal_callback_.get(); }
52
53 void GrabCallbacks(
54 const base::Callback<T>& signal_callback,
55 dbus::ObjectProxy::OnConnectedCallback on_connected_callback) {
56 signal_callback_.reset(new base::Callback<T>(signal_callback));
57 on_connected_callback_.reset(
58 new dbus::ObjectProxy::OnConnectedCallback(on_connected_callback));
59 // Notify from the main loop that the callback was connected.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070060 callback_connected_task_ = brillo::MessageLoop::current()->PostTask(
Alex Deymo30534502015-07-20 15:06:33 -070061 FROM_HERE,
62 base::Bind(&MockSignalHandler<T>::OnCallbackConnected,
63 base::Unretained(this)));
64 }
65
66 private:
67 void OnCallbackConnected() {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070068 callback_connected_task_ = brillo::MessageLoop::kTaskIdNull;
Alex Deymo30534502015-07-20 15:06:33 -070069 on_connected_callback_->Run("", "", true);
70 }
71
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070072 brillo::MessageLoop::TaskId callback_connected_task_{
73 brillo::MessageLoop::kTaskIdNull};
Alex Deymo30534502015-07-20 15:06:33 -070074
75 std::unique_ptr<base::Callback<T>> signal_callback_;
76 std::unique_ptr<dbus::ObjectProxy::OnConnectedCallback>
77 on_connected_callback_;
78};
79
80// Defines the action that will call MockSignalHandler<T>::GrabCallbacks for the
81// right type.
82ACTION_P(GrabCallbacks, mock_signal_handler) {
83 mock_signal_handler->GrabCallbacks(arg0, arg1);
84}
85
86} // namespace dbus_test_utils
87} // namespace chromeos_update_engine
88
89#endif // UPDATE_ENGINE_DBUS_TEST_UTILS_H_