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 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
{}
44 class SimpleDrawQuadConstructMagicNumberOne
: public SimpleDrawQuad
{
46 SimpleDrawQuadConstructMagicNumberOne() : SimpleDrawQuad() {
47 set_value(kMagicNumberToUseForDrawQuadOne
);
51 class SimpleDrawQuadConstructMagicNumberTwo
: public SimpleDrawQuad
{
53 SimpleDrawQuadConstructMagicNumberTwo() : SimpleDrawQuad() {
54 set_value(kMagicNumberToUseForDrawQuadTwo
);
58 class MockDrawQuad
: public DrawQuad
{
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
));
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
));
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
));
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
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);
117 TEST(ListContainerTest
, DestructorCalledOnceWhenErase
) {
118 ListContainer
<DrawQuad
> list(sizeof(kLargestDrawQuad
));
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
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());
139 TEST(ListContainerTest
, SimpleIndexAccessSharedQuadState
) {
140 ListContainer
<SharedQuadState
> list
;
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
;
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());
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());
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());
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());
203 EXPECT_TRUE(list
.empty());
204 EXPECT_EQ(0u, list
.size());
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
;
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
=
232 sqs_iter
!= sqs_list
.end();
234 EXPECT_EQ(*sqs_iter
, &*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
;
247 for (size_t i
= 0; i
< size
; ++i
) {
248 // Before asking for a new element, space available without another
249 // allocation follows.
253 EXPECT_EQ(0u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
257 EXPECT_EQ(1u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
261 EXPECT_EQ(2u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
264 EXPECT_EQ(3u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
267 EXPECT_EQ(5u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
270 EXPECT_EQ(6u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
273 EXPECT_EQ(7u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
278 sqs_list
.push_back(list
.AllocateAndConstruct
<SharedQuadState
>());
279 // After asking for a new element, space available without another
280 // allocation follows.
284 EXPECT_EQ(0u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
288 EXPECT_EQ(1u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
291 EXPECT_EQ(2u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
294 EXPECT_EQ(3u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
297 EXPECT_EQ(4u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
300 EXPECT_EQ(5u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
303 EXPECT_EQ(6u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
306 EXPECT_EQ(7u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
312 EXPECT_EQ(size
, list
.size());
314 ListContainer
<SharedQuadState
>::Iterator iter
= list
.begin();
315 for (std::vector
<SharedQuadState
*>::const_iterator sqs_iter
=
317 sqs_iter
!= sqs_list
.end();
319 EXPECT_EQ(*sqs_iter
, &*iter
);
324 TEST(ListContainerTest
, SimpleIterationSharedQuadState
) {
325 ListContainer
<SharedQuadState
> list
;
326 std::vector
<SharedQuadState
*> sqs_list
;
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();
339 EXPECT_EQ(*sqs_iter
, &*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
=
350 sqs_iter
!= sqs_list
.end();
352 EXPECT_EQ(*sqs_iter
, &*iter
);
353 ++num_iters_in_vector
;
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
;
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
=
373 for (ListContainer
<SharedQuadState
>::ConstIterator iter
= list
.begin();
376 EXPECT_TRUE(isConstSharedQuadStatePointer(&*iter
));
377 EXPECT_EQ(*sqs_iter
, &*iter
);
383 std::vector
<const SharedQuadState
*>::const_iterator sqs_iter
=
385 for (ListContainer
<SharedQuadState
>::Iterator iter
= list
.begin();
388 EXPECT_FALSE(isConstSharedQuadStatePointer(&*iter
));
389 EXPECT_EQ(*sqs_iter
, &*iter
);
395 ListContainer
<SharedQuadState
>::ConstIterator iter
= list
.begin();
396 for (std::vector
<const SharedQuadState
*>::const_iterator sqs_iter
=
398 sqs_iter
!= sqs_list
.end();
400 EXPECT_EQ(*sqs_iter
, &*iter
);
406 TEST(ListContainerTest
, SimpleReverseInsertionSharedQuadState
) {
407 ListContainer
<SharedQuadState
> list
;
408 std::vector
<SharedQuadState
*> sqs_list
;
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
=
418 for (ListContainer
<SharedQuadState
>::ReverseIterator iter
= list
.rbegin();
421 EXPECT_EQ(*sqs_iter
, &(*iter
));
427 ListContainer
<SharedQuadState
>::ReverseIterator iter
= list
.rbegin();
428 for (std::vector
<SharedQuadState
*>::reverse_iterator sqs_iter
=
430 sqs_iter
!= sqs_list
.rend();
432 EXPECT_EQ(*sqs_iter
, &(*iter
));
438 TEST(ListContainerTest
, SimpleDeletion
) {
439 ListContainer
<DrawQuad
> list(sizeof(kLargestDrawQuad
));
440 std::vector
<SimpleDrawQuad
*> sdq_list
;
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());
450 EXPECT_EQ(size
, list
.size());
452 for (ListContainer
<DrawQuad
>::Iterator iter
= list
.begin();
455 EXPECT_EQ(i
, static_cast<SimpleDrawQuad
*>(&*iter
)->get_value());
460 TEST(ListContainerTest
, SimpleIterationAndManipulation
) {
461 ListContainer
<DrawQuad
> list(sizeof(kLargestDrawQuad
));
462 std::vector
<SimpleDrawQuad
*> sdq_list
;
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
);
477 for (std::vector
<SimpleDrawQuad
*>::const_iterator sdq_iter
= sdq_list
.begin();
478 sdq_iter
< sdq_list
.end();
480 EXPECT_EQ(i
, (*sdq_iter
)->get_value());
485 TEST(ListContainerTest
, SimpleManipulationWithIndexSimpleDrawQuad
) {
486 ListContainer
<DrawQuad
> list(sizeof(kLargestDrawQuad
));
487 std::vector
<SimpleDrawQuad
*> dq_list
;
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
);
499 for (std::vector
<SimpleDrawQuad
*>::const_iterator dq_iter
= dq_list
.begin();
500 dq_iter
!= dq_list
.end();
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
;
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
);
521 for (std::vector
<SimpleDrawQuad
*>::const_iterator dq_iter
= dq_list
.begin();
522 dq_iter
!= dq_list
.end();
524 EXPECT_EQ(i
, (*dq_iter
)->get_value());
528 TEST(ListContainerTest
,
529 SimpleIterationAndReverseIterationWithIndexSharedQuadState
) {
530 ListContainer
<SharedQuadState
> list
;
531 std::vector
<SharedQuadState
*> sqs_list
;
533 for (size_t i
= 0; i
< size
; ++i
) {
534 sqs_list
.push_back(list
.AllocateAndConstruct
<SharedQuadState
>());
536 EXPECT_EQ(size
, list
.size());
539 for (ListContainer
<SharedQuadState
>::Iterator iter
= list
.begin();
542 EXPECT_EQ(i
, iter
.index());
547 for (ListContainer
<SharedQuadState
>::ReverseIterator iter
= list
.rbegin();
550 EXPECT_EQ(i
, iter
.index());