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.
7 #include "cc/nine_patch_layer_impl.h"
9 #include "cc/append_quads_data.h"
10 #include "cc/single_thread_proxy.h"
11 #include "cc/test/geometry_test_utils.h"
12 #include "cc/test/layer_test_common.h"
13 #include "cc/test/mock_quad_culler.h"
14 #include "cc/texture_draw_quad.h"
15 #include "ui/gfx/rect_conversions.h"
16 #include "ui/gfx/safe_integer_conversions.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include <public/WebTransformationMatrix.h>
25 gfx::Rect
ToRoundedIntRect(gfx::RectF rect_f
) {
26 return gfx::Rect(gfx::ToRoundedInt(rect_f
.x()), gfx::ToRoundedInt(rect_f
.y()), gfx::ToRoundedInt(rect_f
.width()), gfx::ToRoundedInt(rect_f
.height()));
29 TEST(NinePatchLayerImplTest
, verifyDrawQuads
)
31 // Input is a 100x100 bitmap with a 40x50 aperture at x=20, y=30.
32 // The bounds of the layer are set to 400x400, so the draw quads
33 // generated should leave the border width (40) intact.
34 MockQuadCuller quadCuller
;
35 gfx::Size
bitmapSize(100, 100);
36 gfx::Size
layerSize(400, 400);
37 gfx::Rect
visibleContentRect(gfx::Point(), layerSize
);
38 gfx::Rect
apertureRect(20, 30, 40, 50);
39 gfx::Rect
scaledApertureNonUniform(20, 30, 340, 350);
41 scoped_ptr
<NinePatchLayerImpl
> layer
= NinePatchLayerImpl::create(1);
42 layer
->setVisibleContentRect(visibleContentRect
);
43 layer
->setBounds(layerSize
);
44 layer
->setContentBounds(layerSize
);
45 layer
->createRenderSurface();
46 layer
->setRenderTarget(layer
.get());
47 layer
->setLayout(bitmapSize
, apertureRect
);
48 layer
->setResourceId(1);
50 // This scale should not affect the generated quad geometry, but only
51 // the shared draw transform.
52 WebKit::WebTransformationMatrix transform
;
54 layer
->setDrawTransform(transform
);
57 layer
->appendQuads(quadCuller
, data
);
60 const QuadList
& quads
= quadCuller
.quadList();
61 EXPECT_EQ(quads
.size(), 8);
62 Region
remaining(visibleContentRect
);
63 for (size_t i
= 0; i
< quads
.size(); ++i
) {
64 DrawQuad
* quad
= quads
[i
];
65 gfx::Rect quadRect
= quad
->quadRect();
67 EXPECT_TRUE(visibleContentRect
.Contains(quadRect
)) << i
;
68 EXPECT_TRUE(remaining
.Contains(quadRect
)) << i
;
69 EXPECT_EQ(quad
->sharedQuadState()->quadTransform
, transform
) << i
;
70 remaining
.Subtract(Region(quadRect
));
72 EXPECT_RECT_EQ(remaining
.bounds(), scaledApertureNonUniform
);
73 Region
scaledApertureRegion(scaledApertureNonUniform
);
74 EXPECT_EQ(remaining
, scaledApertureRegion
);
77 gfx::Rect
bitmapRect(gfx::Point(), bitmapSize
);
78 Region
texRemaining(bitmapRect
);
79 for (size_t i
= 0; i
< quads
.size(); ++i
) {
80 DrawQuad
* quad
= quads
[i
];
81 ASSERT_EQ(quad
->material(), DrawQuad::TextureContent
);
82 TextureDrawQuad
* texQuad
= static_cast<TextureDrawQuad
*>(quad
);
83 gfx::RectF texRect
= texQuad
->uvRect();
84 texRect
.Scale(bitmapSize
.width(), bitmapSize
.height());
85 texRemaining
.Subtract(Region(ToRoundedIntRect(texRect
)));
87 EXPECT_RECT_EQ(texRemaining
.bounds(), apertureRect
);
88 Region
apertureRegion(apertureRect
);
89 EXPECT_EQ(texRemaining
, apertureRegion
);