| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // | 
|  | 2 | // Copyright (C) 2010 The Android Open Source Project | 
|  | 3 | // | 
|  | 4 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | // you may not use this file except in compliance with the License. | 
|  | 6 | // You may obtain a copy of the License at | 
|  | 7 | // | 
|  | 8 | //      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | // | 
|  | 10 | // Unless required by applicable law or agreed to in writing, software | 
|  | 11 | // distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | // See the License for the specific language governing permissions and | 
|  | 14 | // limitations under the License. | 
|  | 15 | // | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 16 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #include "update_engine/common/action.h" | 
| Alex Deymo | aab50e3 | 2014-11-10 19:55:35 -0800 | [diff] [blame] | 18 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 19 | #include <string> | 
| Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 20 | #include <utility> | 
|  | 21 |  | 
|  | 22 | #include <gtest/gtest.h> | 
|  | 23 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 24 | #include "update_engine/common/action_processor.h" | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 25 |  | 
| Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 26 | using std::string; | 
|  | 27 |  | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 28 | namespace chromeos_update_engine { | 
|  | 29 |  | 
|  | 30 | using chromeos_update_engine::ActionPipe; | 
|  | 31 |  | 
|  | 32 | class ActionTestAction; | 
|  | 33 |  | 
| Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 34 | template <> | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 35 | class ActionTraits<ActionTestAction> { | 
|  | 36 | public: | 
|  | 37 | typedef string OutputObjectType; | 
|  | 38 | typedef string InputObjectType; | 
|  | 39 | }; | 
|  | 40 |  | 
|  | 41 | // This is a simple Action class for testing. | 
| Yunlian Jiang | a178e5e | 2013-04-05 14:41:56 -0700 | [diff] [blame] | 42 | class ActionTestAction : public Action<ActionTestAction> { | 
|  | 43 | public: | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 44 | typedef string InputObjectType; | 
|  | 45 | typedef string OutputObjectType; | 
|  | 46 | ActionPipe<string>* in_pipe() { return in_pipe_.get(); } | 
|  | 47 | ActionPipe<string>* out_pipe() { return out_pipe_.get(); } | 
|  | 48 | ActionProcessor* processor() { return processor_; } | 
|  | 49 | void PerformAction() {} | 
|  | 50 | void CompleteAction() { | 
|  | 51 | ASSERT_TRUE(processor()); | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 52 | processor()->ActionComplete(this, ErrorCode::kSuccess); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 53 | } | 
|  | 54 | string Type() const { return "ActionTestAction"; } | 
|  | 55 | }; | 
|  | 56 |  | 
| Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 57 | class ActionTest : public ::testing::Test {}; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 58 |  | 
|  | 59 | // This test creates two simple Actions and sends a message via an ActionPipe | 
|  | 60 | // from one to the other. | 
|  | 61 | TEST(ActionTest, SimpleTest) { | 
| Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 62 | auto action = std::make_unique<ActionTestAction>(); | 
|  | 63 | auto action_ptr = action.get(); | 
|  | 64 | EXPECT_FALSE(action->in_pipe()); | 
|  | 65 | EXPECT_FALSE(action->out_pipe()); | 
|  | 66 | EXPECT_FALSE(action->processor()); | 
|  | 67 | EXPECT_FALSE(action->IsRunning()); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 68 |  | 
|  | 69 | ActionProcessor action_processor; | 
| Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 70 | action_processor.EnqueueAction(std::move(action)); | 
|  | 71 | EXPECT_EQ(&action_processor, action_ptr->processor()); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 72 | action_processor.StartProcessing(); | 
| Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 73 | EXPECT_TRUE(action_ptr->IsRunning()); | 
|  | 74 | action_ptr->CompleteAction(); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 75 | } | 
|  | 76 |  | 
|  | 77 | }  // namespace chromeos_update_engine |