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
3 // found in the LICENSE file.
5 #include "mojo/application/public/cpp/lib/service_registry.h"
7 #include "mojo/application/public/cpp/service_connector.h"
8 #include "testing/gtest/include/gtest/gtest.h"
14 class TestConnector
: public ServiceConnector
{
16 explicit TestConnector(int* delete_count
) : delete_count_(delete_count
) {}
17 ~TestConnector() override
{ (*delete_count_
)++; }
18 void ConnectToService(ApplicationConnection
* application_connection
,
19 const std::string
& interface_name
,
20 ScopedMessagePipeHandle client_handle
) override
{}
26 TEST(ServiceRegistryTest
, Ownership
) {
31 ServiceRegistry
* registry
= new ServiceRegistry
;
32 registry
->SetServiceConnectorForName(new TestConnector(&delete_count
),
34 registry
->CloseConnection();
36 EXPECT_EQ(1, delete_count
);
40 ServiceRegistry
* registry
= new ServiceRegistry
;
41 ServiceConnector
* c
= new TestConnector(&delete_count
);
42 registry
->SetServiceConnectorForName(c
, "TC1");
43 registry
->RemoveServiceConnectorForName("TC1");
44 registry
->CloseConnection();
45 EXPECT_EQ(2, delete_count
);
50 ServiceRegistry
* registry
= new ServiceRegistry
;
51 registry
->SetServiceConnectorForName(new TestConnector(&delete_count
),
53 registry
->SetServiceConnectorForName(new TestConnector(&delete_count
),
55 registry
->CloseConnection();
57 EXPECT_EQ(4, delete_count
);
61 ServiceRegistry
* registry
= new ServiceRegistry
;
62 registry
->SetServiceConnectorForName(new TestConnector(&delete_count
),
64 registry
->SetServiceConnectorForName(new TestConnector(&delete_count
),
66 EXPECT_EQ(5, delete_count
);
67 registry
->CloseConnection();
69 EXPECT_EQ(6, delete_count
);
73 } // namespace internal