blob: 6758e232b92601fa054fac53e921beaa640ee7ca [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
13TEST(PMCopyVariableTest, SimpleTest) {
14 int obj_int = 5;
15
16 CopyVariable<int> var(obj_int);
17
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
48TEST(PMCopyVariableTest, UseCopyConstructorTest) {
49 ConstructorTestClass obj;
50 ASSERT_FALSE(obj.copied_);
51
52 string errmsg;
53 CopyVariable<ConstructorTestClass> var(obj);
54 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