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 "ipc/mojo/ipc_channel_mojo.h" | 5 #include "ipc/mojo/ipc_channel_mojo.h" |
6 | 6 |
7 #include "base/base_paths.h" | 7 #include "base/base_paths.h" |
8 #include "base/files/file.h" | 8 #include "base/files/file.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 IPC::Message* message = new IPC::Message( | 48 IPC::Message* message = new IPC::Message( |
49 0, 2, IPC::Message::PRIORITY_NORMAL); | 49 0, 2, IPC::Message::PRIORITY_NORMAL); |
50 message->WriteString(std::string("OK")); | 50 message->WriteString(std::string("OK")); |
51 ASSERT_TRUE(sender->Send(message)); | 51 ASSERT_TRUE(sender->Send(message)); |
52 } | 52 } |
53 | 53 |
54 private: | 54 private: |
55 bool received_ok_; | 55 bool received_ok_; |
56 }; | 56 }; |
57 | 57 |
58 class ListenerThatShouldBeNeverCalled : public IPC::Listener { | |
59 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { | |
60 NOTREACHED(); | |
61 return true; | |
62 } | |
63 | |
64 virtual void OnChannelError() OVERRIDE { | |
65 NOTREACHED(); | |
66 } | |
67 | |
68 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE { | |
69 NOTREACHED(); | |
70 } | |
71 | |
72 virtual void OnBadMessageReceived(const IPC::Message& message) OVERRIDE { | |
73 NOTREACHED(); | |
74 } | |
75 }; | |
76 | |
77 class ChannelClient { | 58 class ChannelClient { |
78 public: | 59 public: |
79 explicit ChannelClient(IPC::Listener* listener, const char* name) { | 60 explicit ChannelClient(IPC::Listener* listener, const char* name) { |
80 scoped_ptr<IPC::Channel> bootstrap(IPC::Channel::CreateClient( | |
81 IPCTestBase::GetChannelName(name), | |
82 &never_called_)); | |
83 channel_ = IPC::ChannelMojo::Create( | 61 channel_ = IPC::ChannelMojo::Create( |
84 bootstrap.Pass(), IPC::Channel::MODE_CLIENT, listener, | 62 IPCTestBase::GetChannelName(name), IPC::Channel::MODE_CLIENT, listener, |
85 main_message_loop_.message_loop_proxy()); | 63 main_message_loop_.message_loop_proxy()); |
86 } | 64 } |
87 | 65 |
88 void Connect() { | 66 void Connect() { |
89 CHECK(channel_->Connect()); | 67 CHECK(channel_->Connect()); |
90 } | 68 } |
91 | 69 |
92 IPC::ChannelMojo* channel() const { return channel_.get(); } | 70 IPC::ChannelMojo* channel() const { return channel_.get(); } |
93 | 71 |
94 private: | 72 private: |
95 scoped_ptr<IPC::ChannelMojo> channel_; | 73 scoped_ptr<IPC::ChannelMojo> channel_; |
96 ListenerThatShouldBeNeverCalled never_called_; | |
97 base::MessageLoopForIO main_message_loop_; | 74 base::MessageLoopForIO main_message_loop_; |
98 }; | 75 }; |
99 | 76 |
100 class IPCChannelMojoTest : public IPCTestBase { | 77 class IPCChannelMojoTest : public IPCTestBase { |
101 public: | |
102 void CreateMojoChannel(IPC::Listener* listener); | |
103 | |
104 protected: | 78 protected: |
105 virtual void SetUp() OVERRIDE { | 79 virtual scoped_ptr<IPC::ChannelFactory> CreateChannelFactory( |
106 IPCTestBase::SetUp(); | 80 const IPC::ChannelHandle& handle, |
| 81 base::TaskRunner* runner) OVERRIDE { |
| 82 return IPC::ChannelMojo::CreateFactory( |
| 83 handle, IPC::Channel::MODE_SERVER, runner); |
107 } | 84 } |
108 | |
109 ListenerThatShouldBeNeverCalled never_called_; | |
110 }; | 85 }; |
111 | 86 |
112 | 87 |
113 void IPCChannelMojoTest::CreateMojoChannel(IPC::Listener* listener) { | |
114 CreateChannel(&never_called_); | |
115 scoped_ptr<IPC::Channel> mojo_channel = IPC::ChannelMojo::Create( | |
116 ReleaseChannel(), IPC::Channel::MODE_SERVER, listener, | |
117 task_runner()).PassAs<IPC::Channel>(); | |
118 SetChannel(mojo_channel.PassAs<IPC::Channel>()); | |
119 } | |
120 | |
121 class TestChannelListenerWithExtraExpectations | 88 class TestChannelListenerWithExtraExpectations |
122 : public IPC::TestChannelListener { | 89 : public IPC::TestChannelListener { |
123 public: | 90 public: |
124 TestChannelListenerWithExtraExpectations() | 91 TestChannelListenerWithExtraExpectations() |
125 : is_connected_called_(false) { | 92 : is_connected_called_(false) { |
126 } | 93 } |
127 | 94 |
128 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE { | 95 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE { |
129 IPC::TestChannelListener::OnChannelConnected(peer_pid); | 96 IPC::TestChannelListener::OnChannelConnected(peer_pid); |
130 EXPECT_TRUE(base::kNullProcessId != peer_pid); | 97 EXPECT_TRUE(base::kNullProcessId != peer_pid); |
131 is_connected_called_ = true; | 98 is_connected_called_ = true; |
132 } | 99 } |
133 | 100 |
134 bool is_connected_called() const { return is_connected_called_; } | 101 bool is_connected_called() const { return is_connected_called_; } |
135 | 102 |
136 private: | 103 private: |
137 bool is_connected_called_; | 104 bool is_connected_called_; |
138 }; | 105 }; |
139 | 106 |
140 TEST_F(IPCChannelMojoTest, ConnectedFromClient) { | 107 TEST_F(IPCChannelMojoTest, ConnectedFromClient) { |
141 Init("IPCChannelMojoTestClient"); | 108 Init("IPCChannelMojoTestClient"); |
142 | 109 |
143 // Set up IPC channel and start client. | 110 // Set up IPC channel and start client. |
144 TestChannelListenerWithExtraExpectations listener; | 111 TestChannelListenerWithExtraExpectations listener; |
145 CreateMojoChannel(&listener); | 112 CreateChannel(&listener); |
146 listener.Init(sender()); | 113 listener.Init(sender()); |
147 ASSERT_TRUE(ConnectChannel()); | 114 ASSERT_TRUE(ConnectChannel()); |
148 ASSERT_TRUE(StartClient()); | 115 ASSERT_TRUE(StartClient()); |
149 | 116 |
150 IPC::TestChannelListener::SendOneMessage( | 117 IPC::TestChannelListener::SendOneMessage( |
151 sender(), "hello from parent"); | 118 sender(), "hello from parent"); |
152 | 119 |
153 base::MessageLoop::current()->Run(); | 120 base::MessageLoop::current()->Run(); |
154 EXPECT_TRUE(base::kNullProcessId != this->channel()->GetPeerPID()); | 121 EXPECT_TRUE(base::kNullProcessId != this->channel()->GetPeerPID()); |
155 | 122 |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 | 196 |
230 private: | 197 private: |
231 IPC::Sender* sender_; | 198 IPC::Sender* sender_; |
232 }; | 199 }; |
233 | 200 |
234 | 201 |
235 TEST_F(IPCChannelMojoTest, SendPlatformHandle) { | 202 TEST_F(IPCChannelMojoTest, SendPlatformHandle) { |
236 Init("IPCChannelMojoTestSendPlatformHandleClient"); | 203 Init("IPCChannelMojoTestSendPlatformHandleClient"); |
237 | 204 |
238 ListenerThatExpectsOK listener; | 205 ListenerThatExpectsOK listener; |
239 CreateMojoChannel(&listener); | 206 CreateChannel(&listener); |
240 ASSERT_TRUE(ConnectChannel()); | 207 ASSERT_TRUE(ConnectChannel()); |
241 ASSERT_TRUE(StartClient()); | 208 ASSERT_TRUE(StartClient()); |
242 | 209 |
243 base::File file(ListenerThatExpectsFile::GetSendingFilePath(), | 210 base::File file(ListenerThatExpectsFile::GetSendingFilePath(), |
244 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE | | 211 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE | |
245 base::File::FLAG_READ); | 212 base::File::FLAG_READ); |
246 ListenerThatExpectsFile::WriteAndSendFile(channel(), file); | 213 ListenerThatExpectsFile::WriteAndSendFile(channel(), file); |
247 base::MessageLoop::current()->Run(); | 214 base::MessageLoop::current()->Run(); |
248 | 215 |
249 this->channel()->Close(); | 216 this->channel()->Close(); |
250 | 217 |
251 EXPECT_TRUE(WaitForClientShutdown()); | 218 EXPECT_TRUE(WaitForClientShutdown()); |
252 DestroyChannel(); | 219 DestroyChannel(); |
253 } | 220 } |
254 | 221 |
255 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestSendPlatformHandleClient) { | 222 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestSendPlatformHandleClient) { |
256 ListenerThatExpectsFile listener; | 223 ListenerThatExpectsFile listener; |
257 ChannelClient client( | 224 ChannelClient client( |
258 &listener, "IPCChannelMojoTestSendPlatformHandleClient"); | 225 &listener, "IPCChannelMojoTestSendPlatformHandleClient"); |
259 client.Connect(); | 226 client.Connect(); |
260 listener.set_sender(client.channel()); | 227 listener.set_sender(client.channel()); |
261 | 228 |
262 base::MessageLoop::current()->Run(); | 229 base::MessageLoop::current()->Run(); |
263 | 230 |
264 return 0; | 231 return 0; |
265 } | 232 } |
266 #endif | 233 #endif |
267 | 234 |
268 } // namespace | 235 } // namespace |
OLD | NEW |