Update broken references to image assets
[chromium-blink-merge.git] / ipc / ipc_channel_reader_unittest.cc
blobcbcd5dc5189cafa9a2410922e9b1216e72ada5bd
1 // Copyright 2015 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.
5 #include "build/build_config.h"
7 #include <set>
9 #include "ipc/attachment_broker.h"
10 #include "ipc/brokerable_attachment.h"
11 #include "ipc/ipc_channel_reader.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 #if USE_ATTACHMENT_BROKER
15 namespace IPC {
16 namespace internal {
18 namespace {
20 class MockAttachment : public BrokerableAttachment {
21 public:
22 MockAttachment(int internal_state) : internal_state_(internal_state) {}
23 MockAttachment(BrokerableAttachment::AttachmentId id)
24 : BrokerableAttachment(id, true), internal_state_(-1) {}
26 void PopulateWithAttachment(const BrokerableAttachment* attachment) override {
27 const MockAttachment* mock_attachment =
28 static_cast<const MockAttachment*>(attachment);
29 internal_state_ = mock_attachment->internal_state_;
32 #if defined(OS_POSIX)
33 base::PlatformFile TakePlatformFile() override {
34 return base::PlatformFile();
36 #endif // OS_POSIX
38 BrokerableType GetBrokerableType() const override { return WIN_HANDLE; }
40 private:
41 ~MockAttachment() override {}
42 // Internal state differentiates MockAttachments.
43 int internal_state_;
46 class MockAttachmentBroker : public AttachmentBroker {
47 public:
48 typedef std::set<scoped_refptr<BrokerableAttachment>> AttachmentSet;
50 bool SendAttachmentToProcess(const BrokerableAttachment* attachment,
51 base::ProcessId destination_process) override {
52 return false;
55 bool OnMessageReceived(const Message& message) override { return false; }
57 void AddAttachment(scoped_refptr<BrokerableAttachment> attachment) {
58 get_attachments()->push_back(attachment);
59 NotifyObservers(attachment->GetIdentifier());
63 class MockChannelReader : public ChannelReader {
64 public:
65 MockChannelReader()
66 : ChannelReader(nullptr), last_dispatched_message_(nullptr) {}
68 ReadState ReadData(char* buffer, int buffer_len, int* bytes_read) override {
69 return READ_FAILED;
72 bool ShouldDispatchInputMessage(Message* msg) override { return true; }
74 bool GetNonBrokeredAttachments(Message* msg) override { return true; }
76 bool DidEmptyInputBuffers() override { return true; }
78 void HandleInternalMessage(const Message& msg) override {}
80 void DispatchMessage(Message* m) override { last_dispatched_message_ = m; }
82 base::ProcessId GetSenderPID() override { return base::kNullProcessId; }
84 bool IsAttachmentBrokerEndpoint() override { return false; }
86 AttachmentBroker* GetAttachmentBroker() override { return broker_; }
88 // This instance takes ownership of |m|.
89 void AddMessageForDispatch(Message* m) {
90 get_queued_messages()->push_back(m);
93 Message* get_last_dispatched_message() { return last_dispatched_message_; }
95 void set_broker(AttachmentBroker* broker) { broker_ = broker; }
97 private:
98 Message* last_dispatched_message_;
99 AttachmentBroker* broker_;
102 } // namespace
104 TEST(ChannelReaderTest, AttachmentAlreadyBrokered) {
105 MockAttachmentBroker broker;
106 MockChannelReader reader;
107 reader.set_broker(&broker);
108 scoped_refptr<MockAttachment> attachment(new MockAttachment(5));
109 broker.AddAttachment(attachment);
111 Message* m = new Message;
112 MockAttachment* needs_brokering_attachment =
113 new MockAttachment(attachment->GetIdentifier());
114 EXPECT_TRUE(m->WriteAttachment(needs_brokering_attachment));
115 reader.AddMessageForDispatch(m);
116 EXPECT_EQ(ChannelReader::DISPATCH_FINISHED, reader.DispatchMessages());
117 EXPECT_EQ(m, reader.get_last_dispatched_message());
120 TEST(ChannelReaderTest, AttachmentNotYetBrokered) {
121 MockAttachmentBroker broker;
122 MockChannelReader reader;
123 reader.set_broker(&broker);
124 scoped_refptr<MockAttachment> attachment(new MockAttachment(5));
126 Message* m = new Message;
127 MockAttachment* needs_brokering_attachment =
128 new MockAttachment(attachment->GetIdentifier());
129 EXPECT_TRUE(m->WriteAttachment(needs_brokering_attachment));
130 reader.AddMessageForDispatch(m);
131 EXPECT_EQ(ChannelReader::DISPATCH_WAITING_ON_BROKER,
132 reader.DispatchMessages());
133 EXPECT_EQ(nullptr, reader.get_last_dispatched_message());
135 broker.AddAttachment(attachment);
136 EXPECT_EQ(m, reader.get_last_dispatched_message());
139 } // namespace internal
140 } // namespace IPC
141 #endif // USE_ATTACHMENT_BROKER