| OLD | NEW |
| 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> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/location.h" | |
| 10 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 11 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 12 #include "base/threading/thread_task_runner_handle.h" | 11 #include "base/threading/thread_task_runner_handle.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 13 |
| 15 namespace chromecast { | 14 namespace chromecast { |
| 16 | 15 |
| 17 class ComponentTest : public ::testing::Test { | 16 class ComponentTest : public ::testing::Test { |
| 18 protected: | 17 protected: |
| 19 ComponentTest() : message_loop_(new base::MessageLoop()) {} | 18 ComponentTest() : message_loop_(new base::MessageLoop()) {} |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 ComponentC c(b.GetRef()); | 174 ComponentC c(b.GetRef()); |
| 176 EXPECT_DEATH(a.MakeTransitiveCircularDependency(c.GetRef()), | 175 EXPECT_DEATH(a.MakeTransitiveCircularDependency(c.GetRef()), |
| 177 "Circular dependency"); | 176 "Circular dependency"); |
| 178 } | 177 } |
| 179 #endif // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && | 178 #endif // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && |
| 180 // GTEST_HAS_DEATH_TEST | 179 // GTEST_HAS_DEATH_TEST |
| 181 | 180 |
| 182 TEST_F(ComponentTest, SimpleEnable) { | 181 TEST_F(ComponentTest, SimpleEnable) { |
| 183 std::unique_ptr<ComponentA> a(new ComponentA()); | 182 std::unique_ptr<ComponentA> a(new ComponentA()); |
| 184 a->Enable(); | 183 a->Enable(); |
| 185 message_loop_->RunUntilIdle(); | 184 base::RunLoop().RunUntilIdle(); |
| 186 EXPECT_TRUE(a->enabled()); | 185 EXPECT_TRUE(a->enabled()); |
| 187 a.release()->Destroy(); | 186 a.release()->Destroy(); |
| 188 } | 187 } |
| 189 | 188 |
| 190 TEST_F(ComponentTest, TransitiveEnable) { | 189 TEST_F(ComponentTest, TransitiveEnable) { |
| 191 std::unique_ptr<ComponentA> a(new ComponentA()); | 190 std::unique_ptr<ComponentA> a(new ComponentA()); |
| 192 std::unique_ptr<ComponentB> b(new ComponentB(a->GetRef())); | 191 std::unique_ptr<ComponentB> b(new ComponentB(a->GetRef())); |
| 193 std::unique_ptr<ComponentC> c(new ComponentC(b->GetRef())); | 192 std::unique_ptr<ComponentC> c(new ComponentC(b->GetRef())); |
| 194 c->Enable(); | 193 c->Enable(); |
| 195 message_loop_->RunUntilIdle(); | 194 base::RunLoop().RunUntilIdle(); |
| 196 EXPECT_TRUE(a->enabled()); | 195 EXPECT_TRUE(a->enabled()); |
| 197 EXPECT_TRUE(b->enabled()); | 196 EXPECT_TRUE(b->enabled()); |
| 198 EXPECT_TRUE(c->enabled()); | 197 EXPECT_TRUE(c->enabled()); |
| 199 a.release()->Destroy(); | 198 a.release()->Destroy(); |
| 200 b.release()->Destroy(); | 199 b.release()->Destroy(); |
| 201 c.release()->Destroy(); | 200 c.release()->Destroy(); |
| 202 } | 201 } |
| 203 | 202 |
| 204 TEST_F(ComponentTest, FailEnable) { | 203 TEST_F(ComponentTest, FailEnable) { |
| 205 std::unique_ptr<ComponentA> a(new ComponentA()); | 204 std::unique_ptr<ComponentA> a(new ComponentA()); |
| 206 a->FailEnable(); | 205 a->FailEnable(); |
| 207 a->Enable(); | 206 a->Enable(); |
| 208 message_loop_->RunUntilIdle(); | 207 base::RunLoop().RunUntilIdle(); |
| 209 EXPECT_FALSE(a->enabled()); | 208 EXPECT_FALSE(a->enabled()); |
| 210 a.release()->Destroy(); | 209 a.release()->Destroy(); |
| 211 } | 210 } |
| 212 | 211 |
| 213 TEST_F(ComponentTest, TransitiveFailEnable) { | 212 TEST_F(ComponentTest, TransitiveFailEnable) { |
| 214 std::unique_ptr<ComponentA> a(new ComponentA()); | 213 std::unique_ptr<ComponentA> a(new ComponentA()); |
| 215 std::unique_ptr<ComponentB> b(new ComponentB(a->GetRef())); | 214 std::unique_ptr<ComponentB> b(new ComponentB(a->GetRef())); |
| 216 std::unique_ptr<ComponentC> c(new ComponentC(b->GetRef())); | 215 std::unique_ptr<ComponentC> c(new ComponentC(b->GetRef())); |
| 217 a->FailEnable(); | 216 a->FailEnable(); |
| 218 c->Enable(); | 217 c->Enable(); |
| 219 message_loop_->RunUntilIdle(); | 218 base::RunLoop().RunUntilIdle(); |
| 220 EXPECT_FALSE(a->enabled()); | 219 EXPECT_FALSE(a->enabled()); |
| 221 EXPECT_FALSE(b->enabled()); | 220 EXPECT_FALSE(b->enabled()); |
| 222 EXPECT_FALSE(c->enabled()); | 221 EXPECT_FALSE(c->enabled()); |
| 223 a.release()->Destroy(); | 222 a.release()->Destroy(); |
| 224 b.release()->Destroy(); | 223 b.release()->Destroy(); |
| 225 c.release()->Destroy(); | 224 c.release()->Destroy(); |
| 226 } | 225 } |
| 227 | 226 |
| 228 TEST_F(ComponentTest, DisableWhileEnabling) { | 227 TEST_F(ComponentTest, DisableWhileEnabling) { |
| 229 std::unique_ptr<ComponentA> a(new ComponentA()); | 228 std::unique_ptr<ComponentA> a(new ComponentA()); |
| 230 a->Enable(); | 229 a->Enable(); |
| 231 a->Disable(); | 230 a->Disable(); |
| 232 message_loop_->RunUntilIdle(); | 231 base::RunLoop().RunUntilIdle(); |
| 233 EXPECT_FALSE(a->enabled()); | 232 EXPECT_FALSE(a->enabled()); |
| 234 a.release()->Destroy(); | 233 a.release()->Destroy(); |
| 235 } | 234 } |
| 236 | 235 |
| 237 TEST_F(ComponentTest, EnableTwice) { | 236 TEST_F(ComponentTest, EnableTwice) { |
| 238 std::unique_ptr<ComponentA> a(new ComponentA()); | 237 std::unique_ptr<ComponentA> a(new ComponentA()); |
| 239 a->Enable(); | 238 a->Enable(); |
| 240 a->Enable(); | 239 a->Enable(); |
| 241 message_loop_->RunUntilIdle(); | 240 base::RunLoop().RunUntilIdle(); |
| 242 EXPECT_TRUE(a->enabled()); | 241 EXPECT_TRUE(a->enabled()); |
| 243 a.release()->Destroy(); | 242 a.release()->Destroy(); |
| 244 } | 243 } |
| 245 | 244 |
| 246 TEST_F(ComponentTest, DisableTwice) { | 245 TEST_F(ComponentTest, DisableTwice) { |
| 247 std::unique_ptr<ComponentA> a(new ComponentA()); | 246 std::unique_ptr<ComponentA> a(new ComponentA()); |
| 248 a->Enable(); | 247 a->Enable(); |
| 249 message_loop_->RunUntilIdle(); | 248 base::RunLoop().RunUntilIdle(); |
| 250 EXPECT_TRUE(a->enabled()); | 249 EXPECT_TRUE(a->enabled()); |
| 251 a->Disable(); | 250 a->Disable(); |
| 252 message_loop_->RunUntilIdle(); | 251 base::RunLoop().RunUntilIdle(); |
| 253 EXPECT_FALSE(a->enabled()); | 252 EXPECT_FALSE(a->enabled()); |
| 254 a->Disable(); | 253 a->Disable(); |
| 255 message_loop_->RunUntilIdle(); | 254 base::RunLoop().RunUntilIdle(); |
| 256 EXPECT_FALSE(a->enabled()); | 255 EXPECT_FALSE(a->enabled()); |
| 257 a.release()->Destroy(); | 256 a.release()->Destroy(); |
| 258 } | 257 } |
| 259 | 258 |
| 260 TEST_F(ComponentTest, DisableAfterFailedEnable) { | 259 TEST_F(ComponentTest, DisableAfterFailedEnable) { |
| 261 std::unique_ptr<ComponentA> a(new ComponentA()); | 260 std::unique_ptr<ComponentA> a(new ComponentA()); |
| 262 a->FailEnable(); | 261 a->FailEnable(); |
| 263 a->Enable(); | 262 a->Enable(); |
| 264 message_loop_->RunUntilIdle(); | 263 base::RunLoop().RunUntilIdle(); |
| 265 EXPECT_FALSE(a->enabled()); | 264 EXPECT_FALSE(a->enabled()); |
| 266 a->Disable(); | 265 a->Disable(); |
| 267 message_loop_->RunUntilIdle(); | 266 base::RunLoop().RunUntilIdle(); |
| 268 EXPECT_FALSE(a->enabled()); | 267 EXPECT_FALSE(a->enabled()); |
| 269 a.release()->Destroy(); | 268 a.release()->Destroy(); |
| 270 } | 269 } |
| 271 | 270 |
| 272 TEST_F(ComponentTest, DisableAfterNeverEnabled) { | 271 TEST_F(ComponentTest, DisableAfterNeverEnabled) { |
| 273 std::unique_ptr<ComponentA> a(new ComponentA()); | 272 std::unique_ptr<ComponentA> a(new ComponentA()); |
| 274 a->Disable(); | 273 a->Disable(); |
| 275 message_loop_->RunUntilIdle(); | 274 base::RunLoop().RunUntilIdle(); |
| 276 EXPECT_FALSE(a->enabled()); | 275 EXPECT_FALSE(a->enabled()); |
| 277 a.release()->Destroy(); | 276 a.release()->Destroy(); |
| 278 } | 277 } |
| 279 | 278 |
| 280 TEST_F(ComponentTest, DisableDependencyWhileEnabling) { | 279 TEST_F(ComponentTest, DisableDependencyWhileEnabling) { |
| 281 std::unique_ptr<ComponentA> a(new ComponentA()); | 280 std::unique_ptr<ComponentA> a(new ComponentA()); |
| 282 std::unique_ptr<ComponentB> b(new ComponentB(a->GetRef())); | 281 std::unique_ptr<ComponentB> b(new ComponentB(a->GetRef())); |
| 283 std::unique_ptr<ComponentC> c(new ComponentC(b->GetRef())); | 282 std::unique_ptr<ComponentC> c(new ComponentC(b->GetRef())); |
| 284 b->Enable(); | 283 b->Enable(); |
| 285 message_loop_->RunUntilIdle(); | 284 base::RunLoop().RunUntilIdle(); |
| 286 c->Enable(); | 285 c->Enable(); |
| 287 a->Disable(); | 286 a->Disable(); |
| 288 message_loop_->RunUntilIdle(); | 287 base::RunLoop().RunUntilIdle(); |
| 289 EXPECT_FALSE(a->enabled()); | 288 EXPECT_FALSE(a->enabled()); |
| 290 EXPECT_FALSE(b->enabled()); | 289 EXPECT_FALSE(b->enabled()); |
| 291 EXPECT_FALSE(c->enabled()); | 290 EXPECT_FALSE(c->enabled()); |
| 292 a.release()->Destroy(); | 291 a.release()->Destroy(); |
| 293 b.release()->Destroy(); | 292 b.release()->Destroy(); |
| 294 c.release()->Destroy(); | 293 c.release()->Destroy(); |
| 295 } | 294 } |
| 296 | 295 |
| 297 TEST_F(ComponentTest, EnableDisableEnable) { | 296 TEST_F(ComponentTest, EnableDisableEnable) { |
| 298 std::unique_ptr<ComponentA> a(new ComponentA()); | 297 std::unique_ptr<ComponentA> a(new ComponentA()); |
| 299 a->Enable(); | 298 a->Enable(); |
| 300 a->Disable(); | 299 a->Disable(); |
| 301 a->Enable(); | 300 a->Enable(); |
| 302 message_loop_->RunUntilIdle(); | 301 base::RunLoop().RunUntilIdle(); |
| 303 EXPECT_TRUE(a->enabled()); | 302 EXPECT_TRUE(a->enabled()); |
| 304 a.release()->Destroy(); | 303 a.release()->Destroy(); |
| 305 } | 304 } |
| 306 | 305 |
| 307 TEST_F(ComponentTest, DisableEnableDisable) { | 306 TEST_F(ComponentTest, DisableEnableDisable) { |
| 308 std::unique_ptr<ComponentA> a(new ComponentA()); | 307 std::unique_ptr<ComponentA> a(new ComponentA()); |
| 309 a->Enable(); | 308 a->Enable(); |
| 310 message_loop_->RunUntilIdle(); | 309 base::RunLoop().RunUntilIdle(); |
| 311 EXPECT_TRUE(a->enabled()); | 310 EXPECT_TRUE(a->enabled()); |
| 312 a->Disable(); | 311 a->Disable(); |
| 313 a->Enable(); | 312 a->Enable(); |
| 314 a->Disable(); | 313 a->Disable(); |
| 315 message_loop_->RunUntilIdle(); | 314 base::RunLoop().RunUntilIdle(); |
| 316 EXPECT_FALSE(a->enabled()); | 315 EXPECT_FALSE(a->enabled()); |
| 317 a.release()->Destroy(); | 316 a.release()->Destroy(); |
| 318 } | 317 } |
| 319 | 318 |
| 320 TEST_F(ComponentTest, TransitiveEnableDisableEnable) { | 319 TEST_F(ComponentTest, TransitiveEnableDisableEnable) { |
| 321 std::unique_ptr<ComponentA> a(new ComponentA()); | 320 std::unique_ptr<ComponentA> a(new ComponentA()); |
| 322 std::unique_ptr<ComponentB> b(new ComponentB(a->GetRef())); | 321 std::unique_ptr<ComponentB> b(new ComponentB(a->GetRef())); |
| 323 std::unique_ptr<ComponentC> c(new ComponentC(b->GetRef())); | 322 std::unique_ptr<ComponentC> c(new ComponentC(b->GetRef())); |
| 324 a->Enable(); | 323 a->Enable(); |
| 325 message_loop_->RunUntilIdle(); | 324 base::RunLoop().RunUntilIdle(); |
| 326 c->Enable(); | 325 c->Enable(); |
| 327 a->Disable(); | 326 a->Disable(); |
| 328 message_loop_->RunUntilIdle(); | 327 base::RunLoop().RunUntilIdle(); |
| 329 EXPECT_FALSE(a->enabled()); | 328 EXPECT_FALSE(a->enabled()); |
| 330 EXPECT_FALSE(b->enabled()); | 329 EXPECT_FALSE(b->enabled()); |
| 331 EXPECT_FALSE(c->enabled()); | 330 EXPECT_FALSE(c->enabled()); |
| 332 c->Enable(); | 331 c->Enable(); |
| 333 message_loop_->RunUntilIdle(); | 332 base::RunLoop().RunUntilIdle(); |
| 334 EXPECT_TRUE(a->enabled()); | 333 EXPECT_TRUE(a->enabled()); |
| 335 EXPECT_TRUE(b->enabled()); | 334 EXPECT_TRUE(b->enabled()); |
| 336 EXPECT_TRUE(c->enabled()); | 335 EXPECT_TRUE(c->enabled()); |
| 337 a.release()->Destroy(); | 336 a.release()->Destroy(); |
| 338 b.release()->Destroy(); | 337 b.release()->Destroy(); |
| 339 c.release()->Destroy(); | 338 c.release()->Destroy(); |
| 340 } | 339 } |
| 341 | 340 |
| 342 TEST_F(ComponentTest, WeakRefs) { | 341 TEST_F(ComponentTest, WeakRefs) { |
| 343 std::unique_ptr<ComponentA> a(new ComponentA()); | 342 std::unique_ptr<ComponentA> a(new ComponentA()); |
| 344 ComponentA::WeakRef weak = a->GetRef(); | 343 ComponentA::WeakRef weak = a->GetRef(); |
| 345 EXPECT_FALSE(weak.Try()); | 344 EXPECT_FALSE(weak.Try()); |
| 346 a->Enable(); | 345 a->Enable(); |
| 347 EXPECT_FALSE(weak.Try()); | 346 EXPECT_FALSE(weak.Try()); |
| 348 message_loop_->RunUntilIdle(); | 347 base::RunLoop().RunUntilIdle(); |
| 349 EXPECT_TRUE(weak.Try()); | 348 EXPECT_TRUE(weak.Try()); |
| 350 weak.Try()->Test(); | 349 weak.Try()->Test(); |
| 351 a->Disable(); | 350 a->Disable(); |
| 352 message_loop_->RunUntilIdle(); | 351 base::RunLoop().RunUntilIdle(); |
| 353 EXPECT_FALSE(weak.Try()); | 352 EXPECT_FALSE(weak.Try()); |
| 354 a.release()->Destroy(); | 353 a.release()->Destroy(); |
| 355 } | 354 } |
| 356 | 355 |
| 357 TEST_F(ComponentTest, WeakRefsKeepEnabled) { | 356 TEST_F(ComponentTest, WeakRefsKeepEnabled) { |
| 358 std::unique_ptr<ComponentA> a(new ComponentA()); | 357 std::unique_ptr<ComponentA> a(new ComponentA()); |
| 359 ComponentA::WeakRef weak = a->GetRef(); | 358 ComponentA::WeakRef weak = a->GetRef(); |
| 360 EXPECT_FALSE(weak.Try()); | 359 EXPECT_FALSE(weak.Try()); |
| 361 a->Enable(); | 360 a->Enable(); |
| 362 EXPECT_FALSE(weak.Try()); | 361 EXPECT_FALSE(weak.Try()); |
| 363 message_loop_->RunUntilIdle(); | 362 base::RunLoop().RunUntilIdle(); |
| 364 { | 363 { |
| 365 auto held_ref = weak.Try(); | 364 auto held_ref = weak.Try(); |
| 366 EXPECT_TRUE(held_ref); | 365 EXPECT_TRUE(held_ref); |
| 367 held_ref->Test(); | 366 held_ref->Test(); |
| 368 a->Disable(); | 367 a->Disable(); |
| 369 message_loop_->RunUntilIdle(); | 368 base::RunLoop().RunUntilIdle(); |
| 370 // The held ref keeps |a| enabled until it goes out of scope. | 369 // The held ref keeps |a| enabled until it goes out of scope. |
| 371 EXPECT_TRUE(a->enabled()); | 370 EXPECT_TRUE(a->enabled()); |
| 372 } | 371 } |
| 373 message_loop_->RunUntilIdle(); | 372 base::RunLoop().RunUntilIdle(); |
| 374 EXPECT_FALSE(a->enabled()); | 373 EXPECT_FALSE(a->enabled()); |
| 375 EXPECT_FALSE(weak.Try()); | 374 EXPECT_FALSE(weak.Try()); |
| 376 a.release()->Destroy(); | 375 a.release()->Destroy(); |
| 377 } | 376 } |
| 378 | 377 |
| 379 } // namespace chromecast | 378 } // namespace chromecast |
| OLD | NEW |