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 "content/child/shared_memory_received_data_factory.h"
7 #include "base/tuple.h"
8 #include "content/common/resource_messages.h"
9 #include "ipc/ipc_message.h"
10 #include "ipc/ipc_sender.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
19 using ::testing::InSequence
;
20 using ::testing::MockFunction
;
21 using ::testing::Return
;
22 using ::testing::StrictMock
;
24 using Checkpoint
= StrictMock
<MockFunction
<void(int)>>;
25 using ReceivedData
= RequestPeer::ReceivedData
;
27 class MockSender
: public IPC::Sender
{
29 bool Send(IPC::Message
* message
) override
{
31 if (message
->type() == ResourceHostMsg_DataReceived_ACK::ID
) {
32 base::Tuple
<int> args
;
33 ResourceHostMsg_DataReceived_ACK::Read(message
, &args
);
34 result
= SendAck(base::get
<0>(args
));
36 result
= SendOtherwise(message
);
41 MOCK_METHOD1(SendAck
, bool(int));
42 MOCK_METHOD1(SendOtherwise
, bool(IPC::Message
*));
45 class SharedMemoryReceivedDataFactoryTest
: public ::testing::Test
{
47 void SetUp() override
{
48 sender_
.reset(new StrictMock
<MockSender
>);
49 request_id_
= 0xdeadbeaf;
50 memory_
.reset(new base::SharedMemory
);
51 factory_
= make_scoped_refptr(new SharedMemoryReceivedDataFactory(
52 sender_
.get(), request_id_
, memory_
));
53 ASSERT_TRUE(memory_
->CreateAndMapAnonymous(memory_size
));
55 ON_CALL(*sender_
, SendAck(_
)).WillByDefault(Return(true));
56 ON_CALL(*sender_
, SendOtherwise(_
)).WillByDefault(Return(true));
59 static const size_t memory_size
= 4 * 1024;
60 scoped_ptr
<MockSender
> sender_
;
62 linked_ptr
<base::SharedMemory
> memory_
;
63 scoped_refptr
<SharedMemoryReceivedDataFactory
> factory_
;
66 TEST_F(SharedMemoryReceivedDataFactoryTest
, Create
) {
67 Checkpoint checkpoint
;
69 EXPECT_CALL(checkpoint
, Call(0));
70 EXPECT_CALL(*sender_
, SendAck(request_id_
));
71 EXPECT_CALL(checkpoint
, Call(1));
73 scoped_ptr
<ReceivedData
> data
= factory_
->Create(12, 34, 56);
74 const char* memory_start
= static_cast<const char*>(memory_
->memory());
77 EXPECT_EQ(memory_start
+ 12, data
->payload());
78 EXPECT_EQ(34, data
->length());
79 EXPECT_EQ(56, data
->encoded_length());
86 TEST_F(SharedMemoryReceivedDataFactoryTest
, CreateMultiple
) {
87 Checkpoint checkpoint
;
89 EXPECT_CALL(checkpoint
, Call(0));
90 EXPECT_CALL(*sender_
, SendAck(request_id_
));
91 EXPECT_CALL(checkpoint
, Call(1));
92 EXPECT_CALL(*sender_
, SendAck(request_id_
));
93 EXPECT_CALL(checkpoint
, Call(2));
94 EXPECT_CALL(*sender_
, SendAck(request_id_
));
95 EXPECT_CALL(checkpoint
, Call(3));
97 scoped_ptr
<ReceivedData
> data1
= factory_
->Create(0, 1, 1);
98 scoped_ptr
<ReceivedData
> data2
= factory_
->Create(1, 1, 1);
99 scoped_ptr
<ReceivedData
> data3
= factory_
->Create(2, 1, 1);
114 TEST_F(SharedMemoryReceivedDataFactoryTest
, ReclaimOutOfOrder
) {
115 Checkpoint checkpoint
;
117 EXPECT_CALL(checkpoint
, Call(0));
118 EXPECT_CALL(checkpoint
, Call(1));
119 EXPECT_CALL(checkpoint
, Call(2));
120 EXPECT_CALL(*sender_
, SendAck(request_id_
));
121 EXPECT_CALL(*sender_
, SendAck(request_id_
));
122 EXPECT_CALL(*sender_
, SendAck(request_id_
));
123 EXPECT_CALL(checkpoint
, Call(3));
125 scoped_ptr
<ReceivedData
> data1
= factory_
->Create(0, 1, 1);
126 scoped_ptr
<ReceivedData
> data2
= factory_
->Create(1, 1, 1);
127 scoped_ptr
<ReceivedData
> data3
= factory_
->Create(2, 1, 1);
142 TEST_F(SharedMemoryReceivedDataFactoryTest
, ReclaimOutOfOrderPartially
) {
143 Checkpoint checkpoint
;
145 EXPECT_CALL(checkpoint
, Call(0));
146 EXPECT_CALL(checkpoint
, Call(1));
147 EXPECT_CALL(*sender_
, SendAck(request_id_
));
148 EXPECT_CALL(*sender_
, SendAck(request_id_
));
149 EXPECT_CALL(*sender_
, SendAck(request_id_
));
150 EXPECT_CALL(checkpoint
, Call(2));
151 EXPECT_CALL(checkpoint
, Call(3));
152 EXPECT_CALL(*sender_
, SendAck(request_id_
));
153 EXPECT_CALL(*sender_
, SendAck(request_id_
));
154 EXPECT_CALL(*sender_
, SendAck(request_id_
));
155 EXPECT_CALL(checkpoint
, Call(4));
157 scoped_ptr
<ReceivedData
> data1
= factory_
->Create(0, 1, 1);
158 scoped_ptr
<ReceivedData
> data2
= factory_
->Create(1, 1, 1);
159 scoped_ptr
<ReceivedData
> data3
= factory_
->Create(2, 1, 1);
160 scoped_ptr
<ReceivedData
> data4
= factory_
->Create(3, 1, 1);
161 scoped_ptr
<ReceivedData
> data5
= factory_
->Create(4, 1, 1);
162 scoped_ptr
<ReceivedData
> data6
= factory_
->Create(5, 1, 1);
184 TEST_F(SharedMemoryReceivedDataFactoryTest
, Stop
) {
185 Checkpoint checkpoint
;
187 EXPECT_CALL(checkpoint
, Call(0));
188 EXPECT_CALL(checkpoint
, Call(1));
189 EXPECT_CALL(*sender_
, SendAck(request_id_
));
190 EXPECT_CALL(*sender_
, SendAck(request_id_
));
191 EXPECT_CALL(*sender_
, SendAck(request_id_
));
192 EXPECT_CALL(checkpoint
, Call(2));
193 EXPECT_CALL(checkpoint
, Call(3));
195 scoped_ptr
<ReceivedData
> data1
= factory_
->Create(0, 1, 1);
196 scoped_ptr
<ReceivedData
> data2
= factory_
->Create(1, 1, 1);
197 scoped_ptr
<ReceivedData
> data3
= factory_
->Create(2, 1, 1);
198 scoped_ptr
<ReceivedData
> data4
= factory_
->Create(3, 1, 1);
199 scoped_ptr
<ReceivedData
> data5
= factory_
->Create(4, 1, 1);
200 scoped_ptr
<ReceivedData
> data6
= factory_
->Create(5, 1, 1);
224 } // namespace content