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

Side by Side Diff: base/observer_list_unittest.cc

Issue 611153004: replace OVERRIDE and FINAL with override and final in base/ (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: CC_ -> BASE_ Created 6 years, 2 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
« no previous file with comments | « base/metrics/stats_table_unittest.cc ('k') | base/posix/file_descriptor_shuffle.h » ('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) 2012 The Chromium Authors. All rights reserved. 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 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/message_loop.h" 12 #include "base/message_loop/message_loop.h"
13 #include "base/run_loop.h" 13 #include "base/run_loop.h"
14 #include "base/threading/platform_thread.h" 14 #include "base/threading/platform_thread.h"
15 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
16 16
17 namespace base { 17 namespace base {
18 namespace { 18 namespace {
19 19
20 class Foo { 20 class Foo {
21 public: 21 public:
22 virtual void Observe(int x) = 0; 22 virtual void Observe(int x) = 0;
23 virtual ~Foo() {} 23 virtual ~Foo() {}
24 }; 24 };
25 25
26 class Adder : public Foo { 26 class Adder : public Foo {
27 public: 27 public:
28 explicit Adder(int scaler) : total(0), scaler_(scaler) {} 28 explicit Adder(int scaler) : total(0), scaler_(scaler) {}
29 virtual void Observe(int x) OVERRIDE { 29 virtual void Observe(int x) override {
30 total += x * scaler_; 30 total += x * scaler_;
31 } 31 }
32 virtual ~Adder() {} 32 virtual ~Adder() {}
33 int total; 33 int total;
34 34
35 private: 35 private:
36 int scaler_; 36 int scaler_;
37 }; 37 };
38 38
39 class Disrupter : public Foo { 39 class Disrupter : public Foo {
40 public: 40 public:
41 Disrupter(ObserverList<Foo>* list, Foo* doomed) 41 Disrupter(ObserverList<Foo>* list, Foo* doomed)
42 : list_(list), 42 : list_(list),
43 doomed_(doomed) { 43 doomed_(doomed) {
44 } 44 }
45 virtual ~Disrupter() {} 45 virtual ~Disrupter() {}
46 virtual void Observe(int x) OVERRIDE { 46 virtual void Observe(int x) override {
47 list_->RemoveObserver(doomed_); 47 list_->RemoveObserver(doomed_);
48 } 48 }
49 49
50 private: 50 private:
51 ObserverList<Foo>* list_; 51 ObserverList<Foo>* list_;
52 Foo* doomed_; 52 Foo* doomed_;
53 }; 53 };
54 54
55 class ThreadSafeDisrupter : public Foo { 55 class ThreadSafeDisrupter : public Foo {
56 public: 56 public:
57 ThreadSafeDisrupter(ObserverListThreadSafe<Foo>* list, Foo* doomed) 57 ThreadSafeDisrupter(ObserverListThreadSafe<Foo>* list, Foo* doomed)
58 : list_(list), 58 : list_(list),
59 doomed_(doomed) { 59 doomed_(doomed) {
60 } 60 }
61 virtual ~ThreadSafeDisrupter() {} 61 virtual ~ThreadSafeDisrupter() {}
62 virtual void Observe(int x) OVERRIDE { 62 virtual void Observe(int x) override {
63 list_->RemoveObserver(doomed_); 63 list_->RemoveObserver(doomed_);
64 } 64 }
65 65
66 private: 66 private:
67 ObserverListThreadSafe<Foo>* list_; 67 ObserverListThreadSafe<Foo>* list_;
68 Foo* doomed_; 68 Foo* doomed_;
69 }; 69 };
70 70
71 template <typename ObserverListType> 71 template <typename ObserverListType>
72 class AddInObserve : public Foo { 72 class AddInObserve : public Foo {
73 public: 73 public:
74 explicit AddInObserve(ObserverListType* observer_list) 74 explicit AddInObserve(ObserverListType* observer_list)
75 : added(false), 75 : added(false),
76 observer_list(observer_list), 76 observer_list(observer_list),
77 adder(1) { 77 adder(1) {
78 } 78 }
79 79
80 virtual void Observe(int x) OVERRIDE { 80 virtual void Observe(int x) override {
81 if (!added) { 81 if (!added) {
82 added = true; 82 added = true;
83 observer_list->AddObserver(&adder); 83 observer_list->AddObserver(&adder);
84 } 84 }
85 } 85 }
86 86
87 bool added; 87 bool added;
88 ObserverListType* observer_list; 88 ObserverListType* observer_list;
89 Adder adder; 89 Adder adder;
90 }; 90 };
(...skipping 14 matching lines...) Expand all
105 start_(Time::Now()), 105 start_(Time::Now()),
106 count_observes_(0), 106 count_observes_(0),
107 count_addtask_(0), 107 count_addtask_(0),
108 do_notifies_(notify), 108 do_notifies_(notify),
109 weak_factory_(this) { 109 weak_factory_(this) {
110 } 110 }
111 111
112 virtual ~AddRemoveThread() { 112 virtual ~AddRemoveThread() {
113 } 113 }
114 114
115 virtual void ThreadMain() OVERRIDE { 115 virtual void ThreadMain() override {
116 loop_ = new MessageLoop(); // Fire up a message loop. 116 loop_ = new MessageLoop(); // Fire up a message loop.
117 loop_->PostTask( 117 loop_->PostTask(
118 FROM_HERE, 118 FROM_HERE,
119 base::Bind(&AddRemoveThread::AddTask, weak_factory_.GetWeakPtr())); 119 base::Bind(&AddRemoveThread::AddTask, weak_factory_.GetWeakPtr()));
120 loop_->Run(); 120 loop_->Run();
121 //LOG(ERROR) << "Loop 0x" << std::hex << loop_ << " done. " << 121 //LOG(ERROR) << "Loop 0x" << std::hex << loop_ << " done. " <<
122 // count_observes_ << ", " << count_addtask_; 122 // count_observes_ << ", " << count_addtask_;
123 delete loop_; 123 delete loop_;
124 loop_ = reinterpret_cast<MessageLoop*>(0xdeadbeef); 124 loop_ = reinterpret_cast<MessageLoop*>(0xdeadbeef);
125 delete this; 125 delete this;
(...skipping 20 matching lines...) Expand all
146 146
147 loop_->PostTask( 147 loop_->PostTask(
148 FROM_HERE, 148 FROM_HERE,
149 base::Bind(&AddRemoveThread::AddTask, weak_factory_.GetWeakPtr())); 149 base::Bind(&AddRemoveThread::AddTask, weak_factory_.GetWeakPtr()));
150 } 150 }
151 151
152 void Quit() { 152 void Quit() {
153 loop_->PostTask(FROM_HERE, MessageLoop::QuitWhenIdleClosure()); 153 loop_->PostTask(FROM_HERE, MessageLoop::QuitWhenIdleClosure());
154 } 154 }
155 155
156 virtual void Observe(int x) OVERRIDE { 156 virtual void Observe(int x) override {
157 count_observes_++; 157 count_observes_++;
158 158
159 // If we're getting called after we removed ourselves from 159 // If we're getting called after we removed ourselves from
160 // the list, that is very bad! 160 // the list, that is very bad!
161 DCHECK(in_list_); 161 DCHECK(in_list_);
162 162
163 // This callback should fire on the appropriate thread 163 // This callback should fire on the appropriate thread
164 EXPECT_EQ(loop_, MessageLoop::current()); 164 EXPECT_EQ(loop_, MessageLoop::current());
165 165
166 list_->RemoveObserver(this); 166 list_->RemoveObserver(this);
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 322
323 class FooRemover : public Foo { 323 class FooRemover : public Foo {
324 public: 324 public:
325 explicit FooRemover(ObserverListThreadSafe<Foo>* list) : list_(list) {} 325 explicit FooRemover(ObserverListThreadSafe<Foo>* list) : list_(list) {}
326 virtual ~FooRemover() {} 326 virtual ~FooRemover() {}
327 327
328 void AddFooToRemove(Foo* foo) { 328 void AddFooToRemove(Foo* foo) {
329 foos_.push_back(foo); 329 foos_.push_back(foo);
330 } 330 }
331 331
332 virtual void Observe(int x) OVERRIDE { 332 virtual void Observe(int x) override {
333 std::vector<Foo*> tmp; 333 std::vector<Foo*> tmp;
334 tmp.swap(foos_); 334 tmp.swap(foos_);
335 for (std::vector<Foo*>::iterator it = tmp.begin(); 335 for (std::vector<Foo*>::iterator it = tmp.begin();
336 it != tmp.end(); ++it) { 336 it != tmp.end(); ++it) {
337 list_->RemoveObserver(*it); 337 list_->RemoveObserver(*it);
338 } 338 }
339 } 339 }
340 340
341 private: 341 private:
342 const scoped_refptr<ObserverListThreadSafe<Foo> > list_; 342 const scoped_refptr<ObserverListThreadSafe<Foo> > list_;
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
474 observer_list->Notify(&Foo::Observe, 1); 474 observer_list->Notify(&Foo::Observe, 1);
475 RunLoop().RunUntilIdle(); 475 RunLoop().RunUntilIdle();
476 EXPECT_EQ(1, b.adder.total); 476 EXPECT_EQ(1, b.adder.total);
477 } 477 }
478 478
479 class AddInClearObserve : public Foo { 479 class AddInClearObserve : public Foo {
480 public: 480 public:
481 explicit AddInClearObserve(ObserverList<Foo>* list) 481 explicit AddInClearObserve(ObserverList<Foo>* list)
482 : list_(list), added_(false), adder_(1) {} 482 : list_(list), added_(false), adder_(1) {}
483 483
484 virtual void Observe(int /* x */) OVERRIDE { 484 virtual void Observe(int /* x */) override {
485 list_->Clear(); 485 list_->Clear();
486 list_->AddObserver(&adder_); 486 list_->AddObserver(&adder_);
487 added_ = true; 487 added_ = true;
488 } 488 }
489 489
490 bool added() const { return added_; } 490 bool added() const { return added_; }
491 const Adder& adder() const { return adder_; } 491 const Adder& adder() const { return adder_; }
492 492
493 private: 493 private:
494 ObserverList<Foo>* const list_; 494 ObserverList<Foo>* const list_;
(...skipping 24 matching lines...) Expand all
519 EXPECT_TRUE(a.added()); 519 EXPECT_TRUE(a.added());
520 EXPECT_EQ(0, a.adder().total) 520 EXPECT_EQ(0, a.adder().total)
521 << "Adder should not observe, so sum should still be 0."; 521 << "Adder should not observe, so sum should still be 0.";
522 } 522 }
523 523
524 class ListDestructor : public Foo { 524 class ListDestructor : public Foo {
525 public: 525 public:
526 explicit ListDestructor(ObserverList<Foo>* list) : list_(list) {} 526 explicit ListDestructor(ObserverList<Foo>* list) : list_(list) {}
527 virtual ~ListDestructor() {} 527 virtual ~ListDestructor() {}
528 528
529 virtual void Observe(int x) OVERRIDE { 529 virtual void Observe(int x) override {
530 delete list_; 530 delete list_;
531 } 531 }
532 532
533 private: 533 private:
534 ObserverList<Foo>* list_; 534 ObserverList<Foo>* list_;
535 }; 535 };
536 536
537 537
538 TEST(ObserverListTest, IteratorOutlivesList) { 538 TEST(ObserverListTest, IteratorOutlivesList) {
539 ObserverList<Foo>* observer_list = new ObserverList<Foo>; 539 ObserverList<Foo>* observer_list = new ObserverList<Foo>;
540 ListDestructor a(observer_list); 540 ListDestructor a(observer_list);
541 observer_list->AddObserver(&a); 541 observer_list->AddObserver(&a);
542 542
543 FOR_EACH_OBSERVER(Foo, *observer_list, Observe(0)); 543 FOR_EACH_OBSERVER(Foo, *observer_list, Observe(0));
544 // If this test fails, there'll be Valgrind errors when this function goes out 544 // If this test fails, there'll be Valgrind errors when this function goes out
545 // of scope. 545 // of scope.
546 } 546 }
547 547
548 } // namespace 548 } // namespace
549 } // namespace base 549 } // namespace base
OLDNEW
« no previous file with comments | « base/metrics/stats_table_unittest.cc ('k') | base/posix/file_descriptor_shuffle.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698