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