Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(587)

Side by Side Diff: base/observer_list_unittest.cc

Issue 10004001: Add virtual and OVERRIDE to base/ implementation files (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Win Fix -> Missing header Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/metrics/stats_table_unittest.cc ('k') | base/pr_time_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/observer_list.h" 5 #include "base/observer_list.h"
6 #include "base/observer_list_threadsafe.h" 6 #include "base/observer_list_threadsafe.h"
7 7
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/compiler_specific.h" 10 #include "base/compiler_specific.h"
11 #include "base/memory/weak_ptr.h" 11 #include "base/memory/weak_ptr.h"
12 #include "base/message_loop.h" 12 #include "base/message_loop.h"
13 #include "base/threading/platform_thread.h" 13 #include "base/threading/platform_thread.h"
14 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
15 15
16 using base::PlatformThread; 16 using base::PlatformThread;
17 using base::Time; 17 using base::Time;
18 18
19 namespace { 19 namespace {
20 20
21 class Foo { 21 class Foo {
22 public: 22 public:
23 virtual void Observe(int x) = 0; 23 virtual void Observe(int x) = 0;
24 virtual ~Foo() {} 24 virtual ~Foo() {}
25 }; 25 };
26 26
27 class Adder : public Foo { 27 class Adder : public Foo {
28 public: 28 public:
29 explicit Adder(int scaler) : total(0), scaler_(scaler) {} 29 explicit Adder(int scaler) : total(0), scaler_(scaler) {}
30 virtual void Observe(int x) { 30 virtual void Observe(int x) OVERRIDE {
31 total += x * scaler_; 31 total += x * scaler_;
32 } 32 }
33 virtual ~Adder() { } 33 virtual ~Adder() {}
34 int total; 34 int total;
35
35 private: 36 private:
36 int scaler_; 37 int scaler_;
37 }; 38 };
38 39
39 class Disrupter : public Foo { 40 class Disrupter : public Foo {
40 public: 41 public:
41 Disrupter(ObserverList<Foo>* list, Foo* doomed) 42 Disrupter(ObserverList<Foo>* list, Foo* doomed)
42 : list_(list), doomed_(doomed) { } 43 : list_(list),
43 virtual ~Disrupter() { } 44 doomed_(doomed) {
44 virtual void Observe(int x) { 45 }
46 virtual ~Disrupter() {}
47 virtual void Observe(int x) OVERRIDE {
45 list_->RemoveObserver(doomed_); 48 list_->RemoveObserver(doomed_);
46 } 49 }
50
47 private: 51 private:
48 ObserverList<Foo>* list_; 52 ObserverList<Foo>* list_;
49 Foo* doomed_; 53 Foo* doomed_;
50 }; 54 };
51 55
52 class ThreadSafeDisrupter : public Foo { 56 class ThreadSafeDisrupter : public Foo {
53 public: 57 public:
54 ThreadSafeDisrupter(ObserverListThreadSafe<Foo>* list, Foo* doomed) 58 ThreadSafeDisrupter(ObserverListThreadSafe<Foo>* list, Foo* doomed)
55 : list_(list), doomed_(doomed) { } 59 : list_(list),
56 virtual ~ThreadSafeDisrupter() { } 60 doomed_(doomed) {
57 virtual void Observe(int x) { 61 }
62 virtual ~ThreadSafeDisrupter() {}
63 virtual void Observe(int x) OVERRIDE {
58 list_->RemoveObserver(doomed_); 64 list_->RemoveObserver(doomed_);
59 } 65 }
66
60 private: 67 private:
61 ObserverListThreadSafe<Foo>* list_; 68 ObserverListThreadSafe<Foo>* list_;
62 Foo* doomed_; 69 Foo* doomed_;
63 }; 70 };
64 71
65 template <typename ObserverListType> 72 template <typename ObserverListType>
66 class AddInObserve : public Foo { 73 class AddInObserve : public Foo {
67 public: 74 public:
68 explicit AddInObserve(ObserverListType* observer_list) 75 explicit AddInObserve(ObserverListType* observer_list)
69 : added(false), 76 : added(false),
70 observer_list(observer_list), 77 observer_list(observer_list),
71 adder(1) { 78 adder(1) {
72 } 79 }
73 virtual void Observe(int x) { 80
81 virtual void Observe(int x) OVERRIDE {
74 if (!added) { 82 if (!added) {
75 added = true; 83 added = true;
76 observer_list->AddObserver(&adder); 84 observer_list->AddObserver(&adder);
77 } 85 }
78 } 86 }
79 87
80 bool added; 88 bool added;
81 ObserverListType* observer_list; 89 ObserverListType* observer_list;
82 Adder adder; 90 Adder adder;
83 }; 91 };
(...skipping 13 matching lines...) Expand all
97 start_(Time::Now()), 105 start_(Time::Now()),
98 count_observes_(0), 106 count_observes_(0),
99 count_addtask_(0), 107 count_addtask_(0),
100 do_notifies_(notify), 108 do_notifies_(notify),
101 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { 109 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
102 } 110 }
103 111
104 virtual ~AddRemoveThread() { 112 virtual ~AddRemoveThread() {
105 } 113 }
106 114
107 void ThreadMain() { 115 virtual void ThreadMain() OVERRIDE {
108 loop_ = new MessageLoop(); // Fire up a message loop. 116 loop_ = new MessageLoop(); // Fire up a message loop.
109 loop_->PostTask( 117 loop_->PostTask(
110 FROM_HERE, 118 FROM_HERE,
111 base::Bind(&AddRemoveThread::AddTask, weak_factory_.GetWeakPtr())); 119 base::Bind(&AddRemoveThread::AddTask, weak_factory_.GetWeakPtr()));
112 loop_->Run(); 120 loop_->Run();
113 //LOG(ERROR) << "Loop 0x" << std::hex << loop_ << " done. " << 121 //LOG(ERROR) << "Loop 0x" << std::hex << loop_ << " done. " <<
114 // count_observes_ << ", " << count_addtask_; 122 // count_observes_ << ", " << count_addtask_;
115 delete loop_; 123 delete loop_;
116 loop_ = reinterpret_cast<MessageLoop*>(0xdeadbeef); 124 loop_ = reinterpret_cast<MessageLoop*>(0xdeadbeef);
117 delete this; 125 delete this;
(...skipping 20 matching lines...) Expand all
138 146
139 loop_->PostTask( 147 loop_->PostTask(
140 FROM_HERE, 148 FROM_HERE,
141 base::Bind(&AddRemoveThread::AddTask, weak_factory_.GetWeakPtr())); 149 base::Bind(&AddRemoveThread::AddTask, weak_factory_.GetWeakPtr()));
142 } 150 }
143 151
144 void Quit() { 152 void Quit() {
145 loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); 153 loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure());
146 } 154 }
147 155
148 virtual void Observe(int x) { 156 virtual void Observe(int x) OVERRIDE {
149 count_observes_++; 157 count_observes_++;
150 158
151 // If we're getting called after we removed ourselves from 159 // If we're getting called after we removed ourselves from
152 // the list, that is very bad! 160 // the list, that is very bad!
153 DCHECK(in_list_); 161 DCHECK(in_list_);
154 162
155 // This callback should fire on the appropriate thread 163 // This callback should fire on the appropriate thread
156 EXPECT_EQ(loop_, MessageLoop::current()); 164 EXPECT_EQ(loop_, MessageLoop::current());
157 165
158 list_->RemoveObserver(this); 166 list_->RemoveObserver(this);
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
311 319
312 class FooRemover : public Foo { 320 class FooRemover : public Foo {
313 public: 321 public:
314 explicit FooRemover(ObserverListThreadSafe<Foo>* list) : list_(list) {} 322 explicit FooRemover(ObserverListThreadSafe<Foo>* list) : list_(list) {}
315 virtual ~FooRemover() {} 323 virtual ~FooRemover() {}
316 324
317 void AddFooToRemove(Foo* foo) { 325 void AddFooToRemove(Foo* foo) {
318 foos_.push_back(foo); 326 foos_.push_back(foo);
319 } 327 }
320 328
321 virtual void Observe(int x) { 329 virtual void Observe(int x) OVERRIDE {
322 std::vector<Foo*> tmp; 330 std::vector<Foo*> tmp;
323 tmp.swap(foos_); 331 tmp.swap(foos_);
324 for (std::vector<Foo*>::iterator it = tmp.begin(); 332 for (std::vector<Foo*>::iterator it = tmp.begin();
325 it != tmp.end(); ++it) { 333 it != tmp.end(); ++it) {
326 list_->RemoveObserver(*it); 334 list_->RemoveObserver(*it);
327 } 335 }
328 } 336 }
329 337
330 private: 338 private:
331 const scoped_refptr<ObserverListThreadSafe<Foo> > list_; 339 const scoped_refptr<ObserverListThreadSafe<Foo> > list_;
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
463 observer_list->Notify(&Foo::Observe, 1); 471 observer_list->Notify(&Foo::Observe, 1);
464 loop.RunAllPending(); 472 loop.RunAllPending();
465 EXPECT_EQ(1, b.adder.total); 473 EXPECT_EQ(1, b.adder.total);
466 } 474 }
467 475
468 class AddInClearObserve : public Foo { 476 class AddInClearObserve : public Foo {
469 public: 477 public:
470 explicit AddInClearObserve(ObserverList<Foo>* list) 478 explicit AddInClearObserve(ObserverList<Foo>* list)
471 : list_(list), added_(false), adder_(1) {} 479 : list_(list), added_(false), adder_(1) {}
472 480
473 virtual void Observe(int /* x */) { 481 virtual void Observe(int /* x */) OVERRIDE {
474 list_->Clear(); 482 list_->Clear();
475 list_->AddObserver(&adder_); 483 list_->AddObserver(&adder_);
476 added_ = true; 484 added_ = true;
477 } 485 }
478 486
479 bool added() const { return added_; } 487 bool added() const { return added_; }
480 const Adder& adder() const { return adder_; } 488 const Adder& adder() const { return adder_; }
481 489
482 private: 490 private:
483 ObserverList<Foo>* const list_; 491 ObserverList<Foo>* const list_;
(...skipping 22 matching lines...) Expand all
506 514
507 FOR_EACH_OBSERVER(Foo, observer_list, Observe(1)); 515 FOR_EACH_OBSERVER(Foo, observer_list, Observe(1));
508 EXPECT_TRUE(a.added()); 516 EXPECT_TRUE(a.added());
509 EXPECT_EQ(0, a.adder().total) 517 EXPECT_EQ(0, a.adder().total)
510 << "Adder should not observe, so sum should still be 0."; 518 << "Adder should not observe, so sum should still be 0.";
511 } 519 }
512 520
513 class ListDestructor : public Foo { 521 class ListDestructor : public Foo {
514 public: 522 public:
515 explicit ListDestructor(ObserverList<Foo>* list) : list_(list) {} 523 explicit ListDestructor(ObserverList<Foo>* list) : list_(list) {}
516 virtual void Observe(int x) { 524 virtual ~ListDestructor() {}
525
526 virtual void Observe(int x) OVERRIDE {
517 delete list_; 527 delete list_;
518 } 528 }
519 virtual ~ListDestructor() { }
520 int total; 529 int total;
521 private: 530 private:
522 ObserverList<Foo>* list_; 531 ObserverList<Foo>* list_;
523 }; 532 };
524 533
525 534
526 TEST(ObserverListTest, IteratorOutlivesList) { 535 TEST(ObserverListTest, IteratorOutlivesList) {
527 ObserverList<Foo>* observer_list = new ObserverList<Foo>; 536 ObserverList<Foo>* observer_list = new ObserverList<Foo>;
528 ListDestructor a(observer_list); 537 ListDestructor a(observer_list);
529 observer_list->AddObserver(&a); 538 observer_list->AddObserver(&a);
530 539
531 FOR_EACH_OBSERVER(Foo, *observer_list, Observe(0)); 540 FOR_EACH_OBSERVER(Foo, *observer_list, Observe(0));
532 // If this test fails, there'll be Valgrind errors when this function goes out 541 // If this test fails, there'll be Valgrind errors when this function goes out
533 // of scope. 542 // of scope.
534 } 543 }
535 544
536 } // namespace 545 } // namespace
OLDNEW
« no previous file with comments | « base/metrics/stats_table_unittest.cc ('k') | base/pr_time_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698