Home | History | Annotate | Download | only in message_loops
      1 // Copyright 2015 The Chromium OS 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 LIBBRILLO_BRILLO_MESSAGE_LOOPS_MOCK_MESSAGE_LOOP_H_
      6 #define LIBBRILLO_BRILLO_MESSAGE_LOOPS_MOCK_MESSAGE_LOOP_H_
      7 
      8 #include <gmock/gmock.h>
      9 
     10 #include <base/location.h>
     11 #include <base/test/simple_test_clock.h>
     12 #include <base/time/time.h>
     13 
     14 #include <brillo/brillo_export.h>
     15 #include <brillo/message_loops/fake_message_loop.h>
     16 #include <brillo/message_loops/message_loop.h>
     17 
     18 namespace brillo {
     19 
     20 // The MockMessageLoop is a mockable MessageLoop that will by default act as a
     21 // FakeMessageLoop. It is possible to set expectations with EXPECT_CALL without
     22 // any action associated and they will call the same methods in the underlying
     23 // FakeMessageLoop implementation.
     24 // This message loop implementation is useful to check interaction with the
     25 // message loop when running unittests.
     26 class BRILLO_EXPORT MockMessageLoop : public MessageLoop {
     27  public:
     28   // Create a FakeMessageLoop optionally using a SimpleTestClock to update the
     29   // time when Run() or RunOnce(true) are called and should block.
     30   explicit MockMessageLoop(base::SimpleTestClock* clock)
     31     : fake_loop_(clock) {
     32     // Redirect all actions to calling the underlying FakeMessageLoop by
     33     // default. For the overloaded methods, we need to disambiguate between the
     34     // different options by specifying the type of the method pointer.
     35     ON_CALL(*this, PostDelayedTask(::testing::_, ::testing::_, ::testing::_))
     36       .WillByDefault(::testing::Invoke(
     37           &fake_loop_,
     38           static_cast<TaskId(FakeMessageLoop::*)(
     39                       const tracked_objects::Location&,
     40                       const base::Closure&,
     41                       base::TimeDelta)>(
     42               &FakeMessageLoop::PostDelayedTask)));
     43     ON_CALL(*this, WatchFileDescriptor(
     44         ::testing::_, ::testing::_, ::testing::_, ::testing::_, ::testing::_))
     45       .WillByDefault(::testing::Invoke(
     46           &fake_loop_,
     47           static_cast<TaskId(FakeMessageLoop::*)(
     48                       const tracked_objects::Location&, int, WatchMode, bool,
     49                       const base::Closure&)>(
     50               &FakeMessageLoop::WatchFileDescriptor)));
     51     ON_CALL(*this, CancelTask(::testing::_))
     52       .WillByDefault(::testing::Invoke(&fake_loop_,
     53                                        &FakeMessageLoop::CancelTask));
     54     ON_CALL(*this, RunOnce(::testing::_))
     55       .WillByDefault(::testing::Invoke(&fake_loop_,
     56                                        &FakeMessageLoop::RunOnce));
     57   }
     58   ~MockMessageLoop() override = default;
     59 
     60   MOCK_METHOD3(PostDelayedTask,
     61                TaskId(const tracked_objects::Location& from_here,
     62                       const base::Closure& task,
     63                       base::TimeDelta delay));
     64   using MessageLoop::PostDelayedTask;
     65   MOCK_METHOD5(WatchFileDescriptor,
     66                TaskId(const tracked_objects::Location& from_here,
     67                       int fd,
     68                       WatchMode mode,
     69                       bool persistent,
     70                       const base::Closure& task));
     71   using MessageLoop::WatchFileDescriptor;
     72   MOCK_METHOD1(CancelTask, bool(TaskId task_id));
     73   MOCK_METHOD1(RunOnce, bool(bool may_block));
     74 
     75   // Returns the actual FakeMessageLoop instance so default actions can be
     76   // override with other actions or call
     77   FakeMessageLoop* fake_loop() {
     78     return &fake_loop_;
     79   }
     80 
     81  private:
     82   FakeMessageLoop fake_loop_;
     83 
     84   DISALLOW_COPY_AND_ASSIGN(MockMessageLoop);
     85 };
     86 
     87 }  // namespace brillo
     88 
     89 #endif  // LIBBRILLO_BRILLO_MESSAGE_LOOPS_MOCK_MESSAGE_LOOP_H_
     90