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