ipc_test_channel_listener.cc 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. #include "ipc/ipc_test_channel_listener.h"
  5. #include "base/run_loop.h"
  6. #include "ipc/ipc_message.h"
  7. #include "ipc/ipc_sender.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. namespace IPC {
  10. // static
  11. void TestChannelListener::SendOneMessage(IPC::Sender* sender,
  12. const char* text) {
  13. static int message_index = 0;
  14. IPC::Message* message = new IPC::Message(0,
  15. 2,
  16. IPC::Message::PRIORITY_NORMAL);
  17. message->WriteInt(message_index++);
  18. message->WriteString(std::string(text));
  19. // Make sure we can handle large messages.
  20. char junk[kLongMessageStringNumBytes];
  21. memset(junk, 'a', sizeof(junk)-1);
  22. junk[sizeof(junk)-1] = 0;
  23. message->WriteString(std::string(junk));
  24. sender->Send(message);
  25. }
  26. bool TestChannelListener::OnMessageReceived(const IPC::Message& message) {
  27. base::PickleIterator iter(message);
  28. int ignored;
  29. EXPECT_TRUE(iter.ReadInt(&ignored));
  30. std::string data;
  31. EXPECT_TRUE(iter.ReadString(&data));
  32. std::string big_string;
  33. EXPECT_TRUE(iter.ReadString(&big_string));
  34. EXPECT_EQ(kLongMessageStringNumBytes - 1, big_string.length());
  35. SendNextMessage();
  36. return true;
  37. }
  38. void TestChannelListener::OnChannelError() {
  39. // There is a race when closing the channel so the last message may be lost.
  40. EXPECT_LE(messages_left_, 1);
  41. base::RunLoop::QuitCurrentWhenIdleDeprecated();
  42. }
  43. void TestChannelListener::SendNextMessage() {
  44. if (--messages_left_ <= 0)
  45. base::RunLoop::QuitCurrentWhenIdleDeprecated();
  46. else
  47. SendOneMessage(sender_, "Foo");
  48. }
  49. }