1 // Copyright (c) 2009 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/renderer/paint_aggregator.h"
6 #include "testing/gtest/include/gtest/gtest.h"
8 TEST(PaintAggregator
, InitialState
) {
10 EXPECT_FALSE(greg
.HasPendingUpdate());
13 TEST(PaintAggregator
, SingleInvalidation
) {
16 gfx::Rect
rect(2, 4, 10, 16);
17 greg
.InvalidateRect(rect
);
19 EXPECT_TRUE(greg
.HasPendingUpdate());
20 PaintAggregator::PendingUpdate update
;
21 greg
.PopPendingUpdate(&update
);
23 EXPECT_TRUE(update
.scroll_rect
.IsEmpty());
24 ASSERT_EQ(1U, update
.paint_rects
.size());
26 EXPECT_EQ(rect
, update
.paint_rects
[0]);
29 TEST(PaintAggregator
, DoubleDisjointInvalidation
) {
32 gfx::Rect
r1(2, 4, 2, 40);
33 gfx::Rect
r2(4, 2, 40, 2);
35 greg
.InvalidateRect(r1
);
36 greg
.InvalidateRect(r2
);
38 gfx::Rect expected_bounds
= r1
.Union(r2
);
40 EXPECT_TRUE(greg
.HasPendingUpdate());
41 PaintAggregator::PendingUpdate update
;
42 greg
.PopPendingUpdate(&update
);
44 EXPECT_TRUE(update
.scroll_rect
.IsEmpty());
45 EXPECT_EQ(2U, update
.paint_rects
.size());
47 EXPECT_EQ(expected_bounds
, update
.GetPaintBounds());
50 TEST(PaintAggregator
, DisjointInvalidationsCombined
) {
53 // Make the rectangles such that they don't overlap but cover a very large
54 // percentage of the area of covered by their union. This is so we're not
55 // very sensitive to the combining heuristic in the paint aggregator.
56 gfx::Rect
r1(2, 4, 2, 1000);
57 gfx::Rect
r2(5, 2, 2, 1000);
59 greg
.InvalidateRect(r1
);
60 greg
.InvalidateRect(r2
);
62 gfx::Rect expected_bounds
= r1
.Union(r2
);
64 EXPECT_TRUE(greg
.HasPendingUpdate());
65 PaintAggregator::PendingUpdate update
;
66 greg
.PopPendingUpdate(&update
);
68 EXPECT_TRUE(update
.scroll_rect
.IsEmpty());
69 ASSERT_EQ(1U, update
.paint_rects
.size());
71 EXPECT_EQ(expected_bounds
, update
.paint_rects
[0]);
74 TEST(PaintAggregator
, SingleScroll
) {
77 gfx::Rect
rect(1, 2, 3, 4);
78 gfx::Point
delta(1, 0);
79 greg
.ScrollRect(delta
.x(), delta
.y(), rect
);
81 EXPECT_TRUE(greg
.HasPendingUpdate());
82 PaintAggregator::PendingUpdate update
;
83 greg
.PopPendingUpdate(&update
);
85 EXPECT_TRUE(update
.paint_rects
.empty());
86 EXPECT_FALSE(update
.scroll_rect
.IsEmpty());
88 EXPECT_EQ(rect
, update
.scroll_rect
);
90 EXPECT_EQ(delta
.x(), update
.scroll_delta
.x());
91 EXPECT_EQ(delta
.y(), update
.scroll_delta
.y());
93 gfx::Rect resulting_damage
= update
.GetScrollDamage();
94 gfx::Rect
expected_damage(1, 2, 1, 4);
95 EXPECT_EQ(expected_damage
, resulting_damage
);
98 TEST(PaintAggregator
, DoubleOverlappingScroll
) {
101 gfx::Rect
rect(1, 2, 3, 4);
102 gfx::Point
delta1(1, 0);
103 gfx::Point
delta2(1, 0);
104 greg
.ScrollRect(delta1
.x(), delta1
.y(), rect
);
105 greg
.ScrollRect(delta2
.x(), delta2
.y(), rect
);
107 EXPECT_TRUE(greg
.HasPendingUpdate());
108 PaintAggregator::PendingUpdate update
;
109 greg
.PopPendingUpdate(&update
);
111 EXPECT_TRUE(update
.paint_rects
.empty());
112 EXPECT_FALSE(update
.scroll_rect
.IsEmpty());
114 EXPECT_EQ(rect
, update
.scroll_rect
);
116 gfx::Point
expected_delta(delta1
.x() + delta2
.x(),
117 delta1
.y() + delta2
.y());
118 EXPECT_EQ(expected_delta
.x(), update
.scroll_delta
.x());
119 EXPECT_EQ(expected_delta
.y(), update
.scroll_delta
.y());
121 gfx::Rect resulting_damage
= update
.GetScrollDamage();
122 gfx::Rect
expected_damage(1, 2, 2, 4);
123 EXPECT_EQ(expected_damage
, resulting_damage
);
126 TEST(PaintAggregator
, NegatingScroll
) {
127 PaintAggregator greg
;
129 // Scroll twice in opposite directions by equal amounts. The result
130 // should be no scrolling.
132 gfx::Rect
rect(1, 2, 3, 4);
133 gfx::Point
delta1(1, 0);
134 gfx::Point
delta2(-1, 0);
135 greg
.ScrollRect(delta1
.x(), delta1
.y(), rect
);
136 greg
.ScrollRect(delta2
.x(), delta2
.y(), rect
);
138 EXPECT_FALSE(greg
.HasPendingUpdate());
141 TEST(PaintAggregator
, DiagonalScroll
) {
142 PaintAggregator greg
;
144 // We don't support optimized diagonal scrolling, so this should result in
147 gfx::Rect
rect(1, 2, 3, 4);
148 gfx::Point
delta(1, 1);
149 greg
.ScrollRect(delta
.x(), delta
.y(), rect
);
151 EXPECT_TRUE(greg
.HasPendingUpdate());
152 PaintAggregator::PendingUpdate update
;
153 greg
.PopPendingUpdate(&update
);
155 EXPECT_TRUE(update
.scroll_rect
.IsEmpty());
156 ASSERT_EQ(1U, update
.paint_rects
.size());
158 EXPECT_EQ(rect
, update
.paint_rects
[0]);
161 TEST(PaintAggregator
, ContainedPaintAfterScroll
) {
162 PaintAggregator greg
;
164 gfx::Rect
scroll_rect(0, 0, 10, 10);
165 greg
.ScrollRect(2, 0, scroll_rect
);
167 gfx::Rect
paint_rect(4, 4, 2, 2);
168 greg
.InvalidateRect(paint_rect
);
170 EXPECT_TRUE(greg
.HasPendingUpdate());
171 PaintAggregator::PendingUpdate update
;
172 greg
.PopPendingUpdate(&update
);
174 // expecting a paint rect inside the scroll rect
175 EXPECT_FALSE(update
.scroll_rect
.IsEmpty());
176 EXPECT_EQ(1U, update
.paint_rects
.size());
178 EXPECT_EQ(scroll_rect
, update
.scroll_rect
);
179 EXPECT_EQ(paint_rect
, update
.paint_rects
[0]);
182 TEST(PaintAggregator
, ContainedPaintBeforeScroll
) {
183 PaintAggregator greg
;
185 gfx::Rect
paint_rect(4, 4, 2, 2);
186 greg
.InvalidateRect(paint_rect
);
188 gfx::Rect
scroll_rect(0, 0, 10, 10);
189 greg
.ScrollRect(2, 0, scroll_rect
);
191 EXPECT_TRUE(greg
.HasPendingUpdate());
192 PaintAggregator::PendingUpdate update
;
193 greg
.PopPendingUpdate(&update
);
195 // Expecting a paint rect inside the scroll rect
196 EXPECT_FALSE(update
.scroll_rect
.IsEmpty());
197 EXPECT_EQ(1U, update
.paint_rects
.size());
199 paint_rect
.Offset(2, 0);
201 EXPECT_EQ(scroll_rect
, update
.scroll_rect
);
202 EXPECT_EQ(paint_rect
, update
.paint_rects
[0]);
205 TEST(PaintAggregator
, ContainedPaintsBeforeAndAfterScroll
) {
206 PaintAggregator greg
;
208 gfx::Rect
paint_rect1(4, 4, 2, 2);
209 greg
.InvalidateRect(paint_rect1
);
211 gfx::Rect
scroll_rect(0, 0, 10, 10);
212 greg
.ScrollRect(2, 0, scroll_rect
);
214 gfx::Rect
paint_rect2(6, 4, 2, 2);
215 greg
.InvalidateRect(paint_rect2
);
217 gfx::Rect expected_paint_rect
= paint_rect2
;
219 EXPECT_TRUE(greg
.HasPendingUpdate());
220 PaintAggregator::PendingUpdate update
;
221 greg
.PopPendingUpdate(&update
);
223 // Expecting a paint rect inside the scroll rect
224 EXPECT_FALSE(update
.scroll_rect
.IsEmpty());
225 EXPECT_EQ(1U, update
.paint_rects
.size());
227 EXPECT_EQ(scroll_rect
, update
.scroll_rect
);
228 EXPECT_EQ(expected_paint_rect
, update
.paint_rects
[0]);
231 TEST(PaintAggregator
, LargeContainedPaintAfterScroll
) {
232 PaintAggregator greg
;
234 gfx::Rect
scroll_rect(0, 0, 10, 10);
235 greg
.ScrollRect(0, 1, scroll_rect
);
237 gfx::Rect
paint_rect(0, 0, 10, 9); // Repaint 90%
238 greg
.InvalidateRect(paint_rect
);
240 EXPECT_TRUE(greg
.HasPendingUpdate());
241 PaintAggregator::PendingUpdate update
;
242 greg
.PopPendingUpdate(&update
);
244 EXPECT_TRUE(update
.scroll_rect
.IsEmpty());
245 EXPECT_EQ(1U, update
.paint_rects
.size());
247 EXPECT_EQ(scroll_rect
, update
.paint_rects
[0]);
250 TEST(PaintAggregator
, LargeContainedPaintBeforeScroll
) {
251 PaintAggregator greg
;
253 gfx::Rect
paint_rect(0, 0, 10, 9); // Repaint 90%
254 greg
.InvalidateRect(paint_rect
);
256 gfx::Rect
scroll_rect(0, 0, 10, 10);
257 greg
.ScrollRect(0, 1, scroll_rect
);
259 EXPECT_TRUE(greg
.HasPendingUpdate());
260 PaintAggregator::PendingUpdate update
;
261 greg
.PopPendingUpdate(&update
);
263 EXPECT_TRUE(update
.scroll_rect
.IsEmpty());
264 EXPECT_EQ(1U, update
.paint_rects
.size());
266 EXPECT_EQ(scroll_rect
, update
.paint_rects
[0]);
269 TEST(PaintAggregator
, OverlappingPaintBeforeScroll
) {
270 PaintAggregator greg
;
272 gfx::Rect
paint_rect(4, 4, 10, 2);
273 greg
.InvalidateRect(paint_rect
);
275 gfx::Rect
scroll_rect(0, 0, 10, 10);
276 greg
.ScrollRect(2, 0, scroll_rect
);
278 gfx::Rect expected_paint_rect
= scroll_rect
.Union(paint_rect
);
280 EXPECT_TRUE(greg
.HasPendingUpdate());
281 PaintAggregator::PendingUpdate update
;
282 greg
.PopPendingUpdate(&update
);
284 EXPECT_TRUE(update
.scroll_rect
.IsEmpty());
285 EXPECT_EQ(1U, update
.paint_rects
.size());
287 EXPECT_EQ(expected_paint_rect
, update
.paint_rects
[0]);
290 TEST(PaintAggregator
, OverlappingPaintAfterScroll
) {
291 PaintAggregator greg
;
293 gfx::Rect
scroll_rect(0, 0, 10, 10);
294 greg
.ScrollRect(2, 0, scroll_rect
);
296 gfx::Rect
paint_rect(4, 4, 10, 2);
297 greg
.InvalidateRect(paint_rect
);
299 gfx::Rect expected_paint_rect
= scroll_rect
.Union(paint_rect
);
301 EXPECT_TRUE(greg
.HasPendingUpdate());
302 PaintAggregator::PendingUpdate update
;
303 greg
.PopPendingUpdate(&update
);
305 EXPECT_TRUE(update
.scroll_rect
.IsEmpty());
306 EXPECT_EQ(1U, update
.paint_rects
.size());
308 EXPECT_EQ(expected_paint_rect
, update
.paint_rects
[0]);
311 TEST(PaintAggregator
, DisjointPaintBeforeScroll
) {
312 PaintAggregator greg
;
314 gfx::Rect
paint_rect(4, 4, 10, 2);
315 greg
.InvalidateRect(paint_rect
);
317 gfx::Rect
scroll_rect(0, 0, 2, 10);
318 greg
.ScrollRect(2, 0, scroll_rect
);
320 EXPECT_TRUE(greg
.HasPendingUpdate());
321 PaintAggregator::PendingUpdate update
;
322 greg
.PopPendingUpdate(&update
);
324 EXPECT_FALSE(update
.scroll_rect
.IsEmpty());
325 EXPECT_EQ(1U, update
.paint_rects
.size());
327 EXPECT_EQ(paint_rect
, update
.paint_rects
[0]);
328 EXPECT_EQ(scroll_rect
, update
.scroll_rect
);
331 TEST(PaintAggregator
, DisjointPaintAfterScroll
) {
332 PaintAggregator greg
;
334 gfx::Rect
scroll_rect(0, 0, 2, 10);
335 greg
.ScrollRect(2, 0, scroll_rect
);
337 gfx::Rect
paint_rect(4, 4, 10, 2);
338 greg
.InvalidateRect(paint_rect
);
340 EXPECT_TRUE(greg
.HasPendingUpdate());
341 PaintAggregator::PendingUpdate update
;
342 greg
.PopPendingUpdate(&update
);
344 EXPECT_FALSE(update
.scroll_rect
.IsEmpty());
345 EXPECT_EQ(1U, update
.paint_rects
.size());
347 EXPECT_EQ(paint_rect
, update
.paint_rects
[0]);
348 EXPECT_EQ(scroll_rect
, update
.scroll_rect
);
351 TEST(PaintAggregator
, ContainedPaintTrimmedByScroll
) {
352 PaintAggregator greg
;
354 gfx::Rect
paint_rect(4, 4, 6, 6);
355 greg
.InvalidateRect(paint_rect
);
357 gfx::Rect
scroll_rect(0, 0, 10, 10);
358 greg
.ScrollRect(2, 0, scroll_rect
);
360 // The paint rect should have become narrower.
361 gfx::Rect
expected_paint_rect(6, 4, 4, 6);
363 EXPECT_TRUE(greg
.HasPendingUpdate());
364 PaintAggregator::PendingUpdate update
;
365 greg
.PopPendingUpdate(&update
);
367 EXPECT_FALSE(update
.scroll_rect
.IsEmpty());
368 EXPECT_EQ(1U, update
.paint_rects
.size());
370 EXPECT_EQ(expected_paint_rect
, update
.paint_rects
[0]);
371 EXPECT_EQ(scroll_rect
, update
.scroll_rect
);
374 TEST(PaintAggregator
, ContainedPaintEliminatedByScroll
) {
375 PaintAggregator greg
;
377 gfx::Rect
paint_rect(4, 4, 6, 6);
378 greg
.InvalidateRect(paint_rect
);
380 gfx::Rect
scroll_rect(0, 0, 10, 10);
381 greg
.ScrollRect(6, 0, scroll_rect
);
383 EXPECT_TRUE(greg
.HasPendingUpdate());
384 PaintAggregator::PendingUpdate update
;
385 greg
.PopPendingUpdate(&update
);
387 EXPECT_FALSE(update
.scroll_rect
.IsEmpty());
388 EXPECT_TRUE(update
.paint_rects
.empty());
390 EXPECT_EQ(scroll_rect
, update
.scroll_rect
);
393 TEST(PaintAggregator
, ContainedPaintAfterScrollTrimmedByScrollDamage
) {
394 PaintAggregator greg
;
396 gfx::Rect
scroll_rect(0, 0, 10, 10);
397 greg
.ScrollRect(4, 0, scroll_rect
);
399 gfx::Rect
paint_rect(2, 0, 4, 10);
400 greg
.InvalidateRect(paint_rect
);
402 gfx::Rect
expected_scroll_damage(0, 0, 4, 10);
403 gfx::Rect
expected_paint_rect(4, 0, 2, 10);
405 EXPECT_TRUE(greg
.HasPendingUpdate());
406 PaintAggregator::PendingUpdate update
;
407 greg
.PopPendingUpdate(&update
);
409 EXPECT_FALSE(update
.scroll_rect
.IsEmpty());
410 EXPECT_EQ(1U, update
.paint_rects
.size());
412 EXPECT_EQ(scroll_rect
, update
.scroll_rect
);
413 EXPECT_EQ(expected_scroll_damage
, update
.GetScrollDamage());
414 EXPECT_EQ(expected_paint_rect
, update
.paint_rects
[0]);
417 TEST(PaintAggregator
, ContainedPaintAfterScrollEliminatedByScrollDamage
) {
418 PaintAggregator greg
;
420 gfx::Rect
scroll_rect(0, 0, 10, 10);
421 greg
.ScrollRect(4, 0, scroll_rect
);
423 gfx::Rect
paint_rect(2, 0, 2, 10);
424 greg
.InvalidateRect(paint_rect
);
426 gfx::Rect
expected_scroll_damage(0, 0, 4, 10);
428 EXPECT_TRUE(greg
.HasPendingUpdate());
429 PaintAggregator::PendingUpdate update
;
430 greg
.PopPendingUpdate(&update
);
432 EXPECT_FALSE(update
.scroll_rect
.IsEmpty());
433 EXPECT_TRUE(update
.paint_rects
.empty());
435 EXPECT_EQ(scroll_rect
, update
.scroll_rect
);
436 EXPECT_EQ(expected_scroll_damage
, update
.GetScrollDamage());