blob: e98f6b7ee156cc12c979321711493f32f40c76d0 [file] [log] [blame]
morrita@chromium.org15996aa2014-08-05 08:44:17 +09001// 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.
4
5#include "ipc/ipc_test_channel_listener.h"
6
7#include "ipc/ipc_message.h"
8#include "ipc/ipc_sender.h"
9#include "testing/gtest/include/gtest/gtest.h"
10
11namespace IPC {
12
13// static
14void TestChannelListener::SendOneMessage(IPC::Sender* sender,
15 const char* text) {
16 static int message_index = 0;
17
18 IPC::Message* message = new IPC::Message(0,
19 2,
20 IPC::Message::PRIORITY_NORMAL);
21 message->WriteInt(message_index++);
22 message->WriteString(std::string(text));
23
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));
29
30 sender->Send(message);
31}
32
33
34bool TestChannelListener::OnMessageReceived(const IPC::Message& message) {
35 PickleIterator iter(message);
36
37 int ignored;
38 EXPECT_TRUE(iter.ReadInt(&ignored));
39 std::string data;
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());
44
45 SendNextMessage();
46 return true;
47}
48
49void 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();
53}
54
55void TestChannelListener::SendNextMessage() {
56 if (--messages_left_ <= 0)
57 base::MessageLoop::current()->Quit();
58 else
59 SendOneMessage(sender_, "Foo");
60}
61
62}