morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 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. |
| 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 | |
| 11 | namespace IPC { |
| 12 | |
| 13 | // static |
| 14 | void 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 | |
| 34 | bool TestChannelListener::OnMessageReceived(const IPC::Message& message) { |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 35 | base::PickleIterator iter(message); |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 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 | |
| 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); |
ki.stfu | ad02964 | 2015-10-13 02:26:00 +0900 | [diff] [blame] | 52 | base::MessageLoop::current()->QuitWhenIdle(); |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void TestChannelListener::SendNextMessage() { |
| 56 | if (--messages_left_ <= 0) |
ki.stfu | ad02964 | 2015-10-13 02:26:00 +0900 | [diff] [blame] | 57 | base::MessageLoop::current()->QuitWhenIdle(); |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 58 | else |
| 59 | SendOneMessage(sender_, "Foo"); |
| 60 | } |
| 61 | |
| 62 | } |