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