policy_test_utils.h 3.7 KB

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