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 | |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 5 | #include <base/memory/scoped_ptr.h> |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 6 | #include <gtest/gtest.h> |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 7 | |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 8 | #include "policy_manager/generic_variables.h" |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 9 | #include "policy_manager/pmtest_utils.h" |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 10 | |
| 11 | using base::TimeDelta; |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 12 | |
| 13 | namespace chromeos_policy_manager { |
| 14 | |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 15 | class PmCopyVariableTest : public ::testing::Test { |
| 16 | protected: |
| 17 | virtual void SetUp() { |
| 18 | default_timeout_ = TimeDelta::FromSeconds(1); |
| 19 | } |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 20 | |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 21 | TimeDelta default_timeout_; |
| 22 | }; |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 23 | |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 24 | |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 25 | TEST_F(PmCopyVariableTest, SimpleTest) { |
| 26 | // Tests that copies are generated as intended. |
| 27 | int source = 5; |
| 28 | CopyVariable<int> var("var", source); |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 29 | |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 30 | // 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 Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 34 | |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 35 | // Assign a different value to the source variable. |
| 36 | source = 42; |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 37 | |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 38 | // Check that the content of the copy was not affected (distinct instance). |
| 39 | EXPECT_EQ(5, *copy_1); |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 40 | |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 41 | // 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 Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 47 | |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 48 | class CopyConstructorTestClass { |
| 49 | public: |
| 50 | CopyConstructorTestClass(void) : copied_(false) {} |
| 51 | CopyConstructorTestClass(const CopyConstructorTestClass& /* ignored */) |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 52 | : copied_(true) {} |
| 53 | |
| 54 | // Tells if the instance was constructed using the copy-constructor. |
| 55 | bool copied_; |
| 56 | }; |
| 57 | |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 58 | |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 59 | TEST_F(PmCopyVariableTest, UseCopyConstructorTest) { |
| 60 | // Ensures that CopyVariables indeed uses the copy contructor. |
| 61 | const CopyConstructorTestClass source; |
| 62 | ASSERT_FALSE(source.copied_); |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 63 | |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 64 | 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 Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | } // namespace chromeos_policy_manager |