Use multiline attribute to check for IA2_STATE_MULTILINE.
[chromium-blink-merge.git] / cc / layers / nine_patch_layer_impl_unittest.cc
bloba71f92ef0dd3cb8c17407b761cb083c9a6be6706
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 "base/containers/hash_tables.h"
6 #include "cc/layers/append_quads_data.h"
7 #include "cc/layers/nine_patch_layer_impl.h"
8 #include "cc/quads/texture_draw_quad.h"
9 #include "cc/resources/ui_resource_bitmap.h"
10 #include "cc/resources/ui_resource_client.h"
11 #include "cc/test/fake_impl_proxy.h"
12 #include "cc/test/fake_output_surface.h"
13 #include "cc/test/fake_ui_resource_layer_tree_host_impl.h"
14 #include "cc/test/geometry_test_utils.h"
15 #include "cc/test/layer_test_common.h"
16 #include "cc/trees/single_thread_proxy.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "ui/gfx/geometry/rect_conversions.h"
20 #include "ui/gfx/geometry/safe_integer_conversions.h"
21 #include "ui/gfx/transform.h"
23 namespace cc {
24 namespace {
26 gfx::Rect ToRoundedIntRect(const gfx::RectF& rect_f) {
27 return gfx::Rect(gfx::ToRoundedInt(rect_f.x()),
28 gfx::ToRoundedInt(rect_f.y()),
29 gfx::ToRoundedInt(rect_f.width()),
30 gfx::ToRoundedInt(rect_f.height()));
33 void NinePatchLayerLayoutTest(const gfx::Size& bitmap_size,
34 const gfx::Rect& aperture_rect,
35 const gfx::Size& layer_size,
36 const gfx::Rect& border,
37 bool fill_center,
38 size_t expected_quad_size) {
39 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
40 gfx::Rect visible_content_rect(layer_size);
41 gfx::Rect expected_remaining(border.x(),
42 border.y(),
43 layer_size.width() - border.width(),
44 layer_size.height() - border.height());
46 FakeImplProxy proxy;
47 TestSharedBitmapManager shared_bitmap_manager;
48 FakeUIResourceLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager);
49 host_impl.InitializeRenderer(FakeOutputSurface::Create3d());
51 scoped_ptr<NinePatchLayerImpl> layer =
52 NinePatchLayerImpl::Create(host_impl.active_tree(), 1);
53 layer->draw_properties().visible_content_rect = visible_content_rect;
54 layer->SetBounds(layer_size);
55 layer->SetContentBounds(layer_size);
56 layer->SetHasRenderSurface(true);
57 layer->draw_properties().render_target = layer.get();
59 UIResourceId uid = 1;
60 bool is_opaque = false;
61 UIResourceBitmap bitmap(bitmap_size, is_opaque);
63 host_impl.CreateUIResource(uid, bitmap);
64 layer->SetUIResourceId(uid);
65 layer->SetImageBounds(bitmap_size);
66 layer->SetLayout(aperture_rect, border, fill_center);
67 AppendQuadsData data;
68 layer->AppendQuads(render_pass.get(), &data);
70 // Verify quad rects
71 const QuadList& quads = render_pass->quad_list;
72 EXPECT_EQ(expected_quad_size, quads.size());
74 Region remaining(visible_content_rect);
75 for (auto iter = quads.cbegin(); iter != quads.cend(); ++iter) {
76 gfx::Rect quad_rect = iter->rect;
78 EXPECT_TRUE(visible_content_rect.Contains(quad_rect)) << iter.index();
79 EXPECT_TRUE(remaining.Contains(quad_rect)) << iter.index();
80 remaining.Subtract(Region(quad_rect));
83 // Check if the left-over quad is the same size as the mapped aperture quad in
84 // layer space.
85 if (!fill_center) {
86 EXPECT_EQ(expected_remaining, gfx::ToEnclosedRect(remaining.bounds()));
87 } else {
88 EXPECT_TRUE(remaining.bounds().IsEmpty());
91 // Verify UV rects
92 gfx::Rect bitmap_rect(bitmap_size);
93 Region tex_remaining(bitmap_rect);
94 for (const auto& quad : quads) {
95 const TextureDrawQuad* tex_quad = TextureDrawQuad::MaterialCast(quad);
96 gfx::RectF tex_rect =
97 gfx::BoundingRect(tex_quad->uv_top_left, tex_quad->uv_bottom_right);
98 tex_rect.Scale(bitmap_size.width(), bitmap_size.height());
99 tex_remaining.Subtract(Region(ToRoundedIntRect(tex_rect)));
102 if (!fill_center) {
103 EXPECT_EQ(aperture_rect, tex_remaining.bounds());
104 Region aperture_region(aperture_rect);
105 EXPECT_EQ(aperture_region, tex_remaining);
106 } else {
107 EXPECT_TRUE(remaining.bounds().IsEmpty());
111 TEST(NinePatchLayerImplTest, VerifyDrawQuads) {
112 // Input is a 100x100 bitmap with a 40x50 aperture at x=20, y=30.
113 // The bounds of the layer are set to 400x400.
114 gfx::Size bitmap_size(100, 100);
115 gfx::Size layer_size(400, 500);
116 gfx::Rect aperture_rect(20, 30, 40, 50);
117 gfx::Rect border(40, 40, 80, 80);
118 bool fill_center = false;
119 size_t expected_quad_size = 8;
120 NinePatchLayerLayoutTest(bitmap_size,
121 aperture_rect,
122 layer_size,
123 border,
124 fill_center,
125 expected_quad_size);
127 // The bounds of the layer are set to less than the bitmap size.
128 bitmap_size = gfx::Size(100, 100);
129 layer_size = gfx::Size(40, 50);
130 aperture_rect = gfx::Rect(20, 30, 40, 50);
131 border = gfx::Rect(10, 10, 25, 15);
132 fill_center = true;
133 expected_quad_size = 9;
134 NinePatchLayerLayoutTest(bitmap_size,
135 aperture_rect,
136 layer_size,
137 border,
138 fill_center,
139 expected_quad_size);
141 // Layer and image sizes are equal.
142 bitmap_size = gfx::Size(100, 100);
143 layer_size = gfx::Size(100, 100);
144 aperture_rect = gfx::Rect(20, 30, 40, 50);
145 border = gfx::Rect(20, 30, 40, 50);
146 fill_center = true;
147 expected_quad_size = 9;
148 NinePatchLayerLayoutTest(bitmap_size,
149 aperture_rect,
150 layer_size,
151 border,
152 fill_center,
153 expected_quad_size);
156 TEST(NinePatchLayerImplTest, VerifyDrawQuadsWithEmptyPatches) {
157 // The top component of the 9-patch is empty, so there should be no quads for
158 // the top three components.
159 gfx::Size bitmap_size(100, 100);
160 gfx::Size layer_size(100, 100);
161 gfx::Rect aperture_rect(10, 0, 80, 90);
162 gfx::Rect border(10, 0, 20, 10);
163 bool fill_center = false;
164 size_t expected_quad_size = 5;
165 NinePatchLayerLayoutTest(bitmap_size,
166 aperture_rect,
167 layer_size,
168 border,
169 fill_center,
170 expected_quad_size);
172 // The top and left components of the 9-patch are empty, so there should be no
173 // quads for the left and top components.
174 bitmap_size = gfx::Size(100, 100);
175 layer_size = gfx::Size(100, 100);
176 aperture_rect = gfx::Rect(0, 0, 90, 90);
177 border = gfx::Rect(0, 0, 10, 10);
178 fill_center = false;
179 expected_quad_size = 3;
180 NinePatchLayerLayoutTest(bitmap_size,
181 aperture_rect,
182 layer_size,
183 border,
184 fill_center,
185 expected_quad_size);
187 // The aperture is the size of the bitmap and the center doesn't draw.
188 bitmap_size = gfx::Size(100, 100);
189 layer_size = gfx::Size(100, 100);
190 aperture_rect = gfx::Rect(0, 0, 100, 100);
191 border = gfx::Rect(0, 0, 0, 0);
192 fill_center = false;
193 expected_quad_size = 0;
194 NinePatchLayerLayoutTest(bitmap_size,
195 aperture_rect,
196 layer_size,
197 border,
198 fill_center,
199 expected_quad_size);
201 // The aperture is the size of the bitmap and the center does draw.
202 bitmap_size = gfx::Size(100, 100);
203 layer_size = gfx::Size(100, 100);
204 aperture_rect = gfx::Rect(0, 0, 100, 100);
205 border = gfx::Rect(0, 0, 0, 0);
206 fill_center = true;
207 expected_quad_size = 1;
208 NinePatchLayerLayoutTest(bitmap_size,
209 aperture_rect,
210 layer_size,
211 border,
212 fill_center,
213 expected_quad_size);
216 TEST(NinePatchLayerImplTest, Occlusion) {
217 gfx::Size layer_size(1000, 1000);
218 gfx::Size viewport_size(1000, 1000);
220 LayerTestCommon::LayerImplTest impl;
222 SkBitmap sk_bitmap;
223 sk_bitmap.allocN32Pixels(10, 10);
224 sk_bitmap.setImmutable();
225 UIResourceId uid = 5;
226 UIResourceBitmap bitmap(sk_bitmap);
227 impl.host_impl()->CreateUIResource(uid, bitmap);
229 NinePatchLayerImpl* nine_patch_layer_impl =
230 impl.AddChildToRoot<NinePatchLayerImpl>();
231 nine_patch_layer_impl->SetBounds(layer_size);
232 nine_patch_layer_impl->SetContentBounds(layer_size);
233 nine_patch_layer_impl->SetDrawsContent(true);
234 nine_patch_layer_impl->SetUIResourceId(uid);
235 nine_patch_layer_impl->SetImageBounds(gfx::Size(10, 10));
237 gfx::Rect aperture = gfx::Rect(3, 3, 4, 4);
238 gfx::Rect border = gfx::Rect(300, 300, 400, 400);
239 nine_patch_layer_impl->SetLayout(aperture, border, true);
241 impl.CalcDrawProps(viewport_size);
244 SCOPED_TRACE("No occlusion");
245 gfx::Rect occluded;
246 impl.AppendQuadsWithOcclusion(nine_patch_layer_impl, occluded);
248 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(),
249 gfx::Rect(layer_size));
250 EXPECT_EQ(9u, impl.quad_list().size());
254 SCOPED_TRACE("Full occlusion");
255 gfx::Rect occluded(nine_patch_layer_impl->visible_content_rect());
256 impl.AppendQuadsWithOcclusion(nine_patch_layer_impl, occluded);
258 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(), gfx::Rect());
259 EXPECT_EQ(impl.quad_list().size(), 0u);
263 SCOPED_TRACE("Partial occlusion");
264 gfx::Rect occluded(0, 0, 500, 1000);
265 impl.AppendQuadsWithOcclusion(nine_patch_layer_impl, occluded);
267 size_t partially_occluded_count = 0;
268 LayerTestCommon::VerifyQuadsAreOccluded(
269 impl.quad_list(), occluded, &partially_occluded_count);
270 // The layer outputs nine quads, three of which are partially occluded, and
271 // three fully occluded.
272 EXPECT_EQ(6u, impl.quad_list().size());
273 EXPECT_EQ(3u, partially_occluded_count);
277 TEST(NinePatchLayerImplTest, OpaqueRect) {
278 gfx::Size layer_size(1000, 1000);
279 gfx::Size viewport_size(1000, 1000);
281 LayerTestCommon::LayerImplTest impl;
283 SkBitmap sk_bitmap_opaque;
284 sk_bitmap_opaque.allocN32Pixels(10, 10);
285 sk_bitmap_opaque.setImmutable();
286 sk_bitmap_opaque.setAlphaType(kOpaque_SkAlphaType);
288 UIResourceId uid_opaque = 6;
289 UIResourceBitmap bitmap_opaque(sk_bitmap_opaque);
290 impl.host_impl()->CreateUIResource(uid_opaque, bitmap_opaque);
292 SkBitmap sk_bitmap_alpha;
293 sk_bitmap_alpha.allocN32Pixels(10, 10);
294 sk_bitmap_alpha.setImmutable();
295 sk_bitmap_alpha.setAlphaType(kUnpremul_SkAlphaType);
297 UIResourceId uid_alpha = 7;
298 UIResourceBitmap bitmap_alpha(sk_bitmap_alpha);
300 impl.host_impl()->CreateUIResource(uid_alpha, bitmap_alpha);
302 NinePatchLayerImpl *nine_patch_layer_impl =
303 impl.AddChildToRoot<NinePatchLayerImpl>();
304 nine_patch_layer_impl->SetBounds(layer_size);
305 nine_patch_layer_impl->SetContentBounds(layer_size);
306 nine_patch_layer_impl->SetDrawsContent(true);
308 impl.CalcDrawProps(viewport_size);
311 SCOPED_TRACE("Use opaque image");
313 nine_patch_layer_impl->SetUIResourceId(uid_opaque);
314 nine_patch_layer_impl->SetImageBounds(gfx::Size(10, 10));
316 gfx::Rect aperture = gfx::Rect(3, 3, 4, 4);
317 gfx::Rect border = gfx::Rect(300, 300, 400, 400);
318 nine_patch_layer_impl->SetLayout(aperture, border, true);
320 impl.AppendQuadsWithOcclusion(nine_patch_layer_impl, gfx::Rect());
322 const QuadList &quad_list = impl.quad_list();
323 for (QuadList::ConstBackToFrontIterator it = quad_list.BackToFrontBegin();
324 it != quad_list.BackToFrontEnd(); ++it)
325 EXPECT_FALSE(it->ShouldDrawWithBlending());
329 SCOPED_TRACE("Use tranparent image");
331 nine_patch_layer_impl->SetUIResourceId(uid_alpha);
333 impl.AppendQuadsWithOcclusion(nine_patch_layer_impl, gfx::Rect());
335 const QuadList &quad_list = impl.quad_list();
336 for (QuadList::ConstBackToFrontIterator it = quad_list.BackToFrontBegin();
337 it != quad_list.BackToFrontEnd(); ++it)
338 EXPECT_TRUE(it->ShouldDrawWithBlending());
342 } // namespace
343 } // namespace cc