[content shell] implement testRunner.overridePreference
[chromium-blink-merge.git] / base / observer_list_unittest.cc
blob1650d3ec80370b9c654eab9d9e8a00063f8b440e
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.h"
13 #include "base/threading/platform_thread.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 using base::PlatformThread;
17 using base::Time;
19 namespace {
21 class Foo {
22 public:
23 virtual void Observe(int x) = 0;
24 virtual ~Foo() {}
27 class Adder : public Foo {
28 public:
29 explicit Adder(int scaler) : total(0), scaler_(scaler) {}
30 virtual void Observe(int x) OVERRIDE {
31 total += x * scaler_;
33 virtual ~Adder() {}
34 int total;
36 private:
37 int scaler_;
40 class Disrupter : public Foo {
41 public:
42 Disrupter(ObserverList<Foo>* list, Foo* doomed)
43 : list_(list),
44 doomed_(doomed) {
46 virtual ~Disrupter() {}
47 virtual void Observe(int x) OVERRIDE {
48 list_->RemoveObserver(doomed_);
51 private:
52 ObserverList<Foo>* list_;
53 Foo* doomed_;
56 class ThreadSafeDisrupter : public Foo {
57 public:
58 ThreadSafeDisrupter(ObserverListThreadSafe<Foo>* list, Foo* doomed)
59 : list_(list),
60 doomed_(doomed) {
62 virtual ~ThreadSafeDisrupter() {}
63 virtual void Observe(int x) OVERRIDE {
64 list_->RemoveObserver(doomed_);
67 private:
68 ObserverListThreadSafe<Foo>* list_;
69 Foo* doomed_;
72 template <typename ObserverListType>
73 class AddInObserve : public Foo {
74 public:
75 explicit AddInObserve(ObserverListType* observer_list)
76 : added(false),
77 observer_list(observer_list),
78 adder(1) {
81 virtual void Observe(int x) OVERRIDE {
82 if (!added) {
83 added = true;
84 observer_list->AddObserver(&adder);
88 bool added;
89 ObserverListType* observer_list;
90 Adder adder;
94 static const int kThreadRunTime = 2000; // ms to run the multi-threaded test.
96 // A thread for use in the ThreadSafeObserver test
97 // which will add and remove itself from the notification
98 // list repeatedly.
99 class AddRemoveThread : public PlatformThread::Delegate,
100 public Foo {
101 public:
102 AddRemoveThread(ObserverListThreadSafe<Foo>* list, bool notify)
103 : list_(list),
104 loop_(NULL),
105 in_list_(false),
106 start_(Time::Now()),
107 count_observes_(0),
108 count_addtask_(0),
109 do_notifies_(notify),
110 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
113 virtual ~AddRemoveThread() {
116 virtual void ThreadMain() OVERRIDE {
117 loop_ = new MessageLoop(); // Fire up a message loop.
118 loop_->PostTask(
119 FROM_HERE,
120 base::Bind(&AddRemoveThread::AddTask, weak_factory_.GetWeakPtr()));
121 loop_->Run();
122 //LOG(ERROR) << "Loop 0x" << std::hex << loop_ << " done. " <<
123 // count_observes_ << ", " << count_addtask_;
124 delete loop_;
125 loop_ = reinterpret_cast<MessageLoop*>(0xdeadbeef);
126 delete this;
129 // This task just keeps posting to itself in an attempt
130 // to race with the notifier.
131 void AddTask() {
132 count_addtask_++;
134 if ((Time::Now() - start_).InMilliseconds() > kThreadRunTime) {
135 VLOG(1) << "DONE!";
136 return;
139 if (!in_list_) {
140 list_->AddObserver(this);
141 in_list_ = true;
144 if (do_notifies_) {
145 list_->Notify(&Foo::Observe, 10);
148 loop_->PostTask(
149 FROM_HERE,
150 base::Bind(&AddRemoveThread::AddTask, weak_factory_.GetWeakPtr()));
153 void Quit() {
154 loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure());
157 virtual void Observe(int x) OVERRIDE {
158 count_observes_++;
160 // If we're getting called after we removed ourselves from
161 // the list, that is very bad!
162 DCHECK(in_list_);
164 // This callback should fire on the appropriate thread
165 EXPECT_EQ(loop_, MessageLoop::current());
167 list_->RemoveObserver(this);
168 in_list_ = false;
171 private:
172 ObserverListThreadSafe<Foo>* list_;
173 MessageLoop* loop_;
174 bool in_list_; // Are we currently registered for notifications.
175 // in_list_ is only used on |this| thread.
176 Time start_; // The time we started the test.
178 int count_observes_; // Number of times we observed.
179 int count_addtask_; // Number of times thread AddTask was called
180 bool do_notifies_; // Whether these threads should do notifications.
182 base::WeakPtrFactory<AddRemoveThread> weak_factory_;
185 TEST(ObserverListTest, BasicTest) {
186 ObserverList<Foo> observer_list;
187 Adder a(1), b(-1), c(1), d(-1), e(-1);
188 Disrupter evil(&observer_list, &c);
190 observer_list.AddObserver(&a);
191 observer_list.AddObserver(&b);
193 FOR_EACH_OBSERVER(Foo, observer_list, Observe(10));
195 observer_list.AddObserver(&evil);
196 observer_list.AddObserver(&c);
197 observer_list.AddObserver(&d);
199 // Removing an observer not in the list should do nothing.
200 observer_list.RemoveObserver(&e);
202 FOR_EACH_OBSERVER(Foo, observer_list, Observe(10));
204 EXPECT_EQ(20, a.total);
205 EXPECT_EQ(-20, b.total);
206 EXPECT_EQ(0, c.total);
207 EXPECT_EQ(-10, d.total);
208 EXPECT_EQ(0, e.total);
211 TEST(ObserverListThreadSafeTest, BasicTest) {
212 MessageLoop loop;
214 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list(
215 new ObserverListThreadSafe<Foo>);
216 Adder a(1);
217 Adder b(-1);
218 Adder c(1);
219 Adder d(-1);
220 ThreadSafeDisrupter evil(observer_list.get(), &c);
222 observer_list->AddObserver(&a);
223 observer_list->AddObserver(&b);
225 observer_list->Notify(&Foo::Observe, 10);
226 loop.RunUntilIdle();
228 observer_list->AddObserver(&evil);
229 observer_list->AddObserver(&c);
230 observer_list->AddObserver(&d);
232 observer_list->Notify(&Foo::Observe, 10);
233 loop.RunUntilIdle();
235 EXPECT_EQ(20, a.total);
236 EXPECT_EQ(-20, b.total);
237 EXPECT_EQ(0, c.total);
238 EXPECT_EQ(-10, d.total);
241 TEST(ObserverListThreadSafeTest, RemoveObserver) {
242 MessageLoop loop;
244 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list(
245 new ObserverListThreadSafe<Foo>);
246 Adder a(1), b(1);
248 // A workaround for the compiler bug. See http://crbug.com/121960.
249 EXPECT_NE(&a, &b);
251 // Should do nothing.
252 observer_list->RemoveObserver(&a);
253 observer_list->RemoveObserver(&b);
255 observer_list->Notify(&Foo::Observe, 10);
256 loop.RunUntilIdle();
258 EXPECT_EQ(0, a.total);
259 EXPECT_EQ(0, b.total);
261 observer_list->AddObserver(&a);
263 // Should also do nothing.
264 observer_list->RemoveObserver(&b);
266 observer_list->Notify(&Foo::Observe, 10);
267 loop.RunUntilIdle();
269 EXPECT_EQ(10, a.total);
270 EXPECT_EQ(0, b.total);
273 TEST(ObserverListThreadSafeTest, WithoutMessageLoop) {
274 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list(
275 new ObserverListThreadSafe<Foo>);
277 Adder a(1), b(1), c(1);
279 // No MessageLoop, so these should not be added.
280 observer_list->AddObserver(&a);
281 observer_list->AddObserver(&b);
284 // Add c when there's a loop.
285 MessageLoop loop;
286 observer_list->AddObserver(&c);
288 observer_list->Notify(&Foo::Observe, 10);
289 loop.RunUntilIdle();
291 EXPECT_EQ(0, a.total);
292 EXPECT_EQ(0, b.total);
293 EXPECT_EQ(10, c.total);
295 // Now add a when there's a loop.
296 observer_list->AddObserver(&a);
298 // Remove c when there's a loop.
299 observer_list->RemoveObserver(&c);
301 // Notify again.
302 observer_list->Notify(&Foo::Observe, 20);
303 loop.RunUntilIdle();
305 EXPECT_EQ(20, a.total);
306 EXPECT_EQ(0, b.total);
307 EXPECT_EQ(10, c.total);
310 // Removing should always succeed with or without a loop.
311 observer_list->RemoveObserver(&a);
313 // Notifying should not fail but should also be a no-op.
314 MessageLoop loop;
315 observer_list->AddObserver(&b);
316 observer_list->Notify(&Foo::Observe, 30);
317 loop.RunUntilIdle();
319 EXPECT_EQ(20, a.total);
320 EXPECT_EQ(30, b.total);
321 EXPECT_EQ(10, c.total);
324 class FooRemover : public Foo {
325 public:
326 explicit FooRemover(ObserverListThreadSafe<Foo>* list) : list_(list) {}
327 virtual ~FooRemover() {}
329 void AddFooToRemove(Foo* foo) {
330 foos_.push_back(foo);
333 virtual void Observe(int x) OVERRIDE {
334 std::vector<Foo*> tmp;
335 tmp.swap(foos_);
336 for (std::vector<Foo*>::iterator it = tmp.begin();
337 it != tmp.end(); ++it) {
338 list_->RemoveObserver(*it);
342 private:
343 const scoped_refptr<ObserverListThreadSafe<Foo> > list_;
344 std::vector<Foo*> foos_;
347 TEST(ObserverListThreadSafeTest, RemoveMultipleObservers) {
348 MessageLoop loop;
349 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list(
350 new ObserverListThreadSafe<Foo>);
352 FooRemover a(observer_list);
353 Adder b(1);
355 observer_list->AddObserver(&a);
356 observer_list->AddObserver(&b);
358 a.AddFooToRemove(&a);
359 a.AddFooToRemove(&b);
361 observer_list->Notify(&Foo::Observe, 1);
362 loop.RunUntilIdle();
365 // A test driver for a multi-threaded notification loop. Runs a number
366 // of observer threads, each of which constantly adds/removes itself
367 // from the observer list. Optionally, if cross_thread_notifies is set
368 // to true, the observer threads will also trigger notifications to
369 // all observers.
370 static void ThreadSafeObserverHarness(int num_threads,
371 bool cross_thread_notifies) {
372 MessageLoop loop;
374 const int kMaxThreads = 15;
375 num_threads = num_threads > kMaxThreads ? kMaxThreads : num_threads;
377 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list(
378 new ObserverListThreadSafe<Foo>);
379 Adder a(1);
380 Adder b(-1);
381 Adder c(1);
382 Adder d(-1);
384 observer_list->AddObserver(&a);
385 observer_list->AddObserver(&b);
387 AddRemoveThread* threaded_observer[kMaxThreads];
388 base::PlatformThreadHandle threads[kMaxThreads];
389 for (int index = 0; index < num_threads; index++) {
390 threaded_observer[index] = new AddRemoveThread(observer_list.get(), false);
391 EXPECT_TRUE(PlatformThread::Create(0,
392 threaded_observer[index], &threads[index]));
395 Time start = Time::Now();
396 while (true) {
397 if ((Time::Now() - start).InMilliseconds() > kThreadRunTime)
398 break;
400 observer_list->Notify(&Foo::Observe, 10);
402 loop.RunUntilIdle();
405 for (int index = 0; index < num_threads; index++) {
406 threaded_observer[index]->Quit();
407 PlatformThread::Join(threads[index]);
411 TEST(ObserverListThreadSafeTest, CrossThreadObserver) {
412 // Use 7 observer threads. Notifications only come from
413 // the main thread.
414 ThreadSafeObserverHarness(7, false);
417 TEST(ObserverListThreadSafeTest, CrossThreadNotifications) {
418 // Use 3 observer threads. Notifications will fire from
419 // the main thread and all 3 observer threads.
420 ThreadSafeObserverHarness(3, true);
423 TEST(ObserverListThreadSafeTest, OutlivesMessageLoop) {
424 MessageLoop* loop = new MessageLoop;
425 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list(
426 new ObserverListThreadSafe<Foo>);
428 Adder a(1);
429 observer_list->AddObserver(&a);
430 delete loop;
431 // Test passes if we don't crash here.
432 observer_list->Notify(&Foo::Observe, 1);
435 TEST(ObserverListTest, Existing) {
436 ObserverList<Foo> observer_list(ObserverList<Foo>::NOTIFY_EXISTING_ONLY);
437 Adder a(1);
438 AddInObserve<ObserverList<Foo> > b(&observer_list);
440 observer_list.AddObserver(&a);
441 observer_list.AddObserver(&b);
443 FOR_EACH_OBSERVER(Foo, observer_list, Observe(1));
445 EXPECT_TRUE(b.added);
446 // B's adder should not have been notified because it was added during
447 // notificaiton.
448 EXPECT_EQ(0, b.adder.total);
450 // Notify again to make sure b's adder is notified.
451 FOR_EACH_OBSERVER(Foo, observer_list, Observe(1));
452 EXPECT_EQ(1, b.adder.total);
455 // Same as above, but for ObserverListThreadSafe
456 TEST(ObserverListThreadSafeTest, Existing) {
457 MessageLoop loop;
458 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list(
459 new ObserverListThreadSafe<Foo>(ObserverList<Foo>::NOTIFY_EXISTING_ONLY));
460 Adder a(1);
461 AddInObserve<ObserverListThreadSafe<Foo> > b(observer_list.get());
463 observer_list->AddObserver(&a);
464 observer_list->AddObserver(&b);
466 observer_list->Notify(&Foo::Observe, 1);
467 loop.RunUntilIdle();
469 EXPECT_TRUE(b.added);
470 // B's adder should not have been notified because it was added during
471 // notificaiton.
472 EXPECT_EQ(0, b.adder.total);
474 // Notify again to make sure b's adder is notified.
475 observer_list->Notify(&Foo::Observe, 1);
476 loop.RunUntilIdle();
477 EXPECT_EQ(1, b.adder.total);
480 class AddInClearObserve : public Foo {
481 public:
482 explicit AddInClearObserve(ObserverList<Foo>* list)
483 : list_(list), added_(false), adder_(1) {}
485 virtual void Observe(int /* x */) OVERRIDE {
486 list_->Clear();
487 list_->AddObserver(&adder_);
488 added_ = true;
491 bool added() const { return added_; }
492 const Adder& adder() const { return adder_; }
494 private:
495 ObserverList<Foo>* const list_;
497 bool added_;
498 Adder adder_;
501 TEST(ObserverListTest, ClearNotifyAll) {
502 ObserverList<Foo> observer_list;
503 AddInClearObserve a(&observer_list);
505 observer_list.AddObserver(&a);
507 FOR_EACH_OBSERVER(Foo, observer_list, Observe(1));
508 EXPECT_TRUE(a.added());
509 EXPECT_EQ(1, a.adder().total)
510 << "Adder should observe once and have sum of 1.";
513 TEST(ObserverListTest, ClearNotifyExistingOnly) {
514 ObserverList<Foo> observer_list(ObserverList<Foo>::NOTIFY_EXISTING_ONLY);
515 AddInClearObserve a(&observer_list);
517 observer_list.AddObserver(&a);
519 FOR_EACH_OBSERVER(Foo, observer_list, Observe(1));
520 EXPECT_TRUE(a.added());
521 EXPECT_EQ(0, a.adder().total)
522 << "Adder should not observe, so sum should still be 0.";
525 class ListDestructor : public Foo {
526 public:
527 explicit ListDestructor(ObserverList<Foo>* list) : list_(list) {}
528 virtual ~ListDestructor() {}
530 virtual void Observe(int x) OVERRIDE {
531 delete list_;
534 private:
535 ObserverList<Foo>* list_;
539 TEST(ObserverListTest, IteratorOutlivesList) {
540 ObserverList<Foo>* observer_list = new ObserverList<Foo>;
541 ListDestructor a(observer_list);
542 observer_list->AddObserver(&a);
544 FOR_EACH_OBSERVER(Foo, *observer_list, Observe(0));
545 // If this test fails, there'll be Valgrind errors when this function goes out
546 // of scope.
549 } // namespace