OLD | NEW |
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" |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 } | 86 } |
87 | 87 |
88 LifeCycleState life_cycle_state() const { return life_cycle_state_; } | 88 LifeCycleState life_cycle_state() const { return life_cycle_state_; } |
89 | 89 |
90 // Factory method for creating a new LifeCycleObject tied to this | 90 // Factory method for creating a new LifeCycleObject tied to this |
91 // LifeCycleWatcher. | 91 // LifeCycleWatcher. |
92 LifeCycleObject* NewLifeCycleObject() { | 92 LifeCycleObject* NewLifeCycleObject() { |
93 return new LifeCycleObject(this); | 93 return new LifeCycleObject(this); |
94 } | 94 } |
95 | 95 |
| 96 // Returns true iff |object| is the same object that this watcher is tracking. |
| 97 bool IsWatching(LifeCycleObject* object) const { |
| 98 return object == constructed_life_cycle_object_.get(); |
| 99 } |
| 100 |
96 private: | 101 private: |
97 LifeCycleState life_cycle_state_; | 102 LifeCycleState life_cycle_state_; |
98 scoped_ptr<LifeCycleObject> constructed_life_cycle_object_; | 103 scoped_ptr<LifeCycleObject> constructed_life_cycle_object_; |
99 | 104 |
100 DISALLOW_COPY_AND_ASSIGN(LifeCycleWatcher); | 105 DISALLOW_COPY_AND_ASSIGN(LifeCycleWatcher); |
101 }; | 106 }; |
102 | 107 |
103 TEST(ScopedVectorTest, LifeCycleWatcher) { | 108 TEST(ScopedVectorTest, LifeCycleWatcher) { |
104 LifeCycleWatcher watcher; | 109 LifeCycleWatcher watcher; |
105 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); | 110 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); |
106 LifeCycleObject* object = watcher.NewLifeCycleObject(); | 111 LifeCycleObject* object = watcher.NewLifeCycleObject(); |
107 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); | 112 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); |
108 delete object; | 113 delete object; |
109 EXPECT_EQ(LC_DESTROYED, watcher.life_cycle_state()); | 114 EXPECT_EQ(LC_DESTROYED, watcher.life_cycle_state()); |
110 } | 115 } |
111 | 116 |
112 TEST(ScopedVectorTest, Clear) { | 117 TEST(ScopedVectorTest, Clear) { |
113 LifeCycleWatcher watcher; | 118 LifeCycleWatcher watcher; |
114 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); | 119 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); |
115 ScopedVector<LifeCycleObject> scoped_vector; | 120 ScopedVector<LifeCycleObject> scoped_vector; |
116 scoped_vector.push_back(watcher.NewLifeCycleObject()); | 121 scoped_vector.push_back(watcher.NewLifeCycleObject()); |
117 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); | 122 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); |
| 123 EXPECT_TRUE(watcher.IsWatching(scoped_vector.back())); |
118 scoped_vector.clear(); | 124 scoped_vector.clear(); |
119 EXPECT_EQ(LC_DESTROYED, watcher.life_cycle_state()); | 125 EXPECT_EQ(LC_DESTROYED, watcher.life_cycle_state()); |
120 EXPECT_TRUE(scoped_vector.empty()); | 126 EXPECT_TRUE(scoped_vector.empty()); |
121 } | 127 } |
122 | 128 |
123 TEST(ScopedVectorTest, WeakClear) { | 129 TEST(ScopedVectorTest, WeakClear) { |
124 LifeCycleWatcher watcher; | 130 LifeCycleWatcher watcher; |
125 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); | 131 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); |
126 ScopedVector<LifeCycleObject> scoped_vector; | 132 ScopedVector<LifeCycleObject> scoped_vector; |
127 scoped_vector.push_back(watcher.NewLifeCycleObject()); | 133 scoped_vector.push_back(watcher.NewLifeCycleObject()); |
128 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); | 134 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); |
| 135 EXPECT_TRUE(watcher.IsWatching(scoped_vector.back())); |
129 scoped_vector.weak_clear(); | 136 scoped_vector.weak_clear(); |
130 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); | 137 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); |
131 EXPECT_TRUE(scoped_vector.empty()); | 138 EXPECT_TRUE(scoped_vector.empty()); |
132 } | 139 } |
133 | 140 |
134 TEST(ScopedVectorTest, ResizeShrink) { | 141 TEST(ScopedVectorTest, ResizeShrink) { |
135 LifeCycleWatcher first_watcher; | 142 LifeCycleWatcher first_watcher; |
136 EXPECT_EQ(LC_INITIAL, first_watcher.life_cycle_state()); | 143 EXPECT_EQ(LC_INITIAL, first_watcher.life_cycle_state()); |
137 LifeCycleWatcher second_watcher; | 144 LifeCycleWatcher second_watcher; |
138 EXPECT_EQ(LC_INITIAL, second_watcher.life_cycle_state()); | 145 EXPECT_EQ(LC_INITIAL, second_watcher.life_cycle_state()); |
139 ScopedVector<LifeCycleObject> scoped_vector; | 146 ScopedVector<LifeCycleObject> scoped_vector; |
140 | 147 |
141 scoped_vector.push_back(first_watcher.NewLifeCycleObject()); | 148 scoped_vector.push_back(first_watcher.NewLifeCycleObject()); |
142 EXPECT_EQ(LC_CONSTRUCTED, first_watcher.life_cycle_state()); | 149 EXPECT_EQ(LC_CONSTRUCTED, first_watcher.life_cycle_state()); |
143 EXPECT_EQ(LC_INITIAL, second_watcher.life_cycle_state()); | 150 EXPECT_EQ(LC_INITIAL, second_watcher.life_cycle_state()); |
| 151 EXPECT_TRUE(first_watcher.IsWatching(scoped_vector[0])); |
| 152 EXPECT_FALSE(second_watcher.IsWatching(scoped_vector[0])); |
144 | 153 |
145 scoped_vector.push_back(second_watcher.NewLifeCycleObject()); | 154 scoped_vector.push_back(second_watcher.NewLifeCycleObject()); |
146 EXPECT_EQ(LC_CONSTRUCTED, first_watcher.life_cycle_state()); | 155 EXPECT_EQ(LC_CONSTRUCTED, first_watcher.life_cycle_state()); |
147 EXPECT_EQ(LC_CONSTRUCTED, second_watcher.life_cycle_state()); | 156 EXPECT_EQ(LC_CONSTRUCTED, second_watcher.life_cycle_state()); |
| 157 EXPECT_FALSE(first_watcher.IsWatching(scoped_vector[1])); |
| 158 EXPECT_TRUE(second_watcher.IsWatching(scoped_vector[1])); |
148 | 159 |
149 // Test that shrinking a vector deletes elements in the dissapearing range. | 160 // Test that shrinking a vector deletes elements in the disappearing range. |
150 scoped_vector.resize(1); | 161 scoped_vector.resize(1); |
151 EXPECT_EQ(LC_CONSTRUCTED, first_watcher.life_cycle_state()); | 162 EXPECT_EQ(LC_CONSTRUCTED, first_watcher.life_cycle_state()); |
152 EXPECT_EQ(LC_DESTROYED, second_watcher.life_cycle_state()); | 163 EXPECT_EQ(LC_DESTROYED, second_watcher.life_cycle_state()); |
153 EXPECT_EQ(1u, scoped_vector.size()); | 164 EXPECT_EQ(1u, scoped_vector.size()); |
| 165 EXPECT_TRUE(first_watcher.IsWatching(scoped_vector[0])); |
154 } | 166 } |
155 | 167 |
156 TEST(ScopedVectorTest, ResizeGrow) { | 168 TEST(ScopedVectorTest, ResizeGrow) { |
157 LifeCycleWatcher watcher; | 169 LifeCycleWatcher watcher; |
158 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); | 170 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); |
159 ScopedVector<LifeCycleObject> scoped_vector; | 171 ScopedVector<LifeCycleObject> scoped_vector; |
160 scoped_vector.push_back(watcher.NewLifeCycleObject()); | 172 scoped_vector.push_back(watcher.NewLifeCycleObject()); |
161 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); | 173 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); |
| 174 EXPECT_TRUE(watcher.IsWatching(scoped_vector.back())); |
162 | 175 |
163 scoped_vector.resize(5); | 176 scoped_vector.resize(5); |
164 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); | 177 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); |
165 EXPECT_EQ(5u, scoped_vector.size()); | 178 ASSERT_EQ(5u, scoped_vector.size()); |
| 179 EXPECT_TRUE(watcher.IsWatching(scoped_vector[0])); |
| 180 EXPECT_FALSE(watcher.IsWatching(scoped_vector[1])); |
| 181 EXPECT_FALSE(watcher.IsWatching(scoped_vector[2])); |
| 182 EXPECT_FALSE(watcher.IsWatching(scoped_vector[3])); |
| 183 EXPECT_FALSE(watcher.IsWatching(scoped_vector[4])); |
166 } | 184 } |
167 | 185 |
168 TEST(ScopedVectorTest, Scope) { | 186 TEST(ScopedVectorTest, Scope) { |
169 LifeCycleWatcher watcher; | 187 LifeCycleWatcher watcher; |
170 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); | 188 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); |
171 { | 189 { |
172 ScopedVector<LifeCycleObject> scoped_vector; | 190 ScopedVector<LifeCycleObject> scoped_vector; |
173 scoped_vector.push_back(watcher.NewLifeCycleObject()); | 191 scoped_vector.push_back(watcher.NewLifeCycleObject()); |
174 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); | 192 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); |
| 193 EXPECT_TRUE(watcher.IsWatching(scoped_vector.back())); |
175 } | 194 } |
176 EXPECT_EQ(LC_DESTROYED, watcher.life_cycle_state()); | 195 EXPECT_EQ(LC_DESTROYED, watcher.life_cycle_state()); |
177 } | 196 } |
178 | 197 |
179 TEST(ScopedVectorTest, MoveConstruct) { | 198 TEST(ScopedVectorTest, MoveConstruct) { |
180 LifeCycleWatcher watcher; | 199 LifeCycleWatcher watcher; |
181 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); | 200 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); |
182 { | 201 { |
183 ScopedVector<LifeCycleObject> scoped_vector; | 202 ScopedVector<LifeCycleObject> scoped_vector; |
184 scoped_vector.push_back(watcher.NewLifeCycleObject()); | 203 scoped_vector.push_back(watcher.NewLifeCycleObject()); |
185 EXPECT_FALSE(scoped_vector.empty()); | 204 EXPECT_FALSE(scoped_vector.empty()); |
| 205 EXPECT_TRUE(watcher.IsWatching(scoped_vector.back())); |
186 | 206 |
187 ScopedVector<LifeCycleObject> scoped_vector_copy(scoped_vector.Pass()); | 207 ScopedVector<LifeCycleObject> scoped_vector_copy(scoped_vector.Pass()); |
188 EXPECT_TRUE(scoped_vector.empty()); | 208 EXPECT_TRUE(scoped_vector.empty()); |
189 EXPECT_FALSE(scoped_vector_copy.empty()); | 209 EXPECT_FALSE(scoped_vector_copy.empty()); |
| 210 EXPECT_TRUE(watcher.IsWatching(scoped_vector_copy.back())); |
190 | 211 |
191 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); | 212 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); |
192 } | 213 } |
193 EXPECT_EQ(LC_DESTROYED, watcher.life_cycle_state()); | 214 EXPECT_EQ(LC_DESTROYED, watcher.life_cycle_state()); |
194 } | 215 } |
195 | 216 |
196 TEST(ScopedVectorTest, MoveAssign) { | 217 TEST(ScopedVectorTest, MoveAssign) { |
197 LifeCycleWatcher watcher; | 218 LifeCycleWatcher watcher; |
198 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); | 219 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); |
199 { | 220 { |
200 ScopedVector<LifeCycleObject> scoped_vector; | 221 ScopedVector<LifeCycleObject> scoped_vector; |
201 scoped_vector.push_back(watcher.NewLifeCycleObject()); | 222 scoped_vector.push_back(watcher.NewLifeCycleObject()); |
202 ScopedVector<LifeCycleObject> scoped_vector_assign; | 223 ScopedVector<LifeCycleObject> scoped_vector_assign; |
203 EXPECT_FALSE(scoped_vector.empty()); | 224 EXPECT_FALSE(scoped_vector.empty()); |
| 225 EXPECT_TRUE(watcher.IsWatching(scoped_vector.back())); |
204 | 226 |
205 scoped_vector_assign = scoped_vector.Pass(); | 227 scoped_vector_assign = scoped_vector.Pass(); |
206 EXPECT_TRUE(scoped_vector.empty()); | 228 EXPECT_TRUE(scoped_vector.empty()); |
207 EXPECT_FALSE(scoped_vector_assign.empty()); | 229 EXPECT_FALSE(scoped_vector_assign.empty()); |
| 230 EXPECT_TRUE(watcher.IsWatching(scoped_vector_assign.back())); |
208 | 231 |
209 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); | 232 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); |
210 } | 233 } |
211 EXPECT_EQ(LC_DESTROYED, watcher.life_cycle_state()); | 234 EXPECT_EQ(LC_DESTROYED, watcher.life_cycle_state()); |
212 } | 235 } |
213 | 236 |
214 class DeleteCounter { | 237 class DeleteCounter { |
215 public: | 238 public: |
216 explicit DeleteCounter(int* deletes) | 239 explicit DeleteCounter(int* deletes) |
217 : deletes_(deletes) { | 240 : deletes_(deletes) { |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 for(LifeCycleWatcher* it = watchers; it != watchers + 1; ++it) | 292 for(LifeCycleWatcher* it = watchers; it != watchers + 1; ++it) |
270 EXPECT_EQ(LC_CONSTRUCTED, it->life_cycle_state()); | 293 EXPECT_EQ(LC_CONSTRUCTED, it->life_cycle_state()); |
271 for(LifeCycleWatcher* it = watchers + 1; it != watchers + 3; ++it) | 294 for(LifeCycleWatcher* it = watchers + 1; it != watchers + 3; ++it) |
272 EXPECT_EQ(LC_DESTROYED, it->life_cycle_state()); | 295 EXPECT_EQ(LC_DESTROYED, it->life_cycle_state()); |
273 for(LifeCycleWatcher* it = watchers + 3; it != watchers + arraysize(watchers); | 296 for(LifeCycleWatcher* it = watchers + 3; it != watchers + arraysize(watchers); |
274 ++it) | 297 ++it) |
275 EXPECT_EQ(LC_CONSTRUCTED, it->life_cycle_state()); | 298 EXPECT_EQ(LC_CONSTRUCTED, it->life_cycle_state()); |
276 } | 299 } |
277 | 300 |
278 } // namespace | 301 } // namespace |
OLD | NEW |