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