OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "cc/resources/picture.h" |
| 6 |
| 7 #include "base/memory/ref_counted.h" |
| 8 #include "cc/test/fake_content_layer_client.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "third_party/skia/include/core/SkCanvas.h" |
| 11 #include "third_party/skia/include/core/SkDevice.h" |
| 12 #include "third_party/skia/include/core/SkGraphics.h" |
| 13 #include "third_party/skia/include/core/SkTileGridPicture.h" |
| 14 #include "ui/gfx/rect.h" |
| 15 #include "ui/gfx/skia_util.h" |
| 16 |
| 17 namespace cc { |
| 18 namespace { |
| 19 |
| 20 void DrawPicture(unsigned char* buffer, |
| 21 gfx::Rect layer_rect, |
| 22 scoped_refptr<Picture> picture) { |
| 23 SkBitmap bitmap; |
| 24 bitmap.setConfig(SkBitmap::kARGB_8888_Config, |
| 25 layer_rect.width(), |
| 26 layer_rect.height()); |
| 27 bitmap.setPixels(buffer); |
| 28 SkDevice device(bitmap); |
| 29 SkCanvas canvas(&device); |
| 30 canvas.clipRect(gfx::RectToSkRect(layer_rect)); |
| 31 picture->Raster(&canvas, layer_rect, 1.0f, false); |
| 32 } |
| 33 |
| 34 TEST(PictureTest, AsBase64String) { |
| 35 SkGraphics::Init(); |
| 36 |
| 37 gfx::Rect layer_rect(100, 100); |
| 38 |
| 39 SkTileGridPicture::TileGridInfo tile_grid_info; |
| 40 tile_grid_info.fTileInterval = SkISize::Make(100, 100); |
| 41 tile_grid_info.fMargin.setEmpty(); |
| 42 tile_grid_info.fOffset.setZero(); |
| 43 |
| 44 FakeContentLayerClient content_layer_client; |
| 45 |
| 46 SkPaint red_paint; |
| 47 red_paint.setColor(SkColorSetARGB(255, 255, 0, 0)); |
| 48 SkPaint green_paint; |
| 49 green_paint.setColor(SkColorSetARGB(255, 0, 255, 0)); |
| 50 |
| 51 // Invalid picture (not base64). |
| 52 scoped_refptr<Picture> invalid_picture = |
| 53 Picture::CreateFromBase64String("abc!@#$%"); |
| 54 EXPECT_TRUE(!invalid_picture); |
| 55 |
| 56 // Invalid picture (empty string). |
| 57 scoped_refptr<Picture> second_invalid_picture = |
| 58 Picture::CreateFromBase64String(""); |
| 59 EXPECT_TRUE(!second_invalid_picture); |
| 60 |
| 61 // Invalid picture (random base64 string). |
| 62 scoped_refptr<Picture> third_invalid_picture = |
| 63 Picture::CreateFromBase64String("ABCDABCDABCDABCDABCDABCDABCDABCDABCD" |
| 64 "ABCDABCDABCDABCDABCDABCDABCDABCDABCD" |
| 65 "ABCDABCDABCDABCDABCDABCDABCDABCDABCD" |
| 66 "ABCDABCDABCDABCDABCDABCDABCDABCDABCD" |
| 67 "ABCDABCDABCDABCDABCDABCDABCDABCDABCD" |
| 68 "ABCDABCDABCDABCDABCDABCDABCDABCDABCD" |
| 69 "ABCDABCDABCDABCDABCDABCDABCDABCDABCD"); |
| 70 EXPECT_TRUE(!third_invalid_picture); |
| 71 |
| 72 // Single full-size rect picture. |
| 73 content_layer_client.add_draw_rect(layer_rect, red_paint); |
| 74 scoped_refptr<Picture> one_rect_picture = Picture::Create(layer_rect); |
| 75 one_rect_picture->Record(&content_layer_client, NULL, tile_grid_info); |
| 76 std::string serialized_one_rect; |
| 77 one_rect_picture->AsBase64String(&serialized_one_rect); |
| 78 |
| 79 // Reconstruct the picture. |
| 80 scoped_refptr<Picture> one_rect_picture_check = |
| 81 Picture::CreateFromBase64String(serialized_one_rect); |
| 82 EXPECT_TRUE(!!one_rect_picture_check); |
| 83 |
| 84 // Check for equivalence. |
| 85 unsigned char one_rect_buffer[4 * 100 * 100] = {0}; |
| 86 DrawPicture(one_rect_buffer, layer_rect, one_rect_picture); |
| 87 unsigned char one_rect_buffer_check[4 * 100 * 100] = {0}; |
| 88 DrawPicture(one_rect_buffer_check, layer_rect, one_rect_picture_check); |
| 89 |
| 90 EXPECT_EQ(one_rect_picture->LayerRect(), |
| 91 one_rect_picture_check->LayerRect()); |
| 92 EXPECT_EQ(one_rect_picture->OpaqueRect(), |
| 93 one_rect_picture_check->OpaqueRect()); |
| 94 EXPECT_TRUE( |
| 95 memcmp(one_rect_buffer, one_rect_buffer_check, 4 * 100 * 100) == 0); |
| 96 |
| 97 // Two rect picture. |
| 98 content_layer_client.add_draw_rect(gfx::Rect(25, 25, 50, 50), green_paint); |
| 99 scoped_refptr<Picture> two_rect_picture = Picture::Create(layer_rect); |
| 100 two_rect_picture->Record(&content_layer_client, NULL, tile_grid_info); |
| 101 std::string serialized_two_rect; |
| 102 two_rect_picture->AsBase64String(&serialized_two_rect); |
| 103 |
| 104 // Reconstruct the picture. |
| 105 scoped_refptr<Picture> two_rect_picture_check = |
| 106 Picture::CreateFromBase64String(serialized_two_rect); |
| 107 EXPECT_TRUE(!!two_rect_picture_check); |
| 108 |
| 109 // Check for equivalence. |
| 110 unsigned char two_rect_buffer[4 * 100 * 100] = {0}; |
| 111 DrawPicture(two_rect_buffer, layer_rect, two_rect_picture); |
| 112 unsigned char two_rect_buffer_check[4 * 100 * 100] = {0}; |
| 113 DrawPicture(two_rect_buffer_check, layer_rect, two_rect_picture_check); |
| 114 |
| 115 EXPECT_EQ(two_rect_picture->LayerRect(), |
| 116 two_rect_picture_check->LayerRect()); |
| 117 EXPECT_EQ(two_rect_picture->OpaqueRect(), |
| 118 two_rect_picture_check->OpaqueRect()); |
| 119 EXPECT_TRUE( |
| 120 memcmp(two_rect_buffer, two_rect_buffer_check, 4 * 100 * 100) == 0); |
| 121 } |
| 122 |
| 123 } // namespace |
| 124 } // namespace cc |
OLD | NEW |