blob: 0657207e187bb7b81b61d27be4672cf8f9b0f899 [file] [log] [blame]
rspangler@google.com49fdf182009-10-10 00:57:34 +00001// Copyright (c) 2009 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
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -08005#include <string>
rspangler@google.com49fdf182009-10-10 00:57:34 +00006#include <gtest/gtest.h>
7#include "update_engine/action.h"
8#include "update_engine/action_processor.h"
9
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080010using std::string;
11
rspangler@google.com49fdf182009-10-10 00:57:34 +000012namespace chromeos_update_engine {
13
14using chromeos_update_engine::ActionPipe;
15
16class ActionProcessorTestAction;
17
18template<>
19class ActionTraits<ActionProcessorTestAction> {
20 public:
21 typedef string OutputObjectType;
22 typedef string InputObjectType;
23};
24
25// This is a simple Action class for testing.
26struct ActionProcessorTestAction : public Action<ActionProcessorTestAction> {
27 typedef string InputObjectType;
28 typedef string OutputObjectType;
29 ActionPipe<string>* in_pipe() { return in_pipe_.get(); }
30 ActionPipe<string>* out_pipe() { return out_pipe_.get(); }
31 ActionProcessor* processor() { return processor_; }
32 void PerformAction() {}
33 void CompleteAction() {
34 ASSERT_TRUE(processor());
35 processor()->ActionComplete(this, true);
36 }
37 string Type() const { return "ActionProcessorTestAction"; }
38};
39
40class ActionProcessorTest : public ::testing::Test { };
41
42// This test creates two simple Actions and sends a message via an ActionPipe
43// from one to the other.
44TEST(ActionProcessorTest, SimpleTest) {
45 ActionProcessorTestAction action;
46 ActionProcessor action_processor;
47 EXPECT_FALSE(action_processor.IsRunning());
48 action_processor.EnqueueAction(&action);
49 EXPECT_FALSE(action_processor.IsRunning());
50 EXPECT_FALSE(action.IsRunning());
51 action_processor.StartProcessing();
52 EXPECT_TRUE(action_processor.IsRunning());
53 EXPECT_TRUE(action.IsRunning());
54 EXPECT_EQ(action_processor.current_action(), &action);
55 action.CompleteAction();
56 EXPECT_FALSE(action_processor.IsRunning());
57 EXPECT_FALSE(action.IsRunning());
58}
59
60namespace {
61class MyActionProcessorDelegate : public ActionProcessorDelegate {
62 public:
63 explicit MyActionProcessorDelegate(const ActionProcessor* processor)
64 : processor_(processor), processing_done_called_(false) {}
65
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080066 virtual void ProcessingDone(const ActionProcessor* processor, bool success) {
rspangler@google.com49fdf182009-10-10 00:57:34 +000067 EXPECT_EQ(processor_, processor);
68 EXPECT_FALSE(processing_done_called_);
69 processing_done_called_ = true;
70 }
71 virtual void ProcessingStopped(const ActionProcessor* processor) {
72 EXPECT_EQ(processor_, processor);
73 EXPECT_FALSE(processing_stopped_called_);
74 processing_stopped_called_ = true;
75 }
adlr@google.comc98a7ed2009-12-04 18:54:03 +000076 virtual void ActionCompleted(ActionProcessor* processor,
77 AbstractAction* action,
rspangler@google.com49fdf182009-10-10 00:57:34 +000078 bool success) {
79 EXPECT_EQ(processor_, processor);
80 EXPECT_FALSE(action_completed_called_);
81 action_completed_called_ = true;
82 action_completed_success_ = success;
83 }
84
85 const ActionProcessor* processor_;
86 bool processing_done_called_;
87 bool processing_stopped_called_;
88 bool action_completed_called_;
89 bool action_completed_success_;
90};
91} // namespace {}
92
93TEST(ActionProcessorTest, DelegateTest) {
94 ActionProcessorTestAction action;
95 ActionProcessor action_processor;
96 MyActionProcessorDelegate delegate(&action_processor);
97 action_processor.set_delegate(&delegate);
98
99 action_processor.EnqueueAction(&action);
100 action_processor.StartProcessing();
101 action.CompleteAction();
102 action_processor.set_delegate(NULL);
103 EXPECT_TRUE(delegate.processing_done_called_);
104 EXPECT_TRUE(delegate.action_completed_called_);
105}
106
107TEST(ActionProcessorTest, StopProcessingTest) {
108 ActionProcessorTestAction action;
109 ActionProcessor action_processor;
110 MyActionProcessorDelegate delegate(&action_processor);
111 action_processor.set_delegate(&delegate);
112
113 action_processor.EnqueueAction(&action);
114 action_processor.StartProcessing();
115 action_processor.StopProcessing();
116 action_processor.set_delegate(NULL);
117 EXPECT_TRUE(delegate.processing_stopped_called_);
118 EXPECT_FALSE(delegate.action_completed_called_);
119 EXPECT_FALSE(action_processor.IsRunning());
120 EXPECT_EQ(NULL, action_processor.current_action());
121}
122
123TEST(ActionProcessorTest, ChainActionsTest) {
124 ActionProcessorTestAction action1, action2;
125 ActionProcessor action_processor;
126 action_processor.EnqueueAction(&action1);
127 action_processor.EnqueueAction(&action2);
128 action_processor.StartProcessing();
129 EXPECT_EQ(&action1, action_processor.current_action());
130 EXPECT_TRUE(action_processor.IsRunning());
131 action1.CompleteAction();
132 EXPECT_EQ(&action2, action_processor.current_action());
133 EXPECT_TRUE(action_processor.IsRunning());
134 action2.CompleteAction();
135 EXPECT_EQ(NULL, action_processor.current_action());
136 EXPECT_FALSE(action_processor.IsRunning());
137}
138
139TEST(ActionProcessorTest, DtorTest) {
140 ActionProcessorTestAction action1, action2;
141 {
142 ActionProcessor action_processor;
143 action_processor.EnqueueAction(&action1);
144 action_processor.EnqueueAction(&action2);
145 action_processor.StartProcessing();
146 }
147 EXPECT_EQ(NULL, action1.processor());
148 EXPECT_FALSE(action1.IsRunning());
149 EXPECT_EQ(NULL, action2.processor());
150 EXPECT_FALSE(action2.IsRunning());
151}
152
153TEST(ActionProcessorTest, DefaultDelegateTest) {
154 // Just make sure it doesn't crash
155 ActionProcessorTestAction action;
156 ActionProcessor action_processor;
157 ActionProcessorDelegate delegate;
158 action_processor.set_delegate(&delegate);
159
160 action_processor.EnqueueAction(&action);
161 action_processor.StartProcessing();
162 action.CompleteAction();
163
164 action_processor.EnqueueAction(&action);
165 action_processor.StartProcessing();
166 action_processor.StopProcessing();
167
168 action_processor.set_delegate(NULL);
169}
170
171} // namespace chromeos_update_engine