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

Side by Side Diff: base/observer_list_unittest.cc

Issue 668783004: Standardize usage of virtual/override/final in base/ (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Formatted 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 void Observe(int x) override { total += x * scaler_; }
30 total += x * scaler_; 30 ~Adder() override {}
31 }
32 virtual ~Adder() {}
33 int total; 31 int total;
34 32
35 private: 33 private:
36 int scaler_; 34 int scaler_;
37 }; 35 };
38 36
39 class Disrupter : public Foo { 37 class Disrupter : public Foo {
40 public: 38 public:
41 Disrupter(ObserverList<Foo>* list, Foo* doomed) 39 Disrupter(ObserverList<Foo>* list, Foo* doomed)
42 : list_(list), 40 : list_(list),
43 doomed_(doomed) { 41 doomed_(doomed) {
44 } 42 }
45 virtual ~Disrupter() {} 43 ~Disrupter() override {}
46 virtual void Observe(int x) override { 44 void Observe(int x) override { list_->RemoveObserver(doomed_); }
47 list_->RemoveObserver(doomed_);
48 }
49 45
50 private: 46 private:
51 ObserverList<Foo>* list_; 47 ObserverList<Foo>* list_;
52 Foo* doomed_; 48 Foo* doomed_;
53 }; 49 };
54 50
55 class ThreadSafeDisrupter : public Foo { 51 class ThreadSafeDisrupter : public Foo {
56 public: 52 public:
57 ThreadSafeDisrupter(ObserverListThreadSafe<Foo>* list, Foo* doomed) 53 ThreadSafeDisrupter(ObserverListThreadSafe<Foo>* list, Foo* doomed)
58 : list_(list), 54 : list_(list),
59 doomed_(doomed) { 55 doomed_(doomed) {
60 } 56 }
61 virtual ~ThreadSafeDisrupter() {} 57 ~ThreadSafeDisrupter() override {}
62 virtual void Observe(int x) override { 58 void Observe(int x) override { list_->RemoveObserver(doomed_); }
63 list_->RemoveObserver(doomed_);
64 }
65 59
66 private: 60 private:
67 ObserverListThreadSafe<Foo>* list_; 61 ObserverListThreadSafe<Foo>* list_;
68 Foo* doomed_; 62 Foo* doomed_;
69 }; 63 };
70 64
71 template <typename ObserverListType> 65 template <typename ObserverListType>
72 class AddInObserve : public Foo { 66 class AddInObserve : public Foo {
73 public: 67 public:
74 explicit AddInObserve(ObserverListType* observer_list) 68 explicit AddInObserve(ObserverListType* observer_list)
(...skipping 27 matching lines...) Expand all
102 : list_(list), 96 : list_(list),
103 loop_(NULL), 97 loop_(NULL),
104 in_list_(false), 98 in_list_(false),
105 start_(Time::Now()), 99 start_(Time::Now()),
106 count_observes_(0), 100 count_observes_(0),
107 count_addtask_(0), 101 count_addtask_(0),
108 do_notifies_(notify), 102 do_notifies_(notify),
109 weak_factory_(this) { 103 weak_factory_(this) {
110 } 104 }
111 105
112 virtual ~AddRemoveThread() { 106 ~AddRemoveThread() override {}
113 }
114 107
115 virtual void ThreadMain() override { 108 void ThreadMain() override {
116 loop_ = new MessageLoop(); // Fire up a message loop. 109 loop_ = new MessageLoop(); // Fire up a message loop.
117 loop_->PostTask( 110 loop_->PostTask(
118 FROM_HERE, 111 FROM_HERE,
119 base::Bind(&AddRemoveThread::AddTask, weak_factory_.GetWeakPtr())); 112 base::Bind(&AddRemoveThread::AddTask, weak_factory_.GetWeakPtr()));
120 loop_->Run(); 113 loop_->Run();
121 //LOG(ERROR) << "Loop 0x" << std::hex << loop_ << " done. " << 114 //LOG(ERROR) << "Loop 0x" << std::hex << loop_ << " done. " <<
122 // count_observes_ << ", " << count_addtask_; 115 // count_observes_ << ", " << count_addtask_;
123 delete loop_; 116 delete loop_;
124 loop_ = reinterpret_cast<MessageLoop*>(0xdeadbeef); 117 loop_ = reinterpret_cast<MessageLoop*>(0xdeadbeef);
125 delete this; 118 delete this;
(...skipping 20 matching lines...) Expand all
146 139
147 loop_->PostTask( 140 loop_->PostTask(
148 FROM_HERE, 141 FROM_HERE,
149 base::Bind(&AddRemoveThread::AddTask, weak_factory_.GetWeakPtr())); 142 base::Bind(&AddRemoveThread::AddTask, weak_factory_.GetWeakPtr()));
150 } 143 }
151 144
152 void Quit() { 145 void Quit() {
153 loop_->PostTask(FROM_HERE, MessageLoop::QuitWhenIdleClosure()); 146 loop_->PostTask(FROM_HERE, MessageLoop::QuitWhenIdleClosure());
154 } 147 }
155 148
156 virtual void Observe(int x) override { 149 void Observe(int x) override {
157 count_observes_++; 150 count_observes_++;
158 151
159 // If we're getting called after we removed ourselves from 152 // If we're getting called after we removed ourselves from
160 // the list, that is very bad! 153 // the list, that is very bad!
161 DCHECK(in_list_); 154 DCHECK(in_list_);
162 155
163 // This callback should fire on the appropriate thread 156 // This callback should fire on the appropriate thread
164 EXPECT_EQ(loop_, MessageLoop::current()); 157 EXPECT_EQ(loop_, MessageLoop::current());
165 158
166 list_->RemoveObserver(this); 159 list_->RemoveObserver(this);
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 RunLoop().RunUntilIdle(); 309 RunLoop().RunUntilIdle();
317 310
318 EXPECT_EQ(20, a.total); 311 EXPECT_EQ(20, a.total);
319 EXPECT_EQ(30, b.total); 312 EXPECT_EQ(30, b.total);
320 EXPECT_EQ(10, c.total); 313 EXPECT_EQ(10, c.total);
321 } 314 }
322 315
323 class FooRemover : public Foo { 316 class FooRemover : public Foo {
324 public: 317 public:
325 explicit FooRemover(ObserverListThreadSafe<Foo>* list) : list_(list) {} 318 explicit FooRemover(ObserverListThreadSafe<Foo>* list) : list_(list) {}
326 virtual ~FooRemover() {} 319 ~FooRemover() override {}
327 320
328 void AddFooToRemove(Foo* foo) { 321 void AddFooToRemove(Foo* foo) {
329 foos_.push_back(foo); 322 foos_.push_back(foo);
330 } 323 }
331 324
332 virtual void Observe(int x) override { 325 void Observe(int x) override {
333 std::vector<Foo*> tmp; 326 std::vector<Foo*> tmp;
334 tmp.swap(foos_); 327 tmp.swap(foos_);
335 for (std::vector<Foo*>::iterator it = tmp.begin(); 328 for (std::vector<Foo*>::iterator it = tmp.begin();
336 it != tmp.end(); ++it) { 329 it != tmp.end(); ++it) {
337 list_->RemoveObserver(*it); 330 list_->RemoveObserver(*it);
338 } 331 }
339 } 332 }
340 333
341 private: 334 private:
342 const scoped_refptr<ObserverListThreadSafe<Foo> > list_; 335 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); 467 observer_list->Notify(&Foo::Observe, 1);
475 RunLoop().RunUntilIdle(); 468 RunLoop().RunUntilIdle();
476 EXPECT_EQ(1, b.adder.total); 469 EXPECT_EQ(1, b.adder.total);
477 } 470 }
478 471
479 class AddInClearObserve : public Foo { 472 class AddInClearObserve : public Foo {
480 public: 473 public:
481 explicit AddInClearObserve(ObserverList<Foo>* list) 474 explicit AddInClearObserve(ObserverList<Foo>* list)
482 : list_(list), added_(false), adder_(1) {} 475 : list_(list), added_(false), adder_(1) {}
483 476
484 virtual void Observe(int /* x */) override { 477 void Observe(int /* x */) override {
485 list_->Clear(); 478 list_->Clear();
486 list_->AddObserver(&adder_); 479 list_->AddObserver(&adder_);
487 added_ = true; 480 added_ = true;
488 } 481 }
489 482
490 bool added() const { return added_; } 483 bool added() const { return added_; }
491 const Adder& adder() const { return adder_; } 484 const Adder& adder() const { return adder_; }
492 485
493 private: 486 private:
494 ObserverList<Foo>* const list_; 487 ObserverList<Foo>* const list_;
(...skipping 22 matching lines...) Expand all
517 510
518 FOR_EACH_OBSERVER(Foo, observer_list, Observe(1)); 511 FOR_EACH_OBSERVER(Foo, observer_list, Observe(1));
519 EXPECT_TRUE(a.added()); 512 EXPECT_TRUE(a.added());
520 EXPECT_EQ(0, a.adder().total) 513 EXPECT_EQ(0, a.adder().total)
521 << "Adder should not observe, so sum should still be 0."; 514 << "Adder should not observe, so sum should still be 0.";
522 } 515 }
523 516
524 class ListDestructor : public Foo { 517 class ListDestructor : public Foo {
525 public: 518 public:
526 explicit ListDestructor(ObserverList<Foo>* list) : list_(list) {} 519 explicit ListDestructor(ObserverList<Foo>* list) : list_(list) {}
527 virtual ~ListDestructor() {} 520 ~ListDestructor() override {}
528 521
529 virtual void Observe(int x) override { 522 void Observe(int x) override { delete list_; }
530 delete list_;
531 }
532 523
533 private: 524 private:
534 ObserverList<Foo>* list_; 525 ObserverList<Foo>* list_;
535 }; 526 };
536 527
537 528
538 TEST(ObserverListTest, IteratorOutlivesList) { 529 TEST(ObserverListTest, IteratorOutlivesList) {
539 ObserverList<Foo>* observer_list = new ObserverList<Foo>; 530 ObserverList<Foo>* observer_list = new ObserverList<Foo>;
540 ListDestructor a(observer_list); 531 ListDestructor a(observer_list);
541 observer_list->AddObserver(&a); 532 observer_list->AddObserver(&a);
542 533
543 FOR_EACH_OBSERVER(Foo, *observer_list, Observe(0)); 534 FOR_EACH_OBSERVER(Foo, *observer_list, Observe(0));
544 // If this test fails, there'll be Valgrind errors when this function goes out 535 // If this test fails, there'll be Valgrind errors when this function goes out
545 // of scope. 536 // of scope.
546 } 537 }
547 538
548 } // namespace 539 } // namespace
549 } // namespace base 540 } // 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