Extract the remote calls into the class (1/2)
[chromium-blink-merge.git] / cc / quads / list_container_unittest.cc
blob8b68852ed0ed9ac62908300b40da16f52f2b3d28
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.
5 #include "cc/quads/list_container.h"
7 #include <vector>
8 #include "cc/quads/draw_quad.h"
9 #include "cc/quads/largest_draw_quad.h"
10 #include "cc/quads/render_pass_draw_quad.h"
11 #include "cc/quads/shared_quad_state.h"
12 #include "cc/quads/stream_video_draw_quad.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace cc {
17 namespace {
19 int kMagicNumberToUseForDrawQuadOne = 42;
20 int kMagicNumberToUseForDrawQuadTwo = 314;
22 bool isConstSharedQuadStatePointer(const SharedQuadState* ptr) {
23 return true;
26 bool isConstSharedQuadStatePointer(SharedQuadState* ptr) {
27 return false;
30 class SimpleDrawQuad : public DrawQuad {
31 public:
32 virtual ~SimpleDrawQuad() {}
33 virtual void IterateResources(
34 const ResourceIteratorCallback& callback) override {}
36 void set_value(int val) { value = val; }
37 int get_value() { return value; }
38 virtual void ExtendValue(base::debug::TracedValue* value) const override {}
40 private:
41 int value;
44 class SimpleDrawQuadConstructMagicNumberOne : public SimpleDrawQuad {
45 public:
46 SimpleDrawQuadConstructMagicNumberOne() : SimpleDrawQuad() {
47 set_value(kMagicNumberToUseForDrawQuadOne);
51 class SimpleDrawQuadConstructMagicNumberTwo : public SimpleDrawQuad {
52 public:
53 SimpleDrawQuadConstructMagicNumberTwo() : SimpleDrawQuad() {
54 set_value(kMagicNumberToUseForDrawQuadTwo);
58 class MockDrawQuad : public DrawQuad {
59 public:
60 virtual ~MockDrawQuad() { Destruct(); }
61 virtual void IterateResources(
62 const ResourceIteratorCallback& callback) override {}
63 virtual void ExtendValue(base::debug::TracedValue* value) const override {}
64 MOCK_METHOD0(Destruct, void());
67 TEST(ListContainerTest, ConstructorCalledInAllocateAndConstruct) {
68 ListContainer<DrawQuad> list(sizeof(kLargestDrawQuad));
70 size_t size = 2;
71 SimpleDrawQuadConstructMagicNumberOne* dq_1 =
72 list.AllocateAndConstruct<SimpleDrawQuadConstructMagicNumberOne>();
73 SimpleDrawQuadConstructMagicNumberTwo* dq_2 =
74 list.AllocateAndConstruct<SimpleDrawQuadConstructMagicNumberTwo>();
76 EXPECT_EQ(size, list.size());
77 EXPECT_EQ(dq_1, list.front());
78 EXPECT_EQ(dq_2, list.back());
80 EXPECT_EQ(kMagicNumberToUseForDrawQuadOne, dq_1->get_value());
81 EXPECT_EQ(kMagicNumberToUseForDrawQuadTwo, dq_2->get_value());
84 TEST(ListContainerTest, DestructorCalled) {
85 ListContainer<DrawQuad> list(sizeof(kLargestDrawQuad));
87 size_t size = 1;
88 MockDrawQuad* dq_1 = list.AllocateAndConstruct<MockDrawQuad>();
90 EXPECT_CALL(*dq_1, Destruct());
91 EXPECT_EQ(size, list.size());
92 EXPECT_EQ(dq_1, list.front());
95 TEST(ListContainerTest, DestructorCalledOnceWhenClear) {
96 ListContainer<DrawQuad> list(sizeof(kLargestDrawQuad));
97 size_t size = 1;
98 MockDrawQuad* dq_1 = list.AllocateAndConstruct<MockDrawQuad>();
100 EXPECT_EQ(size, list.size());
101 EXPECT_EQ(dq_1, list.front());
103 // Make sure destructor is called once during clear, and won't be called
104 // again.
105 testing::MockFunction<void()> separator;
107 testing::InSequence s;
108 EXPECT_CALL(*dq_1, Destruct());
109 EXPECT_CALL(separator, Call());
110 EXPECT_CALL(*dq_1, Destruct()).Times(0);
113 list.clear();
114 separator.Call();
117 TEST(ListContainerTest, DestructorCalledOnceWhenErase) {
118 ListContainer<DrawQuad> list(sizeof(kLargestDrawQuad));
119 size_t size = 1;
120 MockDrawQuad* dq_1 = list.AllocateAndConstruct<MockDrawQuad>();
122 EXPECT_EQ(size, list.size());
123 EXPECT_EQ(dq_1, list.front());
125 // Make sure destructor is called once during clear, and won't be called
126 // again.
127 testing::MockFunction<void()> separator;
129 testing::InSequence s;
130 EXPECT_CALL(*dq_1, Destruct());
131 EXPECT_CALL(separator, Call());
132 EXPECT_CALL(*dq_1, Destruct()).Times(0);
135 list.EraseAndInvalidateAllPointers(list.begin());
136 separator.Call();
139 TEST(ListContainerTest, SimpleIndexAccessSharedQuadState) {
140 ListContainer<SharedQuadState> list;
142 size_t size = 3;
143 SharedQuadState* sqs_1 = list.AllocateAndConstruct<SharedQuadState>();
144 SharedQuadState* sqs_2 = list.AllocateAndConstruct<SharedQuadState>();
145 SharedQuadState* sqs_3 = list.AllocateAndConstruct<SharedQuadState>();
147 EXPECT_EQ(size, list.size());
148 EXPECT_EQ(sqs_1, list.front());
149 EXPECT_EQ(sqs_3, list.back());
150 EXPECT_EQ(list.front(), list.ElementAt(0));
151 EXPECT_EQ(sqs_2, list.ElementAt(1));
152 EXPECT_EQ(list.back(), list.ElementAt(2));
155 TEST(ListContainerTest, SimpleInsertionSharedQuadState) {
156 ListContainer<SharedQuadState> list;
158 size_t size = 3;
159 SharedQuadState* sqs_1 = list.AllocateAndConstruct<SharedQuadState>();
160 list.AllocateAndConstruct<SharedQuadState>();
161 SharedQuadState* sqs_3 = list.AllocateAndConstruct<SharedQuadState>();
163 EXPECT_EQ(size, list.size());
164 EXPECT_EQ(sqs_1, list.front());
165 EXPECT_EQ(sqs_3, list.back());
168 TEST(ListContainerTest, SimpleInsertionAndClearSharedQuadState) {
169 ListContainer<SharedQuadState> list;
170 EXPECT_TRUE(list.empty());
171 EXPECT_EQ(0u, list.size());
173 size_t size = 3;
174 SharedQuadState* sqs_1 = list.AllocateAndConstruct<SharedQuadState>();
175 list.AllocateAndConstruct<SharedQuadState>();
176 SharedQuadState* sqs_3 = list.AllocateAndConstruct<SharedQuadState>();
178 EXPECT_EQ(size, list.size());
179 EXPECT_EQ(sqs_1, list.front());
180 EXPECT_EQ(sqs_3, list.back());
181 EXPECT_FALSE(list.empty());
183 list.clear();
184 EXPECT_TRUE(list.empty());
185 EXPECT_EQ(0u, list.size());
188 TEST(ListContainerTest, SimpleInsertionClearAndInsertAgainSharedQuadState) {
189 ListContainer<SharedQuadState> list;
190 EXPECT_TRUE(list.empty());
191 EXPECT_EQ(0u, list.size());
193 size_t size = 2;
194 SharedQuadState* sqs_front = list.AllocateAndConstruct<SharedQuadState>();
195 SharedQuadState* sqs_back = list.AllocateAndConstruct<SharedQuadState>();
197 EXPECT_EQ(size, list.size());
198 EXPECT_EQ(sqs_front, list.front());
199 EXPECT_EQ(sqs_back, list.back());
200 EXPECT_FALSE(list.empty());
202 list.clear();
203 EXPECT_TRUE(list.empty());
204 EXPECT_EQ(0u, list.size());
206 size = 3;
207 sqs_front = list.AllocateAndConstruct<SharedQuadState>();
208 list.AllocateAndConstruct<SharedQuadState>();
209 sqs_back = list.AllocateAndConstruct<SharedQuadState>();
211 EXPECT_EQ(size, list.size());
212 EXPECT_EQ(sqs_front, list.front());
213 EXPECT_EQ(sqs_back, list.back());
214 EXPECT_FALSE(list.empty());
217 // This test is used to test when there is more than one allocation needed
218 // for, ListContainer can still perform like normal vector.
219 TEST(ListContainerTest,
220 SimpleInsertionTriggerMoreThanOneAllocationSharedQuadState) {
221 ListContainer<SharedQuadState> list(sizeof(SharedQuadState), 2);
222 std::vector<SharedQuadState*> sqs_list;
223 size_t size = 10;
224 for (size_t i = 0; i < size; ++i) {
225 sqs_list.push_back(list.AllocateAndConstruct<SharedQuadState>());
227 EXPECT_EQ(size, list.size());
229 ListContainer<SharedQuadState>::Iterator iter = list.begin();
230 for (std::vector<SharedQuadState*>::const_iterator sqs_iter =
231 sqs_list.begin();
232 sqs_iter != sqs_list.end();
233 ++sqs_iter) {
234 EXPECT_EQ(*sqs_iter, &*iter);
235 ++iter;
239 TEST(ListContainerTest,
240 CorrectAllocationSizeForMoreThanOneAllocationSharedQuadState) {
241 // Constructor sets the allocation size to 2. Every time ListContainer needs
242 // to allocate again, it doubles allocation size. In this test, 10 elements is
243 // needed, thus ListContainerShould allocate spaces 2, 4 and 8 elements.
244 ListContainer<SharedQuadState> list(sizeof(SharedQuadState), 2);
245 std::vector<SharedQuadState*> sqs_list;
246 size_t size = 10;
247 for (size_t i = 0; i < size; ++i) {
248 // Before asking for a new element, space available without another
249 // allocation follows.
250 switch (i) {
251 case 2:
252 case 6:
253 EXPECT_EQ(0u, list.AvailableSizeWithoutAnotherAllocationForTesting());
254 break;
255 case 1:
256 case 5:
257 EXPECT_EQ(1u, list.AvailableSizeWithoutAnotherAllocationForTesting());
258 break;
259 case 0:
260 case 4:
261 EXPECT_EQ(2u, list.AvailableSizeWithoutAnotherAllocationForTesting());
262 break;
263 case 3:
264 EXPECT_EQ(3u, list.AvailableSizeWithoutAnotherAllocationForTesting());
265 break;
266 case 9:
267 EXPECT_EQ(5u, list.AvailableSizeWithoutAnotherAllocationForTesting());
268 break;
269 case 8:
270 EXPECT_EQ(6u, list.AvailableSizeWithoutAnotherAllocationForTesting());
271 break;
272 case 7:
273 EXPECT_EQ(7u, list.AvailableSizeWithoutAnotherAllocationForTesting());
274 break;
275 default:
276 break;
278 sqs_list.push_back(list.AllocateAndConstruct<SharedQuadState>());
279 // After asking for a new element, space available without another
280 // allocation follows.
281 switch (i) {
282 case 1:
283 case 5:
284 EXPECT_EQ(0u, list.AvailableSizeWithoutAnotherAllocationForTesting());
285 break;
286 case 0:
287 case 4:
288 EXPECT_EQ(1u, list.AvailableSizeWithoutAnotherAllocationForTesting());
289 break;
290 case 3:
291 EXPECT_EQ(2u, list.AvailableSizeWithoutAnotherAllocationForTesting());
292 break;
293 case 2:
294 EXPECT_EQ(3u, list.AvailableSizeWithoutAnotherAllocationForTesting());
295 break;
296 case 9:
297 EXPECT_EQ(4u, list.AvailableSizeWithoutAnotherAllocationForTesting());
298 break;
299 case 8:
300 EXPECT_EQ(5u, list.AvailableSizeWithoutAnotherAllocationForTesting());
301 break;
302 case 7:
303 EXPECT_EQ(6u, list.AvailableSizeWithoutAnotherAllocationForTesting());
304 break;
305 case 6:
306 EXPECT_EQ(7u, list.AvailableSizeWithoutAnotherAllocationForTesting());
307 break;
308 default:
309 break;
312 EXPECT_EQ(size, list.size());
314 ListContainer<SharedQuadState>::Iterator iter = list.begin();
315 for (std::vector<SharedQuadState*>::const_iterator sqs_iter =
316 sqs_list.begin();
317 sqs_iter != sqs_list.end();
318 ++sqs_iter) {
319 EXPECT_EQ(*sqs_iter, &*iter);
320 ++iter;
324 TEST(ListContainerTest, SimpleIterationSharedQuadState) {
325 ListContainer<SharedQuadState> list;
326 std::vector<SharedQuadState*> sqs_list;
327 size_t size = 10;
328 for (size_t i = 0; i < size; ++i) {
329 sqs_list.push_back(list.AllocateAndConstruct<SharedQuadState>());
331 EXPECT_EQ(size, list.size());
333 size_t num_iters_in_list = 0;
335 std::vector<SharedQuadState*>::const_iterator sqs_iter = sqs_list.begin();
336 for (ListContainer<SharedQuadState>::Iterator iter = list.begin();
337 iter != list.end();
338 ++iter) {
339 EXPECT_EQ(*sqs_iter, &*iter);
340 ++num_iters_in_list;
341 ++sqs_iter;
345 size_t num_iters_in_vector = 0;
347 ListContainer<SharedQuadState>::Iterator iter = list.begin();
348 for (std::vector<SharedQuadState*>::const_iterator sqs_iter =
349 sqs_list.begin();
350 sqs_iter != sqs_list.end();
351 ++sqs_iter) {
352 EXPECT_EQ(*sqs_iter, &*iter);
353 ++num_iters_in_vector;
354 ++iter;
358 EXPECT_EQ(num_iters_in_vector, num_iters_in_list);
361 TEST(ListContainerTest, SimpleConstIteratorIterationSharedQuadState) {
362 ListContainer<SharedQuadState> list;
363 std::vector<const SharedQuadState*> sqs_list;
364 size_t size = 10;
365 for (size_t i = 0; i < size; ++i) {
366 sqs_list.push_back(list.AllocateAndConstruct<SharedQuadState>());
368 EXPECT_EQ(size, list.size());
371 std::vector<const SharedQuadState*>::const_iterator sqs_iter =
372 sqs_list.begin();
373 for (ListContainer<SharedQuadState>::ConstIterator iter = list.begin();
374 iter != list.end();
375 ++iter) {
376 EXPECT_TRUE(isConstSharedQuadStatePointer(&*iter));
377 EXPECT_EQ(*sqs_iter, &*iter);
378 ++sqs_iter;
383 std::vector<const SharedQuadState*>::const_iterator sqs_iter =
384 sqs_list.begin();
385 for (ListContainer<SharedQuadState>::Iterator iter = list.begin();
386 iter != list.end();
387 ++iter) {
388 EXPECT_FALSE(isConstSharedQuadStatePointer(&*iter));
389 EXPECT_EQ(*sqs_iter, &*iter);
390 ++sqs_iter;
395 ListContainer<SharedQuadState>::ConstIterator iter = list.begin();
396 for (std::vector<const SharedQuadState*>::const_iterator sqs_iter =
397 sqs_list.begin();
398 sqs_iter != sqs_list.end();
399 ++sqs_iter) {
400 EXPECT_EQ(*sqs_iter, &*iter);
401 ++iter;
406 TEST(ListContainerTest, SimpleReverseInsertionSharedQuadState) {
407 ListContainer<SharedQuadState> list;
408 std::vector<SharedQuadState*> sqs_list;
409 size_t size = 10;
410 for (size_t i = 0; i < size; ++i) {
411 sqs_list.push_back(list.AllocateAndConstruct<SharedQuadState>());
413 EXPECT_EQ(size, list.size());
416 std::vector<SharedQuadState*>::const_reverse_iterator sqs_iter =
417 sqs_list.rbegin();
418 for (ListContainer<SharedQuadState>::ReverseIterator iter = list.rbegin();
419 iter != list.rend();
420 ++iter) {
421 EXPECT_EQ(*sqs_iter, &(*iter));
422 ++sqs_iter;
427 ListContainer<SharedQuadState>::ReverseIterator iter = list.rbegin();
428 for (std::vector<SharedQuadState*>::reverse_iterator sqs_iter =
429 sqs_list.rbegin();
430 sqs_iter != sqs_list.rend();
431 ++sqs_iter) {
432 EXPECT_EQ(*sqs_iter, &(*iter));
433 ++iter;
438 TEST(ListContainerTest, SimpleDeletion) {
439 ListContainer<DrawQuad> list(sizeof(kLargestDrawQuad));
440 std::vector<SimpleDrawQuad*> sdq_list;
441 size_t size = 10;
442 for (size_t i = 0; i < size; ++i) {
443 sdq_list.push_back(list.AllocateAndConstruct<SimpleDrawQuad>());
444 sdq_list.back()->set_value(i);
446 EXPECT_EQ(size, list.size());
448 list.EraseAndInvalidateAllPointers(list.begin());
449 --size;
450 EXPECT_EQ(size, list.size());
451 int i = 1;
452 for (ListContainer<DrawQuad>::Iterator iter = list.begin();
453 iter != list.end();
454 ++iter) {
455 EXPECT_EQ(i, static_cast<SimpleDrawQuad*>(&*iter)->get_value());
456 ++i;
460 TEST(ListContainerTest, SimpleIterationAndManipulation) {
461 ListContainer<DrawQuad> list(sizeof(kLargestDrawQuad));
462 std::vector<SimpleDrawQuad*> sdq_list;
463 size_t size = 10;
464 for (size_t i = 0; i < size; ++i) {
465 SimpleDrawQuad* simple_dq = list.AllocateAndConstruct<SimpleDrawQuad>();
466 sdq_list.push_back(simple_dq);
468 EXPECT_EQ(size, list.size());
470 ListContainer<DrawQuad>::Iterator iter = list.begin();
471 for (int i = 0; i < 10; ++i) {
472 static_cast<SimpleDrawQuad*>(&*iter)->set_value(i);
473 ++iter;
476 int i = 0;
477 for (std::vector<SimpleDrawQuad*>::const_iterator sdq_iter = sdq_list.begin();
478 sdq_iter < sdq_list.end();
479 ++sdq_iter) {
480 EXPECT_EQ(i, (*sdq_iter)->get_value());
481 ++i;
485 TEST(ListContainerTest, SimpleManipulationWithIndexSimpleDrawQuad) {
486 ListContainer<DrawQuad> list(sizeof(kLargestDrawQuad));
487 std::vector<SimpleDrawQuad*> dq_list;
488 size_t size = 10;
489 for (size_t i = 0; i < size; ++i) {
490 dq_list.push_back(list.AllocateAndConstruct<SimpleDrawQuad>());
492 EXPECT_EQ(size, list.size());
494 for (size_t i = 0; i < size; ++i) {
495 static_cast<SimpleDrawQuad*>(list.ElementAt(i))->set_value(i);
498 int i = 0;
499 for (std::vector<SimpleDrawQuad*>::const_iterator dq_iter = dq_list.begin();
500 dq_iter != dq_list.end();
501 ++dq_iter, ++i) {
502 EXPECT_EQ(i, (*dq_iter)->get_value());
506 TEST(ListContainerTest,
507 SimpleManipulationWithIndexMoreThanOneAllocationSimpleDrawQuad) {
508 ListContainer<DrawQuad> list(sizeof(kLargestDrawQuad), 2);
509 std::vector<SimpleDrawQuad*> dq_list;
510 size_t size = 10;
511 for (size_t i = 0; i < size; ++i) {
512 dq_list.push_back(list.AllocateAndConstruct<SimpleDrawQuad>());
514 EXPECT_EQ(size, list.size());
516 for (size_t i = 0; i < size; ++i) {
517 static_cast<SimpleDrawQuad*>(list.ElementAt(i))->set_value(i);
520 int i = 0;
521 for (std::vector<SimpleDrawQuad*>::const_iterator dq_iter = dq_list.begin();
522 dq_iter != dq_list.end();
523 ++dq_iter, ++i) {
524 EXPECT_EQ(i, (*dq_iter)->get_value());
528 TEST(ListContainerTest,
529 SimpleIterationAndReverseIterationWithIndexSharedQuadState) {
530 ListContainer<SharedQuadState> list;
531 std::vector<SharedQuadState*> sqs_list;
532 size_t size = 10;
533 for (size_t i = 0; i < size; ++i) {
534 sqs_list.push_back(list.AllocateAndConstruct<SharedQuadState>());
536 EXPECT_EQ(size, list.size());
538 size_t i = 0;
539 for (ListContainer<SharedQuadState>::Iterator iter = list.begin();
540 iter != list.end();
541 ++iter) {
542 EXPECT_EQ(i, iter.index());
543 ++i;
546 i = 0;
547 for (ListContainer<SharedQuadState>::ReverseIterator iter = list.rbegin();
548 iter != list.rend();
549 ++iter) {
550 EXPECT_EQ(i, iter.index());
551 ++i;
555 } // namespace
556 } // namespace cc