Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2014 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 16 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 17 | #include "update_engine/update_manager/variable.h" |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 18 | |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 19 | #include <vector> |
| 20 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 21 | #include <brillo/message_loops/fake_message_loop.h> |
| 22 | #include <brillo/message_loops/message_loop.h> |
| 23 | #include <brillo/message_loops/message_loop_utils.h> |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 24 | #include <gtest/gtest.h> |
| 25 | |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame] | 26 | using base::TimeDelta; |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 27 | using brillo::MessageLoop; |
| 28 | using brillo::MessageLoopRunMaxIterations; |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 29 | using std::string; |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 30 | using std::vector; |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 31 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 32 | namespace chromeos_update_manager { |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 33 | |
| 34 | // Variable class that returns a value constructed with the default value. |
| 35 | template <typename T> |
| 36 | class DefaultVariable : public Variable<T> { |
| 37 | public: |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 38 | DefaultVariable(const string& name, VariableMode mode) |
| 39 | : Variable<T>(name, mode) {} |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame] | 40 | DefaultVariable(const string& name, const TimeDelta& poll_interval) |
| 41 | : Variable<T>(name, poll_interval) {} |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 42 | ~DefaultVariable() override {} |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 43 | |
| 44 | protected: |
Yunlian Jiang | 35866ed | 2015-01-29 13:09:20 -0800 | [diff] [blame] | 45 | const T* GetValue(TimeDelta /* timeout */, |
| 46 | string* /* errmsg */) override { |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 47 | return new T(); |
| 48 | } |
| 49 | |
| 50 | private: |
| 51 | DISALLOW_COPY_AND_ASSIGN(DefaultVariable); |
| 52 | }; |
| 53 | |
Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 54 | class UmBaseVariableTest : public ::testing::Test { |
| 55 | protected: |
| 56 | void SetUp() override { |
| 57 | loop_.SetAsCurrent(); |
| 58 | } |
| 59 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 60 | brillo::FakeMessageLoop loop_{nullptr}; |
Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | TEST_F(UmBaseVariableTest, GetNameTest) { |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 64 | DefaultVariable<int> var("var", kVariableModeConst); |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 65 | EXPECT_EQ(var.GetName(), string("var")); |
| 66 | } |
| 67 | |
Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 68 | TEST_F(UmBaseVariableTest, GetModeTest) { |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 69 | DefaultVariable<int> var("var", kVariableModeConst); |
| 70 | EXPECT_EQ(var.GetMode(), kVariableModeConst); |
| 71 | DefaultVariable<int> other_var("other_var", kVariableModePoll); |
| 72 | EXPECT_EQ(other_var.GetMode(), kVariableModePoll); |
| 73 | } |
| 74 | |
Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 75 | TEST_F(UmBaseVariableTest, DefaultPollIntervalTest) { |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame] | 76 | DefaultVariable<int> const_var("const_var", kVariableModeConst); |
| 77 | EXPECT_EQ(const_var.GetPollInterval(), TimeDelta()); |
| 78 | DefaultVariable<int> poll_var("poll_var", kVariableModePoll); |
| 79 | EXPECT_EQ(poll_var.GetPollInterval(), TimeDelta::FromMinutes(5)); |
| 80 | } |
| 81 | |
Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 82 | TEST_F(UmBaseVariableTest, GetPollIntervalTest) { |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame] | 83 | DefaultVariable<int> var("var", TimeDelta::FromMinutes(3)); |
| 84 | EXPECT_EQ(var.GetMode(), kVariableModePoll); |
| 85 | EXPECT_EQ(var.GetPollInterval(), TimeDelta::FromMinutes(3)); |
| 86 | } |
| 87 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 88 | class BaseVariableObserver : public BaseVariable::ObserverInterface { |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 89 | public: |
| 90 | void ValueChanged(BaseVariable* variable) { |
| 91 | calls_.push_back(variable); |
| 92 | } |
| 93 | |
| 94 | // List of called functions. |
| 95 | vector<BaseVariable*> calls_; |
| 96 | }; |
| 97 | |
Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 98 | TEST_F(UmBaseVariableTest, RepeatedObserverTest) { |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 99 | DefaultVariable<int> var("var", kVariableModeAsync); |
| 100 | BaseVariableObserver observer; |
| 101 | var.AddObserver(&observer); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 102 | EXPECT_EQ(1U, var.observer_list_.size()); |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 103 | var.AddObserver(&observer); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 104 | EXPECT_EQ(1U, var.observer_list_.size()); |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 105 | var.RemoveObserver(&observer); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 106 | EXPECT_EQ(0U, var.observer_list_.size()); |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 107 | var.RemoveObserver(&observer); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 108 | EXPECT_EQ(0U, var.observer_list_.size()); |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 111 | TEST_F(UmBaseVariableTest, NotifyValueChangedTest) { |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 112 | DefaultVariable<int> var("var", kVariableModeAsync); |
| 113 | BaseVariableObserver observer1; |
| 114 | var.AddObserver(&observer1); |
| 115 | // Simulate a value change on the variable's implementation. |
| 116 | var.NotifyValueChanged(); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 117 | ASSERT_EQ(0U, observer1.calls_.size()); |
Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 118 | MessageLoopRunMaxIterations(MessageLoop::current(), 100); |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 119 | |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 120 | ASSERT_EQ(1U, observer1.calls_.size()); |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 121 | // Check that the observer is called with the right argument. |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 122 | EXPECT_EQ(&var, observer1.calls_[0]); |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 123 | |
| 124 | BaseVariableObserver observer2; |
| 125 | var.AddObserver(&observer2); |
| 126 | var.NotifyValueChanged(); |
Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 127 | MessageLoopRunMaxIterations(MessageLoop::current(), 100); |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 128 | |
| 129 | // Check that all the observers are called. |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 130 | EXPECT_EQ(2U, observer1.calls_.size()); |
| 131 | EXPECT_EQ(1U, observer2.calls_.size()); |
Alex Deymo | a5856e4 | 2014-03-31 18:01:36 -0700 | [diff] [blame] | 132 | |
| 133 | var.RemoveObserver(&observer1); |
| 134 | var.RemoveObserver(&observer2); |
| 135 | } |
| 136 | |
| 137 | class BaseVariableObserverRemover : public BaseVariable::ObserverInterface { |
| 138 | public: |
| 139 | BaseVariableObserverRemover() : calls_(0) {} |
| 140 | |
| 141 | void ValueChanged(BaseVariable* variable) override { |
| 142 | for (auto& observer : remove_observers_) { |
| 143 | variable->RemoveObserver(observer); |
| 144 | } |
| 145 | calls_++; |
| 146 | } |
| 147 | |
| 148 | void OnCallRemoveObserver(BaseVariable::ObserverInterface* observer) { |
| 149 | remove_observers_.push_back(observer); |
| 150 | } |
| 151 | |
| 152 | int get_calls() { return calls_; } |
| 153 | |
| 154 | private: |
| 155 | vector<BaseVariable::ObserverInterface*> remove_observers_; |
| 156 | int calls_; |
| 157 | }; |
| 158 | |
| 159 | // Tests that we can remove an observer from a Variable on the ValueChanged() |
| 160 | // call to that observer. |
Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 161 | TEST_F(UmBaseVariableTest, NotifyValueRemovesObserversTest) { |
Alex Deymo | a5856e4 | 2014-03-31 18:01:36 -0700 | [diff] [blame] | 162 | DefaultVariable<int> var("var", kVariableModeAsync); |
| 163 | BaseVariableObserverRemover observer1; |
| 164 | BaseVariableObserverRemover observer2; |
| 165 | |
| 166 | var.AddObserver(&observer1); |
| 167 | var.AddObserver(&observer2); |
| 168 | |
| 169 | // Make each observer remove both observers on ValueChanged. |
| 170 | observer1.OnCallRemoveObserver(&observer1); |
| 171 | observer1.OnCallRemoveObserver(&observer2); |
| 172 | observer2.OnCallRemoveObserver(&observer1); |
| 173 | observer2.OnCallRemoveObserver(&observer2); |
| 174 | |
| 175 | var.NotifyValueChanged(); |
Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 176 | MessageLoopRunMaxIterations(MessageLoop::current(), 100); |
Alex Deymo | a5856e4 | 2014-03-31 18:01:36 -0700 | [diff] [blame] | 177 | |
| 178 | EXPECT_EQ(1, observer1.get_calls() + observer2.get_calls()); |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 181 | } // namespace chromeos_update_manager |