blob: 372670f764a92192852d6048cae2e551768cc862 [file] [log] [blame]
Alex Deymo81f30e82014-01-08 14:33:06 -08001// 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 Deymo63784a52014-05-28 10:46:14 -07005#include "update_engine/update_manager/generic_variables.h"
Alex Deymobd04b142014-03-18 15:00:05 -07006
Ben Chan02f7c1d2014-10-18 15:18:02 -07007#include <memory>
8
Gilad Arnoldc16fca22014-05-20 15:10:40 -07009#include <base/callback.h>
Alex Deymo509dd532015-06-10 14:11:05 -070010#include <chromeos/message_loops/fake_message_loop.h>
11#include <chromeos/message_loops/message_loop.h>
12#include <chromeos/message_loops/message_loop_utils.h>
Alex Deymo81f30e82014-01-08 14:33:06 -080013#include <gtest/gtest.h>
Gilad Arnolda87340b2014-01-30 11:10:18 -080014
Alex Deymo63784a52014-05-28 10:46:14 -070015#include "update_engine/update_manager/umtest_utils.h"
Alex Deymo81f30e82014-01-08 14:33:06 -080016
Alex Deymo509dd532015-06-10 14:11:05 -070017using chromeos::MessageLoop;
18using chromeos::MessageLoopRunMaxIterations;
Ben Chan02f7c1d2014-10-18 15:18:02 -070019using std::unique_ptr;
Alex Deymo81f30e82014-01-08 14:33:06 -080020
Alex Deymo63784a52014-05-28 10:46:14 -070021namespace chromeos_update_manager {
Alex Deymo81f30e82014-01-08 14:33:06 -080022
Alex Deymo63784a52014-05-28 10:46:14 -070023class UmPollCopyVariableTest : public ::testing::Test {};
Alex Deymo81f30e82014-01-08 14:33:06 -080024
Alex Deymo81f30e82014-01-08 14:33:06 -080025
Alex Deymo63784a52014-05-28 10:46:14 -070026TEST_F(UmPollCopyVariableTest, SimpleTest) {
Gilad Arnolda87340b2014-01-30 11:10:18 -080027 // Tests that copies are generated as intended.
28 int source = 5;
Gilad Arnold46eb5f62014-05-20 13:21:25 -070029 PollCopyVariable<int> var("var", source);
Alex Deymo81f30e82014-01-08 14:33:06 -080030
Gilad Arnolda87340b2014-01-30 11:10:18 -080031 // Generate and validate a copy.
Ben Chan02f7c1d2014-10-18 15:18:02 -070032 unique_ptr<const int> copy_1(var.GetValue(
Alex Vakulenko88b591f2014-08-28 16:48:57 -070033 UmTestUtils::DefaultTimeout(), nullptr));
34 ASSERT_NE(nullptr, copy_1.get());
Gilad Arnolda87340b2014-01-30 11:10:18 -080035 EXPECT_EQ(5, *copy_1);
Alex Deymo81f30e82014-01-08 14:33:06 -080036
Gilad Arnolda87340b2014-01-30 11:10:18 -080037 // Assign a different value to the source variable.
38 source = 42;
Alex Deymo81f30e82014-01-08 14:33:06 -080039
Gilad Arnolda87340b2014-01-30 11:10:18 -080040 // Check that the content of the copy was not affected (distinct instance).
41 EXPECT_EQ(5, *copy_1);
Alex Deymo81f30e82014-01-08 14:33:06 -080042
Gilad Arnolda87340b2014-01-30 11:10:18 -080043 // Generate and validate a second copy.
Alex Deymo63784a52014-05-28 10:46:14 -070044 UmTestUtils::ExpectVariableHasValue(42, &var);
Alex Deymo81f30e82014-01-08 14:33:06 -080045}
46
Alex Deymo63784a52014-05-28 10:46:14 -070047TEST_F(UmPollCopyVariableTest, SetFlagTest) {
Gilad Arnold9f7ab352014-04-16 15:27:37 -070048 // Tests that the set flag is being referred to as expected.
49 int source = 5;
50 bool is_set = false;
Gilad Arnold46eb5f62014-05-20 13:21:25 -070051 PollCopyVariable<int> var("var", source, &is_set);
Gilad Arnold9f7ab352014-04-16 15:27:37 -070052
53 // Flag marked unset, nothing should be returned.
Alex Deymo63784a52014-05-28 10:46:14 -070054 UmTestUtils::ExpectVariableNotSet(&var);
Gilad Arnold9f7ab352014-04-16 15:27:37 -070055
56 // Flag marked set, we should be getting a value.
57 is_set = true;
Alex Deymo63784a52014-05-28 10:46:14 -070058 UmTestUtils::ExpectVariableHasValue(5, &var);
Gilad Arnold9f7ab352014-04-16 15:27:37 -070059}
60
Alex Deymo81f30e82014-01-08 14:33:06 -080061
Gilad Arnolda87340b2014-01-30 11:10:18 -080062class CopyConstructorTestClass {
63 public:
64 CopyConstructorTestClass(void) : copied_(false) {}
Gilad Arnoldc16fca22014-05-20 15:10:40 -070065 CopyConstructorTestClass(const CopyConstructorTestClass& other)
66 : copied_(true), val_(other.val_ * 2) {}
Alex Deymo81f30e82014-01-08 14:33:06 -080067
68 // Tells if the instance was constructed using the copy-constructor.
Gilad Arnoldc16fca22014-05-20 15:10:40 -070069 const bool copied_;
70
71 // An auxiliary internal value.
72 int val_ = 0;
Alex Deymo81f30e82014-01-08 14:33:06 -080073};
74
Alex Deymo81f30e82014-01-08 14:33:06 -080075
Alex Deymo63784a52014-05-28 10:46:14 -070076TEST_F(UmPollCopyVariableTest, UseCopyConstructorTest) {
Alex Vakulenko072359c2014-07-18 11:41:07 -070077 // Ensures that CopyVariables indeed uses the copy constructor.
Gilad Arnolda87340b2014-01-30 11:10:18 -080078 const CopyConstructorTestClass source;
79 ASSERT_FALSE(source.copied_);
Alex Deymo81f30e82014-01-08 14:33:06 -080080
Gilad Arnold46eb5f62014-05-20 13:21:25 -070081 PollCopyVariable<CopyConstructorTestClass> var("var", source);
Ben Chan02f7c1d2014-10-18 15:18:02 -070082 unique_ptr<const CopyConstructorTestClass> copy(
Alex Vakulenko88b591f2014-08-28 16:48:57 -070083 var.GetValue(UmTestUtils::DefaultTimeout(), nullptr));
84 ASSERT_NE(nullptr, copy.get());
Gilad Arnolda87340b2014-01-30 11:10:18 -080085 EXPECT_TRUE(copy->copied_);
Alex Deymo81f30e82014-01-08 14:33:06 -080086}
87
Alex Deymobd04b142014-03-18 15:00:05 -070088
Alex Deymo63784a52014-05-28 10:46:14 -070089class UmConstCopyVariableTest : public ::testing::Test {};
Alex Deymobd04b142014-03-18 15:00:05 -070090
Alex Deymo63784a52014-05-28 10:46:14 -070091TEST_F(UmConstCopyVariableTest, SimpleTest) {
Alex Deymobd04b142014-03-18 15:00:05 -070092 int source = 5;
93 ConstCopyVariable<int> var("var", source);
Alex Deymo63784a52014-05-28 10:46:14 -070094 UmTestUtils::ExpectVariableHasValue(5, &var);
Alex Deymobd04b142014-03-18 15:00:05 -070095
96 // Ensure the value is cached.
97 source = 42;
Alex Deymo63784a52014-05-28 10:46:14 -070098 UmTestUtils::ExpectVariableHasValue(5, &var);
Alex Deymobd04b142014-03-18 15:00:05 -070099}
100
Alex Deymoc83baf62014-04-02 17:43:35 -0700101
Alex Deymo63784a52014-05-28 10:46:14 -0700102class UmCallCopyVariableTest : public ::testing::Test {};
Gilad Arnoldc16fca22014-05-20 15:10:40 -0700103
104CopyConstructorTestClass test_func(CopyConstructorTestClass* obj) {
105 obj->val_++; // So we can check that the function was called.
106 return *obj;
107}
108
Alex Deymo63784a52014-05-28 10:46:14 -0700109TEST_F(UmCallCopyVariableTest, SimpleTest) {
Gilad Arnoldc16fca22014-05-20 15:10:40 -0700110 // Tests that the returned value is generated by copying the value returned by
111 // the function call.
112
113 CopyConstructorTestClass test_obj;
114 ASSERT_FALSE(test_obj.copied_);
115 test_obj.val_ = 5;
116
117 base::Callback<CopyConstructorTestClass(void)> cb = base::Bind(
118 test_func, &test_obj);
119 CallCopyVariable<CopyConstructorTestClass> var("var", cb);
120
Ben Chan02f7c1d2014-10-18 15:18:02 -0700121 unique_ptr<const CopyConstructorTestClass> copy(
Alex Deymo63784a52014-05-28 10:46:14 -0700122 var.GetValue(UmTestUtils::DefaultTimeout(), nullptr));
Gilad Arnoldc16fca22014-05-20 15:10:40 -0700123 EXPECT_EQ(6, test_obj.val_); // Check that the function was called.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700124 ASSERT_NE(nullptr, copy.get());
Gilad Arnoldc16fca22014-05-20 15:10:40 -0700125 EXPECT_TRUE(copy->copied_);
126 EXPECT_EQ(12, copy->val_); // Check that copying occurred once.
127}
128
Alex Deymo63784a52014-05-28 10:46:14 -0700129TEST_F(UmCallCopyVariableTest, NullTest) {
Gilad Arnoldc16fca22014-05-20 15:10:40 -0700130 // Ensures that the variable returns null when the callback is null.
131
132 base::Callback<bool(void)> cb;
133 CallCopyVariable<bool> var("var", cb);
Alex Deymo63784a52014-05-28 10:46:14 -0700134 UmTestUtils::ExpectVariableNotSet(&var);
Gilad Arnoldc16fca22014-05-20 15:10:40 -0700135}
136
Alex Deymo63784a52014-05-28 10:46:14 -0700137class UmAsyncCopyVariableTest : public ::testing::Test {
Alex Deymo509dd532015-06-10 14:11:05 -0700138 protected:
139 void SetUp() override {
140 loop_.SetAsCurrent();
141 }
142
Alex Deymo610277e2014-11-11 21:18:11 -0800143 void TearDown() override {
Alex Deymoc83baf62014-04-02 17:43:35 -0700144 // No remaining event on the main loop.
Alex Deymo509dd532015-06-10 14:11:05 -0700145 EXPECT_FALSE(loop_.PendingTasks());
Alex Deymoc83baf62014-04-02 17:43:35 -0700146 }
Alex Deymo509dd532015-06-10 14:11:05 -0700147
148
149 chromeos::FakeMessageLoop loop_{nullptr};
Alex Deymoc83baf62014-04-02 17:43:35 -0700150};
151
Alex Deymo63784a52014-05-28 10:46:14 -0700152TEST_F(UmAsyncCopyVariableTest, ConstructorTest) {
Alex Deymoc83baf62014-04-02 17:43:35 -0700153 AsyncCopyVariable<int> var("var");
Alex Deymo63784a52014-05-28 10:46:14 -0700154 UmTestUtils::ExpectVariableNotSet(&var);
Alex Deymoc83baf62014-04-02 17:43:35 -0700155 EXPECT_EQ(kVariableModeAsync, var.GetMode());
156}
157
Alex Deymo63784a52014-05-28 10:46:14 -0700158TEST_F(UmAsyncCopyVariableTest, SetValueTest) {
Alex Deymoc83baf62014-04-02 17:43:35 -0700159 AsyncCopyVariable<int> var("var");
160 var.SetValue(5);
Alex Deymo63784a52014-05-28 10:46:14 -0700161 UmTestUtils::ExpectVariableHasValue(5, &var);
Alex Deymoc83baf62014-04-02 17:43:35 -0700162 // Execute all the pending observers.
Alex Deymo509dd532015-06-10 14:11:05 -0700163 MessageLoopRunMaxIterations(MessageLoop::current(), 100);
Alex Deymoc83baf62014-04-02 17:43:35 -0700164}
165
Alex Deymo63784a52014-05-28 10:46:14 -0700166TEST_F(UmAsyncCopyVariableTest, UnsetValueTest) {
Alex Deymoc83baf62014-04-02 17:43:35 -0700167 AsyncCopyVariable<int> var("var", 42);
168 var.UnsetValue();
Alex Deymo63784a52014-05-28 10:46:14 -0700169 UmTestUtils::ExpectVariableNotSet(&var);
Alex Deymoc83baf62014-04-02 17:43:35 -0700170 // Execute all the pending observers.
Alex Deymo509dd532015-06-10 14:11:05 -0700171 MessageLoopRunMaxIterations(MessageLoop::current(), 100);
Alex Deymoc83baf62014-04-02 17:43:35 -0700172}
173
174class CallCounterObserver : public BaseVariable::ObserverInterface {
175 public:
176 void ValueChanged(BaseVariable* variable) {
177 calls_count_++;
178 }
179
180 int calls_count_ = 0;
181};
182
Alex Deymo63784a52014-05-28 10:46:14 -0700183TEST_F(UmAsyncCopyVariableTest, ObserverCalledTest) {
Alex Deymoc83baf62014-04-02 17:43:35 -0700184 AsyncCopyVariable<int> var("var", 42);
185 CallCounterObserver observer;
186 var.AddObserver(&observer);
187 EXPECT_EQ(0, observer.calls_count_);
188
189 // Check that a different value fires the notification.
190 var.SetValue(5);
Alex Deymo509dd532015-06-10 14:11:05 -0700191 MessageLoopRunMaxIterations(MessageLoop::current(), 100);
Alex Deymoc83baf62014-04-02 17:43:35 -0700192 EXPECT_EQ(1, observer.calls_count_);
193
194 // Check the same value doesn't.
195 var.SetValue(5);
Alex Deymo509dd532015-06-10 14:11:05 -0700196 MessageLoopRunMaxIterations(MessageLoop::current(), 100);
Alex Deymoc83baf62014-04-02 17:43:35 -0700197 EXPECT_EQ(1, observer.calls_count_);
198
199 // Check that unsetting a previously set value fires the notification.
200 var.UnsetValue();
Alex Deymo509dd532015-06-10 14:11:05 -0700201 MessageLoopRunMaxIterations(MessageLoop::current(), 100);
Alex Deymoc83baf62014-04-02 17:43:35 -0700202 EXPECT_EQ(2, observer.calls_count_);
203
204 // Check that unsetting again doesn't.
205 var.UnsetValue();
Alex Deymo509dd532015-06-10 14:11:05 -0700206 MessageLoopRunMaxIterations(MessageLoop::current(), 100);
Alex Deymoc83baf62014-04-02 17:43:35 -0700207 EXPECT_EQ(2, observer.calls_count_);
208
209 var.RemoveObserver(&observer);
210}
211
Alex Deymo63784a52014-05-28 10:46:14 -0700212} // namespace chromeos_update_manager