blob: e14dc509f24ee6d766c7fce48ff1cecae32c1559 [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
Gilad Arnolda87340b2014-01-30 11:10:18 -08005#include <base/memory/scoped_ptr.h>
Alex Deymo81f30e82014-01-08 14:33:06 -08006#include <gtest/gtest.h>
Gilad Arnolda87340b2014-01-30 11:10:18 -08007
Alex Deymo81f30e82014-01-08 14:33:06 -08008#include "policy_manager/generic_variables.h"
Gilad Arnolda87340b2014-01-30 11:10:18 -08009#include "policy_manager/pmtest_utils.h"
Alex Deymo81f30e82014-01-08 14:33:06 -080010
11using base::TimeDelta;
Alex Deymo81f30e82014-01-08 14:33:06 -080012
13namespace chromeos_policy_manager {
14
Gilad Arnolda87340b2014-01-30 11:10:18 -080015class PmCopyVariableTest : public ::testing::Test {
16 protected:
17 virtual void SetUp() {
18 default_timeout_ = TimeDelta::FromSeconds(1);
19 }
Alex Deymo81f30e82014-01-08 14:33:06 -080020
Gilad Arnolda87340b2014-01-30 11:10:18 -080021 TimeDelta default_timeout_;
22};
Alex Deymo81f30e82014-01-08 14:33:06 -080023
Alex Deymo81f30e82014-01-08 14:33:06 -080024
Gilad Arnolda87340b2014-01-30 11:10:18 -080025TEST_F(PmCopyVariableTest, SimpleTest) {
26 // Tests that copies are generated as intended.
27 int source = 5;
28 CopyVariable<int> var("var", source);
Alex Deymo81f30e82014-01-08 14:33:06 -080029
Gilad Arnolda87340b2014-01-30 11:10:18 -080030 // Generate and validate a copy.
31 scoped_ptr<const int> copy_1(var.GetValue(default_timeout_, NULL));
32 PMTEST_ASSERT_NOT_NULL(copy_1.get());
33 EXPECT_EQ(5, *copy_1);
Alex Deymo81f30e82014-01-08 14:33:06 -080034
Gilad Arnolda87340b2014-01-30 11:10:18 -080035 // Assign a different value to the source variable.
36 source = 42;
Alex Deymo81f30e82014-01-08 14:33:06 -080037
Gilad Arnolda87340b2014-01-30 11:10:18 -080038 // Check that the content of the copy was not affected (distinct instance).
39 EXPECT_EQ(5, *copy_1);
Alex Deymo81f30e82014-01-08 14:33:06 -080040
Gilad Arnolda87340b2014-01-30 11:10:18 -080041 // Generate and validate a second copy.
42 scoped_ptr<const int> copy_2(var.GetValue(default_timeout_, NULL));
43 PMTEST_ASSERT_NOT_NULL(copy_2.get());
44 EXPECT_EQ(42, *copy_2);
Alex Deymo81f30e82014-01-08 14:33:06 -080045}
46
Alex Deymo81f30e82014-01-08 14:33:06 -080047
Gilad Arnolda87340b2014-01-30 11:10:18 -080048class CopyConstructorTestClass {
49 public:
50 CopyConstructorTestClass(void) : copied_(false) {}
51 CopyConstructorTestClass(const CopyConstructorTestClass& /* ignored */)
Alex Deymo81f30e82014-01-08 14:33:06 -080052 : copied_(true) {}
53
54 // Tells if the instance was constructed using the copy-constructor.
55 bool copied_;
56};
57
Alex Deymo81f30e82014-01-08 14:33:06 -080058
Gilad Arnolda87340b2014-01-30 11:10:18 -080059TEST_F(PmCopyVariableTest, UseCopyConstructorTest) {
60 // Ensures that CopyVariables indeed uses the copy contructor.
61 const CopyConstructorTestClass source;
62 ASSERT_FALSE(source.copied_);
Alex Deymo81f30e82014-01-08 14:33:06 -080063
Gilad Arnolda87340b2014-01-30 11:10:18 -080064 CopyVariable<CopyConstructorTestClass> var("var", source);
65 scoped_ptr<const CopyConstructorTestClass> copy(
66 var.GetValue(default_timeout_, NULL));
67 PMTEST_ASSERT_NOT_NULL(copy.get());
68 EXPECT_TRUE(copy->copied_);
Alex Deymo81f30e82014-01-08 14:33:06 -080069}
70
71} // namespace chromeos_policy_manager