Remove connection_history.js from remoting's JS tests.
[chromium-blink-merge.git] / base / observer_list_unittest.cc
blob57843f401fc0ec984a8ee16a5764e9fca4ad5fa3
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 virtual void Observe(int x) OVERRIDE {
30 total += x * scaler_;
32 virtual ~Adder() {}
33 int total;
35 private:
36 int scaler_;
39 class Disrupter : public Foo {
40 public:
41 Disrupter(ObserverList<Foo>* list, Foo* doomed)
42 : list_(list),
43 doomed_(doomed) {
45 virtual ~Disrupter() {}
46 virtual void Observe(int x) OVERRIDE {
47 list_->RemoveObserver(doomed_);
50 private:
51 ObserverList<Foo>* list_;
52 Foo* doomed_;
55 class ThreadSafeDisrupter : public Foo {
56 public:
57 ThreadSafeDisrupter(ObserverListThreadSafe<Foo>* list, Foo* doomed)
58 : list_(list),
59 doomed_(doomed) {
61 virtual ~ThreadSafeDisrupter() {}
62 virtual void Observe(int x) OVERRIDE {
63 list_->RemoveObserver(doomed_);
66 private:
67 ObserverListThreadSafe<Foo>* list_;
68 Foo* doomed_;
71 template <typename ObserverListType>
72 class AddInObserve : public Foo {
73 public:
74 explicit AddInObserve(ObserverListType* observer_list)
75 : added(false),
76 observer_list(observer_list),
77 adder(1) {
80 virtual void Observe(int x) OVERRIDE {
81 if (!added) {
82 added = true;
83 observer_list->AddObserver(&adder);
87 bool added;
88 ObserverListType* observer_list;
89 Adder adder;
93 static const int kThreadRunTime = 2000; // ms to run the multi-threaded test.
95 // A thread for use in the ThreadSafeObserver test
96 // which will add and remove itself from the notification
97 // list repeatedly.
98 class AddRemoveThread : public PlatformThread::Delegate,
99 public Foo {
100 public:
101 AddRemoveThread(ObserverListThreadSafe<Foo>* list, bool notify)
102 : list_(list),
103 loop_(NULL),
104 in_list_(false),
105 start_(Time::Now()),
106 count_observes_(0),
107 count_addtask_(0),
108 do_notifies_(notify),
109 weak_factory_(this) {
112 virtual ~AddRemoveThread() {
115 virtual void ThreadMain() OVERRIDE {
116 loop_ = new MessageLoop(); // Fire up a message loop.
117 loop_->PostTask(
118 FROM_HERE,
119 base::Bind(&AddRemoveThread::AddTask, weak_factory_.GetWeakPtr()));
120 loop_->Run();
121 //LOG(ERROR) << "Loop 0x" << std::hex << loop_ << " done. " <<
122 // count_observes_ << ", " << count_addtask_;
123 delete loop_;
124 loop_ = reinterpret_cast<MessageLoop*>(0xdeadbeef);
125 delete this;
128 // This task just keeps posting to itself in an attempt
129 // to race with the notifier.
130 void AddTask() {
131 count_addtask_++;
133 if ((Time::Now() - start_).InMilliseconds() > kThreadRunTime) {
134 VLOG(1) << "DONE!";
135 return;
138 if (!in_list_) {
139 list_->AddObserver(this);
140 in_list_ = true;
143 if (do_notifies_) {
144 list_->Notify(&Foo::Observe, 10);
147 loop_->PostTask(
148 FROM_HERE,
149 base::Bind(&AddRemoveThread::AddTask, weak_factory_.GetWeakPtr()));
152 void Quit() {
153 loop_->PostTask(FROM_HERE, MessageLoop::QuitWhenIdleClosure());
156 virtual void Observe(int x) OVERRIDE {
157 count_observes_++;
159 // If we're getting called after we removed ourselves from
160 // the list, that is very bad!
161 DCHECK(in_list_);
163 // This callback should fire on the appropriate thread
164 EXPECT_EQ(loop_, MessageLoop::current());
166 list_->RemoveObserver(this);
167 in_list_ = false;
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.
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.
181 base::WeakPtrFactory<AddRemoveThread> weak_factory_;
184 TEST(ObserverListTest, BasicTest) {
185 ObserverList<Foo> observer_list;
186 Adder a(1), b(-1), c(1), d(-1), e(-1);
187 Disrupter evil(&observer_list, &c);
189 observer_list.AddObserver(&a);
190 observer_list.AddObserver(&b);
192 FOR_EACH_OBSERVER(Foo, observer_list, Observe(10));
194 observer_list.AddObserver(&evil);
195 observer_list.AddObserver(&c);
196 observer_list.AddObserver(&d);
198 // Removing an observer not in the list should do nothing.
199 observer_list.RemoveObserver(&e);
201 FOR_EACH_OBSERVER(Foo, observer_list, Observe(10));
203 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);
210 TEST(ObserverListThreadSafeTest, BasicTest) {
211 MessageLoop loop;
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);
221 observer_list->AddObserver(&a);
222 observer_list->AddObserver(&b);
224 observer_list->Notify(&Foo::Observe, 10);
225 RunLoop().RunUntilIdle();
227 observer_list->AddObserver(&evil);
228 observer_list->AddObserver(&c);
229 observer_list->AddObserver(&d);
231 observer_list->Notify(&Foo::Observe, 10);
232 RunLoop().RunUntilIdle();
234 EXPECT_EQ(20, a.total);
235 EXPECT_EQ(-20, b.total);
236 EXPECT_EQ(0, c.total);
237 EXPECT_EQ(-10, d.total);
240 TEST(ObserverListThreadSafeTest, RemoveObserver) {
241 MessageLoop loop;
243 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list(
244 new ObserverListThreadSafe<Foo>);
245 Adder a(1), b(1);
247 // A workaround for the compiler bug. See http://crbug.com/121960.
248 EXPECT_NE(&a, &b);
250 // Should do nothing.
251 observer_list->RemoveObserver(&a);
252 observer_list->RemoveObserver(&b);
254 observer_list->Notify(&Foo::Observe, 10);
255 RunLoop().RunUntilIdle();
257 EXPECT_EQ(0, a.total);
258 EXPECT_EQ(0, b.total);
260 observer_list->AddObserver(&a);
262 // Should also do nothing.
263 observer_list->RemoveObserver(&b);
265 observer_list->Notify(&Foo::Observe, 10);
266 RunLoop().RunUntilIdle();
268 EXPECT_EQ(10, a.total);
269 EXPECT_EQ(0, b.total);
272 TEST(ObserverListThreadSafeTest, WithoutMessageLoop) {
273 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list(
274 new ObserverListThreadSafe<Foo>);
276 Adder a(1), b(1), c(1);
278 // No MessageLoop, so these should not be added.
279 observer_list->AddObserver(&a);
280 observer_list->AddObserver(&b);
283 // Add c when there's a loop.
284 MessageLoop loop;
285 observer_list->AddObserver(&c);
287 observer_list->Notify(&Foo::Observe, 10);
288 RunLoop().RunUntilIdle();
290 EXPECT_EQ(0, a.total);
291 EXPECT_EQ(0, b.total);
292 EXPECT_EQ(10, c.total);
294 // Now add a when there's a loop.
295 observer_list->AddObserver(&a);
297 // Remove c when there's a loop.
298 observer_list->RemoveObserver(&c);
300 // Notify again.
301 observer_list->Notify(&Foo::Observe, 20);
302 RunLoop().RunUntilIdle();
304 EXPECT_EQ(20, a.total);
305 EXPECT_EQ(0, b.total);
306 EXPECT_EQ(10, c.total);
309 // Removing should always succeed with or without a loop.
310 observer_list->RemoveObserver(&a);
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);
316 RunLoop().RunUntilIdle();
318 EXPECT_EQ(20, a.total);
319 EXPECT_EQ(30, b.total);
320 EXPECT_EQ(10, c.total);
323 class FooRemover : public Foo {
324 public:
325 explicit FooRemover(ObserverListThreadSafe<Foo>* list) : list_(list) {}
326 virtual ~FooRemover() {}
328 void AddFooToRemove(Foo* foo) {
329 foos_.push_back(foo);
332 virtual void Observe(int x) OVERRIDE {
333 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);
341 private:
342 const scoped_refptr<ObserverListThreadSafe<Foo> > list_;
343 std::vector<Foo*> foos_;
346 TEST(ObserverListThreadSafeTest, RemoveMultipleObservers) {
347 MessageLoop loop;
348 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list(
349 new ObserverListThreadSafe<Foo>);
351 FooRemover a(observer_list.get());
352 Adder b(1);
354 observer_list->AddObserver(&a);
355 observer_list->AddObserver(&b);
357 a.AddFooToRemove(&a);
358 a.AddFooToRemove(&b);
360 observer_list->Notify(&Foo::Observe, 1);
361 RunLoop().RunUntilIdle();
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.
369 static void ThreadSafeObserverHarness(int num_threads,
370 bool cross_thread_notifies) {
371 MessageLoop loop;
373 const int kMaxThreads = 15;
374 num_threads = num_threads > kMaxThreads ? kMaxThreads : num_threads;
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);
383 observer_list->AddObserver(&a);
384 observer_list->AddObserver(&b);
386 AddRemoveThread* threaded_observer[kMaxThreads];
387 base::PlatformThreadHandle threads[kMaxThreads];
388 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]));
394 Time start = Time::Now();
395 while (true) {
396 if ((Time::Now() - start).InMilliseconds() > kThreadRunTime)
397 break;
399 observer_list->Notify(&Foo::Observe, 10);
401 RunLoop().RunUntilIdle();
404 for (int index = 0; index < num_threads; index++) {
405 threaded_observer[index]->Quit();
406 PlatformThread::Join(threads[index]);
410 TEST(ObserverListThreadSafeTest, CrossThreadObserver) {
411 // Use 7 observer threads. Notifications only come from
412 // the main thread.
413 ThreadSafeObserverHarness(7, false);
416 TEST(ObserverListThreadSafeTest, CrossThreadNotifications) {
417 // Use 3 observer threads. Notifications will fire from
418 // the main thread and all 3 observer threads.
419 ThreadSafeObserverHarness(3, true);
422 TEST(ObserverListThreadSafeTest, OutlivesMessageLoop) {
423 MessageLoop* loop = new MessageLoop;
424 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list(
425 new ObserverListThreadSafe<Foo>);
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);
434 TEST(ObserverListTest, Existing) {
435 ObserverList<Foo> observer_list(ObserverList<Foo>::NOTIFY_EXISTING_ONLY);
436 Adder a(1);
437 AddInObserve<ObserverList<Foo> > b(&observer_list);
439 observer_list.AddObserver(&a);
440 observer_list.AddObserver(&b);
442 FOR_EACH_OBSERVER(Foo, observer_list, Observe(1));
444 EXPECT_TRUE(b.added);
445 // B's adder should not have been notified because it was added during
446 // notificaiton.
447 EXPECT_EQ(0, b.adder.total);
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);
454 // Same as above, but for ObserverListThreadSafe
455 TEST(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());
462 observer_list->AddObserver(&a);
463 observer_list->AddObserver(&b);
465 observer_list->Notify(&Foo::Observe, 1);
466 RunLoop().RunUntilIdle();
468 EXPECT_TRUE(b.added);
469 // B's adder should not have been notified because it was added during
470 // notificaiton.
471 EXPECT_EQ(0, b.adder.total);
473 // Notify again to make sure b's adder is notified.
474 observer_list->Notify(&Foo::Observe, 1);
475 RunLoop().RunUntilIdle();
476 EXPECT_EQ(1, b.adder.total);
479 class AddInClearObserve : public Foo {
480 public:
481 explicit AddInClearObserve(ObserverList<Foo>* list)
482 : list_(list), added_(false), adder_(1) {}
484 virtual void Observe(int /* x */) OVERRIDE {
485 list_->Clear();
486 list_->AddObserver(&adder_);
487 added_ = true;
490 bool added() const { return added_; }
491 const Adder& adder() const { return adder_; }
493 private:
494 ObserverList<Foo>* const list_;
496 bool added_;
497 Adder adder_;
500 TEST(ObserverListTest, ClearNotifyAll) {
501 ObserverList<Foo> observer_list;
502 AddInClearObserve a(&observer_list);
504 observer_list.AddObserver(&a);
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.";
512 TEST(ObserverListTest, ClearNotifyExistingOnly) {
513 ObserverList<Foo> observer_list(ObserverList<Foo>::NOTIFY_EXISTING_ONLY);
514 AddInClearObserve a(&observer_list);
516 observer_list.AddObserver(&a);
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.";
524 class ListDestructor : public Foo {
525 public:
526 explicit ListDestructor(ObserverList<Foo>* list) : list_(list) {}
527 virtual ~ListDestructor() {}
529 virtual void Observe(int x) OVERRIDE {
530 delete list_;
533 private:
534 ObserverList<Foo>* list_;
538 TEST(ObserverListTest, IteratorOutlivesList) {
539 ObserverList<Foo>* observer_list = new ObserverList<Foo>;
540 ListDestructor a(observer_list);
541 observer_list->AddObserver(&a);
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.
548 } // namespace
549 } // namespace base