blob: 7f3914977beb7761b460df541c1623bc76a2bdc9 [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());
Darin Petkovc1a8b422010-07-19 11:34:49 -070035 processor()->ActionComplete(this, kActionCodeSuccess);
rspangler@google.com49fdf182009-10-10 00:57:34 +000036 }
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)
Andrew de los Reyes3270f742010-07-15 22:28:14 -070064 : processor_(processor),
65 processing_done_called_(false),
66 processing_stopped_called_(false),
67 action_completed_called_(false),
Darin Petkovc1a8b422010-07-19 11:34:49 -070068 action_exit_code_(kActionCodeError) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000069
Darin Petkovc1a8b422010-07-19 11:34:49 -070070 virtual void ProcessingDone(const ActionProcessor* processor,
71 ActionExitCode code) {
rspangler@google.com49fdf182009-10-10 00:57:34 +000072 EXPECT_EQ(processor_, processor);
73 EXPECT_FALSE(processing_done_called_);
74 processing_done_called_ = true;
75 }
76 virtual void ProcessingStopped(const ActionProcessor* processor) {
77 EXPECT_EQ(processor_, processor);
78 EXPECT_FALSE(processing_stopped_called_);
79 processing_stopped_called_ = true;
80 }
adlr@google.comc98a7ed2009-12-04 18:54:03 +000081 virtual void ActionCompleted(ActionProcessor* processor,
82 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -070083 ActionExitCode code) {
rspangler@google.com49fdf182009-10-10 00:57:34 +000084 EXPECT_EQ(processor_, processor);
85 EXPECT_FALSE(action_completed_called_);
86 action_completed_called_ = true;
Darin Petkovc1a8b422010-07-19 11:34:49 -070087 action_exit_code_ = code;
rspangler@google.com49fdf182009-10-10 00:57:34 +000088 }
89
90 const ActionProcessor* processor_;
91 bool processing_done_called_;
92 bool processing_stopped_called_;
93 bool action_completed_called_;
Darin Petkovc1a8b422010-07-19 11:34:49 -070094 ActionExitCode action_exit_code_;
rspangler@google.com49fdf182009-10-10 00:57:34 +000095};
96} // namespace {}
97
98TEST(ActionProcessorTest, DelegateTest) {
99 ActionProcessorTestAction action;
100 ActionProcessor action_processor;
101 MyActionProcessorDelegate delegate(&action_processor);
102 action_processor.set_delegate(&delegate);
103
104 action_processor.EnqueueAction(&action);
105 action_processor.StartProcessing();
106 action.CompleteAction();
107 action_processor.set_delegate(NULL);
108 EXPECT_TRUE(delegate.processing_done_called_);
109 EXPECT_TRUE(delegate.action_completed_called_);
110}
111
112TEST(ActionProcessorTest, StopProcessingTest) {
113 ActionProcessorTestAction action;
114 ActionProcessor action_processor;
115 MyActionProcessorDelegate delegate(&action_processor);
116 action_processor.set_delegate(&delegate);
117
118 action_processor.EnqueueAction(&action);
119 action_processor.StartProcessing();
120 action_processor.StopProcessing();
121 action_processor.set_delegate(NULL);
122 EXPECT_TRUE(delegate.processing_stopped_called_);
123 EXPECT_FALSE(delegate.action_completed_called_);
124 EXPECT_FALSE(action_processor.IsRunning());
125 EXPECT_EQ(NULL, action_processor.current_action());
126}
127
128TEST(ActionProcessorTest, ChainActionsTest) {
129 ActionProcessorTestAction action1, action2;
130 ActionProcessor action_processor;
131 action_processor.EnqueueAction(&action1);
132 action_processor.EnqueueAction(&action2);
133 action_processor.StartProcessing();
134 EXPECT_EQ(&action1, action_processor.current_action());
135 EXPECT_TRUE(action_processor.IsRunning());
136 action1.CompleteAction();
137 EXPECT_EQ(&action2, action_processor.current_action());
138 EXPECT_TRUE(action_processor.IsRunning());
139 action2.CompleteAction();
140 EXPECT_EQ(NULL, action_processor.current_action());
141 EXPECT_FALSE(action_processor.IsRunning());
142}
143
144TEST(ActionProcessorTest, DtorTest) {
145 ActionProcessorTestAction action1, action2;
146 {
147 ActionProcessor action_processor;
148 action_processor.EnqueueAction(&action1);
149 action_processor.EnqueueAction(&action2);
150 action_processor.StartProcessing();
151 }
152 EXPECT_EQ(NULL, action1.processor());
153 EXPECT_FALSE(action1.IsRunning());
154 EXPECT_EQ(NULL, action2.processor());
155 EXPECT_FALSE(action2.IsRunning());
156}
157
158TEST(ActionProcessorTest, DefaultDelegateTest) {
159 // Just make sure it doesn't crash
160 ActionProcessorTestAction action;
161 ActionProcessor action_processor;
162 ActionProcessorDelegate delegate;
163 action_processor.set_delegate(&delegate);
164
165 action_processor.EnqueueAction(&action);
166 action_processor.StartProcessing();
167 action.CompleteAction();
168
169 action_processor.EnqueueAction(&action);
170 action_processor.StartProcessing();
171 action_processor.StopProcessing();
172
173 action_processor.set_delegate(NULL);
174}
175
176} // namespace chromeos_update_engine