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
215
216
217
218
219
220
221
222
223
224
225
226
base / scoped_multi_source_observation_unittest.cc [blame]
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/scoped_multi_source_observation.h"
#include "base/containers/contains.h"
#include "base/memory/raw_ptr.h"
#include "base/ranges/algorithm.h"
#include "base/scoped_observation_traits.h"
#include "base/test/gtest_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace {
class TestSourceObserver {};
class TestSource {
public:
void AddObserver(TestSourceObserver* observer);
void RemoveObserver(TestSourceObserver* observer);
bool HasObserver(TestSourceObserver* observer) const;
size_t num_observers() const { return observers_.size(); }
private:
std::vector<raw_ptr<TestSourceObserver, VectorExperimental>> observers_;
};
void TestSource::AddObserver(TestSourceObserver* observer) {
observers_.push_back(observer);
}
void TestSource::RemoveObserver(TestSourceObserver* observer) {
auto it = base::ranges::find(observers_, observer);
ASSERT_TRUE(it != observers_.end());
observers_.erase(it);
}
bool TestSource::HasObserver(TestSourceObserver* observer) const {
return base::Contains(observers_, observer);
}
using TestScopedMultiSourceObservation =
ScopedMultiSourceObservation<TestSource, TestSourceObserver>;
class ScopedMultiSourceObservationTest : public testing::Test {
public:
TestSource* s1() { return &s1_; }
TestSource* s2() { return &s2_; }
TestSourceObserver* o1() { return &o1_; }
private:
TestSource s1_;
TestSource s2_;
TestSourceObserver o1_;
};
} // namespace
TEST_F(ScopedMultiSourceObservationTest, RemovesSourcesOnDestruction) {
{
TestScopedMultiSourceObservation obs(o1());
EXPECT_EQ(0u, s1()->num_observers());
EXPECT_FALSE(s1()->HasObserver(o1()));
obs.AddObservation(s1());
EXPECT_EQ(1u, s1()->num_observers());
EXPECT_TRUE(s1()->HasObserver(o1()));
obs.AddObservation(s2());
EXPECT_EQ(1u, s2()->num_observers());
EXPECT_TRUE(s2()->HasObserver(o1()));
}
// Test that all observations are removed when it goes out of scope.
EXPECT_EQ(0u, s1()->num_observers());
EXPECT_EQ(0u, s2()->num_observers());
}
TEST_F(ScopedMultiSourceObservationTest, RemoveObservation) {
TestScopedMultiSourceObservation obs(o1());
EXPECT_EQ(0u, s1()->num_observers());
EXPECT_FALSE(s1()->HasObserver(o1()));
EXPECT_EQ(0u, s2()->num_observers());
EXPECT_FALSE(s2()->HasObserver(o1()));
obs.AddObservation(s1());
EXPECT_EQ(1u, s1()->num_observers());
EXPECT_TRUE(s1()->HasObserver(o1()));
obs.AddObservation(s2());
EXPECT_EQ(1u, s2()->num_observers());
EXPECT_TRUE(s2()->HasObserver(o1()));
obs.RemoveObservation(s1());
EXPECT_EQ(0u, s1()->num_observers());
EXPECT_FALSE(s1()->HasObserver(o1()));
EXPECT_EQ(1u, s2()->num_observers());
EXPECT_TRUE(s2()->HasObserver(o1()));
obs.RemoveObservation(s2());
EXPECT_EQ(0u, s1()->num_observers());
EXPECT_FALSE(s1()->HasObserver(o1()));
EXPECT_EQ(0u, s2()->num_observers());
EXPECT_FALSE(s2()->HasObserver(o1()));
}
TEST_F(ScopedMultiSourceObservationTest, RemoveAllObservations) {
TestScopedMultiSourceObservation obs(o1());
EXPECT_EQ(0u, s1()->num_observers());
EXPECT_FALSE(s1()->HasObserver(o1()));
EXPECT_EQ(0u, s2()->num_observers());
EXPECT_FALSE(s2()->HasObserver(o1()));
obs.AddObservation(s1());
obs.AddObservation(s2());
EXPECT_EQ(1u, s1()->num_observers());
EXPECT_TRUE(s1()->HasObserver(o1()));
EXPECT_EQ(1u, s2()->num_observers());
EXPECT_TRUE(s2()->HasObserver(o1()));
obs.RemoveAllObservations();
EXPECT_EQ(0u, s1()->num_observers());
EXPECT_FALSE(s1()->HasObserver(o1()));
EXPECT_EQ(0u, s2()->num_observers());
EXPECT_FALSE(s2()->HasObserver(o1()));
}
TEST_F(ScopedMultiSourceObservationTest, IsObservingSource) {
TestScopedMultiSourceObservation obs(o1());
EXPECT_FALSE(obs.IsObservingSource(s1()));
EXPECT_FALSE(obs.IsObservingSource(s2()));
obs.AddObservation(s1());
EXPECT_TRUE(obs.IsObservingSource(s1()));
EXPECT_FALSE(obs.IsObservingSource(s2()));
obs.AddObservation(s2());
EXPECT_TRUE(obs.IsObservingSource(s1()));
EXPECT_TRUE(obs.IsObservingSource(s2()));
obs.RemoveObservation(s1());
EXPECT_FALSE(obs.IsObservingSource(s1()));
EXPECT_TRUE(obs.IsObservingSource(s2()));
}
TEST_F(ScopedMultiSourceObservationTest, IsObservingAnySource) {
TestScopedMultiSourceObservation obs(o1());
EXPECT_FALSE(obs.IsObservingAnySource());
obs.AddObservation(s1());
EXPECT_TRUE(obs.IsObservingAnySource());
obs.AddObservation(s2());
EXPECT_TRUE(obs.IsObservingAnySource());
obs.RemoveAllObservations();
EXPECT_FALSE(obs.IsObservingAnySource());
}
TEST_F(ScopedMultiSourceObservationTest, GetSourcesCount) {
TestScopedMultiSourceObservation obs(o1());
EXPECT_EQ(0u, obs.GetSourcesCount());
obs.AddObservation(s1());
EXPECT_EQ(1u, obs.GetSourcesCount());
obs.AddObservation(s2());
EXPECT_EQ(2u, obs.GetSourcesCount());
obs.RemoveAllObservations();
EXPECT_EQ(0u, obs.GetSourcesCount());
}
namespace {
// A test source with oddly named Add/Remove functions.
class TestSourceWithNonDefaultNames {
public:
void AddFoo(TestSourceObserver* observer) { impl_.AddObserver(observer); }
void RemoveFoo(TestSourceObserver* observer) {
impl_.RemoveObserver(observer);
}
const TestSource& impl() const { return impl_; }
private:
TestSource impl_;
};
using TestScopedMultiSourceObservationWithNonDefaultNames =
ScopedMultiSourceObservation<TestSourceWithNonDefaultNames,
TestSourceObserver>;
} // namespace
template <>
struct ScopedObservationTraits<TestSourceWithNonDefaultNames,
TestSourceObserver> {
static void AddObserver(TestSourceWithNonDefaultNames* source,
TestSourceObserver* observer) {
source->AddFoo(observer);
}
static void RemoveObserver(TestSourceWithNonDefaultNames* source,
TestSourceObserver* observer) {
source->RemoveFoo(observer);
}
};
TEST_F(ScopedMultiSourceObservationTest, NonDefaultNames) {
TestSourceWithNonDefaultNames nds1;
EXPECT_EQ(0u, nds1.impl().num_observers());
{
TestScopedMultiSourceObservationWithNonDefaultNames obs(o1());
obs.AddObservation(&nds1);
EXPECT_EQ(1u, nds1.impl().num_observers());
EXPECT_TRUE(nds1.impl().HasObserver(o1()));
}
EXPECT_EQ(0u, nds1.impl().num_observers());
}
} // namespace base