Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -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 | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 5 | #include "update_engine/update_manager/variable.h" |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 6 | |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 7 | #include <vector> |
| 8 | |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 9 | #include <gtest/gtest.h> |
| 10 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 11 | #include "update_engine/test_utils.h" |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 12 | |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame] | 13 | using base::TimeDelta; |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 14 | using chromeos_update_engine::RunGMainLoopMaxIterations; |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 15 | using std::string; |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 16 | using std::vector; |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 17 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 18 | namespace chromeos_update_manager { |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 19 | |
| 20 | // Variable class that returns a value constructed with the default value. |
| 21 | template <typename T> |
| 22 | class DefaultVariable : public Variable<T> { |
| 23 | public: |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 24 | DefaultVariable(const string& name, VariableMode mode) |
| 25 | : Variable<T>(name, mode) {} |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame] | 26 | DefaultVariable(const string& name, const TimeDelta& poll_interval) |
| 27 | : Variable<T>(name, poll_interval) {} |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 28 | virtual ~DefaultVariable() {} |
| 29 | |
| 30 | protected: |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 31 | virtual const T* GetValue(TimeDelta /* timeout */, |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 32 | string* /* errmsg */) { |
| 33 | return new T(); |
| 34 | } |
| 35 | |
| 36 | private: |
| 37 | DISALLOW_COPY_AND_ASSIGN(DefaultVariable); |
| 38 | }; |
| 39 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 40 | TEST(UmBaseVariableTest, GetNameTest) { |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 41 | DefaultVariable<int> var("var", kVariableModeConst); |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 42 | EXPECT_EQ(var.GetName(), string("var")); |
| 43 | } |
| 44 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 45 | TEST(UmBaseVariableTest, GetModeTest) { |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 46 | DefaultVariable<int> var("var", kVariableModeConst); |
| 47 | EXPECT_EQ(var.GetMode(), kVariableModeConst); |
| 48 | DefaultVariable<int> other_var("other_var", kVariableModePoll); |
| 49 | EXPECT_EQ(other_var.GetMode(), kVariableModePoll); |
| 50 | } |
| 51 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 52 | TEST(UmBaseVariableTest, DefaultPollIntervalTest) { |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame] | 53 | DefaultVariable<int> const_var("const_var", kVariableModeConst); |
| 54 | EXPECT_EQ(const_var.GetPollInterval(), TimeDelta()); |
| 55 | DefaultVariable<int> poll_var("poll_var", kVariableModePoll); |
| 56 | EXPECT_EQ(poll_var.GetPollInterval(), TimeDelta::FromMinutes(5)); |
| 57 | } |
| 58 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 59 | TEST(UmBaseVariableTest, GetPollIntervalTest) { |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame] | 60 | DefaultVariable<int> var("var", TimeDelta::FromMinutes(3)); |
| 61 | EXPECT_EQ(var.GetMode(), kVariableModePoll); |
| 62 | EXPECT_EQ(var.GetPollInterval(), TimeDelta::FromMinutes(3)); |
| 63 | } |
| 64 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 65 | class BaseVariableObserver : public BaseVariable::ObserverInterface { |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 66 | public: |
| 67 | void ValueChanged(BaseVariable* variable) { |
| 68 | calls_.push_back(variable); |
| 69 | } |
| 70 | |
| 71 | // List of called functions. |
| 72 | vector<BaseVariable*> calls_; |
| 73 | }; |
| 74 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 75 | TEST(UmBaseVariableTest, RepeatedObserverTest) { |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 76 | DefaultVariable<int> var("var", kVariableModeAsync); |
| 77 | BaseVariableObserver observer; |
| 78 | var.AddObserver(&observer); |
| 79 | EXPECT_EQ(var.observer_list_.size(), 1); |
| 80 | var.AddObserver(&observer); |
| 81 | EXPECT_EQ(var.observer_list_.size(), 1); |
| 82 | var.RemoveObserver(&observer); |
| 83 | EXPECT_EQ(var.observer_list_.size(), 0); |
| 84 | var.RemoveObserver(&observer); |
| 85 | EXPECT_EQ(var.observer_list_.size(), 0); |
| 86 | } |
| 87 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 88 | TEST(UmBaseVariableTest, NotifyValueChangedTest) { |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 89 | DefaultVariable<int> var("var", kVariableModeAsync); |
| 90 | BaseVariableObserver observer1; |
| 91 | var.AddObserver(&observer1); |
| 92 | // Simulate a value change on the variable's implementation. |
| 93 | var.NotifyValueChanged(); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 94 | ASSERT_EQ(0, observer1.calls_.size()); |
| 95 | RunGMainLoopMaxIterations(100); |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 96 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 97 | ASSERT_EQ(1, observer1.calls_.size()); |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 98 | // Check that the observer is called with the right argument. |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 99 | EXPECT_EQ(&var, observer1.calls_[0]); |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 100 | |
| 101 | BaseVariableObserver observer2; |
| 102 | var.AddObserver(&observer2); |
| 103 | var.NotifyValueChanged(); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 104 | RunGMainLoopMaxIterations(100); |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 105 | |
| 106 | // Check that all the observers are called. |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 107 | EXPECT_EQ(2, observer1.calls_.size()); |
| 108 | EXPECT_EQ(1, observer2.calls_.size()); |
Alex Deymo | a5856e4 | 2014-03-31 18:01:36 -0700 | [diff] [blame] | 109 | |
| 110 | var.RemoveObserver(&observer1); |
| 111 | var.RemoveObserver(&observer2); |
| 112 | } |
| 113 | |
| 114 | class BaseVariableObserverRemover : public BaseVariable::ObserverInterface { |
| 115 | public: |
| 116 | BaseVariableObserverRemover() : calls_(0) {} |
| 117 | |
| 118 | void ValueChanged(BaseVariable* variable) override { |
| 119 | for (auto& observer : remove_observers_) { |
| 120 | variable->RemoveObserver(observer); |
| 121 | } |
| 122 | calls_++; |
| 123 | } |
| 124 | |
| 125 | void OnCallRemoveObserver(BaseVariable::ObserverInterface* observer) { |
| 126 | remove_observers_.push_back(observer); |
| 127 | } |
| 128 | |
| 129 | int get_calls() { return calls_; } |
| 130 | |
| 131 | private: |
| 132 | vector<BaseVariable::ObserverInterface*> remove_observers_; |
| 133 | int calls_; |
| 134 | }; |
| 135 | |
| 136 | // Tests that we can remove an observer from a Variable on the ValueChanged() |
| 137 | // call to that observer. |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 138 | TEST(UmBaseVariableTest, NotifyValueRemovesObserversTest) { |
Alex Deymo | a5856e4 | 2014-03-31 18:01:36 -0700 | [diff] [blame] | 139 | DefaultVariable<int> var("var", kVariableModeAsync); |
| 140 | BaseVariableObserverRemover observer1; |
| 141 | BaseVariableObserverRemover observer2; |
| 142 | |
| 143 | var.AddObserver(&observer1); |
| 144 | var.AddObserver(&observer2); |
| 145 | |
| 146 | // Make each observer remove both observers on ValueChanged. |
| 147 | observer1.OnCallRemoveObserver(&observer1); |
| 148 | observer1.OnCallRemoveObserver(&observer2); |
| 149 | observer2.OnCallRemoveObserver(&observer1); |
| 150 | observer2.OnCallRemoveObserver(&observer2); |
| 151 | |
| 152 | var.NotifyValueChanged(); |
| 153 | RunGMainLoopMaxIterations(100); |
| 154 | |
| 155 | EXPECT_EQ(1, observer1.get_calls() + observer2.get_calls()); |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 158 | } // namespace chromeos_update_manager |