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

Side by Side Diff: base/memory/scoped_vector_unittest.cc

Issue 11416166: Clean up ScopedVectorTest's LifeCycleObject. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: ... lose the casts, make LifeCycleObject more private. Created 8 years 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 | « no previous file | no next file » | 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/memory/scoped_vector.h" 5 #include "base/memory/scoped_vector.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/callback.h" 8 #include "base/callback.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
11 11
12 namespace { 12 namespace {
13 13
14 // The LifeCycleObject notifies its Observer upon construction & destruction. 14 // The LifeCycleObject notifies its Observer upon construction & destruction.
15 class LifeCycleObject { 15 class LifeCycleObject {
16 public: 16 public:
17 ~LifeCycleObject() {
18 observer_->OnLifeCycleDestroy(this);
19 }
20
21 private:
22 friend class LifeCycleWatcher;
23
17 class Observer { 24 class Observer {
18 public: 25 public:
19 virtual void OnLifeCycleConstruct(LifeCycleObject* o) = 0; 26 virtual void OnLifeCycleConstruct(LifeCycleObject* o) = 0;
20 virtual void OnLifeCycleDestroy(LifeCycleObject* o) = 0; 27 virtual void OnLifeCycleDestroy(LifeCycleObject* o) = 0;
21 28
22 protected: 29 protected:
23 virtual ~Observer() {} 30 virtual ~Observer() {}
24 }; 31 };
25 32
26 explicit LifeCycleObject(Observer* observer) 33 explicit LifeCycleObject(Observer* observer)
27 : observer_(observer) { 34 : observer_(observer) {
28 observer_->OnLifeCycleConstruct(this); 35 observer_->OnLifeCycleConstruct(this);
29 } 36 }
30 37
31 ~LifeCycleObject() {
32 observer_->OnLifeCycleDestroy(this);
33 }
34
35 private:
36 Observer* observer_; 38 Observer* observer_;
37 39
38 DISALLOW_COPY_AND_ASSIGN(LifeCycleObject); 40 DISALLOW_COPY_AND_ASSIGN(LifeCycleObject);
39 }; 41 };
40 42
41 // The life cycle states we care about for the purposes of testing ScopedVector 43 // The life cycle states we care about for the purposes of testing ScopedVector
42 // against objects. 44 // against objects.
43 enum LifeCycleState { 45 enum LifeCycleState {
44 LC_INITIAL, 46 LC_INITIAL,
45 LC_CONSTRUCTED, 47 LC_CONSTRUCTED,
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
108 } 110 }
109 111
110 TEST(ScopedVectorTest, Clear) { 112 TEST(ScopedVectorTest, Clear) {
111 LifeCycleWatcher watcher; 113 LifeCycleWatcher watcher;
112 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); 114 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state());
113 ScopedVector<LifeCycleObject> scoped_vector; 115 ScopedVector<LifeCycleObject> scoped_vector;
114 scoped_vector.push_back(watcher.NewLifeCycleObject()); 116 scoped_vector.push_back(watcher.NewLifeCycleObject());
115 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); 117 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state());
116 scoped_vector.clear(); 118 scoped_vector.clear();
117 EXPECT_EQ(LC_DESTROYED, watcher.life_cycle_state()); 119 EXPECT_EQ(LC_DESTROYED, watcher.life_cycle_state());
118 EXPECT_EQ(static_cast<size_t>(0), scoped_vector.size()); 120 EXPECT_TRUE(scoped_vector.empty());
gavinp 2012/11/24 14:27:18 And actually, no need for any cast at all here.
119 } 121 }
120 122
121 TEST(ScopedVectorTest, WeakClear) { 123 TEST(ScopedVectorTest, WeakClear) {
122 LifeCycleWatcher watcher; 124 LifeCycleWatcher watcher;
123 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); 125 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state());
124 ScopedVector<LifeCycleObject> scoped_vector; 126 ScopedVector<LifeCycleObject> scoped_vector;
125 scoped_ptr<LifeCycleObject> object(watcher.NewLifeCycleObject()); 127 scoped_vector.push_back(watcher.NewLifeCycleObject());
126 scoped_vector.push_back(object.get());
127 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); 128 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state());
128 scoped_vector.weak_clear(); 129 scoped_vector.weak_clear();
129 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); 130 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state());
130 EXPECT_EQ(static_cast<size_t>(0), scoped_vector.size()); 131 EXPECT_TRUE(scoped_vector.empty());
131 } 132 }
132 133
133 TEST(ScopedVectorTest, Scope) { 134 TEST(ScopedVectorTest, Scope) {
134 LifeCycleWatcher watcher; 135 LifeCycleWatcher watcher;
135 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); 136 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state());
136 { 137 {
137 ScopedVector<LifeCycleObject> scoped_vector; 138 ScopedVector<LifeCycleObject> scoped_vector;
138 scoped_vector.push_back(watcher.NewLifeCycleObject()); 139 scoped_vector.push_back(watcher.NewLifeCycleObject());
139 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); 140 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state());
140 } 141 }
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
234 for(LifeCycleWatcher* it = watchers; it != watchers + 1; ++it) 235 for(LifeCycleWatcher* it = watchers; it != watchers + 1; ++it)
235 EXPECT_EQ(LC_CONSTRUCTED, it->life_cycle_state()); 236 EXPECT_EQ(LC_CONSTRUCTED, it->life_cycle_state());
236 for(LifeCycleWatcher* it = watchers + 1; it != watchers + 3; ++it) 237 for(LifeCycleWatcher* it = watchers + 1; it != watchers + 3; ++it)
237 EXPECT_EQ(LC_DESTROYED, it->life_cycle_state()); 238 EXPECT_EQ(LC_DESTROYED, it->life_cycle_state());
238 for(LifeCycleWatcher* it = watchers + 3; it != watchers + arraysize(watchers); 239 for(LifeCycleWatcher* it = watchers + 3; it != watchers + arraysize(watchers);
239 ++it) 240 ++it)
240 EXPECT_EQ(LC_CONSTRUCTED, it->life_cycle_state()); 241 EXPECT_EQ(LC_CONSTRUCTED, it->life_cycle_state());
241 } 242 }
242 243
243 } // namespace 244 } // namespace
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698