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