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 | |
Alex Deymo | bd04b14 | 2014-03-18 15:00:05 -0700 | [diff] [blame^] | 5 | #include "update_engine/policy_manager/generic_variables.h" |
| 6 | |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 7 | #include <base/memory/scoped_ptr.h> |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 8 | #include <gtest/gtest.h> |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 9 | |
Alex Deymo | 6e97bb2 | 2014-02-05 16:46:16 -0800 | [diff] [blame] | 10 | #include "update_engine/policy_manager/pmtest_utils.h" |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 11 | |
| 12 | using base::TimeDelta; |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 13 | |
| 14 | namespace chromeos_policy_manager { |
| 15 | |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 16 | class PmCopyVariableTest : public ::testing::Test { |
| 17 | protected: |
Alex Deymo | bd04b14 | 2014-03-18 15:00:05 -0700 | [diff] [blame^] | 18 | TimeDelta default_timeout_ = TimeDelta::FromSeconds(1); |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 19 | }; |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 20 | |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 21 | |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 22 | TEST_F(PmCopyVariableTest, SimpleTest) { |
| 23 | // Tests that copies are generated as intended. |
| 24 | int source = 5; |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 25 | CopyVariable<int> var("var", kVariableModePoll, source); |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 26 | |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 27 | // Generate and validate a copy. |
| 28 | scoped_ptr<const int> copy_1(var.GetValue(default_timeout_, NULL)); |
| 29 | PMTEST_ASSERT_NOT_NULL(copy_1.get()); |
| 30 | EXPECT_EQ(5, *copy_1); |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 31 | |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 32 | // Assign a different value to the source variable. |
| 33 | source = 42; |
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 | // Check that the content of the copy was not affected (distinct instance). |
| 36 | EXPECT_EQ(5, *copy_1); |
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 | // Generate and validate a second copy. |
| 39 | scoped_ptr<const int> copy_2(var.GetValue(default_timeout_, NULL)); |
| 40 | PMTEST_ASSERT_NOT_NULL(copy_2.get()); |
| 41 | EXPECT_EQ(42, *copy_2); |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 42 | } |
| 43 | |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 44 | |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 45 | class CopyConstructorTestClass { |
| 46 | public: |
| 47 | CopyConstructorTestClass(void) : copied_(false) {} |
| 48 | CopyConstructorTestClass(const CopyConstructorTestClass& /* ignored */) |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 49 | : copied_(true) {} |
| 50 | |
| 51 | // Tells if the instance was constructed using the copy-constructor. |
| 52 | bool copied_; |
| 53 | }; |
| 54 | |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 55 | |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 56 | TEST_F(PmCopyVariableTest, UseCopyConstructorTest) { |
| 57 | // Ensures that CopyVariables indeed uses the copy contructor. |
| 58 | const CopyConstructorTestClass source; |
| 59 | ASSERT_FALSE(source.copied_); |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 60 | |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 61 | CopyVariable<CopyConstructorTestClass> var("var", kVariableModePoll, source); |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 62 | scoped_ptr<const CopyConstructorTestClass> copy( |
| 63 | var.GetValue(default_timeout_, NULL)); |
| 64 | PMTEST_ASSERT_NOT_NULL(copy.get()); |
| 65 | EXPECT_TRUE(copy->copied_); |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Alex Deymo | bd04b14 | 2014-03-18 15:00:05 -0700 | [diff] [blame^] | 68 | |
| 69 | class PmConstCopyVariableTest : public ::testing::Test { |
| 70 | protected: |
| 71 | TimeDelta default_timeout_ = TimeDelta::FromSeconds(1); |
| 72 | }; |
| 73 | |
| 74 | TEST_F(PmConstCopyVariableTest, SimpleTest) { |
| 75 | int source = 5; |
| 76 | ConstCopyVariable<int> var("var", source); |
| 77 | scoped_ptr<const int> copy(var.GetValue(default_timeout_, NULL)); |
| 78 | PMTEST_ASSERT_NOT_NULL(copy.get()); |
| 79 | EXPECT_EQ(5, *copy); |
| 80 | |
| 81 | // Ensure the value is cached. |
| 82 | source = 42; |
| 83 | copy.reset(var.GetValue(default_timeout_, NULL)); |
| 84 | PMTEST_ASSERT_NOT_NULL(copy.get()); |
| 85 | EXPECT_EQ(5, *copy); |
| 86 | } |
| 87 | |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 88 | } // namespace chromeos_policy_manager |