1 //
2 // Copyright (C) 2010 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #include "update_engine/common/action.h"
18 
19 #include <string>
20 #include <utility>
21 
22 #include <gtest/gtest.h>
23 
24 #include "update_engine/common/action_processor.h"
25 
26 using std::string;
27 
28 namespace chromeos_update_engine {
29 
30 using chromeos_update_engine::ActionPipe;
31 
32 class ActionTestAction;
33 
34 template <>
35 class ActionTraits<ActionTestAction> {
36  public:
37   typedef string OutputObjectType;
38   typedef string InputObjectType;
39 };
40 
41 // This is a simple Action class for testing.
42 class ActionTestAction : public Action<ActionTestAction> {
43  public:
44   typedef string InputObjectType;
45   typedef string OutputObjectType;
in_pipe()46   ActionPipe<string>* in_pipe() { return in_pipe_.get(); }
out_pipe()47   ActionPipe<string>* out_pipe() { return out_pipe_.get(); }
processor()48   ActionProcessor* processor() { return processor_; }
PerformAction()49   void PerformAction() {}
CompleteAction()50   void CompleteAction() {
51     ASSERT_TRUE(processor());
52     processor()->ActionComplete(this, ErrorCode::kSuccess);
53   }
Type() const54   string Type() const { return "ActionTestAction"; }
55 };
56 
57 class ActionTest : public ::testing::Test {};
58 
59 // This test creates two simple Actions and sends a message via an ActionPipe
60 // from one to the other.
TEST(ActionTest,SimpleTest)61 TEST(ActionTest, SimpleTest) {
62   auto action = std::make_unique<ActionTestAction>();
63   auto action_ptr = action.get();
64   EXPECT_FALSE(action->in_pipe());
65   EXPECT_FALSE(action->out_pipe());
66   EXPECT_FALSE(action->processor());
67   EXPECT_FALSE(action->IsRunning());
68 
69   ActionProcessor action_processor;
70   action_processor.EnqueueAction(std::move(action));
71   EXPECT_EQ(&action_processor, action_ptr->processor());
72   action_processor.StartProcessing();
73   EXPECT_TRUE(action_ptr->IsRunning());
74   action_ptr->CompleteAction();
75 }
76 
77 }  // namespace chromeos_update_engine
78