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