• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 the fake time provider to the time given by the fake clock.
46   virtual void SetUpDefaultTimeProvider();
47 
48   // Sets up the default state in fake_state_.  override to add Policy-specific
49   // items, but only after calling this class's implementation.
50   virtual void SetUpDefaultState();
51 
52   // Returns a default UpdateState structure:
53   virtual UpdateState GetDefaultUpdateState(base::TimeDelta first_seen_period);
54 
55   // Runs the passed |method| after resetting the EvaluationContext and expects
56   // it to return the |expected| return value.
57   template <typename T, typename R, typename... Args>
ExpectStatus(EvalStatus expected,T method,R * result,Args...args)58   void ExpectStatus(EvalStatus expected, T method, R* result, Args... args) {
59     std::string error = "<None>";
60     eval_ctx_->ResetEvaluation();
61     EXPECT_EQ(expected,
62               (*method)(eval_ctx_.get(), &fake_state_, &error, result, args...))
63         << "Returned error: " << error
64         << "\nEvaluation context: " << eval_ctx_->DumpContext();
65   }
66 
67   // Runs the passed |method| after resetting the EvaluationContext, in order
68   // to use the method to get a value for other testing (doesn't validate the
69   // return value, just returns it).
70   template <typename T, typename R, typename... Args>
CallMethodWithContext(T method,R * result,Args...args)71   EvalStatus CallMethodWithContext(T method, R* result, Args... args) {
72     std::string error = "<None>";
73     eval_ctx_->ResetEvaluation();
74     return (*method)(eval_ctx_.get(), &fake_state_, &error, result, args...);
75   }
76 
77   // Runs the passed |policy_method| on the framework policy and expects it to
78   // return the |expected| return value.
79   template <typename T, typename R, typename... Args>
ExpectPolicyStatus(EvalStatus expected,T policy_method,R * result,Args...args)80   void ExpectPolicyStatus(EvalStatus expected,
81                           T policy_method,
82                           R* result,
83                           Args... args) {
84     std::string error = "<None>";
85     eval_ctx_->ResetEvaluation();
86     EXPECT_EQ(expected,
87               (policy_.get()->*policy_method)(
88                   eval_ctx_.get(), &fake_state_, &error, result, args...))
89         << "Returned error: " << error
90         << "\nEvaluation context: " << eval_ctx_->DumpContext();
91   }
92 
93   brillo::FakeMessageLoop loop_{nullptr};
94   chromeos_update_engine::FakeClock* fake_clock_;
95   FakeState fake_state_;
96   std::shared_ptr<EvaluationContext> eval_ctx_;
97   std::unique_ptr<Policy> policy_;
98 };
99 
100 }  // namespace chromeos_update_manager
101 
102 #endif  // UPDATE_ENGINE_UPDATE_MANAGER_POLICY_TEST_UTILS_H_
103