-
Notifications
You must be signed in to change notification settings - Fork 19
/
policy_test_utils.h
102 lines (84 loc) · 3.67 KB
/
policy_test_utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//
// Copyright (C) 2017 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#ifndef UPDATE_ENGINE_UPDATE_MANAGER_POLICY_TEST_UTILS_H_
#define UPDATE_ENGINE_UPDATE_MANAGER_POLICY_TEST_UTILS_H_
#include <memory>
#include <string>
#include <base/time/time.h>
#include <brillo/message_loops/fake_message_loop.h>
#include <gtest/gtest.h>
#include "update_engine/common/fake_clock.h"
#include "update_engine/update_manager/evaluation_context.h"
#include "update_engine/update_manager/fake_state.h"
#include "update_engine/update_manager/policy_utils.h"
namespace chromeos_update_manager {
class UmPolicyTestBase : public ::testing::Test {
protected:
UmPolicyTestBase() = default;
void SetUp() override;
void TearDown() override;
// Sets the clock to fixed values.
virtual void SetUpDefaultClock();
// Sets the fake time provider to the time given by the fake clock.
virtual void SetUpDefaultTimeProvider();
// Sets up the default state in fake_state_. override to add Policy-specific
// items, but only after calling this class's implementation.
virtual void SetUpDefaultState();
// Returns a default UpdateState structure:
virtual UpdateState GetDefaultUpdateState(base::TimeDelta first_seen_period);
// Runs the passed |method| after resetting the EvaluationContext and expects
// it to return the |expected| return value.
template <typename T, typename R, typename... Args>
void ExpectStatus(EvalStatus expected, T method, R* result, Args... args) {
std::string error = "<None>";
eval_ctx_->ResetEvaluation();
EXPECT_EQ(expected,
(*method)(eval_ctx_.get(), &fake_state_, &error, result, args...))
<< "Returned error: " << error
<< "\nEvaluation context: " << eval_ctx_->DumpContext();
}
// Runs the passed |method| after resetting the EvaluationContext, in order
// to use the method to get a value for other testing (doesn't validate the
// return value, just returns it).
template <typename T, typename R, typename... Args>
EvalStatus CallMethodWithContext(T method, R* result, Args... args) {
std::string error = "<None>";
eval_ctx_->ResetEvaluation();
return (*method)(eval_ctx_.get(), &fake_state_, &error, result, args...);
}
// Runs the passed |policy_method| on the framework policy and expects it to
// return the |expected| return value.
template <typename T, typename R, typename... Args>
void ExpectPolicyStatus(EvalStatus expected,
T policy_method,
R* result,
Args... args) {
std::string error = "<None>";
eval_ctx_->ResetEvaluation();
EXPECT_EQ(expected,
(policy_.get()->*policy_method)(
eval_ctx_.get(), &fake_state_, &error, result, args...))
<< "Returned error: " << error
<< "\nEvaluation context: " << eval_ctx_->DumpContext();
}
brillo::FakeMessageLoop loop_{nullptr};
chromeos_update_engine::FakeClock* fake_clock_;
FakeState fake_state_;
std::shared_ptr<EvaluationContext> eval_ctx_;
std::unique_ptr<Policy> policy_;
};
} // namespace chromeos_update_manager
#endif // UPDATE_ENGINE_UPDATE_MANAGER_POLICY_TEST_UTILS_H_