blob: 15ea3d7dac9fc8ee61b1b642119c7499a28bc62c [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
5#include <gtest/gtest.h>
6#include "policy_manager/generic_variables.h"
7
8using base::TimeDelta;
9using std::string;
10
11namespace chromeos_policy_manager {
12
Gilad Arnoldb33e1982014-01-27 14:46:27 -080013TEST(PmCopyVariableTest, SimpleTest) {
Alex Deymo81f30e82014-01-08 14:33:06 -080014 int obj_int = 5;
15
Alex Deymo391ad9f2014-01-29 14:36:20 -080016 CopyVariable<int> var("var", obj_int);
Alex Deymo81f30e82014-01-08 14:33:06 -080017
18 string errmsg = "Nope";
19
20 const int* res_1 = var.GetValue(TimeDelta::FromSeconds(1), &errmsg);
21 EXPECT_NE(res_1, static_cast<void*>(NULL));
22 EXPECT_EQ(5, *res_1);
23
24 obj_int = 42;
25
26 // Check the result in res_1 is actually a new copy.
27 EXPECT_EQ(5, *res_1);
28
29 const int* res_2 = var.GetValue(TimeDelta::FromSeconds(1), &errmsg);
30 EXPECT_NE(res_2, static_cast<void*>(NULL));
31 EXPECT_EQ(42, *res_2);
32
33 delete res_1;
34 delete res_2;
35}
36
37class ConstructorTestClass {
38 public:
39 ConstructorTestClass(void) : copied_(false) {}
40
41 ConstructorTestClass(const ConstructorTestClass& /* ignored */)
42 : copied_(true) {}
43
44 // Tells if the instance was constructed using the copy-constructor.
45 bool copied_;
46};
47
Gilad Arnoldb33e1982014-01-27 14:46:27 -080048TEST(PmCopyVariableTest, UseCopyConstructorTest) {
Alex Deymo81f30e82014-01-08 14:33:06 -080049 ConstructorTestClass obj;
50 ASSERT_FALSE(obj.copied_);
51
52 string errmsg;
Alex Deymo391ad9f2014-01-29 14:36:20 -080053 CopyVariable<ConstructorTestClass> var("var", obj);
Alex Deymo81f30e82014-01-08 14:33:06 -080054 const ConstructorTestClass* value =
55 var.GetValue(TimeDelta::FromSeconds(1), &errmsg);
56 EXPECT_NE(value, static_cast<void*>(NULL));
57 EXPECT_TRUE(value->copied_);
58
59 delete value;
60}
61
62} // namespace chromeos_policy_manager