OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "mojo/application/public/cpp/lib/service_registry.h" | 5 #include "mojo/application/public/cpp/lib/service_registry.h" |
6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" |
7 #include "mojo/application/public/cpp/service_connector.h" | 8 #include "mojo/application/public/cpp/service_connector.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
9 | 10 |
10 namespace mojo { | 11 namespace mojo { |
11 namespace internal { | 12 namespace internal { |
12 namespace { | 13 namespace { |
13 | 14 |
14 class TestConnector : public ServiceConnector { | 15 class TestConnector : public ServiceConnector { |
15 public: | 16 public: |
16 explicit TestConnector(int* delete_count) : delete_count_(delete_count) {} | 17 explicit TestConnector(int* delete_count) : delete_count_(delete_count) {} |
17 ~TestConnector() override { (*delete_count_)++; } | 18 ~TestConnector() override { (*delete_count_)++; } |
18 void ConnectToService(ApplicationConnection* application_connection, | 19 void ConnectToService(ApplicationConnection* application_connection, |
19 const std::string& interface_name, | 20 const std::string& interface_name, |
20 ScopedMessagePipeHandle client_handle) override {} | 21 ScopedMessagePipeHandle client_handle) override {} |
21 | 22 |
22 private: | 23 private: |
23 int* delete_count_; | 24 int* delete_count_; |
24 }; | 25 }; |
25 | 26 |
26 TEST(ServiceRegistryTest, Ownership) { | 27 TEST(ServiceRegistryTest, Ownership) { |
27 int delete_count = 0; | 28 int delete_count = 0; |
28 | 29 |
29 // Destruction. | 30 // Destruction. |
30 { | 31 { |
31 ServiceRegistry* registry = new ServiceRegistry; | 32 ServiceRegistry registry; |
32 registry->SetServiceConnectorForName(new TestConnector(&delete_count), | 33 registry.SetServiceConnectorForName(new TestConnector(&delete_count), |
33 "TC1"); | 34 "TC1"); |
34 registry->CloseConnection(); | |
35 } | 35 } |
36 EXPECT_EQ(1, delete_count); | 36 EXPECT_EQ(1, delete_count); |
37 | 37 |
38 // Removal. | 38 // Removal. |
39 { | 39 { |
40 ServiceRegistry* registry = new ServiceRegistry; | 40 scoped_ptr<ServiceRegistry> registry(new ServiceRegistry); |
41 ServiceConnector* c = new TestConnector(&delete_count); | 41 ServiceConnector* c = new TestConnector(&delete_count); |
42 registry->SetServiceConnectorForName(c, "TC1"); | 42 registry->SetServiceConnectorForName(c, "TC1"); |
43 registry->RemoveServiceConnectorForName("TC1"); | 43 registry->RemoveServiceConnectorForName("TC1"); |
44 registry->CloseConnection(); | 44 registry.reset(); |
45 EXPECT_EQ(2, delete_count); | 45 EXPECT_EQ(2, delete_count); |
46 } | 46 } |
47 | 47 |
48 // Multiple. | 48 // Multiple. |
49 { | 49 { |
50 ServiceRegistry* registry = new ServiceRegistry; | 50 ServiceRegistry registry; |
51 registry->SetServiceConnectorForName(new TestConnector(&delete_count), | 51 registry.SetServiceConnectorForName(new TestConnector(&delete_count), |
52 "TC1"); | 52 "TC1"); |
53 registry->SetServiceConnectorForName(new TestConnector(&delete_count), | 53 registry.SetServiceConnectorForName(new TestConnector(&delete_count), |
54 "TC2"); | 54 "TC2"); |
55 registry->CloseConnection(); | |
56 } | 55 } |
57 EXPECT_EQ(4, delete_count); | 56 EXPECT_EQ(4, delete_count); |
58 | 57 |
59 // Re-addition. | 58 // Re-addition. |
60 { | 59 { |
61 ServiceRegistry* registry = new ServiceRegistry; | 60 ServiceRegistry registry; |
62 registry->SetServiceConnectorForName(new TestConnector(&delete_count), | 61 registry.SetServiceConnectorForName(new TestConnector(&delete_count), |
63 "TC1"); | 62 "TC1"); |
64 registry->SetServiceConnectorForName(new TestConnector(&delete_count), | 63 registry.SetServiceConnectorForName(new TestConnector(&delete_count), |
65 "TC1"); | 64 "TC1"); |
66 EXPECT_EQ(5, delete_count); | 65 EXPECT_EQ(5, delete_count); |
67 registry->CloseConnection(); | |
68 } | 66 } |
69 EXPECT_EQ(6, delete_count); | 67 EXPECT_EQ(6, delete_count); |
70 } | 68 } |
71 | 69 |
72 } // namespace | 70 } // namespace |
73 } // namespace internal | 71 } // namespace internal |
74 } // namespace mojo | 72 } // namespace mojo |
OLD | NEW |