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 "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "base/task_runner.h" | 10 #include "base/task_runner.h" |
(...skipping 30 matching lines...) Expand all Loading... |
41 // before it (and should outlive it). | 41 // before it (and should outlive it). |
42 ChannelManager channel_manager_; | 42 ChannelManager channel_manager_; |
43 | 43 |
44 MOJO_DISALLOW_COPY_AND_ASSIGN(ChannelManagerTest); | 44 MOJO_DISALLOW_COPY_AND_ASSIGN(ChannelManagerTest); |
45 }; | 45 }; |
46 | 46 |
47 TEST_F(ChannelManagerTest, Basic) { | 47 TEST_F(ChannelManagerTest, Basic) { |
48 embedder::PlatformChannelPair channel_pair; | 48 embedder::PlatformChannelPair channel_pair; |
49 | 49 |
50 const ChannelId id = 1; | 50 const ChannelId id = 1; |
51 scoped_refptr<MessagePipeDispatcher> d = | 51 RefPtr<MessagePipeDispatcher> d = channel_manager().CreateChannelOnIOThread( |
52 channel_manager().CreateChannelOnIOThread( | 52 id, channel_pair.PassServerHandle()); |
53 id, channel_pair.PassServerHandle()); | |
54 | 53 |
55 RefPtr<Channel> ch = channel_manager().GetChannel(id); | 54 RefPtr<Channel> ch = channel_manager().GetChannel(id); |
56 EXPECT_TRUE(ch); | 55 EXPECT_TRUE(ch); |
57 | 56 |
58 channel_manager().WillShutdownChannel(id); | 57 channel_manager().WillShutdownChannel(id); |
59 | 58 |
60 channel_manager().ShutdownChannelOnIOThread(id); | 59 channel_manager().ShutdownChannelOnIOThread(id); |
61 // |ChannelManager| should have given up its ref. | 60 // |ChannelManager| should have given up its ref. |
62 ch->AssertHasOneRef(); | 61 ch->AssertHasOneRef(); |
63 | 62 |
64 EXPECT_EQ(MOJO_RESULT_OK, d->Close()); | 63 EXPECT_EQ(MOJO_RESULT_OK, d->Close()); |
65 } | 64 } |
66 | 65 |
67 TEST_F(ChannelManagerTest, TwoChannels) { | 66 TEST_F(ChannelManagerTest, TwoChannels) { |
68 embedder::PlatformChannelPair channel_pair; | 67 embedder::PlatformChannelPair channel_pair; |
69 | 68 |
70 const ChannelId id1 = 1; | 69 const ChannelId id1 = 1; |
71 scoped_refptr<MessagePipeDispatcher> d1 = | 70 RefPtr<MessagePipeDispatcher> d1 = channel_manager().CreateChannelOnIOThread( |
72 channel_manager().CreateChannelOnIOThread( | 71 id1, channel_pair.PassServerHandle()); |
73 id1, channel_pair.PassServerHandle()); | |
74 | 72 |
75 const ChannelId id2 = 2; | 73 const ChannelId id2 = 2; |
76 scoped_refptr<MessagePipeDispatcher> d2 = | 74 RefPtr<MessagePipeDispatcher> d2 = channel_manager().CreateChannelOnIOThread( |
77 channel_manager().CreateChannelOnIOThread( | 75 id2, channel_pair.PassClientHandle()); |
78 id2, channel_pair.PassClientHandle()); | |
79 | 76 |
80 RefPtr<Channel> ch1 = channel_manager().GetChannel(id1); | 77 RefPtr<Channel> ch1 = channel_manager().GetChannel(id1); |
81 EXPECT_TRUE(ch1); | 78 EXPECT_TRUE(ch1); |
82 | 79 |
83 RefPtr<Channel> ch2 = channel_manager().GetChannel(id2); | 80 RefPtr<Channel> ch2 = channel_manager().GetChannel(id2); |
84 EXPECT_TRUE(ch2); | 81 EXPECT_TRUE(ch2); |
85 | 82 |
86 // Calling |WillShutdownChannel()| multiple times (on |id1|) is okay. | 83 // Calling |WillShutdownChannel()| multiple times (on |id1|) is okay. |
87 channel_manager().WillShutdownChannel(id1); | 84 channel_manager().WillShutdownChannel(id1); |
88 channel_manager().WillShutdownChannel(id1); | 85 channel_manager().WillShutdownChannel(id1); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 ChannelId channel_id_; | 134 ChannelId channel_id_; |
138 base::Closure quit_closure_; | 135 base::Closure quit_closure_; |
139 | 136 |
140 MOJO_DISALLOW_COPY_AND_ASSIGN(OtherThread); | 137 MOJO_DISALLOW_COPY_AND_ASSIGN(OtherThread); |
141 }; | 138 }; |
142 | 139 |
143 TEST_F(ChannelManagerTest, CallsFromOtherThread) { | 140 TEST_F(ChannelManagerTest, CallsFromOtherThread) { |
144 embedder::PlatformChannelPair channel_pair; | 141 embedder::PlatformChannelPair channel_pair; |
145 | 142 |
146 const ChannelId id = 1; | 143 const ChannelId id = 1; |
147 scoped_refptr<MessagePipeDispatcher> d = | 144 RefPtr<MessagePipeDispatcher> d = channel_manager().CreateChannelOnIOThread( |
148 channel_manager().CreateChannelOnIOThread( | 145 id, channel_pair.PassServerHandle()); |
149 id, channel_pair.PassServerHandle()); | |
150 | 146 |
151 base::RunLoop run_loop; | 147 base::RunLoop run_loop; |
152 OtherThread thread(base::ThreadTaskRunnerHandle::Get(), &channel_manager(), | 148 OtherThread thread(base::ThreadTaskRunnerHandle::Get(), &channel_manager(), |
153 id, run_loop.QuitClosure()); | 149 id, run_loop.QuitClosure()); |
154 thread.Start(); | 150 thread.Start(); |
155 run_loop.Run(); | 151 run_loop.Run(); |
156 thread.Join(); | 152 thread.Join(); |
157 | 153 |
158 EXPECT_EQ(MOJO_RESULT_OK, d->Close()); | 154 EXPECT_EQ(MOJO_RESULT_OK, d->Close()); |
159 } | 155 } |
160 | 156 |
161 // TODO(vtl): Test |CreateChannelWithoutBootstrapOnIOThread()|. (This will | 157 // TODO(vtl): Test |CreateChannelWithoutBootstrapOnIOThread()|. (This will |
162 // require additional functionality in |Channel|.) | 158 // require additional functionality in |Channel|.) |
163 | 159 |
164 } // namespace | 160 } // namespace |
165 } // namespace system | 161 } // namespace system |
166 } // namespace mojo | 162 } // namespace mojo |
OLD | NEW |