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.
6 #include "cc/blink/web_layer_impl_fixed_bounds.h"
7 #include "cc/layers/picture_image_layer.h"
8 #include "cc/test/fake_layer_tree_host.h"
9 #include "cc/test/geometry_test_utils.h"
10 #include "cc/test/test_task_graph_runner.h"
11 #include "cc/trees/layer_tree_host_common.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/WebKit/public/platform/WebFloatPoint.h"
14 #include "third_party/WebKit/public/platform/WebSize.h"
15 #include "third_party/skia/include/utils/SkMatrix44.h"
16 #include "ui/gfx/geometry/point3_f.h"
18 using blink::WebFloatPoint
;
24 TEST(WebLayerImplFixedBoundsTest
, IdentityBounds
) {
25 scoped_ptr
<WebLayerImplFixedBounds
> layer(new WebLayerImplFixedBounds());
26 layer
->SetFixedBounds(gfx::Size(100, 100));
27 layer
->setBounds(WebSize(100, 100));
28 EXPECT_EQ(WebSize(100, 100), layer
->bounds());
29 EXPECT_EQ(gfx::Size(100, 100), layer
->layer()->bounds());
30 EXPECT_EQ(gfx::Transform(), layer
->layer()->transform());
33 gfx::Point3F
TransformPoint(const gfx::Transform
& transform
,
34 const gfx::Point3F
& point
) {
35 gfx::Point3F result
= point
;
36 transform
.TransformPoint(&result
);
40 void CheckBoundsScaleSimple(WebLayerImplFixedBounds
* layer
,
41 const WebSize
& bounds
,
42 const gfx::Size
& fixed_bounds
) {
43 layer
->setBounds(bounds
);
44 layer
->SetFixedBounds(fixed_bounds
);
46 EXPECT_EQ(bounds
, layer
->bounds());
47 EXPECT_EQ(fixed_bounds
, layer
->layer()->bounds());
48 EXPECT_TRUE(layer
->transform().isIdentity());
50 // An arbitrary point to check the scale and transforms.
51 gfx::Point3F
original_point(10, 20, 1);
52 gfx::Point3F
scaled_point(
53 original_point
.x() * bounds
.width
/ fixed_bounds
.width(),
54 original_point
.y() * bounds
.height
/ fixed_bounds
.height(),
56 // Test if the bounds scale is correctly applied in transform.
57 EXPECT_POINT3F_EQ(scaled_point
, TransformPoint(layer
->layer()->transform(),
61 TEST(WebLayerImplFixedBoundsTest
, BoundsScaleSimple
) {
62 scoped_ptr
<WebLayerImplFixedBounds
> layer(new WebLayerImplFixedBounds());
63 CheckBoundsScaleSimple(layer
.get(), WebSize(100, 200), gfx::Size(150, 250));
64 // Change fixed_bounds.
65 CheckBoundsScaleSimple(layer
.get(), WebSize(100, 200), gfx::Size(75, 100));
67 CheckBoundsScaleSimple(layer
.get(), WebSize(300, 100), gfx::Size(75, 100));
70 void ExpectEqualLayerRectsInTarget(cc::Layer
* layer1
, cc::Layer
* layer2
) {
71 gfx::RectF
layer1_rect_in_target(layer1
->bounds());
72 layer1
->draw_transform().TransformRect(&layer1_rect_in_target
);
74 gfx::RectF
layer2_rect_in_target(layer2
->bounds());
75 layer2
->draw_transform().TransformRect(&layer2_rect_in_target
);
77 EXPECT_FLOAT_RECT_EQ(layer1_rect_in_target
, layer2_rect_in_target
);
80 void CompareFixedBoundsLayerAndNormalLayer(const WebFloatPoint
& anchor_point
,
81 const gfx::Transform
& transform
) {
82 const gfx::Size
kDeviceViewportSize(800, 600);
83 const float kDeviceScaleFactor
= 2.f
;
84 const float kPageScaleFactor
= 1.5f
;
86 WebSize
bounds(150, 200);
87 WebFloatPoint
position(20, 30);
88 gfx::Size
fixed_bounds(160, 70);
90 scoped_ptr
<WebLayerImplFixedBounds
> root_layer(new WebLayerImplFixedBounds());
92 WebLayerImplFixedBounds
* fixed_bounds_layer
= new WebLayerImplFixedBounds(
93 cc::PictureImageLayer::Create(WebLayerImpl::LayerSettings()));
94 fixed_bounds_layer
->setBounds(bounds
);
95 fixed_bounds_layer
->SetFixedBounds(fixed_bounds
);
96 fixed_bounds_layer
->setTransform(transform
.matrix());
97 fixed_bounds_layer
->setPosition(position
);
98 root_layer
->addChild(fixed_bounds_layer
);
100 WebLayerImpl
* normal_layer(
101 new WebLayerImpl(cc::PictureImageLayer::Create(cc::LayerSettings())));
103 normal_layer
->setBounds(bounds
);
104 normal_layer
->setTransform(transform
.matrix());
105 normal_layer
->setPosition(position
);
106 root_layer
->addChild(normal_layer
);
108 cc::FakeLayerTreeHostClient
client(cc::FakeLayerTreeHostClient::DIRECT_3D
);
109 cc::TestTaskGraphRunner task_graph_runner
;
110 scoped_ptr
<cc::FakeLayerTreeHost
> host
=
111 cc::FakeLayerTreeHost::Create(&client
, &task_graph_runner
);
112 host
->SetRootLayer(root_layer
->layer());
115 cc::LayerTreeHostCommon::CalcDrawPropsMainInputs
inputs(
116 root_layer
->layer(), kDeviceViewportSize
);
117 inputs
.device_scale_factor
= kDeviceScaleFactor
;
118 inputs
.page_scale_factor
= kPageScaleFactor
;
119 inputs
.page_scale_layer
= root_layer
->layer(),
120 cc::LayerTreeHostCommon::CalculateDrawProperties(&inputs
);
122 ExpectEqualLayerRectsInTarget(normal_layer
->layer(),
123 fixed_bounds_layer
->layer());
126 // Change of fixed bounds should not affect the target geometries.
127 fixed_bounds_layer
->SetFixedBounds(
128 gfx::Size(fixed_bounds
.width() / 2, fixed_bounds
.height() * 2));
131 cc::LayerTreeHostCommon::CalcDrawPropsMainInputs
inputs(
132 root_layer
->layer(), kDeviceViewportSize
);
133 inputs
.device_scale_factor
= kDeviceScaleFactor
;
134 inputs
.page_scale_factor
= kPageScaleFactor
;
135 inputs
.page_scale_layer
= root_layer
->layer(),
136 cc::LayerTreeHostCommon::CalculateDrawProperties(&inputs
);
138 ExpectEqualLayerRectsInTarget(normal_layer
->layer(),
139 fixed_bounds_layer
->layer());
143 // TODO(perkj): CompareToWebLayerImplSimple disabled on LSAN due to crbug/386080
144 #if defined(LEAK_SANITIZER)
145 #define MAYBE_CompareToWebLayerImplSimple DISABLED_CompareToWebLayerImplSimple
147 #define MAYBE_CompareToWebLayerImplSimple CompareToWebLayerImplSimple
149 // A black box test that ensures WebLayerImplFixedBounds won't change target
150 // geometries. Simple case: identity transforms and zero anchor point.
151 TEST(WebLayerImplFixedBoundsTest
, MAYBE_CompareToWebLayerImplSimple
) {
152 CompareFixedBoundsLayerAndNormalLayer(WebFloatPoint(0, 0), gfx::Transform());
155 // TODO(perkj): CompareToWebLayerImplComplex disabled on LSAN due to
157 #if defined(LEAK_SANITIZER)
158 #define MAYBE_CompareToWebLayerImplComplex DISABLED_CompareToWebLayerImplComplex
160 #define MAYBE_CompareToWebLayerImplComplex CompareToWebLayerImplComplex
162 // A black box test that ensures WebLayerImplFixedBounds won't change target
163 // geometries. Complex case: complex transforms and non-zero anchor point.
164 TEST(WebLayerImplFixedBoundsTest
, MAYBE_CompareToWebLayerImplComplex
) {
165 gfx::Transform transform
;
166 // These are arbitrary values that should not affect the results.
167 transform
.Translate3d(50, 60, 70);
168 transform
.Scale3d(2, 3, 4);
169 transform
.RotateAbout(gfx::Vector3dF(33, 44, 55), 99);
171 CompareFixedBoundsLayerAndNormalLayer(WebFloatPoint(0, 0), transform
);
173 // With non-zero anchor point, WebLayerImplFixedBounds will fall back to
175 CompareFixedBoundsLayerAndNormalLayer(WebFloatPoint(0.4f
, 0.6f
), transform
);
179 } // namespace cc_blink