blob: 20bded6b50338f83959693ee178c90cee3772f03 [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.
Yunlian Jianga178e5e2013-04-05 14:41:56 -070026class ActionProcessorTestAction : public Action<ActionProcessorTestAction> {
27 public:
rspangler@google.com49fdf182009-10-10 00:57:34 +000028 typedef string InputObjectType;
29 typedef string OutputObjectType;
30 ActionPipe<string>* in_pipe() { return in_pipe_.get(); }
31 ActionPipe<string>* out_pipe() { return out_pipe_.get(); }
32 ActionProcessor* processor() { return processor_; }
33 void PerformAction() {}
34 void CompleteAction() {
35 ASSERT_TRUE(processor());
David Zeuthena99981f2013-04-29 13:42:47 -070036 processor()->ActionComplete(this, kErrorCodeSuccess);
rspangler@google.com49fdf182009-10-10 00:57:34 +000037 }
38 string Type() const { return "ActionProcessorTestAction"; }
39};
40
41class ActionProcessorTest : public ::testing::Test { };
42
43// This test creates two simple Actions and sends a message via an ActionPipe
44// from one to the other.
45TEST(ActionProcessorTest, SimpleTest) {
46 ActionProcessorTestAction action;
47 ActionProcessor action_processor;
48 EXPECT_FALSE(action_processor.IsRunning());
49 action_processor.EnqueueAction(&action);
50 EXPECT_FALSE(action_processor.IsRunning());
51 EXPECT_FALSE(action.IsRunning());
52 action_processor.StartProcessing();
53 EXPECT_TRUE(action_processor.IsRunning());
54 EXPECT_TRUE(action.IsRunning());
55 EXPECT_EQ(action_processor.current_action(), &action);
56 action.CompleteAction();
57 EXPECT_FALSE(action_processor.IsRunning());
58 EXPECT_FALSE(action.IsRunning());
59}
60
61namespace {
62class MyActionProcessorDelegate : public ActionProcessorDelegate {
63 public:
64 explicit MyActionProcessorDelegate(const ActionProcessor* processor)
Andrew de los Reyes3270f742010-07-15 22:28:14 -070065 : processor_(processor),
66 processing_done_called_(false),
67 processing_stopped_called_(false),
68 action_completed_called_(false),
David Zeuthena99981f2013-04-29 13:42:47 -070069 action_exit_code_(kErrorCodeError) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000070
Darin Petkovc1a8b422010-07-19 11:34:49 -070071 virtual void ProcessingDone(const ActionProcessor* processor,
David Zeuthena99981f2013-04-29 13:42:47 -070072 ErrorCode code) {
rspangler@google.com49fdf182009-10-10 00:57:34 +000073 EXPECT_EQ(processor_, processor);
74 EXPECT_FALSE(processing_done_called_);
75 processing_done_called_ = true;
76 }
77 virtual void ProcessingStopped(const ActionProcessor* processor) {
78 EXPECT_EQ(processor_, processor);
79 EXPECT_FALSE(processing_stopped_called_);
80 processing_stopped_called_ = true;
81 }
adlr@google.comc98a7ed2009-12-04 18:54:03 +000082 virtual void ActionCompleted(ActionProcessor* processor,
83 AbstractAction* action,
David Zeuthena99981f2013-04-29 13:42:47 -070084 ErrorCode code) {
rspangler@google.com49fdf182009-10-10 00:57:34 +000085 EXPECT_EQ(processor_, processor);
86 EXPECT_FALSE(action_completed_called_);
87 action_completed_called_ = true;
Darin Petkovc1a8b422010-07-19 11:34:49 -070088 action_exit_code_ = code;
rspangler@google.com49fdf182009-10-10 00:57:34 +000089 }
90
91 const ActionProcessor* processor_;
92 bool processing_done_called_;
93 bool processing_stopped_called_;
94 bool action_completed_called_;
David Zeuthena99981f2013-04-29 13:42:47 -070095 ErrorCode action_exit_code_;
rspangler@google.com49fdf182009-10-10 00:57:34 +000096};
97} // namespace {}
98
99TEST(ActionProcessorTest, DelegateTest) {
100 ActionProcessorTestAction action;
101 ActionProcessor action_processor;
102 MyActionProcessorDelegate delegate(&action_processor);
103 action_processor.set_delegate(&delegate);
104
105 action_processor.EnqueueAction(&action);
106 action_processor.StartProcessing();
107 action.CompleteAction();
108 action_processor.set_delegate(NULL);
109 EXPECT_TRUE(delegate.processing_done_called_);
110 EXPECT_TRUE(delegate.action_completed_called_);
111}
112
113TEST(ActionProcessorTest, StopProcessingTest) {
114 ActionProcessorTestAction action;
115 ActionProcessor action_processor;
116 MyActionProcessorDelegate delegate(&action_processor);
117 action_processor.set_delegate(&delegate);
118
119 action_processor.EnqueueAction(&action);
120 action_processor.StartProcessing();
121 action_processor.StopProcessing();
122 action_processor.set_delegate(NULL);
123 EXPECT_TRUE(delegate.processing_stopped_called_);
124 EXPECT_FALSE(delegate.action_completed_called_);
125 EXPECT_FALSE(action_processor.IsRunning());
126 EXPECT_EQ(NULL, action_processor.current_action());
127}
128
129TEST(ActionProcessorTest, ChainActionsTest) {
130 ActionProcessorTestAction action1, action2;
131 ActionProcessor action_processor;
132 action_processor.EnqueueAction(&action1);
133 action_processor.EnqueueAction(&action2);
134 action_processor.StartProcessing();
135 EXPECT_EQ(&action1, action_processor.current_action());
136 EXPECT_TRUE(action_processor.IsRunning());
137 action1.CompleteAction();
138 EXPECT_EQ(&action2, action_processor.current_action());
139 EXPECT_TRUE(action_processor.IsRunning());
140 action2.CompleteAction();
141 EXPECT_EQ(NULL, action_processor.current_action());
142 EXPECT_FALSE(action_processor.IsRunning());
143}
144
145TEST(ActionProcessorTest, DtorTest) {
146 ActionProcessorTestAction action1, action2;
147 {
148 ActionProcessor action_processor;
149 action_processor.EnqueueAction(&action1);
150 action_processor.EnqueueAction(&action2);
151 action_processor.StartProcessing();
152 }
153 EXPECT_EQ(NULL, action1.processor());
154 EXPECT_FALSE(action1.IsRunning());
155 EXPECT_EQ(NULL, action2.processor());
156 EXPECT_FALSE(action2.IsRunning());
157}
158
159TEST(ActionProcessorTest, DefaultDelegateTest) {
160 // Just make sure it doesn't crash
161 ActionProcessorTestAction action;
162 ActionProcessor action_processor;
163 ActionProcessorDelegate delegate;
164 action_processor.set_delegate(&delegate);
165
166 action_processor.EnqueueAction(&action);
167 action_processor.StartProcessing();
168 action.CompleteAction();
169
170 action_processor.EnqueueAction(&action);
171 action_processor.StartProcessing();
172 action_processor.StopProcessing();
173
174 action_processor.set_delegate(NULL);
175}
176
177} // namespace chromeos_update_engine