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
, SimpleInsertionSharedQuadState
) {
140 ListContainer
<SharedQuadState
> list
;
143 SharedQuadState
* sqs_1
= list
.AllocateAndConstruct
<SharedQuadState
>();
144 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());
152 TEST(ListContainerTest
, SimpleInsertionAndClearSharedQuadState
) {
153 ListContainer
<SharedQuadState
> list
;
154 EXPECT_TRUE(list
.empty());
155 EXPECT_EQ(0u, list
.size());
158 SharedQuadState
* sqs_1
= list
.AllocateAndConstruct
<SharedQuadState
>();
159 list
.AllocateAndConstruct
<SharedQuadState
>();
160 SharedQuadState
* sqs_3
= list
.AllocateAndConstruct
<SharedQuadState
>();
162 EXPECT_EQ(size
, list
.size());
163 EXPECT_EQ(sqs_1
, list
.front());
164 EXPECT_EQ(sqs_3
, list
.back());
165 EXPECT_FALSE(list
.empty());
168 EXPECT_TRUE(list
.empty());
169 EXPECT_EQ(0u, list
.size());
172 TEST(ListContainerTest
, SimpleInsertionClearAndInsertAgainSharedQuadState
) {
173 ListContainer
<SharedQuadState
> list
;
174 EXPECT_TRUE(list
.empty());
175 EXPECT_EQ(0u, list
.size());
178 SharedQuadState
* sqs_front
= list
.AllocateAndConstruct
<SharedQuadState
>();
179 SharedQuadState
* sqs_back
= list
.AllocateAndConstruct
<SharedQuadState
>();
181 EXPECT_EQ(size
, list
.size());
182 EXPECT_EQ(sqs_front
, list
.front());
183 EXPECT_EQ(sqs_back
, list
.back());
184 EXPECT_FALSE(list
.empty());
187 EXPECT_TRUE(list
.empty());
188 EXPECT_EQ(0u, list
.size());
191 sqs_front
= list
.AllocateAndConstruct
<SharedQuadState
>();
192 list
.AllocateAndConstruct
<SharedQuadState
>();
193 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 // This test is used to test when there is more than one allocation needed
202 // for, ListContainer can still perform like normal vector.
203 TEST(ListContainerTest
,
204 SimpleInsertionTriggerMoreThanOneAllocationSharedQuadState
) {
205 ListContainer
<SharedQuadState
> list(sizeof(SharedQuadState
), 2);
206 std::vector
<SharedQuadState
*> sqs_list
;
208 for (size_t i
= 0; i
< size
; ++i
) {
209 sqs_list
.push_back(list
.AllocateAndConstruct
<SharedQuadState
>());
211 EXPECT_EQ(size
, list
.size());
213 ListContainer
<SharedQuadState
>::Iterator iter
= list
.begin();
214 for (std::vector
<SharedQuadState
*>::const_iterator sqs_iter
=
216 sqs_iter
!= sqs_list
.end();
218 EXPECT_EQ(*sqs_iter
, &*iter
);
223 TEST(ListContainerTest
,
224 CorrectAllocationSizeForMoreThanOneAllocationSharedQuadState
) {
225 // Constructor sets the allocation size to 2. Every time ListContainer needs
226 // to allocate again, it doubles allocation size. In this test, 10 elements is
227 // needed, thus ListContainerShould allocate spaces 2, 4 and 8 elements.
228 ListContainer
<SharedQuadState
> list(sizeof(SharedQuadState
), 2);
229 std::vector
<SharedQuadState
*> sqs_list
;
231 for (size_t i
= 0; i
< size
; ++i
) {
232 // Before asking for a new element, space available without another
233 // allocation follows.
237 EXPECT_EQ(0u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
241 EXPECT_EQ(1u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
245 EXPECT_EQ(2u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
248 EXPECT_EQ(3u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
251 EXPECT_EQ(5u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
254 EXPECT_EQ(6u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
257 EXPECT_EQ(7u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
262 sqs_list
.push_back(list
.AllocateAndConstruct
<SharedQuadState
>());
263 // After asking for a new element, space available without another
264 // allocation follows.
268 EXPECT_EQ(0u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
272 EXPECT_EQ(1u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
275 EXPECT_EQ(2u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
278 EXPECT_EQ(3u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
281 EXPECT_EQ(4u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
284 EXPECT_EQ(5u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
287 EXPECT_EQ(6u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
290 EXPECT_EQ(7u, list
.AvailableSizeWithoutAnotherAllocationForTesting());
296 EXPECT_EQ(size
, list
.size());
298 ListContainer
<SharedQuadState
>::Iterator iter
= list
.begin();
299 for (std::vector
<SharedQuadState
*>::const_iterator sqs_iter
=
301 sqs_iter
!= sqs_list
.end();
303 EXPECT_EQ(*sqs_iter
, &*iter
);
308 TEST(ListContainerTest
, SimpleIterationSharedQuadState
) {
309 ListContainer
<SharedQuadState
> list
;
310 std::vector
<SharedQuadState
*> sqs_list
;
312 for (size_t i
= 0; i
< size
; ++i
) {
313 sqs_list
.push_back(list
.AllocateAndConstruct
<SharedQuadState
>());
315 EXPECT_EQ(size
, list
.size());
317 size_t num_iters_in_list
= 0;
319 std::vector
<SharedQuadState
*>::const_iterator sqs_iter
= sqs_list
.begin();
320 for (ListContainer
<SharedQuadState
>::Iterator iter
= list
.begin();
323 EXPECT_EQ(*sqs_iter
, &*iter
);
329 size_t num_iters_in_vector
= 0;
331 ListContainer
<SharedQuadState
>::Iterator iter
= list
.begin();
332 for (std::vector
<SharedQuadState
*>::const_iterator sqs_iter
=
334 sqs_iter
!= sqs_list
.end();
336 EXPECT_EQ(*sqs_iter
, &*iter
);
337 ++num_iters_in_vector
;
342 EXPECT_EQ(num_iters_in_vector
, num_iters_in_list
);
345 TEST(ListContainerTest
, SimpleConstIteratorIterationSharedQuadState
) {
346 ListContainer
<SharedQuadState
> list
;
347 std::vector
<const SharedQuadState
*> sqs_list
;
349 for (size_t i
= 0; i
< size
; ++i
) {
350 sqs_list
.push_back(list
.AllocateAndConstruct
<SharedQuadState
>());
352 EXPECT_EQ(size
, list
.size());
355 std::vector
<const SharedQuadState
*>::const_iterator sqs_iter
=
357 for (ListContainer
<SharedQuadState
>::ConstIterator iter
= list
.begin();
360 EXPECT_TRUE(isConstSharedQuadStatePointer(&*iter
));
361 EXPECT_EQ(*sqs_iter
, &*iter
);
367 std::vector
<const SharedQuadState
*>::const_iterator sqs_iter
=
369 for (ListContainer
<SharedQuadState
>::Iterator iter
= list
.begin();
372 EXPECT_FALSE(isConstSharedQuadStatePointer(&*iter
));
373 EXPECT_EQ(*sqs_iter
, &*iter
);
379 ListContainer
<SharedQuadState
>::ConstIterator iter
= list
.begin();
380 for (std::vector
<const SharedQuadState
*>::const_iterator sqs_iter
=
382 sqs_iter
!= sqs_list
.end();
384 EXPECT_EQ(*sqs_iter
, &*iter
);
390 TEST(ListContainerTest
, SimpleReverseInsertionSharedQuadState
) {
391 ListContainer
<SharedQuadState
> list
;
392 std::vector
<SharedQuadState
*> sqs_list
;
394 for (size_t i
= 0; i
< size
; ++i
) {
395 sqs_list
.push_back(list
.AllocateAndConstruct
<SharedQuadState
>());
397 EXPECT_EQ(size
, list
.size());
400 std::vector
<SharedQuadState
*>::const_reverse_iterator sqs_iter
=
402 for (ListContainer
<SharedQuadState
>::ReverseIterator iter
= list
.rbegin();
405 EXPECT_EQ(*sqs_iter
, &(*iter
));
411 ListContainer
<SharedQuadState
>::ReverseIterator iter
= list
.rbegin();
412 for (std::vector
<SharedQuadState
*>::reverse_iterator sqs_iter
=
414 sqs_iter
!= sqs_list
.rend();
416 EXPECT_EQ(*sqs_iter
, &(*iter
));
422 TEST(ListContainerTest
, SimpleDeletion
) {
423 ListContainer
<DrawQuad
> list(sizeof(kLargestDrawQuad
));
424 std::vector
<SimpleDrawQuad
*> sdq_list
;
426 for (size_t i
= 0; i
< size
; ++i
) {
427 sdq_list
.push_back(list
.AllocateAndConstruct
<SimpleDrawQuad
>());
428 sdq_list
.back()->set_value(i
);
430 EXPECT_EQ(size
, list
.size());
432 list
.EraseAndInvalidateAllPointers(list
.begin());
434 EXPECT_EQ(size
, list
.size());
436 for (ListContainer
<DrawQuad
>::Iterator iter
= list
.begin();
439 EXPECT_EQ(i
, static_cast<SimpleDrawQuad
*>(&*iter
)->get_value());
444 TEST(ListContainerTest
, SimpleIterationAndManipulation
) {
445 ListContainer
<DrawQuad
> list(sizeof(kLargestDrawQuad
));
446 std::vector
<SimpleDrawQuad
*> sdq_list
;
448 for (size_t i
= 0; i
< size
; ++i
) {
449 SimpleDrawQuad
* simple_dq
= list
.AllocateAndConstruct
<SimpleDrawQuad
>();
450 sdq_list
.push_back(simple_dq
);
452 EXPECT_EQ(size
, list
.size());
454 ListContainer
<DrawQuad
>::Iterator iter
= list
.begin();
455 for (int i
= 0; i
< 10; ++i
) {
456 static_cast<SimpleDrawQuad
*>(&*iter
)->set_value(i
);
461 for (std::vector
<SimpleDrawQuad
*>::const_iterator sdq_iter
= sdq_list
.begin();
462 sdq_iter
< sdq_list
.end();
464 EXPECT_EQ(i
, (*sdq_iter
)->get_value());