NaCl: Reinstate the "nacl_revision" field in DEPS
[chromium-blink-merge.git] / cc / layers / ui_resource_layer_impl_unittest.cc
blob59c1300b017d81828cfd543a5137db294dfa6c0c
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.
5 #include "cc/layers/append_quads_data.h"
6 #include "cc/layers/ui_resource_layer_impl.h"
7 #include "cc/quads/draw_quad.h"
8 #include "cc/resources/ui_resource_bitmap.h"
9 #include "cc/resources/ui_resource_client.h"
10 #include "cc/test/fake_impl_proxy.h"
11 #include "cc/test/fake_layer_tree_host_impl.h"
12 #include "cc/test/fake_ui_resource_layer_tree_host_impl.h"
13 #include "cc/test/layer_test_common.h"
14 #include "cc/test/test_shared_bitmap_manager.h"
15 #include "cc/trees/single_thread_proxy.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "ui/gfx/transform.h"
20 namespace cc {
21 namespace {
23 scoped_ptr<UIResourceLayerImpl> GenerateUIResourceLayer(
24 FakeUIResourceLayerTreeHostImpl* host_impl,
25 const gfx::Size& bitmap_size,
26 const gfx::Size& layer_size,
27 bool opaque,
28 UIResourceId uid) {
29 gfx::Rect visible_content_rect(layer_size);
30 scoped_ptr<UIResourceLayerImpl> layer =
31 UIResourceLayerImpl::Create(host_impl->active_tree(), 1);
32 layer->draw_properties().visible_content_rect = visible_content_rect;
33 layer->SetBounds(layer_size);
34 layer->SetContentBounds(layer_size);
35 layer->CreateRenderSurface();
36 layer->draw_properties().render_target = layer.get();
38 UIResourceBitmap bitmap(bitmap_size, opaque);
40 host_impl->CreateUIResource(uid, bitmap);
41 layer->SetUIResourceId(uid);
43 return layer.Pass();
46 void QuadSizeTest(scoped_ptr<UIResourceLayerImpl> layer,
47 size_t expected_quad_size) {
48 MockOcclusionTracker<LayerImpl> occlusion_tracker;
49 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
51 AppendQuadsData data;
52 layer->AppendQuads(render_pass.get(), occlusion_tracker, &data);
54 // Verify quad rects
55 const QuadList& quads = render_pass->quad_list;
56 EXPECT_EQ(expected_quad_size, quads.size());
59 TEST(UIResourceLayerImplTest, VerifyDrawQuads) {
60 FakeImplProxy proxy;
61 TestSharedBitmapManager shared_bitmap_manager;
62 FakeUIResourceLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager);
63 // Make sure we're appending quads when there are valid values.
64 gfx::Size bitmap_size(100, 100);
65 gfx::Size layer_size(100, 100);;
66 size_t expected_quad_size = 1;
67 bool opaque = true;
68 UIResourceId uid = 1;
69 scoped_ptr<UIResourceLayerImpl> layer = GenerateUIResourceLayer(&host_impl,
70 bitmap_size,
71 layer_size,
72 opaque,
73 uid);
74 QuadSizeTest(layer.Pass(), expected_quad_size);
76 // Make sure we're not appending quads when there are invalid values.
77 expected_quad_size = 0;
78 uid = 0;
79 layer = GenerateUIResourceLayer(&host_impl,
80 bitmap_size,
81 layer_size,
82 opaque,
83 uid);
84 QuadSizeTest(layer.Pass(), expected_quad_size);
87 void OpaqueBoundsTest(scoped_ptr<UIResourceLayerImpl> layer,
88 const gfx::Rect& expected_opaque_bounds) {
89 MockOcclusionTracker<LayerImpl> occlusion_tracker;
90 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
92 AppendQuadsData data;
93 layer->AppendQuads(render_pass.get(), occlusion_tracker, &data);
95 // Verify quad rects
96 const QuadList& quads = render_pass->quad_list;
97 EXPECT_GE(quads.size(), (size_t)0);
98 gfx::Rect opaque_rect = quads.at(0)->opaque_rect;
99 EXPECT_EQ(expected_opaque_bounds, opaque_rect);
102 TEST(UIResourceLayerImplTest, VerifySetOpaqueOnSkBitmap) {
103 FakeImplProxy proxy;
104 TestSharedBitmapManager shared_bitmap_manager;
105 FakeUIResourceLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager);
107 gfx::Size bitmap_size(100, 100);
108 gfx::Size layer_size(100, 100);;
109 bool opaque = false;
110 UIResourceId uid = 1;
111 scoped_ptr<UIResourceLayerImpl> layer = GenerateUIResourceLayer(&host_impl,
112 bitmap_size,
113 layer_size,
114 opaque,
115 uid);
116 gfx::Rect expected_opaque_bounds;
117 OpaqueBoundsTest(layer.Pass(), expected_opaque_bounds);
119 opaque = true;
120 layer = GenerateUIResourceLayer(&host_impl,
121 bitmap_size,
122 layer_size,
123 opaque,
124 uid);
125 expected_opaque_bounds = gfx::Rect(layer->bounds());
126 OpaqueBoundsTest(layer.Pass(), expected_opaque_bounds);
129 TEST(UIResourceLayerImplTest, VerifySetOpaqueOnLayer) {
130 FakeImplProxy proxy;
131 TestSharedBitmapManager shared_bitmap_manager;
132 FakeUIResourceLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager);
134 gfx::Size bitmap_size(100, 100);
135 gfx::Size layer_size(100, 100);
136 bool skbitmap_opaque = false;
137 UIResourceId uid = 1;
138 scoped_ptr<UIResourceLayerImpl> layer = GenerateUIResourceLayer(
139 &host_impl, bitmap_size, layer_size, skbitmap_opaque, uid);
140 layer->SetContentsOpaque(false);
141 gfx::Rect expected_opaque_bounds;
142 OpaqueBoundsTest(layer.Pass(), expected_opaque_bounds);
144 layer = GenerateUIResourceLayer(
145 &host_impl, bitmap_size, layer_size, skbitmap_opaque, uid);
146 layer->SetContentsOpaque(true);
147 expected_opaque_bounds = gfx::Rect(layer->bounds());
148 OpaqueBoundsTest(layer.Pass(), expected_opaque_bounds);
151 TEST(UIResourceLayerImplTest, Occlusion) {
152 gfx::Size layer_size(1000, 1000);
153 gfx::Size viewport_size(1000, 1000);
155 LayerTestCommon::LayerImplTest impl;
157 SkBitmap sk_bitmap;
158 sk_bitmap.allocN32Pixels(10, 10);
159 sk_bitmap.setImmutable();
160 UIResourceId uid = 5;
161 UIResourceBitmap bitmap(sk_bitmap);
162 impl.host_impl()->CreateUIResource(uid, bitmap);
164 UIResourceLayerImpl* ui_resource_layer_impl =
165 impl.AddChildToRoot<UIResourceLayerImpl>();
166 ui_resource_layer_impl->SetBounds(layer_size);
167 ui_resource_layer_impl->SetContentBounds(layer_size);
168 ui_resource_layer_impl->SetDrawsContent(true);
169 ui_resource_layer_impl->SetUIResourceId(uid);
171 impl.CalcDrawProps(viewport_size);
174 SCOPED_TRACE("No occlusion");
175 gfx::Rect occluded;
176 impl.AppendQuadsWithOcclusion(ui_resource_layer_impl, occluded);
178 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(),
179 gfx::Rect(layer_size));
180 EXPECT_EQ(1u, impl.quad_list().size());
184 SCOPED_TRACE("Full occlusion");
185 gfx::Rect occluded(ui_resource_layer_impl->visible_content_rect());
186 impl.AppendQuadsWithOcclusion(ui_resource_layer_impl, occluded);
188 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(), gfx::Rect());
189 EXPECT_EQ(impl.quad_list().size(), 0u);
193 SCOPED_TRACE("Partial occlusion");
194 gfx::Rect occluded(200, 0, 800, 1000);
195 impl.AppendQuadsWithOcclusion(ui_resource_layer_impl, occluded);
197 size_t partially_occluded_count = 0;
198 LayerTestCommon::VerifyQuadsCoverRectWithOcclusion(
199 impl.quad_list(),
200 gfx::Rect(layer_size),
201 occluded,
202 &partially_occluded_count);
203 // The layer outputs one quad, which is partially occluded.
204 EXPECT_EQ(1u, impl.quad_list().size());
205 EXPECT_EQ(1u, partially_occluded_count);
209 } // namespace
210 } // namespace cc