1 // Copyright 2014 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/painted_scrollbar_layer_impl.h"
7 #include "cc/quads/draw_quad.h"
8 #include "cc/test/layer_test_common.h"
9 #include "testing/gtest/include/gtest/gtest.h"
14 TEST(PaintedScrollbarLayerImplTest
, Occlusion
) {
15 gfx::Size
layer_size(10, 1000);
17 gfx::Size
scaled_layer_size(20, 2000);
18 gfx::Size
viewport_size(1000, 1000);
20 LayerTestCommon::LayerImplTest impl
;
22 SkBitmap thumb_sk_bitmap
;
23 thumb_sk_bitmap
.allocN32Pixels(10, 10);
24 thumb_sk_bitmap
.setImmutable();
25 UIResourceId thumb_uid
= 5;
26 UIResourceBitmap
thumb_bitmap(thumb_sk_bitmap
);
27 impl
.host_impl()->CreateUIResource(thumb_uid
, thumb_bitmap
);
29 SkBitmap track_sk_bitmap
;
30 track_sk_bitmap
.allocN32Pixels(10, 10);
31 track_sk_bitmap
.setImmutable();
32 UIResourceId track_uid
= 6;
33 UIResourceBitmap
track_bitmap(track_sk_bitmap
);
34 impl
.host_impl()->CreateUIResource(track_uid
, track_bitmap
);
36 ScrollbarOrientation orientation
= VERTICAL
;
38 PaintedScrollbarLayerImpl
* scrollbar_layer_impl
=
39 impl
.AddChildToRoot
<PaintedScrollbarLayerImpl
>(orientation
);
40 scrollbar_layer_impl
->SetBounds(layer_size
);
41 scrollbar_layer_impl
->SetContentsOpaque(true);
42 scrollbar_layer_impl
->set_internal_contents_scale_and_bounds(
43 scale
, scaled_layer_size
);
44 scrollbar_layer_impl
->SetDrawsContent(true);
45 scrollbar_layer_impl
->SetThumbThickness(layer_size
.width());
46 scrollbar_layer_impl
->SetThumbLength(500);
47 scrollbar_layer_impl
->SetTrackLength(layer_size
.height());
48 scrollbar_layer_impl
->SetCurrentPos(100.f
/ 4);
49 scrollbar_layer_impl
->SetMaximum(100);
50 scrollbar_layer_impl
->SetVisibleToTotalLengthRatio(1.f
/ 2);
51 scrollbar_layer_impl
->set_track_ui_resource_id(track_uid
);
52 scrollbar_layer_impl
->set_thumb_ui_resource_id(thumb_uid
);
54 impl
.CalcDrawProps(viewport_size
);
56 gfx::Rect thumb_rect
= scrollbar_layer_impl
->ComputeThumbQuadRect();
57 EXPECT_EQ(gfx::Rect(0, 500 / 4, 10, layer_size
.height() / 2).ToString(),
58 thumb_rect
.ToString());
61 SCOPED_TRACE("No occlusion");
63 impl
.AppendQuadsWithOcclusion(scrollbar_layer_impl
, occluded
);
65 size_t partially_occluded_count
= 0;
66 LayerTestCommon::VerifyQuadsAreOccluded(
67 impl
.quad_list(), occluded
, &partially_occluded_count
);
68 EXPECT_EQ(2u, impl
.quad_list().size());
69 EXPECT_EQ(0u, partially_occluded_count
);
71 // Note: this is also testing that the thumb and track are both
72 // scaled by the internal contents scale. It's not occlusion-related
73 // but is easy to verify here.
74 const DrawQuad
* thumb_quad
= impl
.quad_list().ElementAt(0);
75 const DrawQuad
* track_quad
= impl
.quad_list().ElementAt(1);
77 gfx::Rect scaled_thumb_rect
= gfx::ScaleToEnclosingRect(thumb_rect
, scale
);
78 EXPECT_EQ(track_quad
->rect
.ToString(),
79 gfx::Rect(scaled_layer_size
).ToString());
80 EXPECT_EQ(track_quad
->opaque_rect
.ToString(),
81 gfx::Rect(scaled_layer_size
).ToString());
82 EXPECT_EQ(track_quad
->visible_rect
.ToString(),
83 gfx::Rect(scaled_layer_size
).ToString());
84 EXPECT_EQ(thumb_quad
->rect
.ToString(), scaled_thumb_rect
.ToString());
85 EXPECT_EQ(thumb_quad
->visible_rect
.ToString(),
86 scaled_thumb_rect
.ToString());
90 SCOPED_TRACE("Full occlusion");
91 gfx::Rect
occluded(scrollbar_layer_impl
->visible_layer_rect());
92 impl
.AppendQuadsWithOcclusion(scrollbar_layer_impl
, occluded
);
94 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl
.quad_list(), gfx::Rect());
95 EXPECT_EQ(impl
.quad_list().size(), 0u);
99 SCOPED_TRACE("Partial occlusion");
100 gfx::Rect
occluded(0, 0, 5, 1000);
101 impl
.AppendQuadsWithOcclusion(scrollbar_layer_impl
, occluded
);
103 size_t partially_occluded_count
= 0;
104 LayerTestCommon::VerifyQuadsAreOccluded(
105 impl
.quad_list(), occluded
, &partially_occluded_count
);
106 // The layer outputs two quads, which is partially occluded.
107 EXPECT_EQ(2u, impl
.quad_list().size());
108 EXPECT_EQ(2u, partially_occluded_count
);