bluetooth: browser-side implementation of writeValue.
[chromium-blink-merge.git] / content / child / shared_memory_received_data_factory_unittest.cc
blobb197d4fdfd3ca0727808f5b10b3a4d1d71b09082
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"
14 namespace content {
16 namespace {
18 using ::testing::_;
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 {
28 public:
29 bool Send(IPC::Message* message) override {
30 bool result = false;
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));
35 } else {
36 result = SendOtherwise(message);
38 delete message;
39 return result;
41 MOCK_METHOD1(SendAck, bool(int));
42 MOCK_METHOD1(SendOtherwise, bool(IPC::Message*));
45 class SharedMemoryReceivedDataFactoryTest : public ::testing::Test {
46 protected:
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_;
61 int request_id_;
62 linked_ptr<base::SharedMemory> memory_;
63 scoped_refptr<SharedMemoryReceivedDataFactory> factory_;
66 TEST_F(SharedMemoryReceivedDataFactoryTest, Create) {
67 Checkpoint checkpoint;
68 InSequence s;
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());
76 ASSERT_TRUE(data);
77 EXPECT_EQ(memory_start + 12, data->payload());
78 EXPECT_EQ(34, data->length());
79 EXPECT_EQ(56, data->encoded_length());
81 checkpoint.Call(0);
82 data.reset();
83 checkpoint.Call(1);
86 TEST_F(SharedMemoryReceivedDataFactoryTest, CreateMultiple) {
87 Checkpoint checkpoint;
88 InSequence s;
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);
101 EXPECT_TRUE(data1);
102 EXPECT_TRUE(data2);
103 EXPECT_TRUE(data3);
105 checkpoint.Call(0);
106 data1.reset();
107 checkpoint.Call(1);
108 data2.reset();
109 checkpoint.Call(2);
110 data3.reset();
111 checkpoint.Call(3);
114 TEST_F(SharedMemoryReceivedDataFactoryTest, ReclaimOutOfOrder) {
115 Checkpoint checkpoint;
116 InSequence s;
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);
129 EXPECT_TRUE(data1);
130 EXPECT_TRUE(data2);
131 EXPECT_TRUE(data3);
133 checkpoint.Call(0);
134 data3.reset();
135 checkpoint.Call(1);
136 data2.reset();
137 checkpoint.Call(2);
138 data1.reset();
139 checkpoint.Call(3);
142 TEST_F(SharedMemoryReceivedDataFactoryTest, ReclaimOutOfOrderPartially) {
143 Checkpoint checkpoint;
144 InSequence s;
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);
164 EXPECT_TRUE(data1);
165 EXPECT_TRUE(data2);
166 EXPECT_TRUE(data3);
167 EXPECT_TRUE(data4);
168 EXPECT_TRUE(data5);
169 EXPECT_TRUE(data6);
171 checkpoint.Call(0);
172 data3.reset();
173 data6.reset();
174 data2.reset();
175 checkpoint.Call(1);
176 data1.reset();
177 checkpoint.Call(2);
178 data5.reset();
179 checkpoint.Call(3);
180 data4.reset();
181 checkpoint.Call(4);
184 TEST_F(SharedMemoryReceivedDataFactoryTest, Stop) {
185 Checkpoint checkpoint;
186 InSequence s;
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);
202 EXPECT_TRUE(data1);
203 EXPECT_TRUE(data2);
204 EXPECT_TRUE(data3);
205 EXPECT_TRUE(data4);
206 EXPECT_TRUE(data5);
207 EXPECT_TRUE(data6);
209 checkpoint.Call(0);
210 data3.reset();
211 data6.reset();
212 data2.reset();
213 checkpoint.Call(1);
214 data1.reset();
215 checkpoint.Call(2);
216 factory_->Stop();
217 data5.reset();
218 data4.reset();
219 checkpoint.Call(3);
222 } // namespace
224 } // namespace content