1 // 2 // Copyright (C) 2017 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 // 16 17 #ifndef UPDATE_ENGINE_UPDATE_MANAGER_POLICY_TEST_UTILS_H_ 18 #define UPDATE_ENGINE_UPDATE_MANAGER_POLICY_TEST_UTILS_H_ 19 20 #include <memory> 21 #include <string> 22 23 #include <base/time/time.h> 24 #include <brillo/message_loops/fake_message_loop.h> 25 #include <gtest/gtest.h> 26 27 #include "update_engine/common/fake_clock.h" 28 #include "update_engine/update_manager/evaluation_context.h" 29 #include "update_engine/update_manager/fake_state.h" 30 #include "update_engine/update_manager/policy_utils.h" 31 32 namespace chromeos_update_manager { 33 34 class UmPolicyTestBase : public ::testing::Test { 35 protected: 36 UmPolicyTestBase() = default; 37 38 void SetUp() override; 39 40 void TearDown() override; 41 42 // Sets the clock to fixed values. 43 virtual void SetUpDefaultClock(); 44 45 // Sets up the default state in fake_state_. override to add Policy-specific 46 // items, but only after calling this class's implementation. 47 virtual void SetUpDefaultState(); 48 49 // Returns a default UpdateState structure: 50 virtual UpdateState GetDefaultUpdateState(base::TimeDelta first_seen_period); 51 52 // Runs the passed |method| after resetting the EvaluationContext and expects 53 // it to return the |expected| return value. 54 template <typename T, typename R, typename... Args> ExpectStatus(EvalStatus expected,T method,R * result,Args...args)55 void ExpectStatus(EvalStatus expected, T method, R* result, Args... args) { 56 std::string error = "<None>"; 57 eval_ctx_->ResetEvaluation(); 58 EXPECT_EQ(expected, 59 (*method)(eval_ctx_.get(), &fake_state_, &error, result, args...)) 60 << "Returned error: " << error 61 << "\nEvaluation context: " << eval_ctx_->DumpContext(); 62 } 63 64 // Runs the passed |method| after resetting the EvaluationContext, in order 65 // to use the method to get a value for other testing (doesn't validate the 66 // return value, just returns it). 67 template <typename T, typename R, typename... Args> CallMethodWithContext(T method,R * result,Args...args)68 EvalStatus CallMethodWithContext(T method, R* result, Args... args) { 69 std::string error = "<None>"; 70 eval_ctx_->ResetEvaluation(); 71 return (*method)(eval_ctx_.get(), &fake_state_, &error, result, args...); 72 } 73 74 // Runs the passed |policy_method| on the framework policy and expects it to 75 // return the |expected| return value. 76 template <typename T, typename R, typename... Args> ExpectPolicyStatus(EvalStatus expected,T policy_method,R * result,Args...args)77 void ExpectPolicyStatus(EvalStatus expected, 78 T policy_method, 79 R* result, 80 Args... args) { 81 std::string error = "<None>"; 82 eval_ctx_->ResetEvaluation(); 83 EXPECT_EQ(expected, 84 (policy_.get()->*policy_method)( 85 eval_ctx_.get(), &fake_state_, &error, result, args...)) 86 << "Returned error: " << error 87 << "\nEvaluation context: " << eval_ctx_->DumpContext(); 88 } 89 90 brillo::FakeMessageLoop loop_{nullptr}; 91 chromeos_update_engine::FakeClock fake_clock_; 92 FakeState fake_state_; 93 scoped_refptr<EvaluationContext> eval_ctx_; 94 std::unique_ptr<Policy> policy_; 95 }; 96 97 } // namespace chromeos_update_manager 98 99 #endif // UPDATE_ENGINE_UPDATE_MANAGER_POLICY_TEST_UTILS_H_ 100