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 "base/run_loop.h" 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 SendOneMessage(IPC::Sender * sender,const char * text)15void 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 OnMessageReceived(const IPC::Message & message)35bool TestChannelListener::OnMessageReceived(const IPC::Message& message) { 36 base::PickleIterator iter(message); 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 OnChannelError()50void TestChannelListener::OnChannelError() { 51 // There is a race when closing the channel so the last message may be lost. 52 EXPECT_LE(messages_left_, 1); 53 base::RunLoop::QuitCurrentWhenIdleDeprecated(); 54 } 55 SendNextMessage()56void TestChannelListener::SendNextMessage() { 57 if (--messages_left_ <= 0) 58 base::RunLoop::QuitCurrentWhenIdleDeprecated(); 59 else 60 SendOneMessage(sender_, "Foo"); 61 } 62 63 } 64