Remove an old PangoFontDescription forward declaration.
[chromium-blink-merge.git] / base / observer_list_unittest.cc
blob636aa83fb9dcc22f9b1b1360d7609e6dfcd7c513
1 // Copyright (c) 2012 The Chromium 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.
5 #include "base/observer_list.h"
6 #include "base/observer_list_threadsafe.h"
8 #include <vector>
10 #include "base/compiler_specific.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/run_loop.h"
14 #include "base/threading/platform_thread.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 namespace base {
18 namespace {
20 class Foo {
21 public:
22 virtual void Observe(int x) = 0;
23 virtual ~Foo() {}
26 class Adder : public Foo {
27 public:
28 explicit Adder(int scaler) : total(0), scaler_(scaler) {}
29 void Observe(int x) override { total += x * scaler_; }
30 ~Adder() override {}
31 int total;
33 private:
34 int scaler_;
37 class Disrupter : public Foo {
38 public:
39 Disrupter(ObserverList<Foo>* list, Foo* doomed)
40 : list_(list),
41 doomed_(doomed) {
43 ~Disrupter() override {}
44 void Observe(int x) override { list_->RemoveObserver(doomed_); }
46 private:
47 ObserverList<Foo>* list_;
48 Foo* doomed_;
51 class ThreadSafeDisrupter : public Foo {
52 public:
53 ThreadSafeDisrupter(ObserverListThreadSafe<Foo>* list, Foo* doomed)
54 : list_(list),
55 doomed_(doomed) {
57 ~ThreadSafeDisrupter() override {}
58 void Observe(int x) override { list_->RemoveObserver(doomed_); }
60 private:
61 ObserverListThreadSafe<Foo>* list_;
62 Foo* doomed_;
65 template <typename ObserverListType>
66 class AddInObserve : public Foo {
67 public:
68 explicit AddInObserve(ObserverListType* observer_list)
69 : added(false),
70 observer_list(observer_list),
71 adder(1) {
74 virtual void Observe(int x) override {
75 if (!added) {
76 added = true;
77 observer_list->AddObserver(&adder);
81 bool added;
82 ObserverListType* observer_list;
83 Adder adder;
87 static const int kThreadRunTime = 2000; // ms to run the multi-threaded test.
89 // A thread for use in the ThreadSafeObserver test
90 // which will add and remove itself from the notification
91 // list repeatedly.
92 class AddRemoveThread : public PlatformThread::Delegate,
93 public Foo {
94 public:
95 AddRemoveThread(ObserverListThreadSafe<Foo>* list, bool notify)
96 : list_(list),
97 loop_(NULL),
98 in_list_(false),
99 start_(Time::Now()),
100 count_observes_(0),
101 count_addtask_(0),
102 do_notifies_(notify),
103 weak_factory_(this) {
106 ~AddRemoveThread() override {}
108 void ThreadMain() override {
109 loop_ = new MessageLoop(); // Fire up a message loop.
110 loop_->PostTask(
111 FROM_HERE,
112 base::Bind(&AddRemoveThread::AddTask, weak_factory_.GetWeakPtr()));
113 loop_->Run();
114 //LOG(ERROR) << "Loop 0x" << std::hex << loop_ << " done. " <<
115 // count_observes_ << ", " << count_addtask_;
116 delete loop_;
117 loop_ = reinterpret_cast<MessageLoop*>(0xdeadbeef);
118 delete this;
121 // This task just keeps posting to itself in an attempt
122 // to race with the notifier.
123 void AddTask() {
124 count_addtask_++;
126 if ((Time::Now() - start_).InMilliseconds() > kThreadRunTime) {
127 VLOG(1) << "DONE!";
128 return;
131 if (!in_list_) {
132 list_->AddObserver(this);
133 in_list_ = true;
136 if (do_notifies_) {
137 list_->Notify(FROM_HERE, &Foo::Observe, 10);
140 loop_->PostTask(
141 FROM_HERE,
142 base::Bind(&AddRemoveThread::AddTask, weak_factory_.GetWeakPtr()));
145 void Quit() {
146 loop_->PostTask(FROM_HERE, MessageLoop::QuitWhenIdleClosure());
149 void Observe(int x) override {
150 count_observes_++;
152 // If we're getting called after we removed ourselves from
153 // the list, that is very bad!
154 DCHECK(in_list_);
156 // This callback should fire on the appropriate thread
157 EXPECT_EQ(loop_, MessageLoop::current());
159 list_->RemoveObserver(this);
160 in_list_ = false;
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.
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.
174 base::WeakPtrFactory<AddRemoveThread> weak_factory_;
177 TEST(ObserverListTest, BasicTest) {
178 ObserverList<Foo> observer_list;
179 Adder a(1), b(-1), c(1), d(-1), e(-1);
180 Disrupter evil(&observer_list, &c);
182 observer_list.AddObserver(&a);
183 observer_list.AddObserver(&b);
185 EXPECT_TRUE(observer_list.HasObserver(&a));
186 EXPECT_FALSE(observer_list.HasObserver(&c));
188 FOR_EACH_OBSERVER(Foo, observer_list, Observe(10));
190 observer_list.AddObserver(&evil);
191 observer_list.AddObserver(&c);
192 observer_list.AddObserver(&d);
194 // Removing an observer not in the list should do nothing.
195 observer_list.RemoveObserver(&e);
197 FOR_EACH_OBSERVER(Foo, observer_list, Observe(10));
199 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);
206 TEST(ObserverListThreadSafeTest, BasicTest) {
207 MessageLoop loop;
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);
217 observer_list->AddObserver(&a);
218 observer_list->AddObserver(&b);
220 observer_list->Notify(FROM_HERE, &Foo::Observe, 10);
221 RunLoop().RunUntilIdle();
223 observer_list->AddObserver(&evil);
224 observer_list->AddObserver(&c);
225 observer_list->AddObserver(&d);
227 observer_list->Notify(FROM_HERE, &Foo::Observe, 10);
228 RunLoop().RunUntilIdle();
230 EXPECT_EQ(20, a.total);
231 EXPECT_EQ(-20, b.total);
232 EXPECT_EQ(0, c.total);
233 EXPECT_EQ(-10, d.total);
236 TEST(ObserverListThreadSafeTest, RemoveObserver) {
237 MessageLoop loop;
239 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list(
240 new ObserverListThreadSafe<Foo>);
241 Adder a(1), b(1);
243 // A workaround for the compiler bug. See http://crbug.com/121960.
244 EXPECT_NE(&a, &b);
246 // Should do nothing.
247 observer_list->RemoveObserver(&a);
248 observer_list->RemoveObserver(&b);
250 observer_list->Notify(FROM_HERE, &Foo::Observe, 10);
251 RunLoop().RunUntilIdle();
253 EXPECT_EQ(0, a.total);
254 EXPECT_EQ(0, b.total);
256 observer_list->AddObserver(&a);
258 // Should also do nothing.
259 observer_list->RemoveObserver(&b);
261 observer_list->Notify(FROM_HERE, &Foo::Observe, 10);
262 RunLoop().RunUntilIdle();
264 EXPECT_EQ(10, a.total);
265 EXPECT_EQ(0, b.total);
268 TEST(ObserverListThreadSafeTest, WithoutMessageLoop) {
269 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list(
270 new ObserverListThreadSafe<Foo>);
272 Adder a(1), b(1), c(1);
274 // No MessageLoop, so these should not be added.
275 observer_list->AddObserver(&a);
276 observer_list->AddObserver(&b);
279 // Add c when there's a loop.
280 MessageLoop loop;
281 observer_list->AddObserver(&c);
283 observer_list->Notify(FROM_HERE, &Foo::Observe, 10);
284 RunLoop().RunUntilIdle();
286 EXPECT_EQ(0, a.total);
287 EXPECT_EQ(0, b.total);
288 EXPECT_EQ(10, c.total);
290 // Now add a when there's a loop.
291 observer_list->AddObserver(&a);
293 // Remove c when there's a loop.
294 observer_list->RemoveObserver(&c);
296 // Notify again.
297 observer_list->Notify(FROM_HERE, &Foo::Observe, 20);
298 RunLoop().RunUntilIdle();
300 EXPECT_EQ(20, a.total);
301 EXPECT_EQ(0, b.total);
302 EXPECT_EQ(10, c.total);
305 // Removing should always succeed with or without a loop.
306 observer_list->RemoveObserver(&a);
308 // Notifying should not fail but should also be a no-op.
309 MessageLoop loop;
310 observer_list->AddObserver(&b);
311 observer_list->Notify(FROM_HERE, &Foo::Observe, 30);
312 RunLoop().RunUntilIdle();
314 EXPECT_EQ(20, a.total);
315 EXPECT_EQ(30, b.total);
316 EXPECT_EQ(10, c.total);
319 class FooRemover : public Foo {
320 public:
321 explicit FooRemover(ObserverListThreadSafe<Foo>* list) : list_(list) {}
322 ~FooRemover() override {}
324 void AddFooToRemove(Foo* foo) {
325 foos_.push_back(foo);
328 void Observe(int x) override {
329 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);
337 private:
338 const scoped_refptr<ObserverListThreadSafe<Foo> > list_;
339 std::vector<Foo*> foos_;
342 TEST(ObserverListThreadSafeTest, RemoveMultipleObservers) {
343 MessageLoop loop;
344 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list(
345 new ObserverListThreadSafe<Foo>);
347 FooRemover a(observer_list.get());
348 Adder b(1);
350 observer_list->AddObserver(&a);
351 observer_list->AddObserver(&b);
353 a.AddFooToRemove(&a);
354 a.AddFooToRemove(&b);
356 observer_list->Notify(FROM_HERE, &Foo::Observe, 1);
357 RunLoop().RunUntilIdle();
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.
365 static void ThreadSafeObserverHarness(int num_threads,
366 bool cross_thread_notifies) {
367 MessageLoop loop;
369 const int kMaxThreads = 15;
370 num_threads = num_threads > kMaxThreads ? kMaxThreads : num_threads;
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);
379 observer_list->AddObserver(&a);
380 observer_list->AddObserver(&b);
382 AddRemoveThread* threaded_observer[kMaxThreads];
383 base::PlatformThreadHandle threads[kMaxThreads];
384 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]));
390 Time start = Time::Now();
391 while (true) {
392 if ((Time::Now() - start).InMilliseconds() > kThreadRunTime)
393 break;
395 observer_list->Notify(FROM_HERE, &Foo::Observe, 10);
397 RunLoop().RunUntilIdle();
400 for (int index = 0; index < num_threads; index++) {
401 threaded_observer[index]->Quit();
402 PlatformThread::Join(threads[index]);
406 TEST(ObserverListThreadSafeTest, CrossThreadObserver) {
407 // Use 7 observer threads. Notifications only come from
408 // the main thread.
409 ThreadSafeObserverHarness(7, false);
412 TEST(ObserverListThreadSafeTest, CrossThreadNotifications) {
413 // Use 3 observer threads. Notifications will fire from
414 // the main thread and all 3 observer threads.
415 ThreadSafeObserverHarness(3, true);
418 TEST(ObserverListThreadSafeTest, OutlivesMessageLoop) {
419 MessageLoop* loop = new MessageLoop;
420 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list(
421 new ObserverListThreadSafe<Foo>);
423 Adder a(1);
424 observer_list->AddObserver(&a);
425 delete loop;
426 // Test passes if we don't crash here.
427 observer_list->Notify(FROM_HERE, &Foo::Observe, 1);
430 TEST(ObserverListTest, Existing) {
431 ObserverList<Foo> observer_list(ObserverList<Foo>::NOTIFY_EXISTING_ONLY);
432 Adder a(1);
433 AddInObserve<ObserverList<Foo> > b(&observer_list);
435 observer_list.AddObserver(&a);
436 observer_list.AddObserver(&b);
438 FOR_EACH_OBSERVER(Foo, observer_list, Observe(1));
440 EXPECT_TRUE(b.added);
441 // B's adder should not have been notified because it was added during
442 // notification.
443 EXPECT_EQ(0, b.adder.total);
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);
450 // Same as above, but for ObserverListThreadSafe
451 TEST(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());
458 observer_list->AddObserver(&a);
459 observer_list->AddObserver(&b);
461 observer_list->Notify(FROM_HERE, &Foo::Observe, 1);
462 RunLoop().RunUntilIdle();
464 EXPECT_TRUE(b.added);
465 // B's adder should not have been notified because it was added during
466 // notification.
467 EXPECT_EQ(0, b.adder.total);
469 // Notify again to make sure b's adder is notified.
470 observer_list->Notify(FROM_HERE, &Foo::Observe, 1);
471 RunLoop().RunUntilIdle();
472 EXPECT_EQ(1, b.adder.total);
475 class AddInClearObserve : public Foo {
476 public:
477 explicit AddInClearObserve(ObserverList<Foo>* list)
478 : list_(list), added_(false), adder_(1) {}
480 void Observe(int /* x */) override {
481 list_->Clear();
482 list_->AddObserver(&adder_);
483 added_ = true;
486 bool added() const { return added_; }
487 const Adder& adder() const { return adder_; }
489 private:
490 ObserverList<Foo>* const list_;
492 bool added_;
493 Adder adder_;
496 TEST(ObserverListTest, ClearNotifyAll) {
497 ObserverList<Foo> observer_list;
498 AddInClearObserve a(&observer_list);
500 observer_list.AddObserver(&a);
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.";
508 TEST(ObserverListTest, ClearNotifyExistingOnly) {
509 ObserverList<Foo> observer_list(ObserverList<Foo>::NOTIFY_EXISTING_ONLY);
510 AddInClearObserve a(&observer_list);
512 observer_list.AddObserver(&a);
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.";
520 class ListDestructor : public Foo {
521 public:
522 explicit ListDestructor(ObserverList<Foo>* list) : list_(list) {}
523 ~ListDestructor() override {}
525 void Observe(int x) override { delete list_; }
527 private:
528 ObserverList<Foo>* list_;
532 TEST(ObserverListTest, IteratorOutlivesList) {
533 ObserverList<Foo>* observer_list = new ObserverList<Foo>;
534 ListDestructor a(observer_list);
535 observer_list->AddObserver(&a);
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.
542 } // namespace
543 } // namespace base