Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 1 | // Copyright (c) 2014 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <base/memory/scoped_ptr.h> |
| 6 | #include <gtest/gtest.h> |
| 7 | #include <string> |
| 8 | |
| 9 | #include "update_engine/policy_manager/evaluation_context.h" |
| 10 | #include "update_engine/policy_manager/generic_variables.h" |
| 11 | #include "update_engine/policy_manager/pmtest_utils.h" |
| 12 | #include "update_engine/policy_manager/fake_variable.h" |
| 13 | |
| 14 | using std::string; |
| 15 | |
| 16 | namespace chromeos_policy_manager { |
| 17 | |
| 18 | class PmEvaluationContextTest : public ::testing::Test { |
| 19 | public: |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 20 | PmEvaluationContextTest() : fake_int_var_("fake_int", kVariableModePoll) {} |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 21 | |
| 22 | protected: |
| 23 | virtual void SetUp() { |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame^] | 24 | eval_ctx_ = new EvaluationContext(); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 25 | } |
| 26 | |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame^] | 27 | scoped_refptr<EvaluationContext> eval_ctx_; |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 28 | FakeVariable<int> fake_int_var_; |
| 29 | }; |
| 30 | |
| 31 | TEST_F(PmEvaluationContextTest, GetValueFails) { |
| 32 | // FakeVariable is initialized as returning NULL. |
| 33 | PMTEST_EXPECT_NULL(eval_ctx_->GetValue(&fake_int_var_)); |
| 34 | } |
| 35 | |
| 36 | TEST_F(PmEvaluationContextTest, GetValueFailsWithInvalidVar) { |
| 37 | PMTEST_EXPECT_NULL(eval_ctx_->GetValue( |
| 38 | reinterpret_cast<Variable<int>*>(NULL))); |
| 39 | } |
| 40 | |
| 41 | TEST_F(PmEvaluationContextTest, GetValueReturns) { |
| 42 | const int* p_fake_int; |
| 43 | |
| 44 | fake_int_var_.reset(new int(42)); |
| 45 | p_fake_int = eval_ctx_->GetValue(&fake_int_var_); |
| 46 | PMTEST_ASSERT_NOT_NULL(p_fake_int); |
| 47 | EXPECT_EQ(42, *p_fake_int); |
| 48 | } |
| 49 | |
| 50 | TEST_F(PmEvaluationContextTest, GetValueCached) { |
| 51 | const int* p_fake_int; |
| 52 | |
| 53 | fake_int_var_.reset(new int(42)); |
| 54 | p_fake_int = eval_ctx_->GetValue(&fake_int_var_); |
| 55 | |
| 56 | // Check that if the variable changes, the EvaluationContext keeps returning |
| 57 | // the cached value. |
| 58 | fake_int_var_.reset(new int(5)); |
| 59 | |
| 60 | p_fake_int = eval_ctx_->GetValue(&fake_int_var_); |
| 61 | PMTEST_ASSERT_NOT_NULL(p_fake_int); |
| 62 | EXPECT_EQ(42, *p_fake_int); |
| 63 | } |
| 64 | |
| 65 | TEST_F(PmEvaluationContextTest, GetValueDontCacheNULL) { |
| 66 | const int* p_fake_int = eval_ctx_->GetValue(&fake_int_var_); |
| 67 | PMTEST_EXPECT_NULL(p_fake_int); |
| 68 | |
| 69 | fake_int_var_.reset(new int(42)); |
| 70 | // A second attempt to read the variable should work even on the same |
| 71 | // EvaluationContext. |
| 72 | p_fake_int = eval_ctx_->GetValue(&fake_int_var_); |
| 73 | PMTEST_ASSERT_NOT_NULL(p_fake_int); |
| 74 | EXPECT_EQ(42, *p_fake_int); |
| 75 | } |
| 76 | |
| 77 | TEST_F(PmEvaluationContextTest, GetValueMixedTypes) { |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 78 | FakeVariable<string> fake_string_var_("fake_string", kVariableModePoll); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 79 | const int* p_fake_int; |
| 80 | const string* p_fake_string; |
| 81 | |
| 82 | fake_int_var_.reset(new int(42)); |
| 83 | fake_string_var_.reset(new string("Hello world!")); |
| 84 | // Check that the EvaluationContext can handle multiple Variable types. This |
| 85 | // is mostly a compile-time check due to the template nature of this method. |
| 86 | p_fake_int = eval_ctx_->GetValue(&fake_int_var_); |
| 87 | p_fake_string = eval_ctx_->GetValue(&fake_string_var_); |
| 88 | |
| 89 | PMTEST_ASSERT_NOT_NULL(p_fake_int); |
| 90 | EXPECT_EQ(42, *p_fake_int); |
| 91 | |
| 92 | PMTEST_ASSERT_NOT_NULL(p_fake_string); |
| 93 | EXPECT_EQ("Hello world!", *p_fake_string); |
| 94 | } |
| 95 | |
| 96 | } // namespace chromeos_policy_manager |