Home | History | Annotate | Download | only in update_engine
      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_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>
     24 #include <brillo/message_loops/message_loop.h>
     25 #include <gmock/gmock.h>
     26 
     27 namespace chromeos_update_engine {
     28 namespace 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 
     39 template <typename T>
     40 class MockSignalHandler {
     41  public:
     42   MockSignalHandler() = default;
     43   ~MockSignalHandler() {
     44     if (callback_connected_task_ != brillo::MessageLoop::kTaskIdNull)
     45       brillo::MessageLoop::current()->CancelTask(callback_connected_task_);
     46   }
     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.
     60     callback_connected_task_ = brillo::MessageLoop::current()->PostTask(
     61         FROM_HERE,
     62         base::Bind(&MockSignalHandler<T>::OnCallbackConnected,
     63                    base::Unretained(this)));
     64   }
     65 
     66  private:
     67   void OnCallbackConnected() {
     68     callback_connected_task_ = brillo::MessageLoop::kTaskIdNull;
     69     on_connected_callback_->Run("", "", true);
     70   }
     71 
     72   brillo::MessageLoop::TaskId callback_connected_task_{
     73       brillo::MessageLoop::kTaskIdNull};
     74 
     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.
     82 ACTION_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_
     90