blob: 8aad753f15e1d09c256fbf87edfd8c4d13eb47b1 [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 Deymobd04b142014-03-18 15:00:05 -07005#include "update_engine/policy_manager/generic_variables.h"
6
Gilad Arnolda87340b2014-01-30 11:10:18 -08007#include <base/memory/scoped_ptr.h>
Alex Deymo81f30e82014-01-08 14:33:06 -08008#include <gtest/gtest.h>
Gilad Arnolda87340b2014-01-30 11:10:18 -08009
Alex Deymo6e97bb22014-02-05 16:46:16 -080010#include "update_engine/policy_manager/pmtest_utils.h"
Alex Deymoc83baf62014-04-02 17:43:35 -070011#include "update_engine/test_utils.h"
Alex Deymo81f30e82014-01-08 14:33:06 -080012
13using base::TimeDelta;
Alex Deymoc83baf62014-04-02 17:43:35 -070014using chromeos_update_engine::RunGMainLoopMaxIterations;
Alex Deymo81f30e82014-01-08 14:33:06 -080015
16namespace chromeos_policy_manager {
17
Gilad Arnold67ed78d2014-04-23 13:17:46 -070018class PmCopyVariableTest : public ::testing::Test {};
Alex Deymo81f30e82014-01-08 14:33:06 -080019
Alex Deymo81f30e82014-01-08 14:33:06 -080020
Gilad Arnolda87340b2014-01-30 11:10:18 -080021TEST_F(PmCopyVariableTest, SimpleTest) {
22 // Tests that copies are generated as intended.
23 int source = 5;
Alex Deymo0e433692014-02-20 07:23:03 -080024 CopyVariable<int> var("var", kVariableModePoll, source);
Alex Deymo81f30e82014-01-08 14:33:06 -080025
Gilad Arnolda87340b2014-01-30 11:10:18 -080026 // Generate and validate a copy.
Gilad Arnold67ed78d2014-04-23 13:17:46 -070027 scoped_ptr<const int> copy_1(var.GetValue(
28 PmTestUtils::DefaultTimeout(), NULL));
Gilad Arnolda87340b2014-01-30 11:10:18 -080029 PMTEST_ASSERT_NOT_NULL(copy_1.get());
30 EXPECT_EQ(5, *copy_1);
Alex Deymo81f30e82014-01-08 14:33:06 -080031
Gilad Arnolda87340b2014-01-30 11:10:18 -080032 // Assign a different value to the source variable.
33 source = 42;
Alex Deymo81f30e82014-01-08 14:33:06 -080034
Gilad Arnolda87340b2014-01-30 11:10:18 -080035 // Check that the content of the copy was not affected (distinct instance).
36 EXPECT_EQ(5, *copy_1);
Alex Deymo81f30e82014-01-08 14:33:06 -080037
Gilad Arnolda87340b2014-01-30 11:10:18 -080038 // Generate and validate a second copy.
Gilad Arnold67ed78d2014-04-23 13:17:46 -070039 PmTestUtils::ExpectVariableHasValue(42, &var);
Alex Deymo81f30e82014-01-08 14:33:06 -080040}
41
Gilad Arnold9f7ab352014-04-16 15:27:37 -070042TEST_F(PmCopyVariableTest, SetFlagTest) {
43 // Tests that the set flag is being referred to as expected.
44 int source = 5;
45 bool is_set = false;
46 CopyVariable<int> var("var", kVariableModePoll, source, &is_set);
47
48 // Flag marked unset, nothing should be returned.
Gilad Arnold67ed78d2014-04-23 13:17:46 -070049 PmTestUtils::ExpectVariableNotSet(&var);
Gilad Arnold9f7ab352014-04-16 15:27:37 -070050
51 // Flag marked set, we should be getting a value.
52 is_set = true;
Gilad Arnold67ed78d2014-04-23 13:17:46 -070053 PmTestUtils::ExpectVariableHasValue(5, &var);
Gilad Arnold9f7ab352014-04-16 15:27:37 -070054}
55
Alex Deymo81f30e82014-01-08 14:33:06 -080056
Gilad Arnolda87340b2014-01-30 11:10:18 -080057class CopyConstructorTestClass {
58 public:
59 CopyConstructorTestClass(void) : copied_(false) {}
60 CopyConstructorTestClass(const CopyConstructorTestClass& /* ignored */)
Alex Deymo81f30e82014-01-08 14:33:06 -080061 : copied_(true) {}
62
63 // Tells if the instance was constructed using the copy-constructor.
64 bool copied_;
65};
66
Alex Deymo81f30e82014-01-08 14:33:06 -080067
Gilad Arnolda87340b2014-01-30 11:10:18 -080068TEST_F(PmCopyVariableTest, UseCopyConstructorTest) {
69 // Ensures that CopyVariables indeed uses the copy contructor.
70 const CopyConstructorTestClass source;
71 ASSERT_FALSE(source.copied_);
Alex Deymo81f30e82014-01-08 14:33:06 -080072
Alex Deymo0e433692014-02-20 07:23:03 -080073 CopyVariable<CopyConstructorTestClass> var("var", kVariableModePoll, source);
Gilad Arnolda87340b2014-01-30 11:10:18 -080074 scoped_ptr<const CopyConstructorTestClass> copy(
Gilad Arnold67ed78d2014-04-23 13:17:46 -070075 var.GetValue(PmTestUtils::DefaultTimeout(), NULL));
Gilad Arnolda87340b2014-01-30 11:10:18 -080076 PMTEST_ASSERT_NOT_NULL(copy.get());
77 EXPECT_TRUE(copy->copied_);
Alex Deymo81f30e82014-01-08 14:33:06 -080078}
79
Alex Deymobd04b142014-03-18 15:00:05 -070080
Gilad Arnold67ed78d2014-04-23 13:17:46 -070081class PmConstCopyVariableTest : public ::testing::Test {};
Alex Deymobd04b142014-03-18 15:00:05 -070082
83TEST_F(PmConstCopyVariableTest, SimpleTest) {
84 int source = 5;
85 ConstCopyVariable<int> var("var", source);
Gilad Arnold67ed78d2014-04-23 13:17:46 -070086 PmTestUtils::ExpectVariableHasValue(5, &var);
Alex Deymobd04b142014-03-18 15:00:05 -070087
88 // Ensure the value is cached.
89 source = 42;
Gilad Arnold67ed78d2014-04-23 13:17:46 -070090 PmTestUtils::ExpectVariableHasValue(5, &var);
Alex Deymobd04b142014-03-18 15:00:05 -070091}
92
Alex Deymoc83baf62014-04-02 17:43:35 -070093
94class PmAsyncCopyVariableTest : public ::testing::Test {
95 public:
96 void TearDown() {
97 // No remaining event on the main loop.
98 EXPECT_EQ(0, RunGMainLoopMaxIterations(1));
99 }
Alex Deymoc83baf62014-04-02 17:43:35 -0700100};
101
102TEST_F(PmAsyncCopyVariableTest, ConstructorTest) {
103 AsyncCopyVariable<int> var("var");
Gilad Arnold67ed78d2014-04-23 13:17:46 -0700104 PmTestUtils::ExpectVariableNotSet(&var);
Alex Deymoc83baf62014-04-02 17:43:35 -0700105 EXPECT_EQ(kVariableModeAsync, var.GetMode());
106}
107
108TEST_F(PmAsyncCopyVariableTest, SetValueTest) {
109 AsyncCopyVariable<int> var("var");
110 var.SetValue(5);
Gilad Arnold67ed78d2014-04-23 13:17:46 -0700111 PmTestUtils::ExpectVariableHasValue(5, &var);
Alex Deymoc83baf62014-04-02 17:43:35 -0700112 // Execute all the pending observers.
113 RunGMainLoopMaxIterations(100);
114}
115
116TEST_F(PmAsyncCopyVariableTest, UnsetValueTest) {
117 AsyncCopyVariable<int> var("var", 42);
118 var.UnsetValue();
Gilad Arnold67ed78d2014-04-23 13:17:46 -0700119 PmTestUtils::ExpectVariableNotSet(&var);
Alex Deymoc83baf62014-04-02 17:43:35 -0700120 // Execute all the pending observers.
121 RunGMainLoopMaxIterations(100);
122}
123
124class CallCounterObserver : public BaseVariable::ObserverInterface {
125 public:
126 void ValueChanged(BaseVariable* variable) {
127 calls_count_++;
128 }
129
130 int calls_count_ = 0;
131};
132
133TEST_F(PmAsyncCopyVariableTest, ObserverCalledTest) {
134 AsyncCopyVariable<int> var("var", 42);
135 CallCounterObserver observer;
136 var.AddObserver(&observer);
137 EXPECT_EQ(0, observer.calls_count_);
138
139 // Check that a different value fires the notification.
140 var.SetValue(5);
141 RunGMainLoopMaxIterations(100);
142 EXPECT_EQ(1, observer.calls_count_);
143
144 // Check the same value doesn't.
145 var.SetValue(5);
146 RunGMainLoopMaxIterations(100);
147 EXPECT_EQ(1, observer.calls_count_);
148
149 // Check that unsetting a previously set value fires the notification.
150 var.UnsetValue();
151 RunGMainLoopMaxIterations(100);
152 EXPECT_EQ(2, observer.calls_count_);
153
154 // Check that unsetting again doesn't.
155 var.UnsetValue();
156 RunGMainLoopMaxIterations(100);
157 EXPECT_EQ(2, observer.calls_count_);
158
159 var.RemoveObserver(&observer);
160}
161
Alex Deymo81f30e82014-01-08 14:33:06 -0800162} // namespace chromeos_policy_manager