blob: 1bda3dcadae1a8d6193e0458ee7ac5f43bed380a [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"
[email protected]495cad92013-07-18 08:12:4012#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) {}
[email protected]44106182012-04-06 03:53:0229 virtual void Observe(int x) OVERRIDE {
initial.commitd7cae122008-07-26 21:49:3830 total += x * scaler_;
31 }
[email protected]44106182012-04-06 03:53:0232 virtual ~Adder() {}
initial.commitd7cae122008-07-26 21:49:3833 int total;
[email protected]44106182012-04-06 03:53:0234
initial.commitd7cae122008-07-26 21:49:3835 private:
36 int scaler_;
37};
38
39class Disrupter : public Foo {
40 public:
[email protected]503631c2008-10-22 23:09:2141 Disrupter(ObserverList<Foo>* list, Foo* doomed)
[email protected]44106182012-04-06 03:53:0242 : list_(list),
43 doomed_(doomed) {
44 }
45 virtual ~Disrupter() {}
46 virtual void Observe(int x) OVERRIDE {
[email protected]503631c2008-10-22 23:09:2147 list_->RemoveObserver(doomed_);
initial.commitd7cae122008-07-26 21:49:3848 }
[email protected]44106182012-04-06 03:53:0249
initial.commitd7cae122008-07-26 21:49:3850 private:
[email protected]503631c2008-10-22 23:09:2151 ObserverList<Foo>* list_;
initial.commitd7cae122008-07-26 21:49:3852 Foo* doomed_;
53};
54
[email protected]503631c2008-10-22 23:09:2155class ThreadSafeDisrupter : public Foo {
56 public:
57 ThreadSafeDisrupter(ObserverListThreadSafe<Foo>* list, Foo* doomed)
[email protected]44106182012-04-06 03:53:0258 : list_(list),
59 doomed_(doomed) {
60 }
61 virtual ~ThreadSafeDisrupter() {}
62 virtual void Observe(int x) OVERRIDE {
[email protected]503631c2008-10-22 23:09:2163 list_->RemoveObserver(doomed_);
64 }
[email protected]44106182012-04-06 03:53:0265
[email protected]503631c2008-10-22 23:09:2166 private:
67 ObserverListThreadSafe<Foo>* list_;
68 Foo* doomed_;
69};
70
[email protected]920b1fe2011-08-09 21:29:5971template <typename ObserverListType>
[email protected]b3e2fad02008-10-31 03:32:0672class AddInObserve : public Foo {
73 public:
[email protected]920b1fe2011-08-09 21:29:5974 explicit AddInObserve(ObserverListType* observer_list)
[email protected]b3e2fad02008-10-31 03:32:0675 : added(false),
76 observer_list(observer_list),
77 adder(1) {
78 }
[email protected]44106182012-04-06 03:53:0279
80 virtual void Observe(int x) OVERRIDE {
[email protected]b3e2fad02008-10-31 03:32:0681 if (!added) {
82 added = true;
83 observer_list->AddObserver(&adder);
84 }
85 }
86
87 bool added;
[email protected]920b1fe2011-08-09 21:29:5988 ObserverListType* observer_list;
[email protected]b3e2fad02008-10-31 03:32:0689 Adder adder;
90};
91
92
[email protected]ce292012009-11-05 18:46:4793static const int kThreadRunTime = 2000; // ms to run the multi-threaded test.
[email protected]503631c2008-10-22 23:09:2194
95// A thread for use in the ThreadSafeObserver test
96// which will add and remove itself from the notification
97// list repeatedly.
98class AddRemoveThread : public PlatformThread::Delegate,
99 public Foo {
100 public:
101 AddRemoveThread(ObserverListThreadSafe<Foo>* list, bool notify)
102 : list_(list),
[email protected]2e58cbd2012-08-06 01:03:05103 loop_(NULL),
[email protected]503631c2008-10-22 23:09:21104 in_list_(false),
105 start_(Time::Now()),
106 count_observes_(0),
107 count_addtask_(0),
[email protected]a6ffd7d2011-10-12 20:26:50108 do_notifies_(notify),
[email protected]c9f977b2013-04-25 12:17:15109 weak_factory_(this) {
[email protected]503631c2008-10-22 23:09:21110 }
111
[email protected]bd60d732009-01-05 19:40:53112 virtual ~AddRemoveThread() {
[email protected]503631c2008-10-22 23:09:21113 }
114
[email protected]44106182012-04-06 03:53:02115 virtual void ThreadMain() OVERRIDE {
[email protected]503631c2008-10-22 23:09:21116 loop_ = new MessageLoop(); // Fire up a message loop.
[email protected]0586b0e2010-02-12 21:38:37117 loop_->PostTask(
[email protected]a6ffd7d2011-10-12 20:26:50118 FROM_HERE,
119 base::Bind(&AddRemoveThread::AddTask, weak_factory_.GetWeakPtr()));
[email protected]503631c2008-10-22 23:09:21120 loop_->Run();
[email protected]52a261f2009-03-03 15:01:12121 //LOG(ERROR) << "Loop 0x" << std::hex << loop_ << " done. " <<
122 // count_observes_ << ", " << count_addtask_;
[email protected]503631c2008-10-22 23:09:21123 delete loop_;
124 loop_ = reinterpret_cast<MessageLoop*>(0xdeadbeef);
[email protected]bd60d732009-01-05 19:40:53125 delete this;
[email protected]503631c2008-10-22 23:09:21126 }
127
128 // This task just keeps posting to itself in an attempt
129 // to race with the notifier.
130 void AddTask() {
131 count_addtask_++;
132
133 if ((Time::Now() - start_).InMilliseconds() > kThreadRunTime) {
[email protected]b026e35d2010-10-19 02:31:03134 VLOG(1) << "DONE!";
[email protected]503631c2008-10-22 23:09:21135 return;
136 }
137
138 if (!in_list_) {
139 list_->AddObserver(this);
140 in_list_ = true;
141 }
142
143 if (do_notifies_) {
144 list_->Notify(&Foo::Observe, 10);
145 }
146
[email protected]a6ffd7d2011-10-12 20:26:50147 loop_->PostTask(
148 FROM_HERE,
149 base::Bind(&AddRemoveThread::AddTask, weak_factory_.GetWeakPtr()));
[email protected]503631c2008-10-22 23:09:21150 }
151
152 void Quit() {
[email protected]a085953f2013-02-04 23:40:00153 loop_->PostTask(FROM_HERE, MessageLoop::QuitWhenIdleClosure());
[email protected]503631c2008-10-22 23:09:21154 }
155
[email protected]44106182012-04-06 03:53:02156 virtual void Observe(int x) OVERRIDE {
[email protected]503631c2008-10-22 23:09:21157 count_observes_++;
158
159 // If we're getting called after we removed ourselves from
160 // the list, that is very bad!
161 DCHECK(in_list_);
162
163 // This callback should fire on the appropriate thread
164 EXPECT_EQ(loop_, MessageLoop::current());
165
166 list_->RemoveObserver(this);
167 in_list_ = false;
168 }
169
170 private:
171 ObserverListThreadSafe<Foo>* list_;
172 MessageLoop* loop_;
173 bool in_list_; // Are we currently registered for notifications.
174 // in_list_ is only used on |this| thread.
175 Time start_; // The time we started the test.
176
177 int count_observes_; // Number of times we observed.
178 int count_addtask_; // Number of times thread AddTask was called
179 bool do_notifies_; // Whether these threads should do notifications.
180
[email protected]a6ffd7d2011-10-12 20:26:50181 base::WeakPtrFactory<AddRemoveThread> weak_factory_;
[email protected]503631c2008-10-22 23:09:21182};
183
initial.commitd7cae122008-07-26 21:49:38184TEST(ObserverListTest, BasicTest) {
185 ObserverList<Foo> observer_list;
[email protected]631739f2011-06-05 07:07:12186 Adder a(1), b(-1), c(1), d(-1), e(-1);
[email protected]503631c2008-10-22 23:09:21187 Disrupter evil(&observer_list, &c);
initial.commitd7cae122008-07-26 21:49:38188
189 observer_list.AddObserver(&a);
190 observer_list.AddObserver(&b);
191
192 FOR_EACH_OBSERVER(Foo, observer_list, Observe(10));
193
194 observer_list.AddObserver(&evil);
195 observer_list.AddObserver(&c);
196 observer_list.AddObserver(&d);
197
[email protected]631739f2011-06-05 07:07:12198 // Removing an observer not in the list should do nothing.
199 observer_list.RemoveObserver(&e);
200
initial.commitd7cae122008-07-26 21:49:38201 FOR_EACH_OBSERVER(Foo, observer_list, Observe(10));
202
[email protected]14cf9102012-08-23 09:59:19203 EXPECT_EQ(20, a.total);
204 EXPECT_EQ(-20, b.total);
205 EXPECT_EQ(0, c.total);
206 EXPECT_EQ(-10, d.total);
207 EXPECT_EQ(0, e.total);
initial.commitd7cae122008-07-26 21:49:38208}
license.botbf09a502008-08-24 00:55:55209
[email protected]503631c2008-10-22 23:09:21210TEST(ObserverListThreadSafeTest, BasicTest) {
211 MessageLoop loop;
212
213 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list(
214 new ObserverListThreadSafe<Foo>);
215 Adder a(1);
216 Adder b(-1);
217 Adder c(1);
218 Adder d(-1);
219 ThreadSafeDisrupter evil(observer_list.get(), &c);
220
221 observer_list->AddObserver(&a);
222 observer_list->AddObserver(&b);
223
224 observer_list->Notify(&Foo::Observe, 10);
[email protected]7ff48ca2013-02-06 16:56:19225 RunLoop().RunUntilIdle();
[email protected]503631c2008-10-22 23:09:21226
227 observer_list->AddObserver(&evil);
228 observer_list->AddObserver(&c);
229 observer_list->AddObserver(&d);
230
231 observer_list->Notify(&Foo::Observe, 10);
[email protected]7ff48ca2013-02-06 16:56:19232 RunLoop().RunUntilIdle();
[email protected]503631c2008-10-22 23:09:21233
[email protected]14cf9102012-08-23 09:59:19234 EXPECT_EQ(20, a.total);
235 EXPECT_EQ(-20, b.total);
236 EXPECT_EQ(0, c.total);
237 EXPECT_EQ(-10, d.total);
[email protected]503631c2008-10-22 23:09:21238}
239
[email protected]631739f2011-06-05 07:07:12240TEST(ObserverListThreadSafeTest, RemoveObserver) {
241 MessageLoop loop;
242
243 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list(
244 new ObserverListThreadSafe<Foo>);
245 Adder a(1), b(1);
246
[email protected]3fe674d2012-04-05 17:52:10247 // A workaround for the compiler bug. See http://crbug.com/121960.
248 EXPECT_NE(&a, &b);
249
[email protected]631739f2011-06-05 07:07:12250 // Should do nothing.
251 observer_list->RemoveObserver(&a);
252 observer_list->RemoveObserver(&b);
253
254 observer_list->Notify(&Foo::Observe, 10);
[email protected]7ff48ca2013-02-06 16:56:19255 RunLoop().RunUntilIdle();
[email protected]631739f2011-06-05 07:07:12256
[email protected]14cf9102012-08-23 09:59:19257 EXPECT_EQ(0, a.total);
258 EXPECT_EQ(0, b.total);
[email protected]631739f2011-06-05 07:07:12259
260 observer_list->AddObserver(&a);
261
262 // Should also do nothing.
263 observer_list->RemoveObserver(&b);
264
265 observer_list->Notify(&Foo::Observe, 10);
[email protected]7ff48ca2013-02-06 16:56:19266 RunLoop().RunUntilIdle();
[email protected]631739f2011-06-05 07:07:12267
[email protected]14cf9102012-08-23 09:59:19268 EXPECT_EQ(10, a.total);
269 EXPECT_EQ(0, b.total);
[email protected]631739f2011-06-05 07:07:12270}
271
[email protected]c2b1b302011-11-23 20:34:04272TEST(ObserverListThreadSafeTest, WithoutMessageLoop) {
273 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list(
274 new ObserverListThreadSafe<Foo>);
275
276 Adder a(1), b(1), c(1);
277
278 // No MessageLoop, so these should not be added.
279 observer_list->AddObserver(&a);
280 observer_list->AddObserver(&b);
281
282 {
283 // Add c when there's a loop.
284 MessageLoop loop;
285 observer_list->AddObserver(&c);
286
287 observer_list->Notify(&Foo::Observe, 10);
[email protected]7ff48ca2013-02-06 16:56:19288 RunLoop().RunUntilIdle();
[email protected]c2b1b302011-11-23 20:34:04289
290 EXPECT_EQ(0, a.total);
291 EXPECT_EQ(0, b.total);
292 EXPECT_EQ(10, c.total);
293
294 // Now add a when there's a loop.
295 observer_list->AddObserver(&a);
296
297 // Remove c when there's a loop.
298 observer_list->RemoveObserver(&c);
299
300 // Notify again.
301 observer_list->Notify(&Foo::Observe, 20);
[email protected]7ff48ca2013-02-06 16:56:19302 RunLoop().RunUntilIdle();
[email protected]c2b1b302011-11-23 20:34:04303
304 EXPECT_EQ(20, a.total);
305 EXPECT_EQ(0, b.total);
306 EXPECT_EQ(10, c.total);
307 }
308
309 // Removing should always succeed with or without a loop.
310 observer_list->RemoveObserver(&a);
311
312 // Notifying should not fail but should also be a no-op.
313 MessageLoop loop;
314 observer_list->AddObserver(&b);
315 observer_list->Notify(&Foo::Observe, 30);
[email protected]7ff48ca2013-02-06 16:56:19316 RunLoop().RunUntilIdle();
[email protected]c2b1b302011-11-23 20:34:04317
318 EXPECT_EQ(20, a.total);
319 EXPECT_EQ(30, b.total);
320 EXPECT_EQ(10, c.total);
321}
322
[email protected]3c0d45e2010-09-17 19:33:06323class FooRemover : public Foo {
324 public:
325 explicit FooRemover(ObserverListThreadSafe<Foo>* list) : list_(list) {}
326 virtual ~FooRemover() {}
327
328 void AddFooToRemove(Foo* foo) {
329 foos_.push_back(foo);
330 }
331
[email protected]44106182012-04-06 03:53:02332 virtual void Observe(int x) OVERRIDE {
[email protected]3c0d45e2010-09-17 19:33:06333 std::vector<Foo*> tmp;
334 tmp.swap(foos_);
335 for (std::vector<Foo*>::iterator it = tmp.begin();
336 it != tmp.end(); ++it) {
337 list_->RemoveObserver(*it);
338 }
339 }
340
341 private:
342 const scoped_refptr<ObserverListThreadSafe<Foo> > list_;
343 std::vector<Foo*> foos_;
344};
345
346TEST(ObserverListThreadSafeTest, RemoveMultipleObservers) {
347 MessageLoop loop;
348 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list(
349 new ObserverListThreadSafe<Foo>);
350
[email protected]3703e922013-05-31 21:37:53351 FooRemover a(observer_list.get());
[email protected]3c0d45e2010-09-17 19:33:06352 Adder b(1);
353
354 observer_list->AddObserver(&a);
355 observer_list->AddObserver(&b);
356
357 a.AddFooToRemove(&a);
358 a.AddFooToRemove(&b);
359
360 observer_list->Notify(&Foo::Observe, 1);
[email protected]7ff48ca2013-02-06 16:56:19361 RunLoop().RunUntilIdle();
[email protected]3c0d45e2010-09-17 19:33:06362}
[email protected]503631c2008-10-22 23:09:21363
364// A test driver for a multi-threaded notification loop. Runs a number
365// of observer threads, each of which constantly adds/removes itself
366// from the observer list. Optionally, if cross_thread_notifies is set
367// to true, the observer threads will also trigger notifications to
368// all observers.
[email protected]52a261f2009-03-03 15:01:12369static void ThreadSafeObserverHarness(int num_threads,
[email protected]503631c2008-10-22 23:09:21370 bool cross_thread_notifies) {
371 MessageLoop loop;
372
373 const int kMaxThreads = 15;
374 num_threads = num_threads > kMaxThreads ? kMaxThreads : num_threads;
375
376 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list(
377 new ObserverListThreadSafe<Foo>);
378 Adder a(1);
379 Adder b(-1);
380 Adder c(1);
381 Adder d(-1);
382
383 observer_list->AddObserver(&a);
384 observer_list->AddObserver(&b);
385
386 AddRemoveThread* threaded_observer[kMaxThreads];
[email protected]ce072a72010-12-31 20:02:16387 base::PlatformThreadHandle threads[kMaxThreads];
[email protected]503631c2008-10-22 23:09:21388 for (int index = 0; index < num_threads; index++) {
389 threaded_observer[index] = new AddRemoveThread(observer_list.get(), false);
390 EXPECT_TRUE(PlatformThread::Create(0,
391 threaded_observer[index], &threads[index]));
392 }
393
394 Time start = Time::Now();
395 while (true) {
396 if ((Time::Now() - start).InMilliseconds() > kThreadRunTime)
397 break;
398
399 observer_list->Notify(&Foo::Observe, 10);
400
[email protected]7ff48ca2013-02-06 16:56:19401 RunLoop().RunUntilIdle();
[email protected]503631c2008-10-22 23:09:21402 }
403
404 for (int index = 0; index < num_threads; index++) {
405 threaded_observer[index]->Quit();
406 PlatformThread::Join(threads[index]);
407 }
408}
409
410TEST(ObserverListThreadSafeTest, CrossThreadObserver) {
411 // Use 7 observer threads. Notifications only come from
412 // the main thread.
413 ThreadSafeObserverHarness(7, false);
414}
415
416TEST(ObserverListThreadSafeTest, CrossThreadNotifications) {
417 // Use 3 observer threads. Notifications will fire from
418 // the main thread and all 3 observer threads.
419 ThreadSafeObserverHarness(3, true);
420}
[email protected]b3e2fad02008-10-31 03:32:06421
[email protected]920b1fe2011-08-09 21:29:59422TEST(ObserverListThreadSafeTest, OutlivesMessageLoop) {
423 MessageLoop* loop = new MessageLoop;
424 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list(
425 new ObserverListThreadSafe<Foo>);
426
427 Adder a(1);
428 observer_list->AddObserver(&a);
429 delete loop;
430 // Test passes if we don't crash here.
431 observer_list->Notify(&Foo::Observe, 1);
432}
433
[email protected]b3e2fad02008-10-31 03:32:06434TEST(ObserverListTest, Existing) {
435 ObserverList<Foo> observer_list(ObserverList<Foo>::NOTIFY_EXISTING_ONLY);
436 Adder a(1);
[email protected]920b1fe2011-08-09 21:29:59437 AddInObserve<ObserverList<Foo> > b(&observer_list);
[email protected]b3e2fad02008-10-31 03:32:06438
439 observer_list.AddObserver(&a);
440 observer_list.AddObserver(&b);
441
442 FOR_EACH_OBSERVER(Foo, observer_list, Observe(1));
443
444 EXPECT_TRUE(b.added);
445 // B's adder should not have been notified because it was added during
[email protected]6be50d72014-05-08 23:49:40446 // notification.
[email protected]b3e2fad02008-10-31 03:32:06447 EXPECT_EQ(0, b.adder.total);
448
449 // Notify again to make sure b's adder is notified.
450 FOR_EACH_OBSERVER(Foo, observer_list, Observe(1));
451 EXPECT_EQ(1, b.adder.total);
452}
[email protected]84aebed2010-02-25 03:09:41453
[email protected]920b1fe2011-08-09 21:29:59454// Same as above, but for ObserverListThreadSafe
455TEST(ObserverListThreadSafeTest, Existing) {
456 MessageLoop loop;
457 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list(
458 new ObserverListThreadSafe<Foo>(ObserverList<Foo>::NOTIFY_EXISTING_ONLY));
459 Adder a(1);
460 AddInObserve<ObserverListThreadSafe<Foo> > b(observer_list.get());
461
462 observer_list->AddObserver(&a);
463 observer_list->AddObserver(&b);
464
465 observer_list->Notify(&Foo::Observe, 1);
[email protected]7ff48ca2013-02-06 16:56:19466 RunLoop().RunUntilIdle();
[email protected]920b1fe2011-08-09 21:29:59467
468 EXPECT_TRUE(b.added);
469 // B's adder should not have been notified because it was added during
[email protected]6be50d72014-05-08 23:49:40470 // notification.
[email protected]920b1fe2011-08-09 21:29:59471 EXPECT_EQ(0, b.adder.total);
472
473 // Notify again to make sure b's adder is notified.
474 observer_list->Notify(&Foo::Observe, 1);
[email protected]7ff48ca2013-02-06 16:56:19475 RunLoop().RunUntilIdle();
[email protected]920b1fe2011-08-09 21:29:59476 EXPECT_EQ(1, b.adder.total);
477}
478
[email protected]84aebed2010-02-25 03:09:41479class AddInClearObserve : public Foo {
480 public:
481 explicit AddInClearObserve(ObserverList<Foo>* list)
482 : list_(list), added_(false), adder_(1) {}
483
[email protected]44106182012-04-06 03:53:02484 virtual void Observe(int /* x */) OVERRIDE {
[email protected]84aebed2010-02-25 03:09:41485 list_->Clear();
486 list_->AddObserver(&adder_);
487 added_ = true;
488 }
489
490 bool added() const { return added_; }
491 const Adder& adder() const { return adder_; }
492
493 private:
494 ObserverList<Foo>* const list_;
495
496 bool added_;
497 Adder adder_;
498};
499
500TEST(ObserverListTest, ClearNotifyAll) {
501 ObserverList<Foo> observer_list;
502 AddInClearObserve a(&observer_list);
503
504 observer_list.AddObserver(&a);
505
506 FOR_EACH_OBSERVER(Foo, observer_list, Observe(1));
507 EXPECT_TRUE(a.added());
508 EXPECT_EQ(1, a.adder().total)
509 << "Adder should observe once and have sum of 1.";
510}
511
512TEST(ObserverListTest, ClearNotifyExistingOnly) {
513 ObserverList<Foo> observer_list(ObserverList<Foo>::NOTIFY_EXISTING_ONLY);
514 AddInClearObserve a(&observer_list);
515
516 observer_list.AddObserver(&a);
517
518 FOR_EACH_OBSERVER(Foo, observer_list, Observe(1));
519 EXPECT_TRUE(a.added());
520 EXPECT_EQ(0, a.adder().total)
521 << "Adder should not observe, so sum should still be 0.";
522}
523
[email protected]671b74d2011-06-09 03:41:18524class ListDestructor : public Foo {
525 public:
526 explicit ListDestructor(ObserverList<Foo>* list) : list_(list) {}
[email protected]44106182012-04-06 03:53:02527 virtual ~ListDestructor() {}
528
529 virtual void Observe(int x) OVERRIDE {
[email protected]671b74d2011-06-09 03:41:18530 delete list_;
531 }
[email protected]2e58cbd2012-08-06 01:03:05532
[email protected]671b74d2011-06-09 03:41:18533 private:
534 ObserverList<Foo>* list_;
535};
536
537
538TEST(ObserverListTest, IteratorOutlivesList) {
539 ObserverList<Foo>* observer_list = new ObserverList<Foo>;
540 ListDestructor a(observer_list);
541 observer_list->AddObserver(&a);
542
543 FOR_EACH_OBSERVER(Foo, *observer_list, Observe(0));
544 // If this test fails, there'll be Valgrind errors when this function goes out
545 // of scope.
546}
547
[email protected]84aebed2010-02-25 03:09:41548} // namespace
[email protected]7ff48ca2013-02-06 16:56:19549} // namespace base