summaryrefslogtreecommitdiffstats
path: root/cc/playback/picture_unittest.cc
blob: 53310e37c8140a9fee93d7227f89adb5206d1146 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cc/playback/picture.h"

#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/values.h"
#include "cc/test/fake_content_layer_client.h"
#include "cc/test/skia_common.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkGraphics.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/skia_util.h"

namespace cc {
namespace {

TEST(PictureTest, AsBase64String) {
  SkGraphics::Init();

  gfx::Rect layer_rect(100, 100);

  gfx::Size tile_grid_size(100, 100);

  FakeContentLayerClient content_layer_client;

  scoped_ptr<base::Value> tmp;

  SkPaint red_paint;
  red_paint.setColor(SkColorSetARGB(255, 255, 0, 0));
  SkPaint green_paint;
  green_paint.setColor(SkColorSetARGB(255, 0, 255, 0));

  // Invalid picture (not a dict).
  tmp.reset(new base::StringValue("abc!@#$%"));
  scoped_refptr<Picture> invalid_picture =
      Picture::CreateFromValue(tmp.get());
  EXPECT_FALSE(invalid_picture.get());

  // Single full-size rect picture.
  content_layer_client.add_draw_rect(layer_rect, red_paint);

  scoped_refptr<Picture> one_rect_picture =
      Picture::Create(layer_rect, &content_layer_client, tile_grid_size, false,
                      RecordingSource::RECORD_NORMALLY);
  scoped_ptr<base::Value> serialized_one_rect(one_rect_picture->AsValue());

  // Reconstruct the picture.
  scoped_refptr<Picture> one_rect_picture_check =
      Picture::CreateFromValue(serialized_one_rect.get());
  EXPECT_TRUE(one_rect_picture_check);

  // Check for equivalence.
  unsigned char one_rect_buffer[4 * 100 * 100] = {0};
  DrawPicture(one_rect_buffer, layer_rect, one_rect_picture);
  unsigned char one_rect_buffer_check[4 * 100 * 100] = {0};
  DrawPicture(one_rect_buffer_check, layer_rect, one_rect_picture_check);

  EXPECT_EQ(one_rect_picture->LayerRect(), one_rect_picture_check->LayerRect());
  EXPECT_EQ(0, memcmp(one_rect_buffer, one_rect_buffer_check, 4 * 100 * 100));

  // Two rect picture.
  content_layer_client.add_draw_rect(gfx::Rect(25, 25, 50, 50), green_paint);

  scoped_refptr<Picture> two_rect_picture =
      Picture::Create(layer_rect, &content_layer_client, tile_grid_size, false,
                      RecordingSource::RECORD_NORMALLY);

  scoped_ptr<base::Value> serialized_two_rect(two_rect_picture->AsValue());

  // Reconstruct the picture.
  scoped_refptr<Picture> two_rect_picture_check =
      Picture::CreateFromValue(serialized_two_rect.get());
  EXPECT_TRUE(two_rect_picture_check);

  // Check for equivalence.
  unsigned char two_rect_buffer[4 * 100 * 100] = {0};
  DrawPicture(two_rect_buffer, layer_rect, two_rect_picture);
  unsigned char two_rect_buffer_check[4 * 100 * 100] = {0};
  DrawPicture(two_rect_buffer_check, layer_rect, two_rect_picture_check);

  EXPECT_EQ(two_rect_picture->LayerRect(), two_rect_picture_check->LayerRect());
  EXPECT_EQ(0, memcmp(two_rect_buffer, two_rect_buffer_check, 4 * 100 * 100));
}

TEST(PictureTest, CreateFromSkpValue) {
  SkGraphics::Init();

  gfx::Rect layer_rect(100, 200);

  gfx::Size tile_grid_size(100, 200);

  FakeContentLayerClient content_layer_client;

  scoped_ptr<base::Value> tmp;

  SkPaint red_paint;
  red_paint.setColor(SkColorSetARGB(255, 255, 0, 0));
  SkPaint green_paint;
  green_paint.setColor(SkColorSetARGB(255, 0, 255, 0));

  // Invalid picture (not a dict).
  tmp.reset(new base::StringValue("abc!@#$%"));
  scoped_refptr<Picture> invalid_picture =
      Picture::CreateFromSkpValue(tmp.get());
  EXPECT_TRUE(!invalid_picture.get());

  // Single full-size rect picture.
  content_layer_client.add_draw_rect(layer_rect, red_paint);
  scoped_refptr<Picture> one_rect_picture =
      Picture::Create(layer_rect, &content_layer_client, tile_grid_size, false,
                      RecordingSource::RECORD_NORMALLY);
  scoped_ptr<base::Value> serialized_one_rect(
      one_rect_picture->AsValue());

  const base::DictionaryValue* value = NULL;
  EXPECT_TRUE(serialized_one_rect->GetAsDictionary(&value));

  // Decode the picture from base64.
  const base::Value* skp_value;
  EXPECT_TRUE(value->Get("skp64", &skp_value));

  // Reconstruct the picture.
  scoped_refptr<Picture> one_rect_picture_check =
      Picture::CreateFromSkpValue(skp_value);
  EXPECT_TRUE(one_rect_picture_check);

  EXPECT_EQ(100, one_rect_picture_check->LayerRect().width());
  EXPECT_EQ(200, one_rect_picture_check->LayerRect().height());
}

TEST(PictureTest, RecordingModes) {
  SkGraphics::Init();

  gfx::Rect layer_rect(100, 200);

  gfx::Size tile_grid_size(100, 200);

  FakeContentLayerClient content_layer_client;
  EXPECT_EQ(NULL, content_layer_client.last_canvas());

  scoped_refptr<Picture> picture =
      Picture::Create(layer_rect, &content_layer_client, tile_grid_size, false,
                      RecordingSource::RECORD_NORMALLY);
  EXPECT_TRUE(content_layer_client.last_canvas() != NULL);
  EXPECT_EQ(ContentLayerClient::PAINTING_BEHAVIOR_NORMAL,
            content_layer_client.last_painting_control());
  EXPECT_TRUE(picture.get());

  picture = Picture::Create(layer_rect, &content_layer_client, tile_grid_size,
                            false, RecordingSource::RECORD_WITH_SK_NULL_CANVAS);
  EXPECT_TRUE(content_layer_client.last_canvas() != NULL);
  EXPECT_EQ(ContentLayerClient::PAINTING_BEHAVIOR_NORMAL,
            content_layer_client.last_painting_control());
  EXPECT_TRUE(picture.get());

  picture =
      Picture::Create(layer_rect, &content_layer_client, tile_grid_size, false,
                      RecordingSource::RECORD_WITH_PAINTING_DISABLED);
  EXPECT_TRUE(content_layer_client.last_canvas() != NULL);
  EXPECT_EQ(ContentLayerClient::DISPLAY_LIST_PAINTING_DISABLED,
            content_layer_client.last_painting_control());
  EXPECT_TRUE(picture.get());

  picture =
      Picture::Create(layer_rect, &content_layer_client, tile_grid_size, false,
                      RecordingSource::RECORD_WITH_CACHING_DISABLED);
  EXPECT_TRUE(content_layer_client.last_canvas() != NULL);
  EXPECT_EQ(ContentLayerClient::DISPLAY_LIST_CACHING_DISABLED,
            content_layer_client.last_painting_control());
  EXPECT_TRUE(picture.get());

  // RECORD_WITH_CONSTRUCTION_DISABLED is not supported for Picture.

  EXPECT_EQ(5, RecordingSource::RECORDING_MODE_COUNT);
}

}  // namespace
}  // namespace cc