Move FocusController to ViewTreeHost.
[chromium-blink-merge.git] / cc / layers / nine_patch_layer_impl_unittest.cc
blob3535c9851068fb6d170d109393fa6f3f38229a0d
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_layer_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 TestTaskGraphRunner task_graph_runner;
49 FakeUIResourceLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager,
50 &task_graph_runner);
51 host_impl.InitializeRenderer(FakeOutputSurface::Create3d());
53 scoped_ptr<NinePatchLayerImpl> layer =
54 NinePatchLayerImpl::Create(host_impl.active_tree(), 1);
55 layer->draw_properties().visible_layer_rect = visible_layer_rect;
56 layer->SetBounds(layer_size);
57 layer->SetHasRenderSurface(true);
58 layer->draw_properties().render_target = layer.get();
60 UIResourceId uid = 1;
61 bool is_opaque = false;
62 UIResourceBitmap bitmap(bitmap_size, is_opaque);
64 host_impl.CreateUIResource(uid, bitmap);
65 layer->SetUIResourceId(uid);
66 layer->SetImageBounds(bitmap_size);
67 layer->SetLayout(aperture_rect, border, fill_center);
68 AppendQuadsData data;
69 layer->AppendQuads(render_pass.get(), &data);
71 // Verify quad rects
72 const QuadList& quads = render_pass->quad_list;
73 EXPECT_EQ(expected_quad_size, quads.size());
75 Region remaining(visible_layer_rect);
76 for (auto iter = quads.cbegin(); iter != quads.cend(); ++iter) {
77 gfx::Rect quad_rect = iter->rect;
79 EXPECT_TRUE(visible_layer_rect.Contains(quad_rect)) << iter.index();
80 EXPECT_TRUE(remaining.Contains(quad_rect)) << iter.index();
81 remaining.Subtract(Region(quad_rect));
84 // Check if the left-over quad is the same size as the mapped aperture quad in
85 // layer space.
86 if (!fill_center) {
87 EXPECT_EQ(expected_remaining, gfx::ToEnclosedRect(remaining.bounds()));
88 } else {
89 EXPECT_TRUE(remaining.bounds().IsEmpty());
92 // Verify UV rects
93 gfx::Rect bitmap_rect(bitmap_size);
94 Region tex_remaining(bitmap_rect);
95 for (const auto& quad : quads) {
96 const TextureDrawQuad* tex_quad = TextureDrawQuad::MaterialCast(quad);
97 gfx::RectF tex_rect =
98 gfx::BoundingRect(tex_quad->uv_top_left, tex_quad->uv_bottom_right);
99 tex_rect.Scale(bitmap_size.width(), bitmap_size.height());
100 tex_remaining.Subtract(Region(ToRoundedIntRect(tex_rect)));
103 if (!fill_center) {
104 EXPECT_EQ(aperture_rect, tex_remaining.bounds());
105 Region aperture_region(aperture_rect);
106 EXPECT_EQ(aperture_region, tex_remaining);
107 } else {
108 EXPECT_TRUE(remaining.bounds().IsEmpty());
112 TEST(NinePatchLayerImplTest, VerifyDrawQuads) {
113 // Input is a 100x100 bitmap with a 40x50 aperture at x=20, y=30.
114 // The bounds of the layer are set to 400x400.
115 gfx::Size bitmap_size(100, 100);
116 gfx::Size layer_size(400, 500);
117 gfx::Rect aperture_rect(20, 30, 40, 50);
118 gfx::Rect border(40, 40, 80, 80);
119 bool fill_center = false;
120 size_t expected_quad_size = 8;
121 NinePatchLayerLayoutTest(bitmap_size,
122 aperture_rect,
123 layer_size,
124 border,
125 fill_center,
126 expected_quad_size);
128 // The bounds of the layer are set to less than the bitmap size.
129 bitmap_size = gfx::Size(100, 100);
130 layer_size = gfx::Size(40, 50);
131 aperture_rect = gfx::Rect(20, 30, 40, 50);
132 border = gfx::Rect(10, 10, 25, 15);
133 fill_center = true;
134 expected_quad_size = 9;
135 NinePatchLayerLayoutTest(bitmap_size,
136 aperture_rect,
137 layer_size,
138 border,
139 fill_center,
140 expected_quad_size);
142 // Layer and image sizes are equal.
143 bitmap_size = gfx::Size(100, 100);
144 layer_size = gfx::Size(100, 100);
145 aperture_rect = gfx::Rect(20, 30, 40, 50);
146 border = gfx::Rect(20, 30, 40, 50);
147 fill_center = true;
148 expected_quad_size = 9;
149 NinePatchLayerLayoutTest(bitmap_size,
150 aperture_rect,
151 layer_size,
152 border,
153 fill_center,
154 expected_quad_size);
157 TEST(NinePatchLayerImplTest, VerifyDrawQuadsWithEmptyPatches) {
158 // The top component of the 9-patch is empty, so there should be no quads for
159 // the top three components.
160 gfx::Size bitmap_size(100, 100);
161 gfx::Size layer_size(100, 100);
162 gfx::Rect aperture_rect(10, 0, 80, 90);
163 gfx::Rect border(10, 0, 20, 10);
164 bool fill_center = false;
165 size_t expected_quad_size = 5;
166 NinePatchLayerLayoutTest(bitmap_size,
167 aperture_rect,
168 layer_size,
169 border,
170 fill_center,
171 expected_quad_size);
173 // The top and left components of the 9-patch are empty, so there should be no
174 // quads for the left and top components.
175 bitmap_size = gfx::Size(100, 100);
176 layer_size = gfx::Size(100, 100);
177 aperture_rect = gfx::Rect(0, 0, 90, 90);
178 border = gfx::Rect(0, 0, 10, 10);
179 fill_center = false;
180 expected_quad_size = 3;
181 NinePatchLayerLayoutTest(bitmap_size,
182 aperture_rect,
183 layer_size,
184 border,
185 fill_center,
186 expected_quad_size);
188 // The aperture is the size of the bitmap and the center doesn't draw.
189 bitmap_size = gfx::Size(100, 100);
190 layer_size = gfx::Size(100, 100);
191 aperture_rect = gfx::Rect(0, 0, 100, 100);
192 border = gfx::Rect(0, 0, 0, 0);
193 fill_center = false;
194 expected_quad_size = 0;
195 NinePatchLayerLayoutTest(bitmap_size,
196 aperture_rect,
197 layer_size,
198 border,
199 fill_center,
200 expected_quad_size);
202 // The aperture is the size of the bitmap and the center does draw.
203 bitmap_size = gfx::Size(100, 100);
204 layer_size = gfx::Size(100, 100);
205 aperture_rect = gfx::Rect(0, 0, 100, 100);
206 border = gfx::Rect(0, 0, 0, 0);
207 fill_center = true;
208 expected_quad_size = 1;
209 NinePatchLayerLayoutTest(bitmap_size,
210 aperture_rect,
211 layer_size,
212 border,
213 fill_center,
214 expected_quad_size);
217 TEST(NinePatchLayerImplTest, Occlusion) {
218 gfx::Size layer_size(1000, 1000);
219 gfx::Size viewport_size(1000, 1000);
221 LayerTestCommon::LayerImplTest impl;
223 SkBitmap sk_bitmap;
224 sk_bitmap.allocN32Pixels(10, 10);
225 sk_bitmap.setImmutable();
226 UIResourceId uid = 5;
227 UIResourceBitmap bitmap(sk_bitmap);
228 impl.host_impl()->CreateUIResource(uid, bitmap);
230 NinePatchLayerImpl* nine_patch_layer_impl =
231 impl.AddChildToRoot<NinePatchLayerImpl>();
232 nine_patch_layer_impl->SetBounds(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_layer_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->SetDrawsContent(true);
307 impl.CalcDrawProps(viewport_size);
310 SCOPED_TRACE("Use opaque image");
312 nine_patch_layer_impl->SetUIResourceId(uid_opaque);
313 nine_patch_layer_impl->SetImageBounds(gfx::Size(10, 10));
315 gfx::Rect aperture = gfx::Rect(3, 3, 4, 4);
316 gfx::Rect border = gfx::Rect(300, 300, 400, 400);
317 nine_patch_layer_impl->SetLayout(aperture, border, true);
319 impl.AppendQuadsWithOcclusion(nine_patch_layer_impl, gfx::Rect());
321 const QuadList &quad_list = impl.quad_list();
322 for (QuadList::ConstBackToFrontIterator it = quad_list.BackToFrontBegin();
323 it != quad_list.BackToFrontEnd(); ++it)
324 EXPECT_FALSE(it->ShouldDrawWithBlending());
328 SCOPED_TRACE("Use tranparent image");
330 nine_patch_layer_impl->SetUIResourceId(uid_alpha);
332 impl.AppendQuadsWithOcclusion(nine_patch_layer_impl, gfx::Rect());
334 const QuadList &quad_list = impl.quad_list();
335 for (QuadList::ConstBackToFrontIterator it = quad_list.BackToFrontBegin();
336 it != quad_list.BackToFrontEnd(); ++it)
337 EXPECT_TRUE(it->ShouldDrawWithBlending());
341 } // namespace
342 } // namespace cc