summaryrefslogtreecommitdiffstats
path: root/ppapi/tests/test_paint_aggregator.cc
blob: a04cff8a45ce9a70df7133d8528bf387eee931a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ppapi/tests/test_paint_aggregator.h"

#include "ppapi/tests/testing_instance.h"
#include "ppapi/utility/graphics/paint_aggregator.h"

REGISTER_TEST_CASE(PaintAggregator);

bool TestPaintAggregator::Init() {
  return true;
}

void TestPaintAggregator::RunTests(const std::string& filter) {
  RUN_TEST(InitialState, filter);
  RUN_TEST(SingleInvalidation, filter);
  RUN_TEST(DoubleDisjointInvalidation, filter);
  RUN_TEST(SingleScroll, filter);
  RUN_TEST(DoubleOverlappingScroll, filter);
  RUN_TEST(NegatingScroll, filter);
  RUN_TEST(DiagonalScroll, filter);
  RUN_TEST(ContainedPaintAfterScroll, filter);
  RUN_TEST(ContainedPaintBeforeScroll, filter);
  RUN_TEST(ContainedPaintsBeforeAndAfterScroll, filter);
  RUN_TEST(LargeContainedPaintAfterScroll, filter);
  RUN_TEST(LargeContainedPaintBeforeScroll, filter);
  RUN_TEST(OverlappingPaintBeforeScroll, filter);
  RUN_TEST(OverlappingPaintAfterScroll, filter);
  RUN_TEST(DisjointPaintBeforeScroll, filter);
  RUN_TEST(DisjointPaintAfterScroll, filter);
  RUN_TEST(ContainedPaintTrimmedByScroll, filter);
  RUN_TEST(ContainedPaintEliminatedByScroll, filter);
  RUN_TEST(ContainedPaintAfterScrollTrimmedByScrollDamage, filter);
  RUN_TEST(ContainedPaintAfterScrollEliminatedByScrollDamage, filter);
}

std::string TestPaintAggregator::TestInitialState() {
  pp::PaintAggregator greg;
  if (greg.HasPendingUpdate())
    return "Pending update invalid";
  PASS();
}

std::string TestPaintAggregator::TestSingleInvalidation() {
  pp::PaintAggregator greg;

  pp::Rect rect(2, 4, 10, 16);
  greg.InvalidateRect(rect);

  ASSERT_TRUE(greg.HasPendingUpdate());
  ASSERT_TRUE(greg.GetPendingUpdate().scroll_rect.IsEmpty());
  ASSERT_TRUE(1U == greg.GetPendingUpdate().paint_rects.size());

  ASSERT_TRUE(rect == greg.GetPendingUpdate().paint_rects[0]);

  PASS();
}

std::string TestPaintAggregator::TestDoubleDisjointInvalidation() {
  pp::PaintAggregator greg;

  pp::Rect r1(2, 4, 2, 4);
  pp::Rect r2(4, 2, 4, 2);

  greg.InvalidateRect(r1);
  greg.InvalidateRect(r2);

  pp::Rect expected_bounds = r1.Union(r2);

  ASSERT_TRUE(greg.HasPendingUpdate());
  ASSERT_TRUE(greg.GetPendingUpdate().scroll_rect.IsEmpty());
  ASSERT_TRUE(2U == greg.GetPendingUpdate().paint_rects.size());

  ASSERT_TRUE(expected_bounds == greg.GetPendingUpdate().paint_bounds);
  PASS();
}

std::string TestPaintAggregator::TestSingleScroll() {
  pp::PaintAggregator greg;

  pp::Rect r1(2, 4, 2, 4);
  pp::Rect r2(4, 2, 4, 2);

  greg.InvalidateRect(r1);
  greg.InvalidateRect(r2);

  pp::Rect expected_bounds = r1.Union(r2);

  ASSERT_TRUE(greg.HasPendingUpdate());
  ASSERT_TRUE(greg.GetPendingUpdate().scroll_rect.IsEmpty());
  ASSERT_TRUE(2U == greg.GetPendingUpdate().paint_rects.size());

  ASSERT_TRUE(expected_bounds == greg.GetPendingUpdate().paint_bounds);
  PASS();
}

std::string TestPaintAggregator::TestDoubleOverlappingScroll() {
  pp::PaintAggregator greg;

  pp::Rect rect(1, 2, 3, 4);
  pp::Point delta1(1, 0);
  pp::Point delta2(1, 0);
  greg.ScrollRect(rect, delta1);
  greg.ScrollRect(rect, delta2);

  ASSERT_TRUE(greg.HasPendingUpdate());
  ASSERT_TRUE(1U == greg.GetPendingUpdate().paint_rects.size());
  ASSERT_FALSE(greg.GetPendingUpdate().scroll_rect.IsEmpty());

  ASSERT_TRUE(rect == greg.GetPendingUpdate().scroll_rect);

  pp::Point expected_delta(delta1.x() + delta2.x(),
                            delta1.y() + delta2.y());
  ASSERT_TRUE(expected_delta.x() == greg.GetPendingUpdate().scroll_delta.x());
  ASSERT_TRUE(expected_delta.y() == greg.GetPendingUpdate().scroll_delta.y());

  pp::Rect resulting_damage = greg.GetPendingUpdate().paint_rects[0];
  pp::Rect expected_damage(1, 2, 2, 4);
  ASSERT_TRUE(expected_damage == resulting_damage);
  PASS();
}

std::string TestPaintAggregator::TestNegatingScroll() {
  pp::PaintAggregator greg;

  // Scroll twice in opposite directions by equal amounts.  The result
  // should be no scrolling.

  pp::Rect rect(1, 2, 3, 4);
  pp::Point delta1(1, 0);
  pp::Point delta2(-1, 0);
  greg.ScrollRect(rect, delta1);
  greg.ScrollRect(rect, delta2);

  ASSERT_FALSE(greg.HasPendingUpdate());
  PASS();
}

std::string TestPaintAggregator::TestDiagonalScroll() {
  pp::PaintAggregator greg;

  // We don't support optimized diagonal scrolling, so this should result in
  // repainting.

  pp::Rect rect(1, 2, 3, 4);
  pp::Point delta(1, 1);
  greg.ScrollRect(rect, delta);

  ASSERT_TRUE(greg.HasPendingUpdate());
  ASSERT_TRUE(greg.GetPendingUpdate().scroll_rect.IsEmpty());
  ASSERT_TRUE(1U == greg.GetPendingUpdate().paint_rects.size());

  ASSERT_TRUE(rect == greg.GetPendingUpdate().paint_rects[0]);
  PASS();
}

std::string TestPaintAggregator::TestContainedPaintAfterScroll() {
  pp::PaintAggregator greg;

  pp::Rect scroll_rect(0, 0, 10, 10);
  greg.ScrollRect(scroll_rect, pp::Point(2, 0));

  pp::Rect paint_rect(4, 4, 2, 2);
  greg.InvalidateRect(paint_rect);

  ASSERT_TRUE(greg.HasPendingUpdate());

  // Expecting a paint rect inside the scroll rect. The last paint rect is the
  // scroll dirty rect.
  ASSERT_FALSE(greg.GetPendingUpdate().scroll_rect.IsEmpty());
  ASSERT_TRUE(2U == greg.GetPendingUpdate().paint_rects.size());

  ASSERT_TRUE(scroll_rect == greg.GetPendingUpdate().scroll_rect);
  ASSERT_TRUE(paint_rect == greg.GetPendingUpdate().paint_rects[0]);
  PASS();
}

std::string TestPaintAggregator::TestContainedPaintBeforeScroll() {
  pp::PaintAggregator greg;

  pp::Rect paint_rect(4, 4, 2, 2);
  greg.InvalidateRect(paint_rect);

  pp::Rect scroll_rect(0, 0, 10, 10);
  greg.ScrollRect(scroll_rect, pp::Point(2, 0));

  ASSERT_TRUE(greg.HasPendingUpdate());

  // Expecting a paint rect inside the scroll rect. The last paint rect is the
  // scroll dirty rect.
  ASSERT_FALSE(greg.GetPendingUpdate().scroll_rect.IsEmpty());
  ASSERT_TRUE(2U == greg.GetPendingUpdate().paint_rects.size());

  paint_rect.Offset(2, 0);

  ASSERT_TRUE(scroll_rect == greg.GetPendingUpdate().scroll_rect);
  ASSERT_TRUE(paint_rect == greg.GetPendingUpdate().paint_rects[0]);
  PASS();
}

std::string TestPaintAggregator::TestContainedPaintsBeforeAndAfterScroll() {
  pp::PaintAggregator greg;

  pp::Rect paint_rect1(4, 4, 2, 2);
  greg.InvalidateRect(paint_rect1);

  pp::Rect scroll_rect(0, 0, 10, 10);
  greg.ScrollRect(scroll_rect, pp::Point(2, 0));

  pp::Rect paint_rect2(6, 4, 2, 2);
  greg.InvalidateRect(paint_rect2);

  pp::Rect expected_paint_rect = paint_rect2;

  ASSERT_TRUE(greg.HasPendingUpdate());

  // Expecting a paint rect inside the scroll rect
  ASSERT_FALSE(greg.GetPendingUpdate().scroll_rect.IsEmpty());
  ASSERT_TRUE(2U == greg.GetPendingUpdate().paint_rects.size());

  ASSERT_TRUE(scroll_rect == greg.GetPendingUpdate().scroll_rect);
  ASSERT_TRUE(expected_paint_rect == greg.GetPendingUpdate().paint_rects[0]);
  PASS();
}

std::string TestPaintAggregator::TestLargeContainedPaintAfterScroll() {
  pp::PaintAggregator greg;

  pp::Rect scroll_rect(0, 0, 10, 10);
  greg.ScrollRect(scroll_rect, pp::Point(0, 1));

  pp::Rect paint_rect(0, 0, 10, 9);  // Repaint 90%
  greg.InvalidateRect(paint_rect);

  ASSERT_TRUE(greg.HasPendingUpdate());

  ASSERT_TRUE(greg.GetPendingUpdate().scroll_rect.IsEmpty());
  ASSERT_TRUE(1U == greg.GetPendingUpdate().paint_rects.size());

  ASSERT_TRUE(scroll_rect == greg.GetPendingUpdate().paint_rects[0]);
  PASS();
}

std::string TestPaintAggregator::TestLargeContainedPaintBeforeScroll() {
  pp::PaintAggregator greg;

  pp::Rect paint_rect(0, 0, 10, 9);  // Repaint 90%
  greg.InvalidateRect(paint_rect);

  pp::Rect scroll_rect(0, 0, 10, 10);
  greg.ScrollRect(scroll_rect, pp::Point(0, 1));

  ASSERT_TRUE(greg.HasPendingUpdate());

  ASSERT_TRUE(greg.GetPendingUpdate().scroll_rect.IsEmpty());
  ASSERT_TRUE(1U == greg.GetPendingUpdate().paint_rects.size());

  ASSERT_TRUE(scroll_rect == greg.GetPendingUpdate().paint_rects[0]);
  PASS();
}

std::string TestPaintAggregator::TestOverlappingPaintBeforeScroll() {
  pp::PaintAggregator greg;

  pp::Rect paint_rect(4, 4, 10, 2);
  greg.InvalidateRect(paint_rect);

  pp::Rect scroll_rect(0, 0, 10, 10);
  greg.ScrollRect(scroll_rect, pp::Point(2, 0));

  pp::Rect expected_paint_rect = scroll_rect.Union(paint_rect);

  ASSERT_TRUE(greg.HasPendingUpdate());

  ASSERT_TRUE(greg.GetPendingUpdate().scroll_rect.IsEmpty());
  ASSERT_TRUE(1U == greg.GetPendingUpdate().paint_rects.size());

  ASSERT_TRUE(expected_paint_rect == greg.GetPendingUpdate().paint_rects[0]);
  PASS();
}

std::string TestPaintAggregator::TestOverlappingPaintAfterScroll() {
  pp::PaintAggregator greg;

  pp::Rect scroll_rect(0, 0, 10, 10);
  greg.ScrollRect(scroll_rect, pp::Point(2, 0));

  pp::Rect paint_rect(4, 4, 10, 2);
  greg.InvalidateRect(paint_rect);

  pp::Rect expected_paint_rect = scroll_rect.Union(paint_rect);

  ASSERT_TRUE(greg.HasPendingUpdate());

  ASSERT_TRUE(greg.GetPendingUpdate().scroll_rect.IsEmpty());
  ASSERT_TRUE(1U == greg.GetPendingUpdate().paint_rects.size());

  ASSERT_TRUE(expected_paint_rect == greg.GetPendingUpdate().paint_rects[0]);
  PASS();
}

std::string TestPaintAggregator::TestDisjointPaintBeforeScroll() {
  pp::PaintAggregator greg;

  pp::Rect paint_rect(4, 4, 10, 2);
  greg.InvalidateRect(paint_rect);

  pp::Rect scroll_rect(0, 0, 2, 10);
  greg.ScrollRect(scroll_rect, pp::Point(2, 0));

  ASSERT_TRUE(greg.HasPendingUpdate());

  ASSERT_FALSE(greg.GetPendingUpdate().scroll_rect.IsEmpty());
  ASSERT_TRUE(2U == greg.GetPendingUpdate().paint_rects.size());

  ASSERT_TRUE(paint_rect == greg.GetPendingUpdate().paint_rects[0]);
  ASSERT_TRUE(scroll_rect == greg.GetPendingUpdate().scroll_rect);
  PASS();
}

std::string TestPaintAggregator::TestDisjointPaintAfterScroll() {
  pp::PaintAggregator greg;

  pp::Rect scroll_rect(0, 0, 2, 10);
  greg.ScrollRect(scroll_rect, pp::Point(2, 0));

  pp::Rect paint_rect(4, 4, 10, 2);
  greg.InvalidateRect(paint_rect);

  ASSERT_TRUE(greg.HasPendingUpdate());

  ASSERT_FALSE(greg.GetPendingUpdate().scroll_rect.IsEmpty());
  ASSERT_TRUE(2U == greg.GetPendingUpdate().paint_rects.size());

  ASSERT_TRUE(paint_rect == greg.GetPendingUpdate().paint_rects[0]);
  ASSERT_TRUE(scroll_rect == greg.GetPendingUpdate().scroll_rect);
  PASS();
}

std::string TestPaintAggregator::TestContainedPaintTrimmedByScroll() {
  pp::PaintAggregator greg;

  pp::Rect paint_rect(4, 4, 6, 6);
  greg.InvalidateRect(paint_rect);

  pp::Rect scroll_rect(0, 0, 10, 10);
  greg.ScrollRect(scroll_rect, pp::Point(2, 0));

  // The paint rect should have become narrower.
  pp::Rect expected_paint_rect(6, 4, 4, 6);

  ASSERT_TRUE(greg.HasPendingUpdate());

  ASSERT_FALSE(greg.GetPendingUpdate().scroll_rect.IsEmpty());
  ASSERT_TRUE(2U == greg.GetPendingUpdate().paint_rects.size());

  ASSERT_TRUE(expected_paint_rect == greg.GetPendingUpdate().paint_rects[0]);
  ASSERT_TRUE(scroll_rect == greg.GetPendingUpdate().scroll_rect);
  PASS();
}

std::string TestPaintAggregator::TestContainedPaintEliminatedByScroll() {
  pp::PaintAggregator greg;

  pp::Rect paint_rect(4, 4, 6, 6);
  greg.InvalidateRect(paint_rect);

  pp::Rect scroll_rect(0, 0, 10, 10);
  greg.ScrollRect(scroll_rect, pp::Point(6, 0));

  ASSERT_TRUE(greg.HasPendingUpdate());

  ASSERT_FALSE(greg.GetPendingUpdate().scroll_rect.IsEmpty());
  ASSERT_TRUE(1U == greg.GetPendingUpdate().paint_rects.size());

  ASSERT_TRUE(scroll_rect == greg.GetPendingUpdate().scroll_rect);
  PASS();
}

std::string
TestPaintAggregator::TestContainedPaintAfterScrollTrimmedByScrollDamage() {
  pp::PaintAggregator greg;

  pp::Rect scroll_rect(0, 0, 10, 10);
  greg.ScrollRect(scroll_rect, pp::Point(4, 0));

  pp::Rect paint_rect(2, 0, 4, 10);
  greg.InvalidateRect(paint_rect);

  pp::Rect expected_scroll_damage(0, 0, 4, 10);
  pp::Rect expected_paint_rect(4, 0, 2, 10);

  ASSERT_TRUE(greg.HasPendingUpdate());

  ASSERT_FALSE(greg.GetPendingUpdate().scroll_rect.IsEmpty());
  ASSERT_TRUE(2U == greg.GetPendingUpdate().paint_rects.size());

  ASSERT_TRUE(scroll_rect == greg.GetPendingUpdate().scroll_rect);
  ASSERT_TRUE(expected_scroll_damage == greg.GetPendingUpdate().paint_rects[1]);
  ASSERT_TRUE(expected_paint_rect == greg.GetPendingUpdate().paint_rects[0]);
  PASS();
}

std::string
TestPaintAggregator::TestContainedPaintAfterScrollEliminatedByScrollDamage() {
  pp::PaintAggregator greg;

  pp::Rect scroll_rect(0, 0, 10, 10);
  greg.ScrollRect(scroll_rect, pp::Point(4, 0));

  pp::Rect paint_rect(2, 0, 2, 10);
  greg.InvalidateRect(paint_rect);

  pp::Rect expected_scroll_damage(0, 0, 4, 10);

  ASSERT_TRUE(greg.HasPendingUpdate());

  ASSERT_FALSE(greg.GetPendingUpdate().scroll_rect.IsEmpty());
  ASSERT_TRUE(1U == greg.GetPendingUpdate().paint_rects.size());

  ASSERT_TRUE(scroll_rect == greg.GetPendingUpdate().scroll_rect);
  ASSERT_TRUE(expected_scroll_damage == greg.GetPendingUpdate().paint_rects[0]);
  PASS();
}