- add sources.
[platform/framework/web/crosswalk.git] / src / cc / resources / picture_pile_unittest.cc
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_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"
11
12 namespace cc {
13 namespace {
14
15 class TestPicturePile : public PicturePile {
16  public:
17   using PicturePile::buffer_pixels;
18
19   PictureMap& picture_map() { return picture_map_; }
20
21   typedef PicturePile::PictureInfo PictureInfo;
22   typedef PicturePile::PictureMapKey PictureMapKey;
23   typedef PicturePile::PictureMap PictureMap;
24
25  protected:
26     virtual ~TestPicturePile() {}
27 };
28
29 TEST(PicturePileTest, SmallInvalidateInflated) {
30   FakeContentLayerClient client;
31   FakeRenderingStatsInstrumentation stats_instrumentation;
32   scoped_refptr<TestPicturePile> pile = new TestPicturePile;
33   SkColor background_color = SK_ColorBLUE;
34
35   float min_scale = 0.125;
36   gfx::Size base_picture_size = pile->tiling().max_texture_size();
37
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);
42
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);
50
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);
59
60   EXPECT_EQ(1, pile->tiling().num_tiles_x());
61   EXPECT_EQ(1, pile->tiling().num_tiles_y());
62
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);
69
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();
74 }
75
76 TEST(PicturePileTest, LargeInvalidateInflated) {
77   FakeContentLayerClient client;
78   FakeRenderingStatsInstrumentation stats_instrumentation;
79   scoped_refptr<TestPicturePile> pile = new TestPicturePile;
80   SkColor background_color = SK_ColorBLUE;
81
82   float min_scale = 0.125;
83   gfx::Size base_picture_size = pile->tiling().max_texture_size();
84
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);
89
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);
97
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);
106
107   EXPECT_EQ(1, pile->tiling().num_tiles_x());
108   EXPECT_EQ(1, pile->tiling().num_tiles_y());
109
110   TestPicturePile::PictureInfo& picture_info =
111       pile->picture_map().find(TestPicturePile::PictureMapKey(0, 0))->second;
112   EXPECT_TRUE(!!picture_info.picture.get());
113
114   int expected_inflation = pile->buffer_pixels();
115
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());
121 }
122
123 TEST(PicturePileTest, InvalidateOnTileBoundaryInflated) {
124   FakeContentLayerClient client;
125   FakeRenderingStatsInstrumentation stats_instrumentation;
126   scoped_refptr<TestPicturePile> pile = new TestPicturePile;
127   SkColor background_color = SK_ColorBLUE;
128
129   float min_scale = 0.125;
130   gfx::Size base_picture_size = pile->tiling().max_texture_size();
131
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);
137
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());
141
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());
145
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);
153
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,
159       50,
160       50);
161   pile->Update(&client,
162                background_color,
163                false,
164                invalidate_rect,
165                gfx::Rect(layer_size),
166                &stats_instrumentation);
167
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;
173
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());
177     }
178   }
179 }
180
181 }  // namespace
182 }  // namespace cc