blob: ea916b1a9c4c53244aad92a88e7c089dbcd5a20a [file] [log] [blame]
[email protected]3fe674d2012-04-05 17:52:101// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
5#include "base/observer_list.h"
[email protected]503631c2008-10-22 23:09:216#include "base/observer_list_threadsafe.h"
[email protected]3c0d45e2010-09-17 19:33:067
8#include <vector>
9
[email protected]a6ffd7d2011-10-12 20:26:5010#include "base/compiler_specific.h"
11#include "base/memory/weak_ptr.h"
stevenjb76691822015-04-29 21:43:0312#include "base/message_loop/message_loop.h"
[email protected]7ff48ca2013-02-06 16:56:1913#include "base/run_loop.h"
[email protected]ce072a72010-12-31 20:02:1614#include "base/threading/platform_thread.h"
initial.commitd7cae122008-07-26 21:49:3815#include "testing/gtest/include/gtest/gtest.h"
16
[email protected]7ff48ca2013-02-06 16:56:1917namespace base {
initial.commitd7cae122008-07-26 21:49:3818namespace {
19
initial.commitd7cae122008-07-26 21:49:3820class Foo {
21 public:
22 virtual void Observe(int x) = 0;
[email protected]bf92cfbe2008-08-08 16:37:4323 virtual ~Foo() {}
initial.commitd7cae122008-07-26 21:49:3824};
25
26class Adder : public Foo {
27 public:
[email protected]503631c2008-10-22 23:09:2128 explicit Adder(int scaler) : total(0), scaler_(scaler) {}
dcheng56488182014-10-21 10:54:5129 void Observe(int x) override { total += x * scaler_; }
30 ~Adder() override {}
initial.commitd7cae122008-07-26 21:49:3831 int total;
[email protected]44106182012-04-06 03:53:0232
initial.commitd7cae122008-07-26 21:49:3833 private:
34 int scaler_;
35};
36
37class Disrupter : public Foo {
38 public:
[email protected]503631c2008-10-22 23:09:2139 Disrupter(ObserverList<Foo>* list, Foo* doomed)
[email protected]44106182012-04-06 03:53:0240 : list_(list),
41 doomed_(doomed) {
42 }
dcheng56488182014-10-21 10:54:5143 ~Disrupter() override {}
44 void Observe(int x) override { list_->RemoveObserver(doomed_); }
[email protected]44106182012-04-06 03:53:0245
initial.commitd7cae122008-07-26 21:49:3846 private:
[email protected]503631c2008-10-22 23:09:2147 ObserverList<Foo>* list_;
initial.commitd7cae122008-07-26 21:49:3848 Foo* doomed_;
49};
50
[email protected]503631c2008-10-22 23:09:2151class ThreadSafeDisrupter : public Foo {
52 public:
53 ThreadSafeDisrupter(ObserverListThreadSafe<Foo>* list, Foo* doomed)
[email protected]44106182012-04-06 03:53:0254 : list_(list),
55 doomed_(doomed) {
56 }
dcheng56488182014-10-21 10:54:5157 ~ThreadSafeDisrupter() override {}
58 void Observe(int x) override { list_->RemoveObserver(doomed_); }
[email protected]44106182012-04-06 03:53:0259
[email protected]503631c2008-10-22 23:09:2160 private:
61 ObserverListThreadSafe<Foo>* list_;
62 Foo* doomed_;
63};
64
[email protected]920b1fe2011-08-09 21:29:5965template <typename ObserverListType>
[email protected]b3e2fad02008-10-31 03:32:0666class AddInObserve : public Foo {
67 public:
[email protected]920b1fe2011-08-09 21:29:5968 explicit AddInObserve(ObserverListType* observer_list)
[email protected]b3e2fad02008-10-31 03:32:0669 : added(false),
70 observer_list(observer_list),
71 adder(1) {
72 }
[email protected]44106182012-04-06 03:53:0273
nickc0b001062015-04-22 23:17:2074 void Observe(int x) override {
[email protected]b3e2fad02008-10-31 03:32:0675 if (!added) {
76 added = true;
77 observer_list->AddObserver(&adder);
78 }
79 }
80
81 bool added;
[email protected]920b1fe2011-08-09 21:29:5982 ObserverListType* observer_list;
[email protected]b3e2fad02008-10-31 03:32:0683 Adder adder;
84};
85
86
[email protected]ce292012009-11-05 18:46:4787static const int kThreadRunTime = 2000; // ms to run the multi-threaded test.
[email protected]503631c2008-10-22 23:09:2188
89// A thread for use in the ThreadSafeObserver test
90// which will add and remove itself from the notification
91// list repeatedly.
92class AddRemoveThread : public PlatformThread::Delegate,
93 public Foo {
94 public:
95 AddRemoveThread(ObserverListThreadSafe<Foo>* list, bool notify)
96 : list_(list),
robliao6f5e4232015-04-23 22:45:3297 loop_(nullptr),
[email protected]503631c2008-10-22 23:09:2198 in_list_(false),
99 start_(Time::Now()),
100 count_observes_(0),
101 count_addtask_(0),
[email protected]a6ffd7d2011-10-12 20:26:50102 do_notifies_(notify),
[email protected]c9f977b2013-04-25 12:17:15103 weak_factory_(this) {
[email protected]503631c2008-10-22 23:09:21104 }
105
dcheng56488182014-10-21 10:54:51106 ~AddRemoveThread() override {}
[email protected]503631c2008-10-22 23:09:21107
dcheng56488182014-10-21 10:54:51108 void ThreadMain() override {
[email protected]503631c2008-10-22 23:09:21109 loop_ = new MessageLoop(); // Fire up a message loop.
stevenjb76691822015-04-29 21:43:03110 loop_->PostTask(
[email protected]a6ffd7d2011-10-12 20:26:50111 FROM_HERE,
112 base::Bind(&AddRemoveThread::AddTask, weak_factory_.GetWeakPtr()));
[email protected]503631c2008-10-22 23:09:21113 loop_->Run();
[email protected]52a261f2009-03-03 15:01:12114 //LOG(ERROR) << "Loop 0x" << std::hex << loop_ << " done. " <<
115 // count_observes_ << ", " << count_addtask_;
[email protected]503631c2008-10-22 23:09:21116 delete loop_;
117 loop_ = reinterpret_cast<MessageLoop*>(0xdeadbeef);
[email protected]bd60d732009-01-05 19:40:53118 delete this;
[email protected]503631c2008-10-22 23:09:21119 }
120
121 // This task just keeps posting to itself in an attempt
122 // to race with the notifier.
123 void AddTask() {
124 count_addtask_++;
125
126 if ((Time::Now() - start_).InMilliseconds() > kThreadRunTime) {
[email protected]b026e35d2010-10-19 02:31:03127 VLOG(1) << "DONE!";
[email protected]503631c2008-10-22 23:09:21128 return;
129 }
130
131 if (!in_list_) {
132 list_->AddObserver(this);
133 in_list_ = true;
134 }
135
136 if (do_notifies_) {
reillyg9a77a722015-02-09 20:18:33137 list_->Notify(FROM_HERE, &Foo::Observe, 10);
[email protected]503631c2008-10-22 23:09:21138 }
139
stevenjb76691822015-04-29 21:43:03140 loop_->PostTask(
[email protected]a6ffd7d2011-10-12 20:26:50141 FROM_HERE,
142 base::Bind(&AddRemoveThread::AddTask, weak_factory_.GetWeakPtr()));
[email protected]503631c2008-10-22 23:09:21143 }
144
145 void Quit() {
stevenjb76691822015-04-29 21:43:03146 loop_->PostTask(FROM_HERE, MessageLoop::QuitWhenIdleClosure());
[email protected]503631c2008-10-22 23:09:21147 }
148
dcheng56488182014-10-21 10:54:51149 void Observe(int x) override {
[email protected]503631c2008-10-22 23:09:21150 count_observes_++;
151
152 // If we're getting called after we removed ourselves from
153 // the list, that is very bad!
154 DCHECK(in_list_);
155
156 // This callback should fire on the appropriate thread
157 EXPECT_EQ(loop_, MessageLoop::current());
158
159 list_->RemoveObserver(this);
160 in_list_ = false;
161 }
162
163 private:
164 ObserverListThreadSafe<Foo>* list_;
165 MessageLoop* loop_;
166 bool in_list_; // Are we currently registered for notifications.
167 // in_list_ is only used on |this| thread.
168 Time start_; // The time we started the test.
169
170 int count_observes_; // Number of times we observed.
171 int count_addtask_; // Number of times thread AddTask was called
172 bool do_notifies_; // Whether these threads should do notifications.
173
[email protected]a6ffd7d2011-10-12 20:26:50174 base::WeakPtrFactory<AddRemoveThread> weak_factory_;
[email protected]503631c2008-10-22 23:09:21175};
176
initial.commitd7cae122008-07-26 21:49:38177TEST(ObserverListTest, BasicTest) {
178 ObserverList<Foo> observer_list;
[email protected]631739f2011-06-05 07:07:12179 Adder a(1), b(-1), c(1), d(-1), e(-1);
[email protected]503631c2008-10-22 23:09:21180 Disrupter evil(&observer_list, &c);
initial.commitd7cae122008-07-26 21:49:38181
182 observer_list.AddObserver(&a);
183 observer_list.AddObserver(&b);
184
mgiuca64ccf2362014-11-10 06:44:23185 EXPECT_TRUE(observer_list.HasObserver(&a));
186 EXPECT_FALSE(observer_list.HasObserver(&c));
187
initial.commitd7cae122008-07-26 21:49:38188 FOR_EACH_OBSERVER(Foo, observer_list, Observe(10));
189
190 observer_list.AddObserver(&evil);
191 observer_list.AddObserver(&c);
192 observer_list.AddObserver(&d);
193
[email protected]631739f2011-06-05 07:07:12194 // Removing an observer not in the list should do nothing.
195 observer_list.RemoveObserver(&e);
196
initial.commitd7cae122008-07-26 21:49:38197 FOR_EACH_OBSERVER(Foo, observer_list, Observe(10));
198
[email protected]14cf9102012-08-23 09:59:19199 EXPECT_EQ(20, a.total);
200 EXPECT_EQ(-20, b.total);
201 EXPECT_EQ(0, c.total);
202 EXPECT_EQ(-10, d.total);
203 EXPECT_EQ(0, e.total);
initial.commitd7cae122008-07-26 21:49:38204}
license.botbf09a502008-08-24 00:55:55205
[email protected]503631c2008-10-22 23:09:21206TEST(ObserverListThreadSafeTest, BasicTest) {
207 MessageLoop loop;
208
209 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list(
210 new ObserverListThreadSafe<Foo>);
211 Adder a(1);
212 Adder b(-1);
213 Adder c(1);
214 Adder d(-1);
215 ThreadSafeDisrupter evil(observer_list.get(), &c);
216
217 observer_list->AddObserver(&a);
218 observer_list->AddObserver(&b);
219
reillyg9a77a722015-02-09 20:18:33220 observer_list->Notify(FROM_HERE, &Foo::Observe, 10);
[email protected]7ff48ca2013-02-06 16:56:19221 RunLoop().RunUntilIdle();
[email protected]503631c2008-10-22 23:09:21222
223 observer_list->AddObserver(&evil);
224 observer_list->AddObserver(&c);
225 observer_list->AddObserver(&d);
226
reillyg9a77a722015-02-09 20:18:33227 observer_list->Notify(FROM_HERE, &Foo::Observe, 10);
[email protected]7ff48ca2013-02-06 16:56:19228 RunLoop().RunUntilIdle();
[email protected]503631c2008-10-22 23:09:21229
[email protected]14cf9102012-08-23 09:59:19230 EXPECT_EQ(20, a.total);
231 EXPECT_EQ(-20, b.total);
232 EXPECT_EQ(0, c.total);
233 EXPECT_EQ(-10, d.total);
[email protected]503631c2008-10-22 23:09:21234}
235
[email protected]631739f2011-06-05 07:07:12236TEST(ObserverListThreadSafeTest, RemoveObserver) {
237 MessageLoop loop;
238
239 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list(
240 new ObserverListThreadSafe<Foo>);
241 Adder a(1), b(1);
242
[email protected]3fe674d2012-04-05 17:52:10243 // A workaround for the compiler bug. See http://crbug.com/121960.
244 EXPECT_NE(&a, &b);
245
[email protected]631739f2011-06-05 07:07:12246 // Should do nothing.
247 observer_list->RemoveObserver(&a);
248 observer_list->RemoveObserver(&b);
249
reillyg9a77a722015-02-09 20:18:33250 observer_list->Notify(FROM_HERE, &Foo::Observe, 10);
[email protected]7ff48ca2013-02-06 16:56:19251 RunLoop().RunUntilIdle();
[email protected]631739f2011-06-05 07:07:12252
[email protected]14cf9102012-08-23 09:59:19253 EXPECT_EQ(0, a.total);
254 EXPECT_EQ(0, b.total);
[email protected]631739f2011-06-05 07:07:12255
256 observer_list->AddObserver(&a);
257
258 // Should also do nothing.
259 observer_list->RemoveObserver(&b);
260
reillyg9a77a722015-02-09 20:18:33261 observer_list->Notify(FROM_HERE, &Foo::Observe, 10);
[email protected]7ff48ca2013-02-06 16:56:19262 RunLoop().RunUntilIdle();
[email protected]631739f2011-06-05 07:07:12263
[email protected]14cf9102012-08-23 09:59:19264 EXPECT_EQ(10, a.total);
265 EXPECT_EQ(0, b.total);
[email protected]631739f2011-06-05 07:07:12266}
267
[email protected]c2b1b302011-11-23 20:34:04268TEST(ObserverListThreadSafeTest, WithoutMessageLoop) {
269 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list(
270 new ObserverListThreadSafe<Foo>);
271
272 Adder a(1), b(1), c(1);
273
274 // No MessageLoop, so these should not be added.
275 observer_list->AddObserver(&a);
276 observer_list->AddObserver(&b);
277
278 {
279 // Add c when there's a loop.
280 MessageLoop loop;
281 observer_list->AddObserver(&c);
282
reillyg9a77a722015-02-09 20:18:33283 observer_list->Notify(FROM_HERE, &Foo::Observe, 10);
[email protected]7ff48ca2013-02-06 16:56:19284 RunLoop().RunUntilIdle();
[email protected]c2b1b302011-11-23 20:34:04285
286 EXPECT_EQ(0, a.total);
287 EXPECT_EQ(0, b.total);
288 EXPECT_EQ(10, c.total);
289
290 // Now add a when there's a loop.
291 observer_list->AddObserver(&a);
292
293 // Remove c when there's a loop.
294 observer_list->RemoveObserver(&c);
295
296 // Notify again.
reillyg9a77a722015-02-09 20:18:33297 observer_list->Notify(FROM_HERE, &Foo::Observe, 20);
[email protected]7ff48ca2013-02-06 16:56:19298 RunLoop().RunUntilIdle();
[email protected]c2b1b302011-11-23 20:34:04299
300 EXPECT_EQ(20, a.total);
301 EXPECT_EQ(0, b.total);
302 EXPECT_EQ(10, c.total);
303 }
304
305 // Removing should always succeed with or without a loop.
306 observer_list->RemoveObserver(&a);
307
308 // Notifying should not fail but should also be a no-op.
309 MessageLoop loop;
310 observer_list->AddObserver(&b);
reillyg9a77a722015-02-09 20:18:33311 observer_list->Notify(FROM_HERE, &Foo::Observe, 30);
[email protected]7ff48ca2013-02-06 16:56:19312 RunLoop().RunUntilIdle();
[email protected]c2b1b302011-11-23 20:34:04313
314 EXPECT_EQ(20, a.total);
315 EXPECT_EQ(30, b.total);
316 EXPECT_EQ(10, c.total);
317}
318
[email protected]3c0d45e2010-09-17 19:33:06319class FooRemover : public Foo {
320 public:
321 explicit FooRemover(ObserverListThreadSafe<Foo>* list) : list_(list) {}
dcheng56488182014-10-21 10:54:51322 ~FooRemover() override {}
[email protected]3c0d45e2010-09-17 19:33:06323
324 void AddFooToRemove(Foo* foo) {
325 foos_.push_back(foo);
326 }
327
dcheng56488182014-10-21 10:54:51328 void Observe(int x) override {
[email protected]3c0d45e2010-09-17 19:33:06329 std::vector<Foo*> tmp;
330 tmp.swap(foos_);
331 for (std::vector<Foo*>::iterator it = tmp.begin();
332 it != tmp.end(); ++it) {
333 list_->RemoveObserver(*it);
334 }
335 }
336
337 private:
338 const scoped_refptr<ObserverListThreadSafe<Foo> > list_;
339 std::vector<Foo*> foos_;
340};
341
342TEST(ObserverListThreadSafeTest, RemoveMultipleObservers) {
343 MessageLoop loop;
344 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list(
345 new ObserverListThreadSafe<Foo>);
346
[email protected]3703e922013-05-31 21:37:53347 FooRemover a(observer_list.get());
[email protected]3c0d45e2010-09-17 19:33:06348 Adder b(1);
349
350 observer_list->AddObserver(&a);
351 observer_list->AddObserver(&b);
352
353 a.AddFooToRemove(&a);
354 a.AddFooToRemove(&b);
355
reillyg9a77a722015-02-09 20:18:33356 observer_list->Notify(FROM_HERE, &Foo::Observe, 1);
[email protected]7ff48ca2013-02-06 16:56:19357 RunLoop().RunUntilIdle();
[email protected]3c0d45e2010-09-17 19:33:06358}
[email protected]503631c2008-10-22 23:09:21359
360// A test driver for a multi-threaded notification loop. Runs a number
361// of observer threads, each of which constantly adds/removes itself
362// from the observer list. Optionally, if cross_thread_notifies is set
363// to true, the observer threads will also trigger notifications to
364// all observers.
[email protected]52a261f2009-03-03 15:01:12365static void ThreadSafeObserverHarness(int num_threads,
[email protected]503631c2008-10-22 23:09:21366 bool cross_thread_notifies) {
367 MessageLoop loop;
368
369 const int kMaxThreads = 15;
370 num_threads = num_threads > kMaxThreads ? kMaxThreads : num_threads;
371
372 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list(
373 new ObserverListThreadSafe<Foo>);
374 Adder a(1);
375 Adder b(-1);
376 Adder c(1);
377 Adder d(-1);
378
379 observer_list->AddObserver(&a);
380 observer_list->AddObserver(&b);
381
382 AddRemoveThread* threaded_observer[kMaxThreads];
[email protected]ce072a72010-12-31 20:02:16383 base::PlatformThreadHandle threads[kMaxThreads];
[email protected]503631c2008-10-22 23:09:21384 for (int index = 0; index < num_threads; index++) {
385 threaded_observer[index] = new AddRemoveThread(observer_list.get(), false);
386 EXPECT_TRUE(PlatformThread::Create(0,
387 threaded_observer[index], &threads[index]));
388 }
389
390 Time start = Time::Now();
391 while (true) {
392 if ((Time::Now() - start).InMilliseconds() > kThreadRunTime)
393 break;
394
reillyg9a77a722015-02-09 20:18:33395 observer_list->Notify(FROM_HERE, &Foo::Observe, 10);
[email protected]503631c2008-10-22 23:09:21396
[email protected]7ff48ca2013-02-06 16:56:19397 RunLoop().RunUntilIdle();
[email protected]503631c2008-10-22 23:09:21398 }
399
400 for (int index = 0; index < num_threads; index++) {
401 threaded_observer[index]->Quit();
402 PlatformThread::Join(threads[index]);
403 }
404}
405
406TEST(ObserverListThreadSafeTest, CrossThreadObserver) {
407 // Use 7 observer threads. Notifications only come from
408 // the main thread.
409 ThreadSafeObserverHarness(7, false);
410}
411
412TEST(ObserverListThreadSafeTest, CrossThreadNotifications) {
413 // Use 3 observer threads. Notifications will fire from
414 // the main thread and all 3 observer threads.
415 ThreadSafeObserverHarness(3, true);
416}
[email protected]b3e2fad02008-10-31 03:32:06417
[email protected]920b1fe2011-08-09 21:29:59418TEST(ObserverListThreadSafeTest, OutlivesMessageLoop) {
419 MessageLoop* loop = new MessageLoop;
420 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list(
421 new ObserverListThreadSafe<Foo>);
422
423 Adder a(1);
424 observer_list->AddObserver(&a);
425 delete loop;
426 // Test passes if we don't crash here.
reillyg9a77a722015-02-09 20:18:33427 observer_list->Notify(FROM_HERE, &Foo::Observe, 1);
[email protected]920b1fe2011-08-09 21:29:59428}
429
[email protected]b3e2fad02008-10-31 03:32:06430TEST(ObserverListTest, Existing) {
431 ObserverList<Foo> observer_list(ObserverList<Foo>::NOTIFY_EXISTING_ONLY);
432 Adder a(1);
[email protected]920b1fe2011-08-09 21:29:59433 AddInObserve<ObserverList<Foo> > b(&observer_list);
[email protected]b3e2fad02008-10-31 03:32:06434
435 observer_list.AddObserver(&a);
436 observer_list.AddObserver(&b);
437
438 FOR_EACH_OBSERVER(Foo, observer_list, Observe(1));
439
440 EXPECT_TRUE(b.added);
441 // B's adder should not have been notified because it was added during
[email protected]6be50d72014-05-08 23:49:40442 // notification.
[email protected]b3e2fad02008-10-31 03:32:06443 EXPECT_EQ(0, b.adder.total);
444
445 // Notify again to make sure b's adder is notified.
446 FOR_EACH_OBSERVER(Foo, observer_list, Observe(1));
447 EXPECT_EQ(1, b.adder.total);
448}
[email protected]84aebed2010-02-25 03:09:41449
[email protected]920b1fe2011-08-09 21:29:59450// Same as above, but for ObserverListThreadSafe
451TEST(ObserverListThreadSafeTest, Existing) {
452 MessageLoop loop;
453 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list(
454 new ObserverListThreadSafe<Foo>(ObserverList<Foo>::NOTIFY_EXISTING_ONLY));
455 Adder a(1);
456 AddInObserve<ObserverListThreadSafe<Foo> > b(observer_list.get());
457
458 observer_list->AddObserver(&a);
459 observer_list->AddObserver(&b);
460
reillyg9a77a722015-02-09 20:18:33461 observer_list->Notify(FROM_HERE, &Foo::Observe, 1);
[email protected]7ff48ca2013-02-06 16:56:19462 RunLoop().RunUntilIdle();
[email protected]920b1fe2011-08-09 21:29:59463
464 EXPECT_TRUE(b.added);
465 // B's adder should not have been notified because it was added during
[email protected]6be50d72014-05-08 23:49:40466 // notification.
[email protected]920b1fe2011-08-09 21:29:59467 EXPECT_EQ(0, b.adder.total);
468
469 // Notify again to make sure b's adder is notified.
reillyg9a77a722015-02-09 20:18:33470 observer_list->Notify(FROM_HERE, &Foo::Observe, 1);
[email protected]7ff48ca2013-02-06 16:56:19471 RunLoop().RunUntilIdle();
[email protected]920b1fe2011-08-09 21:29:59472 EXPECT_EQ(1, b.adder.total);
473}
474
[email protected]84aebed2010-02-25 03:09:41475class AddInClearObserve : public Foo {
476 public:
477 explicit AddInClearObserve(ObserverList<Foo>* list)
478 : list_(list), added_(false), adder_(1) {}
479
dcheng56488182014-10-21 10:54:51480 void Observe(int /* x */) override {
[email protected]84aebed2010-02-25 03:09:41481 list_->Clear();
482 list_->AddObserver(&adder_);
483 added_ = true;
484 }
485
486 bool added() const { return added_; }
487 const Adder& adder() const { return adder_; }
488
489 private:
490 ObserverList<Foo>* const list_;
491
492 bool added_;
493 Adder adder_;
494};
495
496TEST(ObserverListTest, ClearNotifyAll) {
497 ObserverList<Foo> observer_list;
498 AddInClearObserve a(&observer_list);
499
500 observer_list.AddObserver(&a);
501
502 FOR_EACH_OBSERVER(Foo, observer_list, Observe(1));
503 EXPECT_TRUE(a.added());
504 EXPECT_EQ(1, a.adder().total)
505 << "Adder should observe once and have sum of 1.";
506}
507
508TEST(ObserverListTest, ClearNotifyExistingOnly) {
509 ObserverList<Foo> observer_list(ObserverList<Foo>::NOTIFY_EXISTING_ONLY);
510 AddInClearObserve a(&observer_list);
511
512 observer_list.AddObserver(&a);
513
514 FOR_EACH_OBSERVER(Foo, observer_list, Observe(1));
515 EXPECT_TRUE(a.added());
516 EXPECT_EQ(0, a.adder().total)
517 << "Adder should not observe, so sum should still be 0.";
518}
519
[email protected]671b74d2011-06-09 03:41:18520class ListDestructor : public Foo {
521 public:
522 explicit ListDestructor(ObserverList<Foo>* list) : list_(list) {}
dcheng56488182014-10-21 10:54:51523 ~ListDestructor() override {}
[email protected]44106182012-04-06 03:53:02524
dcheng56488182014-10-21 10:54:51525 void Observe(int x) override { delete list_; }
[email protected]2e58cbd2012-08-06 01:03:05526
[email protected]671b74d2011-06-09 03:41:18527 private:
528 ObserverList<Foo>* list_;
529};
530
531
532TEST(ObserverListTest, IteratorOutlivesList) {
533 ObserverList<Foo>* observer_list = new ObserverList<Foo>;
534 ListDestructor a(observer_list);
535 observer_list->AddObserver(&a);
536
537 FOR_EACH_OBSERVER(Foo, *observer_list, Observe(0));
538 // If this test fails, there'll be Valgrind errors when this function goes out
539 // of scope.
540}
541
[email protected]84aebed2010-02-25 03:09:41542} // namespace
[email protected]7ff48ca2013-02-06 16:56:19543} // namespace base