blob: 2e51e455216b96d077c0b27bd656daaf317be51c [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"
skyostil054861d2015-04-30 19:06:1511#include "base/location.h"
[email protected]a6ffd7d2011-10-12 20:26:5012#include "base/memory/weak_ptr.h"
[email protected]7ff48ca2013-02-06 16:56:1913#include "base/run_loop.h"
skyostil054861d2015-04-30 19:06:1514#include "base/single_thread_task_runner.h"
[email protected]ce072a72010-12-31 20:02:1615#include "base/threading/platform_thread.h"
initial.commitd7cae122008-07-26 21:49:3816#include "testing/gtest/include/gtest/gtest.h"
17
[email protected]7ff48ca2013-02-06 16:56:1918namespace base {
initial.commitd7cae122008-07-26 21:49:3819namespace {
20
initial.commitd7cae122008-07-26 21:49:3821class Foo {
22 public:
23 virtual void Observe(int x) = 0;
[email protected]bf92cfbe2008-08-08 16:37:4324 virtual ~Foo() {}
initial.commitd7cae122008-07-26 21:49:3825};
26
27class Adder : public Foo {
28 public:
[email protected]503631c2008-10-22 23:09:2129 explicit Adder(int scaler) : total(0), scaler_(scaler) {}
dcheng56488182014-10-21 10:54:5130 void Observe(int x) override { total += x * scaler_; }
31 ~Adder() override {}
initial.commitd7cae122008-07-26 21:49:3832 int total;
[email protected]44106182012-04-06 03:53:0233
initial.commitd7cae122008-07-26 21:49:3834 private:
35 int scaler_;
36};
37
38class Disrupter : public Foo {
39 public:
[email protected]503631c2008-10-22 23:09:2140 Disrupter(ObserverList<Foo>* list, Foo* doomed)
[email protected]44106182012-04-06 03:53:0241 : list_(list),
42 doomed_(doomed) {
43 }
dcheng56488182014-10-21 10:54:5144 ~Disrupter() override {}
45 void Observe(int x) override { list_->RemoveObserver(doomed_); }
[email protected]44106182012-04-06 03:53:0246
initial.commitd7cae122008-07-26 21:49:3847 private:
[email protected]503631c2008-10-22 23:09:2148 ObserverList<Foo>* list_;
initial.commitd7cae122008-07-26 21:49:3849 Foo* doomed_;
50};
51
[email protected]503631c2008-10-22 23:09:2152class ThreadSafeDisrupter : public Foo {
53 public:
54 ThreadSafeDisrupter(ObserverListThreadSafe<Foo>* list, Foo* doomed)
[email protected]44106182012-04-06 03:53:0255 : list_(list),
56 doomed_(doomed) {
57 }
dcheng56488182014-10-21 10:54:5158 ~ThreadSafeDisrupter() override {}
59 void Observe(int x) override { list_->RemoveObserver(doomed_); }
[email protected]44106182012-04-06 03:53:0260
[email protected]503631c2008-10-22 23:09:2161 private:
62 ObserverListThreadSafe<Foo>* list_;
63 Foo* doomed_;
64};
65
[email protected]920b1fe2011-08-09 21:29:5966template <typename ObserverListType>
[email protected]b3e2fad02008-10-31 03:32:0667class AddInObserve : public Foo {
68 public:
[email protected]920b1fe2011-08-09 21:29:5969 explicit AddInObserve(ObserverListType* observer_list)
[email protected]b3e2fad02008-10-31 03:32:0670 : added(false),
71 observer_list(observer_list),
72 adder(1) {
73 }
[email protected]44106182012-04-06 03:53:0274
nickc0b001062015-04-22 23:17:2075 void Observe(int x) override {
[email protected]b3e2fad02008-10-31 03:32:0676 if (!added) {
77 added = true;
78 observer_list->AddObserver(&adder);
79 }
80 }
81
82 bool added;
[email protected]920b1fe2011-08-09 21:29:5983 ObserverListType* observer_list;
[email protected]b3e2fad02008-10-31 03:32:0684 Adder adder;
85};
86
87
[email protected]ce292012009-11-05 18:46:4788static const int kThreadRunTime = 2000; // ms to run the multi-threaded test.
[email protected]503631c2008-10-22 23:09:2189
90// A thread for use in the ThreadSafeObserver test
91// which will add and remove itself from the notification
92// list repeatedly.
93class AddRemoveThread : public PlatformThread::Delegate,
94 public Foo {
95 public:
96 AddRemoveThread(ObserverListThreadSafe<Foo>* list, bool notify)
97 : list_(list),
robliao6f5e4232015-04-23 22:45:3298 loop_(nullptr),
[email protected]503631c2008-10-22 23:09:2199 in_list_(false),
100 start_(Time::Now()),
101 count_observes_(0),
102 count_addtask_(0),
[email protected]a6ffd7d2011-10-12 20:26:50103 do_notifies_(notify),
[email protected]c9f977b2013-04-25 12:17:15104 weak_factory_(this) {
[email protected]503631c2008-10-22 23:09:21105 }
106
dcheng56488182014-10-21 10:54:51107 ~AddRemoveThread() override {}
[email protected]503631c2008-10-22 23:09:21108
dcheng56488182014-10-21 10:54:51109 void ThreadMain() override {
[email protected]503631c2008-10-22 23:09:21110 loop_ = new MessageLoop(); // Fire up a message loop.
skyostil054861d2015-04-30 19:06:15111 loop_->task_runner()->PostTask(
[email protected]a6ffd7d2011-10-12 20:26:50112 FROM_HERE,
113 base::Bind(&AddRemoveThread::AddTask, weak_factory_.GetWeakPtr()));
[email protected]503631c2008-10-22 23:09:21114 loop_->Run();
[email protected]52a261f2009-03-03 15:01:12115 //LOG(ERROR) << "Loop 0x" << std::hex << loop_ << " done. " <<
116 // count_observes_ << ", " << count_addtask_;
[email protected]503631c2008-10-22 23:09:21117 delete loop_;
118 loop_ = reinterpret_cast<MessageLoop*>(0xdeadbeef);
[email protected]bd60d732009-01-05 19:40:53119 delete this;
[email protected]503631c2008-10-22 23:09:21120 }
121
122 // This task just keeps posting to itself in an attempt
123 // to race with the notifier.
124 void AddTask() {
125 count_addtask_++;
126
127 if ((Time::Now() - start_).InMilliseconds() > kThreadRunTime) {
[email protected]b026e35d2010-10-19 02:31:03128 VLOG(1) << "DONE!";
[email protected]503631c2008-10-22 23:09:21129 return;
130 }
131
132 if (!in_list_) {
133 list_->AddObserver(this);
134 in_list_ = true;
135 }
136
137 if (do_notifies_) {
reillyg9a77a722015-02-09 20:18:33138 list_->Notify(FROM_HERE, &Foo::Observe, 10);
[email protected]503631c2008-10-22 23:09:21139 }
140
skyostil054861d2015-04-30 19:06:15141 loop_->task_runner()->PostTask(
[email protected]a6ffd7d2011-10-12 20:26:50142 FROM_HERE,
143 base::Bind(&AddRemoveThread::AddTask, weak_factory_.GetWeakPtr()));
[email protected]503631c2008-10-22 23:09:21144 }
145
146 void Quit() {
skyostil054861d2015-04-30 19:06:15147 loop_->task_runner()->PostTask(FROM_HERE,
148 MessageLoop::QuitWhenIdleClosure());
[email protected]503631c2008-10-22 23:09:21149 }
150
dcheng56488182014-10-21 10:54:51151 void Observe(int x) override {
[email protected]503631c2008-10-22 23:09:21152 count_observes_++;
153
154 // If we're getting called after we removed ourselves from
155 // the list, that is very bad!
156 DCHECK(in_list_);
157
158 // This callback should fire on the appropriate thread
159 EXPECT_EQ(loop_, MessageLoop::current());
160
161 list_->RemoveObserver(this);
162 in_list_ = false;
163 }
164
165 private:
166 ObserverListThreadSafe<Foo>* list_;
167 MessageLoop* loop_;
168 bool in_list_; // Are we currently registered for notifications.
169 // in_list_ is only used on |this| thread.
170 Time start_; // The time we started the test.
171
172 int count_observes_; // Number of times we observed.
173 int count_addtask_; // Number of times thread AddTask was called
174 bool do_notifies_; // Whether these threads should do notifications.
175
[email protected]a6ffd7d2011-10-12 20:26:50176 base::WeakPtrFactory<AddRemoveThread> weak_factory_;
[email protected]503631c2008-10-22 23:09:21177};
178
initial.commitd7cae122008-07-26 21:49:38179TEST(ObserverListTest, BasicTest) {
180 ObserverList<Foo> observer_list;
[email protected]631739f2011-06-05 07:07:12181 Adder a(1), b(-1), c(1), d(-1), e(-1);
[email protected]503631c2008-10-22 23:09:21182 Disrupter evil(&observer_list, &c);
initial.commitd7cae122008-07-26 21:49:38183
184 observer_list.AddObserver(&a);
185 observer_list.AddObserver(&b);
186
mgiuca64ccf2362014-11-10 06:44:23187 EXPECT_TRUE(observer_list.HasObserver(&a));
188 EXPECT_FALSE(observer_list.HasObserver(&c));
189
initial.commitd7cae122008-07-26 21:49:38190 FOR_EACH_OBSERVER(Foo, observer_list, Observe(10));
191
192 observer_list.AddObserver(&evil);
193 observer_list.AddObserver(&c);
194 observer_list.AddObserver(&d);
195
[email protected]631739f2011-06-05 07:07:12196 // Removing an observer not in the list should do nothing.
197 observer_list.RemoveObserver(&e);
198
initial.commitd7cae122008-07-26 21:49:38199 FOR_EACH_OBSERVER(Foo, observer_list, Observe(10));
200
[email protected]14cf9102012-08-23 09:59:19201 EXPECT_EQ(20, a.total);
202 EXPECT_EQ(-20, b.total);
203 EXPECT_EQ(0, c.total);
204 EXPECT_EQ(-10, d.total);
205 EXPECT_EQ(0, e.total);
initial.commitd7cae122008-07-26 21:49:38206}
license.botbf09a502008-08-24 00:55:55207
[email protected]503631c2008-10-22 23:09:21208TEST(ObserverListThreadSafeTest, BasicTest) {
209 MessageLoop loop;
210
211 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list(
212 new ObserverListThreadSafe<Foo>);
213 Adder a(1);
214 Adder b(-1);
215 Adder c(1);
216 Adder d(-1);
217 ThreadSafeDisrupter evil(observer_list.get(), &c);
218
219 observer_list->AddObserver(&a);
220 observer_list->AddObserver(&b);
221
reillyg9a77a722015-02-09 20:18:33222 observer_list->Notify(FROM_HERE, &Foo::Observe, 10);
[email protected]7ff48ca2013-02-06 16:56:19223 RunLoop().RunUntilIdle();
[email protected]503631c2008-10-22 23:09:21224
225 observer_list->AddObserver(&evil);
226 observer_list->AddObserver(&c);
227 observer_list->AddObserver(&d);
228
reillyg9a77a722015-02-09 20:18:33229 observer_list->Notify(FROM_HERE, &Foo::Observe, 10);
[email protected]7ff48ca2013-02-06 16:56:19230 RunLoop().RunUntilIdle();
[email protected]503631c2008-10-22 23:09:21231
[email protected]14cf9102012-08-23 09:59:19232 EXPECT_EQ(20, a.total);
233 EXPECT_EQ(-20, b.total);
234 EXPECT_EQ(0, c.total);
235 EXPECT_EQ(-10, d.total);
[email protected]503631c2008-10-22 23:09:21236}
237
[email protected]631739f2011-06-05 07:07:12238TEST(ObserverListThreadSafeTest, RemoveObserver) {
239 MessageLoop loop;
240
241 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list(
242 new ObserverListThreadSafe<Foo>);
243 Adder a(1), b(1);
244
[email protected]3fe674d2012-04-05 17:52:10245 // A workaround for the compiler bug. See http://crbug.com/121960.
246 EXPECT_NE(&a, &b);
247
[email protected]631739f2011-06-05 07:07:12248 // Should do nothing.
249 observer_list->RemoveObserver(&a);
250 observer_list->RemoveObserver(&b);
251
reillyg9a77a722015-02-09 20:18:33252 observer_list->Notify(FROM_HERE, &Foo::Observe, 10);
[email protected]7ff48ca2013-02-06 16:56:19253 RunLoop().RunUntilIdle();
[email protected]631739f2011-06-05 07:07:12254
[email protected]14cf9102012-08-23 09:59:19255 EXPECT_EQ(0, a.total);
256 EXPECT_EQ(0, b.total);
[email protected]631739f2011-06-05 07:07:12257
258 observer_list->AddObserver(&a);
259
260 // Should also do nothing.
261 observer_list->RemoveObserver(&b);
262
reillyg9a77a722015-02-09 20:18:33263 observer_list->Notify(FROM_HERE, &Foo::Observe, 10);
[email protected]7ff48ca2013-02-06 16:56:19264 RunLoop().RunUntilIdle();
[email protected]631739f2011-06-05 07:07:12265
[email protected]14cf9102012-08-23 09:59:19266 EXPECT_EQ(10, a.total);
267 EXPECT_EQ(0, b.total);
[email protected]631739f2011-06-05 07:07:12268}
269
[email protected]c2b1b302011-11-23 20:34:04270TEST(ObserverListThreadSafeTest, WithoutMessageLoop) {
271 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list(
272 new ObserverListThreadSafe<Foo>);
273
274 Adder a(1), b(1), c(1);
275
276 // No MessageLoop, so these should not be added.
277 observer_list->AddObserver(&a);
278 observer_list->AddObserver(&b);
279
280 {
281 // Add c when there's a loop.
282 MessageLoop loop;
283 observer_list->AddObserver(&c);
284
reillyg9a77a722015-02-09 20:18:33285 observer_list->Notify(FROM_HERE, &Foo::Observe, 10);
[email protected]7ff48ca2013-02-06 16:56:19286 RunLoop().RunUntilIdle();
[email protected]c2b1b302011-11-23 20:34:04287
288 EXPECT_EQ(0, a.total);
289 EXPECT_EQ(0, b.total);
290 EXPECT_EQ(10, c.total);
291
292 // Now add a when there's a loop.
293 observer_list->AddObserver(&a);
294
295 // Remove c when there's a loop.
296 observer_list->RemoveObserver(&c);
297
298 // Notify again.
reillyg9a77a722015-02-09 20:18:33299 observer_list->Notify(FROM_HERE, &Foo::Observe, 20);
[email protected]7ff48ca2013-02-06 16:56:19300 RunLoop().RunUntilIdle();
[email protected]c2b1b302011-11-23 20:34:04301
302 EXPECT_EQ(20, a.total);
303 EXPECT_EQ(0, b.total);
304 EXPECT_EQ(10, c.total);
305 }
306
307 // Removing should always succeed with or without a loop.
308 observer_list->RemoveObserver(&a);
309
310 // Notifying should not fail but should also be a no-op.
311 MessageLoop loop;
312 observer_list->AddObserver(&b);
reillyg9a77a722015-02-09 20:18:33313 observer_list->Notify(FROM_HERE, &Foo::Observe, 30);
[email protected]7ff48ca2013-02-06 16:56:19314 RunLoop().RunUntilIdle();
[email protected]c2b1b302011-11-23 20:34:04315
316 EXPECT_EQ(20, a.total);
317 EXPECT_EQ(30, b.total);
318 EXPECT_EQ(10, c.total);
319}
320
[email protected]3c0d45e2010-09-17 19:33:06321class FooRemover : public Foo {
322 public:
323 explicit FooRemover(ObserverListThreadSafe<Foo>* list) : list_(list) {}
dcheng56488182014-10-21 10:54:51324 ~FooRemover() override {}
[email protected]3c0d45e2010-09-17 19:33:06325
326 void AddFooToRemove(Foo* foo) {
327 foos_.push_back(foo);
328 }
329
dcheng56488182014-10-21 10:54:51330 void Observe(int x) override {
[email protected]3c0d45e2010-09-17 19:33:06331 std::vector<Foo*> tmp;
332 tmp.swap(foos_);
333 for (std::vector<Foo*>::iterator it = tmp.begin();
334 it != tmp.end(); ++it) {
335 list_->RemoveObserver(*it);
336 }
337 }
338
339 private:
340 const scoped_refptr<ObserverListThreadSafe<Foo> > list_;
341 std::vector<Foo*> foos_;
342};
343
344TEST(ObserverListThreadSafeTest, RemoveMultipleObservers) {
345 MessageLoop loop;
346 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list(
347 new ObserverListThreadSafe<Foo>);
348
[email protected]3703e922013-05-31 21:37:53349 FooRemover a(observer_list.get());
[email protected]3c0d45e2010-09-17 19:33:06350 Adder b(1);
351
352 observer_list->AddObserver(&a);
353 observer_list->AddObserver(&b);
354
355 a.AddFooToRemove(&a);
356 a.AddFooToRemove(&b);
357
reillyg9a77a722015-02-09 20:18:33358 observer_list->Notify(FROM_HERE, &Foo::Observe, 1);
[email protected]7ff48ca2013-02-06 16:56:19359 RunLoop().RunUntilIdle();
[email protected]3c0d45e2010-09-17 19:33:06360}
[email protected]503631c2008-10-22 23:09:21361
362// A test driver for a multi-threaded notification loop. Runs a number
363// of observer threads, each of which constantly adds/removes itself
364// from the observer list. Optionally, if cross_thread_notifies is set
365// to true, the observer threads will also trigger notifications to
366// all observers.
[email protected]52a261f2009-03-03 15:01:12367static void ThreadSafeObserverHarness(int num_threads,
[email protected]503631c2008-10-22 23:09:21368 bool cross_thread_notifies) {
369 MessageLoop loop;
370
371 const int kMaxThreads = 15;
372 num_threads = num_threads > kMaxThreads ? kMaxThreads : num_threads;
373
374 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list(
375 new ObserverListThreadSafe<Foo>);
376 Adder a(1);
377 Adder b(-1);
378 Adder c(1);
379 Adder d(-1);
380
381 observer_list->AddObserver(&a);
382 observer_list->AddObserver(&b);
383
384 AddRemoveThread* threaded_observer[kMaxThreads];
[email protected]ce072a72010-12-31 20:02:16385 base::PlatformThreadHandle threads[kMaxThreads];
[email protected]503631c2008-10-22 23:09:21386 for (int index = 0; index < num_threads; index++) {
387 threaded_observer[index] = new AddRemoveThread(observer_list.get(), false);
388 EXPECT_TRUE(PlatformThread::Create(0,
389 threaded_observer[index], &threads[index]));
390 }
391
392 Time start = Time::Now();
393 while (true) {
394 if ((Time::Now() - start).InMilliseconds() > kThreadRunTime)
395 break;
396
reillyg9a77a722015-02-09 20:18:33397 observer_list->Notify(FROM_HERE, &Foo::Observe, 10);
[email protected]503631c2008-10-22 23:09:21398
[email protected]7ff48ca2013-02-06 16:56:19399 RunLoop().RunUntilIdle();
[email protected]503631c2008-10-22 23:09:21400 }
401
402 for (int index = 0; index < num_threads; index++) {
403 threaded_observer[index]->Quit();
404 PlatformThread::Join(threads[index]);
405 }
406}
407
408TEST(ObserverListThreadSafeTest, CrossThreadObserver) {
409 // Use 7 observer threads. Notifications only come from
410 // the main thread.
411 ThreadSafeObserverHarness(7, false);
412}
413
414TEST(ObserverListThreadSafeTest, CrossThreadNotifications) {
415 // Use 3 observer threads. Notifications will fire from
416 // the main thread and all 3 observer threads.
417 ThreadSafeObserverHarness(3, true);
418}
[email protected]b3e2fad02008-10-31 03:32:06419
[email protected]920b1fe2011-08-09 21:29:59420TEST(ObserverListThreadSafeTest, OutlivesMessageLoop) {
421 MessageLoop* loop = new MessageLoop;
422 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list(
423 new ObserverListThreadSafe<Foo>);
424
425 Adder a(1);
426 observer_list->AddObserver(&a);
427 delete loop;
428 // Test passes if we don't crash here.
reillyg9a77a722015-02-09 20:18:33429 observer_list->Notify(FROM_HERE, &Foo::Observe, 1);
[email protected]920b1fe2011-08-09 21:29:59430}
431
[email protected]b3e2fad02008-10-31 03:32:06432TEST(ObserverListTest, Existing) {
433 ObserverList<Foo> observer_list(ObserverList<Foo>::NOTIFY_EXISTING_ONLY);
434 Adder a(1);
[email protected]920b1fe2011-08-09 21:29:59435 AddInObserve<ObserverList<Foo> > b(&observer_list);
[email protected]b3e2fad02008-10-31 03:32:06436
437 observer_list.AddObserver(&a);
438 observer_list.AddObserver(&b);
439
440 FOR_EACH_OBSERVER(Foo, observer_list, Observe(1));
441
442 EXPECT_TRUE(b.added);
443 // B's adder should not have been notified because it was added during
[email protected]6be50d72014-05-08 23:49:40444 // notification.
[email protected]b3e2fad02008-10-31 03:32:06445 EXPECT_EQ(0, b.adder.total);
446
447 // Notify again to make sure b's adder is notified.
448 FOR_EACH_OBSERVER(Foo, observer_list, Observe(1));
449 EXPECT_EQ(1, b.adder.total);
450}
[email protected]84aebed2010-02-25 03:09:41451
[email protected]920b1fe2011-08-09 21:29:59452// Same as above, but for ObserverListThreadSafe
453TEST(ObserverListThreadSafeTest, Existing) {
454 MessageLoop loop;
455 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list(
456 new ObserverListThreadSafe<Foo>(ObserverList<Foo>::NOTIFY_EXISTING_ONLY));
457 Adder a(1);
458 AddInObserve<ObserverListThreadSafe<Foo> > b(observer_list.get());
459
460 observer_list->AddObserver(&a);
461 observer_list->AddObserver(&b);
462
reillyg9a77a722015-02-09 20:18:33463 observer_list->Notify(FROM_HERE, &Foo::Observe, 1);
[email protected]7ff48ca2013-02-06 16:56:19464 RunLoop().RunUntilIdle();
[email protected]920b1fe2011-08-09 21:29:59465
466 EXPECT_TRUE(b.added);
467 // B's adder should not have been notified because it was added during
[email protected]6be50d72014-05-08 23:49:40468 // notification.
[email protected]920b1fe2011-08-09 21:29:59469 EXPECT_EQ(0, b.adder.total);
470
471 // Notify again to make sure b's adder is notified.
reillyg9a77a722015-02-09 20:18:33472 observer_list->Notify(FROM_HERE, &Foo::Observe, 1);
[email protected]7ff48ca2013-02-06 16:56:19473 RunLoop().RunUntilIdle();
[email protected]920b1fe2011-08-09 21:29:59474 EXPECT_EQ(1, b.adder.total);
475}
476
[email protected]84aebed2010-02-25 03:09:41477class AddInClearObserve : public Foo {
478 public:
479 explicit AddInClearObserve(ObserverList<Foo>* list)
480 : list_(list), added_(false), adder_(1) {}
481
dcheng56488182014-10-21 10:54:51482 void Observe(int /* x */) override {
[email protected]84aebed2010-02-25 03:09:41483 list_->Clear();
484 list_->AddObserver(&adder_);
485 added_ = true;
486 }
487
488 bool added() const { return added_; }
489 const Adder& adder() const { return adder_; }
490
491 private:
492 ObserverList<Foo>* const list_;
493
494 bool added_;
495 Adder adder_;
496};
497
498TEST(ObserverListTest, ClearNotifyAll) {
499 ObserverList<Foo> observer_list;
500 AddInClearObserve a(&observer_list);
501
502 observer_list.AddObserver(&a);
503
504 FOR_EACH_OBSERVER(Foo, observer_list, Observe(1));
505 EXPECT_TRUE(a.added());
506 EXPECT_EQ(1, a.adder().total)
507 << "Adder should observe once and have sum of 1.";
508}
509
510TEST(ObserverListTest, ClearNotifyExistingOnly) {
511 ObserverList<Foo> observer_list(ObserverList<Foo>::NOTIFY_EXISTING_ONLY);
512 AddInClearObserve a(&observer_list);
513
514 observer_list.AddObserver(&a);
515
516 FOR_EACH_OBSERVER(Foo, observer_list, Observe(1));
517 EXPECT_TRUE(a.added());
518 EXPECT_EQ(0, a.adder().total)
519 << "Adder should not observe, so sum should still be 0.";
520}
521
[email protected]671b74d2011-06-09 03:41:18522class ListDestructor : public Foo {
523 public:
524 explicit ListDestructor(ObserverList<Foo>* list) : list_(list) {}
dcheng56488182014-10-21 10:54:51525 ~ListDestructor() override {}
[email protected]44106182012-04-06 03:53:02526
dcheng56488182014-10-21 10:54:51527 void Observe(int x) override { delete list_; }
[email protected]2e58cbd2012-08-06 01:03:05528
[email protected]671b74d2011-06-09 03:41:18529 private:
530 ObserverList<Foo>* list_;
531};
532
533
534TEST(ObserverListTest, IteratorOutlivesList) {
535 ObserverList<Foo>* observer_list = new ObserverList<Foo>;
536 ListDestructor a(observer_list);
537 observer_list->AddObserver(&a);
538
539 FOR_EACH_OBSERVER(Foo, *observer_list, Observe(0));
540 // If this test fails, there'll be Valgrind errors when this function goes out
541 // of scope.
542}
543
[email protected]84aebed2010-02-25 03:09:41544} // namespace
[email protected]7ff48ca2013-02-06 16:56:19545} // namespace base