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.
5 #include "ipc/ipc_test_channel_listener.h"
7 #include "ipc/ipc_message.h"
8 #include "ipc/ipc_sender.h"
9 #include "testing/gtest/include/gtest/gtest.h"
14 void TestChannelListener::SendOneMessage(IPC::Sender
* sender
,
16 static int message_index
= 0;
18 IPC::Message
* message
= new IPC::Message(0,
20 IPC::Message::PRIORITY_NORMAL
);
21 message
->WriteInt(message_index
++);
22 message
->WriteString(std::string(text
));
24 // Make sure we can handle large messages.
25 char junk
[kLongMessageStringNumBytes
];
26 memset(junk
, 'a', sizeof(junk
)-1);
27 junk
[sizeof(junk
)-1] = 0;
28 message
->WriteString(std::string(junk
));
30 sender
->Send(message
);
34 bool TestChannelListener::OnMessageReceived(const IPC::Message
& message
) {
35 base::PickleIterator
iter(message
);
38 EXPECT_TRUE(iter
.ReadInt(&ignored
));
40 EXPECT_TRUE(iter
.ReadString(&data
));
41 std::string big_string
;
42 EXPECT_TRUE(iter
.ReadString(&big_string
));
43 EXPECT_EQ(kLongMessageStringNumBytes
- 1, big_string
.length());
49 void TestChannelListener::OnChannelError() {
50 // There is a race when closing the channel so the last message may be lost.
51 EXPECT_LE(messages_left_
, 1);
52 base::MessageLoop::current()->Quit();
55 void TestChannelListener::SendNextMessage() {
56 if (--messages_left_
<= 0)
57 base::MessageLoop::current()->Quit();
59 SendOneMessage(sender_
, "Foo");