rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 1 | // 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 Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 5 | #include <string> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 6 | #include <gtest/gtest.h> |
| 7 | #include "update_engine/action.h" |
| 8 | #include "update_engine/action_processor.h" |
| 9 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 10 | using std::string; |
| 11 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 12 | namespace chromeos_update_engine { |
| 13 | |
| 14 | using chromeos_update_engine::ActionPipe; |
| 15 | |
| 16 | class ActionProcessorTestAction; |
| 17 | |
| 18 | template<> |
| 19 | class ActionTraits<ActionProcessorTestAction> { |
| 20 | public: |
| 21 | typedef string OutputObjectType; |
| 22 | typedef string InputObjectType; |
| 23 | }; |
| 24 | |
| 25 | // This is a simple Action class for testing. |
| 26 | struct 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 Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 35 | processor()->ActionComplete(this, kActionCodeSuccess); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 36 | } |
| 37 | string Type() const { return "ActionProcessorTestAction"; } |
| 38 | }; |
| 39 | |
| 40 | class 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. |
| 44 | TEST(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 | |
| 60 | namespace { |
| 61 | class MyActionProcessorDelegate : public ActionProcessorDelegate { |
| 62 | public: |
| 63 | explicit MyActionProcessorDelegate(const ActionProcessor* processor) |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 64 | : processor_(processor), |
| 65 | processing_done_called_(false), |
| 66 | processing_stopped_called_(false), |
| 67 | action_completed_called_(false), |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 68 | action_exit_code_(kActionCodeError) {} |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 69 | |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 70 | virtual void ProcessingDone(const ActionProcessor* processor, |
| 71 | ActionExitCode code) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 72 | 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.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 81 | virtual void ActionCompleted(ActionProcessor* processor, |
| 82 | AbstractAction* action, |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 83 | ActionExitCode code) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 84 | EXPECT_EQ(processor_, processor); |
| 85 | EXPECT_FALSE(action_completed_called_); |
| 86 | action_completed_called_ = true; |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 87 | action_exit_code_ = code; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | const ActionProcessor* processor_; |
| 91 | bool processing_done_called_; |
| 92 | bool processing_stopped_called_; |
| 93 | bool action_completed_called_; |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 94 | ActionExitCode action_exit_code_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 95 | }; |
| 96 | } // namespace {} |
| 97 | |
| 98 | TEST(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 | |
| 112 | TEST(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 | |
| 128 | TEST(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 | |
| 144 | TEST(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 | |
| 158 | TEST(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 |