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

Side by Side Diff: chromecast/base/component/component_unittest.cc

Issue 1875623002: Convert //chromecast from scoped_ptr to std::unique_ptr (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 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
« no previous file with comments | « chromecast/base/cast_sys_info_util_simple.cc ('k') | chromecast/base/device_capabilities.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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "chromecast/base/component/component.h" 5 #include "chromecast/base/component/component.h"
6 6
7 #include <memory>
8
7 #include "base/location.h" 9 #include "base/location.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h" 11 #include "base/run_loop.h"
11 #include "base/thread_task_runner_handle.h" 12 #include "base/thread_task_runner_handle.h"
12 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
13 14
14 namespace chromecast { 15 namespace chromecast {
15 16
16 class ComponentTest : public ::testing::Test { 17 class ComponentTest : public ::testing::Test {
17 protected: 18 protected:
18 ComponentTest() : message_loop_(new base::MessageLoop()) {} 19 ComponentTest() : message_loop_(new base::MessageLoop()) {}
19 20
20 const scoped_ptr<base::MessageLoop> message_loop_; 21 const std::unique_ptr<base::MessageLoop> message_loop_;
21 }; 22 };
22 23
23 using ComponentDeathTest = ComponentTest; 24 using ComponentDeathTest = ComponentTest;
24 25
25 class ComponentB; 26 class ComponentB;
26 class ComponentC; 27 class ComponentC;
27 class ComponentA : public Component<ComponentA> { 28 class ComponentA : public Component<ComponentA> {
28 public: 29 public:
29 void MakeSelfDependency() { 30 void MakeSelfDependency() {
30 a_.reset(new Component<ComponentA>::Dependency(GetRef(), this)); 31 a_.reset(new Component<ComponentA>::Dependency(GetRef(), this));
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 EXPECT_FALSE(fail_enable_); 64 EXPECT_FALSE(fail_enable_);
64 } 65 }
65 66
66 bool enabled() const { return enabled_; } 67 bool enabled() const { return enabled_; }
67 void FailEnable() { fail_enable_ = true; } 68 void FailEnable() { fail_enable_ = true; }
68 69
69 private: 70 private:
70 bool enabled_ = false; 71 bool enabled_ = false;
71 bool fail_enable_ = false; 72 bool fail_enable_ = false;
72 73
73 scoped_ptr<Component<ComponentA>::Dependency> a_; 74 std::unique_ptr<Component<ComponentA>::Dependency> a_;
74 scoped_ptr<Component<ComponentB>::Dependency> b_; 75 std::unique_ptr<Component<ComponentB>::Dependency> b_;
75 scoped_ptr<Component<ComponentC>::Dependency> c_; 76 std::unique_ptr<Component<ComponentC>::Dependency> c_;
76 }; 77 };
77 78
78 class ComponentB : public Component<ComponentB> { 79 class ComponentB : public Component<ComponentB> {
79 public: 80 public:
80 explicit ComponentB(const ComponentA::WeakRef& a) : a_(a, this) {} 81 explicit ComponentB(const ComponentA::WeakRef& a) : a_(a, this) {}
81 82
82 void OnEnable() override { 83 void OnEnable() override {
83 if (!fail_enable_) { 84 if (!fail_enable_) {
84 enabled_ = true; 85 enabled_ = true;
85 Test(); 86 Test();
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 ComponentA a; 173 ComponentA a;
173 ComponentB b(a.GetRef()); 174 ComponentB b(a.GetRef());
174 ComponentC c(b.GetRef()); 175 ComponentC c(b.GetRef());
175 EXPECT_DEATH(a.MakeTransitiveCircularDependency(c.GetRef()), 176 EXPECT_DEATH(a.MakeTransitiveCircularDependency(c.GetRef()),
176 "Circular dependency"); 177 "Circular dependency");
177 } 178 }
178 #endif // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && 179 #endif // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) &&
179 // GTEST_HAS_DEATH_TEST 180 // GTEST_HAS_DEATH_TEST
180 181
181 TEST_F(ComponentTest, SimpleEnable) { 182 TEST_F(ComponentTest, SimpleEnable) {
182 scoped_ptr<ComponentA> a(new ComponentA()); 183 std::unique_ptr<ComponentA> a(new ComponentA());
183 a->Enable(); 184 a->Enable();
184 message_loop_->RunUntilIdle(); 185 message_loop_->RunUntilIdle();
185 EXPECT_TRUE(a->enabled()); 186 EXPECT_TRUE(a->enabled());
186 a.release()->Destroy(); 187 a.release()->Destroy();
187 } 188 }
188 189
189 TEST_F(ComponentTest, TransitiveEnable) { 190 TEST_F(ComponentTest, TransitiveEnable) {
190 scoped_ptr<ComponentA> a(new ComponentA()); 191 std::unique_ptr<ComponentA> a(new ComponentA());
191 scoped_ptr<ComponentB> b(new ComponentB(a->GetRef())); 192 std::unique_ptr<ComponentB> b(new ComponentB(a->GetRef()));
192 scoped_ptr<ComponentC> c(new ComponentC(b->GetRef())); 193 std::unique_ptr<ComponentC> c(new ComponentC(b->GetRef()));
193 c->Enable(); 194 c->Enable();
194 message_loop_->RunUntilIdle(); 195 message_loop_->RunUntilIdle();
195 EXPECT_TRUE(a->enabled()); 196 EXPECT_TRUE(a->enabled());
196 EXPECT_TRUE(b->enabled()); 197 EXPECT_TRUE(b->enabled());
197 EXPECT_TRUE(c->enabled()); 198 EXPECT_TRUE(c->enabled());
198 a.release()->Destroy(); 199 a.release()->Destroy();
199 b.release()->Destroy(); 200 b.release()->Destroy();
200 c.release()->Destroy(); 201 c.release()->Destroy();
201 } 202 }
202 203
203 TEST_F(ComponentTest, FailEnable) { 204 TEST_F(ComponentTest, FailEnable) {
204 scoped_ptr<ComponentA> a(new ComponentA()); 205 std::unique_ptr<ComponentA> a(new ComponentA());
205 a->FailEnable(); 206 a->FailEnable();
206 a->Enable(); 207 a->Enable();
207 message_loop_->RunUntilIdle(); 208 message_loop_->RunUntilIdle();
208 EXPECT_FALSE(a->enabled()); 209 EXPECT_FALSE(a->enabled());
209 a.release()->Destroy(); 210 a.release()->Destroy();
210 } 211 }
211 212
212 TEST_F(ComponentTest, TransitiveFailEnable) { 213 TEST_F(ComponentTest, TransitiveFailEnable) {
213 scoped_ptr<ComponentA> a(new ComponentA()); 214 std::unique_ptr<ComponentA> a(new ComponentA());
214 scoped_ptr<ComponentB> b(new ComponentB(a->GetRef())); 215 std::unique_ptr<ComponentB> b(new ComponentB(a->GetRef()));
215 scoped_ptr<ComponentC> c(new ComponentC(b->GetRef())); 216 std::unique_ptr<ComponentC> c(new ComponentC(b->GetRef()));
216 a->FailEnable(); 217 a->FailEnable();
217 c->Enable(); 218 c->Enable();
218 message_loop_->RunUntilIdle(); 219 message_loop_->RunUntilIdle();
219 EXPECT_FALSE(a->enabled()); 220 EXPECT_FALSE(a->enabled());
220 EXPECT_FALSE(b->enabled()); 221 EXPECT_FALSE(b->enabled());
221 EXPECT_FALSE(c->enabled()); 222 EXPECT_FALSE(c->enabled());
222 a.release()->Destroy(); 223 a.release()->Destroy();
223 b.release()->Destroy(); 224 b.release()->Destroy();
224 c.release()->Destroy(); 225 c.release()->Destroy();
225 } 226 }
226 227
227 TEST_F(ComponentTest, DisableWhileEnabling) { 228 TEST_F(ComponentTest, DisableWhileEnabling) {
228 scoped_ptr<ComponentA> a(new ComponentA()); 229 std::unique_ptr<ComponentA> a(new ComponentA());
229 a->Enable(); 230 a->Enable();
230 a->Disable(); 231 a->Disable();
231 message_loop_->RunUntilIdle(); 232 message_loop_->RunUntilIdle();
232 EXPECT_FALSE(a->enabled()); 233 EXPECT_FALSE(a->enabled());
233 a.release()->Destroy(); 234 a.release()->Destroy();
234 } 235 }
235 236
236 TEST_F(ComponentTest, EnableTwice) { 237 TEST_F(ComponentTest, EnableTwice) {
237 scoped_ptr<ComponentA> a(new ComponentA()); 238 std::unique_ptr<ComponentA> a(new ComponentA());
238 a->Enable(); 239 a->Enable();
239 a->Enable(); 240 a->Enable();
240 message_loop_->RunUntilIdle(); 241 message_loop_->RunUntilIdle();
241 EXPECT_TRUE(a->enabled()); 242 EXPECT_TRUE(a->enabled());
242 a.release()->Destroy(); 243 a.release()->Destroy();
243 } 244 }
244 245
245 TEST_F(ComponentTest, DisableTwice) { 246 TEST_F(ComponentTest, DisableTwice) {
246 scoped_ptr<ComponentA> a(new ComponentA()); 247 std::unique_ptr<ComponentA> a(new ComponentA());
247 a->Enable(); 248 a->Enable();
248 message_loop_->RunUntilIdle(); 249 message_loop_->RunUntilIdle();
249 EXPECT_TRUE(a->enabled()); 250 EXPECT_TRUE(a->enabled());
250 a->Disable(); 251 a->Disable();
251 message_loop_->RunUntilIdle(); 252 message_loop_->RunUntilIdle();
252 EXPECT_FALSE(a->enabled()); 253 EXPECT_FALSE(a->enabled());
253 a->Disable(); 254 a->Disable();
254 message_loop_->RunUntilIdle(); 255 message_loop_->RunUntilIdle();
255 EXPECT_FALSE(a->enabled()); 256 EXPECT_FALSE(a->enabled());
256 a.release()->Destroy(); 257 a.release()->Destroy();
257 } 258 }
258 259
259 TEST_F(ComponentTest, DisableAfterFailedEnable) { 260 TEST_F(ComponentTest, DisableAfterFailedEnable) {
260 scoped_ptr<ComponentA> a(new ComponentA()); 261 std::unique_ptr<ComponentA> a(new ComponentA());
261 a->FailEnable(); 262 a->FailEnable();
262 a->Enable(); 263 a->Enable();
263 message_loop_->RunUntilIdle(); 264 message_loop_->RunUntilIdle();
264 EXPECT_FALSE(a->enabled()); 265 EXPECT_FALSE(a->enabled());
265 a->Disable(); 266 a->Disable();
266 message_loop_->RunUntilIdle(); 267 message_loop_->RunUntilIdle();
267 EXPECT_FALSE(a->enabled()); 268 EXPECT_FALSE(a->enabled());
268 a.release()->Destroy(); 269 a.release()->Destroy();
269 } 270 }
270 271
271 TEST_F(ComponentTest, DisableAfterNeverEnabled) { 272 TEST_F(ComponentTest, DisableAfterNeverEnabled) {
272 scoped_ptr<ComponentA> a(new ComponentA()); 273 std::unique_ptr<ComponentA> a(new ComponentA());
273 a->Disable(); 274 a->Disable();
274 message_loop_->RunUntilIdle(); 275 message_loop_->RunUntilIdle();
275 EXPECT_FALSE(a->enabled()); 276 EXPECT_FALSE(a->enabled());
276 a.release()->Destroy(); 277 a.release()->Destroy();
277 } 278 }
278 279
279 TEST_F(ComponentTest, DisableDependencyWhileEnabling) { 280 TEST_F(ComponentTest, DisableDependencyWhileEnabling) {
280 scoped_ptr<ComponentA> a(new ComponentA()); 281 std::unique_ptr<ComponentA> a(new ComponentA());
281 scoped_ptr<ComponentB> b(new ComponentB(a->GetRef())); 282 std::unique_ptr<ComponentB> b(new ComponentB(a->GetRef()));
282 scoped_ptr<ComponentC> c(new ComponentC(b->GetRef())); 283 std::unique_ptr<ComponentC> c(new ComponentC(b->GetRef()));
283 b->Enable(); 284 b->Enable();
284 message_loop_->RunUntilIdle(); 285 message_loop_->RunUntilIdle();
285 c->Enable(); 286 c->Enable();
286 a->Disable(); 287 a->Disable();
287 message_loop_->RunUntilIdle(); 288 message_loop_->RunUntilIdle();
288 EXPECT_FALSE(a->enabled()); 289 EXPECT_FALSE(a->enabled());
289 EXPECT_FALSE(b->enabled()); 290 EXPECT_FALSE(b->enabled());
290 EXPECT_FALSE(c->enabled()); 291 EXPECT_FALSE(c->enabled());
291 a.release()->Destroy(); 292 a.release()->Destroy();
292 b.release()->Destroy(); 293 b.release()->Destroy();
293 c.release()->Destroy(); 294 c.release()->Destroy();
294 } 295 }
295 296
296 TEST_F(ComponentTest, EnableDisableEnable) { 297 TEST_F(ComponentTest, EnableDisableEnable) {
297 scoped_ptr<ComponentA> a(new ComponentA()); 298 std::unique_ptr<ComponentA> a(new ComponentA());
298 a->Enable(); 299 a->Enable();
299 a->Disable(); 300 a->Disable();
300 a->Enable(); 301 a->Enable();
301 message_loop_->RunUntilIdle(); 302 message_loop_->RunUntilIdle();
302 EXPECT_TRUE(a->enabled()); 303 EXPECT_TRUE(a->enabled());
303 a.release()->Destroy(); 304 a.release()->Destroy();
304 } 305 }
305 306
306 TEST_F(ComponentTest, DisableEnableDisable) { 307 TEST_F(ComponentTest, DisableEnableDisable) {
307 scoped_ptr<ComponentA> a(new ComponentA()); 308 std::unique_ptr<ComponentA> a(new ComponentA());
308 a->Enable(); 309 a->Enable();
309 message_loop_->RunUntilIdle(); 310 message_loop_->RunUntilIdle();
310 EXPECT_TRUE(a->enabled()); 311 EXPECT_TRUE(a->enabled());
311 a->Disable(); 312 a->Disable();
312 a->Enable(); 313 a->Enable();
313 a->Disable(); 314 a->Disable();
314 message_loop_->RunUntilIdle(); 315 message_loop_->RunUntilIdle();
315 EXPECT_FALSE(a->enabled()); 316 EXPECT_FALSE(a->enabled());
316 a.release()->Destroy(); 317 a.release()->Destroy();
317 } 318 }
318 319
319 TEST_F(ComponentTest, TransitiveEnableDisableEnable) { 320 TEST_F(ComponentTest, TransitiveEnableDisableEnable) {
320 scoped_ptr<ComponentA> a(new ComponentA()); 321 std::unique_ptr<ComponentA> a(new ComponentA());
321 scoped_ptr<ComponentB> b(new ComponentB(a->GetRef())); 322 std::unique_ptr<ComponentB> b(new ComponentB(a->GetRef()));
322 scoped_ptr<ComponentC> c(new ComponentC(b->GetRef())); 323 std::unique_ptr<ComponentC> c(new ComponentC(b->GetRef()));
323 a->Enable(); 324 a->Enable();
324 message_loop_->RunUntilIdle(); 325 message_loop_->RunUntilIdle();
325 c->Enable(); 326 c->Enable();
326 a->Disable(); 327 a->Disable();
327 message_loop_->RunUntilIdle(); 328 message_loop_->RunUntilIdle();
328 EXPECT_FALSE(a->enabled()); 329 EXPECT_FALSE(a->enabled());
329 EXPECT_FALSE(b->enabled()); 330 EXPECT_FALSE(b->enabled());
330 EXPECT_FALSE(c->enabled()); 331 EXPECT_FALSE(c->enabled());
331 c->Enable(); 332 c->Enable();
332 message_loop_->RunUntilIdle(); 333 message_loop_->RunUntilIdle();
333 EXPECT_TRUE(a->enabled()); 334 EXPECT_TRUE(a->enabled());
334 EXPECT_TRUE(b->enabled()); 335 EXPECT_TRUE(b->enabled());
335 EXPECT_TRUE(c->enabled()); 336 EXPECT_TRUE(c->enabled());
336 a.release()->Destroy(); 337 a.release()->Destroy();
337 b.release()->Destroy(); 338 b.release()->Destroy();
338 c.release()->Destroy(); 339 c.release()->Destroy();
339 } 340 }
340 341
341 TEST_F(ComponentTest, WeakRefs) { 342 TEST_F(ComponentTest, WeakRefs) {
342 scoped_ptr<ComponentA> a(new ComponentA()); 343 std::unique_ptr<ComponentA> a(new ComponentA());
343 ComponentA::WeakRef weak = a->GetRef(); 344 ComponentA::WeakRef weak = a->GetRef();
344 EXPECT_FALSE(weak.Try()); 345 EXPECT_FALSE(weak.Try());
345 a->Enable(); 346 a->Enable();
346 EXPECT_FALSE(weak.Try()); 347 EXPECT_FALSE(weak.Try());
347 message_loop_->RunUntilIdle(); 348 message_loop_->RunUntilIdle();
348 EXPECT_TRUE(weak.Try()); 349 EXPECT_TRUE(weak.Try());
349 weak.Try()->Test(); 350 weak.Try()->Test();
350 a->Disable(); 351 a->Disable();
351 message_loop_->RunUntilIdle(); 352 message_loop_->RunUntilIdle();
352 EXPECT_FALSE(weak.Try()); 353 EXPECT_FALSE(weak.Try());
353 a.release()->Destroy(); 354 a.release()->Destroy();
354 } 355 }
355 356
356 TEST_F(ComponentTest, WeakRefsKeepEnabled) { 357 TEST_F(ComponentTest, WeakRefsKeepEnabled) {
357 scoped_ptr<ComponentA> a(new ComponentA()); 358 std::unique_ptr<ComponentA> a(new ComponentA());
358 ComponentA::WeakRef weak = a->GetRef(); 359 ComponentA::WeakRef weak = a->GetRef();
359 EXPECT_FALSE(weak.Try()); 360 EXPECT_FALSE(weak.Try());
360 a->Enable(); 361 a->Enable();
361 EXPECT_FALSE(weak.Try()); 362 EXPECT_FALSE(weak.Try());
362 message_loop_->RunUntilIdle(); 363 message_loop_->RunUntilIdle();
363 { 364 {
364 auto held_ref = weak.Try(); 365 auto held_ref = weak.Try();
365 EXPECT_TRUE(held_ref); 366 EXPECT_TRUE(held_ref);
366 held_ref->Test(); 367 held_ref->Test();
367 a->Disable(); 368 a->Disable();
368 message_loop_->RunUntilIdle(); 369 message_loop_->RunUntilIdle();
369 // The held ref keeps |a| enabled until it goes out of scope. 370 // The held ref keeps |a| enabled until it goes out of scope.
370 EXPECT_TRUE(a->enabled()); 371 EXPECT_TRUE(a->enabled());
371 } 372 }
372 message_loop_->RunUntilIdle(); 373 message_loop_->RunUntilIdle();
373 EXPECT_FALSE(a->enabled()); 374 EXPECT_FALSE(a->enabled());
374 EXPECT_FALSE(weak.Try()); 375 EXPECT_FALSE(weak.Try());
375 a.release()->Destroy(); 376 a.release()->Destroy();
376 } 377 }
377 378
378 } // namespace chromecast 379 } // namespace chromecast
OLDNEW
« no previous file with comments | « chromecast/base/cast_sys_info_util_simple.cc ('k') | chromecast/base/device_capabilities.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698