blob: 40275a03390f268224ff777be42d32dfd3fd7774 [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)
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),
68 action_completed_success_(false) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000069
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080070 virtual void ProcessingDone(const ActionProcessor* processor, bool success) {
rspangler@google.com49fdf182009-10-10 00:57:34 +000071 EXPECT_EQ(processor_, processor);
72 EXPECT_FALSE(processing_done_called_);
73 processing_done_called_ = true;
74 }
75 virtual void ProcessingStopped(const ActionProcessor* processor) {
76 EXPECT_EQ(processor_, processor);
77 EXPECT_FALSE(processing_stopped_called_);
78 processing_stopped_called_ = true;
79 }
adlr@google.comc98a7ed2009-12-04 18:54:03 +000080 virtual void ActionCompleted(ActionProcessor* processor,
81 AbstractAction* action,
rspangler@google.com49fdf182009-10-10 00:57:34 +000082 bool success) {
83 EXPECT_EQ(processor_, processor);
84 EXPECT_FALSE(action_completed_called_);
85 action_completed_called_ = true;
86 action_completed_success_ = success;
87 }
88
89 const ActionProcessor* processor_;
90 bool processing_done_called_;
91 bool processing_stopped_called_;
92 bool action_completed_called_;
93 bool action_completed_success_;
94};
95} // namespace {}
96
97TEST(ActionProcessorTest, DelegateTest) {
98 ActionProcessorTestAction action;
99 ActionProcessor action_processor;
100 MyActionProcessorDelegate delegate(&action_processor);
101 action_processor.set_delegate(&delegate);
102
103 action_processor.EnqueueAction(&action);
104 action_processor.StartProcessing();
105 action.CompleteAction();
106 action_processor.set_delegate(NULL);
107 EXPECT_TRUE(delegate.processing_done_called_);
108 EXPECT_TRUE(delegate.action_completed_called_);
109}
110
111TEST(ActionProcessorTest, StopProcessingTest) {
112 ActionProcessorTestAction action;
113 ActionProcessor action_processor;
114 MyActionProcessorDelegate delegate(&action_processor);
115 action_processor.set_delegate(&delegate);
116
117 action_processor.EnqueueAction(&action);
118 action_processor.StartProcessing();
119 action_processor.StopProcessing();
120 action_processor.set_delegate(NULL);
121 EXPECT_TRUE(delegate.processing_stopped_called_);
122 EXPECT_FALSE(delegate.action_completed_called_);
123 EXPECT_FALSE(action_processor.IsRunning());
124 EXPECT_EQ(NULL, action_processor.current_action());
125}
126
127TEST(ActionProcessorTest, ChainActionsTest) {
128 ActionProcessorTestAction action1, action2;
129 ActionProcessor action_processor;
130 action_processor.EnqueueAction(&action1);
131 action_processor.EnqueueAction(&action2);
132 action_processor.StartProcessing();
133 EXPECT_EQ(&action1, action_processor.current_action());
134 EXPECT_TRUE(action_processor.IsRunning());
135 action1.CompleteAction();
136 EXPECT_EQ(&action2, action_processor.current_action());
137 EXPECT_TRUE(action_processor.IsRunning());
138 action2.CompleteAction();
139 EXPECT_EQ(NULL, action_processor.current_action());
140 EXPECT_FALSE(action_processor.IsRunning());
141}
142
143TEST(ActionProcessorTest, DtorTest) {
144 ActionProcessorTestAction action1, action2;
145 {
146 ActionProcessor action_processor;
147 action_processor.EnqueueAction(&action1);
148 action_processor.EnqueueAction(&action2);
149 action_processor.StartProcessing();
150 }
151 EXPECT_EQ(NULL, action1.processor());
152 EXPECT_FALSE(action1.IsRunning());
153 EXPECT_EQ(NULL, action2.processor());
154 EXPECT_FALSE(action2.IsRunning());
155}
156
157TEST(ActionProcessorTest, DefaultDelegateTest) {
158 // Just make sure it doesn't crash
159 ActionProcessorTestAction action;
160 ActionProcessor action_processor;
161 ActionProcessorDelegate delegate;
162 action_processor.set_delegate(&delegate);
163
164 action_processor.EnqueueAction(&action);
165 action_processor.StartProcessing();
166 action.CompleteAction();
167
168 action_processor.EnqueueAction(&action);
169 action_processor.StartProcessing();
170 action_processor.StopProcessing();
171
172 action_processor.set_delegate(NULL);
173}
174
175} // namespace chromeos_update_engine