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 | |
Gilad Arnold | 9f7ab35 | 2014-04-16 15:27:37 -0700 | [diff] [blame^] | 44 | TEST_F(PmCopyVariableTest, SetFlagTest) { |
| 45 | // Tests that the set flag is being referred to as expected. |
| 46 | int source = 5; |
| 47 | bool is_set = false; |
| 48 | CopyVariable<int> var("var", kVariableModePoll, source, &is_set); |
| 49 | |
| 50 | // Flag marked unset, nothing should be returned. |
| 51 | PMTEST_ASSERT_NULL(var.GetValue(default_timeout_, NULL)); |
| 52 | |
| 53 | // Flag marked set, we should be getting a value. |
| 54 | is_set = true; |
| 55 | scoped_ptr<const int> copy(var.GetValue(default_timeout_, NULL)); |
| 56 | PMTEST_ASSERT_NOT_NULL(copy.get()); |
| 57 | EXPECT_EQ(5, *copy); |
| 58 | } |
| 59 | |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 60 | |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 61 | class CopyConstructorTestClass { |
| 62 | public: |
| 63 | CopyConstructorTestClass(void) : copied_(false) {} |
| 64 | CopyConstructorTestClass(const CopyConstructorTestClass& /* ignored */) |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 65 | : copied_(true) {} |
| 66 | |
| 67 | // Tells if the instance was constructed using the copy-constructor. |
| 68 | bool copied_; |
| 69 | }; |
| 70 | |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 71 | |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 72 | TEST_F(PmCopyVariableTest, UseCopyConstructorTest) { |
| 73 | // Ensures that CopyVariables indeed uses the copy contructor. |
| 74 | const CopyConstructorTestClass source; |
| 75 | ASSERT_FALSE(source.copied_); |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 76 | |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 77 | CopyVariable<CopyConstructorTestClass> var("var", kVariableModePoll, source); |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 78 | scoped_ptr<const CopyConstructorTestClass> copy( |
| 79 | var.GetValue(default_timeout_, NULL)); |
| 80 | PMTEST_ASSERT_NOT_NULL(copy.get()); |
| 81 | EXPECT_TRUE(copy->copied_); |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Alex Deymo | bd04b14 | 2014-03-18 15:00:05 -0700 | [diff] [blame] | 84 | |
| 85 | class PmConstCopyVariableTest : public ::testing::Test { |
| 86 | protected: |
| 87 | TimeDelta default_timeout_ = TimeDelta::FromSeconds(1); |
| 88 | }; |
| 89 | |
| 90 | TEST_F(PmConstCopyVariableTest, SimpleTest) { |
| 91 | int source = 5; |
| 92 | ConstCopyVariable<int> var("var", source); |
| 93 | scoped_ptr<const int> copy(var.GetValue(default_timeout_, NULL)); |
| 94 | PMTEST_ASSERT_NOT_NULL(copy.get()); |
| 95 | EXPECT_EQ(5, *copy); |
| 96 | |
| 97 | // Ensure the value is cached. |
| 98 | source = 42; |
| 99 | copy.reset(var.GetValue(default_timeout_, NULL)); |
| 100 | PMTEST_ASSERT_NOT_NULL(copy.get()); |
| 101 | EXPECT_EQ(5, *copy); |
| 102 | } |
| 103 | |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 104 | } // namespace chromeos_policy_manager |