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 "cc/scrollbar_layer.h"
7 #include "cc/scrollbar_animation_controller.h"
8 #include "cc/scrollbar_layer_impl.h"
9 #include "cc/single_thread_proxy.h"
10 #include "cc/test/fake_impl_proxy.h"
11 #include "cc/test/fake_layer_tree_host_impl.h"
12 #include "cc/test/fake_web_scrollbar_theme_geometry.h"
13 #include "cc/test/layer_tree_test_common.h"
14 #include "cc/tree_synchronizer.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include <public/WebScrollbar.h>
17 #include <public/WebScrollbarThemeGeometry.h>
18 #include <public/WebScrollbarThemePainter.h>
23 class FakeWebScrollbar
: public WebKit::WebScrollbar
{
25 static scoped_ptr
<FakeWebScrollbar
> create() { return make_scoped_ptr(new FakeWebScrollbar()); }
27 // WebScrollbar implementation
28 virtual bool isOverlay() const OVERRIDE
{ return false; }
29 virtual int value() const OVERRIDE
{ return 0; }
30 virtual WebKit::WebPoint
location() const OVERRIDE
{ return WebKit::WebPoint(); }
31 virtual WebKit::WebSize
size() const OVERRIDE
{ return WebKit::WebSize(); }
32 virtual bool enabled() const OVERRIDE
{ return true; }
33 virtual int maximum() const OVERRIDE
{ return 0; }
34 virtual int totalSize() const OVERRIDE
{ return 0; }
35 virtual bool isScrollViewScrollbar() const OVERRIDE
{ return false; }
36 virtual bool isScrollableAreaActive() const OVERRIDE
{ return true; }
37 virtual void getTickmarks(WebKit::WebVector
<WebKit::WebRect
>&) const OVERRIDE
{ }
38 virtual ScrollbarControlSize
controlSize() const OVERRIDE
{ return WebScrollbar::RegularScrollbar
; }
39 virtual ScrollbarPart
pressedPart() const OVERRIDE
{ return WebScrollbar::NoPart
; }
40 virtual ScrollbarPart
hoveredPart() const OVERRIDE
{ return WebScrollbar::NoPart
; }
41 virtual ScrollbarOverlayStyle
scrollbarOverlayStyle() const OVERRIDE
{ return WebScrollbar::ScrollbarOverlayStyleDefault
; }
42 virtual bool isCustomScrollbar() const OVERRIDE
{ return false; }
43 virtual Orientation
orientation() const OVERRIDE
{ return WebScrollbar::Horizontal
; }
46 TEST(ScrollbarLayerTest
, resolveScrollLayerPointer
)
49 FakeLayerTreeHostImpl
hostImpl(&proxy
);
50 WebKit::WebScrollbarThemePainter painter
;
53 scoped_ptr
<WebKit::WebScrollbar
> scrollbar(FakeWebScrollbar::create());
54 scoped_refptr
<Layer
> layerTreeRoot
= Layer::create();
55 scoped_refptr
<Layer
> child1
= Layer::create();
56 scoped_refptr
<Layer
> child2
= ScrollbarLayer::create(scrollbar
.Pass(), painter
, WebKit::FakeWebScrollbarThemeGeometry::create(), child1
->id());
57 layerTreeRoot
->addChild(child1
);
58 layerTreeRoot
->addChild(child2
);
60 scoped_ptr
<LayerImpl
> layerImplTreeRoot
= TreeSynchronizer::synchronizeTrees(layerTreeRoot
.get(), scoped_ptr
<LayerImpl
>(), &hostImpl
);
62 LayerImpl
* ccChild1
= layerImplTreeRoot
->children()[0];
63 ScrollbarLayerImpl
* ccChild2
= static_cast<ScrollbarLayerImpl
*>(layerImplTreeRoot
->children()[1]);
65 EXPECT_TRUE(ccChild1
->scrollbarAnimationController());
66 EXPECT_EQ(ccChild1
->horizontalScrollbarLayer(), ccChild2
);
69 { // another traverse order
70 scoped_ptr
<WebKit::WebScrollbar
> scrollbar(FakeWebScrollbar::create());
71 scoped_refptr
<Layer
> layerTreeRoot
= Layer::create();
72 scoped_refptr
<Layer
> child2
= Layer::create();
73 scoped_refptr
<Layer
> child1
= ScrollbarLayer::create(scrollbar
.Pass(), painter
, WebKit::FakeWebScrollbarThemeGeometry::create(), child2
->id());
74 layerTreeRoot
->addChild(child1
);
75 layerTreeRoot
->addChild(child2
);
77 scoped_ptr
<LayerImpl
> layerImplTreeRoot
= TreeSynchronizer::synchronizeTrees(layerTreeRoot
.get(), scoped_ptr
<LayerImpl
>(), &hostImpl
);
79 ScrollbarLayerImpl
* ccChild1
= static_cast<ScrollbarLayerImpl
*>(layerImplTreeRoot
->children()[0]);
80 LayerImpl
* ccChild2
= layerImplTreeRoot
->children()[1];
82 EXPECT_TRUE(ccChild2
->scrollbarAnimationController());
83 EXPECT_EQ(ccChild2
->horizontalScrollbarLayer(), ccChild1
);
87 TEST(ScrollbarLayerTest
, scrollOffsetSynchronization
)
90 FakeLayerTreeHostImpl
hostImpl(&proxy
);
91 WebKit::WebScrollbarThemePainter painter
;
93 scoped_ptr
<WebKit::WebScrollbar
> scrollbar(FakeWebScrollbar::create());
94 scoped_refptr
<Layer
> layerTreeRoot
= Layer::create();
95 scoped_refptr
<Layer
> contentLayer
= Layer::create();
96 scoped_refptr
<Layer
> scrollbarLayer
= ScrollbarLayer::create(scrollbar
.Pass(), painter
, WebKit::FakeWebScrollbarThemeGeometry::create(), layerTreeRoot
->id());
97 layerTreeRoot
->addChild(contentLayer
);
98 layerTreeRoot
->addChild(scrollbarLayer
);
100 layerTreeRoot
->setScrollOffset(gfx::Vector2d(10, 20));
101 layerTreeRoot
->setMaxScrollOffset(gfx::Vector2d(30, 50));
102 contentLayer
->setBounds(gfx::Size(100, 200));
104 scoped_ptr
<LayerImpl
> layerImplTreeRoot
= TreeSynchronizer::synchronizeTrees(layerTreeRoot
.get(), scoped_ptr
<LayerImpl
>(), &hostImpl
);
106 ScrollbarLayerImpl
* ccScrollbarLayer
= static_cast<ScrollbarLayerImpl
*>(layerImplTreeRoot
->children()[1]);
108 EXPECT_EQ(10, ccScrollbarLayer
->currentPos());
109 EXPECT_EQ(100, ccScrollbarLayer
->totalSize());
110 EXPECT_EQ(30, ccScrollbarLayer
->maximum());
112 layerTreeRoot
->setScrollOffset(gfx::Vector2d(100, 200));
113 layerTreeRoot
->setMaxScrollOffset(gfx::Vector2d(300, 500));
114 contentLayer
->setBounds(gfx::Size(1000, 2000));
116 ScrollbarAnimationController
* scrollbarController
= layerImplTreeRoot
->scrollbarAnimationController();
117 layerImplTreeRoot
= TreeSynchronizer::synchronizeTrees(layerTreeRoot
.get(), layerImplTreeRoot
.Pass(), &hostImpl
);
118 EXPECT_EQ(scrollbarController
, layerImplTreeRoot
->scrollbarAnimationController());
120 EXPECT_EQ(100, ccScrollbarLayer
->currentPos());
121 EXPECT_EQ(1000, ccScrollbarLayer
->totalSize());
122 EXPECT_EQ(300, ccScrollbarLayer
->maximum());
124 layerImplTreeRoot
->scrollBy(gfx::Vector2d(12, 34));
126 EXPECT_EQ(112, ccScrollbarLayer
->currentPos());
127 EXPECT_EQ(1000, ccScrollbarLayer
->totalSize());
128 EXPECT_EQ(300, ccScrollbarLayer
->maximum());
131 class ScrollbarLayerTestMaxTextureSize
: public ThreadedTest
{
133 ScrollbarLayerTestMaxTextureSize() {}
135 void setScrollbarBounds(gfx::Size bounds
) {
139 virtual void beginTest() OVERRIDE
141 scoped_ptr
<WebKit::WebScrollbar
> scrollbar(FakeWebScrollbar::create());
142 m_scrollbarLayer
= ScrollbarLayer::create(scrollbar
.Pass(), m_painter
, WebKit::FakeWebScrollbarThemeGeometry::create(), 1);
143 m_scrollbarLayer
->setBounds(m_bounds
);
144 m_layerTreeHost
->rootLayer()->addChild(m_scrollbarLayer
);
146 m_scrollLayer
= Layer::create();
147 m_scrollbarLayer
->setScrollLayerId(m_scrollLayer
->id());
148 m_layerTreeHost
->rootLayer()->addChild(m_scrollLayer
);
150 postSetNeedsCommitToMainThread();
153 virtual void commitCompleteOnThread(LayerTreeHostImpl
* impl
) OVERRIDE
155 m_layerTreeHost
->initializeRendererIfNeeded();
157 const int kMaxTextureSize
= m_layerTreeHost
->rendererCapabilities().maxTextureSize
;
159 // Check first that we're actually testing something.
160 EXPECT_GT(m_scrollbarLayer
->bounds().width(), kMaxTextureSize
);
162 EXPECT_EQ(m_scrollbarLayer
->contentBounds().width(), kMaxTextureSize
- 1);
163 EXPECT_EQ(m_scrollbarLayer
->contentBounds().height(), kMaxTextureSize
- 1);
168 virtual void afterTest() OVERRIDE
173 scoped_refptr
<ScrollbarLayer
> m_scrollbarLayer
;
174 scoped_refptr
<Layer
> m_scrollLayer
;
175 WebKit::WebScrollbarThemePainter m_painter
;
179 TEST_F(ScrollbarLayerTestMaxTextureSize
, runTest
) {
180 WebKit::FakeWebGraphicsContext3D context
;
182 context
.getIntegerv(GL_MAX_TEXTURE_SIZE
, &max_size
);
183 setScrollbarBounds(gfx::Size(max_size
+ 100, max_size
+ 100));