blob: 7783e9fa8fb1aed282b3d4750e2422336de005b9 [file] [log] [blame]
Alex Deymo81f30e82014-01-08 14:33:06 -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
Alex Deymobd04b142014-03-18 15:00:05 -07005#include "update_engine/policy_manager/generic_variables.h"
6
Gilad Arnolda87340b2014-01-30 11:10:18 -08007#include <base/memory/scoped_ptr.h>
Alex Deymo81f30e82014-01-08 14:33:06 -08008#include <gtest/gtest.h>
Gilad Arnolda87340b2014-01-30 11:10:18 -08009
Alex Deymo6e97bb22014-02-05 16:46:16 -080010#include "update_engine/policy_manager/pmtest_utils.h"
Alex Deymo81f30e82014-01-08 14:33:06 -080011
12using base::TimeDelta;
Alex Deymo81f30e82014-01-08 14:33:06 -080013
14namespace chromeos_policy_manager {
15
Gilad Arnolda87340b2014-01-30 11:10:18 -080016class PmCopyVariableTest : public ::testing::Test {
17 protected:
Alex Deymobd04b142014-03-18 15:00:05 -070018 TimeDelta default_timeout_ = TimeDelta::FromSeconds(1);
Gilad Arnolda87340b2014-01-30 11:10:18 -080019};
Alex Deymo81f30e82014-01-08 14:33:06 -080020
Alex Deymo81f30e82014-01-08 14:33:06 -080021
Gilad Arnolda87340b2014-01-30 11:10:18 -080022TEST_F(PmCopyVariableTest, SimpleTest) {
23 // Tests that copies are generated as intended.
24 int source = 5;
Alex Deymo0e433692014-02-20 07:23:03 -080025 CopyVariable<int> var("var", kVariableModePoll, source);
Alex Deymo81f30e82014-01-08 14:33:06 -080026
Gilad Arnolda87340b2014-01-30 11:10:18 -080027 // Generate and validate a copy.
28 scoped_ptr<const int> copy_1(var.GetValue(default_timeout_, NULL));
29 PMTEST_ASSERT_NOT_NULL(copy_1.get());
30 EXPECT_EQ(5, *copy_1);
Alex Deymo81f30e82014-01-08 14:33:06 -080031
Gilad Arnolda87340b2014-01-30 11:10:18 -080032 // Assign a different value to the source variable.
33 source = 42;
Alex Deymo81f30e82014-01-08 14:33:06 -080034
Gilad Arnolda87340b2014-01-30 11:10:18 -080035 // Check that the content of the copy was not affected (distinct instance).
36 EXPECT_EQ(5, *copy_1);
Alex Deymo81f30e82014-01-08 14:33:06 -080037
Gilad Arnolda87340b2014-01-30 11:10:18 -080038 // Generate and validate a second copy.
39 scoped_ptr<const int> copy_2(var.GetValue(default_timeout_, NULL));
40 PMTEST_ASSERT_NOT_NULL(copy_2.get());
41 EXPECT_EQ(42, *copy_2);
Alex Deymo81f30e82014-01-08 14:33:06 -080042}
43
Gilad Arnold9f7ab352014-04-16 15:27:37 -070044TEST_F(PmCopyVariableTest, SetFlagTest) {
45 // Tests that the set flag is being referred to as expected.
46 int source = 5;
47 bool is_set = false;
48 CopyVariable<int> var("var", kVariableModePoll, source, &is_set);
49
50 // Flag marked unset, nothing should be returned.
51 PMTEST_ASSERT_NULL(var.GetValue(default_timeout_, NULL));
52
53 // Flag marked set, we should be getting a value.
54 is_set = true;
55 scoped_ptr<const int> copy(var.GetValue(default_timeout_, NULL));
56 PMTEST_ASSERT_NOT_NULL(copy.get());
57 EXPECT_EQ(5, *copy);
58}
59
Alex Deymo81f30e82014-01-08 14:33:06 -080060
Gilad Arnolda87340b2014-01-30 11:10:18 -080061class CopyConstructorTestClass {
62 public:
63 CopyConstructorTestClass(void) : copied_(false) {}
64 CopyConstructorTestClass(const CopyConstructorTestClass& /* ignored */)
Alex Deymo81f30e82014-01-08 14:33:06 -080065 : copied_(true) {}
66
67 // Tells if the instance was constructed using the copy-constructor.
68 bool copied_;
69};
70
Alex Deymo81f30e82014-01-08 14:33:06 -080071
Gilad Arnolda87340b2014-01-30 11:10:18 -080072TEST_F(PmCopyVariableTest, UseCopyConstructorTest) {
73 // Ensures that CopyVariables indeed uses the copy contructor.
74 const CopyConstructorTestClass source;
75 ASSERT_FALSE(source.copied_);
Alex Deymo81f30e82014-01-08 14:33:06 -080076
Alex Deymo0e433692014-02-20 07:23:03 -080077 CopyVariable<CopyConstructorTestClass> var("var", kVariableModePoll, source);
Gilad Arnolda87340b2014-01-30 11:10:18 -080078 scoped_ptr<const CopyConstructorTestClass> copy(
79 var.GetValue(default_timeout_, NULL));
80 PMTEST_ASSERT_NOT_NULL(copy.get());
81 EXPECT_TRUE(copy->copied_);
Alex Deymo81f30e82014-01-08 14:33:06 -080082}
83
Alex Deymobd04b142014-03-18 15:00:05 -070084
85class PmConstCopyVariableTest : public ::testing::Test {
86 protected:
87 TimeDelta default_timeout_ = TimeDelta::FromSeconds(1);
88};
89
90TEST_F(PmConstCopyVariableTest, SimpleTest) {
91 int source = 5;
92 ConstCopyVariable<int> var("var", source);
93 scoped_ptr<const int> copy(var.GetValue(default_timeout_, NULL));
94 PMTEST_ASSERT_NOT_NULL(copy.get());
95 EXPECT_EQ(5, *copy);
96
97 // Ensure the value is cached.
98 source = 42;
99 copy.reset(var.GetValue(default_timeout_, NULL));
100 PMTEST_ASSERT_NOT_NULL(copy.get());
101 EXPECT_EQ(5, *copy);
102}
103
Alex Deymo81f30e82014-01-08 14:33:06 -0800104} // namespace chromeos_policy_manager