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