blob: 96de97e93992db8e7a7df46f89e356c92de86d86 [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
Alex Deymo81f30e82014-01-08 14:33:06 -080044
Gilad Arnolda87340b2014-01-30 11:10:18 -080045class CopyConstructorTestClass {
46 public:
47 CopyConstructorTestClass(void) : copied_(false) {}
48 CopyConstructorTestClass(const CopyConstructorTestClass& /* ignored */)
Alex Deymo81f30e82014-01-08 14:33:06 -080049 : copied_(true) {}
50
51 // Tells if the instance was constructed using the copy-constructor.
52 bool copied_;
53};
54
Alex Deymo81f30e82014-01-08 14:33:06 -080055
Gilad Arnolda87340b2014-01-30 11:10:18 -080056TEST_F(PmCopyVariableTest, UseCopyConstructorTest) {
57 // Ensures that CopyVariables indeed uses the copy contructor.
58 const CopyConstructorTestClass source;
59 ASSERT_FALSE(source.copied_);
Alex Deymo81f30e82014-01-08 14:33:06 -080060
Alex Deymo0e433692014-02-20 07:23:03 -080061 CopyVariable<CopyConstructorTestClass> var("var", kVariableModePoll, source);
Gilad Arnolda87340b2014-01-30 11:10:18 -080062 scoped_ptr<const CopyConstructorTestClass> copy(
63 var.GetValue(default_timeout_, NULL));
64 PMTEST_ASSERT_NOT_NULL(copy.get());
65 EXPECT_TRUE(copy->copied_);
Alex Deymo81f30e82014-01-08 14:33:06 -080066}
67
Alex Deymobd04b142014-03-18 15:00:05 -070068
69class PmConstCopyVariableTest : public ::testing::Test {
70 protected:
71 TimeDelta default_timeout_ = TimeDelta::FromSeconds(1);
72};
73
74TEST_F(PmConstCopyVariableTest, SimpleTest) {
75 int source = 5;
76 ConstCopyVariable<int> var("var", source);
77 scoped_ptr<const int> copy(var.GetValue(default_timeout_, NULL));
78 PMTEST_ASSERT_NOT_NULL(copy.get());
79 EXPECT_EQ(5, *copy);
80
81 // Ensure the value is cached.
82 source = 42;
83 copy.reset(var.GetValue(default_timeout_, NULL));
84 PMTEST_ASSERT_NOT_NULL(copy.get());
85 EXPECT_EQ(5, *copy);
86}
87
Alex Deymo81f30e82014-01-08 14:33:06 -080088} // namespace chromeos_policy_manager