Enable right clicking on the applist doodle web contents and log the data.
[chromium-blink-merge.git] / cc / resources / picture_unittest.cc
blob1e2ba44fe3bc0619f45d70f1ce9d2dfa53915792
1 // Copyright 2013 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/resources/picture.h"
7 #include "base/memory/ref_counted.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/values.h"
10 #include "cc/test/fake_content_layer_client.h"
11 #include "cc/test/skia_common.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/skia/include/core/SkGraphics.h"
14 #include "ui/gfx/geometry/rect.h"
15 #include "ui/gfx/skia_util.h"
17 namespace cc {
18 namespace {
20 TEST(PictureTest, AsBase64String) {
21 SkGraphics::Init();
23 gfx::Rect layer_rect(100, 100);
25 gfx::Size tile_grid_size(100, 100);
27 FakeContentLayerClient content_layer_client;
29 scoped_ptr<base::Value> tmp;
31 SkPaint red_paint;
32 red_paint.setColor(SkColorSetARGB(255, 255, 0, 0));
33 SkPaint green_paint;
34 green_paint.setColor(SkColorSetARGB(255, 0, 255, 0));
36 // Invalid picture (not a dict).
37 tmp.reset(new base::StringValue("abc!@#$%"));
38 scoped_refptr<Picture> invalid_picture =
39 Picture::CreateFromValue(tmp.get());
40 EXPECT_FALSE(invalid_picture.get());
42 // Single full-size rect picture.
43 content_layer_client.add_draw_rect(layer_rect, red_paint);
45 scoped_refptr<Picture> one_rect_picture =
46 Picture::Create(layer_rect, &content_layer_client, tile_grid_size, false,
47 RecordingSource::RECORD_NORMALLY);
48 scoped_ptr<base::Value> serialized_one_rect(one_rect_picture->AsValue());
50 // Reconstruct the picture.
51 scoped_refptr<Picture> one_rect_picture_check =
52 Picture::CreateFromValue(serialized_one_rect.get());
53 EXPECT_TRUE(!!one_rect_picture_check.get());
55 // Check for equivalence.
56 unsigned char one_rect_buffer[4 * 100 * 100] = {0};
57 DrawPicture(one_rect_buffer, layer_rect, one_rect_picture);
58 unsigned char one_rect_buffer_check[4 * 100 * 100] = {0};
59 DrawPicture(one_rect_buffer_check, layer_rect, one_rect_picture_check);
61 EXPECT_EQ(one_rect_picture->LayerRect(), one_rect_picture_check->LayerRect());
62 EXPECT_EQ(0, memcmp(one_rect_buffer, one_rect_buffer_check, 4 * 100 * 100));
64 // Two rect picture.
65 content_layer_client.add_draw_rect(gfx::Rect(25, 25, 50, 50), green_paint);
67 scoped_refptr<Picture> two_rect_picture =
68 Picture::Create(layer_rect, &content_layer_client, tile_grid_size, false,
69 RecordingSource::RECORD_NORMALLY);
71 scoped_ptr<base::Value> serialized_two_rect(two_rect_picture->AsValue());
73 // Reconstruct the picture.
74 scoped_refptr<Picture> two_rect_picture_check =
75 Picture::CreateFromValue(serialized_two_rect.get());
76 EXPECT_TRUE(!!two_rect_picture_check.get());
78 // Check for equivalence.
79 unsigned char two_rect_buffer[4 * 100 * 100] = {0};
80 DrawPicture(two_rect_buffer, layer_rect, two_rect_picture);
81 unsigned char two_rect_buffer_check[4 * 100 * 100] = {0};
82 DrawPicture(two_rect_buffer_check, layer_rect, two_rect_picture_check);
84 EXPECT_EQ(two_rect_picture->LayerRect(), two_rect_picture_check->LayerRect());
85 EXPECT_EQ(0, memcmp(two_rect_buffer, two_rect_buffer_check, 4 * 100 * 100));
88 TEST(PictureTest, PixelRefIterator) {
89 gfx::Rect layer_rect(2048, 2048);
91 gfx::Size tile_grid_size(512, 512);
93 FakeContentLayerClient content_layer_client;
95 // Discardable pixel refs are found in the following grids:
96 // |---|---|---|---|
97 // | | x | | x |
98 // |---|---|---|---|
99 // | x | | x | |
100 // |---|---|---|---|
101 // | | x | | x |
102 // |---|---|---|---|
103 // | x | | x | |
104 // |---|---|---|---|
105 SkBitmap discardable_bitmap[4][4];
106 for (int y = 0; y < 4; ++y) {
107 for (int x = 0; x < 4; ++x) {
108 if ((x + y) & 1) {
109 CreateBitmap(
110 gfx::Size(500, 500), "discardable", &discardable_bitmap[y][x]);
111 SkPaint paint;
112 content_layer_client.add_draw_bitmap(
113 discardable_bitmap[y][x],
114 gfx::Point(x * 512 + 6, y * 512 + 6), paint);
119 scoped_refptr<Picture> picture =
120 Picture::Create(layer_rect, &content_layer_client, tile_grid_size, true,
121 RecordingSource::RECORD_NORMALLY);
123 // Default iterator does not have any pixel refs
125 Picture::PixelRefIterator iterator;
126 EXPECT_FALSE(iterator);
128 for (int y = 0; y < 4; ++y) {
129 for (int x = 0; x < 4; ++x) {
130 Picture::PixelRefIterator iterator(gfx::Rect(x * 512, y * 512, 500, 500),
131 picture.get());
132 if ((x + y) & 1) {
133 EXPECT_TRUE(iterator) << x << " " << y;
134 EXPECT_TRUE(*iterator == discardable_bitmap[y][x].pixelRef()) << x <<
135 " " << y;
136 EXPECT_FALSE(++iterator) << x << " " << y;
137 } else {
138 EXPECT_FALSE(iterator) << x << " " << y;
142 // Capture 4 pixel refs.
144 Picture::PixelRefIterator iterator(gfx::Rect(512, 512, 2048, 2048),
145 picture.get());
146 EXPECT_TRUE(iterator);
147 EXPECT_TRUE(*iterator == discardable_bitmap[1][2].pixelRef());
148 EXPECT_TRUE(++iterator);
149 EXPECT_TRUE(*iterator == discardable_bitmap[2][1].pixelRef());
150 EXPECT_TRUE(++iterator);
151 EXPECT_TRUE(*iterator == discardable_bitmap[2][3].pixelRef());
152 EXPECT_TRUE(++iterator);
153 EXPECT_TRUE(*iterator == discardable_bitmap[3][2].pixelRef());
154 EXPECT_FALSE(++iterator);
157 // Copy test.
158 Picture::PixelRefIterator iterator(gfx::Rect(512, 512, 2048, 2048),
159 picture.get());
160 EXPECT_TRUE(iterator);
161 EXPECT_TRUE(*iterator == discardable_bitmap[1][2].pixelRef());
162 EXPECT_TRUE(++iterator);
163 EXPECT_TRUE(*iterator == discardable_bitmap[2][1].pixelRef());
165 // copy now points to the same spot as iterator,
166 // but both can be incremented independently.
167 Picture::PixelRefIterator copy = iterator;
168 EXPECT_TRUE(++iterator);
169 EXPECT_TRUE(*iterator == discardable_bitmap[2][3].pixelRef());
170 EXPECT_TRUE(++iterator);
171 EXPECT_TRUE(*iterator == discardable_bitmap[3][2].pixelRef());
172 EXPECT_FALSE(++iterator);
174 EXPECT_TRUE(copy);
175 EXPECT_TRUE(*copy == discardable_bitmap[2][1].pixelRef());
176 EXPECT_TRUE(++copy);
177 EXPECT_TRUE(*copy == discardable_bitmap[2][3].pixelRef());
178 EXPECT_TRUE(++copy);
179 EXPECT_TRUE(*copy == discardable_bitmap[3][2].pixelRef());
180 EXPECT_FALSE(++copy);
183 TEST(PictureTest, PixelRefIteratorNonZeroLayer) {
184 gfx::Rect layer_rect(1024, 0, 2048, 2048);
186 gfx::Size tile_grid_size(512, 512);
188 FakeContentLayerClient content_layer_client;
190 // Discardable pixel refs are found in the following grids:
191 // |---|---|---|---|
192 // | | x | | x |
193 // |---|---|---|---|
194 // | x | | x | |
195 // |---|---|---|---|
196 // | | x | | x |
197 // |---|---|---|---|
198 // | x | | x | |
199 // |---|---|---|---|
200 SkBitmap discardable_bitmap[4][4];
201 for (int y = 0; y < 4; ++y) {
202 for (int x = 0; x < 4; ++x) {
203 if ((x + y) & 1) {
204 CreateBitmap(
205 gfx::Size(500, 500), "discardable", &discardable_bitmap[y][x]);
206 SkPaint paint;
207 content_layer_client.add_draw_bitmap(
208 discardable_bitmap[y][x],
209 gfx::Point(1024 + x * 512 + 6, y * 512 + 6), paint);
214 scoped_refptr<Picture> picture =
215 Picture::Create(layer_rect, &content_layer_client, tile_grid_size, true,
216 RecordingSource::RECORD_NORMALLY);
218 // Default iterator does not have any pixel refs
220 Picture::PixelRefIterator iterator;
221 EXPECT_FALSE(iterator);
223 for (int y = 0; y < 4; ++y) {
224 for (int x = 0; x < 4; ++x) {
225 Picture::PixelRefIterator iterator(
226 gfx::Rect(1024 + x * 512, y * 512, 500, 500), picture.get());
227 if ((x + y) & 1) {
228 EXPECT_TRUE(iterator) << x << " " << y;
229 EXPECT_TRUE(*iterator == discardable_bitmap[y][x].pixelRef());
230 EXPECT_FALSE(++iterator) << x << " " << y;
231 } else {
232 EXPECT_FALSE(iterator) << x << " " << y;
236 // Capture 4 pixel refs.
238 Picture::PixelRefIterator iterator(gfx::Rect(1024 + 512, 512, 2048, 2048),
239 picture.get());
240 EXPECT_TRUE(iterator);
241 EXPECT_TRUE(*iterator == discardable_bitmap[1][2].pixelRef());
242 EXPECT_TRUE(++iterator);
243 EXPECT_TRUE(*iterator == discardable_bitmap[2][1].pixelRef());
244 EXPECT_TRUE(++iterator);
245 EXPECT_TRUE(*iterator == discardable_bitmap[2][3].pixelRef());
246 EXPECT_TRUE(++iterator);
247 EXPECT_TRUE(*iterator == discardable_bitmap[3][2].pixelRef());
248 EXPECT_FALSE(++iterator);
251 // Copy test.
253 Picture::PixelRefIterator iterator(gfx::Rect(1024 + 512, 512, 2048, 2048),
254 picture.get());
255 EXPECT_TRUE(iterator);
256 EXPECT_TRUE(*iterator == discardable_bitmap[1][2].pixelRef());
257 EXPECT_TRUE(++iterator);
258 EXPECT_TRUE(*iterator == discardable_bitmap[2][1].pixelRef());
260 // copy now points to the same spot as iterator,
261 // but both can be incremented independently.
262 Picture::PixelRefIterator copy = iterator;
263 EXPECT_TRUE(++iterator);
264 EXPECT_TRUE(*iterator == discardable_bitmap[2][3].pixelRef());
265 EXPECT_TRUE(++iterator);
266 EXPECT_TRUE(*iterator == discardable_bitmap[3][2].pixelRef());
267 EXPECT_FALSE(++iterator);
269 EXPECT_TRUE(copy);
270 EXPECT_TRUE(*copy == discardable_bitmap[2][1].pixelRef());
271 EXPECT_TRUE(++copy);
272 EXPECT_TRUE(*copy == discardable_bitmap[2][3].pixelRef());
273 EXPECT_TRUE(++copy);
274 EXPECT_TRUE(*copy == discardable_bitmap[3][2].pixelRef());
275 EXPECT_FALSE(++copy);
278 // Non intersecting rects
280 Picture::PixelRefIterator iterator(gfx::Rect(0, 0, 1000, 1000),
281 picture.get());
282 EXPECT_FALSE(iterator);
285 Picture::PixelRefIterator iterator(gfx::Rect(3500, 0, 1000, 1000),
286 picture.get());
287 EXPECT_FALSE(iterator);
290 Picture::PixelRefIterator iterator(gfx::Rect(0, 1100, 1000, 1000),
291 picture.get());
292 EXPECT_FALSE(iterator);
295 Picture::PixelRefIterator iterator(gfx::Rect(3500, 1100, 1000, 1000),
296 picture.get());
297 EXPECT_FALSE(iterator);
301 TEST(PictureTest, PixelRefIteratorOnePixelQuery) {
302 gfx::Rect layer_rect(2048, 2048);
304 gfx::Size tile_grid_size(512, 512);
306 FakeContentLayerClient content_layer_client;
308 // Discardable pixel refs are found in the following grids:
309 // |---|---|---|---|
310 // | | x | | x |
311 // |---|---|---|---|
312 // | x | | x | |
313 // |---|---|---|---|
314 // | | x | | x |
315 // |---|---|---|---|
316 // | x | | x | |
317 // |---|---|---|---|
318 SkBitmap discardable_bitmap[4][4];
319 for (int y = 0; y < 4; ++y) {
320 for (int x = 0; x < 4; ++x) {
321 if ((x + y) & 1) {
322 CreateBitmap(
323 gfx::Size(500, 500), "discardable", &discardable_bitmap[y][x]);
324 SkPaint paint;
325 content_layer_client.add_draw_bitmap(
326 discardable_bitmap[y][x],
327 gfx::Point(x * 512 + 6, y * 512 + 6), paint);
332 scoped_refptr<Picture> picture =
333 Picture::Create(layer_rect, &content_layer_client, tile_grid_size, true,
334 RecordingSource::RECORD_NORMALLY);
336 for (int y = 0; y < 4; ++y) {
337 for (int x = 0; x < 4; ++x) {
338 Picture::PixelRefIterator iterator(
339 gfx::Rect(x * 512, y * 512 + 256, 1, 1), picture.get());
340 if ((x + y) & 1) {
341 EXPECT_TRUE(iterator) << x << " " << y;
342 EXPECT_TRUE(*iterator == discardable_bitmap[y][x].pixelRef());
343 EXPECT_FALSE(++iterator) << x << " " << y;
344 } else {
345 EXPECT_FALSE(iterator) << x << " " << y;
351 TEST(PictureTest, CreateFromSkpValue) {
352 SkGraphics::Init();
354 gfx::Rect layer_rect(100, 200);
356 gfx::Size tile_grid_size(100, 200);
358 FakeContentLayerClient content_layer_client;
360 scoped_ptr<base::Value> tmp;
362 SkPaint red_paint;
363 red_paint.setColor(SkColorSetARGB(255, 255, 0, 0));
364 SkPaint green_paint;
365 green_paint.setColor(SkColorSetARGB(255, 0, 255, 0));
367 // Invalid picture (not a dict).
368 tmp.reset(new base::StringValue("abc!@#$%"));
369 scoped_refptr<Picture> invalid_picture =
370 Picture::CreateFromSkpValue(tmp.get());
371 EXPECT_TRUE(!invalid_picture.get());
373 // Single full-size rect picture.
374 content_layer_client.add_draw_rect(layer_rect, red_paint);
375 scoped_refptr<Picture> one_rect_picture =
376 Picture::Create(layer_rect, &content_layer_client, tile_grid_size, false,
377 RecordingSource::RECORD_NORMALLY);
378 scoped_ptr<base::Value> serialized_one_rect(
379 one_rect_picture->AsValue());
381 const base::DictionaryValue* value = NULL;
382 EXPECT_TRUE(serialized_one_rect->GetAsDictionary(&value));
384 // Decode the picture from base64.
385 const base::Value* skp_value;
386 EXPECT_TRUE(value->Get("skp64", &skp_value));
388 // Reconstruct the picture.
389 scoped_refptr<Picture> one_rect_picture_check =
390 Picture::CreateFromSkpValue(skp_value);
391 EXPECT_TRUE(!!one_rect_picture_check.get());
393 EXPECT_EQ(100, one_rect_picture_check->LayerRect().width());
394 EXPECT_EQ(200, one_rect_picture_check->LayerRect().height());
397 TEST(PictureTest, RecordingModes) {
398 SkGraphics::Init();
400 gfx::Rect layer_rect(100, 200);
402 gfx::Size tile_grid_size(100, 200);
404 FakeContentLayerClient content_layer_client;
405 EXPECT_EQ(NULL, content_layer_client.last_canvas());
407 scoped_refptr<Picture> picture =
408 Picture::Create(layer_rect, &content_layer_client, tile_grid_size, false,
409 RecordingSource::RECORD_NORMALLY);
410 EXPECT_TRUE(content_layer_client.last_canvas() != NULL);
411 EXPECT_EQ(ContentLayerClient::PAINTING_BEHAVIOR_NORMAL,
412 content_layer_client.last_painting_control());
413 EXPECT_TRUE(picture.get());
415 picture = Picture::Create(layer_rect, &content_layer_client, tile_grid_size,
416 false, RecordingSource::RECORD_WITH_SK_NULL_CANVAS);
417 EXPECT_TRUE(content_layer_client.last_canvas() != NULL);
418 EXPECT_EQ(ContentLayerClient::PAINTING_BEHAVIOR_NORMAL,
419 content_layer_client.last_painting_control());
420 EXPECT_TRUE(picture.get());
422 picture =
423 Picture::Create(layer_rect, &content_layer_client, tile_grid_size, false,
424 RecordingSource::RECORD_WITH_PAINTING_DISABLED);
425 EXPECT_TRUE(content_layer_client.last_canvas() != NULL);
426 EXPECT_EQ(ContentLayerClient::DISPLAY_LIST_CONSTRUCTION_DISABLED,
427 content_layer_client.last_painting_control());
428 EXPECT_TRUE(picture.get());
430 picture =
431 Picture::Create(layer_rect, &content_layer_client, tile_grid_size, false,
432 RecordingSource::RECORD_WITH_CACHING_DISABLED);
433 EXPECT_TRUE(content_layer_client.last_canvas() != NULL);
434 EXPECT_EQ(ContentLayerClient::DISPLAY_LIST_CACHING_DISABLED,
435 content_layer_client.last_painting_control());
436 EXPECT_TRUE(picture.get());
438 EXPECT_EQ(4, RecordingSource::RECORDING_MODE_COUNT);
441 } // namespace
442 } // namespace cc