Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / mojo / public / cpp / application / tests / service_registry_unittest.cc
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.
4
5 #include "mojo/public/cpp/application/lib/service_registry.h"
6
7 #include "mojo/public/cpp/application/lib/service_connector.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9
10 namespace mojo {
11 namespace internal {
12 namespace {
13
14 class TestConnector : public ServiceConnectorBase {
15  public:
16   TestConnector(const std::string& name, int* delete_count)
17       : ServiceConnectorBase(name), delete_count_(delete_count) {}
18   ~TestConnector() override { (*delete_count_)++; }
19   void ConnectToService(const std::string& name,
20                         ScopedMessagePipeHandle client_handle) override {}
21
22  private:
23   int* delete_count_;
24 };
25
26 TEST(ServiceRegistryTest, Ownership) {
27   int delete_count = 0;
28
29   // Destruction.
30   {
31     ServiceRegistry registry;
32     registry.AddServiceConnector(new TestConnector("TC1", &delete_count));
33   }
34   EXPECT_EQ(1, delete_count);
35
36   // Removal.
37   {
38     ServiceRegistry registry;
39     ServiceConnectorBase* c = new TestConnector("TC1", &delete_count);
40     registry.AddServiceConnector(c);
41     registry.RemoveServiceConnector(c);
42     EXPECT_EQ(2, delete_count);
43   }
44
45   // Multiple.
46   {
47     ServiceRegistry registry;
48     registry.AddServiceConnector(new TestConnector("TC1", &delete_count));
49     registry.AddServiceConnector(new TestConnector("TC2", &delete_count));
50   }
51   EXPECT_EQ(4, delete_count);
52
53   // Re-addition.
54   {
55     ServiceRegistry registry;
56     registry.AddServiceConnector(new TestConnector("TC1", &delete_count));
57     registry.AddServiceConnector(new TestConnector("TC1", &delete_count));
58     EXPECT_EQ(5, delete_count);
59   }
60   EXPECT_EQ(6, delete_count);
61 }
62
63 }  // namespace
64 }  // namespace internal
65 }  // namespace mojo