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/edk/system/channel_manager.h" | 5 #include "mojo/edk/system/channel_manager.h" |
6 | 6 |
7 #include <functional> | 7 #include <functional> |
8 #include <memory> | 8 #include <memory> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "mojo/edk/embedder/platform_channel_pair.h" | |
12 #include "mojo/edk/embedder/simple_platform_support.h" | 11 #include "mojo/edk/embedder/simple_platform_support.h" |
13 #include "mojo/edk/platform/message_loop.h" | 12 #include "mojo/edk/platform/message_loop.h" |
| 13 #include "mojo/edk/platform/platform_pipe.h" |
14 #include "mojo/edk/platform/task_runner.h" | 14 #include "mojo/edk/platform/task_runner.h" |
15 #include "mojo/edk/platform/test_message_loops.h" | 15 #include "mojo/edk/platform/test_message_loops.h" |
16 #include "mojo/edk/system/channel.h" | 16 #include "mojo/edk/system/channel.h" |
17 #include "mojo/edk/system/channel_endpoint.h" | 17 #include "mojo/edk/system/channel_endpoint.h" |
18 #include "mojo/edk/system/message_pipe_dispatcher.h" | 18 #include "mojo/edk/system/message_pipe_dispatcher.h" |
19 #include "mojo/edk/system/test/simple_test_thread.h" | 19 #include "mojo/edk/system/test/simple_test_thread.h" |
20 #include "mojo/edk/util/ref_ptr.h" | 20 #include "mojo/edk/util/ref_ptr.h" |
21 #include "mojo/public/cpp/system/macros.h" | 21 #include "mojo/public/cpp/system/macros.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
23 | 23 |
24 using mojo::platform::MessageLoop; | 24 using mojo::platform::MessageLoop; |
25 using mojo::platform::PlatformHandleWatcher; | 25 using mojo::platform::PlatformHandleWatcher; |
| 26 using mojo::platform::PlatformPipe; |
26 using mojo::platform::TaskRunner; | 27 using mojo::platform::TaskRunner; |
27 using mojo::platform::test::CreateTestMessageLoop; | 28 using mojo::platform::test::CreateTestMessageLoop; |
28 using mojo::platform::test::CreateTestMessageLoopForIO; | 29 using mojo::platform::test::CreateTestMessageLoopForIO; |
29 using mojo::util::RefPtr; | 30 using mojo::util::RefPtr; |
30 | 31 |
31 namespace mojo { | 32 namespace mojo { |
32 namespace system { | 33 namespace system { |
33 namespace { | 34 namespace { |
34 | 35 |
35 class ChannelManagerTest : public testing::Test { | 36 class ChannelManagerTest : public testing::Test { |
(...skipping 22 matching lines...) Expand all Loading... |
58 PlatformHandleWatcher* platform_handle_watcher_; | 59 PlatformHandleWatcher* platform_handle_watcher_; |
59 std::unique_ptr<MessageLoop> message_loop_; | 60 std::unique_ptr<MessageLoop> message_loop_; |
60 // Note: This should be *after* the above, since they must be initialized | 61 // Note: This should be *after* the above, since they must be initialized |
61 // before it (and should outlive it). | 62 // before it (and should outlive it). |
62 ChannelManager channel_manager_; | 63 ChannelManager channel_manager_; |
63 | 64 |
64 MOJO_DISALLOW_COPY_AND_ASSIGN(ChannelManagerTest); | 65 MOJO_DISALLOW_COPY_AND_ASSIGN(ChannelManagerTest); |
65 }; | 66 }; |
66 | 67 |
67 TEST_F(ChannelManagerTest, Basic) { | 68 TEST_F(ChannelManagerTest, Basic) { |
68 embedder::PlatformChannelPair channel_pair; | 69 PlatformPipe channel_pair; |
69 | 70 |
70 const ChannelId id = 1; | 71 const ChannelId id = 1; |
71 RefPtr<MessagePipeDispatcher> d = channel_manager().CreateChannelOnIOThread( | 72 RefPtr<MessagePipeDispatcher> d = channel_manager().CreateChannelOnIOThread( |
72 id, channel_pair.handle0.Pass()); | 73 id, channel_pair.handle0.Pass()); |
73 | 74 |
74 RefPtr<Channel> ch = channel_manager().GetChannel(id); | 75 RefPtr<Channel> ch = channel_manager().GetChannel(id); |
75 EXPECT_TRUE(ch); | 76 EXPECT_TRUE(ch); |
76 // |ChannelManager| should have a ref. | 77 // |ChannelManager| should have a ref. |
77 EXPECT_FALSE(ch->HasOneRef()); | 78 EXPECT_FALSE(ch->HasOneRef()); |
78 | 79 |
79 channel_manager().WillShutdownChannel(id); | 80 channel_manager().WillShutdownChannel(id); |
80 // |ChannelManager| should still have a ref. | 81 // |ChannelManager| should still have a ref. |
81 EXPECT_FALSE(ch->HasOneRef()); | 82 EXPECT_FALSE(ch->HasOneRef()); |
82 | 83 |
83 channel_manager().ShutdownChannelOnIOThread(id); | 84 channel_manager().ShutdownChannelOnIOThread(id); |
84 // |ChannelManager| should have given up its ref. | 85 // |ChannelManager| should have given up its ref. |
85 EXPECT_TRUE(ch->HasOneRef()); | 86 EXPECT_TRUE(ch->HasOneRef()); |
86 | 87 |
87 EXPECT_EQ(MOJO_RESULT_OK, d->Close()); | 88 EXPECT_EQ(MOJO_RESULT_OK, d->Close()); |
88 } | 89 } |
89 | 90 |
90 TEST_F(ChannelManagerTest, TwoChannels) { | 91 TEST_F(ChannelManagerTest, TwoChannels) { |
91 embedder::PlatformChannelPair channel_pair; | 92 PlatformPipe channel_pair; |
92 | 93 |
93 const ChannelId id1 = 1; | 94 const ChannelId id1 = 1; |
94 RefPtr<MessagePipeDispatcher> d1 = channel_manager().CreateChannelOnIOThread( | 95 RefPtr<MessagePipeDispatcher> d1 = channel_manager().CreateChannelOnIOThread( |
95 id1, channel_pair.handle0.Pass()); | 96 id1, channel_pair.handle0.Pass()); |
96 | 97 |
97 const ChannelId id2 = 2; | 98 const ChannelId id2 = 2; |
98 RefPtr<MessagePipeDispatcher> d2 = channel_manager().CreateChannelOnIOThread( | 99 RefPtr<MessagePipeDispatcher> d2 = channel_manager().CreateChannelOnIOThread( |
99 id2, channel_pair.handle1.Pass()); | 100 id2, channel_pair.handle1.Pass()); |
100 | 101 |
101 RefPtr<Channel> ch1 = channel_manager().GetChannel(id1); | 102 RefPtr<Channel> ch1 = channel_manager().GetChannel(id1); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 | 161 |
161 const RefPtr<TaskRunner> task_runner_; | 162 const RefPtr<TaskRunner> task_runner_; |
162 ChannelManager* const channel_manager_; | 163 ChannelManager* const channel_manager_; |
163 const ChannelId channel_id_; | 164 const ChannelId channel_id_; |
164 std::function<void()> quit_closure_; | 165 std::function<void()> quit_closure_; |
165 | 166 |
166 MOJO_DISALLOW_COPY_AND_ASSIGN(OtherThread); | 167 MOJO_DISALLOW_COPY_AND_ASSIGN(OtherThread); |
167 }; | 168 }; |
168 | 169 |
169 TEST_F(ChannelManagerTest, CallsFromOtherThread) { | 170 TEST_F(ChannelManagerTest, CallsFromOtherThread) { |
170 embedder::PlatformChannelPair channel_pair; | 171 PlatformPipe channel_pair; |
171 | 172 |
172 const ChannelId id = 1; | 173 const ChannelId id = 1; |
173 RefPtr<MessagePipeDispatcher> d = channel_manager().CreateChannelOnIOThread( | 174 RefPtr<MessagePipeDispatcher> d = channel_manager().CreateChannelOnIOThread( |
174 id, channel_pair.handle0.Pass()); | 175 id, channel_pair.handle0.Pass()); |
175 | 176 |
176 OtherThread thread(task_runner().Clone(), &channel_manager(), id, | 177 OtherThread thread(task_runner().Clone(), &channel_manager(), id, |
177 [this]() { message_loop()->QuitNow(); }); | 178 [this]() { message_loop()->QuitNow(); }); |
178 thread.Start(); | 179 thread.Start(); |
179 message_loop()->Run(); | 180 message_loop()->Run(); |
180 thread.Join(); | 181 thread.Join(); |
181 | 182 |
182 EXPECT_EQ(MOJO_RESULT_OK, d->Close()); | 183 EXPECT_EQ(MOJO_RESULT_OK, d->Close()); |
183 } | 184 } |
184 | 185 |
185 // TODO(vtl): Test |CreateChannelWithoutBootstrapOnIOThread()|. (This will | 186 // TODO(vtl): Test |CreateChannelWithoutBootstrapOnIOThread()|. (This will |
186 // require additional functionality in |Channel|.) | 187 // require additional functionality in |Channel|.) |
187 | 188 |
188 } // namespace | 189 } // namespace |
189 } // namespace system | 190 } // namespace system |
190 } // namespace mojo | 191 } // namespace mojo |
OLD | NEW |