blob: 6ef6f74fe873d8773a862d30fc76d5da184f94a0 [file] [log] [blame]
Alex Deymo23949d42014-02-05 15:20:59 -08001// 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
14using std::string;
15
16namespace chromeos_policy_manager {
17
18class PmEvaluationContextTest : public ::testing::Test {
19 public:
Alex Deymo0e433692014-02-20 07:23:03 -080020 PmEvaluationContextTest() : fake_int_var_("fake_int", kVariableModePoll) {}
Alex Deymo23949d42014-02-05 15:20:59 -080021
22 protected:
23 virtual void SetUp() {
Alex Deymo7b948f02014-03-10 17:01:10 -070024 eval_ctx_ = new EvaluationContext();
Alex Deymo23949d42014-02-05 15:20:59 -080025 }
26
Alex Deymo7b948f02014-03-10 17:01:10 -070027 scoped_refptr<EvaluationContext> eval_ctx_;
Alex Deymo23949d42014-02-05 15:20:59 -080028 FakeVariable<int> fake_int_var_;
29};
30
31TEST_F(PmEvaluationContextTest, GetValueFails) {
32 // FakeVariable is initialized as returning NULL.
33 PMTEST_EXPECT_NULL(eval_ctx_->GetValue(&fake_int_var_));
34}
35
36TEST_F(PmEvaluationContextTest, GetValueFailsWithInvalidVar) {
37 PMTEST_EXPECT_NULL(eval_ctx_->GetValue(
38 reinterpret_cast<Variable<int>*>(NULL)));
39}
40
41TEST_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
50TEST_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
65TEST_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
77TEST_F(PmEvaluationContextTest, GetValueMixedTypes) {
Alex Deymo0e433692014-02-20 07:23:03 -080078 FakeVariable<string> fake_string_var_("fake_string", kVariableModePoll);
Alex Deymo23949d42014-02-05 15:20:59 -080079 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