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 | |
Gabriel Charette | 274bec8 | 2017-07-26 21:36:23 +0900 | [diff] [blame] | 7 | #include "base/run_loop.h" |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 8 | #include "ipc/ipc_message.h" |
| 9 | #include "ipc/ipc_sender.h" |
| 10 | #include "testing/gtest/include/gtest/gtest.h" |
| 11 | |
| 12 | namespace IPC { |
| 13 | |
| 14 | // static |
| 15 | void TestChannelListener::SendOneMessage(IPC::Sender* sender, |
| 16 | const char* text) { |
| 17 | static int message_index = 0; |
| 18 | |
| 19 | IPC::Message* message = new IPC::Message(0, |
| 20 | 2, |
| 21 | IPC::Message::PRIORITY_NORMAL); |
| 22 | message->WriteInt(message_index++); |
| 23 | message->WriteString(std::string(text)); |
| 24 | |
| 25 | // Make sure we can handle large messages. |
| 26 | char junk[kLongMessageStringNumBytes]; |
| 27 | memset(junk, 'a', sizeof(junk)-1); |
| 28 | junk[sizeof(junk)-1] = 0; |
| 29 | message->WriteString(std::string(junk)); |
| 30 | |
| 31 | sender->Send(message); |
| 32 | } |
| 33 | |
| 34 | |
| 35 | bool TestChannelListener::OnMessageReceived(const IPC::Message& message) { |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 36 | base::PickleIterator iter(message); |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 37 | |
| 38 | int ignored; |
| 39 | EXPECT_TRUE(iter.ReadInt(&ignored)); |
| 40 | std::string data; |
| 41 | EXPECT_TRUE(iter.ReadString(&data)); |
| 42 | std::string big_string; |
| 43 | EXPECT_TRUE(iter.ReadString(&big_string)); |
| 44 | EXPECT_EQ(kLongMessageStringNumBytes - 1, big_string.length()); |
| 45 | |
| 46 | SendNextMessage(); |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | void TestChannelListener::OnChannelError() { |
| 51 | // There is a race when closing the channel so the last message may be lost. |
| 52 | EXPECT_LE(messages_left_, 1); |
Gabriel Charette | 274bec8 | 2017-07-26 21:36:23 +0900 | [diff] [blame] | 53 | base::RunLoop::QuitCurrentWhenIdleDeprecated(); |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void TestChannelListener::SendNextMessage() { |
| 57 | if (--messages_left_ <= 0) |
Gabriel Charette | 274bec8 | 2017-07-26 21:36:23 +0900 | [diff] [blame] | 58 | base::RunLoop::QuitCurrentWhenIdleDeprecated(); |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 59 | else |
| 60 | SendOneMessage(sender_, "Foo"); |
| 61 | } |
| 62 | |
| 63 | } |