1// Copyright 2014 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#include <brillo/dbus/async_event_sequencer.h>
6
7#include <base/bind_helpers.h>
8#include <gmock/gmock.h>
9#include <gtest/gtest.h>
10
11namespace brillo {
12
13namespace dbus_utils {
14
15namespace {
16
17const char kTestInterface[] = "org.test.if";
18const char kTestMethod1[] = "TestMethod1";
19const char kTestMethod2[] = "TestMethod2";
20
21}  // namespace
22
23class AsyncEventSequencerTest : public ::testing::Test {
24 public:
25  MOCK_METHOD1(HandleCompletion, void(bool all_succeeded));
26
27  void SetUp() {
28    aec_ = new AsyncEventSequencer();
29    cb_ = base::Bind(&AsyncEventSequencerTest::HandleCompletion,
30                     base::Unretained(this));
31  }
32
33  scoped_refptr<AsyncEventSequencer> aec_;
34  AsyncEventSequencer::CompletionAction cb_;
35};
36
37TEST_F(AsyncEventSequencerTest, WaitForCompletionActions) {
38  auto finished_handler = aec_->GetHandler("handler failed", false);
39  finished_handler.Run(true);
40  EXPECT_CALL(*this, HandleCompletion(true)).Times(1);
41  aec_->OnAllTasksCompletedCall({cb_});
42}
43
44TEST_F(AsyncEventSequencerTest, MultiInitActionsSucceed) {
45  auto finished_handler1 = aec_->GetHandler("handler failed", false);
46  auto finished_handler2 = aec_->GetHandler("handler failed", false);
47  aec_->OnAllTasksCompletedCall({cb_});
48  finished_handler1.Run(true);
49  EXPECT_CALL(*this, HandleCompletion(true)).Times(1);
50  finished_handler2.Run(true);
51}
52
53TEST_F(AsyncEventSequencerTest, SomeInitActionsFail) {
54  auto finished_handler1 = aec_->GetHandler("handler failed", false);
55  auto finished_handler2 = aec_->GetHandler("handler failed", false);
56  aec_->OnAllTasksCompletedCall({cb_});
57  finished_handler1.Run(false);
58  EXPECT_CALL(*this, HandleCompletion(false)).Times(1);
59  finished_handler2.Run(true);
60}
61
62TEST_F(AsyncEventSequencerTest, MultiDBusActionsSucceed) {
63  auto handler1 = aec_->GetExportHandler(
64      kTestInterface, kTestMethod1, "method export failed", false);
65  auto handler2 = aec_->GetExportHandler(
66      kTestInterface, kTestMethod2, "method export failed", false);
67  aec_->OnAllTasksCompletedCall({cb_});
68  handler1.Run(kTestInterface, kTestMethod1, true);
69  EXPECT_CALL(*this, HandleCompletion(true)).Times(1);
70  handler2.Run(kTestInterface, kTestMethod2, true);
71}
72
73TEST_F(AsyncEventSequencerTest, SomeDBusActionsFail) {
74  auto handler1 = aec_->GetExportHandler(
75      kTestInterface, kTestMethod1, "method export failed", false);
76  auto handler2 = aec_->GetExportHandler(
77      kTestInterface, kTestMethod2, "method export failed", false);
78  aec_->OnAllTasksCompletedCall({cb_});
79  handler1.Run(kTestInterface, kTestMethod1, true);
80  EXPECT_CALL(*this, HandleCompletion(false)).Times(1);
81  handler2.Run(kTestInterface, kTestMethod2, false);
82}
83
84TEST_F(AsyncEventSequencerTest, MixedActions) {
85  auto handler1 = aec_->GetExportHandler(
86      kTestInterface, kTestMethod1, "method export failed", false);
87  auto handler2 = aec_->GetHandler("handler failed", false);
88  aec_->OnAllTasksCompletedCall({cb_});
89  handler1.Run(kTestInterface, kTestMethod1, true);
90  EXPECT_CALL(*this, HandleCompletion(true)).Times(1);
91  handler2.Run(true);
92}
93
94}  // namespace dbus_utils
95
96}  // namespace brillo
97