blob: 634acce59b69c52ffe43bb9ece3e49920e956f6e [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
Gilad Arnoldc16fca22014-05-20 15:10:40 -07007#include <base/callback.h>
Gilad Arnolda87340b2014-01-30 11:10:18 -08008#include <base/memory/scoped_ptr.h>
Alex Deymo81f30e82014-01-08 14:33:06 -08009#include <gtest/gtest.h>
Gilad Arnolda87340b2014-01-30 11:10:18 -080010
Alex Deymoc83baf62014-04-02 17:43:35 -070011#include "update_engine/test_utils.h"
Alex Deymo63784a52014-05-28 10:46:14 -070012#include "update_engine/update_manager/umtest_utils.h"
Alex Deymo81f30e82014-01-08 14:33:06 -080013
14using base::TimeDelta;
Alex Deymoc83baf62014-04-02 17:43:35 -070015using chromeos_update_engine::RunGMainLoopMaxIterations;
Alex Deymo81f30e82014-01-08 14:33:06 -080016
Alex Deymo63784a52014-05-28 10:46:14 -070017namespace chromeos_update_manager {
Alex Deymo81f30e82014-01-08 14:33:06 -080018
Alex Deymo63784a52014-05-28 10:46:14 -070019class UmPollCopyVariableTest : public ::testing::Test {};
Alex Deymo81f30e82014-01-08 14:33:06 -080020
Alex Deymo81f30e82014-01-08 14:33:06 -080021
Alex Deymo63784a52014-05-28 10:46:14 -070022TEST_F(UmPollCopyVariableTest, SimpleTest) {
Gilad Arnolda87340b2014-01-30 11:10:18 -080023 // Tests that copies are generated as intended.
24 int source = 5;
Gilad Arnold46eb5f62014-05-20 13:21:25 -070025 PollCopyVariable<int> var("var", source);
Alex Deymo81f30e82014-01-08 14:33:06 -080026
Gilad Arnolda87340b2014-01-30 11:10:18 -080027 // Generate and validate a copy.
Gilad Arnold67ed78d2014-04-23 13:17:46 -070028 scoped_ptr<const int> copy_1(var.GetValue(
Alex Vakulenko88b591f2014-08-28 16:48:57 -070029 UmTestUtils::DefaultTimeout(), nullptr));
30 ASSERT_NE(nullptr, copy_1.get());
Gilad Arnolda87340b2014-01-30 11:10:18 -080031 EXPECT_EQ(5, *copy_1);
Alex Deymo81f30e82014-01-08 14:33:06 -080032
Gilad Arnolda87340b2014-01-30 11:10:18 -080033 // Assign a different value to the source variable.
34 source = 42;
Alex Deymo81f30e82014-01-08 14:33:06 -080035
Gilad Arnolda87340b2014-01-30 11:10:18 -080036 // Check that the content of the copy was not affected (distinct instance).
37 EXPECT_EQ(5, *copy_1);
Alex Deymo81f30e82014-01-08 14:33:06 -080038
Gilad Arnolda87340b2014-01-30 11:10:18 -080039 // Generate and validate a second copy.
Alex Deymo63784a52014-05-28 10:46:14 -070040 UmTestUtils::ExpectVariableHasValue(42, &var);
Alex Deymo81f30e82014-01-08 14:33:06 -080041}
42
Alex Deymo63784a52014-05-28 10:46:14 -070043TEST_F(UmPollCopyVariableTest, SetFlagTest) {
Gilad Arnold9f7ab352014-04-16 15:27:37 -070044 // Tests that the set flag is being referred to as expected.
45 int source = 5;
46 bool is_set = false;
Gilad Arnold46eb5f62014-05-20 13:21:25 -070047 PollCopyVariable<int> var("var", source, &is_set);
Gilad Arnold9f7ab352014-04-16 15:27:37 -070048
49 // Flag marked unset, nothing should be returned.
Alex Deymo63784a52014-05-28 10:46:14 -070050 UmTestUtils::ExpectVariableNotSet(&var);
Gilad Arnold9f7ab352014-04-16 15:27:37 -070051
52 // Flag marked set, we should be getting a value.
53 is_set = true;
Alex Deymo63784a52014-05-28 10:46:14 -070054 UmTestUtils::ExpectVariableHasValue(5, &var);
Gilad Arnold9f7ab352014-04-16 15:27:37 -070055}
56
Alex Deymo81f30e82014-01-08 14:33:06 -080057
Gilad Arnolda87340b2014-01-30 11:10:18 -080058class CopyConstructorTestClass {
59 public:
60 CopyConstructorTestClass(void) : copied_(false) {}
Gilad Arnoldc16fca22014-05-20 15:10:40 -070061 CopyConstructorTestClass(const CopyConstructorTestClass& other)
62 : copied_(true), val_(other.val_ * 2) {}
Alex Deymo81f30e82014-01-08 14:33:06 -080063
64 // Tells if the instance was constructed using the copy-constructor.
Gilad Arnoldc16fca22014-05-20 15:10:40 -070065 const bool copied_;
66
67 // An auxiliary internal value.
68 int val_ = 0;
Alex Deymo81f30e82014-01-08 14:33:06 -080069};
70
Alex Deymo81f30e82014-01-08 14:33:06 -080071
Alex Deymo63784a52014-05-28 10:46:14 -070072TEST_F(UmPollCopyVariableTest, UseCopyConstructorTest) {
Alex Vakulenko072359c2014-07-18 11:41:07 -070073 // Ensures that CopyVariables indeed uses the copy constructor.
Gilad Arnolda87340b2014-01-30 11:10:18 -080074 const CopyConstructorTestClass source;
75 ASSERT_FALSE(source.copied_);
Alex Deymo81f30e82014-01-08 14:33:06 -080076
Gilad Arnold46eb5f62014-05-20 13:21:25 -070077 PollCopyVariable<CopyConstructorTestClass> var("var", source);
Gilad Arnolda87340b2014-01-30 11:10:18 -080078 scoped_ptr<const CopyConstructorTestClass> copy(
Alex Vakulenko88b591f2014-08-28 16:48:57 -070079 var.GetValue(UmTestUtils::DefaultTimeout(), nullptr));
80 ASSERT_NE(nullptr, copy.get());
Gilad Arnolda87340b2014-01-30 11:10:18 -080081 EXPECT_TRUE(copy->copied_);
Alex Deymo81f30e82014-01-08 14:33:06 -080082}
83
Alex Deymobd04b142014-03-18 15:00:05 -070084
Alex Deymo63784a52014-05-28 10:46:14 -070085class UmConstCopyVariableTest : public ::testing::Test {};
Alex Deymobd04b142014-03-18 15:00:05 -070086
Alex Deymo63784a52014-05-28 10:46:14 -070087TEST_F(UmConstCopyVariableTest, SimpleTest) {
Alex Deymobd04b142014-03-18 15:00:05 -070088 int source = 5;
89 ConstCopyVariable<int> var("var", source);
Alex Deymo63784a52014-05-28 10:46:14 -070090 UmTestUtils::ExpectVariableHasValue(5, &var);
Alex Deymobd04b142014-03-18 15:00:05 -070091
92 // Ensure the value is cached.
93 source = 42;
Alex Deymo63784a52014-05-28 10:46:14 -070094 UmTestUtils::ExpectVariableHasValue(5, &var);
Alex Deymobd04b142014-03-18 15:00:05 -070095}
96
Alex Deymoc83baf62014-04-02 17:43:35 -070097
Alex Deymo63784a52014-05-28 10:46:14 -070098class UmCallCopyVariableTest : public ::testing::Test {};
Gilad Arnoldc16fca22014-05-20 15:10:40 -070099
100CopyConstructorTestClass test_func(CopyConstructorTestClass* obj) {
101 obj->val_++; // So we can check that the function was called.
102 return *obj;
103}
104
Alex Deymo63784a52014-05-28 10:46:14 -0700105TEST_F(UmCallCopyVariableTest, SimpleTest) {
Gilad Arnoldc16fca22014-05-20 15:10:40 -0700106 // Tests that the returned value is generated by copying the value returned by
107 // the function call.
108
109 CopyConstructorTestClass test_obj;
110 ASSERT_FALSE(test_obj.copied_);
111 test_obj.val_ = 5;
112
113 base::Callback<CopyConstructorTestClass(void)> cb = base::Bind(
114 test_func, &test_obj);
115 CallCopyVariable<CopyConstructorTestClass> var("var", cb);
116
117 scoped_ptr<const CopyConstructorTestClass> copy(
Alex Deymo63784a52014-05-28 10:46:14 -0700118 var.GetValue(UmTestUtils::DefaultTimeout(), nullptr));
Gilad Arnoldc16fca22014-05-20 15:10:40 -0700119 EXPECT_EQ(6, test_obj.val_); // Check that the function was called.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700120 ASSERT_NE(nullptr, copy.get());
Gilad Arnoldc16fca22014-05-20 15:10:40 -0700121 EXPECT_TRUE(copy->copied_);
122 EXPECT_EQ(12, copy->val_); // Check that copying occurred once.
123}
124
Alex Deymo63784a52014-05-28 10:46:14 -0700125TEST_F(UmCallCopyVariableTest, NullTest) {
Gilad Arnoldc16fca22014-05-20 15:10:40 -0700126 // Ensures that the variable returns null when the callback is null.
127
128 base::Callback<bool(void)> cb;
129 CallCopyVariable<bool> var("var", cb);
Alex Deymo63784a52014-05-28 10:46:14 -0700130 UmTestUtils::ExpectVariableNotSet(&var);
Gilad Arnoldc16fca22014-05-20 15:10:40 -0700131}
132
Alex Deymo63784a52014-05-28 10:46:14 -0700133class UmAsyncCopyVariableTest : public ::testing::Test {
Alex Deymoc83baf62014-04-02 17:43:35 -0700134 public:
135 void TearDown() {
136 // No remaining event on the main loop.
137 EXPECT_EQ(0, RunGMainLoopMaxIterations(1));
138 }
Alex Deymoc83baf62014-04-02 17:43:35 -0700139};
140
Alex Deymo63784a52014-05-28 10:46:14 -0700141TEST_F(UmAsyncCopyVariableTest, ConstructorTest) {
Alex Deymoc83baf62014-04-02 17:43:35 -0700142 AsyncCopyVariable<int> var("var");
Alex Deymo63784a52014-05-28 10:46:14 -0700143 UmTestUtils::ExpectVariableNotSet(&var);
Alex Deymoc83baf62014-04-02 17:43:35 -0700144 EXPECT_EQ(kVariableModeAsync, var.GetMode());
145}
146
Alex Deymo63784a52014-05-28 10:46:14 -0700147TEST_F(UmAsyncCopyVariableTest, SetValueTest) {
Alex Deymoc83baf62014-04-02 17:43:35 -0700148 AsyncCopyVariable<int> var("var");
149 var.SetValue(5);
Alex Deymo63784a52014-05-28 10:46:14 -0700150 UmTestUtils::ExpectVariableHasValue(5, &var);
Alex Deymoc83baf62014-04-02 17:43:35 -0700151 // Execute all the pending observers.
152 RunGMainLoopMaxIterations(100);
153}
154
Alex Deymo63784a52014-05-28 10:46:14 -0700155TEST_F(UmAsyncCopyVariableTest, UnsetValueTest) {
Alex Deymoc83baf62014-04-02 17:43:35 -0700156 AsyncCopyVariable<int> var("var", 42);
157 var.UnsetValue();
Alex Deymo63784a52014-05-28 10:46:14 -0700158 UmTestUtils::ExpectVariableNotSet(&var);
Alex Deymoc83baf62014-04-02 17:43:35 -0700159 // Execute all the pending observers.
160 RunGMainLoopMaxIterations(100);
161}
162
163class CallCounterObserver : public BaseVariable::ObserverInterface {
164 public:
165 void ValueChanged(BaseVariable* variable) {
166 calls_count_++;
167 }
168
169 int calls_count_ = 0;
170};
171
Alex Deymo63784a52014-05-28 10:46:14 -0700172TEST_F(UmAsyncCopyVariableTest, ObserverCalledTest) {
Alex Deymoc83baf62014-04-02 17:43:35 -0700173 AsyncCopyVariable<int> var("var", 42);
174 CallCounterObserver observer;
175 var.AddObserver(&observer);
176 EXPECT_EQ(0, observer.calls_count_);
177
178 // Check that a different value fires the notification.
179 var.SetValue(5);
180 RunGMainLoopMaxIterations(100);
181 EXPECT_EQ(1, observer.calls_count_);
182
183 // Check the same value doesn't.
184 var.SetValue(5);
185 RunGMainLoopMaxIterations(100);
186 EXPECT_EQ(1, observer.calls_count_);
187
188 // Check that unsetting a previously set value fires the notification.
189 var.UnsetValue();
190 RunGMainLoopMaxIterations(100);
191 EXPECT_EQ(2, observer.calls_count_);
192
193 // Check that unsetting again doesn't.
194 var.UnsetValue();
195 RunGMainLoopMaxIterations(100);
196 EXPECT_EQ(2, observer.calls_count_);
197
198 var.RemoveObserver(&observer);
199}
200
Alex Deymo63784a52014-05-28 10:46:14 -0700201} // namespace chromeos_update_manager