OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "media/mojo/services/strong_binding_set.h" |
| 6 |
| 7 #include "base/macros.h" |
| 8 #include "base/run_loop.h" |
| 9 #include "base/test/test_message_loop.h" |
| 10 #include "mojo/public/cpp/bindings/interface_request.h" |
| 11 #include "services/service_manager/tests/test.mojom.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace media { |
| 15 |
| 16 namespace { |
| 17 |
| 18 using TestService = service_manager::TestService; |
| 19 using TestServicePtr = service_manager::TestServicePtr; |
| 20 |
| 21 const int kInvalidBindingId = 100; |
| 22 |
| 23 class TestServiceImpl : public TestService { |
| 24 public: |
| 25 TestServiceImpl() { instance_count++; } |
| 26 ~TestServiceImpl() override { instance_count--; } |
| 27 |
| 28 // TestService implementation. |
| 29 void Test(const std::string& test_string, |
| 30 const TestCallback& callback) override { |
| 31 DVLOG(1) << __func__ << ": " << test_string; |
| 32 callback.Run(); |
| 33 } |
| 34 |
| 35 static int instance_count; |
| 36 }; |
| 37 |
| 38 int TestServiceImpl::instance_count = 0; |
| 39 |
| 40 } // namespace |
| 41 |
| 42 class StrongBindingSetTest : public testing::Test { |
| 43 public: |
| 44 StrongBindingSetTest() |
| 45 : bindings_(base::MakeUnique<StrongBindingSet<TestService>>()) {} |
| 46 ~StrongBindingSetTest() override {} |
| 47 |
| 48 protected: |
| 49 void AddBindings() { |
| 50 binding_id_1_ = bindings_->AddBinding(base::MakeUnique<TestServiceImpl>(), |
| 51 mojo::GetProxy(&test_service_ptr_1_)); |
| 52 EXPECT_EQ(1, TestServiceImpl::instance_count); |
| 53 |
| 54 binding_id_2_ = bindings_->AddBinding(base::MakeUnique<TestServiceImpl>(), |
| 55 mojo::GetProxy(&test_service_ptr_2_)); |
| 56 EXPECT_EQ(2, TestServiceImpl::instance_count); |
| 57 } |
| 58 |
| 59 TestServicePtr test_service_ptr_1_; |
| 60 TestServicePtr test_service_ptr_2_; |
| 61 StrongBindingSet<TestService>::BindingId binding_id_1_ = 0; |
| 62 StrongBindingSet<TestService>::BindingId binding_id_2_ = 0; |
| 63 std::unique_ptr<StrongBindingSet<TestService>> bindings_; |
| 64 |
| 65 private: |
| 66 base::TestMessageLoop message_loop_; |
| 67 |
| 68 DISALLOW_COPY_AND_ASSIGN(StrongBindingSetTest); |
| 69 }; |
| 70 |
| 71 TEST_F(StrongBindingSetTest, AddBinding_Destruct) { |
| 72 AddBindings(); |
| 73 |
| 74 bindings_.reset(); |
| 75 EXPECT_EQ(0, TestServiceImpl::instance_count); |
| 76 } |
| 77 |
| 78 TEST_F(StrongBindingSetTest, AddBinding_ConnectionError) { |
| 79 AddBindings(); |
| 80 |
| 81 test_service_ptr_1_.reset(); |
| 82 base::RunLoop().RunUntilIdle(); |
| 83 EXPECT_EQ(1, TestServiceImpl::instance_count); |
| 84 |
| 85 test_service_ptr_2_.reset(); |
| 86 base::RunLoop().RunUntilIdle(); |
| 87 EXPECT_EQ(0, TestServiceImpl::instance_count); |
| 88 } |
| 89 |
| 90 TEST_F(StrongBindingSetTest, AddBinding_RemoveBinding) { |
| 91 AddBindings(); |
| 92 |
| 93 EXPECT_TRUE(bindings_->RemoveBinding(binding_id_1_)); |
| 94 EXPECT_EQ(1, TestServiceImpl::instance_count); |
| 95 |
| 96 EXPECT_FALSE(bindings_->RemoveBinding(kInvalidBindingId)); |
| 97 EXPECT_EQ(1, TestServiceImpl::instance_count); |
| 98 |
| 99 EXPECT_TRUE(bindings_->RemoveBinding(binding_id_2_)); |
| 100 EXPECT_EQ(0, TestServiceImpl::instance_count); |
| 101 } |
| 102 |
| 103 } // namespace media |
OLD | NEW |