Chromium Code Reviews| 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 <memory> | 5 #include <memory> |
| 6 #include <utility> | 6 #include <utility> |
| 7 | 7 |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "mojo/public/cpp/bindings/associated_binding_set.h" | 10 #include "mojo/public/cpp/bindings/associated_binding_set.h" |
| 11 #include "mojo/public/cpp/bindings/binding_set.h" | 11 #include "mojo/public/cpp/bindings/binding_set.h" |
| 12 #include "mojo/public/cpp/bindings/strong_binding_set.h" | |
| 12 #include "mojo/public/interfaces/bindings/tests/ping_service.mojom.h" | 13 #include "mojo/public/interfaces/bindings/tests/ping_service.mojom.h" |
| 13 #include "mojo/public/interfaces/bindings/tests/test_associated_interfaces.mojom .h" | 14 #include "mojo/public/interfaces/bindings/tests/test_associated_interfaces.mojom .h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 16 |
| 16 namespace mojo { | 17 namespace mojo { |
| 17 namespace test { | 18 namespace test { |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 class BindingSetTest : public testing::Test { | 21 class BindingSetTest : public testing::Test { |
| 21 public: | 22 public: |
| (...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 342 run_loop.QuitClosure())); | 343 run_loop.QuitClosure())); |
| 343 | 344 |
| 344 PingServiceAssociatedPtr ptr; | 345 PingServiceAssociatedPtr ptr; |
| 345 master_ptr->GetPing(MakeRequest(&ptr, master_ptr.associated_group())); | 346 master_ptr->GetPing(MakeRequest(&ptr, master_ptr.associated_group())); |
| 346 | 347 |
| 347 ptr.ResetWithReason(2048u, "bye"); | 348 ptr.ResetWithReason(2048u, "bye"); |
| 348 | 349 |
| 349 run_loop.Run(); | 350 run_loop.Run(); |
| 350 } | 351 } |
| 351 | 352 |
| 353 class PingInstanceCounter : public PingService { | |
| 354 public: | |
| 355 PingInstanceCounter() { ++instance_count; } | |
| 356 ~PingInstanceCounter() override { --instance_count; } | |
| 357 | |
| 358 void Ping(const PingCallback& callback) override {} | |
| 359 | |
| 360 static int instance_count; | |
| 361 }; | |
| 362 int PingInstanceCounter::instance_count = 0; | |
| 363 | |
| 364 TEST_F(BindingSetTest, StrongBinding_Destructor) { | |
| 365 PingServicePtr ping_a, ping_b; | |
| 366 auto bindings = base::MakeUnique<StrongBindingSet<PingService>>(); | |
| 367 | |
| 368 bindings->AddBinding(base::MakeUnique<PingInstanceCounter>(), | |
| 369 mojo::MakeRequest(&ping_a)); | |
| 370 EXPECT_EQ(1, PingInstanceCounter::instance_count); | |
| 371 | |
| 372 bindings->AddBinding(base::MakeUnique<PingInstanceCounter>(), | |
| 373 mojo::MakeRequest(&ping_b)); | |
| 374 EXPECT_EQ(2, PingInstanceCounter::instance_count); | |
| 375 | |
| 376 bindings.reset(); | |
| 377 EXPECT_EQ(0, PingInstanceCounter::instance_count); | |
| 378 } | |
| 379 | |
| 380 TEST_F(BindingSetTest, StrongBinding_ConnectionError) { | |
| 381 PingServicePtr ping_a, ping_b; | |
| 382 StrongBindingSet<PingService> bindings; | |
| 383 bindings.AddBinding(base::MakeUnique<PingInstanceCounter>(), | |
| 384 mojo::MakeRequest(&ping_a)); | |
| 385 bindings.AddBinding(base::MakeUnique<PingInstanceCounter>(), | |
| 386 mojo::MakeRequest(&ping_b)); | |
| 387 EXPECT_EQ(2, PingInstanceCounter::instance_count); | |
| 388 | |
| 389 ping_a.reset(); | |
| 390 base::RunLoop().RunUntilIdle(); | |
| 391 EXPECT_EQ(1, PingInstanceCounter::instance_count); | |
| 392 | |
| 393 ping_b.reset(); | |
| 394 base::RunLoop().RunUntilIdle(); | |
| 395 EXPECT_EQ(0, PingInstanceCounter::instance_count); | |
| 396 } | |
| 397 | |
| 398 TEST_F(BindingSetTest, StrongBinding_RemoveBinding) { | |
| 399 PingServicePtr ping_a, ping_b; | |
| 400 StrongBindingSet<PingService> bindings; | |
| 401 BindingId binding_id_a = bindings.AddBinding( | |
| 402 base::MakeUnique<PingInstanceCounter>(), mojo::MakeRequest(&ping_a)); | |
| 403 BindingId binding_id_b = bindings.AddBinding( | |
| 404 base::MakeUnique<PingInstanceCounter>(), mojo::MakeRequest(&ping_b)); | |
| 405 EXPECT_EQ(2, PingInstanceCounter::instance_count); | |
| 406 | |
| 407 EXPECT_TRUE(bindings.RemoveBinding(binding_id_a)); | |
| 408 EXPECT_EQ(1, PingInstanceCounter::instance_count); | |
| 409 | |
| 410 EXPECT_TRUE(bindings.RemoveBinding(binding_id_b)); | |
| 411 EXPECT_EQ(0, PingInstanceCounter::instance_count); | |
|
xhwang
2017/02/16 03:54:49
What happens if you call RemoveBinding with a rand
alokp
2017/02/16 04:42:46
That would be a test for any BindingSet derived fr
| |
| 412 } | |
| 413 | |
| 352 } // namespace | 414 } // namespace |
| 353 } // namespace test | 415 } // namespace test |
| 354 } // namespace mojo | 416 } // namespace mojo |
| OLD | NEW |