painted_scrollbar_layer_impl_unittest.cc revision 46d4c2bc3267f3f028f39e7e311b0f89aba2e4fd
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
10namespace cc {
11namespace {
12
13TEST(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::VerifyQuadsCoverRectWithOcclusion(
62        impl.quad_list(),
63        gfx::Rect(layer_size),
64        occluded,
65        &partially_occluded_count);
66    EXPECT_EQ(2u, impl.quad_list().size());
67    EXPECT_EQ(0u, partially_occluded_count);
68  }
69
70  {
71    SCOPED_TRACE("Full occlusion");
72    gfx::Rect occluded(scrollbar_layer_impl->visible_content_rect());
73    impl.AppendQuadsWithOcclusion(scrollbar_layer_impl, occluded);
74
75    LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(), gfx::Rect());
76    EXPECT_EQ(impl.quad_list().size(), 0u);
77  }
78
79  {
80    SCOPED_TRACE("Partial occlusion");
81    gfx::Rect occluded(0, 0, 5, 1000);
82    impl.AppendQuadsWithOcclusion(scrollbar_layer_impl, occluded);
83
84    size_t partially_occluded_count = 0;
85    LayerTestCommon::VerifyQuadsCoverRectWithOcclusion(
86        impl.quad_list(), thumb_rect, occluded, &partially_occluded_count);
87    // The layer outputs two quads, which is partially occluded.
88    EXPECT_EQ(2u, impl.quad_list().size());
89    EXPECT_EQ(2u, partially_occluded_count);
90  }
91}
92
93}  // namespace
94}  // namespace cc
95