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"
20 TEST(PictureTest
, AsBase64String
) {
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
;
32 red_paint
.setColor(SkColorSetARGB(255, 255, 0, 0));
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));
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
, CreateFromSkpValue
) {
91 gfx::Rect
layer_rect(100, 200);
93 gfx::Size
tile_grid_size(100, 200);
95 FakeContentLayerClient content_layer_client
;
97 scoped_ptr
<base::Value
> tmp
;
100 red_paint
.setColor(SkColorSetARGB(255, 255, 0, 0));
102 green_paint
.setColor(SkColorSetARGB(255, 0, 255, 0));
104 // Invalid picture (not a dict).
105 tmp
.reset(new base::StringValue("abc!@#$%"));
106 scoped_refptr
<Picture
> invalid_picture
=
107 Picture::CreateFromSkpValue(tmp
.get());
108 EXPECT_TRUE(!invalid_picture
.get());
110 // Single full-size rect picture.
111 content_layer_client
.add_draw_rect(layer_rect
, red_paint
);
112 scoped_refptr
<Picture
> one_rect_picture
=
113 Picture::Create(layer_rect
, &content_layer_client
, tile_grid_size
, false,
114 RecordingSource::RECORD_NORMALLY
);
115 scoped_ptr
<base::Value
> serialized_one_rect(
116 one_rect_picture
->AsValue());
118 const base::DictionaryValue
* value
= NULL
;
119 EXPECT_TRUE(serialized_one_rect
->GetAsDictionary(&value
));
121 // Decode the picture from base64.
122 const base::Value
* skp_value
;
123 EXPECT_TRUE(value
->Get("skp64", &skp_value
));
125 // Reconstruct the picture.
126 scoped_refptr
<Picture
> one_rect_picture_check
=
127 Picture::CreateFromSkpValue(skp_value
);
128 EXPECT_TRUE(!!one_rect_picture_check
.get());
130 EXPECT_EQ(100, one_rect_picture_check
->LayerRect().width());
131 EXPECT_EQ(200, one_rect_picture_check
->LayerRect().height());
134 TEST(PictureTest
, RecordingModes
) {
137 gfx::Rect
layer_rect(100, 200);
139 gfx::Size
tile_grid_size(100, 200);
141 FakeContentLayerClient content_layer_client
;
142 EXPECT_EQ(NULL
, content_layer_client
.last_canvas());
144 scoped_refptr
<Picture
> picture
=
145 Picture::Create(layer_rect
, &content_layer_client
, tile_grid_size
, false,
146 RecordingSource::RECORD_NORMALLY
);
147 EXPECT_TRUE(content_layer_client
.last_canvas() != NULL
);
148 EXPECT_EQ(ContentLayerClient::PAINTING_BEHAVIOR_NORMAL
,
149 content_layer_client
.last_painting_control());
150 EXPECT_TRUE(picture
.get());
152 picture
= Picture::Create(layer_rect
, &content_layer_client
, tile_grid_size
,
153 false, RecordingSource::RECORD_WITH_SK_NULL_CANVAS
);
154 EXPECT_TRUE(content_layer_client
.last_canvas() != NULL
);
155 EXPECT_EQ(ContentLayerClient::PAINTING_BEHAVIOR_NORMAL
,
156 content_layer_client
.last_painting_control());
157 EXPECT_TRUE(picture
.get());
160 Picture::Create(layer_rect
, &content_layer_client
, tile_grid_size
, false,
161 RecordingSource::RECORD_WITH_PAINTING_DISABLED
);
162 EXPECT_TRUE(content_layer_client
.last_canvas() != NULL
);
163 EXPECT_EQ(ContentLayerClient::DISPLAY_LIST_CONSTRUCTION_DISABLED
,
164 content_layer_client
.last_painting_control());
165 EXPECT_TRUE(picture
.get());
168 Picture::Create(layer_rect
, &content_layer_client
, tile_grid_size
, false,
169 RecordingSource::RECORD_WITH_CACHING_DISABLED
);
170 EXPECT_TRUE(content_layer_client
.last_canvas() != NULL
);
171 EXPECT_EQ(ContentLayerClient::DISPLAY_LIST_CACHING_DISABLED
,
172 content_layer_client
.last_painting_control());
173 EXPECT_TRUE(picture
.get());
175 EXPECT_EQ(4, RecordingSource::RECORDING_MODE_COUNT
);