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