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"
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"
19 int kMagicNumberToUseForDrawQuadOne
= 42;
20 int kMagicNumberToUseForDrawQuadTwo
= 314;
22 bool isConstSharedQuadStatePointer(const SharedQuadState
* ptr
) {
26 bool isConstSharedQuadStatePointer(SharedQuadState
* ptr
) {
30 class SimpleDrawQuad
: public DrawQuad
{
32 ~SimpleDrawQuad() override
{}
33 void IterateResources(const ResourceIteratorCallback
& callback
) override
{}
35 void set_value(int val
) { value
= val
; }
36 int get_value() { return value
; }
37 void ExtendValue(base::trace_event::TracedValue
* value
) const override
{}
43 class SimpleDrawQuadConstructMagicNumberOne
: public SimpleDrawQuad
{
45 SimpleDrawQuadConstructMagicNumberOne() : SimpleDrawQuad() {
46 set_value(kMagicNumberToUseForDrawQuadOne
);
50 class SimpleDrawQuadConstructMagicNumberTwo
: public SimpleDrawQuad
{
52 SimpleDrawQuadConstructMagicNumberTwo() : SimpleDrawQuad() {
53 set_value(kMagicNumberToUseForDrawQuadTwo
);
57 class MockDrawQuad
: public DrawQuad
{
59 ~MockDrawQuad() override
{ Destruct(); }
60 void IterateResources(const ResourceIteratorCallback
& callback
) override
{}
61 void ExtendValue(base::trace_event::TracedValue
* value
) const override
{}
62 MOCK_METHOD0(Destruct
, void());
65 TEST(ListContainerTest
, ConstructorCalledInAllocateAndConstruct
) {
66 ListContainer
<DrawQuad
> list(LargestDrawQuadSize());
69 SimpleDrawQuadConstructMagicNumberOne
* dq_1
=
70 list
.AllocateAndConstruct
<SimpleDrawQuadConstructMagicNumberOne
>();
71 SimpleDrawQuadConstructMagicNumberTwo
* dq_2
=
72 list
.AllocateAndConstruct
<SimpleDrawQuadConstructMagicNumberTwo
>();
74 EXPECT_EQ(size
, list
.size());
75 EXPECT_EQ(dq_1
, list
.front());
76 EXPECT_EQ(dq_2
, list
.back());
78 EXPECT_EQ(kMagicNumberToUseForDrawQuadOne
, dq_1
->get_value());
79 EXPECT_EQ(kMagicNumberToUseForDrawQuadTwo
, dq_2
->get_value());
82 TEST(ListContainerTest
, DestructorCalled
) {
83 ListContainer
<DrawQuad
> list(LargestDrawQuadSize());
86 MockDrawQuad
* dq_1
= list
.AllocateAndConstruct
<MockDrawQuad
>();
88 EXPECT_CALL(*dq_1
, Destruct());
89 EXPECT_EQ(size
, list
.size());
90 EXPECT_EQ(dq_1
, list
.front());
93 TEST(ListContainerTest
, DestructorCalledOnceWhenClear
) {
94 ListContainer
<DrawQuad
> list(LargestDrawQuadSize());
96 MockDrawQuad
* dq_1
= list
.AllocateAndConstruct
<MockDrawQuad
>();
98 EXPECT_EQ(size
, list
.size());
99 EXPECT_EQ(dq_1
, list
.front());
101 // Make sure destructor is called once during clear, and won't be called
103 testing::MockFunction
<void()> separator
;
105 testing::InSequence s
;
106 EXPECT_CALL(*dq_1
, Destruct());
107 EXPECT_CALL(separator
, Call());
108 EXPECT_CALL(*dq_1
, Destruct()).Times(0);
115 TEST(ListContainerTest
, DestructorCalledOnceWhenErase
) {
116 ListContainer
<DrawQuad
> list(LargestDrawQuadSize());
118 MockDrawQuad
* dq_1
= list
.AllocateAndConstruct
<MockDrawQuad
>();
120 EXPECT_EQ(size
, list
.size());
121 EXPECT_EQ(dq_1
, list
.front());
123 // Make sure destructor is called once during clear, and won't be called
125 testing::MockFunction
<void()> separator
;
127 testing::InSequence s
;
128 EXPECT_CALL(*dq_1
, Destruct());
129 EXPECT_CALL(separator
, Call());
130 EXPECT_CALL(*dq_1
, Destruct()).Times(0);
133 list
.EraseAndInvalidateAllPointers(list
.begin());
137 TEST(ListContainerTest
, SimpleIndexAccessSharedQuadState
) {
138 ListContainer
<SharedQuadState
> list
;
141 SharedQuadState
* sqs_1
= list
.AllocateAndConstruct
<SharedQuadState
>();
142 SharedQuadState
* sqs_2
= list
.AllocateAndConstruct
<SharedQuadState
>();
143 SharedQuadState
* sqs_3
= list
.AllocateAndConstruct
<SharedQuadState
>();
145 EXPECT_EQ(size
, list
.size());
146 EXPECT_EQ(sqs_1
, list
.front());
147 EXPECT_EQ(sqs_3
, list
.back());
148 EXPECT_EQ(list
.front(), list
.ElementAt(0));
149 EXPECT_EQ(sqs_2
, list
.ElementAt(1));
150 EXPECT_EQ(list
.back(), list
.ElementAt(2));
153 TEST(ListContainerTest
, SimpleInsertionSharedQuadState
) {
154 ListContainer
<SharedQuadState
> list
;
157 SharedQuadState
* sqs_1
= list
.AllocateAndConstruct
<SharedQuadState
>();
158 list
.AllocateAndConstruct
<SharedQuadState
>();
159 SharedQuadState
* sqs_3
= list
.AllocateAndConstruct
<SharedQuadState
>();
161 EXPECT_EQ(size
, list
.size());
162 EXPECT_EQ(sqs_1
, list
.front());
163 EXPECT_EQ(sqs_3
, list
.back());
166 TEST(ListContainerTest
, SimpleInsertionAndClearSharedQuadState
) {
167 ListContainer
<SharedQuadState
> list
;
168 EXPECT_TRUE(list
.empty());
169 EXPECT_EQ(0u, list
.size());
172 SharedQuadState
* sqs_1
= list
.AllocateAndConstruct
<SharedQuadState
>();
173 list
.AllocateAndConstruct
<SharedQuadState
>();
174 SharedQuadState
* sqs_3
= list
.AllocateAndConstruct
<SharedQuadState
>();
176 EXPECT_EQ(size
, list
.size());
177 EXPECT_EQ(sqs_1
, list
.front());
178 EXPECT_EQ(sqs_3
, list
.back());
179 EXPECT_FALSE(list
.empty());
182 EXPECT_TRUE(list
.empty());
183 EXPECT_EQ(0u, list
.size());
186 TEST(ListContainerTest
, SimpleInsertionClearAndInsertAgainSharedQuadState
) {
187 ListContainer
<SharedQuadState
> list
;
188 EXPECT_TRUE(list
.empty());
189 EXPECT_EQ(0u, list
.size());
192 SharedQuadState
* sqs_front
= list
.AllocateAndConstruct
<SharedQuadState
>();
193 SharedQuadState
* sqs_back
= list
.AllocateAndConstruct
<SharedQuadState
>();
195 EXPECT_EQ(size
, list
.size());
196 EXPECT_EQ(sqs_front
, list
.front());
197 EXPECT_EQ(sqs_back
, list
.back());
198 EXPECT_FALSE(list
.empty());
201 EXPECT_TRUE(list
.empty());
202 EXPECT_EQ(0u, list
.size());
205 sqs_front
= list
.AllocateAndConstruct
<SharedQuadState
>();
206 list
.AllocateAndConstruct
<SharedQuadState
>();
207 sqs_back
= list
.AllocateAndConstruct
<SharedQuadState
>();
209 EXPECT_EQ(size
, list
.size());
210 EXPECT_EQ(sqs_front
, list
.front());
211 EXPECT_EQ(sqs_back
, list
.back());
212 EXPECT_FALSE(list
.empty());
215 // This test is used to test when there is more than one allocation needed
216 // for, ListContainer can still perform like normal vector.
217 TEST(ListContainerTest
,
218 SimpleInsertionTriggerMoreThanOneAllocationSharedQuadState
) {
219 ListContainer
<SharedQuadState
> list(sizeof(SharedQuadState
), 2);
220 std::vector
<SharedQuadState
*> sqs_list
;
222 for (size_t i
= 0; i
< size
; ++i
) {
223 sqs_list
.push_back(list
.AllocateAndConstruct
<SharedQuadState
>());
225 EXPECT_EQ(size
, list
.size());
227 ListContainer
<SharedQuadState
>::Iterator iter
= list
.begin();
228 for (std::vector
<SharedQuadState
*>::const_iterator sqs_iter
=
230 sqs_iter
!= sqs_list
.end();
232 EXPECT_EQ(*sqs_iter
, *iter
);
237 TEST(ListContainerTest
,
238 CorrectAllocationSizeForMoreThanOneAllocationSharedQuadState
) {
239 // Constructor sets the allocation size to 2. Every time ListContainer needs
240 // to allocate again, it doubles allocation size. In this test, 10 elements is
241 // needed, thus ListContainerShould allocate spaces 2, 4 and 8 elements.
242 ListContainer
<SharedQuadState
> list(sizeof(SharedQuadState
), 2);
243 std::vector
<SharedQuadState
*> sqs_list
;
245 for (size_t i
= 0; i
< size
; ++i
) {
246 // Before asking for a new element, space available without another
247 // allocation follows.
251 EXPECT_EQ(0u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
255 EXPECT_EQ(1u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
259 EXPECT_EQ(2u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
262 EXPECT_EQ(3u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
265 EXPECT_EQ(5u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
268 EXPECT_EQ(6u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
271 EXPECT_EQ(7u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
276 sqs_list
.push_back(list
.AllocateAndConstruct
<SharedQuadState
>());
277 // After asking for a new element, space available without another
278 // allocation follows.
282 EXPECT_EQ(0u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
286 EXPECT_EQ(1u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
289 EXPECT_EQ(2u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
292 EXPECT_EQ(3u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
295 EXPECT_EQ(4u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
298 EXPECT_EQ(5u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
301 EXPECT_EQ(6u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
304 EXPECT_EQ(7u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
310 EXPECT_EQ(size
, list
.size());
312 ListContainer
<SharedQuadState
>::Iterator iter
= list
.begin();
313 for (std::vector
<SharedQuadState
*>::const_iterator sqs_iter
=
315 sqs_iter
!= sqs_list
.end();
317 EXPECT_EQ(*sqs_iter
, *iter
);
322 TEST(ListContainerTest
, SimpleIterationSharedQuadState
) {
323 ListContainer
<SharedQuadState
> list
;
324 std::vector
<SharedQuadState
*> sqs_list
;
326 for (size_t i
= 0; i
< size
; ++i
) {
327 sqs_list
.push_back(list
.AllocateAndConstruct
<SharedQuadState
>());
329 EXPECT_EQ(size
, list
.size());
331 size_t num_iters_in_list
= 0;
333 std::vector
<SharedQuadState
*>::const_iterator sqs_iter
= sqs_list
.begin();
334 for (ListContainer
<SharedQuadState
>::Iterator iter
= list
.begin();
337 EXPECT_EQ(*sqs_iter
, *iter
);
343 size_t num_iters_in_vector
= 0;
345 ListContainer
<SharedQuadState
>::Iterator iter
= list
.begin();
346 for (std::vector
<SharedQuadState
*>::const_iterator sqs_iter
=
348 sqs_iter
!= sqs_list
.end();
350 EXPECT_EQ(*sqs_iter
, *iter
);
351 ++num_iters_in_vector
;
356 EXPECT_EQ(num_iters_in_vector
, num_iters_in_list
);
359 TEST(ListContainerTest
, SimpleConstIteratorIterationSharedQuadState
) {
360 ListContainer
<SharedQuadState
> list
;
361 std::vector
<const SharedQuadState
*> sqs_list
;
363 for (size_t i
= 0; i
< size
; ++i
) {
364 sqs_list
.push_back(list
.AllocateAndConstruct
<SharedQuadState
>());
366 EXPECT_EQ(size
, list
.size());
369 std::vector
<const SharedQuadState
*>::const_iterator sqs_iter
=
371 for (ListContainer
<SharedQuadState
>::ConstIterator iter
= list
.begin();
374 EXPECT_TRUE(isConstSharedQuadStatePointer(*iter
));
375 EXPECT_EQ(*sqs_iter
, *iter
);
381 std::vector
<const SharedQuadState
*>::const_iterator sqs_iter
=
383 for (ListContainer
<SharedQuadState
>::Iterator iter
= list
.begin();
386 EXPECT_FALSE(isConstSharedQuadStatePointer(*iter
));
387 EXPECT_EQ(*sqs_iter
, *iter
);
393 ListContainer
<SharedQuadState
>::ConstIterator iter
= list
.begin();
394 for (std::vector
<const SharedQuadState
*>::const_iterator sqs_iter
=
396 sqs_iter
!= sqs_list
.end();
398 EXPECT_EQ(*sqs_iter
, *iter
);
404 TEST(ListContainerTest
, SimpleReverseInsertionSharedQuadState
) {
405 ListContainer
<SharedQuadState
> list
;
406 std::vector
<SharedQuadState
*> sqs_list
;
408 for (size_t i
= 0; i
< size
; ++i
) {
409 sqs_list
.push_back(list
.AllocateAndConstruct
<SharedQuadState
>());
411 EXPECT_EQ(size
, list
.size());
414 std::vector
<SharedQuadState
*>::const_reverse_iterator sqs_iter
=
416 for (ListContainer
<SharedQuadState
>::ReverseIterator iter
= list
.rbegin();
419 EXPECT_EQ(*sqs_iter
, *iter
);
425 ListContainer
<SharedQuadState
>::ReverseIterator iter
= list
.rbegin();
426 for (std::vector
<SharedQuadState
*>::reverse_iterator sqs_iter
=
428 sqs_iter
!= sqs_list
.rend();
430 EXPECT_EQ(*sqs_iter
, *iter
);
436 TEST(ListContainerTest
, SimpleDeletion
) {
437 ListContainer
<DrawQuad
> list(LargestDrawQuadSize());
438 std::vector
<SimpleDrawQuad
*> sdq_list
;
440 for (size_t i
= 0; i
< size
; ++i
) {
441 sdq_list
.push_back(list
.AllocateAndConstruct
<SimpleDrawQuad
>());
442 sdq_list
.back()->set_value(i
);
444 EXPECT_EQ(size
, list
.size());
446 list
.EraseAndInvalidateAllPointers(list
.begin());
448 EXPECT_EQ(size
, list
.size());
450 for (ListContainer
<DrawQuad
>::Iterator iter
= list
.begin();
453 EXPECT_EQ(i
, static_cast<SimpleDrawQuad
*>(*iter
)->get_value());
458 TEST(ListContainerTest
, SimpleIterationAndManipulation
) {
459 ListContainer
<DrawQuad
> list(LargestDrawQuadSize());
460 std::vector
<SimpleDrawQuad
*> sdq_list
;
462 for (size_t i
= 0; i
< size
; ++i
) {
463 SimpleDrawQuad
* simple_dq
= list
.AllocateAndConstruct
<SimpleDrawQuad
>();
464 sdq_list
.push_back(simple_dq
);
466 EXPECT_EQ(size
, list
.size());
468 ListContainer
<DrawQuad
>::Iterator iter
= list
.begin();
469 for (int i
= 0; i
< 10; ++i
) {
470 static_cast<SimpleDrawQuad
*>(*iter
)->set_value(i
);
475 for (std::vector
<SimpleDrawQuad
*>::const_iterator sdq_iter
= sdq_list
.begin();
476 sdq_iter
< sdq_list
.end();
478 EXPECT_EQ(i
, (*sdq_iter
)->get_value());
483 TEST(ListContainerTest
, SimpleManipulationWithIndexSimpleDrawQuad
) {
484 ListContainer
<DrawQuad
> list(LargestDrawQuadSize());
485 std::vector
<SimpleDrawQuad
*> dq_list
;
487 for (size_t i
= 0; i
< size
; ++i
) {
488 dq_list
.push_back(list
.AllocateAndConstruct
<SimpleDrawQuad
>());
490 EXPECT_EQ(size
, list
.size());
492 for (size_t i
= 0; i
< size
; ++i
) {
493 static_cast<SimpleDrawQuad
*>(list
.ElementAt(i
))->set_value(i
);
497 for (std::vector
<SimpleDrawQuad
*>::const_iterator dq_iter
= dq_list
.begin();
498 dq_iter
!= dq_list
.end();
500 EXPECT_EQ(i
, (*dq_iter
)->get_value());
504 TEST(ListContainerTest
,
505 SimpleManipulationWithIndexMoreThanOneAllocationSimpleDrawQuad
) {
506 ListContainer
<DrawQuad
> list(LargestDrawQuadSize(), 2);
507 std::vector
<SimpleDrawQuad
*> dq_list
;
509 for (size_t i
= 0; i
< size
; ++i
) {
510 dq_list
.push_back(list
.AllocateAndConstruct
<SimpleDrawQuad
>());
512 EXPECT_EQ(size
, list
.size());
514 for (size_t i
= 0; i
< size
; ++i
) {
515 static_cast<SimpleDrawQuad
*>(list
.ElementAt(i
))->set_value(i
);
519 for (std::vector
<SimpleDrawQuad
*>::const_iterator dq_iter
= dq_list
.begin();
520 dq_iter
!= dq_list
.end();
522 EXPECT_EQ(i
, (*dq_iter
)->get_value());
526 TEST(ListContainerTest
,
527 SimpleIterationAndReverseIterationWithIndexSharedQuadState
) {
528 ListContainer
<SharedQuadState
> list
;
529 std::vector
<SharedQuadState
*> sqs_list
;
531 for (size_t i
= 0; i
< size
; ++i
) {
532 sqs_list
.push_back(list
.AllocateAndConstruct
<SharedQuadState
>());
534 EXPECT_EQ(size
, list
.size());
537 for (ListContainer
<SharedQuadState
>::Iterator iter
= list
.begin();
540 EXPECT_EQ(i
, iter
.index());
545 for (ListContainer
<SharedQuadState
>::ReverseIterator iter
= list
.rbegin();
548 EXPECT_EQ(i
, iter
.index());