Fix translation extraction failure.
[chromium-blink-merge.git] / ipc / ipc_channel_unittest.cc
blob10023bc5d8ca1d7799bd199457e1b57b34561305
1 // Copyright (c) 2012 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.
5 #include "build/build_config.h"
7 #if defined(OS_WIN)
8 #include <windows.h>
9 #endif
11 #include <string>
13 #include "base/pickle.h"
14 #include "base/strings/string16.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "base/threading/thread.h"
17 #include "ipc/ipc_test_base.h"
18 #include "ipc/ipc_test_channel_listener.h"
20 namespace {
22 class IPCChannelTest : public IPCTestBase {
25 #if defined(OS_ANDROID)
26 #define MAYBE_ChannelTest DISABLED_ChannelTest
27 #else
28 #define MAYBE_ChannelTest ChannelTest
29 #endif
30 TEST_F(IPCChannelTest, MAYBE_ChannelTest) {
31 Init("GenericClient");
33 // Set up IPC channel and start client.
34 IPC::TestChannelListener listener;
35 CreateChannel(&listener);
36 listener.Init(sender());
37 ASSERT_TRUE(ConnectChannel());
38 ASSERT_TRUE(StartClient());
40 IPC::TestChannelListener::SendOneMessage(sender(), "hello from parent");
42 // Run message loop.
43 base::MessageLoop::current()->Run();
45 // Close the channel so the client's OnChannelError() gets fired.
46 channel()->Close();
48 EXPECT_TRUE(WaitForClientShutdown());
49 DestroyChannel();
52 // TODO(viettrungluu): Move to a separate IPCChannelWinTest.
53 #if defined(OS_WIN)
54 TEST_F(IPCChannelTest, ChannelTestExistingPipe) {
55 Init("GenericClient");
57 // Create pipe manually using the standard Chromium name and set up IPC
58 // channel.
59 IPC::TestChannelListener listener;
60 std::string name("\\\\.\\pipe\\chrome.");
61 name.append(GetChannelName("GenericClient"));
62 HANDLE pipe = CreateNamedPipeA(name.c_str(),
63 PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED |
64 FILE_FLAG_FIRST_PIPE_INSTANCE,
65 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE,
67 4096,
68 4096,
69 5000,
70 NULL);
71 CreateChannelFromChannelHandle(IPC::ChannelHandle(pipe), &listener);
72 CloseHandle(pipe); // The channel duplicates the handle.
73 listener.Init(sender());
75 // Connect to channel and start client.
76 ASSERT_TRUE(ConnectChannel());
77 ASSERT_TRUE(StartClient());
79 IPC::TestChannelListener::SendOneMessage(sender(), "hello from parent");
81 // Run message loop.
82 base::MessageLoop::current()->Run();
84 // Close the channel so the client's OnChannelError() gets fired.
85 channel()->Close();
87 EXPECT_TRUE(WaitForClientShutdown());
88 DestroyChannel();
90 #endif // defined (OS_WIN)
92 #if defined(OS_ANDROID)
93 #define MAYBE_ChannelProxyTest DISABLED_ChannelProxyTest
94 #else
95 #define MAYBE_ChannelProxyTest ChannelProxyTest
96 #endif
97 TEST_F(IPCChannelTest, MAYBE_ChannelProxyTest) {
98 Init("GenericClient");
100 base::Thread thread("ChannelProxyTestServer");
101 base::Thread::Options options;
102 options.message_loop_type = base::MessageLoop::TYPE_IO;
103 thread.StartWithOptions(options);
105 // Set up IPC channel proxy.
106 IPC::TestChannelListener listener;
107 CreateChannelProxy(&listener, thread.task_runner().get());
108 listener.Init(sender());
110 ASSERT_TRUE(StartClient());
112 IPC::TestChannelListener::SendOneMessage(sender(), "hello from parent");
114 // Run message loop.
115 base::MessageLoop::current()->Run();
117 EXPECT_TRUE(WaitForClientShutdown());
119 // Destroy the channel proxy before shutting down the thread.
120 DestroyChannelProxy();
121 thread.Stop();
124 class ChannelListenerWithOnConnectedSend : public IPC::TestChannelListener {
125 public:
126 ChannelListenerWithOnConnectedSend() {}
127 ~ChannelListenerWithOnConnectedSend() override {}
129 void OnChannelConnected(int32_t peer_pid) override {
130 SendNextMessage();
134 #if defined(OS_WIN) || defined(OS_ANDROID)
135 // Acting flakey in Windows. http://crbug.com/129595
136 #define MAYBE_SendMessageInChannelConnected DISABLED_SendMessageInChannelConnected
137 #else
138 #define MAYBE_SendMessageInChannelConnected SendMessageInChannelConnected
139 #endif
140 // This tests the case of a listener sending back an event in its
141 // OnChannelConnected handler.
142 TEST_F(IPCChannelTest, MAYBE_SendMessageInChannelConnected) {
143 Init("GenericClient");
145 // Set up IPC channel and start client.
146 ChannelListenerWithOnConnectedSend listener;
147 CreateChannel(&listener);
148 listener.Init(sender());
149 ASSERT_TRUE(ConnectChannel());
150 ASSERT_TRUE(StartClient());
152 IPC::TestChannelListener::SendOneMessage(sender(), "hello from parent");
154 // Run message loop.
155 base::MessageLoop::current()->Run();
157 // Close the channel so the client's OnChannelError() gets fired.
158 channel()->Close();
160 EXPECT_TRUE(WaitForClientShutdown());
161 DestroyChannel();
164 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(GenericClient) {
165 base::MessageLoopForIO main_message_loop;
166 IPC::TestChannelListener listener;
168 // Set up IPC channel.
169 scoped_ptr<IPC::Channel> channel(IPC::Channel::CreateClient(
170 IPCTestBase::GetChannelName("GenericClient"), &listener, nullptr));
171 CHECK(channel->Connect());
172 listener.Init(channel.get());
173 IPC::TestChannelListener::SendOneMessage(channel.get(), "hello from child");
175 base::MessageLoop::current()->Run();
176 return 0;
179 } // namespace