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 
11 namespace brillo {
12 
13 namespace dbus_utils {
14 
15 namespace {
16 
17 const char kTestInterface[] = "org.test.if";
18 const char kTestMethod1[] = "TestMethod1";
19 const char kTestMethod2[] = "TestMethod2";
20 
21 }  // namespace
22 
23 class AsyncEventSequencerTest : public ::testing::Test {
24  public:
25   MOCK_METHOD1(HandleCompletion, void(bool all_succeeded));
26 
SetUp()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 
TEST_F(AsyncEventSequencerTest,WaitForCompletionActions)37 TEST_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 
TEST_F(AsyncEventSequencerTest,MultiInitActionsSucceed)44 TEST_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 
TEST_F(AsyncEventSequencerTest,SomeInitActionsFail)53 TEST_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 
TEST_F(AsyncEventSequencerTest,MultiDBusActionsSucceed)62 TEST_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 
TEST_F(AsyncEventSequencerTest,SomeDBusActionsFail)73 TEST_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 
TEST_F(AsyncEventSequencerTest,MixedActions)84 TEST_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