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