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 | c16fca2 | 2014-05-20 15:10:40 -0700 | [diff] [blame^] | 7 | #include <base/callback.h> |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 8 | #include <base/memory/scoped_ptr.h> |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 9 | #include <gtest/gtest.h> |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 10 | |
Alex Deymo | 6e97bb2 | 2014-02-05 16:46:16 -0800 | [diff] [blame] | 11 | #include "update_engine/policy_manager/pmtest_utils.h" |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 12 | #include "update_engine/test_utils.h" |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 13 | |
| 14 | using base::TimeDelta; |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 15 | using chromeos_update_engine::RunGMainLoopMaxIterations; |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 16 | |
| 17 | namespace chromeos_policy_manager { |
| 18 | |
Gilad Arnold | 67ed78d | 2014-04-23 13:17:46 -0700 | [diff] [blame] | 19 | class PmCopyVariableTest : public ::testing::Test {}; |
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. |
Gilad Arnold | 67ed78d | 2014-04-23 13:17:46 -0700 | [diff] [blame] | 28 | scoped_ptr<const int> copy_1(var.GetValue( |
| 29 | PmTestUtils::DefaultTimeout(), NULL)); |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 30 | PMTEST_ASSERT_NOT_NULL(copy_1.get()); |
| 31 | EXPECT_EQ(5, *copy_1); |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 32 | |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 33 | // Assign a different value to the source variable. |
| 34 | source = 42; |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 35 | |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 36 | // Check that the content of the copy was not affected (distinct instance). |
| 37 | EXPECT_EQ(5, *copy_1); |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 38 | |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 39 | // Generate and validate a second copy. |
Gilad Arnold | 67ed78d | 2014-04-23 13:17:46 -0700 | [diff] [blame] | 40 | PmTestUtils::ExpectVariableHasValue(42, &var); |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 41 | } |
| 42 | |
Gilad Arnold | 9f7ab35 | 2014-04-16 15:27:37 -0700 | [diff] [blame] | 43 | TEST_F(PmCopyVariableTest, SetFlagTest) { |
| 44 | // Tests that the set flag is being referred to as expected. |
| 45 | int source = 5; |
| 46 | bool is_set = false; |
| 47 | CopyVariable<int> var("var", kVariableModePoll, source, &is_set); |
| 48 | |
| 49 | // Flag marked unset, nothing should be returned. |
Gilad Arnold | 67ed78d | 2014-04-23 13:17:46 -0700 | [diff] [blame] | 50 | PmTestUtils::ExpectVariableNotSet(&var); |
Gilad Arnold | 9f7ab35 | 2014-04-16 15:27:37 -0700 | [diff] [blame] | 51 | |
| 52 | // Flag marked set, we should be getting a value. |
| 53 | is_set = true; |
Gilad Arnold | 67ed78d | 2014-04-23 13:17:46 -0700 | [diff] [blame] | 54 | PmTestUtils::ExpectVariableHasValue(5, &var); |
Gilad Arnold | 9f7ab35 | 2014-04-16 15:27:37 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 57 | |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 58 | class CopyConstructorTestClass { |
| 59 | public: |
| 60 | CopyConstructorTestClass(void) : copied_(false) {} |
Gilad Arnold | c16fca2 | 2014-05-20 15:10:40 -0700 | [diff] [blame^] | 61 | CopyConstructorTestClass(const CopyConstructorTestClass& other) |
| 62 | : copied_(true), val_(other.val_ * 2) {} |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 63 | |
| 64 | // Tells if the instance was constructed using the copy-constructor. |
Gilad Arnold | c16fca2 | 2014-05-20 15:10:40 -0700 | [diff] [blame^] | 65 | const bool copied_; |
| 66 | |
| 67 | // An auxiliary internal value. |
| 68 | int val_ = 0; |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 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( |
Gilad Arnold | 67ed78d | 2014-04-23 13:17:46 -0700 | [diff] [blame] | 79 | var.GetValue(PmTestUtils::DefaultTimeout(), NULL)); |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 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 | |
Gilad Arnold | 67ed78d | 2014-04-23 13:17:46 -0700 | [diff] [blame] | 85 | class PmConstCopyVariableTest : public ::testing::Test {}; |
Alex Deymo | bd04b14 | 2014-03-18 15:00:05 -0700 | [diff] [blame] | 86 | |
| 87 | TEST_F(PmConstCopyVariableTest, SimpleTest) { |
| 88 | int source = 5; |
| 89 | ConstCopyVariable<int> var("var", source); |
Gilad Arnold | 67ed78d | 2014-04-23 13:17:46 -0700 | [diff] [blame] | 90 | PmTestUtils::ExpectVariableHasValue(5, &var); |
Alex Deymo | bd04b14 | 2014-03-18 15:00:05 -0700 | [diff] [blame] | 91 | |
| 92 | // Ensure the value is cached. |
| 93 | source = 42; |
Gilad Arnold | 67ed78d | 2014-04-23 13:17:46 -0700 | [diff] [blame] | 94 | PmTestUtils::ExpectVariableHasValue(5, &var); |
Alex Deymo | bd04b14 | 2014-03-18 15:00:05 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 97 | |
Gilad Arnold | c16fca2 | 2014-05-20 15:10:40 -0700 | [diff] [blame^] | 98 | class PmCallCopyVariableTest : public ::testing::Test {}; |
| 99 | |
| 100 | CopyConstructorTestClass test_func(CopyConstructorTestClass* obj) { |
| 101 | obj->val_++; // So we can check that the function was called. |
| 102 | return *obj; |
| 103 | } |
| 104 | |
| 105 | TEST_F(PmCallCopyVariableTest, SimpleTest) { |
| 106 | // Tests that the returned value is generated by copying the value returned by |
| 107 | // the function call. |
| 108 | |
| 109 | CopyConstructorTestClass test_obj; |
| 110 | ASSERT_FALSE(test_obj.copied_); |
| 111 | test_obj.val_ = 5; |
| 112 | |
| 113 | base::Callback<CopyConstructorTestClass(void)> cb = base::Bind( |
| 114 | test_func, &test_obj); |
| 115 | CallCopyVariable<CopyConstructorTestClass> var("var", cb); |
| 116 | |
| 117 | scoped_ptr<const CopyConstructorTestClass> copy( |
| 118 | var.GetValue(PmTestUtils::DefaultTimeout(), nullptr)); |
| 119 | EXPECT_EQ(6, test_obj.val_); // Check that the function was called. |
| 120 | PMTEST_ASSERT_NOT_NULL(copy.get()); |
| 121 | EXPECT_TRUE(copy->copied_); |
| 122 | EXPECT_EQ(12, copy->val_); // Check that copying occurred once. |
| 123 | } |
| 124 | |
| 125 | TEST_F(PmCallCopyVariableTest, NullTest) { |
| 126 | // Ensures that the variable returns null when the callback is null. |
| 127 | |
| 128 | base::Callback<bool(void)> cb; |
| 129 | CallCopyVariable<bool> var("var", cb); |
| 130 | PmTestUtils::ExpectVariableNotSet(&var); |
| 131 | } |
| 132 | |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 133 | class PmAsyncCopyVariableTest : public ::testing::Test { |
| 134 | public: |
| 135 | void TearDown() { |
| 136 | // No remaining event on the main loop. |
| 137 | EXPECT_EQ(0, RunGMainLoopMaxIterations(1)); |
| 138 | } |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | TEST_F(PmAsyncCopyVariableTest, ConstructorTest) { |
| 142 | AsyncCopyVariable<int> var("var"); |
Gilad Arnold | 67ed78d | 2014-04-23 13:17:46 -0700 | [diff] [blame] | 143 | PmTestUtils::ExpectVariableNotSet(&var); |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 144 | EXPECT_EQ(kVariableModeAsync, var.GetMode()); |
| 145 | } |
| 146 | |
| 147 | TEST_F(PmAsyncCopyVariableTest, SetValueTest) { |
| 148 | AsyncCopyVariable<int> var("var"); |
| 149 | var.SetValue(5); |
Gilad Arnold | 67ed78d | 2014-04-23 13:17:46 -0700 | [diff] [blame] | 150 | PmTestUtils::ExpectVariableHasValue(5, &var); |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 151 | // Execute all the pending observers. |
| 152 | RunGMainLoopMaxIterations(100); |
| 153 | } |
| 154 | |
| 155 | TEST_F(PmAsyncCopyVariableTest, UnsetValueTest) { |
| 156 | AsyncCopyVariable<int> var("var", 42); |
| 157 | var.UnsetValue(); |
Gilad Arnold | 67ed78d | 2014-04-23 13:17:46 -0700 | [diff] [blame] | 158 | PmTestUtils::ExpectVariableNotSet(&var); |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 159 | // Execute all the pending observers. |
| 160 | RunGMainLoopMaxIterations(100); |
| 161 | } |
| 162 | |
| 163 | class CallCounterObserver : public BaseVariable::ObserverInterface { |
| 164 | public: |
| 165 | void ValueChanged(BaseVariable* variable) { |
| 166 | calls_count_++; |
| 167 | } |
| 168 | |
| 169 | int calls_count_ = 0; |
| 170 | }; |
| 171 | |
| 172 | TEST_F(PmAsyncCopyVariableTest, ObserverCalledTest) { |
| 173 | AsyncCopyVariable<int> var("var", 42); |
| 174 | CallCounterObserver observer; |
| 175 | var.AddObserver(&observer); |
| 176 | EXPECT_EQ(0, observer.calls_count_); |
| 177 | |
| 178 | // Check that a different value fires the notification. |
| 179 | var.SetValue(5); |
| 180 | RunGMainLoopMaxIterations(100); |
| 181 | EXPECT_EQ(1, observer.calls_count_); |
| 182 | |
| 183 | // Check the same value doesn't. |
| 184 | var.SetValue(5); |
| 185 | RunGMainLoopMaxIterations(100); |
| 186 | EXPECT_EQ(1, observer.calls_count_); |
| 187 | |
| 188 | // Check that unsetting a previously set value fires the notification. |
| 189 | var.UnsetValue(); |
| 190 | RunGMainLoopMaxIterations(100); |
| 191 | EXPECT_EQ(2, observer.calls_count_); |
| 192 | |
| 193 | // Check that unsetting again doesn't. |
| 194 | var.UnsetValue(); |
| 195 | RunGMainLoopMaxIterations(100); |
| 196 | EXPECT_EQ(2, observer.calls_count_); |
| 197 | |
| 198 | var.RemoveObserver(&observer); |
| 199 | } |
| 200 | |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 201 | } // namespace chromeos_policy_manager |