Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame^] | 1 | // 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 | |
| 8 | using base::TimeDelta; |
| 9 | using std::string; |
| 10 | |
| 11 | namespace chromeos_policy_manager { |
| 12 | |
| 13 | TEST(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 | |
| 37 | class 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 | |
| 48 | TEST(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 |