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 | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 11 | #include "update_engine/test_utils.h" |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 12 | |
| 13 | using base::TimeDelta; |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 14 | using chromeos_update_engine::RunGMainLoopMaxIterations; |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 15 | |
| 16 | namespace chromeos_policy_manager { |
| 17 | |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 18 | class PmCopyVariableTest : public ::testing::Test { |
| 19 | protected: |
Alex Deymo | bd04b14 | 2014-03-18 15:00:05 -0700 | [diff] [blame] | 20 | TimeDelta default_timeout_ = TimeDelta::FromSeconds(1); |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 21 | }; |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 22 | |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 23 | |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 24 | TEST_F(PmCopyVariableTest, SimpleTest) { |
| 25 | // Tests that copies are generated as intended. |
| 26 | int source = 5; |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 27 | CopyVariable<int> var("var", kVariableModePoll, source); |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 28 | |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 29 | // Generate and validate a copy. |
| 30 | scoped_ptr<const int> copy_1(var.GetValue(default_timeout_, NULL)); |
| 31 | PMTEST_ASSERT_NOT_NULL(copy_1.get()); |
| 32 | EXPECT_EQ(5, *copy_1); |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 33 | |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 34 | // Assign a different value to the source variable. |
| 35 | source = 42; |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 36 | |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 37 | // Check that the content of the copy was not affected (distinct instance). |
| 38 | EXPECT_EQ(5, *copy_1); |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 39 | |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 40 | // Generate and validate a second copy. |
| 41 | scoped_ptr<const int> copy_2(var.GetValue(default_timeout_, NULL)); |
| 42 | PMTEST_ASSERT_NOT_NULL(copy_2.get()); |
| 43 | EXPECT_EQ(42, *copy_2); |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Gilad Arnold | 9f7ab35 | 2014-04-16 15:27:37 -0700 | [diff] [blame] | 46 | TEST_F(PmCopyVariableTest, SetFlagTest) { |
| 47 | // Tests that the set flag is being referred to as expected. |
| 48 | int source = 5; |
| 49 | bool is_set = false; |
| 50 | CopyVariable<int> var("var", kVariableModePoll, source, &is_set); |
| 51 | |
| 52 | // Flag marked unset, nothing should be returned. |
| 53 | PMTEST_ASSERT_NULL(var.GetValue(default_timeout_, NULL)); |
| 54 | |
| 55 | // Flag marked set, we should be getting a value. |
| 56 | is_set = true; |
| 57 | scoped_ptr<const int> copy(var.GetValue(default_timeout_, NULL)); |
| 58 | PMTEST_ASSERT_NOT_NULL(copy.get()); |
| 59 | EXPECT_EQ(5, *copy); |
| 60 | } |
| 61 | |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 62 | |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 63 | class CopyConstructorTestClass { |
| 64 | public: |
| 65 | CopyConstructorTestClass(void) : copied_(false) {} |
| 66 | CopyConstructorTestClass(const CopyConstructorTestClass& /* ignored */) |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 67 | : copied_(true) {} |
| 68 | |
| 69 | // Tells if the instance was constructed using the copy-constructor. |
| 70 | bool copied_; |
| 71 | }; |
| 72 | |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 73 | |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 74 | TEST_F(PmCopyVariableTest, UseCopyConstructorTest) { |
| 75 | // Ensures that CopyVariables indeed uses the copy contructor. |
| 76 | const CopyConstructorTestClass source; |
| 77 | ASSERT_FALSE(source.copied_); |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 78 | |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 79 | CopyVariable<CopyConstructorTestClass> var("var", kVariableModePoll, source); |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 80 | scoped_ptr<const CopyConstructorTestClass> copy( |
| 81 | var.GetValue(default_timeout_, NULL)); |
| 82 | PMTEST_ASSERT_NOT_NULL(copy.get()); |
| 83 | EXPECT_TRUE(copy->copied_); |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Alex Deymo | bd04b14 | 2014-03-18 15:00:05 -0700 | [diff] [blame] | 86 | |
| 87 | class PmConstCopyVariableTest : public ::testing::Test { |
| 88 | protected: |
| 89 | TimeDelta default_timeout_ = TimeDelta::FromSeconds(1); |
| 90 | }; |
| 91 | |
| 92 | TEST_F(PmConstCopyVariableTest, SimpleTest) { |
| 93 | int source = 5; |
| 94 | ConstCopyVariable<int> var("var", source); |
| 95 | scoped_ptr<const int> copy(var.GetValue(default_timeout_, NULL)); |
| 96 | PMTEST_ASSERT_NOT_NULL(copy.get()); |
| 97 | EXPECT_EQ(5, *copy); |
| 98 | |
| 99 | // Ensure the value is cached. |
| 100 | source = 42; |
| 101 | copy.reset(var.GetValue(default_timeout_, NULL)); |
| 102 | PMTEST_ASSERT_NOT_NULL(copy.get()); |
| 103 | EXPECT_EQ(5, *copy); |
| 104 | } |
| 105 | |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 106 | |
| 107 | class PmAsyncCopyVariableTest : public ::testing::Test { |
| 108 | public: |
| 109 | void TearDown() { |
| 110 | // No remaining event on the main loop. |
| 111 | EXPECT_EQ(0, RunGMainLoopMaxIterations(1)); |
| 112 | } |
| 113 | |
| 114 | protected: |
| 115 | TimeDelta default_timeout_ = TimeDelta::FromSeconds(1); |
| 116 | }; |
| 117 | |
| 118 | TEST_F(PmAsyncCopyVariableTest, ConstructorTest) { |
| 119 | AsyncCopyVariable<int> var("var"); |
| 120 | PMTEST_EXPECT_NULL(var.GetValue(default_timeout_, NULL)); |
| 121 | EXPECT_EQ(kVariableModeAsync, var.GetMode()); |
| 122 | } |
| 123 | |
| 124 | TEST_F(PmAsyncCopyVariableTest, SetValueTest) { |
| 125 | AsyncCopyVariable<int> var("var"); |
| 126 | var.SetValue(5); |
| 127 | scoped_ptr<const int> copy(var.GetValue(default_timeout_, NULL)); |
| 128 | PMTEST_ASSERT_NOT_NULL(copy.get()); |
| 129 | EXPECT_EQ(5, *copy); |
| 130 | // Execute all the pending observers. |
| 131 | RunGMainLoopMaxIterations(100); |
| 132 | } |
| 133 | |
| 134 | TEST_F(PmAsyncCopyVariableTest, UnsetValueTest) { |
| 135 | AsyncCopyVariable<int> var("var", 42); |
| 136 | var.UnsetValue(); |
| 137 | PMTEST_EXPECT_NULL(var.GetValue(default_timeout_, NULL)); |
| 138 | // Execute all the pending observers. |
| 139 | RunGMainLoopMaxIterations(100); |
| 140 | } |
| 141 | |
| 142 | class CallCounterObserver : public BaseVariable::ObserverInterface { |
| 143 | public: |
| 144 | void ValueChanged(BaseVariable* variable) { |
| 145 | calls_count_++; |
| 146 | } |
| 147 | |
| 148 | int calls_count_ = 0; |
| 149 | }; |
| 150 | |
| 151 | TEST_F(PmAsyncCopyVariableTest, ObserverCalledTest) { |
| 152 | AsyncCopyVariable<int> var("var", 42); |
| 153 | CallCounterObserver observer; |
| 154 | var.AddObserver(&observer); |
| 155 | EXPECT_EQ(0, observer.calls_count_); |
| 156 | |
| 157 | // Check that a different value fires the notification. |
| 158 | var.SetValue(5); |
| 159 | RunGMainLoopMaxIterations(100); |
| 160 | EXPECT_EQ(1, observer.calls_count_); |
| 161 | |
| 162 | // Check the same value doesn't. |
| 163 | var.SetValue(5); |
| 164 | RunGMainLoopMaxIterations(100); |
| 165 | EXPECT_EQ(1, observer.calls_count_); |
| 166 | |
| 167 | // Check that unsetting a previously set value fires the notification. |
| 168 | var.UnsetValue(); |
| 169 | RunGMainLoopMaxIterations(100); |
| 170 | EXPECT_EQ(2, observer.calls_count_); |
| 171 | |
| 172 | // Check that unsetting again doesn't. |
| 173 | var.UnsetValue(); |
| 174 | RunGMainLoopMaxIterations(100); |
| 175 | EXPECT_EQ(2, observer.calls_count_); |
| 176 | |
| 177 | var.RemoveObserver(&observer); |
| 178 | } |
| 179 | |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 180 | } // namespace chromeos_policy_manager |