1 // Copyright 2012 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/picture_layer_tiling.h"
7 #include "cc/test/fake_picture_layer_tiling_client.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "ui/gfx/size_conversions.h"
13 class PictureLayerTilingIteratorTest
: public testing::Test
{
15 PictureLayerTilingIteratorTest() {}
16 virtual ~PictureLayerTilingIteratorTest() {}
18 void Initialize(gfx::Size tile_size
,
20 gfx::Size layer_bounds
) {
21 client_
.SetTileSize(tile_size
);
22 tiling_
= PictureLayerTiling::Create(contents_scale
, tile_size
);
23 tiling_
->SetClient(&client_
);
24 tiling_
->SetLayerBounds(layer_bounds
);
27 void VerifyTilesExactlyCoverRect(float rect_scale
, gfx::Rect rect
) {
28 // Iterators are not valid if this ratio is too large (i.e. the
29 // tiling is too high-res for a low-res destination rect.) This is an
30 // artifact of snapping geometry to integer coordinates and then mapping
31 // back to floating point texture coordinates.
32 float dest_to_contents_scale
= tiling_
->contents_scale() / rect_scale
;
33 ASSERT_LE(dest_to_contents_scale
, 2.0);
35 Region remaining
= rect
;
36 for (PictureLayerTiling::Iterator
iter(tiling_
.get(), rect_scale
, rect
);
40 // Geometry cannot overlap previous geometry at all
41 gfx::Rect geometry
= iter
.geometry_rect();
42 EXPECT_TRUE(rect
.Contains(geometry
));
43 EXPECT_TRUE(remaining
.Contains(geometry
));
44 remaining
.Subtract(geometry
);
46 // Sanity check that texture coords are within the texture rect.
47 gfx::RectF texture_rect
= iter
.texture_rect();
48 EXPECT_GE(texture_rect
.x(), 0);
49 EXPECT_GE(texture_rect
.y(), 0);
50 EXPECT_LE(texture_rect
.right(), client_
.TileSize().width());
51 EXPECT_LE(texture_rect
.bottom(), client_
.TileSize().height());
53 EXPECT_EQ(iter
.texture_size(), client_
.TileSize());
56 // The entire rect must be filled by geometry from the tiling.
57 EXPECT_TRUE(remaining
.IsEmpty());
61 FakePictureLayerTilingClient client_
;
62 scoped_ptr
<PictureLayerTiling
> tiling_
;
64 DISALLOW_COPY_AND_ASSIGN(PictureLayerTilingIteratorTest
);
67 TEST_F(PictureLayerTilingIteratorTest
, IteratorCoversLayerBoundsNoScale
) {
68 Initialize(gfx::Size(100, 100), 1, gfx::Size(1099, 801));
69 VerifyTilesExactlyCoverRect(1, gfx::Rect());
70 VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1099, 801));
71 VerifyTilesExactlyCoverRect(1, gfx::Rect(52, 83, 789, 412));
73 // With borders, a size of 3x3 = 1 pixel of content.
74 Initialize(gfx::Size(3, 3), 1, gfx::Size(10, 10));
75 VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1, 1));
76 VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 2, 2));
77 VerifyTilesExactlyCoverRect(1, gfx::Rect(1, 1, 2, 2));
78 VerifyTilesExactlyCoverRect(1, gfx::Rect(3, 2, 5, 2));
81 TEST_F(PictureLayerTilingIteratorTest
, IteratorCoversLayerBoundsTilingScale
) {
82 Initialize(gfx::Size(200, 100), 2.0f
, gfx::Size(1005, 2010));
83 VerifyTilesExactlyCoverRect(1, gfx::Rect());
84 VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1005, 2010));
85 VerifyTilesExactlyCoverRect(1, gfx::Rect(50, 112, 512, 381));
87 Initialize(gfx::Size(3, 3), 2.0f
, gfx::Size(10, 10));
88 VerifyTilesExactlyCoverRect(1, gfx::Rect());
89 VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1, 1));
90 VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 2, 2));
91 VerifyTilesExactlyCoverRect(1, gfx::Rect(1, 1, 2, 2));
92 VerifyTilesExactlyCoverRect(1, gfx::Rect(3, 2, 5, 2));
94 Initialize(gfx::Size(100, 200), 0.5f
, gfx::Size(1005, 2010));
95 VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1005, 2010));
96 VerifyTilesExactlyCoverRect(1, gfx::Rect(50, 112, 512, 381));
98 Initialize(gfx::Size(150, 250), 0.37f
, gfx::Size(1005, 2010));
99 VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1005, 2010));
100 VerifyTilesExactlyCoverRect(1, gfx::Rect(50, 112, 512, 381));
102 Initialize(gfx::Size(312, 123), 0.01f
, gfx::Size(1005, 2010));
103 VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1005, 2010));
104 VerifyTilesExactlyCoverRect(1, gfx::Rect(50, 112, 512, 381));
107 TEST_F(PictureLayerTilingIteratorTest
, IteratorCoversLayerBoundsBothScale
) {
108 Initialize(gfx::Size(50, 50), 4.0f
, gfx::Size(800, 600));
109 VerifyTilesExactlyCoverRect(2.0f
, gfx::Rect());
110 VerifyTilesExactlyCoverRect(2.0f
, gfx::Rect(0, 0, 1600, 1200));
111 VerifyTilesExactlyCoverRect(2.0f
, gfx::Rect(512, 365, 253, 182));
114 gfx::Size
bounds(800, 600);
115 gfx::Rect
full_rect(gfx::ToCeiledSize(gfx::ScaleSize(bounds
, scale
)));
116 Initialize(gfx::Size(256, 512), 5.2f
, bounds
);
117 VerifyTilesExactlyCoverRect(scale
, full_rect
);
118 VerifyTilesExactlyCoverRect(scale
, gfx::Rect(2014, 1579, 867, 1033));