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 //
16 
17 #include "update_engine/update_manager/generic_variables.h"
18 
19 #include <memory>
20 
21 #include <base/callback.h>
22 #include <brillo/message_loops/fake_message_loop.h>
23 #include <brillo/message_loops/message_loop.h>
24 #include <brillo/message_loops/message_loop_utils.h>
25 #include <gtest/gtest.h>
26 
27 #include "update_engine/update_manager/umtest_utils.h"
28 
29 using brillo::MessageLoop;
30 using brillo::MessageLoopRunMaxIterations;
31 using std::unique_ptr;
32 
33 namespace chromeos_update_manager {
34 
35 class UmPollCopyVariableTest : public ::testing::Test {};
36 
37 TEST_F(UmPollCopyVariableTest, SimpleTest) {
38   // Tests that copies are generated as intended.
39   int source = 5;
40   PollCopyVariable<int> var("var", source);
41 
42   // Generate and validate a copy.
43   unique_ptr<const int> copy_1(
44       var.GetValue(UmTestUtils::DefaultTimeout(), nullptr));
45   ASSERT_NE(nullptr, copy_1.get());
46   EXPECT_EQ(5, *copy_1);
47 
48   // Assign a different value to the source variable.
49   source = 42;
50 
51   // Check that the content of the copy was not affected (distinct instance).
52   EXPECT_EQ(5, *copy_1);
53 
54   // Generate and validate a second copy.
55   UmTestUtils::ExpectVariableHasValue(42, &var);
56 }
57 
58 TEST_F(UmPollCopyVariableTest, SetFlagTest) {
59   // Tests that the set flag is being referred to as expected.
60   int source = 5;
61   bool is_set = false;
62   PollCopyVariable<int> var("var", source, &is_set);
63 
64   // Flag marked unset, nothing should be returned.
65   UmTestUtils::ExpectVariableNotSet(&var);
66 
67   // Flag marked set, we should be getting a value.
68   is_set = true;
69   UmTestUtils::ExpectVariableHasValue(5, &var);
70 }
71 
72 class CopyConstructorTestClass {
73  public:
74   CopyConstructorTestClass(void) : copied_(false) {}
75   CopyConstructorTestClass(const CopyConstructorTestClass& other)
76       : copied_(true), val_(other.val_ * 2) {}
77 
78   // Tells if the instance was constructed using the copy-constructor.
79   const bool copied_;
80 
81   // An auxiliary internal value.
82   int val_ = 0;
83 };
84 
85 TEST_F(UmPollCopyVariableTest, UseCopyConstructorTest) {
86   // Ensures that CopyVariables indeed uses the copy constructor.
87   const CopyConstructorTestClass source;
88   ASSERT_FALSE(source.copied_);
89 
90   PollCopyVariable<CopyConstructorTestClass> var("var", source);
91   unique_ptr<const CopyConstructorTestClass> copy(
92       var.GetValue(UmTestUtils::DefaultTimeout(), nullptr));
93   ASSERT_NE(nullptr, copy.get());
94   EXPECT_TRUE(copy->copied_);
95 }
96 
97 class UmConstCopyVariableTest : public ::testing::Test {};
98 
99 TEST_F(UmConstCopyVariableTest, SimpleTest) {
100   int source = 5;
101   ConstCopyVariable<int> var("var", source);
102   UmTestUtils::ExpectVariableHasValue(5, &var);
103 
104   // Ensure the value is cached.
105   source = 42;
106   UmTestUtils::ExpectVariableHasValue(5, &var);
107 }
108 
109 class UmCallCopyVariableTest : public ::testing::Test {};
110 
111 CopyConstructorTestClass test_func(CopyConstructorTestClass* obj) {
112   obj->val_++;  // So we can check that the function was called.
113   return *obj;
114 }
115 
116 TEST_F(UmCallCopyVariableTest, SimpleTest) {
117   // Tests that the returned value is generated by copying the value returned by
118   // the function call.
119 
120   CopyConstructorTestClass test_obj;
121   ASSERT_FALSE(test_obj.copied_);
122   test_obj.val_ = 5;
123 
124   base::Callback<CopyConstructorTestClass(void)> cb =
125       base::Bind(test_func, &test_obj);
126   CallCopyVariable<CopyConstructorTestClass> var("var", cb);
127 
128   unique_ptr<const CopyConstructorTestClass> copy(
129       var.GetValue(UmTestUtils::DefaultTimeout(), nullptr));
130   EXPECT_EQ(6, test_obj.val_);  // Check that the function was called.
131   ASSERT_NE(nullptr, copy.get());
132   EXPECT_TRUE(copy->copied_);
133   EXPECT_EQ(12, copy->val_);  // Check that copying occurred once.
134 }
135 
136 TEST_F(UmCallCopyVariableTest, NullTest) {
137   // Ensures that the variable returns null when the callback is null.
138 
139   base::Callback<bool(void)> cb;
140   CallCopyVariable<bool> var("var", cb);
141   UmTestUtils::ExpectVariableNotSet(&var);
142 }
143 
144 class UmAsyncCopyVariableTest : public ::testing::Test {
145  protected:
146   void SetUp() override { loop_.SetAsCurrent(); }
147 
148   void TearDown() override {
149     // No remaining event on the main loop.
150     EXPECT_FALSE(loop_.PendingTasks());
151   }
152 
153   brillo::FakeMessageLoop loop_{nullptr};
154 };
155 
156 TEST_F(UmAsyncCopyVariableTest, ConstructorTest) {
157   AsyncCopyVariable<int> var("var");
158   UmTestUtils::ExpectVariableNotSet(&var);
159   EXPECT_EQ(kVariableModeAsync, var.GetMode());
160 }
161 
162 TEST_F(UmAsyncCopyVariableTest, SetValueTest) {
163   AsyncCopyVariable<int> var("var");
164   var.SetValue(5);
165   UmTestUtils::ExpectVariableHasValue(5, &var);
166   // Execute all the pending observers.
167   MessageLoopRunMaxIterations(MessageLoop::current(), 100);
168 }
169 
170 TEST_F(UmAsyncCopyVariableTest, UnsetValueTest) {
171   AsyncCopyVariable<int> var("var", 42);
172   var.UnsetValue();
173   UmTestUtils::ExpectVariableNotSet(&var);
174   // Execute all the pending observers.
175   MessageLoopRunMaxIterations(MessageLoop::current(), 100);
176 }
177 
178 class CallCounterObserver : public BaseVariable::ObserverInterface {
179  public:
180   void ValueChanged(BaseVariable* variable) { calls_count_++; }
181 
182   int calls_count_ = 0;
183 };
184 
185 TEST_F(UmAsyncCopyVariableTest, ObserverCalledTest) {
186   AsyncCopyVariable<int> var("var", 42);
187   CallCounterObserver observer;
188   var.AddObserver(&observer);
189   EXPECT_EQ(0, observer.calls_count_);
190 
191   // Check that a different value fires the notification.
192   var.SetValue(5);
193   MessageLoopRunMaxIterations(MessageLoop::current(), 100);
194   EXPECT_EQ(1, observer.calls_count_);
195 
196   // Check the same value doesn't.
197   var.SetValue(5);
198   MessageLoopRunMaxIterations(MessageLoop::current(), 100);
199   EXPECT_EQ(1, observer.calls_count_);
200 
201   // Check that unsetting a previously set value fires the notification.
202   var.UnsetValue();
203   MessageLoopRunMaxIterations(MessageLoop::current(), 100);
204   EXPECT_EQ(2, observer.calls_count_);
205 
206   // Check that unsetting again doesn't.
207   var.UnsetValue();
208   MessageLoopRunMaxIterations(MessageLoop::current(), 100);
209   EXPECT_EQ(2, observer.calls_count_);
210 
211   var.RemoveObserver(&observer);
212 }
213 
214 }  // namespace chromeos_update_manager
215