ash: Add missing include to test from r236852.
[chromium-blink-merge.git] / cc / resources / picture_pile_unittest.cc
blob0c9b2e1194b30064cfcf93d946450715dcabd2ff
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_pile.h"
6 #include "cc/test/fake_content_layer_client.h"
7 #include "cc/test/fake_rendering_stats_instrumentation.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "ui/gfx/rect_conversions.h"
10 #include "ui/gfx/size_conversions.h"
12 namespace cc {
13 namespace {
15 class TestPicturePile : public PicturePile {
16 public:
17 using PicturePile::buffer_pixels;
19 PictureMap& picture_map() { return picture_map_; }
21 typedef PicturePile::PictureInfo PictureInfo;
22 typedef PicturePile::PictureMapKey PictureMapKey;
23 typedef PicturePile::PictureMap PictureMap;
25 protected:
26 virtual ~TestPicturePile() {}
29 TEST(PicturePileTest, SmallInvalidateInflated) {
30 FakeContentLayerClient client;
31 FakeRenderingStatsInstrumentation stats_instrumentation;
32 scoped_refptr<TestPicturePile> pile = new TestPicturePile;
33 SkColor background_color = SK_ColorBLUE;
35 float min_scale = 0.125;
36 gfx::Size base_picture_size = pile->tiling().max_texture_size();
38 gfx::Size layer_size = base_picture_size;
39 pile->Resize(layer_size);
40 pile->SetTileGridSize(gfx::Size(1000, 1000));
41 pile->SetMinContentsScale(min_scale);
43 // Update the whole layer.
44 pile->Update(&client,
45 background_color,
46 false,
47 gfx::Rect(layer_size),
48 gfx::Rect(layer_size),
49 &stats_instrumentation);
51 // Invalidate something inside a tile.
52 gfx::Rect invalidate_rect(50, 50, 1, 1);
53 pile->Update(&client,
54 background_color,
55 false,
56 invalidate_rect,
57 gfx::Rect(layer_size),
58 &stats_instrumentation);
60 EXPECT_EQ(1, pile->tiling().num_tiles_x());
61 EXPECT_EQ(1, pile->tiling().num_tiles_y());
63 TestPicturePile::PictureInfo& picture_info =
64 pile->picture_map().find(TestPicturePile::PictureMapKey(0, 0))->second;
65 // We should have a picture.
66 EXPECT_TRUE(!!picture_info.picture.get());
67 gfx::Rect picture_rect =
68 gfx::ScaleToEnclosedRect(picture_info.picture->LayerRect(), min_scale);
70 // The the picture should be large enough that scaling it never makes a rect
71 // smaller than 1 px wide or tall.
72 EXPECT_FALSE(picture_rect.IsEmpty()) << "Picture rect " <<
73 picture_rect.ToString();
76 TEST(PicturePileTest, LargeInvalidateInflated) {
77 FakeContentLayerClient client;
78 FakeRenderingStatsInstrumentation stats_instrumentation;
79 scoped_refptr<TestPicturePile> pile = new TestPicturePile;
80 SkColor background_color = SK_ColorBLUE;
82 float min_scale = 0.125;
83 gfx::Size base_picture_size = pile->tiling().max_texture_size();
85 gfx::Size layer_size = base_picture_size;
86 pile->Resize(layer_size);
87 pile->SetTileGridSize(gfx::Size(1000, 1000));
88 pile->SetMinContentsScale(min_scale);
90 // Update the whole layer.
91 pile->Update(&client,
92 background_color,
93 false,
94 gfx::Rect(layer_size),
95 gfx::Rect(layer_size),
96 &stats_instrumentation);
98 // Invalidate something inside a tile.
99 gfx::Rect invalidate_rect(50, 50, 100, 100);
100 pile->Update(&client,
101 background_color,
102 false,
103 invalidate_rect,
104 gfx::Rect(layer_size),
105 &stats_instrumentation);
107 EXPECT_EQ(1, pile->tiling().num_tiles_x());
108 EXPECT_EQ(1, pile->tiling().num_tiles_y());
110 TestPicturePile::PictureInfo& picture_info =
111 pile->picture_map().find(TestPicturePile::PictureMapKey(0, 0))->second;
112 EXPECT_TRUE(!!picture_info.picture.get());
114 int expected_inflation = pile->buffer_pixels();
116 scoped_refptr<Picture> base_picture = picture_info.picture;
117 gfx::Rect base_picture_rect(layer_size);
118 base_picture_rect.Inset(-expected_inflation, -expected_inflation);
119 EXPECT_EQ(base_picture_rect.ToString(),
120 base_picture->LayerRect().ToString());
123 TEST(PicturePileTest, InvalidateOnTileBoundaryInflated) {
124 FakeContentLayerClient client;
125 FakeRenderingStatsInstrumentation stats_instrumentation;
126 scoped_refptr<TestPicturePile> pile = new TestPicturePile;
127 SkColor background_color = SK_ColorBLUE;
129 float min_scale = 0.125;
130 gfx::Size base_picture_size = pile->tiling().max_texture_size();
132 gfx::Size layer_size =
133 gfx::ToFlooredSize(gfx::ScaleSize(base_picture_size, 2.f));
134 pile->Resize(layer_size);
135 pile->SetTileGridSize(gfx::Size(1000, 1000));
136 pile->SetMinContentsScale(min_scale);
138 // Due to border pixels, we should have 3 tiles.
139 EXPECT_EQ(3, pile->tiling().num_tiles_x());
140 EXPECT_EQ(3, pile->tiling().num_tiles_y());
142 // We should have 1/.125 - 1 = 7 border pixels.
143 EXPECT_EQ(7, pile->buffer_pixels());
144 EXPECT_EQ(7, pile->tiling().border_texels());
146 // Update the whole layer.
147 pile->Update(&client,
148 background_color,
149 false,
150 gfx::Rect(layer_size),
151 gfx::Rect(layer_size),
152 &stats_instrumentation);
154 // Invalidate something just over a tile boundary by a single pixel.
155 // This will invalidate the tile (1, 1), as well as 1 row of pixels in (1, 0).
156 gfx::Rect invalidate_rect(
157 pile->tiling().TileBoundsWithBorder(0, 0).right(),
158 pile->tiling().TileBoundsWithBorder(0, 0).bottom() - 1,
160 50);
161 pile->Update(&client,
162 background_color,
163 false,
164 invalidate_rect,
165 gfx::Rect(layer_size),
166 &stats_instrumentation);
168 for (int i = 0; i < pile->tiling().num_tiles_x(); ++i) {
169 for (int j = 0; j < pile->tiling().num_tiles_y(); ++j) {
170 TestPicturePile::PictureInfo& picture_info =
171 pile->picture_map().find(
172 TestPicturePile::PictureMapKey(i, j))->second;
174 // TODO(vmpstr): Fix this to check invalidation frequency instead
175 // of the picture, since we always have one picture per tile.
176 EXPECT_TRUE(!!picture_info.picture.get());
181 } // namespace
182 } // namespace cc