aboutsummaryrefslogtreecommitdiff
path: root/brillo/dbus/async_event_sequencer_test.cc
blob: 1026afe3fb1504f3af1ff92010ba3be7bfc3b682 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2014 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <brillo/dbus/async_event_sequencer.h>

#include <base/bind.h>
#include <base/bind_helpers.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>

namespace brillo {

namespace dbus_utils {

namespace {

const char kTestInterface[] = "org.test.if";
const char kTestMethod1[] = "TestMethod1";
const char kTestMethod2[] = "TestMethod2";

}  // namespace

class AsyncEventSequencerTest : public ::testing::Test {
 public:
  MOCK_METHOD(void, HandleCompletion, (bool));

  void SetUp() {
    aec_ = new AsyncEventSequencer();
    cb_ = base::Bind(&AsyncEventSequencerTest::HandleCompletion,
                     base::Unretained(this));
  }

  scoped_refptr<AsyncEventSequencer> aec_;
  AsyncEventSequencer::CompletionAction cb_;
};

TEST_F(AsyncEventSequencerTest, WaitForCompletionActions) {
  auto finished_handler = aec_->GetHandler("handler failed", false);
  finished_handler.Run(true);
  EXPECT_CALL(*this, HandleCompletion(true)).Times(1);
  aec_->OnAllTasksCompletedCall({cb_});
}

TEST_F(AsyncEventSequencerTest, MultiInitActionsSucceed) {
  auto finished_handler1 = aec_->GetHandler("handler failed", false);
  auto finished_handler2 = aec_->GetHandler("handler failed", false);
  aec_->OnAllTasksCompletedCall({cb_});
  finished_handler1.Run(true);
  EXPECT_CALL(*this, HandleCompletion(true)).Times(1);
  finished_handler2.Run(true);
}

TEST_F(AsyncEventSequencerTest, SomeInitActionsFail) {
  auto finished_handler1 = aec_->GetHandler("handler failed", false);
  auto finished_handler2 = aec_->GetHandler("handler failed", false);
  aec_->OnAllTasksCompletedCall({cb_});
  finished_handler1.Run(false);
  EXPECT_CALL(*this, HandleCompletion(false)).Times(1);
  finished_handler2.Run(true);
}

TEST_F(AsyncEventSequencerTest, MultiDBusActionsSucceed) {
  auto handler1 = aec_->GetExportHandler(
      kTestInterface, kTestMethod1, "method export failed", false);
  auto handler2 = aec_->GetExportHandler(
      kTestInterface, kTestMethod2, "method export failed", false);
  aec_->OnAllTasksCompletedCall({cb_});
  handler1.Run(kTestInterface, kTestMethod1, true);
  EXPECT_CALL(*this, HandleCompletion(true)).Times(1);
  handler2.Run(kTestInterface, kTestMethod2, true);
}

TEST_F(AsyncEventSequencerTest, SomeDBusActionsFail) {
  auto handler1 = aec_->GetExportHandler(
      kTestInterface, kTestMethod1, "method export failed", false);
  auto handler2 = aec_->GetExportHandler(
      kTestInterface, kTestMethod2, "method export failed", false);
  aec_->OnAllTasksCompletedCall({cb_});
  handler1.Run(kTestInterface, kTestMethod1, true);
  EXPECT_CALL(*this, HandleCompletion(false)).Times(1);
  handler2.Run(kTestInterface, kTestMethod2, false);
}

TEST_F(AsyncEventSequencerTest, MixedActions) {
  auto handler1 = aec_->GetExportHandler(
      kTestInterface, kTestMethod1, "method export failed", false);
  auto handler2 = aec_->GetHandler("handler failed", false);
  aec_->OnAllTasksCompletedCall({cb_});
  handler1.Run(kTestInterface, kTestMethod1, true);
  EXPECT_CALL(*this, HandleCompletion(true)).Times(1);
  handler2.Run(true);
}

}  // namespace dbus_utils

}  // namespace brillo