ChromeVox should only intercept 'tab' key if nothing has focus.
[chromium-blink-merge.git] / cc / resources / picture_unittest.cc
blob07faf0cf4a6c557dc5332b85b4a212468e8b00dc
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/SkBBHFactory.h"
14 #include "third_party/skia/include/core/SkGraphics.h"
15 #include "ui/gfx/geometry/rect.h"
16 #include "ui/gfx/skia_util.h"
18 namespace cc {
19 namespace {
21 TEST(PictureTest, AsBase64String) {
22 SkGraphics::Init();
24 gfx::Rect layer_rect(100, 100);
26 SkTileGridFactory::TileGridInfo tile_grid_info;
27 tile_grid_info.fTileInterval = SkISize::Make(100, 100);
28 tile_grid_info.fMargin.setEmpty();
29 tile_grid_info.fOffset.setZero();
31 FakeContentLayerClient content_layer_client;
33 scoped_ptr<base::Value> tmp;
35 SkPaint red_paint;
36 red_paint.setColor(SkColorSetARGB(255, 255, 0, 0));
37 SkPaint green_paint;
38 green_paint.setColor(SkColorSetARGB(255, 0, 255, 0));
40 // Invalid picture (not a dict).
41 tmp.reset(new base::StringValue("abc!@#$%"));
42 scoped_refptr<Picture> invalid_picture =
43 Picture::CreateFromValue(tmp.get());
44 EXPECT_FALSE(invalid_picture.get());
46 // Single full-size rect picture.
47 content_layer_client.add_draw_rect(layer_rect, red_paint);
49 scoped_refptr<Picture> one_rect_picture =
50 Picture::Create(layer_rect,
51 &content_layer_client,
52 tile_grid_info,
53 false,
54 Picture::RECORD_NORMALLY);
55 scoped_ptr<base::Value> serialized_one_rect(one_rect_picture->AsValue());
57 // Reconstruct the picture.
58 scoped_refptr<Picture> one_rect_picture_check =
59 Picture::CreateFromValue(serialized_one_rect.get());
60 EXPECT_TRUE(!!one_rect_picture_check.get());
62 // Check for equivalence.
63 unsigned char one_rect_buffer[4 * 100 * 100] = {0};
64 DrawPicture(one_rect_buffer, layer_rect, one_rect_picture);
65 unsigned char one_rect_buffer_check[4 * 100 * 100] = {0};
66 DrawPicture(one_rect_buffer_check, layer_rect, one_rect_picture_check);
68 EXPECT_EQ(one_rect_picture->LayerRect(), one_rect_picture_check->LayerRect());
69 EXPECT_EQ(0, memcmp(one_rect_buffer, one_rect_buffer_check, 4 * 100 * 100));
71 // Two rect picture.
72 content_layer_client.add_draw_rect(gfx::Rect(25, 25, 50, 50), green_paint);
74 scoped_refptr<Picture> two_rect_picture =
75 Picture::Create(layer_rect,
76 &content_layer_client,
77 tile_grid_info,
78 false,
79 Picture::RECORD_NORMALLY);
81 scoped_ptr<base::Value> serialized_two_rect(two_rect_picture->AsValue());
83 // Reconstruct the picture.
84 scoped_refptr<Picture> two_rect_picture_check =
85 Picture::CreateFromValue(serialized_two_rect.get());
86 EXPECT_TRUE(!!two_rect_picture_check.get());
88 // Check for equivalence.
89 unsigned char two_rect_buffer[4 * 100 * 100] = {0};
90 DrawPicture(two_rect_buffer, layer_rect, two_rect_picture);
91 unsigned char two_rect_buffer_check[4 * 100 * 100] = {0};
92 DrawPicture(two_rect_buffer_check, layer_rect, two_rect_picture_check);
94 EXPECT_EQ(two_rect_picture->LayerRect(), two_rect_picture_check->LayerRect());
95 EXPECT_EQ(0, memcmp(two_rect_buffer, two_rect_buffer_check, 4 * 100 * 100));
98 TEST(PictureTest, PixelRefIterator) {
99 gfx::Rect layer_rect(2048, 2048);
101 SkTileGridFactory::TileGridInfo tile_grid_info;
102 tile_grid_info.fTileInterval = SkISize::Make(512, 512);
103 tile_grid_info.fMargin.setEmpty();
104 tile_grid_info.fOffset.setZero();
106 FakeContentLayerClient content_layer_client;
108 // Discardable pixel refs are found in the following grids:
109 // |---|---|---|---|
110 // | | x | | x |
111 // |---|---|---|---|
112 // | x | | x | |
113 // |---|---|---|---|
114 // | | x | | x |
115 // |---|---|---|---|
116 // | x | | x | |
117 // |---|---|---|---|
118 SkBitmap discardable_bitmap[4][4];
119 for (int y = 0; y < 4; ++y) {
120 for (int x = 0; x < 4; ++x) {
121 if ((x + y) & 1) {
122 CreateBitmap(
123 gfx::Size(500, 500), "discardable", &discardable_bitmap[y][x]);
124 SkPaint paint;
125 content_layer_client.add_draw_bitmap(
126 discardable_bitmap[y][x],
127 gfx::Point(x * 512 + 6, y * 512 + 6), paint);
132 scoped_refptr<Picture> picture = Picture::Create(layer_rect,
133 &content_layer_client,
134 tile_grid_info,
135 true,
136 Picture::RECORD_NORMALLY);
138 // Default iterator does not have any pixel refs
140 Picture::PixelRefIterator iterator;
141 EXPECT_FALSE(iterator);
143 for (int y = 0; y < 4; ++y) {
144 for (int x = 0; x < 4; ++x) {
145 Picture::PixelRefIterator iterator(gfx::Rect(x * 512, y * 512, 500, 500),
146 picture.get());
147 if ((x + y) & 1) {
148 EXPECT_TRUE(iterator) << x << " " << y;
149 EXPECT_TRUE(*iterator == discardable_bitmap[y][x].pixelRef()) << x <<
150 " " << y;
151 EXPECT_FALSE(++iterator) << x << " " << y;
152 } else {
153 EXPECT_FALSE(iterator) << x << " " << y;
157 // Capture 4 pixel refs.
159 Picture::PixelRefIterator iterator(gfx::Rect(512, 512, 2048, 2048),
160 picture.get());
161 EXPECT_TRUE(iterator);
162 EXPECT_TRUE(*iterator == discardable_bitmap[1][2].pixelRef());
163 EXPECT_TRUE(++iterator);
164 EXPECT_TRUE(*iterator == discardable_bitmap[2][1].pixelRef());
165 EXPECT_TRUE(++iterator);
166 EXPECT_TRUE(*iterator == discardable_bitmap[2][3].pixelRef());
167 EXPECT_TRUE(++iterator);
168 EXPECT_TRUE(*iterator == discardable_bitmap[3][2].pixelRef());
169 EXPECT_FALSE(++iterator);
172 // Copy test.
173 Picture::PixelRefIterator iterator(gfx::Rect(512, 512, 2048, 2048),
174 picture.get());
175 EXPECT_TRUE(iterator);
176 EXPECT_TRUE(*iterator == discardable_bitmap[1][2].pixelRef());
177 EXPECT_TRUE(++iterator);
178 EXPECT_TRUE(*iterator == discardable_bitmap[2][1].pixelRef());
180 // copy now points to the same spot as iterator,
181 // but both can be incremented independently.
182 Picture::PixelRefIterator copy = iterator;
183 EXPECT_TRUE(++iterator);
184 EXPECT_TRUE(*iterator == discardable_bitmap[2][3].pixelRef());
185 EXPECT_TRUE(++iterator);
186 EXPECT_TRUE(*iterator == discardable_bitmap[3][2].pixelRef());
187 EXPECT_FALSE(++iterator);
189 EXPECT_TRUE(copy);
190 EXPECT_TRUE(*copy == discardable_bitmap[2][1].pixelRef());
191 EXPECT_TRUE(++copy);
192 EXPECT_TRUE(*copy == discardable_bitmap[2][3].pixelRef());
193 EXPECT_TRUE(++copy);
194 EXPECT_TRUE(*copy == discardable_bitmap[3][2].pixelRef());
195 EXPECT_FALSE(++copy);
198 TEST(PictureTest, PixelRefIteratorNonZeroLayer) {
199 gfx::Rect layer_rect(1024, 0, 2048, 2048);
201 SkTileGridFactory::TileGridInfo tile_grid_info;
202 tile_grid_info.fTileInterval = SkISize::Make(512, 512);
203 tile_grid_info.fMargin.setEmpty();
204 tile_grid_info.fOffset.setZero();
206 FakeContentLayerClient content_layer_client;
208 // Discardable pixel refs are found in the following grids:
209 // |---|---|---|---|
210 // | | x | | x |
211 // |---|---|---|---|
212 // | x | | x | |
213 // |---|---|---|---|
214 // | | x | | x |
215 // |---|---|---|---|
216 // | x | | x | |
217 // |---|---|---|---|
218 SkBitmap discardable_bitmap[4][4];
219 for (int y = 0; y < 4; ++y) {
220 for (int x = 0; x < 4; ++x) {
221 if ((x + y) & 1) {
222 CreateBitmap(
223 gfx::Size(500, 500), "discardable", &discardable_bitmap[y][x]);
224 SkPaint paint;
225 content_layer_client.add_draw_bitmap(
226 discardable_bitmap[y][x],
227 gfx::Point(1024 + x * 512 + 6, y * 512 + 6), paint);
232 scoped_refptr<Picture> picture = Picture::Create(layer_rect,
233 &content_layer_client,
234 tile_grid_info,
235 true,
236 Picture::RECORD_NORMALLY);
238 // Default iterator does not have any pixel refs
240 Picture::PixelRefIterator iterator;
241 EXPECT_FALSE(iterator);
243 for (int y = 0; y < 4; ++y) {
244 for (int x = 0; x < 4; ++x) {
245 Picture::PixelRefIterator iterator(
246 gfx::Rect(1024 + x * 512, y * 512, 500, 500), picture.get());
247 if ((x + y) & 1) {
248 EXPECT_TRUE(iterator) << x << " " << y;
249 EXPECT_TRUE(*iterator == discardable_bitmap[y][x].pixelRef());
250 EXPECT_FALSE(++iterator) << x << " " << y;
251 } else {
252 EXPECT_FALSE(iterator) << x << " " << y;
256 // Capture 4 pixel refs.
258 Picture::PixelRefIterator iterator(gfx::Rect(1024 + 512, 512, 2048, 2048),
259 picture.get());
260 EXPECT_TRUE(iterator);
261 EXPECT_TRUE(*iterator == discardable_bitmap[1][2].pixelRef());
262 EXPECT_TRUE(++iterator);
263 EXPECT_TRUE(*iterator == discardable_bitmap[2][1].pixelRef());
264 EXPECT_TRUE(++iterator);
265 EXPECT_TRUE(*iterator == discardable_bitmap[2][3].pixelRef());
266 EXPECT_TRUE(++iterator);
267 EXPECT_TRUE(*iterator == discardable_bitmap[3][2].pixelRef());
268 EXPECT_FALSE(++iterator);
271 // Copy test.
273 Picture::PixelRefIterator iterator(gfx::Rect(1024 + 512, 512, 2048, 2048),
274 picture.get());
275 EXPECT_TRUE(iterator);
276 EXPECT_TRUE(*iterator == discardable_bitmap[1][2].pixelRef());
277 EXPECT_TRUE(++iterator);
278 EXPECT_TRUE(*iterator == discardable_bitmap[2][1].pixelRef());
280 // copy now points to the same spot as iterator,
281 // but both can be incremented independently.
282 Picture::PixelRefIterator copy = iterator;
283 EXPECT_TRUE(++iterator);
284 EXPECT_TRUE(*iterator == discardable_bitmap[2][3].pixelRef());
285 EXPECT_TRUE(++iterator);
286 EXPECT_TRUE(*iterator == discardable_bitmap[3][2].pixelRef());
287 EXPECT_FALSE(++iterator);
289 EXPECT_TRUE(copy);
290 EXPECT_TRUE(*copy == discardable_bitmap[2][1].pixelRef());
291 EXPECT_TRUE(++copy);
292 EXPECT_TRUE(*copy == discardable_bitmap[2][3].pixelRef());
293 EXPECT_TRUE(++copy);
294 EXPECT_TRUE(*copy == discardable_bitmap[3][2].pixelRef());
295 EXPECT_FALSE(++copy);
298 // Non intersecting rects
300 Picture::PixelRefIterator iterator(gfx::Rect(0, 0, 1000, 1000),
301 picture.get());
302 EXPECT_FALSE(iterator);
305 Picture::PixelRefIterator iterator(gfx::Rect(3500, 0, 1000, 1000),
306 picture.get());
307 EXPECT_FALSE(iterator);
310 Picture::PixelRefIterator iterator(gfx::Rect(0, 1100, 1000, 1000),
311 picture.get());
312 EXPECT_FALSE(iterator);
315 Picture::PixelRefIterator iterator(gfx::Rect(3500, 1100, 1000, 1000),
316 picture.get());
317 EXPECT_FALSE(iterator);
321 TEST(PictureTest, PixelRefIteratorOnePixelQuery) {
322 gfx::Rect layer_rect(2048, 2048);
324 SkTileGridFactory::TileGridInfo tile_grid_info;
325 tile_grid_info.fTileInterval = SkISize::Make(512, 512);
326 tile_grid_info.fMargin.setEmpty();
327 tile_grid_info.fOffset.setZero();
329 FakeContentLayerClient content_layer_client;
331 // Discardable pixel refs are found in the following grids:
332 // |---|---|---|---|
333 // | | x | | x |
334 // |---|---|---|---|
335 // | x | | x | |
336 // |---|---|---|---|
337 // | | x | | x |
338 // |---|---|---|---|
339 // | x | | x | |
340 // |---|---|---|---|
341 SkBitmap discardable_bitmap[4][4];
342 for (int y = 0; y < 4; ++y) {
343 for (int x = 0; x < 4; ++x) {
344 if ((x + y) & 1) {
345 CreateBitmap(
346 gfx::Size(500, 500), "discardable", &discardable_bitmap[y][x]);
347 SkPaint paint;
348 content_layer_client.add_draw_bitmap(
349 discardable_bitmap[y][x],
350 gfx::Point(x * 512 + 6, y * 512 + 6), paint);
355 scoped_refptr<Picture> picture = Picture::Create(layer_rect,
356 &content_layer_client,
357 tile_grid_info,
358 true,
359 Picture::RECORD_NORMALLY);
361 for (int y = 0; y < 4; ++y) {
362 for (int x = 0; x < 4; ++x) {
363 Picture::PixelRefIterator iterator(
364 gfx::Rect(x * 512, y * 512 + 256, 1, 1), picture.get());
365 if ((x + y) & 1) {
366 EXPECT_TRUE(iterator) << x << " " << y;
367 EXPECT_TRUE(*iterator == discardable_bitmap[y][x].pixelRef());
368 EXPECT_FALSE(++iterator) << x << " " << y;
369 } else {
370 EXPECT_FALSE(iterator) << x << " " << y;
376 TEST(PictureTest, CreateFromSkpValue) {
377 SkGraphics::Init();
379 gfx::Rect layer_rect(100, 200);
381 SkTileGridFactory::TileGridInfo tile_grid_info;
382 tile_grid_info.fTileInterval = SkISize::Make(100, 200);
383 tile_grid_info.fMargin.setEmpty();
384 tile_grid_info.fOffset.setZero();
386 FakeContentLayerClient content_layer_client;
388 scoped_ptr<base::Value> tmp;
390 SkPaint red_paint;
391 red_paint.setColor(SkColorSetARGB(255, 255, 0, 0));
392 SkPaint green_paint;
393 green_paint.setColor(SkColorSetARGB(255, 0, 255, 0));
395 // Invalid picture (not a dict).
396 tmp.reset(new base::StringValue("abc!@#$%"));
397 scoped_refptr<Picture> invalid_picture =
398 Picture::CreateFromSkpValue(tmp.get());
399 EXPECT_TRUE(!invalid_picture.get());
401 // Single full-size rect picture.
402 content_layer_client.add_draw_rect(layer_rect, red_paint);
403 scoped_refptr<Picture> one_rect_picture =
404 Picture::Create(layer_rect,
405 &content_layer_client,
406 tile_grid_info,
407 false,
408 Picture::RECORD_NORMALLY);
409 scoped_ptr<base::Value> serialized_one_rect(
410 one_rect_picture->AsValue());
412 const base::DictionaryValue* value = NULL;
413 EXPECT_TRUE(serialized_one_rect->GetAsDictionary(&value));
415 // Decode the picture from base64.
416 const base::Value* skp_value;
417 EXPECT_TRUE(value->Get("skp64", &skp_value));
419 // Reconstruct the picture.
420 scoped_refptr<Picture> one_rect_picture_check =
421 Picture::CreateFromSkpValue(skp_value);
422 EXPECT_TRUE(!!one_rect_picture_check.get());
424 EXPECT_EQ(100, one_rect_picture_check->LayerRect().width());
425 EXPECT_EQ(200, one_rect_picture_check->LayerRect().height());
428 TEST(PictureTest, RecordingModes) {
429 SkGraphics::Init();
431 gfx::Rect layer_rect(100, 200);
433 SkTileGridFactory::TileGridInfo tile_grid_info;
434 tile_grid_info.fTileInterval = SkISize::Make(100, 200);
435 tile_grid_info.fMargin.setEmpty();
436 tile_grid_info.fOffset.setZero();
438 FakeContentLayerClient content_layer_client;
439 EXPECT_EQ(NULL, content_layer_client.last_canvas());
441 scoped_refptr<Picture> picture = Picture::Create(layer_rect,
442 &content_layer_client,
443 tile_grid_info,
444 false,
445 Picture::RECORD_NORMALLY);
446 EXPECT_TRUE(content_layer_client.last_canvas() != NULL);
447 EXPECT_EQ(ContentLayerClient::GRAPHICS_CONTEXT_ENABLED,
448 content_layer_client.last_context_status());
449 EXPECT_TRUE(picture.get());
451 picture = Picture::Create(layer_rect,
452 &content_layer_client,
453 tile_grid_info,
454 false,
455 Picture::RECORD_WITH_SK_NULL_CANVAS);
456 EXPECT_TRUE(content_layer_client.last_canvas() != NULL);
457 EXPECT_EQ(ContentLayerClient::GRAPHICS_CONTEXT_ENABLED,
458 content_layer_client.last_context_status());
459 EXPECT_TRUE(picture.get());
461 picture = Picture::Create(layer_rect,
462 &content_layer_client,
463 tile_grid_info,
464 false,
465 Picture::RECORD_WITH_PAINTING_DISABLED);
466 EXPECT_TRUE(content_layer_client.last_canvas() != NULL);
467 EXPECT_EQ(ContentLayerClient::GRAPHICS_CONTEXT_DISABLED,
468 content_layer_client.last_context_status());
469 EXPECT_TRUE(picture.get());
471 EXPECT_EQ(3, Picture::RECORDING_MODE_COUNT);
474 } // namespace
475 } // namespace cc