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.
4
5 #include "cc/layers/painted_scrollbar_layer_impl.h"
6
7 #include "cc/test/layer_test_common.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9
10 namespace cc {
11 namespace {
12
TEST(PaintedScrollbarLayerImplTest,Occlusion)13 TEST(PaintedScrollbarLayerImplTest, Occlusion) {
14 gfx::Size layer_size(10, 1000);
15 gfx::Size viewport_size(1000, 1000);
16
17 LayerTestCommon::LayerImplTest impl;
18
19 SkBitmap thumb_sk_bitmap;
20 thumb_sk_bitmap.allocN32Pixels(10, 10);
21 thumb_sk_bitmap.setImmutable();
22 UIResourceId thumb_uid = 5;
23 UIResourceBitmap thumb_bitmap(thumb_sk_bitmap);
24 impl.host_impl()->CreateUIResource(thumb_uid, thumb_bitmap);
25
26 SkBitmap track_sk_bitmap;
27 track_sk_bitmap.allocN32Pixels(10, 10);
28 track_sk_bitmap.setImmutable();
29 UIResourceId track_uid = 6;
30 UIResourceBitmap track_bitmap(track_sk_bitmap);
31 impl.host_impl()->CreateUIResource(track_uid, track_bitmap);
32
33 ScrollbarOrientation orientation = VERTICAL;
34
35 PaintedScrollbarLayerImpl* scrollbar_layer_impl =
36 impl.AddChildToRoot<PaintedScrollbarLayerImpl>(orientation);
37 scrollbar_layer_impl->SetBounds(layer_size);
38 scrollbar_layer_impl->SetContentBounds(layer_size);
39 scrollbar_layer_impl->SetDrawsContent(true);
40 scrollbar_layer_impl->SetThumbThickness(layer_size.width());
41 scrollbar_layer_impl->SetThumbLength(500);
42 scrollbar_layer_impl->SetTrackLength(layer_size.height());
43 scrollbar_layer_impl->SetCurrentPos(100.f / 4);
44 scrollbar_layer_impl->SetMaximum(100);
45 scrollbar_layer_impl->SetVisibleToTotalLengthRatio(1.f / 2);
46 scrollbar_layer_impl->set_track_ui_resource_id(track_uid);
47 scrollbar_layer_impl->set_thumb_ui_resource_id(thumb_uid);
48
49 impl.CalcDrawProps(viewport_size);
50
51 gfx::Rect thumb_rect = scrollbar_layer_impl->ComputeThumbQuadRect();
52 EXPECT_EQ(gfx::Rect(0, 500 / 4, 10, layer_size.height() / 2).ToString(),
53 thumb_rect.ToString());
54
55 {
56 SCOPED_TRACE("No occlusion");
57 gfx::Rect occluded;
58 impl.AppendQuadsWithOcclusion(scrollbar_layer_impl, occluded);
59
60 size_t partially_occluded_count = 0;
61 LayerTestCommon::VerifyQuadsAreOccluded(
62 impl.quad_list(), occluded, &partially_occluded_count);
63 EXPECT_EQ(2u, impl.quad_list().size());
64 EXPECT_EQ(0u, partially_occluded_count);
65 }
66
67 {
68 SCOPED_TRACE("Full occlusion");
69 gfx::Rect occluded(scrollbar_layer_impl->visible_content_rect());
70 impl.AppendQuadsWithOcclusion(scrollbar_layer_impl, occluded);
71
72 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(), gfx::Rect());
73 EXPECT_EQ(impl.quad_list().size(), 0u);
74 }
75
76 {
77 SCOPED_TRACE("Partial occlusion");
78 gfx::Rect occluded(0, 0, 5, 1000);
79 impl.AppendQuadsWithOcclusion(scrollbar_layer_impl, occluded);
80
81 size_t partially_occluded_count = 0;
82 LayerTestCommon::VerifyQuadsAreOccluded(
83 impl.quad_list(), occluded, &partially_occluded_count);
84 // The layer outputs two quads, which is partially occluded.
85 EXPECT_EQ(2u, impl.quad_list().size());
86 EXPECT_EQ(2u, partially_occluded_count);
87 }
88 }
89
90 } // namespace
91 } // namespace cc
92