Attempt to fix expansion of the goma directory in MB on windows.
[chromium-blink-merge.git] / cc / trees / layer_tree_host_impl_unittest.cc
blobf3f494a62f8331169a6465eaebb240b7941b6426
1 // Copyright 2011 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/trees/layer_tree_host_impl.h"
7 #include <algorithm>
8 #include <cmath>
10 #include "base/bind.h"
11 #include "base/command_line.h"
12 #include "base/containers/hash_tables.h"
13 #include "base/containers/scoped_ptr_hash_map.h"
14 #include "base/location.h"
15 #include "base/thread_task_runner_handle.h"
16 #include "cc/animation/scrollbar_animation_controller_thinning.h"
17 #include "cc/animation/transform_operations.h"
18 #include "cc/base/math_util.h"
19 #include "cc/input/page_scale_animation.h"
20 #include "cc/input/top_controls_manager.h"
21 #include "cc/layers/append_quads_data.h"
22 #include "cc/layers/delegated_renderer_layer_impl.h"
23 #include "cc/layers/heads_up_display_layer_impl.h"
24 #include "cc/layers/io_surface_layer_impl.h"
25 #include "cc/layers/layer_impl.h"
26 #include "cc/layers/painted_scrollbar_layer_impl.h"
27 #include "cc/layers/render_surface_impl.h"
28 #include "cc/layers/solid_color_layer_impl.h"
29 #include "cc/layers/solid_color_scrollbar_layer_impl.h"
30 #include "cc/layers/texture_layer_impl.h"
31 #include "cc/layers/video_layer_impl.h"
32 #include "cc/layers/viewport.h"
33 #include "cc/output/begin_frame_args.h"
34 #include "cc/output/compositor_frame_ack.h"
35 #include "cc/output/compositor_frame_metadata.h"
36 #include "cc/output/copy_output_request.h"
37 #include "cc/output/copy_output_result.h"
38 #include "cc/output/gl_renderer.h"
39 #include "cc/output/latency_info_swap_promise.h"
40 #include "cc/quads/render_pass_draw_quad.h"
41 #include "cc/quads/solid_color_draw_quad.h"
42 #include "cc/quads/texture_draw_quad.h"
43 #include "cc/quads/tile_draw_quad.h"
44 #include "cc/test/animation_test_common.h"
45 #include "cc/test/begin_frame_args_test.h"
46 #include "cc/test/fake_layer_tree_host_impl.h"
47 #include "cc/test/fake_output_surface.h"
48 #include "cc/test/fake_output_surface_client.h"
49 #include "cc/test/fake_picture_layer_impl.h"
50 #include "cc/test/fake_picture_pile_impl.h"
51 #include "cc/test/fake_proxy.h"
52 #include "cc/test/fake_video_frame_provider.h"
53 #include "cc/test/geometry_test_utils.h"
54 #include "cc/test/gpu_rasterization_enabled_settings.h"
55 #include "cc/test/layer_test_common.h"
56 #include "cc/test/layer_tree_test.h"
57 #include "cc/test/test_gpu_memory_buffer_manager.h"
58 #include "cc/test/test_shared_bitmap_manager.h"
59 #include "cc/test/test_task_graph_runner.h"
60 #include "cc/test/test_web_graphics_context_3d.h"
61 #include "cc/trees/layer_tree_impl.h"
62 #include "cc/trees/single_thread_proxy.h"
63 #include "media/base/media.h"
64 #include "testing/gmock/include/gmock/gmock.h"
65 #include "testing/gtest/include/gtest/gtest.h"
66 #include "third_party/skia/include/core/SkMallocPixelRef.h"
67 #include "ui/gfx/geometry/rect_conversions.h"
68 #include "ui/gfx/geometry/size_conversions.h"
69 #include "ui/gfx/geometry/vector2d_conversions.h"
71 using ::testing::Mock;
72 using ::testing::Return;
73 using ::testing::AnyNumber;
74 using ::testing::AtLeast;
75 using ::testing::_;
76 using media::VideoFrame;
78 namespace cc {
79 namespace {
81 class LayerTreeHostImplTest : public testing::Test,
82 public LayerTreeHostImplClient {
83 public:
84 LayerTreeHostImplTest()
85 : proxy_(base::ThreadTaskRunnerHandle::Get(),
86 base::ThreadTaskRunnerHandle::Get()),
87 always_impl_thread_(&proxy_),
88 always_main_thread_blocked_(&proxy_),
89 on_can_draw_state_changed_called_(false),
90 did_notify_ready_to_activate_(false),
91 did_request_commit_(false),
92 did_request_redraw_(false),
93 did_request_animate_(false),
94 did_request_prepare_tiles_(false),
95 did_complete_page_scale_animation_(false),
96 reduce_memory_result_(true) {
97 media::InitializeMediaLibrary();
100 LayerTreeSettings DefaultSettings() {
101 LayerTreeSettings settings;
102 settings.minimum_occlusion_tracking_size = gfx::Size();
103 settings.renderer_settings.texture_id_allocation_chunk_size = 1;
104 settings.gpu_rasterization_enabled = true;
105 return settings;
108 void SetUp() override {
109 CreateHostImpl(DefaultSettings(), CreateOutputSurface());
112 void TearDown() override {}
114 void UpdateRendererCapabilitiesOnImplThread() override {}
115 void DidLoseOutputSurfaceOnImplThread() override {}
116 void CommitVSyncParameters(base::TimeTicks timebase,
117 base::TimeDelta interval) override {}
118 void SetEstimatedParentDrawTime(base::TimeDelta draw_time) override {}
119 void SetMaxSwapsPendingOnImplThread(int max) override {}
120 void DidSwapBuffersOnImplThread() override {}
121 void DidSwapBuffersCompleteOnImplThread() override {}
122 void OnCanDrawStateChanged(bool can_draw) override {
123 on_can_draw_state_changed_called_ = true;
125 void NotifyReadyToActivate() override {
126 did_notify_ready_to_activate_ = true;
127 host_impl_->ActivateSyncTree();
129 void NotifyReadyToDraw() override {}
130 void SetNeedsRedrawOnImplThread() override { did_request_redraw_ = true; }
131 void SetNeedsRedrawRectOnImplThread(const gfx::Rect& damage_rect) override {
132 did_request_redraw_ = true;
134 void SetNeedsAnimateOnImplThread() override { did_request_animate_ = true; }
135 void SetNeedsPrepareTilesOnImplThread() override {
136 did_request_prepare_tiles_ = true;
138 void SetNeedsCommitOnImplThread() override { did_request_commit_ = true; }
139 void SetVideoNeedsBeginFrames(bool needs_begin_frames) override {}
140 void PostAnimationEventsToMainThreadOnImplThread(
141 scoped_ptr<AnimationEventsVector> events) override {}
142 bool IsInsideDraw() override { return false; }
143 void RenewTreePriority() override {}
144 void PostDelayedAnimationTaskOnImplThread(const base::Closure& task,
145 base::TimeDelta delay) override {
146 animation_task_ = task;
147 requested_animation_delay_ = delay;
149 void DidActivateSyncTree() override {}
150 void WillPrepareTiles() override {}
151 void DidPrepareTiles() override {}
152 void DidCompletePageScaleAnimationOnImplThread() override {
153 did_complete_page_scale_animation_ = true;
155 void OnDrawForOutputSurface() override {}
156 void PostFrameTimingEventsOnImplThread(
157 scoped_ptr<FrameTimingTracker::CompositeTimingSet> composite_events,
158 scoped_ptr<FrameTimingTracker::MainFrameTimingSet> main_frame_events)
159 override {}
161 void set_reduce_memory_result(bool reduce_memory_result) {
162 reduce_memory_result_ = reduce_memory_result;
165 virtual bool CreateHostImpl(const LayerTreeSettings& settings,
166 scoped_ptr<OutputSurface> output_surface) {
167 host_impl_ = LayerTreeHostImpl::Create(
168 settings, this, &proxy_, &stats_instrumentation_,
169 &shared_bitmap_manager_, &gpu_memory_buffer_manager_,
170 &task_graph_runner_, 0);
171 bool init = host_impl_->InitializeRenderer(output_surface.Pass());
172 host_impl_->SetViewportSize(gfx::Size(10, 10));
173 host_impl_->active_tree()->PushPageScaleFromMainThread(1.f, 1.f, 1.f);
174 // Set the BeginFrameArgs so that methods which use it are able to.
175 host_impl_->WillBeginImplFrame(CreateBeginFrameArgsForTesting(
176 BEGINFRAME_FROM_HERE,
177 base::TimeTicks() + base::TimeDelta::FromMilliseconds(1)));
178 host_impl_->DidFinishImplFrame();
179 return init;
182 void SetupRootLayerImpl(scoped_ptr<LayerImpl> root) {
183 root->SetPosition(gfx::PointF());
184 root->SetBounds(gfx::Size(10, 10));
185 root->SetDrawsContent(true);
186 root->draw_properties().visible_layer_rect = gfx::Rect(0, 0, 10, 10);
187 root->SetHasRenderSurface(true);
188 host_impl_->active_tree()->SetRootLayer(root.Pass());
191 static void ExpectClearedScrollDeltasRecursive(LayerImpl* layer) {
192 ASSERT_EQ(layer->ScrollDelta(), gfx::Vector2d());
193 for (size_t i = 0; i < layer->children().size(); ++i)
194 ExpectClearedScrollDeltasRecursive(layer->children()[i]);
197 static ::testing::AssertionResult ScrollInfoContains(
198 const ScrollAndScaleSet& scroll_info,
199 int id,
200 const gfx::Vector2d& scroll_delta) {
201 int times_encountered = 0;
203 for (size_t i = 0; i < scroll_info.scrolls.size(); ++i) {
204 if (scroll_info.scrolls[i].layer_id != id)
205 continue;
207 if (scroll_delta != scroll_info.scrolls[i].scroll_delta) {
208 return ::testing::AssertionFailure()
209 << "Expected " << scroll_delta.ToString() << ", not "
210 << scroll_info.scrolls[i].scroll_delta.ToString();
212 times_encountered++;
215 if (times_encountered != 1)
216 return ::testing::AssertionFailure() << "No layer found with id " << id;
217 return ::testing::AssertionSuccess();
220 static void ExpectNone(const ScrollAndScaleSet& scroll_info, int id) {
221 int times_encountered = 0;
223 for (size_t i = 0; i < scroll_info.scrolls.size(); ++i) {
224 if (scroll_info.scrolls[i].layer_id != id)
225 continue;
226 times_encountered++;
229 ASSERT_EQ(0, times_encountered);
232 LayerImpl* CreateScrollAndContentsLayers(LayerTreeImpl* layer_tree_impl,
233 const gfx::Size& content_size) {
234 // Create both an inner viewport scroll layer and an outer viewport scroll
235 // layer. The MaxScrollOffset of the outer viewport scroll layer will be
236 // 0x0, so the scrolls will be applied directly to the inner viewport.
237 const int kOuterViewportClipLayerId = 116;
238 const int kOuterViewportScrollLayerId = 117;
239 const int kContentLayerId = 118;
240 const int kInnerViewportScrollLayerId = 2;
241 const int kInnerViewportClipLayerId = 4;
242 const int kPageScaleLayerId = 5;
244 scoped_ptr<LayerImpl> root =
245 LayerImpl::Create(layer_tree_impl, 1);
246 root->SetBounds(content_size);
247 root->SetPosition(gfx::PointF());
248 root->SetHasRenderSurface(true);
250 scoped_ptr<LayerImpl> inner_scroll =
251 LayerImpl::Create(layer_tree_impl, kInnerViewportScrollLayerId);
252 inner_scroll->SetIsContainerForFixedPositionLayers(true);
253 inner_scroll->PushScrollOffsetFromMainThread(gfx::ScrollOffset());
255 scoped_ptr<LayerImpl> inner_clip =
256 LayerImpl::Create(layer_tree_impl, kInnerViewportClipLayerId);
257 inner_clip->SetBounds(
258 gfx::Size(content_size.width() / 2, content_size.height() / 2));
260 scoped_ptr<LayerImpl> page_scale =
261 LayerImpl::Create(layer_tree_impl, kPageScaleLayerId);
263 inner_scroll->SetScrollClipLayer(inner_clip->id());
264 inner_scroll->SetBounds(content_size);
265 inner_scroll->SetPosition(gfx::PointF());
267 scoped_ptr<LayerImpl> outer_clip =
268 LayerImpl::Create(layer_tree_impl, kOuterViewportClipLayerId);
269 outer_clip->SetBounds(content_size);
270 outer_clip->SetIsContainerForFixedPositionLayers(true);
272 scoped_ptr<LayerImpl> outer_scroll =
273 LayerImpl::Create(layer_tree_impl, kOuterViewportScrollLayerId);
274 outer_scroll->SetScrollClipLayer(outer_clip->id());
275 outer_scroll->PushScrollOffsetFromMainThread(gfx::ScrollOffset());
276 outer_scroll->SetBounds(content_size);
277 outer_scroll->SetPosition(gfx::PointF());
279 scoped_ptr<LayerImpl> contents =
280 LayerImpl::Create(layer_tree_impl, kContentLayerId);
281 contents->SetDrawsContent(true);
282 contents->SetBounds(content_size);
283 contents->SetPosition(gfx::PointF());
285 outer_scroll->AddChild(contents.Pass());
286 outer_clip->AddChild(outer_scroll.Pass());
287 inner_scroll->AddChild(outer_clip.Pass());
288 page_scale->AddChild(inner_scroll.Pass());
289 inner_clip->AddChild(page_scale.Pass());
290 root->AddChild(inner_clip.Pass());
292 layer_tree_impl->SetRootLayer(root.Pass());
293 layer_tree_impl->SetViewportLayersFromIds(
294 Layer::INVALID_ID, kPageScaleLayerId, kInnerViewportScrollLayerId,
295 kOuterViewportScrollLayerId);
297 layer_tree_impl->DidBecomeActive();
298 return layer_tree_impl->InnerViewportScrollLayer();
301 LayerImpl* SetupScrollAndContentsLayers(const gfx::Size& content_size) {
302 LayerImpl* scroll_layer = CreateScrollAndContentsLayers(
303 host_impl_->active_tree(), content_size);
304 host_impl_->active_tree()->DidBecomeActive();
305 return scroll_layer;
308 // Sets up a typical virtual viewport setup with one child content layer.
309 // Returns a pointer to the content layer.
310 LayerImpl* CreateBasicVirtualViewportLayers(const gfx::Size& viewport_size,
311 const gfx::Size& content_size) {
312 // CreateScrollAndContentsLayers makes the outer viewport unscrollable and
313 // the inner a different size from the outer. We'll reuse its layer
314 // hierarchy but adjust the sizing to our needs.
315 CreateScrollAndContentsLayers(host_impl_->active_tree(), content_size);
317 LayerImpl* content_layer =
318 host_impl_->OuterViewportScrollLayer()->children().back();
319 content_layer->SetBounds(content_size);
320 host_impl_->OuterViewportScrollLayer()->SetBounds(content_size);
322 LayerImpl* outer_clip = host_impl_->OuterViewportScrollLayer()->parent();
323 outer_clip->SetBounds(viewport_size);
325 LayerImpl* inner_clip_layer =
326 host_impl_->InnerViewportScrollLayer()->parent()->parent();
327 inner_clip_layer->SetBounds(viewport_size);
328 host_impl_->InnerViewportScrollLayer()->SetBounds(viewport_size);
330 host_impl_->SetViewportSize(viewport_size);
331 host_impl_->active_tree()->DidBecomeActive();
333 return content_layer;
336 scoped_ptr<LayerImpl> CreateScrollableLayer(int id,
337 const gfx::Size& size,
338 LayerImpl* clip_layer) {
339 DCHECK(clip_layer);
340 DCHECK(id != clip_layer->id());
341 scoped_ptr<LayerImpl> layer =
342 LayerImpl::Create(host_impl_->active_tree(), id);
343 layer->SetScrollClipLayer(clip_layer->id());
344 layer->SetDrawsContent(true);
345 layer->SetBounds(size);
346 clip_layer->SetBounds(gfx::Size(size.width() / 2, size.height() / 2));
347 return layer.Pass();
350 void DrawFrame() {
351 LayerTreeHostImpl::FrameData frame;
352 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
353 host_impl_->DrawLayers(&frame);
354 host_impl_->DidDrawAllLayers(frame);
357 void pinch_zoom_pan_viewport_forces_commit_redraw(float device_scale_factor);
358 void pinch_zoom_pan_viewport_test(float device_scale_factor);
359 void pinch_zoom_pan_viewport_and_scroll_test(float device_scale_factor);
360 void pinch_zoom_pan_viewport_and_scroll_boundary_test(
361 float device_scale_factor);
363 void CheckNotifyCalledIfCanDrawChanged(bool always_draw) {
364 // Note: It is not possible to disable the renderer once it has been set,
365 // so we do not need to test that disabling the renderer notifies us
366 // that can_draw changed.
367 EXPECT_FALSE(host_impl_->CanDraw());
368 on_can_draw_state_changed_called_ = false;
370 // Set up the root layer, which allows us to draw.
371 SetupScrollAndContentsLayers(gfx::Size(100, 100));
372 EXPECT_TRUE(host_impl_->CanDraw());
373 EXPECT_TRUE(on_can_draw_state_changed_called_);
374 on_can_draw_state_changed_called_ = false;
376 // Toggle the root layer to make sure it toggles can_draw
377 host_impl_->active_tree()->SetRootLayer(nullptr);
378 EXPECT_FALSE(host_impl_->CanDraw());
379 EXPECT_TRUE(on_can_draw_state_changed_called_);
380 on_can_draw_state_changed_called_ = false;
382 SetupScrollAndContentsLayers(gfx::Size(100, 100));
383 EXPECT_TRUE(host_impl_->CanDraw());
384 EXPECT_TRUE(on_can_draw_state_changed_called_);
385 on_can_draw_state_changed_called_ = false;
387 // Toggle the device viewport size to make sure it toggles can_draw.
388 host_impl_->SetViewportSize(gfx::Size());
389 if (always_draw) {
390 EXPECT_TRUE(host_impl_->CanDraw());
391 } else {
392 EXPECT_FALSE(host_impl_->CanDraw());
394 EXPECT_TRUE(on_can_draw_state_changed_called_);
395 on_can_draw_state_changed_called_ = false;
397 host_impl_->SetViewportSize(gfx::Size(100, 100));
398 EXPECT_TRUE(host_impl_->CanDraw());
399 EXPECT_TRUE(on_can_draw_state_changed_called_);
400 on_can_draw_state_changed_called_ = false;
403 void SetupMouseMoveAtWithDeviceScale(float device_scale_factor);
405 protected:
406 virtual scoped_ptr<OutputSurface> CreateOutputSurface() {
407 return FakeOutputSurface::Create3d();
410 void DrawOneFrame() {
411 LayerTreeHostImpl::FrameData frame_data;
412 host_impl_->PrepareToDraw(&frame_data);
413 host_impl_->DidDrawAllLayers(frame_data);
416 FakeProxy proxy_;
417 DebugScopedSetImplThread always_impl_thread_;
418 DebugScopedSetMainThreadBlocked always_main_thread_blocked_;
420 TestSharedBitmapManager shared_bitmap_manager_;
421 TestGpuMemoryBufferManager gpu_memory_buffer_manager_;
422 TestTaskGraphRunner task_graph_runner_;
423 scoped_ptr<LayerTreeHostImpl> host_impl_;
424 FakeRenderingStatsInstrumentation stats_instrumentation_;
425 bool on_can_draw_state_changed_called_;
426 bool did_notify_ready_to_activate_;
427 bool did_request_commit_;
428 bool did_request_redraw_;
429 bool did_request_animate_;
430 bool did_request_prepare_tiles_;
431 bool did_complete_page_scale_animation_;
432 bool reduce_memory_result_;
433 base::Closure animation_task_;
434 base::TimeDelta requested_animation_delay_;
437 // A test fixture for new animation timelines tests.
438 class LayerTreeHostImplTimelinesTest : public LayerTreeHostImplTest {
439 public:
440 void SetUp() override {
441 LayerTreeSettings settings = DefaultSettings();
442 settings.use_compositor_animation_timelines = true;
443 CreateHostImpl(settings, CreateOutputSurface());
447 TEST_F(LayerTreeHostImplTest, NotifyIfCanDrawChanged) {
448 bool always_draw = false;
449 CheckNotifyCalledIfCanDrawChanged(always_draw);
452 TEST_F(LayerTreeHostImplTest, CanDrawIncompleteFrames) {
453 CreateHostImpl(DefaultSettings(),
454 FakeOutputSurface::CreateAlwaysDrawAndSwap3d());
456 bool always_draw = true;
457 CheckNotifyCalledIfCanDrawChanged(always_draw);
460 TEST_F(LayerTreeHostImplTest, ScrollDeltaNoLayers) {
461 ASSERT_FALSE(host_impl_->active_tree()->root_layer());
463 scoped_ptr<ScrollAndScaleSet> scroll_info = host_impl_->ProcessScrollDeltas();
464 ASSERT_EQ(scroll_info->scrolls.size(), 0u);
467 TEST_F(LayerTreeHostImplTest, ScrollDeltaTreeButNoChanges) {
469 scoped_ptr<LayerImpl> root =
470 LayerImpl::Create(host_impl_->active_tree(), 1);
471 root->AddChild(LayerImpl::Create(host_impl_->active_tree(), 2));
472 root->AddChild(LayerImpl::Create(host_impl_->active_tree(), 3));
473 root->children()[1]->AddChild(
474 LayerImpl::Create(host_impl_->active_tree(), 4));
475 root->children()[1]->AddChild(
476 LayerImpl::Create(host_impl_->active_tree(), 5));
477 root->children()[1]->children()[0]->AddChild(
478 LayerImpl::Create(host_impl_->active_tree(), 6));
479 host_impl_->active_tree()->SetRootLayer(root.Pass());
481 LayerImpl* root = host_impl_->active_tree()->root_layer();
483 ExpectClearedScrollDeltasRecursive(root);
485 scoped_ptr<ScrollAndScaleSet> scroll_info;
487 scroll_info = host_impl_->ProcessScrollDeltas();
488 ASSERT_EQ(scroll_info->scrolls.size(), 0u);
489 ExpectClearedScrollDeltasRecursive(root);
491 scroll_info = host_impl_->ProcessScrollDeltas();
492 ASSERT_EQ(scroll_info->scrolls.size(), 0u);
493 ExpectClearedScrollDeltasRecursive(root);
496 TEST_F(LayerTreeHostImplTest, ScrollDeltaRepeatedScrolls) {
497 gfx::ScrollOffset scroll_offset(20, 30);
498 gfx::Vector2d scroll_delta(11, -15);
500 scoped_ptr<LayerImpl> root_clip =
501 LayerImpl::Create(host_impl_->active_tree(), 2);
502 scoped_ptr<LayerImpl> root =
503 LayerImpl::Create(host_impl_->active_tree(), 1);
504 root_clip->SetBounds(gfx::Size(10, 10));
505 LayerImpl* root_layer = root.get();
506 root_clip->AddChild(root.Pass());
507 root_layer->SetBounds(gfx::Size(110, 110));
508 root_layer->SetScrollClipLayer(root_clip->id());
509 root_layer->PushScrollOffsetFromMainThread(scroll_offset);
510 root_layer->ScrollBy(scroll_delta);
511 host_impl_->active_tree()->SetRootLayer(root_clip.Pass());
513 LayerImpl* root = host_impl_->active_tree()->root_layer()->children()[0];
515 scoped_ptr<ScrollAndScaleSet> scroll_info;
517 scroll_info = host_impl_->ProcessScrollDeltas();
518 ASSERT_EQ(scroll_info->scrolls.size(), 1u);
519 EXPECT_TRUE(ScrollInfoContains(*scroll_info, root->id(), scroll_delta));
521 gfx::Vector2d scroll_delta2(-5, 27);
522 root->ScrollBy(scroll_delta2);
523 scroll_info = host_impl_->ProcessScrollDeltas();
524 ASSERT_EQ(scroll_info->scrolls.size(), 1u);
525 EXPECT_TRUE(ScrollInfoContains(*scroll_info, root->id(),
526 scroll_delta + scroll_delta2));
528 root->ScrollBy(gfx::Vector2d());
529 scroll_info = host_impl_->ProcessScrollDeltas();
530 EXPECT_TRUE(ScrollInfoContains(*scroll_info, root->id(),
531 scroll_delta + scroll_delta2));
534 TEST_F(LayerTreeHostImplTest, ScrollRootCallsCommitAndRedraw) {
535 SetupScrollAndContentsLayers(gfx::Size(100, 100));
536 host_impl_->SetViewportSize(gfx::Size(50, 50));
537 DrawFrame();
539 EXPECT_EQ(InputHandler::SCROLL_STARTED,
540 host_impl_->ScrollBegin(gfx::Point(), InputHandler::WHEEL));
541 EXPECT_TRUE(host_impl_->IsCurrentlyScrollingLayerAt(gfx::Point(),
542 InputHandler::WHEEL));
543 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(0, 10));
544 EXPECT_TRUE(host_impl_->IsCurrentlyScrollingLayerAt(gfx::Point(0, 10),
545 InputHandler::WHEEL));
546 host_impl_->ScrollEnd();
547 EXPECT_FALSE(host_impl_->IsCurrentlyScrollingLayerAt(gfx::Point(),
548 InputHandler::WHEEL));
549 EXPECT_TRUE(did_request_redraw_);
550 EXPECT_TRUE(did_request_commit_);
553 TEST_F(LayerTreeHostImplTest, ScrollActiveOnlyAfterScrollMovement) {
554 SetupScrollAndContentsLayers(gfx::Size(100, 100));
555 host_impl_->SetViewportSize(gfx::Size(50, 50));
556 DrawFrame();
558 EXPECT_EQ(InputHandler::SCROLL_STARTED,
559 host_impl_->ScrollBegin(gfx::Point(), InputHandler::WHEEL));
560 EXPECT_FALSE(host_impl_->IsActivelyScrolling());
561 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(0, 10));
562 EXPECT_TRUE(host_impl_->IsActivelyScrolling());
563 host_impl_->ScrollEnd();
564 EXPECT_FALSE(host_impl_->IsActivelyScrolling());
567 TEST_F(LayerTreeHostImplTest, ScrollWithoutRootLayer) {
568 // We should not crash when trying to scroll an empty layer tree.
569 EXPECT_EQ(InputHandler::SCROLL_IGNORED,
570 host_impl_->ScrollBegin(gfx::Point(), InputHandler::WHEEL));
573 TEST_F(LayerTreeHostImplTest, ScrollWithoutRenderer) {
574 scoped_ptr<TestWebGraphicsContext3D> context_owned =
575 TestWebGraphicsContext3D::Create();
576 context_owned->set_context_lost(true);
578 // Initialization will fail.
579 EXPECT_FALSE(CreateHostImpl(
580 DefaultSettings(), FakeOutputSurface::Create3d(context_owned.Pass())));
582 SetupScrollAndContentsLayers(gfx::Size(100, 100));
584 // We should not crash when trying to scroll after the renderer initialization
585 // fails.
586 EXPECT_EQ(InputHandler::SCROLL_STARTED,
587 host_impl_->ScrollBegin(gfx::Point(), InputHandler::WHEEL));
590 TEST_F(LayerTreeHostImplTest, ReplaceTreeWhileScrolling) {
591 LayerImpl* scroll_layer = SetupScrollAndContentsLayers(gfx::Size(100, 100));
592 host_impl_->SetViewportSize(gfx::Size(50, 50));
593 DrawFrame();
595 // We should not crash if the tree is replaced while we are scrolling.
596 EXPECT_EQ(InputHandler::SCROLL_STARTED,
597 host_impl_->ScrollBegin(gfx::Point(), InputHandler::WHEEL));
598 host_impl_->active_tree()->DetachLayerTree();
600 scroll_layer = SetupScrollAndContentsLayers(gfx::Size(100, 100));
602 // We should still be scrolling, because the scrolled layer also exists in the
603 // new tree.
604 gfx::Vector2d scroll_delta(0, 10);
605 host_impl_->ScrollBy(gfx::Point(), scroll_delta);
606 host_impl_->ScrollEnd();
607 scoped_ptr<ScrollAndScaleSet> scroll_info = host_impl_->ProcessScrollDeltas();
608 EXPECT_TRUE(
609 ScrollInfoContains(*scroll_info, scroll_layer->id(), scroll_delta));
612 TEST_F(LayerTreeHostImplTest, ScrollBlocksOnWheelEventHandlers) {
613 SetupScrollAndContentsLayers(gfx::Size(100, 100));
614 host_impl_->SetViewportSize(gfx::Size(50, 50));
615 DrawFrame();
616 LayerImpl* root = host_impl_->active_tree()->root_layer();
618 // With registered event handlers, wheel scrolls don't necessarily
619 // have to go to the main thread.
620 root->SetHaveWheelEventHandlers(true);
621 EXPECT_EQ(InputHandler::SCROLL_STARTED,
622 host_impl_->ScrollBegin(gfx::Point(), InputHandler::WHEEL));
623 host_impl_->ScrollEnd();
625 // But typically the scroll-blocks-on mode will require them to.
626 root->SetScrollBlocksOn(SCROLL_BLOCKS_ON_WHEEL_EVENT |
627 SCROLL_BLOCKS_ON_START_TOUCH);
628 EXPECT_EQ(InputHandler::SCROLL_ON_MAIN_THREAD,
629 host_impl_->ScrollBegin(gfx::Point(), InputHandler::WHEEL));
631 // But gesture scrolls can still be handled.
632 EXPECT_EQ(InputHandler::SCROLL_STARTED,
633 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
634 host_impl_->ScrollEnd();
636 // And if the handlers go away, wheel scrolls can again be processed
637 // on impl (despite the scroll-blocks-on mode).
638 root->SetHaveWheelEventHandlers(false);
639 EXPECT_EQ(InputHandler::SCROLL_STARTED,
640 host_impl_->ScrollBegin(gfx::Point(), InputHandler::WHEEL));
641 host_impl_->ScrollEnd();
644 TEST_F(LayerTreeHostImplTest, ScrollBlocksOnTouchEventHandlers) {
645 LayerImpl* scroll = SetupScrollAndContentsLayers(gfx::Size(100, 100));
646 host_impl_->SetViewportSize(gfx::Size(50, 50));
647 DrawFrame();
648 LayerImpl* root = host_impl_->active_tree()->root_layer();
650 LayerImpl* child = 0;
652 scoped_ptr<LayerImpl> child_layer =
653 LayerImpl::Create(host_impl_->active_tree(), 6);
654 child = child_layer.get();
655 child_layer->SetDrawsContent(true);
656 child_layer->SetPosition(gfx::PointF(0, 20));
657 child_layer->SetBounds(gfx::Size(50, 50));
658 scroll->AddChild(child_layer.Pass());
661 // Touch handler regions determine whether touch events block scroll.
662 root->SetTouchEventHandlerRegion(gfx::Rect(0, 0, 100, 100));
663 EXPECT_FALSE(host_impl_->DoTouchEventsBlockScrollAt(gfx::Point(10, 10)));
664 root->SetScrollBlocksOn(SCROLL_BLOCKS_ON_START_TOUCH |
665 SCROLL_BLOCKS_ON_WHEEL_EVENT);
666 EXPECT_TRUE(host_impl_->DoTouchEventsBlockScrollAt(gfx::Point(10, 10)));
668 // But they don't influence the actual handling of the scroll gestures.
669 EXPECT_EQ(InputHandler::SCROLL_STARTED,
670 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
671 host_impl_->ScrollEnd();
673 // It's the union of scroll-blocks-on mode bits across all layers in the
674 // scroll paret chain that matters.
675 EXPECT_TRUE(host_impl_->DoTouchEventsBlockScrollAt(gfx::Point(10, 30)));
676 root->SetScrollBlocksOn(SCROLL_BLOCKS_ON_NONE);
677 EXPECT_FALSE(host_impl_->DoTouchEventsBlockScrollAt(gfx::Point(10, 30)));
678 child->SetScrollBlocksOn(SCROLL_BLOCKS_ON_START_TOUCH);
679 EXPECT_TRUE(host_impl_->DoTouchEventsBlockScrollAt(gfx::Point(10, 30)));
682 TEST_F(LayerTreeHostImplTest, ScrollBlocksOnScrollEventHandlers) {
683 SetupScrollAndContentsLayers(gfx::Size(100, 100));
684 host_impl_->SetViewportSize(gfx::Size(50, 50));
685 DrawFrame();
686 LayerImpl* root = host_impl_->active_tree()->root_layer();
688 // With registered scroll handlers, scrolls don't generally have to go
689 // to the main thread.
690 root->SetHaveScrollEventHandlers(true);
691 EXPECT_EQ(InputHandler::SCROLL_STARTED,
692 host_impl_->ScrollBegin(gfx::Point(), InputHandler::WHEEL));
693 host_impl_->ScrollEnd();
695 // Even the default scroll blocks on mode doesn't require this.
696 root->SetScrollBlocksOn(SCROLL_BLOCKS_ON_WHEEL_EVENT |
697 SCROLL_BLOCKS_ON_START_TOUCH);
698 EXPECT_EQ(InputHandler::SCROLL_STARTED,
699 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
700 host_impl_->ScrollEnd();
702 // But the page can opt in to blocking on scroll event handlers.
703 root->SetScrollBlocksOn(SCROLL_BLOCKS_ON_SCROLL_EVENT);
704 EXPECT_EQ(InputHandler::SCROLL_ON_MAIN_THREAD,
705 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
707 // GESTURE and WHEEL scrolls behave identically in this regard.
708 EXPECT_EQ(InputHandler::SCROLL_ON_MAIN_THREAD,
709 host_impl_->ScrollBegin(gfx::Point(), InputHandler::WHEEL));
711 // And if the handlers go away, scrolls can again be processed on impl
712 // (despite the scroll-blocks-on mode).
713 root->SetHaveScrollEventHandlers(false);
714 EXPECT_EQ(InputHandler::SCROLL_STARTED,
715 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
716 host_impl_->ScrollEnd();
719 TEST_F(LayerTreeHostImplTest, ScrollBlocksOnLayerTopology) {
720 host_impl_->SetViewportSize(gfx::Size(50, 50));
722 // Create a normal scrollable root layer
723 LayerImpl* root_scroll = SetupScrollAndContentsLayers(gfx::Size(100, 100));
724 LayerImpl* root_child = root_scroll->children()[0];
725 LayerImpl* root = host_impl_->active_tree()->root_layer();
726 DrawFrame();
728 // Create two child scrollable layers
729 LayerImpl* child1 = 0;
731 scoped_ptr<LayerImpl> scrollable_child_clip_1 =
732 LayerImpl::Create(host_impl_->active_tree(), 6);
733 scoped_ptr<LayerImpl> scrollable_child_1 = CreateScrollableLayer(
734 7, gfx::Size(10, 10), scrollable_child_clip_1.get());
735 child1 = scrollable_child_1.get();
736 scrollable_child_1->SetPosition(gfx::Point(5, 5));
737 scrollable_child_1->SetHaveWheelEventHandlers(true);
738 scrollable_child_1->SetHaveScrollEventHandlers(true);
739 scrollable_child_clip_1->AddChild(scrollable_child_1.Pass());
740 root_child->AddChild(scrollable_child_clip_1.Pass());
743 LayerImpl* child2 = 0;
745 scoped_ptr<LayerImpl> scrollable_child_clip_2 =
746 LayerImpl::Create(host_impl_->active_tree(), 8);
747 scoped_ptr<LayerImpl> scrollable_child_2 = CreateScrollableLayer(
748 9, gfx::Size(10, 10), scrollable_child_clip_2.get());
749 child2 = scrollable_child_2.get();
750 scrollable_child_2->SetPosition(gfx::Point(5, 20));
751 scrollable_child_2->SetHaveWheelEventHandlers(true);
752 scrollable_child_2->SetHaveScrollEventHandlers(true);
753 scrollable_child_clip_2->AddChild(scrollable_child_2.Pass());
754 root_child->AddChild(scrollable_child_clip_2.Pass());
757 // Scroll-blocks-on on a layer affects scrolls that hit that layer.
758 EXPECT_EQ(InputHandler::SCROLL_STARTED,
759 host_impl_->ScrollBegin(gfx::Point(10, 10), InputHandler::GESTURE));
760 host_impl_->ScrollEnd();
761 child1->SetScrollBlocksOn(SCROLL_BLOCKS_ON_SCROLL_EVENT);
762 EXPECT_EQ(InputHandler::SCROLL_ON_MAIN_THREAD,
763 host_impl_->ScrollBegin(gfx::Point(10, 10), InputHandler::GESTURE));
765 // But not those that hit only other layers.
766 EXPECT_EQ(InputHandler::SCROLL_STARTED,
767 host_impl_->ScrollBegin(gfx::Point(10, 25), InputHandler::GESTURE));
768 host_impl_->ScrollEnd();
770 // It's the union of bits set across the scroll ancestor chain that matters.
771 EXPECT_EQ(InputHandler::SCROLL_STARTED,
772 host_impl_->ScrollBegin(gfx::Point(10, 25), InputHandler::GESTURE));
773 host_impl_->ScrollEnd();
774 EXPECT_EQ(InputHandler::SCROLL_STARTED,
775 host_impl_->ScrollBegin(gfx::Point(10, 25), InputHandler::WHEEL));
776 host_impl_->ScrollEnd();
777 root->SetScrollBlocksOn(SCROLL_BLOCKS_ON_WHEEL_EVENT);
778 EXPECT_EQ(InputHandler::SCROLL_STARTED,
779 host_impl_->ScrollBegin(gfx::Point(10, 25), InputHandler::GESTURE));
780 host_impl_->ScrollEnd();
781 EXPECT_EQ(InputHandler::SCROLL_ON_MAIN_THREAD,
782 host_impl_->ScrollBegin(gfx::Point(10, 25), InputHandler::WHEEL));
783 child2->SetScrollBlocksOn(SCROLL_BLOCKS_ON_SCROLL_EVENT);
784 EXPECT_EQ(InputHandler::SCROLL_ON_MAIN_THREAD,
785 host_impl_->ScrollBegin(gfx::Point(10, 25), InputHandler::WHEEL));
786 EXPECT_EQ(InputHandler::SCROLL_ON_MAIN_THREAD,
787 host_impl_->ScrollBegin(gfx::Point(10, 25), InputHandler::GESTURE));
790 TEST_F(LayerTreeHostImplTest, FlingOnlyWhenScrollingTouchscreen) {
791 SetupScrollAndContentsLayers(gfx::Size(100, 100));
792 host_impl_->SetViewportSize(gfx::Size(50, 50));
793 DrawFrame();
795 // Ignore the fling since no layer is being scrolled
796 EXPECT_EQ(InputHandler::SCROLL_IGNORED, host_impl_->FlingScrollBegin());
798 // Start scrolling a layer
799 EXPECT_EQ(InputHandler::SCROLL_STARTED,
800 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
802 // Now the fling should go ahead since we've started scrolling a layer
803 EXPECT_EQ(InputHandler::SCROLL_STARTED, host_impl_->FlingScrollBegin());
806 TEST_F(LayerTreeHostImplTest, FlingOnlyWhenScrollingTouchpad) {
807 SetupScrollAndContentsLayers(gfx::Size(100, 100));
808 host_impl_->SetViewportSize(gfx::Size(50, 50));
809 DrawFrame();
811 // Ignore the fling since no layer is being scrolled
812 EXPECT_EQ(InputHandler::SCROLL_IGNORED, host_impl_->FlingScrollBegin());
814 // Start scrolling a layer
815 EXPECT_EQ(InputHandler::SCROLL_STARTED,
816 host_impl_->ScrollBegin(gfx::Point(), InputHandler::WHEEL));
818 // Now the fling should go ahead since we've started scrolling a layer
819 EXPECT_EQ(InputHandler::SCROLL_STARTED, host_impl_->FlingScrollBegin());
822 TEST_F(LayerTreeHostImplTest, NoFlingWhenScrollingOnMain) {
823 SetupScrollAndContentsLayers(gfx::Size(100, 100));
824 host_impl_->SetViewportSize(gfx::Size(50, 50));
825 DrawFrame();
826 LayerImpl* root = host_impl_->active_tree()->root_layer();
828 root->SetShouldScrollOnMainThread(true);
830 // Start scrolling a layer
831 EXPECT_EQ(InputHandler::SCROLL_ON_MAIN_THREAD,
832 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
834 // The fling should be ignored since there's no layer being scrolled impl-side
835 EXPECT_EQ(InputHandler::SCROLL_IGNORED, host_impl_->FlingScrollBegin());
838 TEST_F(LayerTreeHostImplTest, ShouldScrollOnMainThread) {
839 SetupScrollAndContentsLayers(gfx::Size(100, 100));
840 host_impl_->SetViewportSize(gfx::Size(50, 50));
841 DrawFrame();
842 LayerImpl* root = host_impl_->active_tree()->root_layer();
844 root->SetShouldScrollOnMainThread(true);
846 EXPECT_EQ(InputHandler::SCROLL_ON_MAIN_THREAD,
847 host_impl_->ScrollBegin(gfx::Point(), InputHandler::WHEEL));
848 EXPECT_EQ(InputHandler::SCROLL_ON_MAIN_THREAD,
849 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
852 TEST_F(LayerTreeHostImplTest, NonFastScrollableRegionBasic) {
853 SetupScrollAndContentsLayers(gfx::Size(200, 200));
854 host_impl_->SetViewportSize(gfx::Size(100, 100));
856 LayerImpl* root = host_impl_->active_tree()->root_layer();
857 root->SetNonFastScrollableRegion(gfx::Rect(0, 0, 50, 50));
859 DrawFrame();
861 // All scroll types inside the non-fast scrollable region should fail.
862 EXPECT_EQ(InputHandler::SCROLL_ON_MAIN_THREAD,
863 host_impl_->ScrollBegin(gfx::Point(25, 25), InputHandler::WHEEL));
864 EXPECT_FALSE(host_impl_->IsCurrentlyScrollingLayerAt(gfx::Point(25, 25),
865 InputHandler::WHEEL));
866 EXPECT_EQ(InputHandler::SCROLL_ON_MAIN_THREAD,
867 host_impl_->ScrollBegin(gfx::Point(25, 25), InputHandler::GESTURE));
868 EXPECT_FALSE(host_impl_->IsCurrentlyScrollingLayerAt(gfx::Point(25, 25),
869 InputHandler::GESTURE));
871 // All scroll types outside this region should succeed.
872 EXPECT_EQ(InputHandler::SCROLL_STARTED,
873 host_impl_->ScrollBegin(gfx::Point(75, 75), InputHandler::WHEEL));
874 EXPECT_TRUE(host_impl_->IsCurrentlyScrollingLayerAt(gfx::Point(75, 75),
875 InputHandler::GESTURE));
876 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(0, 10));
877 EXPECT_FALSE(host_impl_->IsCurrentlyScrollingLayerAt(gfx::Point(25, 25),
878 InputHandler::GESTURE));
879 host_impl_->ScrollEnd();
880 EXPECT_FALSE(host_impl_->IsCurrentlyScrollingLayerAt(gfx::Point(75, 75),
881 InputHandler::GESTURE));
882 EXPECT_EQ(InputHandler::SCROLL_STARTED,
883 host_impl_->ScrollBegin(gfx::Point(75, 75), InputHandler::GESTURE));
884 EXPECT_TRUE(host_impl_->IsCurrentlyScrollingLayerAt(gfx::Point(75, 75),
885 InputHandler::GESTURE));
886 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(0, 10));
887 host_impl_->ScrollEnd();
888 EXPECT_FALSE(host_impl_->IsCurrentlyScrollingLayerAt(gfx::Point(75, 75),
889 InputHandler::GESTURE));
892 TEST_F(LayerTreeHostImplTest, NonFastScrollableRegionWithOffset) {
893 SetupScrollAndContentsLayers(gfx::Size(200, 200));
894 host_impl_->SetViewportSize(gfx::Size(100, 100));
896 LayerImpl* root = host_impl_->active_tree()->root_layer();
897 root->SetNonFastScrollableRegion(gfx::Rect(0, 0, 50, 50));
898 root->SetPosition(gfx::PointF(-25.f, 0.f));
900 DrawFrame();
902 // This point would fall into the non-fast scrollable region except that we've
903 // moved the layer down by 25 pixels.
904 EXPECT_EQ(InputHandler::SCROLL_STARTED,
905 host_impl_->ScrollBegin(gfx::Point(40, 10), InputHandler::WHEEL));
906 EXPECT_TRUE(host_impl_->IsCurrentlyScrollingLayerAt(gfx::Point(40, 10),
907 InputHandler::WHEEL));
908 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(0, 1));
909 host_impl_->ScrollEnd();
911 // This point is still inside the non-fast region.
912 EXPECT_EQ(InputHandler::SCROLL_ON_MAIN_THREAD,
913 host_impl_->ScrollBegin(gfx::Point(10, 10), InputHandler::WHEEL));
916 TEST_F(LayerTreeHostImplTest, ScrollHandlerNotPresent) {
917 LayerImpl* scroll_layer = SetupScrollAndContentsLayers(gfx::Size(200, 200));
918 EXPECT_FALSE(scroll_layer->have_scroll_event_handlers());
919 host_impl_->SetViewportSize(gfx::Size(50, 50));
920 DrawFrame();
922 EXPECT_FALSE(host_impl_->scroll_affects_scroll_handler());
923 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE);
924 EXPECT_FALSE(host_impl_->scroll_affects_scroll_handler());
925 host_impl_->ScrollEnd();
926 EXPECT_FALSE(host_impl_->scroll_affects_scroll_handler());
929 TEST_F(LayerTreeHostImplTest, ScrollHandlerPresent) {
930 LayerImpl* scroll_layer = SetupScrollAndContentsLayers(gfx::Size(200, 200));
931 scroll_layer->SetHaveScrollEventHandlers(true);
932 host_impl_->SetViewportSize(gfx::Size(50, 50));
933 DrawFrame();
935 EXPECT_FALSE(host_impl_->scroll_affects_scroll_handler());
936 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE);
937 EXPECT_TRUE(host_impl_->scroll_affects_scroll_handler());
938 host_impl_->ScrollEnd();
939 EXPECT_FALSE(host_impl_->scroll_affects_scroll_handler());
942 TEST_F(LayerTreeHostImplTest, ScrollByReturnsCorrectValue) {
943 SetupScrollAndContentsLayers(gfx::Size(200, 200));
944 host_impl_->SetViewportSize(gfx::Size(100, 100));
946 DrawFrame();
948 EXPECT_EQ(InputHandler::SCROLL_STARTED,
949 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
951 // Trying to scroll to the left/top will not succeed.
952 EXPECT_FALSE(
953 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(-10, 0)).did_scroll);
954 EXPECT_FALSE(
955 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(0, -10)).did_scroll);
956 EXPECT_FALSE(
957 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(-10, -10)).did_scroll);
959 // Scrolling to the right/bottom will succeed.
960 EXPECT_TRUE(
961 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(10, 0)).did_scroll);
962 EXPECT_TRUE(
963 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(0, 10)).did_scroll);
964 EXPECT_TRUE(
965 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(10, 10)).did_scroll);
967 // Scrolling to left/top will now succeed.
968 EXPECT_TRUE(
969 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(-10, 0)).did_scroll);
970 EXPECT_TRUE(
971 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(0, -10)).did_scroll);
972 EXPECT_TRUE(
973 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(-10, -10)).did_scroll);
975 // Scrolling diagonally against an edge will succeed.
976 EXPECT_TRUE(
977 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(10, -10)).did_scroll);
978 EXPECT_TRUE(
979 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(-10, 0)).did_scroll);
980 EXPECT_TRUE(
981 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(-10, 10)).did_scroll);
983 // Trying to scroll more than the available space will also succeed.
984 EXPECT_TRUE(
985 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(5000, 5000)).did_scroll);
988 TEST_F(LayerTreeHostImplTest, ScrollVerticallyByPageReturnsCorrectValue) {
989 SetupScrollAndContentsLayers(gfx::Size(200, 2000));
990 host_impl_->SetViewportSize(gfx::Size(100, 1000));
992 DrawFrame();
994 EXPECT_EQ(InputHandler::SCROLL_STARTED,
995 host_impl_->ScrollBegin(gfx::Point(), InputHandler::WHEEL));
997 // Trying to scroll without a vertical scrollbar will fail.
998 EXPECT_FALSE(host_impl_->ScrollVerticallyByPage(
999 gfx::Point(), SCROLL_FORWARD));
1000 EXPECT_FALSE(host_impl_->ScrollVerticallyByPage(
1001 gfx::Point(), SCROLL_BACKWARD));
1003 scoped_ptr<PaintedScrollbarLayerImpl> vertical_scrollbar(
1004 PaintedScrollbarLayerImpl::Create(
1005 host_impl_->active_tree(),
1007 VERTICAL));
1008 vertical_scrollbar->SetBounds(gfx::Size(15, 1000));
1009 host_impl_->InnerViewportScrollLayer()->AddScrollbar(
1010 vertical_scrollbar.get());
1012 // Trying to scroll with a vertical scrollbar will succeed.
1013 EXPECT_TRUE(host_impl_->ScrollVerticallyByPage(
1014 gfx::Point(), SCROLL_FORWARD));
1015 EXPECT_FLOAT_EQ(875.f,
1016 host_impl_->InnerViewportScrollLayer()->ScrollDelta().y());
1017 EXPECT_TRUE(host_impl_->ScrollVerticallyByPage(
1018 gfx::Point(), SCROLL_BACKWARD));
1021 TEST_F(LayerTreeHostImplTest, ScrollWithUserUnscrollableLayers) {
1022 LayerImpl* scroll_layer = SetupScrollAndContentsLayers(gfx::Size(200, 200));
1023 host_impl_->SetViewportSize(gfx::Size(100, 100));
1025 gfx::Size overflow_size(400, 400);
1026 ASSERT_EQ(1u, scroll_layer->children().size());
1027 LayerImpl* overflow = scroll_layer->children()[0];
1028 overflow->SetBounds(overflow_size);
1029 overflow->SetScrollClipLayer(scroll_layer->parent()->id());
1030 overflow->PushScrollOffsetFromMainThread(gfx::ScrollOffset());
1031 overflow->SetPosition(gfx::PointF());
1033 DrawFrame();
1034 gfx::Point scroll_position(10, 10);
1036 EXPECT_EQ(InputHandler::SCROLL_STARTED,
1037 host_impl_->ScrollBegin(scroll_position, InputHandler::WHEEL));
1038 EXPECT_VECTOR_EQ(gfx::Vector2dF(), scroll_layer->CurrentScrollOffset());
1039 EXPECT_VECTOR_EQ(gfx::Vector2dF(), overflow->CurrentScrollOffset());
1041 gfx::Vector2dF scroll_delta(10, 10);
1042 host_impl_->ScrollBy(scroll_position, scroll_delta);
1043 host_impl_->ScrollEnd();
1044 EXPECT_VECTOR_EQ(gfx::Vector2dF(), scroll_layer->CurrentScrollOffset());
1045 EXPECT_VECTOR_EQ(gfx::Vector2dF(10, 10), overflow->CurrentScrollOffset());
1047 overflow->set_user_scrollable_horizontal(false);
1049 EXPECT_EQ(InputHandler::SCROLL_STARTED,
1050 host_impl_->ScrollBegin(scroll_position, InputHandler::WHEEL));
1051 EXPECT_VECTOR_EQ(gfx::Vector2dF(), scroll_layer->CurrentScrollOffset());
1052 EXPECT_VECTOR_EQ(gfx::Vector2dF(10, 10), overflow->CurrentScrollOffset());
1054 host_impl_->ScrollBy(scroll_position, scroll_delta);
1055 host_impl_->ScrollEnd();
1056 EXPECT_VECTOR_EQ(gfx::Vector2dF(10, 0), scroll_layer->CurrentScrollOffset());
1057 EXPECT_VECTOR_EQ(gfx::Vector2dF(10, 20), overflow->CurrentScrollOffset());
1059 overflow->set_user_scrollable_vertical(false);
1061 EXPECT_EQ(InputHandler::SCROLL_STARTED,
1062 host_impl_->ScrollBegin(scroll_position, InputHandler::WHEEL));
1063 EXPECT_VECTOR_EQ(gfx::Vector2dF(10, 0), scroll_layer->CurrentScrollOffset());
1064 EXPECT_VECTOR_EQ(gfx::Vector2dF(10, 20), overflow->CurrentScrollOffset());
1066 host_impl_->ScrollBy(scroll_position, scroll_delta);
1067 host_impl_->ScrollEnd();
1068 EXPECT_VECTOR_EQ(gfx::Vector2dF(20, 10), scroll_layer->CurrentScrollOffset());
1069 EXPECT_VECTOR_EQ(gfx::Vector2dF(10, 20), overflow->CurrentScrollOffset());
1072 TEST_F(LayerTreeHostImplTest, AnimationSchedulingPendingTree) {
1073 host_impl_->SetViewportSize(gfx::Size(50, 50));
1075 host_impl_->CreatePendingTree();
1076 host_impl_->pending_tree()->SetRootLayer(
1077 LayerImpl::Create(host_impl_->pending_tree(), 1));
1078 LayerImpl* root = host_impl_->pending_tree()->root_layer();
1079 root->SetBounds(gfx::Size(50, 50));
1080 root->SetHasRenderSurface(true);
1082 root->AddChild(LayerImpl::Create(host_impl_->pending_tree(), 2));
1083 LayerImpl* child = root->children()[0];
1084 child->SetBounds(gfx::Size(10, 10));
1085 child->draw_properties().visible_layer_rect = gfx::Rect(10, 10);
1086 child->SetDrawsContent(true);
1087 AddAnimatedTransformToLayer(child, 10.0, 3, 0);
1089 EXPECT_FALSE(did_request_animate_);
1090 EXPECT_FALSE(did_request_redraw_);
1091 EXPECT_FALSE(did_request_commit_);
1093 host_impl_->Animate();
1095 // An animation exists on the pending layer. Doing Animate() requests another
1096 // frame.
1097 // In reality, animations without has_set_start_time() == true do not need to
1098 // be continuously ticked on the pending tree, so it should not request
1099 // another animation frame here. But we currently do so blindly if any
1100 // animation exists.
1101 EXPECT_TRUE(did_request_animate_);
1102 // The pending tree with an animation does not need to draw after animating.
1103 EXPECT_FALSE(did_request_redraw_);
1104 EXPECT_FALSE(did_request_commit_);
1106 did_request_animate_ = false;
1107 did_request_redraw_ = false;
1108 did_request_commit_ = false;
1110 host_impl_->ActivateSyncTree();
1112 // When the animation activates, we should request another animation frame
1113 // to keep the animation moving.
1114 EXPECT_TRUE(did_request_animate_);
1115 // On activation we don't need to request a redraw for the animation,
1116 // activating will draw on its own when it's ready.
1117 EXPECT_FALSE(did_request_redraw_);
1118 EXPECT_FALSE(did_request_commit_);
1121 TEST_F(LayerTreeHostImplTest, AnimationSchedulingActiveTree) {
1122 host_impl_->SetViewportSize(gfx::Size(50, 50));
1124 host_impl_->active_tree()->SetRootLayer(
1125 LayerImpl::Create(host_impl_->active_tree(), 1));
1126 LayerImpl* root = host_impl_->active_tree()->root_layer();
1127 root->SetBounds(gfx::Size(50, 50));
1128 root->SetHasRenderSurface(true);
1130 root->AddChild(LayerImpl::Create(host_impl_->active_tree(), 2));
1131 LayerImpl* child = root->children()[0];
1132 child->SetBounds(gfx::Size(10, 10));
1133 child->draw_properties().visible_layer_rect = gfx::Rect(10, 10);
1134 child->SetDrawsContent(true);
1136 // Add a translate from 6,7 to 8,9.
1137 TransformOperations start;
1138 start.AppendTranslate(6.f, 7.f, 0.f);
1139 TransformOperations end;
1140 end.AppendTranslate(8.f, 9.f, 0.f);
1141 AddAnimatedTransformToLayer(child, 4.0, start, end);
1143 base::TimeTicks now = base::TimeTicks::Now();
1144 host_impl_->WillBeginImplFrame(
1145 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, now));
1147 EXPECT_FALSE(did_request_animate_);
1148 EXPECT_FALSE(did_request_redraw_);
1149 EXPECT_FALSE(did_request_commit_);
1151 host_impl_->ActivateAnimations();
1152 did_request_animate_ = false;
1153 did_request_redraw_ = false;
1154 did_request_commit_ = false;
1156 host_impl_->Animate();
1158 // An animation exists on the active layer. Doing Animate() requests another
1159 // frame after the current one.
1160 EXPECT_TRUE(did_request_animate_);
1161 // TODO(danakj): We also need to draw in the current frame if something
1162 // animated, but this is currently handled by
1163 // SchedulerStateMachine::WillAnimate.
1164 EXPECT_FALSE(did_request_redraw_);
1165 EXPECT_FALSE(did_request_commit_);
1168 TEST_F(LayerTreeHostImplTest, ImplPinchZoom) {
1169 LayerImpl* scroll_layer = SetupScrollAndContentsLayers(gfx::Size(100, 100));
1170 host_impl_->SetViewportSize(gfx::Size(50, 50));
1171 DrawFrame();
1173 EXPECT_EQ(scroll_layer, host_impl_->InnerViewportScrollLayer());
1174 LayerImpl* container_layer = scroll_layer->scroll_clip_layer();
1175 EXPECT_EQ(gfx::Size(50, 50), container_layer->bounds());
1177 float min_page_scale = 1.f, max_page_scale = 4.f;
1178 float page_scale_factor = 1.f;
1180 // The impl-based pinch zoom should adjust the max scroll position.
1182 host_impl_->active_tree()->PushPageScaleFromMainThread(
1183 page_scale_factor, min_page_scale, max_page_scale);
1184 host_impl_->active_tree()->SetPageScaleOnActiveTree(page_scale_factor);
1185 scroll_layer->SetScrollDelta(gfx::Vector2d());
1187 float page_scale_delta = 2.f;
1189 host_impl_->ScrollBegin(gfx::Point(50, 50), InputHandler::GESTURE);
1190 host_impl_->PinchGestureBegin();
1191 host_impl_->PinchGestureUpdate(page_scale_delta, gfx::Point(50, 50));
1192 host_impl_->PinchGestureEnd();
1193 host_impl_->ScrollEnd();
1194 EXPECT_FALSE(did_request_animate_);
1195 EXPECT_TRUE(did_request_redraw_);
1196 EXPECT_TRUE(did_request_commit_);
1197 EXPECT_EQ(gfx::Size(50, 50), container_layer->bounds());
1199 scoped_ptr<ScrollAndScaleSet> scroll_info =
1200 host_impl_->ProcessScrollDeltas();
1201 EXPECT_EQ(scroll_info->page_scale_delta, page_scale_delta);
1203 EXPECT_EQ(gfx::ScrollOffset(75.0, 75.0).ToString(),
1204 scroll_layer->MaxScrollOffset().ToString());
1207 // Scrolling after a pinch gesture should always be in local space. The
1208 // scroll deltas have the page scale factor applied.
1210 host_impl_->active_tree()->PushPageScaleFromMainThread(
1211 page_scale_factor, min_page_scale, max_page_scale);
1212 host_impl_->active_tree()->SetPageScaleOnActiveTree(page_scale_factor);
1213 scroll_layer->SetScrollDelta(gfx::Vector2d());
1215 float page_scale_delta = 2.f;
1216 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE);
1217 host_impl_->PinchGestureBegin();
1218 host_impl_->PinchGestureUpdate(page_scale_delta, gfx::Point());
1219 host_impl_->PinchGestureEnd();
1220 host_impl_->ScrollEnd();
1222 gfx::Vector2d scroll_delta(0, 10);
1223 EXPECT_EQ(InputHandler::SCROLL_STARTED,
1224 host_impl_->ScrollBegin(gfx::Point(5, 5), InputHandler::WHEEL));
1225 host_impl_->ScrollBy(gfx::Point(), scroll_delta);
1226 host_impl_->ScrollEnd();
1228 scoped_ptr<ScrollAndScaleSet> scroll_info =
1229 host_impl_->ProcessScrollDeltas();
1230 EXPECT_TRUE(ScrollInfoContains(
1231 *scroll_info.get(), scroll_layer->id(),
1232 gfx::Vector2d(0, scroll_delta.y() / page_scale_delta)));
1236 TEST_F(LayerTreeHostImplTest, ViewportScrollOrder) {
1237 LayerTreeSettings settings = DefaultSettings();
1238 CreateHostImpl(settings,
1239 CreateOutputSurface());
1240 host_impl_->active_tree()->PushPageScaleFromMainThread(1.f, 0.25f, 4.f);
1242 const gfx::Size content_size(1000, 1000);
1243 const gfx::Size viewport_size(500, 500);
1244 CreateBasicVirtualViewportLayers(viewport_size, content_size);
1246 LayerImpl* outer_scroll_layer = host_impl_->OuterViewportScrollLayer();
1247 LayerImpl* inner_scroll_layer = host_impl_->InnerViewportScrollLayer();
1249 EXPECT_VECTOR_EQ(
1250 gfx::Vector2dF(500, 500),
1251 outer_scroll_layer->MaxScrollOffset());
1253 host_impl_->ScrollBegin(gfx::Point(250, 250), InputHandler::GESTURE);
1254 host_impl_->PinchGestureBegin();
1255 host_impl_->PinchGestureUpdate(2.f, gfx::Point(0, 0));
1256 host_impl_->PinchGestureEnd();
1257 host_impl_->ScrollEnd();
1259 // Sanity check - we're zoomed in, starting from the origin.
1260 EXPECT_VECTOR_EQ(
1261 gfx::Vector2dF(0, 0),
1262 outer_scroll_layer->CurrentScrollOffset());
1263 EXPECT_VECTOR_EQ(
1264 gfx::Vector2dF(0, 0),
1265 inner_scroll_layer->CurrentScrollOffset());
1267 // Scroll down - only the inner viewport should scroll.
1268 host_impl_->ScrollBegin(gfx::Point(0, 0), InputHandler::GESTURE);
1269 host_impl_->ScrollBy(gfx::Point(0, 0), gfx::Vector2dF(100.f, 100.f));
1270 host_impl_->ScrollEnd();
1272 EXPECT_VECTOR_EQ(
1273 gfx::Vector2dF(50, 50),
1274 inner_scroll_layer->CurrentScrollOffset());
1275 EXPECT_VECTOR_EQ(
1276 gfx::Vector2dF(0, 0),
1277 outer_scroll_layer->CurrentScrollOffset());
1279 // Scroll down - outer viewport should start scrolling after the inner is at
1280 // its maximum.
1281 host_impl_->ScrollBegin(gfx::Point(0, 0), InputHandler::GESTURE);
1282 host_impl_->ScrollBy(gfx::Point(0, 0), gfx::Vector2dF(1000.f, 1000.f));
1283 host_impl_->ScrollEnd();
1285 EXPECT_VECTOR_EQ(
1286 gfx::Vector2dF(250, 250),
1287 inner_scroll_layer->CurrentScrollOffset());
1288 EXPECT_VECTOR_EQ(
1289 gfx::Vector2dF(300, 300),
1290 outer_scroll_layer->CurrentScrollOffset());
1293 // Tests that scrolls during a pinch gesture (i.e. "two-finger" scrolls) work
1294 // as expected. That is, scrolling during a pinch should bubble from the inner
1295 // to the outer viewport.
1296 TEST_F(LayerTreeHostImplTest, ScrollDuringPinchGesture) {
1297 LayerTreeSettings settings = DefaultSettings();
1298 CreateHostImpl(settings,
1299 CreateOutputSurface());
1300 host_impl_->active_tree()->PushPageScaleFromMainThread(1.f, 1.f, 2.f);
1302 const gfx::Size content_size(1000, 1000);
1303 const gfx::Size viewport_size(500, 500);
1304 CreateBasicVirtualViewportLayers(viewport_size, content_size);
1306 LayerImpl* outer_scroll_layer = host_impl_->OuterViewportScrollLayer();
1307 LayerImpl* inner_scroll_layer = host_impl_->InnerViewportScrollLayer();
1309 EXPECT_VECTOR_EQ(
1310 gfx::Vector2dF(500, 500),
1311 outer_scroll_layer->MaxScrollOffset());
1313 host_impl_->ScrollBegin(gfx::Point(250, 250), InputHandler::GESTURE);
1314 host_impl_->PinchGestureBegin();
1316 host_impl_->PinchGestureUpdate(2, gfx::Point(250, 250));
1317 EXPECT_VECTOR_EQ(
1318 gfx::Vector2dF(0, 0),
1319 outer_scroll_layer->CurrentScrollOffset());
1320 EXPECT_VECTOR_EQ(
1321 gfx::Vector2dF(125, 125),
1322 inner_scroll_layer->CurrentScrollOffset());
1324 // Needed so that the pinch is accounted for in draw properties.
1325 DrawFrame();
1327 host_impl_->ScrollBy(gfx::Point(250, 250), gfx::Vector2dF(10.f, 10.f));
1328 EXPECT_VECTOR_EQ(
1329 gfx::Vector2dF(0, 0),
1330 outer_scroll_layer->CurrentScrollOffset());
1331 EXPECT_VECTOR_EQ(
1332 gfx::Vector2dF(130, 130),
1333 inner_scroll_layer->CurrentScrollOffset());
1335 DrawFrame();
1337 host_impl_->ScrollBy(gfx::Point(250, 250), gfx::Vector2dF(400.f, 400.f));
1338 EXPECT_VECTOR_EQ(
1339 gfx::Vector2dF(80, 80),
1340 outer_scroll_layer->CurrentScrollOffset());
1341 EXPECT_VECTOR_EQ(
1342 gfx::Vector2dF(250, 250),
1343 inner_scroll_layer->CurrentScrollOffset());
1345 host_impl_->PinchGestureEnd();
1346 host_impl_->ScrollEnd();
1349 // Tests the "snapping" of pinch-zoom gestures to the screen edge. That is, when
1350 // a pinch zoom is anchored within a certain margin of the screen edge, we
1351 // should assume the user means to scroll into the edge of the screen.
1352 TEST_F(LayerTreeHostImplTest, PinchZoomSnapsToScreenEdge) {
1353 LayerTreeSettings settings = DefaultSettings();
1354 CreateHostImpl(settings,
1355 CreateOutputSurface());
1356 host_impl_->active_tree()->PushPageScaleFromMainThread(1.f, 1.f, 2.f);
1358 const gfx::Size content_size(1000, 1000);
1359 const gfx::Size viewport_size(500, 500);
1360 CreateBasicVirtualViewportLayers(viewport_size, content_size);
1362 int offsetFromEdge = Viewport::kPinchZoomSnapMarginDips - 5;
1363 gfx::Point anchor(viewport_size.width() - offsetFromEdge,
1364 viewport_size.height() - offsetFromEdge);
1366 // Pinch in within the margins. The scroll should stay exactly locked to the
1367 // bottom and right.
1368 host_impl_->ScrollBegin(anchor, InputHandler::GESTURE);
1369 host_impl_->PinchGestureBegin();
1370 host_impl_->PinchGestureUpdate(2, anchor);
1371 host_impl_->PinchGestureEnd();
1372 host_impl_->ScrollEnd();
1374 EXPECT_VECTOR_EQ(
1375 gfx::Vector2dF(250, 250),
1376 host_impl_->InnerViewportScrollLayer()->CurrentScrollOffset());
1378 // Reset.
1379 host_impl_->active_tree()->SetPageScaleOnActiveTree(1.f);
1380 host_impl_->InnerViewportScrollLayer()->SetScrollDelta(gfx::Vector2d());
1381 host_impl_->OuterViewportScrollLayer()->SetScrollDelta(gfx::Vector2d());
1383 // Pinch in within the margins. The scroll should stay exactly locked to the
1384 // top and left.
1385 anchor = gfx::Point(offsetFromEdge, offsetFromEdge);
1386 host_impl_->ScrollBegin(anchor, InputHandler::GESTURE);
1387 host_impl_->PinchGestureBegin();
1388 host_impl_->PinchGestureUpdate(2, anchor);
1389 host_impl_->PinchGestureEnd();
1390 host_impl_->ScrollEnd();
1392 EXPECT_VECTOR_EQ(
1393 gfx::Vector2dF(0, 0),
1394 host_impl_->InnerViewportScrollLayer()->CurrentScrollOffset());
1396 // Reset.
1397 host_impl_->active_tree()->SetPageScaleOnActiveTree(1.f);
1398 host_impl_->InnerViewportScrollLayer()->SetScrollDelta(gfx::Vector2d());
1399 host_impl_->OuterViewportScrollLayer()->SetScrollDelta(gfx::Vector2d());
1401 // Pinch in just outside the margin. There should be no snapping.
1402 offsetFromEdge = Viewport::kPinchZoomSnapMarginDips;
1403 anchor = gfx::Point(offsetFromEdge, offsetFromEdge);
1404 host_impl_->ScrollBegin(anchor, InputHandler::GESTURE);
1405 host_impl_->PinchGestureBegin();
1406 host_impl_->PinchGestureUpdate(2, anchor);
1407 host_impl_->PinchGestureEnd();
1408 host_impl_->ScrollEnd();
1410 EXPECT_VECTOR_EQ(
1411 gfx::Vector2dF(50, 50),
1412 host_impl_->InnerViewportScrollLayer()->CurrentScrollOffset());
1414 // Reset.
1415 host_impl_->active_tree()->SetPageScaleOnActiveTree(1.f);
1416 host_impl_->InnerViewportScrollLayer()->SetScrollDelta(gfx::Vector2d());
1417 host_impl_->OuterViewportScrollLayer()->SetScrollDelta(gfx::Vector2d());
1419 // Pinch in just outside the margin. There should be no snapping.
1420 offsetFromEdge = Viewport::kPinchZoomSnapMarginDips;
1421 anchor = gfx::Point(viewport_size.width() - offsetFromEdge,
1422 viewport_size.height() - offsetFromEdge);
1423 host_impl_->ScrollBegin(anchor, InputHandler::GESTURE);
1424 host_impl_->PinchGestureBegin();
1425 host_impl_->PinchGestureUpdate(2, anchor);
1426 host_impl_->PinchGestureEnd();
1427 host_impl_->ScrollEnd();
1429 EXPECT_VECTOR_EQ(
1430 gfx::Vector2dF(200, 200),
1431 host_impl_->InnerViewportScrollLayer()->CurrentScrollOffset());
1434 TEST_F(LayerTreeHostImplTest, ImplPinchZoomWheelBubbleBetweenViewports) {
1435 const gfx::Size content_size(200, 200);
1436 const gfx::Size viewport_size(100, 100);
1437 CreateBasicVirtualViewportLayers(viewport_size, content_size);
1439 LayerImpl* outer_scroll_layer = host_impl_->OuterViewportScrollLayer();
1440 LayerImpl* inner_scroll_layer = host_impl_->InnerViewportScrollLayer();
1442 // Zoom into the page by a 2X factor
1443 float min_page_scale = 1.f, max_page_scale = 4.f;
1444 float page_scale_factor = 2.f;
1445 host_impl_->active_tree()->PushPageScaleFromMainThread(
1446 page_scale_factor, min_page_scale, max_page_scale);
1447 host_impl_->active_tree()->SetPageScaleOnActiveTree(page_scale_factor);
1449 // Scroll by a small amount, there should be no bubbling to the outer
1450 // viewport.
1451 host_impl_->ScrollBegin(gfx::Point(0, 0), InputHandler::WHEEL);
1452 host_impl_->ScrollBy(gfx::Point(0, 0), gfx::Vector2dF(10.f, 20.f));
1453 host_impl_->ScrollEnd();
1455 EXPECT_VECTOR_EQ(
1456 gfx::Vector2dF(5, 10),
1457 inner_scroll_layer->CurrentScrollOffset());
1458 EXPECT_VECTOR_EQ(
1459 gfx::Vector2dF(),
1460 outer_scroll_layer->CurrentScrollOffset());
1462 // Scroll by the inner viewport's max scroll extent, the remainder
1463 // should bubble up to the outer viewport.
1464 host_impl_->ScrollBegin(gfx::Point(0, 0), InputHandler::WHEEL);
1465 host_impl_->ScrollBy(gfx::Point(0, 0), gfx::Vector2dF(100.f, 100.f));
1466 host_impl_->ScrollEnd();
1468 EXPECT_VECTOR_EQ(
1469 gfx::Vector2dF(50, 50),
1470 inner_scroll_layer->CurrentScrollOffset());
1471 EXPECT_VECTOR_EQ(
1472 gfx::Vector2dF(5, 10),
1473 outer_scroll_layer->CurrentScrollOffset());
1475 // Scroll by the outer viewport's max scroll extent, it should all go to the
1476 // outer viewport.
1477 host_impl_->ScrollBegin(gfx::Point(0, 0), InputHandler::WHEEL);
1478 host_impl_->ScrollBy(gfx::Point(0, 0), gfx::Vector2dF(190.f, 180.f));
1479 host_impl_->ScrollEnd();
1481 EXPECT_VECTOR_EQ(
1482 gfx::Vector2dF(100, 100),
1483 outer_scroll_layer->CurrentScrollOffset());
1484 EXPECT_VECTOR_EQ(
1485 gfx::Vector2dF(50, 50),
1486 inner_scroll_layer->CurrentScrollOffset());
1489 TEST_F(LayerTreeHostImplTest, ScrollWithSwapPromises) {
1490 ui::LatencyInfo latency_info;
1491 latency_info.AddLatencyNumber(ui::INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT, 0,
1492 1234);
1493 scoped_ptr<SwapPromise> swap_promise(
1494 new LatencyInfoSwapPromise(latency_info));
1496 SetupScrollAndContentsLayers(gfx::Size(100, 100));
1497 EXPECT_EQ(InputHandler::SCROLL_STARTED,
1498 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
1499 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(0, 10));
1500 host_impl_->QueueSwapPromiseForMainThreadScrollUpdate(swap_promise.Pass());
1501 host_impl_->ScrollEnd();
1503 scoped_ptr<ScrollAndScaleSet> scroll_info = host_impl_->ProcessScrollDeltas();
1504 EXPECT_EQ(1u, scroll_info->swap_promises.size());
1505 EXPECT_EQ(latency_info.trace_id(), scroll_info->swap_promises[0]->TraceId());
1508 // Test that scrolls targeting a layer with a non-null scroll_parent() bubble
1509 // up to the scroll_parent, rather than the stacking parent.
1510 TEST_F(LayerTreeHostImplTest, ScrollBubblesToScrollParent) {
1511 LayerImpl* viewport_scroll =
1512 SetupScrollAndContentsLayers(gfx::Size(100, 100));
1513 host_impl_->SetViewportSize(gfx::Size(50, 50));
1515 // Set up two scrolling children of the root, one of which is a scroll parent
1516 // to the other. Scrolls bubbling from the child should bubble to the parent,
1517 // not the viewport.
1518 LayerImpl *parent;
1519 LayerImpl *child;
1520 LayerImpl *child_clip;
1522 scoped_ptr<LayerImpl> scroll_parent_clip =
1523 LayerImpl::Create(host_impl_->active_tree(), 6);
1524 scoped_ptr<LayerImpl> scroll_parent = CreateScrollableLayer(
1525 7, gfx::Size(10, 10), scroll_parent_clip.get());
1526 parent = scroll_parent.get();
1527 scroll_parent_clip->AddChild(scroll_parent.Pass());
1529 viewport_scroll->AddChild(scroll_parent_clip.Pass());
1531 scoped_ptr<LayerImpl> scroll_child_clip =
1532 LayerImpl::Create(host_impl_->active_tree(), 8);
1533 scoped_ptr<LayerImpl> scroll_child = CreateScrollableLayer(
1534 9, gfx::Size(10, 10), scroll_child_clip.get());
1535 child = scroll_child.get();
1536 scroll_child->SetPosition(gfx::Point(20, 20));
1537 scroll_child_clip->AddChild(scroll_child.Pass());
1539 child_clip = scroll_child_clip.get();
1540 viewport_scroll->AddChild(scroll_child_clip.Pass());
1542 child_clip->SetScrollParent(parent);
1544 DrawFrame();
1547 host_impl_->ScrollBegin(gfx::Point(21, 21), InputHandler::GESTURE);
1548 host_impl_->ScrollBy(gfx::Point(21, 21), gfx::Vector2d(5, 5));
1549 host_impl_->ScrollBy(gfx::Point(21, 21), gfx::Vector2d(2, 1));
1550 host_impl_->ScrollEnd();
1552 // The child should be fully scrolled by the first ScrollBy.
1553 EXPECT_VECTOR_EQ(gfx::Vector2dF(5, 5), child->CurrentScrollOffset());
1555 // The scroll_parent should receive the bubbled up second ScrollBy.
1556 EXPECT_VECTOR_EQ(gfx::Vector2dF(2, 1), parent->CurrentScrollOffset());
1558 // The viewport shouldn't have been scrolled at all.
1559 EXPECT_VECTOR_EQ(
1560 gfx::Vector2dF(0, 0),
1561 host_impl_->InnerViewportScrollLayer()->CurrentScrollOffset());
1562 EXPECT_VECTOR_EQ(
1563 gfx::Vector2dF(0, 0),
1564 host_impl_->OuterViewportScrollLayer()->CurrentScrollOffset());
1568 host_impl_->ScrollBegin(gfx::Point(21, 21), InputHandler::GESTURE);
1569 host_impl_->ScrollBy(gfx::Point(21, 21), gfx::Vector2d(3, 4));
1570 host_impl_->ScrollBy(gfx::Point(21, 21), gfx::Vector2d(2, 1));
1571 host_impl_->ScrollEnd();
1573 // The first ScrollBy should scroll the parent to its extent.
1574 EXPECT_VECTOR_EQ(gfx::Vector2dF(5, 5), parent->CurrentScrollOffset());
1576 // The viewport should now be next in bubbling order.
1577 EXPECT_VECTOR_EQ(
1578 gfx::Vector2dF(2, 1),
1579 host_impl_->InnerViewportScrollLayer()->CurrentScrollOffset());
1580 EXPECT_VECTOR_EQ(
1581 gfx::Vector2dF(0, 0),
1582 host_impl_->OuterViewportScrollLayer()->CurrentScrollOffset());
1587 TEST_F(LayerTreeHostImplTest, PinchGesture) {
1588 SetupScrollAndContentsLayers(gfx::Size(100, 100));
1589 host_impl_->SetViewportSize(gfx::Size(50, 50));
1590 DrawFrame();
1592 LayerImpl* scroll_layer = host_impl_->InnerViewportScrollLayer();
1593 DCHECK(scroll_layer);
1595 float min_page_scale = 1.f;
1596 float max_page_scale = 4.f;
1598 // Basic pinch zoom in gesture
1600 host_impl_->active_tree()->PushPageScaleFromMainThread(1.f, min_page_scale,
1601 max_page_scale);
1602 scroll_layer->SetScrollDelta(gfx::Vector2d());
1604 float page_scale_delta = 2.f;
1605 host_impl_->ScrollBegin(gfx::Point(50, 50), InputHandler::GESTURE);
1606 host_impl_->PinchGestureBegin();
1607 host_impl_->PinchGestureUpdate(page_scale_delta, gfx::Point(50, 50));
1608 host_impl_->PinchGestureEnd();
1609 host_impl_->ScrollEnd();
1610 EXPECT_FALSE(did_request_animate_);
1611 EXPECT_TRUE(did_request_redraw_);
1612 EXPECT_TRUE(did_request_commit_);
1614 scoped_ptr<ScrollAndScaleSet> scroll_info =
1615 host_impl_->ProcessScrollDeltas();
1616 EXPECT_EQ(scroll_info->page_scale_delta, page_scale_delta);
1619 // Zoom-in clamping
1621 host_impl_->active_tree()->PushPageScaleFromMainThread(1.f, min_page_scale,
1622 max_page_scale);
1623 scroll_layer->SetScrollDelta(gfx::Vector2d());
1624 float page_scale_delta = 10.f;
1626 host_impl_->ScrollBegin(gfx::Point(50, 50), InputHandler::GESTURE);
1627 host_impl_->PinchGestureBegin();
1628 host_impl_->PinchGestureUpdate(page_scale_delta, gfx::Point(50, 50));
1629 host_impl_->PinchGestureEnd();
1630 host_impl_->ScrollEnd();
1632 scoped_ptr<ScrollAndScaleSet> scroll_info =
1633 host_impl_->ProcessScrollDeltas();
1634 EXPECT_EQ(scroll_info->page_scale_delta, max_page_scale);
1637 // Zoom-out clamping
1639 host_impl_->active_tree()->PushPageScaleFromMainThread(1.f, min_page_scale,
1640 max_page_scale);
1641 scroll_layer->SetScrollDelta(gfx::Vector2d());
1642 scroll_layer->PullDeltaForMainThread();
1643 scroll_layer->PushScrollOffsetFromMainThread(gfx::ScrollOffset(50, 50));
1645 float page_scale_delta = 0.1f;
1646 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE);
1647 host_impl_->PinchGestureBegin();
1648 host_impl_->PinchGestureUpdate(page_scale_delta, gfx::Point());
1649 host_impl_->PinchGestureEnd();
1650 host_impl_->ScrollEnd();
1652 scoped_ptr<ScrollAndScaleSet> scroll_info =
1653 host_impl_->ProcessScrollDeltas();
1654 EXPECT_EQ(scroll_info->page_scale_delta, min_page_scale);
1656 EXPECT_TRUE(scroll_info->scrolls.empty());
1659 // Two-finger panning should not happen based on pinch events only
1661 host_impl_->active_tree()->PushPageScaleFromMainThread(1.f, min_page_scale,
1662 max_page_scale);
1663 scroll_layer->SetScrollDelta(gfx::Vector2d());
1664 scroll_layer->PullDeltaForMainThread();
1665 scroll_layer->PushScrollOffsetFromMainThread(gfx::ScrollOffset(20, 20));
1667 float page_scale_delta = 1.f;
1668 host_impl_->ScrollBegin(gfx::Point(10, 10), InputHandler::GESTURE);
1669 host_impl_->PinchGestureBegin();
1670 host_impl_->PinchGestureUpdate(page_scale_delta, gfx::Point(10, 10));
1671 host_impl_->PinchGestureUpdate(page_scale_delta, gfx::Point(20, 20));
1672 host_impl_->PinchGestureEnd();
1673 host_impl_->ScrollEnd();
1675 scoped_ptr<ScrollAndScaleSet> scroll_info =
1676 host_impl_->ProcessScrollDeltas();
1677 EXPECT_EQ(scroll_info->page_scale_delta, page_scale_delta);
1678 EXPECT_TRUE(scroll_info->scrolls.empty());
1681 // Two-finger panning should work with interleaved scroll events
1683 host_impl_->active_tree()->PushPageScaleFromMainThread(1.f, min_page_scale,
1684 max_page_scale);
1685 scroll_layer->SetScrollDelta(gfx::Vector2d());
1686 scroll_layer->PullDeltaForMainThread();
1687 scroll_layer->PushScrollOffsetFromMainThread(gfx::ScrollOffset(20, 20));
1689 float page_scale_delta = 1.f;
1690 host_impl_->ScrollBegin(gfx::Point(10, 10), InputHandler::GESTURE);
1691 host_impl_->PinchGestureBegin();
1692 host_impl_->PinchGestureUpdate(page_scale_delta, gfx::Point(10, 10));
1693 host_impl_->ScrollBy(gfx::Point(10, 10), gfx::Vector2d(-10, -10));
1694 host_impl_->PinchGestureUpdate(page_scale_delta, gfx::Point(20, 20));
1695 host_impl_->PinchGestureEnd();
1696 host_impl_->ScrollEnd();
1698 scoped_ptr<ScrollAndScaleSet> scroll_info =
1699 host_impl_->ProcessScrollDeltas();
1700 EXPECT_EQ(scroll_info->page_scale_delta, page_scale_delta);
1701 EXPECT_TRUE(ScrollInfoContains(*scroll_info, scroll_layer->id(),
1702 gfx::Vector2d(-10, -10)));
1705 // Two-finger panning should work when starting fully zoomed out.
1707 host_impl_->active_tree()->PushPageScaleFromMainThread(0.5f, 0.5f, 4.f);
1708 scroll_layer->SetScrollDelta(gfx::Vector2d());
1709 scroll_layer->PullDeltaForMainThread();
1710 scroll_layer->PushScrollOffsetFromMainThread(gfx::ScrollOffset(0, 0));
1712 host_impl_->ScrollBegin(gfx::Point(0, 0), InputHandler::GESTURE);
1713 host_impl_->PinchGestureBegin();
1714 host_impl_->PinchGestureUpdate(2.f, gfx::Point(0, 0));
1715 host_impl_->PinchGestureUpdate(1.f, gfx::Point(0, 0));
1717 // Needed so layer transform includes page scale.
1718 DrawFrame();
1720 host_impl_->ScrollBy(gfx::Point(0, 0), gfx::Vector2d(10, 10));
1721 host_impl_->PinchGestureUpdate(1.f, gfx::Point(10, 10));
1722 host_impl_->PinchGestureEnd();
1723 host_impl_->ScrollEnd();
1725 scoped_ptr<ScrollAndScaleSet> scroll_info =
1726 host_impl_->ProcessScrollDeltas();
1727 EXPECT_EQ(scroll_info->page_scale_delta, 2.f);
1728 EXPECT_TRUE(ScrollInfoContains(*scroll_info, scroll_layer->id(),
1729 gfx::Vector2d(10, 10)));
1733 TEST_F(LayerTreeHostImplTest, PageScaleAnimation) {
1734 SetupScrollAndContentsLayers(gfx::Size(100, 100));
1735 host_impl_->SetViewportSize(gfx::Size(50, 50));
1736 DrawFrame();
1738 LayerImpl* scroll_layer = host_impl_->InnerViewportScrollLayer();
1739 DCHECK(scroll_layer);
1741 float min_page_scale = 0.5f;
1742 float max_page_scale = 4.f;
1743 base::TimeTicks start_time = base::TimeTicks() +
1744 base::TimeDelta::FromSeconds(1);
1745 base::TimeDelta duration = base::TimeDelta::FromMilliseconds(100);
1746 base::TimeTicks halfway_through_animation = start_time + duration / 2;
1747 base::TimeTicks end_time = start_time + duration;
1749 BeginFrameArgs begin_frame_args =
1750 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE);
1752 // Non-anchor zoom-in
1754 host_impl_->active_tree()->PushPageScaleFromMainThread(1.f, min_page_scale,
1755 max_page_scale);
1756 scroll_layer->PushScrollOffsetFromMainThread(gfx::ScrollOffset(50, 50));
1758 did_request_redraw_ = false;
1759 did_request_animate_ = false;
1760 host_impl_->active_tree()->SetPendingPageScaleAnimation(
1761 scoped_ptr<PendingPageScaleAnimation>(new PendingPageScaleAnimation(
1762 gfx::Vector2d(),
1763 false,
1764 2.f,
1765 duration)));
1766 host_impl_->ActivateSyncTree();
1767 EXPECT_FALSE(did_request_redraw_);
1768 EXPECT_TRUE(did_request_animate_);
1770 did_request_redraw_ = false;
1771 did_request_animate_ = false;
1772 begin_frame_args.frame_time = start_time;
1773 host_impl_->WillBeginImplFrame(begin_frame_args);
1774 host_impl_->Animate();
1775 EXPECT_TRUE(did_request_redraw_);
1776 EXPECT_TRUE(did_request_animate_);
1777 host_impl_->DidFinishImplFrame();
1779 did_request_redraw_ = false;
1780 did_request_animate_ = false;
1781 begin_frame_args.frame_time = halfway_through_animation;
1782 host_impl_->WillBeginImplFrame(begin_frame_args);
1783 host_impl_->Animate();
1784 EXPECT_TRUE(did_request_redraw_);
1785 EXPECT_TRUE(did_request_animate_);
1786 host_impl_->DidFinishImplFrame();
1788 did_request_redraw_ = false;
1789 did_request_animate_ = false;
1790 did_request_commit_ = false;
1791 begin_frame_args.frame_time = end_time;
1792 host_impl_->WillBeginImplFrame(begin_frame_args);
1793 host_impl_->Animate();
1794 EXPECT_TRUE(did_request_commit_);
1795 EXPECT_FALSE(did_request_animate_);
1796 host_impl_->DidFinishImplFrame();
1798 scoped_ptr<ScrollAndScaleSet> scroll_info =
1799 host_impl_->ProcessScrollDeltas();
1800 EXPECT_EQ(scroll_info->page_scale_delta, 2);
1801 EXPECT_TRUE(ScrollInfoContains(*scroll_info, scroll_layer->id(),
1802 gfx::Vector2d(-50, -50)));
1805 start_time += base::TimeDelta::FromSeconds(10);
1806 halfway_through_animation += base::TimeDelta::FromSeconds(10);
1807 end_time += base::TimeDelta::FromSeconds(10);
1809 // Anchor zoom-out
1811 host_impl_->active_tree()->PushPageScaleFromMainThread(1.f, min_page_scale,
1812 max_page_scale);
1813 scroll_layer->PushScrollOffsetFromMainThread(gfx::ScrollOffset(50, 50));
1815 did_request_redraw_ = false;
1816 did_request_animate_ = false;
1817 host_impl_->active_tree()->SetPendingPageScaleAnimation(
1818 scoped_ptr<PendingPageScaleAnimation> (new PendingPageScaleAnimation(
1819 gfx::Vector2d(25, 25),
1820 true,
1821 min_page_scale,
1822 duration)));
1823 host_impl_->ActivateSyncTree();
1824 EXPECT_FALSE(did_request_redraw_);
1825 EXPECT_TRUE(did_request_animate_);
1827 did_request_redraw_ = false;
1828 did_request_animate_ = false;
1829 begin_frame_args.frame_time = start_time;
1830 host_impl_->WillBeginImplFrame(begin_frame_args);
1831 host_impl_->Animate();
1832 EXPECT_TRUE(did_request_redraw_);
1833 EXPECT_TRUE(did_request_animate_);
1834 host_impl_->DidFinishImplFrame();
1836 did_request_redraw_ = false;
1837 did_request_commit_ = false;
1838 did_request_animate_ = false;
1839 begin_frame_args.frame_time = end_time;
1840 host_impl_->WillBeginImplFrame(begin_frame_args);
1841 host_impl_->Animate();
1842 EXPECT_TRUE(did_request_redraw_);
1843 EXPECT_FALSE(did_request_animate_);
1844 EXPECT_TRUE(did_request_commit_);
1845 host_impl_->DidFinishImplFrame();
1847 scoped_ptr<ScrollAndScaleSet> scroll_info =
1848 host_impl_->ProcessScrollDeltas();
1849 EXPECT_EQ(scroll_info->page_scale_delta, min_page_scale);
1850 // Pushed to (0,0) via clamping against contents layer size.
1851 EXPECT_TRUE(ScrollInfoContains(*scroll_info, scroll_layer->id(),
1852 gfx::Vector2d(-50, -50)));
1856 TEST_F(LayerTreeHostImplTest, PageScaleAnimationNoOp) {
1857 SetupScrollAndContentsLayers(gfx::Size(100, 100));
1858 host_impl_->SetViewportSize(gfx::Size(50, 50));
1859 DrawFrame();
1861 LayerImpl* scroll_layer = host_impl_->InnerViewportScrollLayer();
1862 DCHECK(scroll_layer);
1864 float min_page_scale = 0.5f;
1865 float max_page_scale = 4.f;
1866 base::TimeTicks start_time = base::TimeTicks() +
1867 base::TimeDelta::FromSeconds(1);
1868 base::TimeDelta duration = base::TimeDelta::FromMilliseconds(100);
1869 base::TimeTicks halfway_through_animation = start_time + duration / 2;
1870 base::TimeTicks end_time = start_time + duration;
1872 BeginFrameArgs begin_frame_args =
1873 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE);
1875 // Anchor zoom with unchanged page scale should not change scroll or scale.
1877 host_impl_->active_tree()->PushPageScaleFromMainThread(1.f, min_page_scale,
1878 max_page_scale);
1879 scroll_layer->PushScrollOffsetFromMainThread(gfx::ScrollOffset(50, 50));
1881 host_impl_->active_tree()->SetPendingPageScaleAnimation(
1882 scoped_ptr<PendingPageScaleAnimation>(new PendingPageScaleAnimation(
1883 gfx::Vector2d(),
1884 true,
1885 1.f,
1886 duration)));
1887 host_impl_->ActivateSyncTree();
1888 begin_frame_args.frame_time = start_time;
1889 host_impl_->WillBeginImplFrame(begin_frame_args);
1890 host_impl_->Animate();
1891 host_impl_->DidFinishImplFrame();
1893 begin_frame_args.frame_time = halfway_through_animation;
1894 host_impl_->WillBeginImplFrame(begin_frame_args);
1895 host_impl_->Animate();
1896 EXPECT_TRUE(did_request_redraw_);
1897 host_impl_->DidFinishImplFrame();
1899 begin_frame_args.frame_time = end_time;
1900 host_impl_->WillBeginImplFrame(begin_frame_args);
1901 host_impl_->Animate();
1902 EXPECT_TRUE(did_request_commit_);
1903 host_impl_->DidFinishImplFrame();
1905 scoped_ptr<ScrollAndScaleSet> scroll_info =
1906 host_impl_->ProcessScrollDeltas();
1907 EXPECT_EQ(scroll_info->page_scale_delta, 1);
1908 ExpectNone(*scroll_info, scroll_layer->id());
1912 TEST_F(LayerTreeHostImplTest, PageScaleAnimationTransferedOnSyncTreeActivate) {
1913 host_impl_->CreatePendingTree();
1914 host_impl_->pending_tree()->PushPageScaleFromMainThread(1.f, 1.f, 1.f);
1915 CreateScrollAndContentsLayers(
1916 host_impl_->pending_tree(),
1917 gfx::Size(100, 100));
1918 host_impl_->ActivateSyncTree();
1919 DrawFrame();
1921 LayerImpl* scroll_layer = host_impl_->InnerViewportScrollLayer();
1922 DCHECK(scroll_layer);
1924 float min_page_scale = 0.5f;
1925 float max_page_scale = 4.f;
1926 host_impl_->sync_tree()->PushPageScaleFromMainThread(1.f, min_page_scale,
1927 max_page_scale);
1928 host_impl_->ActivateSyncTree();
1930 base::TimeTicks start_time = base::TimeTicks() +
1931 base::TimeDelta::FromSeconds(1);
1932 base::TimeDelta duration = base::TimeDelta::FromMilliseconds(100);
1933 base::TimeTicks third_through_animation = start_time + duration / 3;
1934 base::TimeTicks halfway_through_animation = start_time + duration / 2;
1935 base::TimeTicks end_time = start_time + duration;
1936 float target_scale = 2.f;
1938 BeginFrameArgs begin_frame_args =
1939 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE);
1941 scroll_layer->PushScrollOffsetFromMainThread(gfx::ScrollOffset(50, 50));
1943 // Make sure TakePageScaleAnimation works properly.
1945 host_impl_->sync_tree()->SetPendingPageScaleAnimation(
1946 scoped_ptr<PendingPageScaleAnimation>(new PendingPageScaleAnimation(
1947 gfx::Vector2d(),
1948 false,
1949 target_scale,
1950 duration)));
1951 scoped_ptr<PendingPageScaleAnimation> psa =
1952 host_impl_->sync_tree()->TakePendingPageScaleAnimation();
1953 EXPECT_EQ(target_scale, psa->scale);
1954 EXPECT_EQ(duration, psa->duration);
1955 EXPECT_EQ(nullptr, host_impl_->sync_tree()->TakePendingPageScaleAnimation());
1957 // Recreate the PSA. Nothing should happen here since the tree containing the
1958 // PSA hasn't been activated yet.
1959 did_request_redraw_ = false;
1960 did_request_animate_ = false;
1961 host_impl_->sync_tree()->SetPendingPageScaleAnimation(
1962 scoped_ptr<PendingPageScaleAnimation>(new PendingPageScaleAnimation(
1963 gfx::Vector2d(),
1964 false,
1965 target_scale,
1966 duration)));
1967 begin_frame_args.frame_time = halfway_through_animation;
1968 host_impl_->WillBeginImplFrame(begin_frame_args);
1969 host_impl_->Animate();
1970 EXPECT_FALSE(did_request_animate_);
1971 EXPECT_FALSE(did_request_redraw_);
1972 host_impl_->DidFinishImplFrame();
1974 // Activate the sync tree. This should cause the animation to become enabled.
1975 // It should also clear the pointer on the sync tree.
1976 host_impl_->ActivateSyncTree();
1977 EXPECT_EQ(nullptr,
1978 host_impl_->sync_tree()->TakePendingPageScaleAnimation().get());
1979 EXPECT_FALSE(did_request_redraw_);
1980 EXPECT_TRUE(did_request_animate_);
1982 start_time += base::TimeDelta::FromSeconds(10);
1983 third_through_animation += base::TimeDelta::FromSeconds(10);
1984 halfway_through_animation += base::TimeDelta::FromSeconds(10);
1985 end_time += base::TimeDelta::FromSeconds(10);
1987 // From here on, make sure the animation runs as normal.
1988 did_request_redraw_ = false;
1989 did_request_animate_ = false;
1990 begin_frame_args.frame_time = start_time;
1991 host_impl_->WillBeginImplFrame(begin_frame_args);
1992 host_impl_->Animate();
1993 EXPECT_TRUE(did_request_redraw_);
1994 EXPECT_TRUE(did_request_animate_);
1995 host_impl_->DidFinishImplFrame();
1997 did_request_redraw_ = false;
1998 did_request_animate_ = false;
1999 begin_frame_args.frame_time = third_through_animation;
2000 host_impl_->WillBeginImplFrame(begin_frame_args);
2001 host_impl_->Animate();
2002 EXPECT_TRUE(did_request_redraw_);
2003 EXPECT_TRUE(did_request_animate_);
2004 host_impl_->DidFinishImplFrame();
2006 // Another activation shouldn't have any effect on the animation.
2007 host_impl_->ActivateSyncTree();
2009 did_request_redraw_ = false;
2010 did_request_animate_ = false;
2011 begin_frame_args.frame_time = halfway_through_animation;
2012 host_impl_->WillBeginImplFrame(begin_frame_args);
2013 host_impl_->Animate();
2014 EXPECT_TRUE(did_request_redraw_);
2015 EXPECT_TRUE(did_request_animate_);
2016 host_impl_->DidFinishImplFrame();
2018 did_request_redraw_ = false;
2019 did_request_animate_ = false;
2020 did_request_commit_ = false;
2021 begin_frame_args.frame_time = end_time;
2022 host_impl_->WillBeginImplFrame(begin_frame_args);
2023 host_impl_->Animate();
2024 EXPECT_TRUE(did_request_commit_);
2025 EXPECT_FALSE(did_request_animate_);
2026 host_impl_->DidFinishImplFrame();
2028 scoped_ptr<ScrollAndScaleSet> scroll_info =
2029 host_impl_->ProcessScrollDeltas();
2030 EXPECT_EQ(scroll_info->page_scale_delta, target_scale);
2031 EXPECT_TRUE(ScrollInfoContains(*scroll_info, scroll_layer->id(),
2032 gfx::Vector2d(-50, -50)));
2035 TEST_F(LayerTreeHostImplTest, PageScaleAnimationCompletedNotification) {
2036 SetupScrollAndContentsLayers(gfx::Size(100, 100));
2037 host_impl_->SetViewportSize(gfx::Size(50, 50));
2038 DrawFrame();
2040 LayerImpl* scroll_layer = host_impl_->InnerViewportScrollLayer();
2041 DCHECK(scroll_layer);
2043 base::TimeTicks start_time =
2044 base::TimeTicks() + base::TimeDelta::FromSeconds(1);
2045 base::TimeDelta duration = base::TimeDelta::FromMilliseconds(100);
2046 base::TimeTicks halfway_through_animation = start_time + duration / 2;
2047 base::TimeTicks end_time = start_time + duration;
2049 BeginFrameArgs begin_frame_args =
2050 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE);
2052 host_impl_->active_tree()->PushPageScaleFromMainThread(1.f, 0.5f, 4.f);
2053 scroll_layer->PushScrollOffsetFromMainThread(gfx::ScrollOffset(50, 50));
2055 did_complete_page_scale_animation_ = false;
2056 host_impl_->active_tree()->SetPendingPageScaleAnimation(
2057 scoped_ptr<PendingPageScaleAnimation>(new PendingPageScaleAnimation(
2058 gfx::Vector2d(), false, 2.f, duration)));
2059 host_impl_->ActivateSyncTree();
2060 begin_frame_args.frame_time = start_time;
2061 host_impl_->WillBeginImplFrame(begin_frame_args);
2062 host_impl_->Animate();
2063 EXPECT_FALSE(did_complete_page_scale_animation_);
2064 host_impl_->DidFinishImplFrame();
2066 begin_frame_args.frame_time = halfway_through_animation;
2067 host_impl_->WillBeginImplFrame(begin_frame_args);
2068 host_impl_->Animate();
2069 EXPECT_FALSE(did_complete_page_scale_animation_);
2070 host_impl_->DidFinishImplFrame();
2072 begin_frame_args.frame_time = end_time;
2073 host_impl_->WillBeginImplFrame(begin_frame_args);
2074 host_impl_->Animate();
2075 EXPECT_TRUE(did_complete_page_scale_animation_);
2076 host_impl_->DidFinishImplFrame();
2079 class LayerTreeHostImplOverridePhysicalTime : public LayerTreeHostImpl {
2080 public:
2081 LayerTreeHostImplOverridePhysicalTime(
2082 const LayerTreeSettings& settings,
2083 LayerTreeHostImplClient* client,
2084 Proxy* proxy,
2085 SharedBitmapManager* manager,
2086 TaskGraphRunner* task_graph_runner,
2087 RenderingStatsInstrumentation* rendering_stats_instrumentation)
2088 : LayerTreeHostImpl(settings,
2089 client,
2090 proxy,
2091 rendering_stats_instrumentation,
2092 manager,
2093 nullptr,
2094 task_graph_runner,
2095 0) {}
2097 BeginFrameArgs CurrentBeginFrameArgs() const override {
2098 return CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE,
2099 fake_current_physical_time_);
2102 void SetCurrentPhysicalTimeTicksForTest(base::TimeTicks fake_now) {
2103 fake_current_physical_time_ = fake_now;
2106 private:
2107 base::TimeTicks fake_current_physical_time_;
2110 class LayerTreeHostImplTestScrollbarAnimation : public LayerTreeHostImplTest {
2111 protected:
2112 void SetupLayers(LayerTreeSettings settings) {
2113 gfx::Size content_size(100, 100);
2115 LayerTreeHostImplOverridePhysicalTime* host_impl_override_time =
2116 new LayerTreeHostImplOverridePhysicalTime(
2117 settings, this, &proxy_, &shared_bitmap_manager_,
2118 &task_graph_runner_, &stats_instrumentation_);
2119 host_impl_ = make_scoped_ptr(host_impl_override_time);
2120 host_impl_->InitializeRenderer(CreateOutputSurface());
2122 SetupScrollAndContentsLayers(content_size);
2123 host_impl_->active_tree()->PushPageScaleFromMainThread(1.f, 1.f, 4.f);
2124 host_impl_->SetViewportSize(
2125 gfx::Size(content_size.width() / 2, content_size.height() / 2));
2127 scoped_ptr<SolidColorScrollbarLayerImpl> scrollbar =
2128 SolidColorScrollbarLayerImpl::Create(host_impl_->active_tree(), 400,
2129 VERTICAL, 10, 0, false, true);
2130 EXPECT_FLOAT_EQ(0.f, scrollbar->opacity());
2132 LayerImpl* scroll = host_impl_->InnerViewportScrollLayer();
2133 LayerImpl* root = scroll->parent()->parent();
2134 scrollbar->SetScrollLayerAndClipLayerByIds(scroll->id(), root->id());
2135 root->AddChild(scrollbar.Pass());
2137 host_impl_->active_tree()->DidBecomeActive();
2138 DrawFrame();
2141 void RunTest(LayerTreeSettings::ScrollbarAnimator animator) {
2142 LayerTreeSettings settings;
2143 settings.scrollbar_animator = animator;
2144 settings.scrollbar_fade_delay_ms = 20;
2145 settings.scrollbar_fade_duration_ms = 20;
2147 SetupLayers(settings);
2149 base::TimeTicks fake_now = base::TimeTicks::Now();
2151 EXPECT_FALSE(did_request_animate_);
2152 EXPECT_FALSE(did_request_redraw_);
2153 EXPECT_EQ(base::TimeDelta(), requested_animation_delay_);
2154 EXPECT_TRUE(animation_task_.Equals(base::Closure()));
2156 // If no scroll happened during a scroll gesture, it should have no effect.
2157 host_impl_->ScrollBegin(gfx::Point(), InputHandler::WHEEL);
2158 host_impl_->ScrollEnd();
2159 EXPECT_FALSE(did_request_animate_);
2160 EXPECT_FALSE(did_request_redraw_);
2161 EXPECT_EQ(base::TimeDelta(), requested_animation_delay_);
2162 EXPECT_TRUE(animation_task_.Equals(base::Closure()));
2164 // After a scroll, a scrollbar animation should be scheduled about 20ms from
2165 // now.
2166 host_impl_->ScrollBegin(gfx::Point(), InputHandler::WHEEL);
2167 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2dF(0, 5));
2168 EXPECT_FALSE(did_request_animate_);
2169 EXPECT_TRUE(did_request_redraw_);
2170 did_request_redraw_ = false;
2171 EXPECT_EQ(base::TimeDelta(), requested_animation_delay_);
2172 EXPECT_TRUE(animation_task_.Equals(base::Closure()));
2174 host_impl_->ScrollEnd();
2175 EXPECT_FALSE(did_request_animate_);
2176 EXPECT_FALSE(did_request_redraw_);
2177 EXPECT_EQ(base::TimeDelta::FromMilliseconds(20),
2178 requested_animation_delay_);
2179 EXPECT_FALSE(animation_task_.Equals(base::Closure()));
2181 fake_now += requested_animation_delay_;
2182 requested_animation_delay_ = base::TimeDelta();
2183 animation_task_.Run();
2184 animation_task_ = base::Closure();
2185 EXPECT_TRUE(did_request_animate_);
2186 did_request_animate_ = false;
2187 EXPECT_FALSE(did_request_redraw_);
2189 // After the scrollbar animation begins, we should start getting redraws.
2190 BeginFrameArgs begin_frame_args =
2191 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, fake_now);
2192 host_impl_->WillBeginImplFrame(begin_frame_args);
2193 host_impl_->Animate();
2194 EXPECT_TRUE(did_request_animate_);
2195 did_request_animate_ = false;
2196 EXPECT_TRUE(did_request_redraw_);
2197 did_request_redraw_ = false;
2198 EXPECT_EQ(base::TimeDelta(), requested_animation_delay_);
2199 EXPECT_TRUE(animation_task_.Equals(base::Closure()));
2200 host_impl_->DidFinishImplFrame();
2202 // Setting the scroll offset outside a scroll should also cause the
2203 // scrollbar to appear and to schedule a scrollbar animation.
2204 host_impl_->InnerViewportScrollLayer()->PushScrollOffsetFromMainThread(
2205 gfx::ScrollOffset(5, 5));
2206 EXPECT_FALSE(did_request_animate_);
2207 EXPECT_FALSE(did_request_redraw_);
2208 EXPECT_EQ(base::TimeDelta::FromMilliseconds(20),
2209 requested_animation_delay_);
2210 EXPECT_FALSE(animation_task_.Equals(base::Closure()));
2211 requested_animation_delay_ = base::TimeDelta();
2212 animation_task_ = base::Closure();
2214 // Scrollbar animation is not triggered unnecessarily.
2215 host_impl_->ScrollBegin(gfx::Point(), InputHandler::WHEEL);
2216 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2dF(5, 0));
2217 EXPECT_FALSE(did_request_animate_);
2218 EXPECT_TRUE(did_request_redraw_);
2219 did_request_redraw_ = false;
2220 EXPECT_EQ(base::TimeDelta(), requested_animation_delay_);
2221 EXPECT_TRUE(animation_task_.Equals(base::Closure()));
2223 host_impl_->ScrollEnd();
2224 EXPECT_FALSE(did_request_animate_);
2225 EXPECT_FALSE(did_request_redraw_);
2226 EXPECT_EQ(base::TimeDelta(), requested_animation_delay_);
2227 EXPECT_TRUE(animation_task_.Equals(base::Closure()));
2229 // Changing page scale triggers scrollbar animation.
2230 host_impl_->active_tree()->PushPageScaleFromMainThread(1.f, 1.f, 4.f);
2231 host_impl_->active_tree()->SetPageScaleOnActiveTree(1.1f);
2232 EXPECT_FALSE(did_request_animate_);
2233 EXPECT_FALSE(did_request_redraw_);
2234 EXPECT_EQ(base::TimeDelta::FromMilliseconds(20),
2235 requested_animation_delay_);
2236 EXPECT_FALSE(animation_task_.Equals(base::Closure()));
2237 requested_animation_delay_ = base::TimeDelta();
2238 animation_task_ = base::Closure();
2242 TEST_F(LayerTreeHostImplTestScrollbarAnimation, LinearFade) {
2243 RunTest(LayerTreeSettings::LINEAR_FADE);
2246 TEST_F(LayerTreeHostImplTestScrollbarAnimation, Thinning) {
2247 RunTest(LayerTreeSettings::THINNING);
2250 void LayerTreeHostImplTest::SetupMouseMoveAtWithDeviceScale(
2251 float device_scale_factor) {
2252 LayerTreeSettings settings;
2253 settings.scrollbar_fade_delay_ms = 500;
2254 settings.scrollbar_fade_duration_ms = 300;
2255 settings.scrollbar_animator = LayerTreeSettings::THINNING;
2257 gfx::Size viewport_size(300, 200);
2258 gfx::Size device_viewport_size = gfx::ToFlooredSize(
2259 gfx::ScaleSize(viewport_size, device_scale_factor));
2260 gfx::Size content_size(1000, 1000);
2262 CreateHostImpl(settings, CreateOutputSurface());
2263 host_impl_->SetDeviceScaleFactor(device_scale_factor);
2264 host_impl_->SetViewportSize(device_viewport_size);
2266 scoped_ptr<LayerImpl> root =
2267 LayerImpl::Create(host_impl_->active_tree(), 1);
2268 root->SetBounds(viewport_size);
2269 root->SetHasRenderSurface(true);
2271 scoped_ptr<LayerImpl> scroll =
2272 LayerImpl::Create(host_impl_->active_tree(), 2);
2273 scroll->SetScrollClipLayer(root->id());
2274 scroll->PushScrollOffsetFromMainThread(gfx::ScrollOffset());
2275 scroll->SetBounds(content_size);
2276 scroll->SetIsContainerForFixedPositionLayers(true);
2278 scoped_ptr<LayerImpl> contents =
2279 LayerImpl::Create(host_impl_->active_tree(), 3);
2280 contents->SetDrawsContent(true);
2281 contents->SetBounds(content_size);
2283 // The scrollbar is on the right side.
2284 scoped_ptr<PaintedScrollbarLayerImpl> scrollbar =
2285 PaintedScrollbarLayerImpl::Create(host_impl_->active_tree(), 5, VERTICAL);
2286 scrollbar->SetDrawsContent(true);
2287 scrollbar->SetBounds(gfx::Size(15, viewport_size.height()));
2288 scrollbar->SetPosition(gfx::Point(285, 0));
2290 scroll->AddChild(contents.Pass());
2291 root->AddChild(scroll.Pass());
2292 scrollbar->SetScrollLayerAndClipLayerByIds(2, 1);
2293 root->AddChild(scrollbar.Pass());
2295 host_impl_->active_tree()->SetRootLayer(root.Pass());
2296 host_impl_->active_tree()->SetViewportLayersFromIds(Layer::INVALID_ID, 1, 2,
2297 Layer::INVALID_ID);
2298 host_impl_->active_tree()->DidBecomeActive();
2299 DrawFrame();
2301 LayerImpl* root_scroll =
2302 host_impl_->active_tree()->InnerViewportScrollLayer();
2303 ASSERT_TRUE(root_scroll->scrollbar_animation_controller());
2304 ScrollbarAnimationControllerThinning* scrollbar_animation_controller =
2305 static_cast<ScrollbarAnimationControllerThinning*>(
2306 root_scroll->scrollbar_animation_controller());
2307 scrollbar_animation_controller->set_mouse_move_distance_for_test(100.f);
2309 host_impl_->MouseMoveAt(gfx::Point(1, 1));
2310 EXPECT_FALSE(scrollbar_animation_controller->mouse_is_near_scrollbar());
2312 host_impl_->MouseMoveAt(gfx::Point(200, 50));
2313 EXPECT_TRUE(scrollbar_animation_controller->mouse_is_near_scrollbar());
2315 host_impl_->MouseMoveAt(gfx::Point(184, 100));
2316 EXPECT_FALSE(scrollbar_animation_controller->mouse_is_near_scrollbar());
2318 scrollbar_animation_controller->set_mouse_move_distance_for_test(102.f);
2319 host_impl_->MouseMoveAt(gfx::Point(184, 100));
2320 EXPECT_TRUE(scrollbar_animation_controller->mouse_is_near_scrollbar());
2322 did_request_redraw_ = false;
2323 EXPECT_EQ(0, host_impl_->scroll_layer_id_when_mouse_over_scrollbar());
2324 host_impl_->MouseMoveAt(gfx::Point(290, 100));
2325 EXPECT_EQ(2, host_impl_->scroll_layer_id_when_mouse_over_scrollbar());
2326 host_impl_->MouseMoveAt(gfx::Point(290, 120));
2327 EXPECT_EQ(2, host_impl_->scroll_layer_id_when_mouse_over_scrollbar());
2328 host_impl_->MouseMoveAt(gfx::Point(150, 120));
2329 EXPECT_EQ(0, host_impl_->scroll_layer_id_when_mouse_over_scrollbar());
2332 TEST_F(LayerTreeHostImplTest, MouseMoveAtWithDeviceScaleOf1) {
2333 SetupMouseMoveAtWithDeviceScale(1.f);
2336 TEST_F(LayerTreeHostImplTest, MouseMoveAtWithDeviceScaleOf2) {
2337 SetupMouseMoveAtWithDeviceScale(2.f);
2340 TEST_F(LayerTreeHostImplTest, CompositorFrameMetadata) {
2341 SetupScrollAndContentsLayers(gfx::Size(100, 100));
2342 host_impl_->SetViewportSize(gfx::Size(50, 50));
2343 host_impl_->active_tree()->PushPageScaleFromMainThread(1.f, 0.5f, 4.f);
2344 DrawFrame();
2346 CompositorFrameMetadata metadata =
2347 host_impl_->MakeCompositorFrameMetadata();
2348 EXPECT_EQ(gfx::Vector2dF(), metadata.root_scroll_offset);
2349 EXPECT_EQ(1.f, metadata.page_scale_factor);
2350 EXPECT_EQ(gfx::SizeF(50.f, 50.f), metadata.scrollable_viewport_size);
2351 EXPECT_EQ(gfx::SizeF(100.f, 100.f), metadata.root_layer_size);
2352 EXPECT_EQ(0.5f, metadata.min_page_scale_factor);
2353 EXPECT_EQ(4.f, metadata.max_page_scale_factor);
2354 EXPECT_FALSE(metadata.root_overflow_x_hidden);
2355 EXPECT_FALSE(metadata.root_overflow_y_hidden);
2358 // Scrolling should update metadata immediately.
2359 EXPECT_EQ(InputHandler::SCROLL_STARTED,
2360 host_impl_->ScrollBegin(gfx::Point(), InputHandler::WHEEL));
2361 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(0, 10));
2363 CompositorFrameMetadata metadata =
2364 host_impl_->MakeCompositorFrameMetadata();
2365 EXPECT_EQ(gfx::Vector2dF(0.f, 10.f), metadata.root_scroll_offset);
2367 host_impl_->ScrollEnd();
2369 CompositorFrameMetadata metadata =
2370 host_impl_->MakeCompositorFrameMetadata();
2371 EXPECT_EQ(gfx::Vector2dF(0.f, 10.f), metadata.root_scroll_offset);
2374 // Root "overflow: hidden" properties should be reflected on the outer
2375 // viewport scroll layer.
2377 host_impl_->active_tree()
2378 ->OuterViewportScrollLayer()
2379 ->set_user_scrollable_horizontal(false);
2380 CompositorFrameMetadata metadata =
2381 host_impl_->MakeCompositorFrameMetadata();
2382 EXPECT_TRUE(metadata.root_overflow_x_hidden);
2383 EXPECT_FALSE(metadata.root_overflow_y_hidden);
2385 host_impl_->active_tree()
2386 ->OuterViewportScrollLayer()
2387 ->set_user_scrollable_vertical(false);
2388 metadata = host_impl_->MakeCompositorFrameMetadata();
2389 EXPECT_TRUE(metadata.root_overflow_x_hidden);
2390 EXPECT_TRUE(metadata.root_overflow_y_hidden);
2393 // Re-enable scrollability and verify that overflows are no longer hidden.
2395 host_impl_->active_tree()
2396 ->OuterViewportScrollLayer()
2397 ->set_user_scrollable_horizontal(true);
2398 host_impl_->active_tree()
2399 ->OuterViewportScrollLayer()
2400 ->set_user_scrollable_vertical(true);
2401 CompositorFrameMetadata metadata =
2402 host_impl_->MakeCompositorFrameMetadata();
2403 EXPECT_FALSE(metadata.root_overflow_x_hidden);
2404 EXPECT_FALSE(metadata.root_overflow_y_hidden);
2407 // Root "overflow: hidden" properties should also be reflected on the
2408 // inner viewport scroll layer.
2410 host_impl_->active_tree()
2411 ->InnerViewportScrollLayer()
2412 ->set_user_scrollable_horizontal(false);
2413 CompositorFrameMetadata metadata =
2414 host_impl_->MakeCompositorFrameMetadata();
2415 EXPECT_TRUE(metadata.root_overflow_x_hidden);
2416 EXPECT_FALSE(metadata.root_overflow_y_hidden);
2418 host_impl_->active_tree()
2419 ->InnerViewportScrollLayer()
2420 ->set_user_scrollable_vertical(false);
2421 metadata = host_impl_->MakeCompositorFrameMetadata();
2422 EXPECT_TRUE(metadata.root_overflow_x_hidden);
2423 EXPECT_TRUE(metadata.root_overflow_y_hidden);
2426 // Page scale should update metadata correctly (shrinking only the viewport).
2427 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE);
2428 host_impl_->PinchGestureBegin();
2429 host_impl_->PinchGestureUpdate(2.f, gfx::Point());
2430 host_impl_->PinchGestureEnd();
2431 host_impl_->ScrollEnd();
2433 CompositorFrameMetadata metadata =
2434 host_impl_->MakeCompositorFrameMetadata();
2435 EXPECT_EQ(gfx::Vector2dF(0.f, 10.f), metadata.root_scroll_offset);
2436 EXPECT_EQ(2.f, metadata.page_scale_factor);
2437 EXPECT_EQ(gfx::SizeF(25.f, 25.f), metadata.scrollable_viewport_size);
2438 EXPECT_EQ(gfx::SizeF(100.f, 100.f), metadata.root_layer_size);
2439 EXPECT_EQ(0.5f, metadata.min_page_scale_factor);
2440 EXPECT_EQ(4.f, metadata.max_page_scale_factor);
2443 // Likewise if set from the main thread.
2444 host_impl_->ProcessScrollDeltas();
2445 host_impl_->active_tree()->PushPageScaleFromMainThread(4.f, 0.5f, 4.f);
2446 host_impl_->active_tree()->SetPageScaleOnActiveTree(4.f);
2448 CompositorFrameMetadata metadata =
2449 host_impl_->MakeCompositorFrameMetadata();
2450 EXPECT_EQ(gfx::Vector2dF(0.f, 10.f), metadata.root_scroll_offset);
2451 EXPECT_EQ(4.f, metadata.page_scale_factor);
2452 EXPECT_EQ(gfx::SizeF(12.5f, 12.5f), metadata.scrollable_viewport_size);
2453 EXPECT_EQ(gfx::SizeF(100.f, 100.f), metadata.root_layer_size);
2454 EXPECT_EQ(0.5f, metadata.min_page_scale_factor);
2455 EXPECT_EQ(4.f, metadata.max_page_scale_factor);
2459 class DidDrawCheckLayer : public LayerImpl {
2460 public:
2461 static scoped_ptr<LayerImpl> Create(LayerTreeImpl* tree_impl, int id) {
2462 return make_scoped_ptr(new DidDrawCheckLayer(tree_impl, id));
2465 bool WillDraw(DrawMode draw_mode, ResourceProvider* provider) override {
2466 will_draw_called_ = true;
2467 if (will_draw_returns_false_)
2468 return false;
2469 return LayerImpl::WillDraw(draw_mode, provider);
2472 void AppendQuads(RenderPass* render_pass,
2473 AppendQuadsData* append_quads_data) override {
2474 append_quads_called_ = true;
2475 LayerImpl::AppendQuads(render_pass, append_quads_data);
2478 void DidDraw(ResourceProvider* provider) override {
2479 did_draw_called_ = true;
2480 LayerImpl::DidDraw(provider);
2483 bool will_draw_called() const { return will_draw_called_; }
2484 bool append_quads_called() const { return append_quads_called_; }
2485 bool did_draw_called() const { return did_draw_called_; }
2487 void set_will_draw_returns_false() { will_draw_returns_false_ = true; }
2489 void ClearDidDrawCheck() {
2490 will_draw_called_ = false;
2491 append_quads_called_ = false;
2492 did_draw_called_ = false;
2495 static void IgnoreResult(scoped_ptr<CopyOutputResult> result) {}
2497 void AddCopyRequest() {
2498 ScopedPtrVector<CopyOutputRequest> requests;
2499 requests.push_back(
2500 CopyOutputRequest::CreateRequest(base::Bind(&IgnoreResult)));
2501 SetHasRenderSurface(true);
2502 PassCopyRequests(&requests);
2505 protected:
2506 DidDrawCheckLayer(LayerTreeImpl* tree_impl, int id)
2507 : LayerImpl(tree_impl, id),
2508 will_draw_returns_false_(false),
2509 will_draw_called_(false),
2510 append_quads_called_(false),
2511 did_draw_called_(false) {
2512 SetBounds(gfx::Size(10, 10));
2513 SetDrawsContent(true);
2514 draw_properties().visible_layer_rect = gfx::Rect(0, 0, 10, 10);
2517 private:
2518 bool will_draw_returns_false_;
2519 bool will_draw_called_;
2520 bool append_quads_called_;
2521 bool did_draw_called_;
2524 TEST_F(LayerTreeHostImplTest, WillDrawReturningFalseDoesNotCall) {
2525 // The root layer is always drawn, so run this test on a child layer that
2526 // will be masked out by the root layer's bounds.
2527 host_impl_->active_tree()->SetRootLayer(
2528 DidDrawCheckLayer::Create(host_impl_->active_tree(), 1));
2529 DidDrawCheckLayer* root = static_cast<DidDrawCheckLayer*>(
2530 host_impl_->active_tree()->root_layer());
2532 root->AddChild(DidDrawCheckLayer::Create(host_impl_->active_tree(), 2));
2533 root->SetHasRenderSurface(true);
2534 DidDrawCheckLayer* layer =
2535 static_cast<DidDrawCheckLayer*>(root->children()[0]);
2538 LayerTreeHostImpl::FrameData frame;
2539 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
2540 host_impl_->DrawLayers(&frame);
2541 host_impl_->DidDrawAllLayers(frame);
2543 EXPECT_TRUE(layer->will_draw_called());
2544 EXPECT_TRUE(layer->append_quads_called());
2545 EXPECT_TRUE(layer->did_draw_called());
2548 host_impl_->SetViewportDamage(gfx::Rect(10, 10));
2551 LayerTreeHostImpl::FrameData frame;
2553 layer->set_will_draw_returns_false();
2554 layer->ClearDidDrawCheck();
2556 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
2557 host_impl_->DrawLayers(&frame);
2558 host_impl_->DidDrawAllLayers(frame);
2560 EXPECT_TRUE(layer->will_draw_called());
2561 EXPECT_FALSE(layer->append_quads_called());
2562 EXPECT_FALSE(layer->did_draw_called());
2566 TEST_F(LayerTreeHostImplTest, DidDrawNotCalledOnHiddenLayer) {
2567 // The root layer is always drawn, so run this test on a child layer that
2568 // will be masked out by the root layer's bounds.
2569 host_impl_->active_tree()->SetRootLayer(
2570 DidDrawCheckLayer::Create(host_impl_->active_tree(), 1));
2571 DidDrawCheckLayer* root = static_cast<DidDrawCheckLayer*>(
2572 host_impl_->active_tree()->root_layer());
2573 root->SetMasksToBounds(true);
2574 root->SetHasRenderSurface(true);
2575 root->AddChild(DidDrawCheckLayer::Create(host_impl_->active_tree(), 2));
2576 DidDrawCheckLayer* layer =
2577 static_cast<DidDrawCheckLayer*>(root->children()[0]);
2578 // Ensure visible_layer_rect for layer is empty.
2579 layer->SetPosition(gfx::PointF(100.f, 100.f));
2580 layer->SetBounds(gfx::Size(10, 10));
2582 LayerTreeHostImpl::FrameData frame;
2584 EXPECT_FALSE(layer->will_draw_called());
2585 EXPECT_FALSE(layer->did_draw_called());
2587 host_impl_->active_tree()->BuildPropertyTreesForTesting();
2588 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
2589 host_impl_->DrawLayers(&frame);
2590 host_impl_->DidDrawAllLayers(frame);
2592 EXPECT_FALSE(layer->will_draw_called());
2593 EXPECT_FALSE(layer->did_draw_called());
2595 EXPECT_TRUE(layer->visible_layer_rect().IsEmpty());
2597 // Ensure visible_layer_rect for layer is not empty
2598 layer->SetPosition(gfx::PointF());
2600 EXPECT_FALSE(layer->will_draw_called());
2601 EXPECT_FALSE(layer->did_draw_called());
2603 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
2604 host_impl_->DrawLayers(&frame);
2605 host_impl_->DidDrawAllLayers(frame);
2607 EXPECT_TRUE(layer->will_draw_called());
2608 EXPECT_TRUE(layer->did_draw_called());
2610 EXPECT_FALSE(layer->visible_layer_rect().IsEmpty());
2613 TEST_F(LayerTreeHostImplTest, WillDrawNotCalledOnOccludedLayer) {
2614 gfx::Size big_size(1000, 1000);
2615 host_impl_->SetViewportSize(big_size);
2617 host_impl_->active_tree()->SetRootLayer(
2618 DidDrawCheckLayer::Create(host_impl_->active_tree(), 1));
2619 DidDrawCheckLayer* root =
2620 static_cast<DidDrawCheckLayer*>(host_impl_->active_tree()->root_layer());
2622 root->AddChild(DidDrawCheckLayer::Create(host_impl_->active_tree(), 2));
2623 DidDrawCheckLayer* occluded_layer =
2624 static_cast<DidDrawCheckLayer*>(root->children()[0]);
2626 root->AddChild(DidDrawCheckLayer::Create(host_impl_->active_tree(), 3));
2627 root->SetHasRenderSurface(true);
2628 DidDrawCheckLayer* top_layer =
2629 static_cast<DidDrawCheckLayer*>(root->children()[1]);
2630 // This layer covers the occluded_layer above. Make this layer large so it can
2631 // occlude.
2632 top_layer->SetBounds(big_size);
2633 top_layer->SetContentsOpaque(true);
2635 LayerTreeHostImpl::FrameData frame;
2637 EXPECT_FALSE(occluded_layer->will_draw_called());
2638 EXPECT_FALSE(occluded_layer->did_draw_called());
2639 EXPECT_FALSE(top_layer->will_draw_called());
2640 EXPECT_FALSE(top_layer->did_draw_called());
2642 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
2643 host_impl_->DrawLayers(&frame);
2644 host_impl_->DidDrawAllLayers(frame);
2646 EXPECT_FALSE(occluded_layer->will_draw_called());
2647 EXPECT_FALSE(occluded_layer->did_draw_called());
2648 EXPECT_TRUE(top_layer->will_draw_called());
2649 EXPECT_TRUE(top_layer->did_draw_called());
2652 TEST_F(LayerTreeHostImplTest, DidDrawCalledOnAllLayers) {
2653 host_impl_->active_tree()->SetRootLayer(
2654 DidDrawCheckLayer::Create(host_impl_->active_tree(), 1));
2655 DidDrawCheckLayer* root =
2656 static_cast<DidDrawCheckLayer*>(host_impl_->active_tree()->root_layer());
2658 root->AddChild(DidDrawCheckLayer::Create(host_impl_->active_tree(), 2));
2659 root->SetHasRenderSurface(true);
2660 DidDrawCheckLayer* layer1 =
2661 static_cast<DidDrawCheckLayer*>(root->children()[0]);
2663 layer1->AddChild(DidDrawCheckLayer::Create(host_impl_->active_tree(), 3));
2664 DidDrawCheckLayer* layer2 =
2665 static_cast<DidDrawCheckLayer*>(layer1->children()[0]);
2667 layer1->SetHasRenderSurface(true);
2668 layer1->SetShouldFlattenTransform(true);
2670 EXPECT_FALSE(root->did_draw_called());
2671 EXPECT_FALSE(layer1->did_draw_called());
2672 EXPECT_FALSE(layer2->did_draw_called());
2674 LayerTreeHostImpl::FrameData frame;
2675 FakeLayerTreeHostImpl::RecursiveUpdateNumChildren(
2676 host_impl_->active_tree()->root_layer());
2677 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
2678 host_impl_->DrawLayers(&frame);
2679 host_impl_->DidDrawAllLayers(frame);
2681 EXPECT_TRUE(root->did_draw_called());
2682 EXPECT_TRUE(layer1->did_draw_called());
2683 EXPECT_TRUE(layer2->did_draw_called());
2685 EXPECT_NE(root->render_surface(), layer1->render_surface());
2686 EXPECT_TRUE(layer1->render_surface());
2689 class MissingTextureAnimatingLayer : public DidDrawCheckLayer {
2690 public:
2691 static scoped_ptr<LayerImpl> Create(LayerTreeImpl* tree_impl,
2692 int id,
2693 bool tile_missing,
2694 bool had_incomplete_tile,
2695 bool animating,
2696 ResourceProvider* resource_provider) {
2697 return make_scoped_ptr(new MissingTextureAnimatingLayer(tree_impl,
2699 tile_missing,
2700 had_incomplete_tile,
2701 animating,
2702 resource_provider));
2705 void AppendQuads(RenderPass* render_pass,
2706 AppendQuadsData* append_quads_data) override {
2707 LayerImpl::AppendQuads(render_pass, append_quads_data);
2708 if (had_incomplete_tile_)
2709 append_quads_data->num_incomplete_tiles++;
2710 if (tile_missing_)
2711 append_quads_data->num_missing_tiles++;
2714 private:
2715 MissingTextureAnimatingLayer(LayerTreeImpl* tree_impl,
2716 int id,
2717 bool tile_missing,
2718 bool had_incomplete_tile,
2719 bool animating,
2720 ResourceProvider* resource_provider)
2721 : DidDrawCheckLayer(tree_impl, id),
2722 tile_missing_(tile_missing),
2723 had_incomplete_tile_(had_incomplete_tile) {
2724 if (animating)
2725 AddAnimatedTransformToLayer(this, 10.0, 3, 0);
2728 bool tile_missing_;
2729 bool had_incomplete_tile_;
2732 struct PrepareToDrawSuccessTestCase {
2733 struct State {
2734 bool has_missing_tile = false;
2735 bool has_incomplete_tile = false;
2736 bool is_animating = false;
2737 bool has_copy_request = false;
2739 bool high_res_required = false;
2740 State layer_before;
2741 State layer_between;
2742 State layer_after;
2743 DrawResult expected_result;
2745 explicit PrepareToDrawSuccessTestCase(DrawResult result)
2746 : expected_result(result) {}
2749 TEST_F(LayerTreeHostImplTest, PrepareToDrawSucceedsAndFails) {
2750 std::vector<PrepareToDrawSuccessTestCase> cases;
2752 // 0. Default case.
2753 cases.push_back(PrepareToDrawSuccessTestCase(DRAW_SUCCESS));
2754 // 1. Animated layer first.
2755 cases.push_back(PrepareToDrawSuccessTestCase(DRAW_SUCCESS));
2756 cases.back().layer_before.is_animating = true;
2757 // 2. Animated layer between.
2758 cases.push_back(PrepareToDrawSuccessTestCase(DRAW_SUCCESS));
2759 cases.back().layer_between.is_animating = true;
2760 // 3. Animated layer last.
2761 cases.push_back(PrepareToDrawSuccessTestCase(DRAW_SUCCESS));
2762 cases.back().layer_after.is_animating = true;
2763 // 4. Missing tile first.
2764 cases.push_back(PrepareToDrawSuccessTestCase(DRAW_SUCCESS));
2765 cases.back().layer_before.has_missing_tile = true;
2766 // 5. Missing tile between.
2767 cases.push_back(PrepareToDrawSuccessTestCase(DRAW_SUCCESS));
2768 cases.back().layer_between.has_missing_tile = true;
2769 // 6. Missing tile last.
2770 cases.push_back(PrepareToDrawSuccessTestCase(DRAW_SUCCESS));
2771 cases.back().layer_after.has_missing_tile = true;
2772 // 7. Incomplete tile first.
2773 cases.push_back(PrepareToDrawSuccessTestCase(DRAW_SUCCESS));
2774 cases.back().layer_before.has_incomplete_tile = true;
2775 // 8. Incomplete tile between.
2776 cases.push_back(PrepareToDrawSuccessTestCase(DRAW_SUCCESS));
2777 cases.back().layer_between.has_incomplete_tile = true;
2778 // 9. Incomplete tile last.
2779 cases.push_back(PrepareToDrawSuccessTestCase(DRAW_SUCCESS));
2780 cases.back().layer_after.has_incomplete_tile = true;
2781 // 10. Animation with missing tile.
2782 cases.push_back(
2783 PrepareToDrawSuccessTestCase(DRAW_ABORTED_CHECKERBOARD_ANIMATIONS));
2784 cases.back().layer_between.has_missing_tile = true;
2785 cases.back().layer_between.is_animating = true;
2786 // 11. Animation with incomplete tile.
2787 cases.push_back(PrepareToDrawSuccessTestCase(DRAW_SUCCESS));
2788 cases.back().layer_between.has_incomplete_tile = true;
2789 cases.back().layer_between.is_animating = true;
2791 // 12. High res required.
2792 cases.push_back(PrepareToDrawSuccessTestCase(DRAW_SUCCESS));
2793 cases.back().high_res_required = true;
2794 // 13. High res required with incomplete tile.
2795 cases.push_back(
2796 PrepareToDrawSuccessTestCase(DRAW_ABORTED_MISSING_HIGH_RES_CONTENT));
2797 cases.back().high_res_required = true;
2798 cases.back().layer_between.has_incomplete_tile = true;
2799 // 14. High res required with missing tile.
2800 cases.push_back(
2801 PrepareToDrawSuccessTestCase(DRAW_ABORTED_MISSING_HIGH_RES_CONTENT));
2802 cases.back().high_res_required = true;
2803 cases.back().layer_between.has_missing_tile = true;
2805 // 15. High res required is higher priority than animating missing tiles.
2806 cases.push_back(
2807 PrepareToDrawSuccessTestCase(DRAW_ABORTED_MISSING_HIGH_RES_CONTENT));
2808 cases.back().high_res_required = true;
2809 cases.back().layer_between.has_missing_tile = true;
2810 cases.back().layer_after.has_missing_tile = true;
2811 cases.back().layer_after.is_animating = true;
2812 // 16. High res required is higher priority than animating missing tiles.
2813 cases.push_back(
2814 PrepareToDrawSuccessTestCase(DRAW_ABORTED_MISSING_HIGH_RES_CONTENT));
2815 cases.back().high_res_required = true;
2816 cases.back().layer_between.has_missing_tile = true;
2817 cases.back().layer_before.has_missing_tile = true;
2818 cases.back().layer_before.is_animating = true;
2820 host_impl_->active_tree()->SetRootLayer(
2821 DidDrawCheckLayer::Create(host_impl_->active_tree(), 1));
2822 DidDrawCheckLayer* root =
2823 static_cast<DidDrawCheckLayer*>(host_impl_->active_tree()->root_layer());
2824 root->SetHasRenderSurface(true);
2826 LayerTreeHostImpl::FrameData frame;
2827 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
2828 host_impl_->DrawLayers(&frame);
2829 host_impl_->DidDrawAllLayers(frame);
2830 host_impl_->SwapBuffers(frame);
2832 for (size_t i = 0; i < cases.size(); ++i) {
2833 const auto& testcase = cases[i];
2834 std::vector<LayerImpl*> to_remove;
2835 for (auto* child : root->children())
2836 to_remove.push_back(child);
2837 for (auto* child : to_remove)
2838 root->RemoveChild(child);
2840 std::ostringstream scope;
2841 scope << "Test case: " << i;
2842 SCOPED_TRACE(scope.str());
2844 root->AddChild(MissingTextureAnimatingLayer::Create(
2845 host_impl_->active_tree(), 2, testcase.layer_before.has_missing_tile,
2846 testcase.layer_before.has_incomplete_tile,
2847 testcase.layer_before.is_animating, host_impl_->resource_provider()));
2848 DidDrawCheckLayer* before =
2849 static_cast<DidDrawCheckLayer*>(root->children().back());
2850 if (testcase.layer_before.has_copy_request)
2851 before->AddCopyRequest();
2853 root->AddChild(MissingTextureAnimatingLayer::Create(
2854 host_impl_->active_tree(), 3, testcase.layer_between.has_missing_tile,
2855 testcase.layer_between.has_incomplete_tile,
2856 testcase.layer_between.is_animating, host_impl_->resource_provider()));
2857 DidDrawCheckLayer* between =
2858 static_cast<DidDrawCheckLayer*>(root->children().back());
2859 if (testcase.layer_between.has_copy_request)
2860 between->AddCopyRequest();
2862 root->AddChild(MissingTextureAnimatingLayer::Create(
2863 host_impl_->active_tree(), 4, testcase.layer_after.has_missing_tile,
2864 testcase.layer_after.has_incomplete_tile,
2865 testcase.layer_after.is_animating, host_impl_->resource_provider()));
2866 DidDrawCheckLayer* after =
2867 static_cast<DidDrawCheckLayer*>(root->children().back());
2868 if (testcase.layer_after.has_copy_request)
2869 after->AddCopyRequest();
2871 if (testcase.high_res_required)
2872 host_impl_->SetRequiresHighResToDraw();
2874 LayerTreeHostImpl::FrameData frame;
2875 EXPECT_EQ(testcase.expected_result, host_impl_->PrepareToDraw(&frame));
2876 host_impl_->DrawLayers(&frame);
2877 host_impl_->DidDrawAllLayers(frame);
2878 host_impl_->SwapBuffers(frame);
2882 TEST_F(LayerTreeHostImplTest,
2883 PrepareToDrawWhenDrawAndSwapFullViewportEveryFrame) {
2884 CreateHostImpl(DefaultSettings(),
2885 FakeOutputSurface::CreateAlwaysDrawAndSwap3d());
2886 EXPECT_TRUE(host_impl_->output_surface()
2887 ->capabilities()
2888 .draw_and_swap_full_viewport_every_frame);
2890 std::vector<PrepareToDrawSuccessTestCase> cases;
2892 // 0. Default case.
2893 cases.push_back(PrepareToDrawSuccessTestCase(DRAW_SUCCESS));
2894 // 1. Animation with missing tile.
2895 cases.push_back(PrepareToDrawSuccessTestCase(DRAW_SUCCESS));
2896 cases.back().layer_between.has_missing_tile = true;
2897 cases.back().layer_between.is_animating = true;
2898 // 2. High res required with incomplete tile.
2899 cases.push_back(PrepareToDrawSuccessTestCase(DRAW_SUCCESS));
2900 cases.back().high_res_required = true;
2901 cases.back().layer_between.has_incomplete_tile = true;
2902 // 3. High res required with missing tile.
2903 cases.push_back(PrepareToDrawSuccessTestCase(DRAW_SUCCESS));
2904 cases.back().high_res_required = true;
2905 cases.back().layer_between.has_missing_tile = true;
2907 host_impl_->active_tree()->SetRootLayer(
2908 DidDrawCheckLayer::Create(host_impl_->active_tree(), 1));
2909 DidDrawCheckLayer* root =
2910 static_cast<DidDrawCheckLayer*>(host_impl_->active_tree()->root_layer());
2911 root->SetHasRenderSurface(true);
2913 LayerTreeHostImpl::FrameData frame;
2914 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
2915 host_impl_->DrawLayers(&frame);
2916 host_impl_->DidDrawAllLayers(frame);
2917 host_impl_->SwapBuffers(frame);
2919 for (size_t i = 0; i < cases.size(); ++i) {
2920 const auto& testcase = cases[i];
2921 std::vector<LayerImpl*> to_remove;
2922 for (auto* child : root->children())
2923 to_remove.push_back(child);
2924 for (auto* child : to_remove)
2925 root->RemoveChild(child);
2927 std::ostringstream scope;
2928 scope << "Test case: " << i;
2929 SCOPED_TRACE(scope.str());
2931 root->AddChild(MissingTextureAnimatingLayer::Create(
2932 host_impl_->active_tree(), 2, testcase.layer_before.has_missing_tile,
2933 testcase.layer_before.has_incomplete_tile,
2934 testcase.layer_before.is_animating, host_impl_->resource_provider()));
2935 DidDrawCheckLayer* before =
2936 static_cast<DidDrawCheckLayer*>(root->children().back());
2937 if (testcase.layer_before.has_copy_request)
2938 before->AddCopyRequest();
2940 root->AddChild(MissingTextureAnimatingLayer::Create(
2941 host_impl_->active_tree(), 3, testcase.layer_between.has_missing_tile,
2942 testcase.layer_between.has_incomplete_tile,
2943 testcase.layer_between.is_animating, host_impl_->resource_provider()));
2944 DidDrawCheckLayer* between =
2945 static_cast<DidDrawCheckLayer*>(root->children().back());
2946 if (testcase.layer_between.has_copy_request)
2947 between->AddCopyRequest();
2949 root->AddChild(MissingTextureAnimatingLayer::Create(
2950 host_impl_->active_tree(), 4, testcase.layer_after.has_missing_tile,
2951 testcase.layer_after.has_incomplete_tile,
2952 testcase.layer_after.is_animating, host_impl_->resource_provider()));
2953 DidDrawCheckLayer* after =
2954 static_cast<DidDrawCheckLayer*>(root->children().back());
2955 if (testcase.layer_after.has_copy_request)
2956 after->AddCopyRequest();
2958 if (testcase.high_res_required)
2959 host_impl_->SetRequiresHighResToDraw();
2961 LayerTreeHostImpl::FrameData frame;
2962 EXPECT_EQ(testcase.expected_result, host_impl_->PrepareToDraw(&frame));
2963 host_impl_->DrawLayers(&frame);
2964 host_impl_->DidDrawAllLayers(frame);
2965 host_impl_->SwapBuffers(frame);
2969 TEST_F(LayerTreeHostImplTest, ScrollRootIgnored) {
2970 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl_->active_tree(), 1);
2971 root->SetScrollClipLayer(Layer::INVALID_ID);
2972 root->SetHasRenderSurface(true);
2973 host_impl_->active_tree()->SetRootLayer(root.Pass());
2974 DrawFrame();
2976 // Scroll event is ignored because layer is not scrollable.
2977 EXPECT_EQ(InputHandler::SCROLL_IGNORED,
2978 host_impl_->ScrollBegin(gfx::Point(), InputHandler::WHEEL));
2979 EXPECT_FALSE(did_request_redraw_);
2980 EXPECT_FALSE(did_request_commit_);
2983 TEST_F(LayerTreeHostImplTest, ClampingAfterActivation) {
2984 host_impl_->CreatePendingTree();
2985 host_impl_->pending_tree()->PushPageScaleFromMainThread(1.f, 1.f, 1.f);
2986 CreateScrollAndContentsLayers(host_impl_->pending_tree(),
2987 gfx::Size(100, 100));
2988 host_impl_->ActivateSyncTree();
2990 host_impl_->CreatePendingTree();
2991 const gfx::ScrollOffset pending_scroll = gfx::ScrollOffset(-100, -100);
2992 LayerImpl* active_outer_layer =
2993 host_impl_->active_tree()->OuterViewportScrollLayer();
2994 LayerImpl* pending_outer_layer =
2995 host_impl_->pending_tree()->OuterViewportScrollLayer();
2996 pending_outer_layer->PushScrollOffsetFromMainThread(pending_scroll);
2998 host_impl_->ActivateSyncTree();
2999 // Scrolloffsets on the active tree will be clamped after activation.
3000 EXPECT_EQ(active_outer_layer->CurrentScrollOffset(), gfx::ScrollOffset(0, 0));
3003 class LayerTreeHostImplTopControlsTest : public LayerTreeHostImplTest {
3004 public:
3005 LayerTreeHostImplTopControlsTest()
3006 // Make the clip size the same as the layer (content) size so the layer is
3007 // non-scrollable.
3008 : layer_size_(10, 10),
3009 clip_size_(layer_size_),
3010 top_controls_height_(50) {
3011 viewport_size_ = gfx::Size(clip_size_.width(),
3012 clip_size_.height() + top_controls_height_);
3015 bool CreateHostImpl(const LayerTreeSettings& settings,
3016 scoped_ptr<OutputSurface> output_surface) override {
3017 bool init =
3018 LayerTreeHostImplTest::CreateHostImpl(settings, output_surface.Pass());
3019 if (init) {
3020 host_impl_->active_tree()->set_top_controls_height(top_controls_height_);
3021 host_impl_->active_tree()->SetCurrentTopControlsShownRatio(1.f);
3022 host_impl_->active_tree()->PushPageScaleFromMainThread(1.f, 1.f, 1.f);
3024 return init;
3027 void SetupTopControlsAndScrollLayerWithVirtualViewport(
3028 const gfx::Size& inner_viewport_size,
3029 const gfx::Size& outer_viewport_size,
3030 const gfx::Size& scroll_layer_size) {
3031 CreateHostImpl(settings_, CreateOutputSurface());
3032 host_impl_->sync_tree()->set_top_controls_shrink_blink_size(true);
3033 host_impl_->sync_tree()->set_top_controls_height(top_controls_height_);
3034 host_impl_->DidChangeTopControlsPosition();
3036 scoped_ptr<LayerImpl> root =
3037 LayerImpl::Create(host_impl_->active_tree(), 1);
3038 scoped_ptr<LayerImpl> root_clip =
3039 LayerImpl::Create(host_impl_->active_tree(), 2);
3040 scoped_ptr<LayerImpl> page_scale =
3041 LayerImpl::Create(host_impl_->active_tree(), 3);
3043 scoped_ptr<LayerImpl> outer_scroll =
3044 LayerImpl::Create(host_impl_->active_tree(), 4);
3045 scoped_ptr<LayerImpl> outer_clip =
3046 LayerImpl::Create(host_impl_->active_tree(), 5);
3048 root_clip->SetBounds(inner_viewport_size);
3049 root->SetScrollClipLayer(root_clip->id());
3050 root->SetBounds(outer_viewport_size);
3051 root->SetPosition(gfx::PointF());
3052 root->SetDrawsContent(false);
3053 root->SetIsContainerForFixedPositionLayers(true);
3054 root_clip->SetHasRenderSurface(true);
3055 outer_clip->SetBounds(outer_viewport_size);
3056 outer_scroll->SetScrollClipLayer(outer_clip->id());
3057 outer_scroll->SetBounds(scroll_layer_size);
3058 outer_scroll->SetPosition(gfx::PointF());
3059 outer_scroll->SetDrawsContent(false);
3060 outer_scroll->SetIsContainerForFixedPositionLayers(true);
3062 int inner_viewport_scroll_layer_id = root->id();
3063 int outer_viewport_scroll_layer_id = outer_scroll->id();
3064 int page_scale_layer_id = page_scale->id();
3066 outer_clip->AddChild(outer_scroll.Pass());
3067 root->AddChild(outer_clip.Pass());
3068 page_scale->AddChild(root.Pass());
3069 root_clip->AddChild(page_scale.Pass());
3071 host_impl_->active_tree()->SetRootLayer(root_clip.Pass());
3072 host_impl_->active_tree()->SetViewportLayersFromIds(
3073 Layer::INVALID_ID, page_scale_layer_id, inner_viewport_scroll_layer_id,
3074 outer_viewport_scroll_layer_id);
3076 host_impl_->SetViewportSize(inner_viewport_size);
3077 LayerImpl* root_clip_ptr = host_impl_->active_tree()->root_layer();
3078 EXPECT_EQ(inner_viewport_size, root_clip_ptr->bounds());
3081 protected:
3082 gfx::Size layer_size_;
3083 gfx::Size clip_size_;
3084 gfx::Size viewport_size_;
3085 float top_controls_height_;
3087 LayerTreeSettings settings_;
3088 }; // class LayerTreeHostImplTopControlsTest
3090 // Tests that, on a page with content the same size as the viewport, hiding
3091 // the top controls also increases the ScrollableSize (i.e. the content size).
3092 // Since the viewport got larger, the effective scrollable "content" also did.
3093 // This ensures, for one thing, that the overscroll glow is shown in the right
3094 // place.
3095 TEST_F(LayerTreeHostImplTopControlsTest,
3096 HidingTopControlsExpandsScrollableSize) {
3097 SetupTopControlsAndScrollLayerWithVirtualViewport(
3098 gfx::Size(50, 50), gfx::Size(50, 50), gfx::Size(50, 50));
3100 LayerTreeImpl* active_tree = host_impl_->active_tree();
3102 // Create a content layer beneath the outer viewport scroll layer.
3103 int id = host_impl_->OuterViewportScrollLayer()->id();
3104 host_impl_->OuterViewportScrollLayer()->AddChild(
3105 LayerImpl::Create(host_impl_->active_tree(), id + 2));
3106 LayerImpl* content = active_tree->OuterViewportScrollLayer()->children()[0];
3107 content->SetBounds(gfx::Size(50, 50));
3109 DrawFrame();
3111 LayerImpl* inner_container = active_tree->InnerViewportContainerLayer();
3112 LayerImpl* outer_container = active_tree->OuterViewportContainerLayer();
3114 // The top controls should start off showing so the viewport should be shrunk.
3115 ASSERT_EQ(gfx::Size(50, 50), inner_container->bounds());
3116 ASSERT_EQ(gfx::Size(50, 50), outer_container->bounds());
3118 EXPECT_EQ(gfx::SizeF(50, 50), active_tree->ScrollableSize());
3120 EXPECT_EQ(InputHandler::SCROLL_STARTED,
3121 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
3123 host_impl_->top_controls_manager()->ScrollBegin();
3125 // Hide the top controls by a bit, the scrollable size should increase but the
3126 // actual content bounds shouldn't.
3128 host_impl_->top_controls_manager()->ScrollBy(gfx::Vector2dF(0.f, 25.f));
3129 ASSERT_EQ(gfx::Size(50, 75), inner_container->bounds());
3130 ASSERT_EQ(gfx::Size(50, 75), outer_container->bounds());
3131 EXPECT_EQ(gfx::SizeF(50, 75), active_tree->ScrollableSize());
3132 EXPECT_EQ(gfx::SizeF(50, 50), content->BoundsForScrolling());
3135 // Fully hide the top controls.
3137 host_impl_->top_controls_manager()->ScrollBy(gfx::Vector2dF(0.f, 25.f));
3138 ASSERT_EQ(gfx::Size(50, 100), inner_container->bounds());
3139 ASSERT_EQ(gfx::Size(50, 100), outer_container->bounds());
3140 EXPECT_EQ(gfx::SizeF(50, 100), active_tree->ScrollableSize());
3141 EXPECT_EQ(gfx::SizeF(50, 50), content->BoundsForScrolling());
3144 // Scrolling additionally shouldn't have any effect.
3146 host_impl_->top_controls_manager()->ScrollBy(gfx::Vector2dF(0.f, 25.f));
3147 ASSERT_EQ(gfx::Size(50, 100), inner_container->bounds());
3148 ASSERT_EQ(gfx::Size(50, 100), outer_container->bounds());
3149 EXPECT_EQ(gfx::SizeF(50, 100), active_tree->ScrollableSize());
3150 EXPECT_EQ(gfx::SizeF(50, 50), content->BoundsForScrolling());
3153 host_impl_->top_controls_manager()->ScrollEnd();
3154 host_impl_->ScrollEnd();
3157 TEST_F(LayerTreeHostImplTopControlsTest, ScrollTopControlsByFractionalAmount) {
3158 SetupTopControlsAndScrollLayerWithVirtualViewport(
3159 gfx::Size(10, 10), gfx::Size(10, 10), gfx::Size(10, 10));
3160 DrawFrame();
3162 EXPECT_EQ(InputHandler::SCROLL_STARTED,
3163 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
3165 // Make the test scroll delta a fractional amount, to verify that the
3166 // fixed container size delta is (1) non-zero, and (2) fractional, and
3167 // (3) matches the movement of the top controls.
3168 gfx::Vector2dF top_controls_scroll_delta(0.f, 5.25f);
3169 host_impl_->top_controls_manager()->ScrollBegin();
3170 host_impl_->top_controls_manager()->ScrollBy(top_controls_scroll_delta);
3171 host_impl_->top_controls_manager()->ScrollEnd();
3173 LayerImpl* inner_viewport_scroll_layer =
3174 host_impl_->active_tree()->InnerViewportScrollLayer();
3175 DCHECK(inner_viewport_scroll_layer);
3176 host_impl_->ScrollEnd();
3177 EXPECT_FLOAT_EQ(top_controls_scroll_delta.y(),
3178 inner_viewport_scroll_layer->FixedContainerSizeDelta().y());
3181 // In this test, the outer viewport is initially unscrollable. We test that a
3182 // scroll initiated on the inner viewport, causing the top controls to show and
3183 // thus making the outer viewport scrollable, still scrolls the outer viewport.
3184 TEST_F(LayerTreeHostImplTopControlsTest,
3185 TopControlsOuterViewportBecomesScrollable) {
3186 SetupTopControlsAndScrollLayerWithVirtualViewport(
3187 gfx::Size(10, 50), gfx::Size(10, 50), gfx::Size(10, 100));
3188 DrawFrame();
3190 LayerImpl* inner_scroll =
3191 host_impl_->active_tree()->InnerViewportScrollLayer();
3192 LayerImpl* inner_container =
3193 host_impl_->active_tree()->InnerViewportContainerLayer();
3194 LayerImpl* outer_scroll =
3195 host_impl_->active_tree()->OuterViewportScrollLayer();
3196 LayerImpl* outer_container =
3197 host_impl_->active_tree()->OuterViewportContainerLayer();
3199 // Need SetDrawsContent so ScrollBegin's hit test finds an actual layer.
3200 outer_scroll->SetDrawsContent(true);
3201 host_impl_->active_tree()->PushPageScaleFromMainThread(2.f, 1.f, 2.f);
3203 EXPECT_EQ(InputHandler::SCROLL_STARTED,
3204 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
3205 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2dF(0.f, 50.f));
3207 // The entire scroll delta should have been used to hide the top controls.
3208 // The viewport layers should be resized back to their full sizes.
3209 EXPECT_EQ(0.f, host_impl_->active_tree()->CurrentTopControlsShownRatio());
3210 EXPECT_EQ(0.f, inner_scroll->CurrentScrollOffset().y());
3211 EXPECT_EQ(100.f, inner_container->BoundsForScrolling().height());
3212 EXPECT_EQ(100.f, outer_container->BoundsForScrolling().height());
3214 // The inner viewport should be scrollable by 50px * page_scale.
3215 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2dF(0.f, 100.f));
3216 EXPECT_EQ(50.f, inner_scroll->CurrentScrollOffset().y());
3217 EXPECT_EQ(0.f, outer_scroll->CurrentScrollOffset().y());
3218 EXPECT_EQ(gfx::ScrollOffset(), outer_scroll->MaxScrollOffset());
3220 host_impl_->ScrollEnd();
3222 EXPECT_EQ(InputHandler::SCROLL_STARTED,
3223 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
3224 EXPECT_EQ(host_impl_->CurrentlyScrollingLayer(), inner_scroll);
3226 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2dF(0.f, -50.f));
3228 // The entire scroll delta should have been used to show the top controls.
3229 // The outer viewport should be resized to accomodate and scrolled to the
3230 // bottom of the document to keep the viewport in place.
3231 EXPECT_EQ(1.f, host_impl_->active_tree()->CurrentTopControlsShownRatio());
3232 EXPECT_EQ(50.f, outer_container->BoundsForScrolling().height());
3233 EXPECT_EQ(50.f, inner_container->BoundsForScrolling().height());
3234 EXPECT_EQ(25.f, outer_scroll->CurrentScrollOffset().y());
3235 EXPECT_EQ(25.f, inner_scroll->CurrentScrollOffset().y());
3237 // Now when we continue scrolling, make sure the outer viewport gets scrolled
3238 // since it wasn't scrollable when the scroll began.
3239 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2dF(0.f, -20.f));
3240 EXPECT_EQ(25.f, outer_scroll->CurrentScrollOffset().y());
3241 EXPECT_EQ(15.f, inner_scroll->CurrentScrollOffset().y());
3243 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2dF(0.f, -30.f));
3244 EXPECT_EQ(25.f, outer_scroll->CurrentScrollOffset().y());
3245 EXPECT_EQ(0.f, inner_scroll->CurrentScrollOffset().y());
3247 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2dF(0.f, -50.f));
3248 host_impl_->ScrollEnd();
3250 EXPECT_EQ(0.f, outer_scroll->CurrentScrollOffset().y());
3251 EXPECT_EQ(0.f, inner_scroll->CurrentScrollOffset().y());
3254 // Test that the fixed position container delta is appropriately adjusted
3255 // by the top controls showing/hiding and page scale doesn't affect it.
3256 TEST_F(LayerTreeHostImplTopControlsTest, FixedContainerDelta) {
3257 SetupTopControlsAndScrollLayerWithVirtualViewport(
3258 gfx::Size(100, 100), gfx::Size(100, 100), gfx::Size(100, 100));
3259 DrawFrame();
3260 host_impl_->active_tree()->PushPageScaleFromMainThread(1.f, 1.f, 2.f);
3262 float page_scale = 1.5f;
3263 LayerImpl* outer_viewport_scroll_layer =
3264 host_impl_->active_tree()->OuterViewportScrollLayer();
3266 // Zoom in, since the fixed container is the outer viewport, the delta should
3267 // not be scaled.
3268 host_impl_->active_tree()->PushPageScaleFromMainThread(page_scale, 1.f, 2.f);
3270 EXPECT_EQ(InputHandler::SCROLL_STARTED,
3271 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
3273 // Scroll down, the top controls hiding should expand the viewport size so
3274 // the delta should be equal to the scroll distance.
3275 gfx::Vector2dF top_controls_scroll_delta(0.f, 20.f);
3276 host_impl_->top_controls_manager()->ScrollBegin();
3277 host_impl_->top_controls_manager()->ScrollBy(top_controls_scroll_delta);
3278 EXPECT_FLOAT_EQ(top_controls_height_ - top_controls_scroll_delta.y(),
3279 host_impl_->top_controls_manager()->ContentTopOffset());
3280 EXPECT_FLOAT_EQ(top_controls_scroll_delta.y(),
3281 outer_viewport_scroll_layer->FixedContainerSizeDelta().y());
3282 host_impl_->ScrollEnd();
3284 // Scroll past the maximum extent. The delta shouldn't be greater than the
3285 // top controls height.
3286 host_impl_->top_controls_manager()->ScrollBegin();
3287 host_impl_->top_controls_manager()->ScrollBy(top_controls_scroll_delta);
3288 host_impl_->top_controls_manager()->ScrollBy(top_controls_scroll_delta);
3289 host_impl_->top_controls_manager()->ScrollBy(top_controls_scroll_delta);
3290 EXPECT_EQ(0.f, host_impl_->top_controls_manager()->ContentTopOffset());
3291 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, top_controls_height_),
3292 outer_viewport_scroll_layer->FixedContainerSizeDelta());
3293 host_impl_->ScrollEnd();
3295 // Scroll in the direction to make the top controls show.
3296 host_impl_->top_controls_manager()->ScrollBegin();
3297 host_impl_->top_controls_manager()->ScrollBy(-top_controls_scroll_delta);
3298 EXPECT_EQ(top_controls_scroll_delta.y(),
3299 host_impl_->top_controls_manager()->ContentTopOffset());
3300 EXPECT_VECTOR_EQ(
3301 gfx::Vector2dF(0, top_controls_height_ - top_controls_scroll_delta.y()),
3302 outer_viewport_scroll_layer->FixedContainerSizeDelta());
3303 host_impl_->top_controls_manager()->ScrollEnd();
3306 // Test that if only the top controls are scrolled, we shouldn't request a
3307 // commit.
3308 TEST_F(LayerTreeHostImplTopControlsTest, TopControlsDontTriggerCommit) {
3309 SetupTopControlsAndScrollLayerWithVirtualViewport(
3310 gfx::Size(100, 50), gfx::Size(100, 100), gfx::Size(100, 100));
3311 DrawFrame();
3313 // Show top controls
3314 EXPECT_EQ(1.f, host_impl_->active_tree()->CurrentTopControlsShownRatio());
3316 // Scroll 25px to hide top controls
3317 gfx::Vector2dF scroll_delta(0.f, 25.f);
3318 EXPECT_EQ(InputHandler::SCROLL_STARTED,
3319 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
3320 host_impl_->ScrollBy(gfx::Point(), scroll_delta);
3321 EXPECT_FALSE(did_request_commit_);
3324 // Test that if a scrollable sublayer doesn't consume the scroll,
3325 // top controls should hide when scrolling down.
3326 TEST_F(LayerTreeHostImplTopControlsTest, TopControlsScrollableSublayer) {
3327 gfx::Size sub_content_size(100, 400);
3328 gfx::Size sub_content_layer_size(100, 300);
3329 SetupTopControlsAndScrollLayerWithVirtualViewport(
3330 gfx::Size(100, 50), gfx::Size(100, 100), gfx::Size(100, 100));
3331 DrawFrame();
3333 // Show top controls
3334 EXPECT_EQ(1.f, host_impl_->active_tree()->CurrentTopControlsShownRatio());
3336 LayerImpl* outer_viewport_scroll_layer =
3337 host_impl_->active_tree()->OuterViewportScrollLayer();
3338 int id = outer_viewport_scroll_layer->id();
3340 scoped_ptr<LayerImpl> child =
3341 LayerImpl::Create(host_impl_->active_tree(), id + 2);
3342 scoped_ptr<LayerImpl> child_clip =
3343 LayerImpl::Create(host_impl_->active_tree(), id + 3);
3345 child_clip->SetBounds(sub_content_layer_size);
3346 child->SetScrollClipLayer(child_clip->id());
3347 child->SetBounds(sub_content_size);
3348 child->SetPosition(gfx::PointF());
3349 child->SetDrawsContent(true);
3350 child->SetIsContainerForFixedPositionLayers(true);
3352 // scroll child to limit
3353 child->SetScrollDelta(gfx::Vector2dF(0, 100.f));
3354 child_clip->AddChild(child.Pass());
3355 outer_viewport_scroll_layer->AddChild(child_clip.Pass());
3357 // Scroll 25px to hide top controls
3358 gfx::Vector2dF scroll_delta(0.f, 25.f);
3359 EXPECT_EQ(InputHandler::SCROLL_STARTED,
3360 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
3361 host_impl_->ScrollBy(gfx::Point(), scroll_delta);
3362 host_impl_->ScrollEnd();
3364 // Top controls should be hidden
3365 EXPECT_EQ(scroll_delta.y(),
3366 top_controls_height_ -
3367 host_impl_->top_controls_manager()->ContentTopOffset());
3370 // Ensure setting the top controls position explicitly using the setters on the
3371 // TreeImpl correctly affects the top controls manager and viewport bounds.
3372 TEST_F(LayerTreeHostImplTopControlsTest, PositionTopControlsExplicitly) {
3373 CreateHostImpl(settings_, CreateOutputSurface());
3374 SetupTopControlsAndScrollLayerWithVirtualViewport(
3375 layer_size_, layer_size_, layer_size_);
3376 DrawFrame();
3378 host_impl_->active_tree()->SetCurrentTopControlsShownRatio(0.f);
3379 host_impl_->active_tree()->top_controls_shown_ratio()->PushFromMainThread(
3380 30.f / top_controls_height_);
3381 host_impl_->active_tree()->top_controls_shown_ratio()->PushPendingToActive();
3382 EXPECT_FLOAT_EQ(30.f, host_impl_->top_controls_manager()->ContentTopOffset());
3383 EXPECT_FLOAT_EQ(-20.f,
3384 host_impl_->top_controls_manager()->ControlsTopOffset());
3386 host_impl_->active_tree()->SetCurrentTopControlsShownRatio(0.f);
3387 EXPECT_FLOAT_EQ(0.f, host_impl_->top_controls_manager()->ContentTopOffset());
3388 EXPECT_FLOAT_EQ(-50.f,
3389 host_impl_->top_controls_manager()->ControlsTopOffset());
3391 host_impl_->DidChangeTopControlsPosition();
3393 // Now that top controls have moved, expect the clip to resize.
3394 LayerImpl* inner_clip_ptr =
3395 host_impl_->InnerViewportScrollLayer()->parent()->parent();
3396 EXPECT_EQ(viewport_size_, inner_clip_ptr->bounds());
3399 // Test that the top_controls delta and sent delta are appropriately
3400 // applied on sync tree activation. The total top controls offset shouldn't
3401 // change after the activation.
3402 TEST_F(LayerTreeHostImplTopControlsTest, ApplyDeltaOnTreeActivation) {
3403 CreateHostImpl(settings_, CreateOutputSurface());
3404 SetupTopControlsAndScrollLayerWithVirtualViewport(
3405 layer_size_, layer_size_, layer_size_);
3406 DrawFrame();
3408 host_impl_->active_tree()->top_controls_shown_ratio()->PushFromMainThread(
3409 20.f / top_controls_height_);
3410 host_impl_->active_tree()->top_controls_shown_ratio()->PushPendingToActive();
3411 host_impl_->active_tree()->SetCurrentTopControlsShownRatio(
3412 15.f / top_controls_height_);
3413 host_impl_->active_tree()
3414 ->top_controls_shown_ratio()
3415 ->PullDeltaForMainThread();
3416 host_impl_->active_tree()->SetCurrentTopControlsShownRatio(0.f);
3417 host_impl_->sync_tree()->PushTopControlsFromMainThread(15.f /
3418 top_controls_height_);
3420 host_impl_->DidChangeTopControlsPosition();
3421 LayerImpl* inner_clip_ptr =
3422 host_impl_->InnerViewportScrollLayer()->parent()->parent();
3423 EXPECT_EQ(viewport_size_, inner_clip_ptr->bounds());
3424 EXPECT_EQ(0.f, host_impl_->top_controls_manager()->ContentTopOffset());
3426 host_impl_->ActivateSyncTree();
3428 inner_clip_ptr = host_impl_->InnerViewportScrollLayer()->parent()->parent();
3429 EXPECT_EQ(0.f, host_impl_->top_controls_manager()->ContentTopOffset());
3430 EXPECT_EQ(viewport_size_, inner_clip_ptr->bounds());
3432 EXPECT_FLOAT_EQ(
3433 -15.f, host_impl_->active_tree()->top_controls_shown_ratio()->Delta() *
3434 top_controls_height_);
3435 EXPECT_FLOAT_EQ(
3436 15.f,
3437 host_impl_->active_tree()->top_controls_shown_ratio()->ActiveBase() *
3438 top_controls_height_);
3441 // Test that changing the top controls layout height is correctly applied to
3442 // the inner viewport container bounds. That is, the top controls layout
3443 // height is the amount that the inner viewport container was shrunk outside
3444 // the compositor to accommodate the top controls.
3445 TEST_F(LayerTreeHostImplTopControlsTest, TopControlsLayoutHeightChanged) {
3446 CreateHostImpl(settings_, CreateOutputSurface());
3447 SetupTopControlsAndScrollLayerWithVirtualViewport(
3448 layer_size_, layer_size_, layer_size_);
3449 DrawFrame();
3451 host_impl_->sync_tree()->PushTopControlsFromMainThread(1.f);
3452 host_impl_->sync_tree()->set_top_controls_shrink_blink_size(true);
3454 host_impl_->active_tree()->top_controls_shown_ratio()->PushFromMainThread(
3455 1.f);
3456 host_impl_->active_tree()->top_controls_shown_ratio()->PushPendingToActive();
3457 host_impl_->active_tree()->SetCurrentTopControlsShownRatio(0.f);
3459 host_impl_->DidChangeTopControlsPosition();
3460 LayerImpl* inner_clip_ptr =
3461 host_impl_->InnerViewportScrollLayer()->parent()->parent();
3462 EXPECT_EQ(viewport_size_, inner_clip_ptr->bounds());
3463 EXPECT_EQ(0.f, host_impl_->top_controls_manager()->ContentTopOffset());
3465 host_impl_->sync_tree()->root_layer()->SetBounds(
3466 gfx::Size(inner_clip_ptr->bounds().width(),
3467 inner_clip_ptr->bounds().height() - 50.f));
3469 host_impl_->ActivateSyncTree();
3471 inner_clip_ptr =
3472 host_impl_->InnerViewportScrollLayer()->parent()->parent();
3473 EXPECT_EQ(0.f, host_impl_->top_controls_manager()->ContentTopOffset());
3475 // The total bounds should remain unchanged since the bounds delta should
3476 // account for the difference between the layout height and the current
3477 // top controls offset.
3478 EXPECT_EQ(viewport_size_, inner_clip_ptr->bounds());
3479 EXPECT_VECTOR_EQ(gfx::Vector2dF(0.f, 50.f), inner_clip_ptr->bounds_delta());
3481 host_impl_->active_tree()->SetCurrentTopControlsShownRatio(1.f);
3482 host_impl_->DidChangeTopControlsPosition();
3484 EXPECT_EQ(1.f, host_impl_->top_controls_manager()->TopControlsShownRatio());
3485 EXPECT_EQ(50.f, host_impl_->top_controls_manager()->TopControlsHeight());
3486 EXPECT_EQ(50.f, host_impl_->top_controls_manager()->ContentTopOffset());
3487 EXPECT_VECTOR_EQ(gfx::Vector2dF(0.f, 0.f), inner_clip_ptr->bounds_delta());
3488 EXPECT_EQ(gfx::Size(viewport_size_.width(), viewport_size_.height() - 50.f),
3489 inner_clip_ptr->bounds());
3492 // Test that showing/hiding the top controls when the viewport is fully scrolled
3493 // doesn't incorrectly change the viewport offset due to clamping from changing
3494 // viewport bounds.
3495 TEST_F(LayerTreeHostImplTopControlsTest, TopControlsViewportOffsetClamping) {
3496 SetupTopControlsAndScrollLayerWithVirtualViewport(
3497 gfx::Size(100, 100), gfx::Size(200, 200), gfx::Size(200, 400));
3498 DrawFrame();
3500 EXPECT_EQ(1.f, host_impl_->active_tree()->CurrentTopControlsShownRatio());
3502 LayerImpl* outer_scroll = host_impl_->OuterViewportScrollLayer();
3503 LayerImpl* inner_scroll = host_impl_->InnerViewportScrollLayer();
3505 // Scroll the viewports to max scroll offset.
3506 outer_scroll->SetScrollDelta(gfx::Vector2dF(0, 200.f));
3507 inner_scroll->SetScrollDelta(gfx::Vector2dF(100, 100.f));
3509 gfx::ScrollOffset viewport_offset =
3510 host_impl_->active_tree()->TotalScrollOffset();
3511 EXPECT_EQ(host_impl_->active_tree()->TotalMaxScrollOffset(), viewport_offset);
3513 // Hide the top controls by 25px.
3514 gfx::Vector2dF scroll_delta(0.f, 25.f);
3515 EXPECT_EQ(InputHandler::SCROLL_STARTED,
3516 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
3517 host_impl_->ScrollBy(gfx::Point(), scroll_delta);
3519 // scrolling down at the max extents no longer hides the top controls
3520 EXPECT_EQ(1.f, host_impl_->active_tree()->CurrentTopControlsShownRatio());
3522 // forcefully hide the top controls by 25px
3523 host_impl_->top_controls_manager()->ScrollBy(scroll_delta);
3524 host_impl_->ScrollEnd();
3526 EXPECT_FLOAT_EQ(scroll_delta.y(),
3527 top_controls_height_ -
3528 host_impl_->top_controls_manager()->ContentTopOffset());
3530 inner_scroll->ClampScrollToMaxScrollOffset();
3531 outer_scroll->ClampScrollToMaxScrollOffset();
3533 // We should still be fully scrolled.
3534 EXPECT_EQ(host_impl_->active_tree()->TotalMaxScrollOffset(),
3535 host_impl_->active_tree()->TotalScrollOffset());
3537 viewport_offset = host_impl_->active_tree()->TotalScrollOffset();
3539 // Bring the top controls down by 25px.
3540 scroll_delta = gfx::Vector2dF(0.f, -25.f);
3541 EXPECT_EQ(InputHandler::SCROLL_STARTED,
3542 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
3543 host_impl_->ScrollBy(gfx::Point(), scroll_delta);
3544 host_impl_->ScrollEnd();
3546 // The viewport offset shouldn't have changed.
3547 EXPECT_EQ(viewport_offset,
3548 host_impl_->active_tree()->TotalScrollOffset());
3550 // Scroll the viewports to max scroll offset.
3551 outer_scroll->SetScrollDelta(gfx::Vector2dF(0, 200.f));
3552 inner_scroll->SetScrollDelta(gfx::Vector2dF(100, 100.f));
3553 EXPECT_EQ(host_impl_->active_tree()->TotalMaxScrollOffset(),
3554 host_impl_->active_tree()->TotalScrollOffset());
3557 // Test that the top controls coming in and out maintains the same aspect ratio
3558 // between the inner and outer viewports.
3559 TEST_F(LayerTreeHostImplTopControlsTest, TopControlsAspectRatio) {
3560 SetupTopControlsAndScrollLayerWithVirtualViewport(
3561 gfx::Size(100, 100), gfx::Size(200, 200), gfx::Size(200, 400));
3562 host_impl_->active_tree()->PushPageScaleFromMainThread(1.f, 0.5f, 2.f);
3563 DrawFrame();
3565 EXPECT_FLOAT_EQ(top_controls_height_,
3566 host_impl_->top_controls_manager()->ContentTopOffset());
3568 gfx::Vector2dF scroll_delta(0.f, 25.f);
3569 EXPECT_EQ(InputHandler::SCROLL_STARTED,
3570 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
3571 host_impl_->ScrollBy(gfx::Point(), scroll_delta);
3572 host_impl_->ScrollEnd();
3574 EXPECT_FLOAT_EQ(scroll_delta.y(),
3575 top_controls_height_ -
3576 host_impl_->top_controls_manager()->ContentTopOffset());
3578 // Top controls were hidden by 25px so the inner viewport should have expanded
3579 // by that much.
3580 LayerImpl* outer_container =
3581 host_impl_->active_tree()->OuterViewportContainerLayer();
3582 LayerImpl* inner_container =
3583 host_impl_->active_tree()->InnerViewportContainerLayer();
3584 EXPECT_EQ(gfx::Size(100, 100+25), inner_container->BoundsForScrolling());
3586 // Outer viewport should match inner's aspect ratio. The bounds are ceiled.
3587 float aspect_ratio = inner_container->BoundsForScrolling().width() /
3588 inner_container->BoundsForScrolling().height();
3589 gfx::Size expected = gfx::ToCeiledSize(gfx::SizeF(200, 200 / aspect_ratio));
3590 EXPECT_EQ(expected, outer_container->BoundsForScrolling());
3591 EXPECT_EQ(expected,
3592 host_impl_->InnerViewportScrollLayer()->BoundsForScrolling());
3595 // Test that scrolling the outer viewport affects the top controls.
3596 TEST_F(LayerTreeHostImplTopControlsTest, TopControlsScrollOuterViewport) {
3597 SetupTopControlsAndScrollLayerWithVirtualViewport(
3598 gfx::Size(100, 100), gfx::Size(200, 200), gfx::Size(200, 400));
3599 DrawFrame();
3601 EXPECT_EQ(top_controls_height_,
3602 host_impl_->top_controls_manager()->ContentTopOffset());
3604 // Send a gesture scroll that will scroll the outer viewport, make sure the
3605 // top controls get scrolled.
3606 gfx::Vector2dF scroll_delta(0.f, 15.f);
3607 EXPECT_EQ(InputHandler::SCROLL_STARTED,
3608 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
3609 host_impl_->ScrollBy(gfx::Point(), scroll_delta);
3610 EXPECT_EQ(host_impl_->OuterViewportScrollLayer(),
3611 host_impl_->CurrentlyScrollingLayer());
3612 host_impl_->ScrollEnd();
3614 EXPECT_FLOAT_EQ(scroll_delta.y(),
3615 top_controls_height_ -
3616 host_impl_->top_controls_manager()->ContentTopOffset());
3618 scroll_delta = gfx::Vector2dF(0.f, 50.f);
3619 EXPECT_EQ(InputHandler::SCROLL_STARTED,
3620 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
3621 host_impl_->ScrollBy(gfx::Point(), scroll_delta);
3623 EXPECT_EQ(0, host_impl_->top_controls_manager()->ContentTopOffset());
3624 EXPECT_EQ(host_impl_->OuterViewportScrollLayer(),
3625 host_impl_->CurrentlyScrollingLayer());
3627 host_impl_->ScrollEnd();
3629 // Position the viewports such that the inner viewport will be scrolled.
3630 gfx::Vector2dF inner_viewport_offset(0.f, 25.f);
3631 host_impl_->OuterViewportScrollLayer()->SetScrollDelta(gfx::Vector2dF());
3632 host_impl_->InnerViewportScrollLayer()->SetScrollDelta(inner_viewport_offset);
3634 scroll_delta = gfx::Vector2dF(0.f, -65.f);
3635 EXPECT_EQ(InputHandler::SCROLL_STARTED,
3636 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
3637 host_impl_->ScrollBy(gfx::Point(), scroll_delta);
3639 EXPECT_EQ(top_controls_height_,
3640 host_impl_->top_controls_manager()->ContentTopOffset());
3641 EXPECT_FLOAT_EQ(
3642 inner_viewport_offset.y() + (scroll_delta.y() + top_controls_height_),
3643 host_impl_->InnerViewportScrollLayer()->ScrollDelta().y());
3645 host_impl_->ScrollEnd();
3648 TEST_F(LayerTreeHostImplTopControlsTest,
3649 ScrollNonScrollableRootWithTopControls) {
3650 CreateHostImpl(settings_, CreateOutputSurface());
3651 SetupTopControlsAndScrollLayerWithVirtualViewport(
3652 layer_size_, layer_size_, layer_size_);
3653 DrawFrame();
3655 EXPECT_EQ(InputHandler::SCROLL_STARTED,
3656 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
3658 host_impl_->top_controls_manager()->ScrollBegin();
3659 host_impl_->top_controls_manager()->ScrollBy(gfx::Vector2dF(0.f, 50.f));
3660 host_impl_->top_controls_manager()->ScrollEnd();
3661 EXPECT_EQ(0.f, host_impl_->top_controls_manager()->ContentTopOffset());
3662 // Now that top controls have moved, expect the clip to resize.
3663 LayerImpl* inner_clip_ptr =
3664 host_impl_->InnerViewportScrollLayer()->parent()->parent();
3665 EXPECT_EQ(viewport_size_, inner_clip_ptr->bounds());
3667 host_impl_->ScrollEnd();
3669 EXPECT_EQ(InputHandler::SCROLL_STARTED,
3670 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
3672 float scroll_increment_y = -25.f;
3673 host_impl_->top_controls_manager()->ScrollBegin();
3674 host_impl_->top_controls_manager()->ScrollBy(
3675 gfx::Vector2dF(0.f, scroll_increment_y));
3676 EXPECT_FLOAT_EQ(-scroll_increment_y,
3677 host_impl_->top_controls_manager()->ContentTopOffset());
3678 // Now that top controls have moved, expect the clip to resize.
3679 EXPECT_EQ(gfx::Size(viewport_size_.width(),
3680 viewport_size_.height() + scroll_increment_y),
3681 inner_clip_ptr->bounds());
3683 host_impl_->top_controls_manager()->ScrollBy(
3684 gfx::Vector2dF(0.f, scroll_increment_y));
3685 host_impl_->top_controls_manager()->ScrollEnd();
3686 EXPECT_FLOAT_EQ(-2 * scroll_increment_y,
3687 host_impl_->top_controls_manager()->ContentTopOffset());
3688 // Now that top controls have moved, expect the clip to resize.
3689 EXPECT_EQ(clip_size_, inner_clip_ptr->bounds());
3691 host_impl_->ScrollEnd();
3693 // Verify the layer is once-again non-scrollable.
3694 EXPECT_EQ(
3695 gfx::ScrollOffset(),
3696 host_impl_->active_tree()->InnerViewportScrollLayer()->MaxScrollOffset());
3698 EXPECT_EQ(InputHandler::SCROLL_STARTED,
3699 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
3702 TEST_F(LayerTreeHostImplTest, ScrollNonCompositedRoot) {
3703 // Test the configuration where a non-composited root layer is embedded in a
3704 // scrollable outer layer.
3705 gfx::Size surface_size(10, 10);
3706 gfx::Size contents_size(20, 20);
3708 scoped_ptr<LayerImpl> content_layer =
3709 LayerImpl::Create(host_impl_->active_tree(), 1);
3710 content_layer->SetDrawsContent(true);
3711 content_layer->SetPosition(gfx::PointF());
3712 content_layer->SetBounds(contents_size);
3714 scoped_ptr<LayerImpl> scroll_clip_layer =
3715 LayerImpl::Create(host_impl_->active_tree(), 3);
3716 scroll_clip_layer->SetBounds(surface_size);
3718 scoped_ptr<LayerImpl> scroll_layer =
3719 LayerImpl::Create(host_impl_->active_tree(), 2);
3720 scroll_layer->SetScrollClipLayer(3);
3721 scroll_layer->SetBounds(contents_size);
3722 scroll_layer->SetPosition(gfx::PointF());
3723 scroll_layer->AddChild(content_layer.Pass());
3724 scroll_clip_layer->AddChild(scroll_layer.Pass());
3726 scroll_clip_layer->SetHasRenderSurface(true);
3727 host_impl_->active_tree()->SetRootLayer(scroll_clip_layer.Pass());
3728 host_impl_->SetViewportSize(surface_size);
3729 DrawFrame();
3731 EXPECT_EQ(InputHandler::SCROLL_STARTED,
3732 host_impl_->ScrollBegin(gfx::Point(5, 5), InputHandler::WHEEL));
3733 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(0, 10));
3734 host_impl_->ScrollEnd();
3735 EXPECT_TRUE(did_request_redraw_);
3736 EXPECT_TRUE(did_request_commit_);
3739 TEST_F(LayerTreeHostImplTest, ScrollChildCallsCommitAndRedraw) {
3740 gfx::Size surface_size(10, 10);
3741 gfx::Size contents_size(20, 20);
3742 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl_->active_tree(), 1);
3743 root->SetBounds(surface_size);
3744 root->AddChild(CreateScrollableLayer(2, contents_size, root.get()));
3745 root->SetHasRenderSurface(true);
3746 host_impl_->active_tree()->SetRootLayer(root.Pass());
3747 host_impl_->SetViewportSize(surface_size);
3748 DrawFrame();
3750 EXPECT_EQ(InputHandler::SCROLL_STARTED,
3751 host_impl_->ScrollBegin(gfx::Point(5, 5), InputHandler::WHEEL));
3752 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(0, 10));
3753 host_impl_->ScrollEnd();
3754 EXPECT_TRUE(did_request_redraw_);
3755 EXPECT_TRUE(did_request_commit_);
3758 TEST_F(LayerTreeHostImplTest, ScrollMissesChild) {
3759 gfx::Size surface_size(10, 10);
3760 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl_->active_tree(), 1);
3761 root->AddChild(CreateScrollableLayer(2, surface_size, root.get()));
3762 root->SetHasRenderSurface(true);
3763 host_impl_->active_tree()->SetRootLayer(root.Pass());
3764 host_impl_->SetViewportSize(surface_size);
3765 DrawFrame();
3767 // Scroll event is ignored because the input coordinate is outside the layer
3768 // boundaries.
3769 EXPECT_EQ(InputHandler::SCROLL_IGNORED,
3770 host_impl_->ScrollBegin(gfx::Point(15, 5), InputHandler::WHEEL));
3771 EXPECT_FALSE(did_request_redraw_);
3772 EXPECT_FALSE(did_request_commit_);
3775 TEST_F(LayerTreeHostImplTest, ScrollMissesBackfacingChild) {
3776 gfx::Size surface_size(10, 10);
3777 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl_->active_tree(), 1);
3778 root->SetHasRenderSurface(true);
3779 scoped_ptr<LayerImpl> child =
3780 CreateScrollableLayer(2, surface_size, root.get());
3781 host_impl_->SetViewportSize(surface_size);
3783 gfx::Transform matrix;
3784 matrix.RotateAboutXAxis(180.0);
3785 child->SetTransform(matrix);
3786 child->SetDoubleSided(false);
3788 root->AddChild(child.Pass());
3789 host_impl_->active_tree()->SetRootLayer(root.Pass());
3790 DrawFrame();
3792 // Scroll event is ignored because the scrollable layer is not facing the
3793 // viewer and there is nothing scrollable behind it.
3794 EXPECT_EQ(InputHandler::SCROLL_IGNORED,
3795 host_impl_->ScrollBegin(gfx::Point(5, 5), InputHandler::WHEEL));
3796 EXPECT_FALSE(did_request_redraw_);
3797 EXPECT_FALSE(did_request_commit_);
3800 TEST_F(LayerTreeHostImplTest, ScrollBlockedByContentLayer) {
3801 gfx::Size surface_size(10, 10);
3802 scoped_ptr<LayerImpl> clip_layer =
3803 LayerImpl::Create(host_impl_->active_tree(), 3);
3804 scoped_ptr<LayerImpl> content_layer =
3805 CreateScrollableLayer(1, surface_size, clip_layer.get());
3806 content_layer->SetShouldScrollOnMainThread(true);
3807 content_layer->SetScrollClipLayer(Layer::INVALID_ID);
3809 // Note: we can use the same clip layer for both since both calls to
3810 // CreateScrollableLayer() use the same surface size.
3811 scoped_ptr<LayerImpl> scroll_layer =
3812 CreateScrollableLayer(2, surface_size, clip_layer.get());
3813 scroll_layer->AddChild(content_layer.Pass());
3814 clip_layer->AddChild(scroll_layer.Pass());
3815 clip_layer->SetHasRenderSurface(true);
3817 host_impl_->active_tree()->SetRootLayer(clip_layer.Pass());
3818 host_impl_->SetViewportSize(surface_size);
3819 DrawFrame();
3821 // Scrolling fails because the content layer is asking to be scrolled on the
3822 // main thread.
3823 EXPECT_EQ(InputHandler::SCROLL_ON_MAIN_THREAD,
3824 host_impl_->ScrollBegin(gfx::Point(5, 5), InputHandler::WHEEL));
3827 TEST_F(LayerTreeHostImplTest, ScrollRootAndChangePageScaleOnMainThread) {
3828 gfx::Size viewport_size(20, 20);
3829 float page_scale = 2.f;
3831 SetupScrollAndContentsLayers(viewport_size);
3833 // Setup the layers so that the outer viewport is scrollable.
3834 host_impl_->active_tree()->InnerViewportScrollLayer()->parent()->SetBounds(
3835 viewport_size);
3836 host_impl_->active_tree()->OuterViewportScrollLayer()->SetBounds(
3837 gfx::Size(40, 40));
3838 host_impl_->active_tree()->PushPageScaleFromMainThread(1.f, 1.f, 2.f);
3839 DrawFrame();
3841 LayerImpl* root_scroll =
3842 host_impl_->active_tree()->OuterViewportScrollLayer();
3843 LayerImpl* inner_scroll =
3844 host_impl_->active_tree()->InnerViewportScrollLayer();
3845 EXPECT_EQ(viewport_size, root_scroll->scroll_clip_layer()->bounds());
3847 gfx::Vector2d scroll_delta(0, 10);
3848 gfx::Vector2d expected_scroll_delta = scroll_delta;
3849 gfx::ScrollOffset expected_max_scroll = root_scroll->MaxScrollOffset();
3850 EXPECT_EQ(InputHandler::SCROLL_STARTED,
3851 host_impl_->ScrollBegin(gfx::Point(5, 5), InputHandler::WHEEL));
3852 host_impl_->ScrollBy(gfx::Point(), scroll_delta);
3853 host_impl_->ScrollEnd();
3855 // Set new page scale from main thread.
3856 host_impl_->active_tree()->PushPageScaleFromMainThread(page_scale, 1.f, 2.f);
3858 scoped_ptr<ScrollAndScaleSet> scroll_info = host_impl_->ProcessScrollDeltas();
3859 EXPECT_TRUE(ScrollInfoContains(*scroll_info.get(), inner_scroll->id(),
3860 expected_scroll_delta));
3862 // The scroll range should also have been updated.
3863 EXPECT_EQ(expected_max_scroll, root_scroll->MaxScrollOffset());
3865 // The page scale delta remains constant because the impl thread did not
3866 // scale.
3867 EXPECT_EQ(1.f, host_impl_->active_tree()->page_scale_delta());
3870 TEST_F(LayerTreeHostImplTest, ScrollRootAndChangePageScaleOnImplThread) {
3871 gfx::Size viewport_size(20, 20);
3872 float page_scale = 2.f;
3874 SetupScrollAndContentsLayers(viewport_size);
3876 // Setup the layers so that the outer viewport is scrollable.
3877 host_impl_->active_tree()->InnerViewportScrollLayer()->parent()->SetBounds(
3878 viewport_size);
3879 host_impl_->active_tree()->OuterViewportScrollLayer()->SetBounds(
3880 gfx::Size(40, 40));
3881 host_impl_->active_tree()->PushPageScaleFromMainThread(1.f, 1.f, 2.f);
3882 DrawFrame();
3884 LayerImpl* root_scroll =
3885 host_impl_->active_tree()->OuterViewportScrollLayer();
3886 LayerImpl* inner_scroll =
3887 host_impl_->active_tree()->InnerViewportScrollLayer();
3888 EXPECT_EQ(viewport_size, root_scroll->scroll_clip_layer()->bounds());
3890 gfx::Vector2d scroll_delta(0, 10);
3891 gfx::Vector2d expected_scroll_delta = scroll_delta;
3892 gfx::ScrollOffset expected_max_scroll = root_scroll->MaxScrollOffset();
3893 EXPECT_EQ(InputHandler::SCROLL_STARTED,
3894 host_impl_->ScrollBegin(gfx::Point(5, 5), InputHandler::WHEEL));
3895 host_impl_->ScrollBy(gfx::Point(), scroll_delta);
3896 host_impl_->ScrollEnd();
3898 // Set new page scale on impl thread by pinching.
3899 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE);
3900 host_impl_->PinchGestureBegin();
3901 host_impl_->PinchGestureUpdate(page_scale, gfx::Point());
3902 host_impl_->PinchGestureEnd();
3903 host_impl_->ScrollEnd();
3904 DrawOneFrame();
3906 // The scroll delta is not scaled because the main thread did not scale.
3907 scoped_ptr<ScrollAndScaleSet> scroll_info = host_impl_->ProcessScrollDeltas();
3908 EXPECT_TRUE(ScrollInfoContains(*scroll_info.get(), inner_scroll->id(),
3909 expected_scroll_delta));
3911 // The scroll range should also have been updated.
3912 EXPECT_EQ(expected_max_scroll, root_scroll->MaxScrollOffset());
3914 // The page scale delta should match the new scale on the impl side.
3915 EXPECT_EQ(page_scale, host_impl_->active_tree()->current_page_scale_factor());
3918 TEST_F(LayerTreeHostImplTest, PageScaleDeltaAppliedToRootScrollLayerOnly) {
3919 host_impl_->active_tree()->PushPageScaleFromMainThread(1.f, 1.f, 2.f);
3920 gfx::Size surface_size(10, 10);
3921 float default_page_scale = 1.f;
3922 gfx::Transform default_page_scale_matrix;
3923 default_page_scale_matrix.Scale(default_page_scale, default_page_scale);
3925 float new_page_scale = 2.f;
3926 gfx::Transform new_page_scale_matrix;
3927 new_page_scale_matrix.Scale(new_page_scale, new_page_scale);
3929 // Create a normal scrollable root layer and another scrollable child layer.
3930 LayerImpl* scroll = SetupScrollAndContentsLayers(surface_size);
3931 LayerImpl* root = host_impl_->active_tree()->root_layer();
3932 LayerImpl* child = scroll->children()[0];
3934 scoped_ptr<LayerImpl> scrollable_child_clip =
3935 LayerImpl::Create(host_impl_->active_tree(), 6);
3936 scoped_ptr<LayerImpl> scrollable_child =
3937 CreateScrollableLayer(7, surface_size, scrollable_child_clip.get());
3938 scrollable_child_clip->AddChild(scrollable_child.Pass());
3939 child->AddChild(scrollable_child_clip.Pass());
3940 LayerImpl* grand_child = child->children()[0];
3942 // Set new page scale on impl thread by pinching.
3943 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE);
3944 host_impl_->PinchGestureBegin();
3945 host_impl_->PinchGestureUpdate(new_page_scale, gfx::Point());
3946 host_impl_->PinchGestureEnd();
3947 host_impl_->ScrollEnd();
3948 DrawOneFrame();
3950 // Make sure all the layers are drawn with the page scale delta applied, i.e.,
3951 // the page scale delta on the root layer is applied hierarchically.
3952 LayerTreeHostImpl::FrameData frame;
3953 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
3954 host_impl_->DrawLayers(&frame);
3955 host_impl_->DidDrawAllLayers(frame);
3957 EXPECT_EQ(1.f, root->draw_transform().matrix().getDouble(0, 0));
3958 EXPECT_EQ(1.f, root->draw_transform().matrix().getDouble(1, 1));
3959 EXPECT_EQ(new_page_scale, scroll->draw_transform().matrix().getDouble(0, 0));
3960 EXPECT_EQ(new_page_scale, scroll->draw_transform().matrix().getDouble(1, 1));
3961 EXPECT_EQ(new_page_scale, child->draw_transform().matrix().getDouble(0, 0));
3962 EXPECT_EQ(new_page_scale, child->draw_transform().matrix().getDouble(1, 1));
3963 EXPECT_EQ(new_page_scale,
3964 grand_child->draw_transform().matrix().getDouble(0, 0));
3965 EXPECT_EQ(new_page_scale,
3966 grand_child->draw_transform().matrix().getDouble(1, 1));
3969 TEST_F(LayerTreeHostImplTest, ScrollChildAndChangePageScaleOnMainThread) {
3970 SetupScrollAndContentsLayers(gfx::Size(30, 30));
3972 LayerImpl* outer_scroll = host_impl_->OuterViewportScrollLayer();
3973 LayerImpl* inner_scroll = host_impl_->InnerViewportScrollLayer();
3975 // Make the outer scroll layer scrollable.
3976 outer_scroll->SetBounds(gfx::Size(50, 50));
3978 DrawFrame();
3980 gfx::Vector2d scroll_delta(0, 10);
3981 gfx::Vector2d expected_scroll_delta(scroll_delta);
3982 gfx::ScrollOffset expected_max_scroll(outer_scroll->MaxScrollOffset());
3983 EXPECT_EQ(InputHandler::SCROLL_STARTED,
3984 host_impl_->ScrollBegin(gfx::Point(5, 5), InputHandler::WHEEL));
3985 host_impl_->ScrollBy(gfx::Point(), scroll_delta);
3986 host_impl_->ScrollEnd();
3988 float page_scale = 2.f;
3989 host_impl_->active_tree()->PushPageScaleFromMainThread(page_scale, 1.f,
3990 page_scale);
3992 DrawOneFrame();
3994 scoped_ptr<ScrollAndScaleSet> scroll_info = host_impl_->ProcessScrollDeltas();
3995 EXPECT_TRUE(ScrollInfoContains(*scroll_info.get(), inner_scroll->id(),
3996 expected_scroll_delta));
3998 // The scroll range should not have changed.
3999 EXPECT_EQ(outer_scroll->MaxScrollOffset(), expected_max_scroll);
4001 // The page scale delta remains constant because the impl thread did not
4002 // scale.
4003 EXPECT_EQ(1.f, host_impl_->active_tree()->page_scale_delta());
4006 TEST_F(LayerTreeHostImplTest, ScrollChildBeyondLimit) {
4007 // Scroll a child layer beyond its maximum scroll range and make sure the
4008 // parent layer is scrolled on the axis on which the child was unable to
4009 // scroll.
4010 gfx::Size surface_size(10, 10);
4011 gfx::Size content_size(20, 20);
4012 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl_->active_tree(), 1);
4013 root->SetBounds(surface_size);
4014 root->SetHasRenderSurface(true);
4015 scoped_ptr<LayerImpl> grand_child =
4016 CreateScrollableLayer(3, content_size, root.get());
4018 scoped_ptr<LayerImpl> child =
4019 CreateScrollableLayer(2, content_size, root.get());
4020 LayerImpl* grand_child_layer = grand_child.get();
4021 child->AddChild(grand_child.Pass());
4023 LayerImpl* child_layer = child.get();
4024 root->AddChild(child.Pass());
4025 host_impl_->active_tree()->SetRootLayer(root.Pass());
4026 host_impl_->active_tree()->DidBecomeActive();
4027 host_impl_->SetViewportSize(surface_size);
4028 grand_child_layer->PushScrollOffsetFromMainThread(gfx::ScrollOffset(0, 5));
4029 child_layer->PushScrollOffsetFromMainThread(gfx::ScrollOffset(3, 0));
4031 DrawFrame();
4033 gfx::Vector2d scroll_delta(-8, -7);
4034 EXPECT_EQ(InputHandler::SCROLL_STARTED,
4035 host_impl_->ScrollBegin(gfx::Point(), InputHandler::WHEEL));
4036 host_impl_->ScrollBy(gfx::Point(), scroll_delta);
4037 host_impl_->ScrollEnd();
4039 scoped_ptr<ScrollAndScaleSet> scroll_info =
4040 host_impl_->ProcessScrollDeltas();
4042 // The grand child should have scrolled up to its limit.
4043 LayerImpl* child = host_impl_->active_tree()->root_layer()->children()[0];
4044 LayerImpl* grand_child = child->children()[0];
4045 EXPECT_TRUE(ScrollInfoContains(*scroll_info.get(), grand_child->id(),
4046 gfx::Vector2d(0, -5)));
4048 // The child should have only scrolled on the other axis.
4049 EXPECT_TRUE(ScrollInfoContains(*scroll_info.get(), child->id(),
4050 gfx::Vector2d(-3, 0)));
4054 TEST_F(LayerTreeHostImplTest, ScrollWithoutBubbling) {
4055 // Scroll a child layer beyond its maximum scroll range and make sure the
4056 // the scroll doesn't bubble up to the parent layer.
4057 gfx::Size surface_size(20, 20);
4058 gfx::Size viewport_size(10, 10);
4059 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl_->active_tree(), 1);
4060 root->SetHasRenderSurface(true);
4061 scoped_ptr<LayerImpl> root_scrolling =
4062 CreateScrollableLayer(2, surface_size, root.get());
4063 root_scrolling->SetIsContainerForFixedPositionLayers(true);
4065 scoped_ptr<LayerImpl> grand_child =
4066 CreateScrollableLayer(4, surface_size, root.get());
4068 scoped_ptr<LayerImpl> child =
4069 CreateScrollableLayer(3, surface_size, root.get());
4070 LayerImpl* grand_child_layer = grand_child.get();
4071 child->AddChild(grand_child.Pass());
4073 LayerImpl* child_layer = child.get();
4074 root_scrolling->AddChild(child.Pass());
4075 root->AddChild(root_scrolling.Pass());
4076 EXPECT_EQ(viewport_size, root->bounds());
4077 host_impl_->active_tree()->SetRootLayer(root.Pass());
4078 host_impl_->active_tree()->SetViewportLayersFromIds(Layer::INVALID_ID, 1, 2,
4079 Layer::INVALID_ID);
4080 host_impl_->active_tree()->DidBecomeActive();
4081 host_impl_->SetViewportSize(viewport_size);
4083 grand_child_layer->PushScrollOffsetFromMainThread(gfx::ScrollOffset(0, 2));
4084 child_layer->PushScrollOffsetFromMainThread(gfx::ScrollOffset(0, 3));
4086 DrawFrame();
4088 gfx::Vector2d scroll_delta(0, -10);
4089 EXPECT_EQ(InputHandler::SCROLL_STARTED,
4090 host_impl_->ScrollBegin(gfx::Point(),
4091 InputHandler::NON_BUBBLING_GESTURE));
4092 host_impl_->ScrollBy(gfx::Point(), scroll_delta);
4093 host_impl_->ScrollEnd();
4095 scoped_ptr<ScrollAndScaleSet> scroll_info =
4096 host_impl_->ProcessScrollDeltas();
4098 // The grand child should have scrolled up to its limit.
4099 LayerImpl* child =
4100 host_impl_->active_tree()->root_layer()->children()[0]->children()[0];
4101 LayerImpl* grand_child = child->children()[0];
4102 EXPECT_TRUE(ScrollInfoContains(*scroll_info.get(), grand_child->id(),
4103 gfx::Vector2d(0, -2)));
4105 // The child should not have scrolled.
4106 ExpectNone(*scroll_info.get(), child->id());
4108 // The next time we scroll we should only scroll the parent.
4109 scroll_delta = gfx::Vector2d(0, -3);
4110 EXPECT_EQ(InputHandler::SCROLL_STARTED,
4111 host_impl_->ScrollBegin(gfx::Point(5, 5),
4112 InputHandler::NON_BUBBLING_GESTURE));
4113 EXPECT_EQ(host_impl_->CurrentlyScrollingLayer(), grand_child);
4114 host_impl_->ScrollBy(gfx::Point(), scroll_delta);
4115 EXPECT_EQ(host_impl_->CurrentlyScrollingLayer(), child);
4116 host_impl_->ScrollEnd();
4118 scroll_info = host_impl_->ProcessScrollDeltas();
4120 // The child should have scrolled up to its limit.
4121 EXPECT_TRUE(ScrollInfoContains(*scroll_info.get(), child->id(),
4122 gfx::Vector2d(0, -3)));
4124 // The grand child should not have scrolled.
4125 EXPECT_TRUE(ScrollInfoContains(*scroll_info.get(), grand_child->id(),
4126 gfx::Vector2d(0, -2)));
4128 // After scrolling the parent, another scroll on the opposite direction
4129 // should still scroll the child.
4130 scroll_delta = gfx::Vector2d(0, 7);
4131 EXPECT_EQ(InputHandler::SCROLL_STARTED,
4132 host_impl_->ScrollBegin(gfx::Point(5, 5),
4133 InputHandler::NON_BUBBLING_GESTURE));
4134 EXPECT_EQ(host_impl_->CurrentlyScrollingLayer(), grand_child);
4135 host_impl_->ScrollBy(gfx::Point(), scroll_delta);
4136 EXPECT_EQ(host_impl_->CurrentlyScrollingLayer(), grand_child);
4137 host_impl_->ScrollEnd();
4139 scroll_info = host_impl_->ProcessScrollDeltas();
4141 // The grand child should have scrolled.
4142 EXPECT_TRUE(ScrollInfoContains(*scroll_info.get(), grand_child->id(),
4143 gfx::Vector2d(0, 5)));
4145 // The child should not have scrolled.
4146 EXPECT_TRUE(ScrollInfoContains(*scroll_info.get(), child->id(),
4147 gfx::Vector2d(0, -3)));
4149 // Scrolling should be adjusted from viewport space.
4150 host_impl_->active_tree()->PushPageScaleFromMainThread(2.f, 2.f, 2.f);
4151 host_impl_->active_tree()->SetPageScaleOnActiveTree(2.f);
4153 scroll_delta = gfx::Vector2d(0, -2);
4154 EXPECT_EQ(InputHandler::SCROLL_STARTED,
4155 host_impl_->ScrollBegin(gfx::Point(1, 1),
4156 InputHandler::NON_BUBBLING_GESTURE));
4157 EXPECT_EQ(grand_child, host_impl_->CurrentlyScrollingLayer());
4158 host_impl_->ScrollBy(gfx::Point(), scroll_delta);
4159 host_impl_->ScrollEnd();
4161 scroll_info = host_impl_->ProcessScrollDeltas();
4163 // Should have scrolled by half the amount in layer space (5 - 2/2)
4164 EXPECT_TRUE(ScrollInfoContains(*scroll_info.get(), grand_child->id(),
4165 gfx::Vector2d(0, 4)));
4168 TEST_F(LayerTreeHostImplTest, ScrollEventBubbling) {
4169 // When we try to scroll a non-scrollable child layer, the scroll delta
4170 // should be applied to one of its ancestors if possible.
4171 gfx::Size surface_size(10, 10);
4172 gfx::Size content_size(20, 20);
4173 scoped_ptr<LayerImpl> root_clip =
4174 LayerImpl::Create(host_impl_->active_tree(), 3);
4175 root_clip->SetHasRenderSurface(true);
4176 scoped_ptr<LayerImpl> root =
4177 CreateScrollableLayer(1, content_size, root_clip.get());
4178 // Make 'root' the clip layer for child: since they have the same sizes the
4179 // child will have zero max_scroll_offset and scrolls will bubble.
4180 scoped_ptr<LayerImpl> child =
4181 CreateScrollableLayer(2, content_size, root.get());
4182 child->SetIsContainerForFixedPositionLayers(true);
4183 root->SetBounds(content_size);
4185 int root_scroll_id = root->id();
4186 root->AddChild(child.Pass());
4187 root_clip->AddChild(root.Pass());
4189 host_impl_->SetViewportSize(surface_size);
4190 host_impl_->active_tree()->SetRootLayer(root_clip.Pass());
4191 host_impl_->active_tree()->SetViewportLayersFromIds(Layer::INVALID_ID, 3, 2,
4192 Layer::INVALID_ID);
4193 host_impl_->active_tree()->DidBecomeActive();
4194 DrawFrame();
4196 gfx::Vector2d scroll_delta(0, 4);
4197 EXPECT_EQ(InputHandler::SCROLL_STARTED,
4198 host_impl_->ScrollBegin(gfx::Point(5, 5), InputHandler::WHEEL));
4199 host_impl_->ScrollBy(gfx::Point(), scroll_delta);
4200 host_impl_->ScrollEnd();
4202 scoped_ptr<ScrollAndScaleSet> scroll_info =
4203 host_impl_->ProcessScrollDeltas();
4205 // Only the root scroll should have scrolled.
4206 ASSERT_EQ(scroll_info->scrolls.size(), 1u);
4207 EXPECT_TRUE(
4208 ScrollInfoContains(*scroll_info.get(), root_scroll_id, scroll_delta));
4212 TEST_F(LayerTreeHostImplTest, ScrollBeforeRedraw) {
4213 gfx::Size surface_size(10, 10);
4214 scoped_ptr<LayerImpl> root_clip =
4215 LayerImpl::Create(host_impl_->active_tree(), 1);
4216 scoped_ptr<LayerImpl> root_scroll =
4217 CreateScrollableLayer(2, surface_size, root_clip.get());
4218 root_scroll->SetIsContainerForFixedPositionLayers(true);
4219 root_clip->SetHasRenderSurface(true);
4220 root_clip->AddChild(root_scroll.Pass());
4221 host_impl_->active_tree()->SetRootLayer(root_clip.Pass());
4222 host_impl_->active_tree()->SetViewportLayersFromIds(Layer::INVALID_ID, 1, 2,
4223 Layer::INVALID_ID);
4224 host_impl_->active_tree()->DidBecomeActive();
4225 host_impl_->SetViewportSize(surface_size);
4227 // Draw one frame and then immediately rebuild the layer tree to mimic a tree
4228 // synchronization.
4229 DrawFrame();
4230 host_impl_->active_tree()->DetachLayerTree();
4231 scoped_ptr<LayerImpl> root_clip2 =
4232 LayerImpl::Create(host_impl_->active_tree(), 3);
4233 scoped_ptr<LayerImpl> root_scroll2 =
4234 CreateScrollableLayer(4, surface_size, root_clip2.get());
4235 root_scroll2->SetIsContainerForFixedPositionLayers(true);
4236 root_clip2->AddChild(root_scroll2.Pass());
4237 root_clip2->SetHasRenderSurface(true);
4238 host_impl_->active_tree()->SetRootLayer(root_clip2.Pass());
4239 host_impl_->active_tree()->SetViewportLayersFromIds(Layer::INVALID_ID, 3, 4,
4240 Layer::INVALID_ID);
4241 host_impl_->active_tree()->DidBecomeActive();
4243 // Scrolling should still work even though we did not draw yet.
4244 EXPECT_EQ(InputHandler::SCROLL_STARTED,
4245 host_impl_->ScrollBegin(gfx::Point(5, 5), InputHandler::WHEEL));
4248 TEST_F(LayerTreeHostImplTest, ScrollAxisAlignedRotatedLayer) {
4249 LayerImpl* scroll_layer = SetupScrollAndContentsLayers(gfx::Size(100, 100));
4251 // Rotate the root layer 90 degrees counter-clockwise about its center.
4252 gfx::Transform rotate_transform;
4253 rotate_transform.Rotate(-90.0);
4254 host_impl_->active_tree()->root_layer()->SetTransform(rotate_transform);
4256 gfx::Size surface_size(50, 50);
4257 host_impl_->SetViewportSize(surface_size);
4258 DrawFrame();
4260 // Scroll to the right in screen coordinates with a gesture.
4261 gfx::Vector2d gesture_scroll_delta(10, 0);
4262 EXPECT_EQ(InputHandler::SCROLL_STARTED,
4263 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
4264 host_impl_->ScrollBy(gfx::Point(), gesture_scroll_delta);
4265 host_impl_->ScrollEnd();
4267 // The layer should have scrolled down in its local coordinates.
4268 scoped_ptr<ScrollAndScaleSet> scroll_info = host_impl_->ProcessScrollDeltas();
4269 EXPECT_TRUE(ScrollInfoContains(*scroll_info.get(), scroll_layer->id(),
4270 gfx::Vector2d(0, gesture_scroll_delta.x())));
4272 // Reset and scroll down with the wheel.
4273 scroll_layer->SetScrollDelta(gfx::Vector2dF());
4274 gfx::Vector2d wheel_scroll_delta(0, 10);
4275 EXPECT_EQ(InputHandler::SCROLL_STARTED,
4276 host_impl_->ScrollBegin(gfx::Point(), InputHandler::WHEEL));
4277 host_impl_->ScrollBy(gfx::Point(), wheel_scroll_delta);
4278 host_impl_->ScrollEnd();
4280 // The layer should have scrolled down in its local coordinates.
4281 scroll_info = host_impl_->ProcessScrollDeltas();
4282 EXPECT_TRUE(ScrollInfoContains(*scroll_info.get(), scroll_layer->id(),
4283 wheel_scroll_delta));
4286 TEST_F(LayerTreeHostImplTest, ScrollNonAxisAlignedRotatedLayer) {
4287 LayerImpl* scroll_layer = SetupScrollAndContentsLayers(gfx::Size(100, 100));
4288 int child_clip_layer_id = 6;
4289 int child_layer_id = 7;
4290 float child_layer_angle = -20.f;
4292 // Create a child layer that is rotated to a non-axis-aligned angle.
4293 scoped_ptr<LayerImpl> clip_layer =
4294 LayerImpl::Create(host_impl_->active_tree(), child_clip_layer_id);
4295 scoped_ptr<LayerImpl> child = CreateScrollableLayer(
4296 child_layer_id, scroll_layer->bounds(), clip_layer.get());
4297 gfx::Transform rotate_transform;
4298 rotate_transform.Translate(-50.0, -50.0);
4299 rotate_transform.Rotate(child_layer_angle);
4300 rotate_transform.Translate(50.0, 50.0);
4301 clip_layer->SetTransform(rotate_transform);
4303 // Only allow vertical scrolling.
4304 clip_layer->SetBounds(
4305 gfx::Size(child->bounds().width(), child->bounds().height() / 2));
4306 // The rotation depends on the layer's transform origin, and the child layer
4307 // is a different size than the clip, so make sure the clip layer's origin
4308 // lines up over the child.
4309 clip_layer->SetTransformOrigin(gfx::Point3F(
4310 clip_layer->bounds().width() * 0.5f, clip_layer->bounds().height(), 0.f));
4311 LayerImpl* child_ptr = child.get();
4312 clip_layer->AddChild(child.Pass());
4313 scroll_layer->AddChild(clip_layer.Pass());
4315 gfx::Size surface_size(50, 50);
4316 host_impl_->SetViewportSize(surface_size);
4317 DrawFrame();
4319 // Scroll down in screen coordinates with a gesture.
4320 gfx::Vector2d gesture_scroll_delta(0, 10);
4321 EXPECT_EQ(InputHandler::SCROLL_STARTED,
4322 host_impl_->ScrollBegin(gfx::Point(1, 1), InputHandler::GESTURE));
4323 host_impl_->ScrollBy(gfx::Point(), gesture_scroll_delta);
4324 host_impl_->ScrollEnd();
4326 // The child layer should have scrolled down in its local coordinates an
4327 // amount proportional to the angle between it and the input scroll delta.
4328 gfx::Vector2d expected_scroll_delta(
4329 0, gesture_scroll_delta.y() *
4330 std::cos(MathUtil::Deg2Rad(child_layer_angle)));
4331 scoped_ptr<ScrollAndScaleSet> scroll_info =
4332 host_impl_->ProcessScrollDeltas();
4333 EXPECT_TRUE(ScrollInfoContains(*scroll_info.get(), child_layer_id,
4334 expected_scroll_delta));
4336 // The root scroll layer should not have scrolled, because the input delta
4337 // was close to the layer's axis of movement.
4338 EXPECT_EQ(scroll_info->scrolls.size(), 1u);
4341 // Now reset and scroll the same amount horizontally.
4342 child_ptr->SetScrollDelta(gfx::Vector2dF());
4343 gfx::Vector2d gesture_scroll_delta(10, 0);
4344 EXPECT_EQ(InputHandler::SCROLL_STARTED,
4345 host_impl_->ScrollBegin(gfx::Point(1, 1), InputHandler::GESTURE));
4346 host_impl_->ScrollBy(gfx::Point(), gesture_scroll_delta);
4347 host_impl_->ScrollEnd();
4349 // The child layer should have scrolled down in its local coordinates an
4350 // amount proportional to the angle between it and the input scroll delta.
4351 gfx::Vector2d expected_scroll_delta(
4352 0, -gesture_scroll_delta.x() *
4353 std::sin(MathUtil::Deg2Rad(child_layer_angle)));
4354 scoped_ptr<ScrollAndScaleSet> scroll_info =
4355 host_impl_->ProcessScrollDeltas();
4356 EXPECT_TRUE(ScrollInfoContains(*scroll_info.get(), child_layer_id,
4357 expected_scroll_delta));
4359 // The root scroll layer should have scrolled more, since the input scroll
4360 // delta was mostly orthogonal to the child layer's vertical scroll axis.
4361 gfx::Vector2d expected_root_scroll_delta(
4362 gesture_scroll_delta.x() *
4363 std::pow(std::cos(MathUtil::Deg2Rad(child_layer_angle)), 2),
4365 EXPECT_TRUE(ScrollInfoContains(*scroll_info.get(), scroll_layer->id(),
4366 expected_root_scroll_delta));
4370 TEST_F(LayerTreeHostImplTest, ScrollPerspectiveTransformedLayer) {
4371 // When scrolling an element with perspective, the distance scrolled
4372 // depends on the point at which the scroll begins.
4373 LayerImpl* scroll_layer = SetupScrollAndContentsLayers(gfx::Size(100, 100));
4374 int child_clip_layer_id = 6;
4375 int child_layer_id = 7;
4377 // Create a child layer that is rotated on its x axis, with perspective.
4378 scoped_ptr<LayerImpl> clip_layer =
4379 LayerImpl::Create(host_impl_->active_tree(), child_clip_layer_id);
4380 scoped_ptr<LayerImpl> child = CreateScrollableLayer(
4381 child_layer_id, scroll_layer->bounds(), clip_layer.get());
4382 LayerImpl* child_ptr = child.get();
4383 gfx::Transform perspective_transform;
4384 perspective_transform.Translate(-50.0, -50.0);
4385 perspective_transform.ApplyPerspectiveDepth(20);
4386 perspective_transform.RotateAboutXAxis(45);
4387 perspective_transform.Translate(50.0, 50.0);
4388 clip_layer->SetTransform(perspective_transform);
4390 clip_layer->SetBounds(gfx::Size(child_ptr->bounds().width() / 2,
4391 child_ptr->bounds().height() / 2));
4392 // The transform depends on the layer's transform origin, and the child layer
4393 // is a different size than the clip, so make sure the clip layer's origin
4394 // lines up over the child.
4395 clip_layer->SetTransformOrigin(gfx::Point3F(
4396 clip_layer->bounds().width(), clip_layer->bounds().height(), 0.f));
4397 clip_layer->AddChild(child.Pass());
4398 scroll_layer->AddChild(clip_layer.Pass());
4400 gfx::Size surface_size(50, 50);
4401 host_impl_->SetViewportSize(surface_size);
4403 scoped_ptr<ScrollAndScaleSet> scroll_info;
4405 gfx::Vector2d gesture_scroll_deltas[4];
4406 gesture_scroll_deltas[0] = gfx::Vector2d(4, 10);
4407 gesture_scroll_deltas[1] = gfx::Vector2d(4, 10);
4408 gesture_scroll_deltas[2] = gfx::Vector2d(10, 0);
4409 gesture_scroll_deltas[3] = gfx::Vector2d(10, 0);
4411 gfx::Vector2d expected_scroll_deltas[4];
4412 // Perspective affects the vertical delta by a different
4413 // amount depending on the vertical position of the |viewport_point|.
4414 expected_scroll_deltas[0] = gfx::Vector2d(2, 8);
4415 expected_scroll_deltas[1] = gfx::Vector2d(1, 4);
4416 // Deltas which start with the same vertical position of the
4417 // |viewport_point| are subject to identical perspective effects.
4418 expected_scroll_deltas[2] = gfx::Vector2d(4, 0);
4419 expected_scroll_deltas[3] = gfx::Vector2d(4, 0);
4421 gfx::Point viewport_point(1, 1);
4423 // Scroll in screen coordinates with a gesture. Each scroll starts
4424 // where the previous scroll ended, but the scroll position is reset
4425 // for each scroll.
4426 for (int i = 0; i < 4; ++i) {
4427 child_ptr->SetScrollDelta(gfx::Vector2dF());
4428 DrawFrame();
4429 EXPECT_EQ(InputHandler::SCROLL_STARTED,
4430 host_impl_->ScrollBegin(viewport_point, InputHandler::GESTURE));
4431 host_impl_->ScrollBy(viewport_point, gesture_scroll_deltas[i]);
4432 viewport_point += gesture_scroll_deltas[i];
4433 host_impl_->ScrollEnd();
4435 scroll_info = host_impl_->ProcessScrollDeltas();
4436 EXPECT_TRUE(ScrollInfoContains(*scroll_info.get(), child_layer_id,
4437 expected_scroll_deltas[i]));
4439 // The root scroll layer should not have scrolled, because the input delta
4440 // was close to the layer's axis of movement.
4441 EXPECT_EQ(scroll_info->scrolls.size(), 1u);
4445 TEST_F(LayerTreeHostImplTest, ScrollScaledLayer) {
4446 LayerImpl* scroll_layer =
4447 SetupScrollAndContentsLayers(gfx::Size(100, 100));
4449 // Scale the layer to twice its normal size.
4450 int scale = 2;
4451 gfx::Transform scale_transform;
4452 scale_transform.Scale(scale, scale);
4453 scroll_layer->SetTransform(scale_transform);
4455 gfx::Size surface_size(50, 50);
4456 host_impl_->SetViewportSize(surface_size);
4457 DrawFrame();
4459 // Scroll down in screen coordinates with a gesture.
4460 gfx::Vector2d scroll_delta(0, 10);
4461 EXPECT_EQ(InputHandler::SCROLL_STARTED,
4462 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
4463 host_impl_->ScrollBy(gfx::Point(), scroll_delta);
4464 host_impl_->ScrollEnd();
4466 // The layer should have scrolled down in its local coordinates, but half the
4467 // amount.
4468 scoped_ptr<ScrollAndScaleSet> scroll_info = host_impl_->ProcessScrollDeltas();
4469 EXPECT_TRUE(ScrollInfoContains(*scroll_info.get(), scroll_layer->id(),
4470 gfx::Vector2d(0, scroll_delta.y() / scale)));
4472 // Reset and scroll down with the wheel.
4473 scroll_layer->SetScrollDelta(gfx::Vector2dF());
4474 gfx::Vector2d wheel_scroll_delta(0, 10);
4475 EXPECT_EQ(InputHandler::SCROLL_STARTED,
4476 host_impl_->ScrollBegin(gfx::Point(), InputHandler::WHEEL));
4477 host_impl_->ScrollBy(gfx::Point(), wheel_scroll_delta);
4478 host_impl_->ScrollEnd();
4480 // It should apply the scale factor to the scroll delta for the wheel event.
4481 scroll_info = host_impl_->ProcessScrollDeltas();
4482 EXPECT_TRUE(ScrollInfoContains(*scroll_info.get(), scroll_layer->id(),
4483 wheel_scroll_delta));
4486 TEST_F(LayerTreeHostImplTest, ScrollViewportRounding) {
4487 int width = 332;
4488 int height = 20;
4489 int scale = 3;
4490 SetupScrollAndContentsLayers(gfx::Size(width, height));
4491 host_impl_->active_tree()->InnerViewportContainerLayer()->SetBounds(
4492 gfx::Size(width * scale - 1, height * scale));
4493 host_impl_->SetDeviceScaleFactor(scale);
4494 host_impl_->active_tree()->PushPageScaleFromMainThread(1.f, 0.5f, 4.f);
4496 LayerImpl* inner_viewport_scroll_layer =
4497 host_impl_->active_tree()->InnerViewportScrollLayer();
4498 EXPECT_EQ(gfx::ScrollOffset(0, 0),
4499 inner_viewport_scroll_layer->MaxScrollOffset());
4502 class TestScrollOffsetDelegate : public LayerScrollOffsetDelegate {
4503 public:
4504 TestScrollOffsetDelegate()
4505 : page_scale_factor_(0.f),
4506 min_page_scale_factor_(-1.f),
4507 max_page_scale_factor_(-1.f) {}
4509 ~TestScrollOffsetDelegate() override {}
4511 void UpdateRootLayerState(const gfx::ScrollOffset& total_scroll_offset,
4512 const gfx::ScrollOffset& max_scroll_offset,
4513 const gfx::SizeF& scrollable_size,
4514 float page_scale_factor,
4515 float min_page_scale_factor,
4516 float max_page_scale_factor) override {
4517 DCHECK(total_scroll_offset.x() <= max_scroll_offset.x());
4518 DCHECK(total_scroll_offset.y() <= max_scroll_offset.y());
4519 last_set_scroll_offset_ = total_scroll_offset;
4520 max_scroll_offset_ = max_scroll_offset;
4521 scrollable_size_ = scrollable_size;
4522 page_scale_factor_ = page_scale_factor;
4523 min_page_scale_factor_ = min_page_scale_factor;
4524 max_page_scale_factor_ = max_page_scale_factor;
4527 gfx::ScrollOffset last_set_scroll_offset() {
4528 return last_set_scroll_offset_;
4531 gfx::ScrollOffset max_scroll_offset() const {
4532 return max_scroll_offset_;
4535 gfx::SizeF scrollable_size() const {
4536 return scrollable_size_;
4539 float page_scale_factor() const {
4540 return page_scale_factor_;
4543 float min_page_scale_factor() const {
4544 return min_page_scale_factor_;
4547 float max_page_scale_factor() const {
4548 return max_page_scale_factor_;
4551 private:
4552 gfx::ScrollOffset last_set_scroll_offset_;
4553 gfx::ScrollOffset max_scroll_offset_;
4554 gfx::SizeF scrollable_size_;
4555 float page_scale_factor_;
4556 float min_page_scale_factor_;
4557 float max_page_scale_factor_;
4560 TEST_F(LayerTreeHostImplTest, RootLayerScrollOffsetDelegation) {
4561 TestScrollOffsetDelegate scroll_delegate;
4562 host_impl_->SetViewportSize(gfx::Size(10, 20));
4563 LayerImpl* scroll_layer = SetupScrollAndContentsLayers(gfx::Size(100, 100));
4564 LayerImpl* clip_layer = scroll_layer->parent()->parent();
4565 clip_layer->SetBounds(gfx::Size(10, 20));
4567 // Setting the delegate results in the current scroll offset being set.
4568 gfx::Vector2dF initial_scroll_delta(10.f, 10.f);
4569 scroll_layer->PushScrollOffsetFromMainThread(gfx::ScrollOffset());
4570 scroll_layer->SetScrollDelta(initial_scroll_delta);
4571 host_impl_->SetRootLayerScrollOffsetDelegate(&scroll_delegate);
4572 EXPECT_EQ(initial_scroll_delta.ToString(),
4573 scroll_delegate.last_set_scroll_offset().ToString());
4575 // Setting the delegate results in the scrollable_size, max_scroll_offset,
4576 // page_scale_factor and {min|max}_page_scale_factor being set.
4577 EXPECT_EQ(gfx::SizeF(100, 100), scroll_delegate.scrollable_size());
4578 EXPECT_EQ(gfx::ScrollOffset(90, 80), scroll_delegate.max_scroll_offset());
4579 EXPECT_EQ(1.f, scroll_delegate.page_scale_factor());
4580 EXPECT_EQ(1.f, scroll_delegate.min_page_scale_factor());
4581 EXPECT_EQ(1.f, scroll_delegate.max_page_scale_factor());
4583 // Put a page scale on the tree.
4584 host_impl_->active_tree()->PushPageScaleFromMainThread(2.f, 0.5f, 4.f);
4585 EXPECT_EQ(1.f, scroll_delegate.page_scale_factor());
4586 EXPECT_EQ(1.f, scroll_delegate.min_page_scale_factor());
4587 EXPECT_EQ(1.f, scroll_delegate.max_page_scale_factor());
4588 // Activation will update the delegate.
4589 host_impl_->ActivateSyncTree();
4590 EXPECT_EQ(2.f, scroll_delegate.page_scale_factor());
4591 EXPECT_EQ(.5f, scroll_delegate.min_page_scale_factor());
4592 EXPECT_EQ(4.f, scroll_delegate.max_page_scale_factor());
4594 // Reset the page scale for the rest of the test.
4595 host_impl_->active_tree()->PushPageScaleFromMainThread(1.f, 0.5f, 4.f);
4596 EXPECT_EQ(2.f, scroll_delegate.page_scale_factor());
4597 EXPECT_EQ(.5f, scroll_delegate.min_page_scale_factor());
4598 EXPECT_EQ(4.f, scroll_delegate.max_page_scale_factor());
4600 // Animating page scale can change the root offset, so it should update the
4601 // delegate.
4602 host_impl_->Animate();
4603 EXPECT_EQ(1.f, scroll_delegate.page_scale_factor());
4604 EXPECT_EQ(.5f, scroll_delegate.min_page_scale_factor());
4605 EXPECT_EQ(4.f, scroll_delegate.max_page_scale_factor());
4607 // The pinch gesture doesn't put the delegate into a state where the scroll
4608 // offset is outside of the scroll range. (this is verified by DCHECKs in the
4609 // delegate).
4610 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE);
4611 host_impl_->PinchGestureBegin();
4612 host_impl_->PinchGestureUpdate(2.f, gfx::Point());
4613 host_impl_->PinchGestureUpdate(.5f, gfx::Point());
4614 host_impl_->PinchGestureEnd();
4615 host_impl_->ScrollEnd();
4617 // Scrolling should be relative to the offset as given by the delegate.
4618 gfx::Vector2dF scroll_delta(0.f, 10.f);
4619 gfx::ScrollOffset current_offset(7.f, 8.f);
4621 EXPECT_EQ(InputHandler::SCROLL_STARTED,
4622 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
4623 host_impl_->OnRootLayerDelegatedScrollOffsetChanged(current_offset);
4625 host_impl_->ScrollBy(gfx::Point(), scroll_delta);
4626 EXPECT_EQ(ScrollOffsetWithDelta(current_offset, scroll_delta),
4627 scroll_delegate.last_set_scroll_offset());
4629 current_offset = gfx::ScrollOffset(42.f, 41.f);
4630 host_impl_->OnRootLayerDelegatedScrollOffsetChanged(current_offset);
4631 host_impl_->ScrollBy(gfx::Point(), scroll_delta);
4632 EXPECT_EQ(current_offset + gfx::ScrollOffset(scroll_delta),
4633 scroll_delegate.last_set_scroll_offset());
4634 host_impl_->ScrollEnd();
4635 host_impl_->OnRootLayerDelegatedScrollOffsetChanged(gfx::ScrollOffset());
4637 // Forces a full tree synchronization and ensures that the scroll delegate
4638 // sees the correct size of the new tree.
4639 gfx::Size new_size(42, 24);
4640 host_impl_->CreatePendingTree();
4641 host_impl_->pending_tree()->PushPageScaleFromMainThread(1.f, 1.f, 1.f);
4642 CreateScrollAndContentsLayers(host_impl_->pending_tree(), new_size);
4643 host_impl_->ActivateSyncTree();
4644 EXPECT_EQ(new_size, scroll_delegate.scrollable_size());
4646 // Un-setting the delegate should propagate the delegate's current offset to
4647 // the root scrollable layer.
4648 current_offset = gfx::ScrollOffset(13.f, 12.f);
4649 host_impl_->OnRootLayerDelegatedScrollOffsetChanged(current_offset);
4650 host_impl_->SetRootLayerScrollOffsetDelegate(NULL);
4652 EXPECT_EQ(current_offset.ToString(),
4653 scroll_layer->CurrentScrollOffset().ToString());
4656 void CheckLayerScrollDelta(LayerImpl* layer, gfx::Vector2dF scroll_delta) {
4657 const gfx::Transform target_space_transform =
4658 layer->draw_properties().target_space_transform;
4659 EXPECT_TRUE(target_space_transform.IsScaleOrTranslation());
4660 gfx::Point translated_point;
4661 target_space_transform.TransformPoint(&translated_point);
4662 gfx::Point expected_point = gfx::Point() - ToRoundedVector2d(scroll_delta);
4663 EXPECT_EQ(expected_point.ToString(), translated_point.ToString());
4666 TEST_F(LayerTreeHostImplTest,
4667 ExternalRootLayerScrollOffsetDelegationReflectedInNextDraw) {
4668 TestScrollOffsetDelegate scroll_delegate;
4669 host_impl_->SetViewportSize(gfx::Size(10, 20));
4670 LayerImpl* scroll_layer = SetupScrollAndContentsLayers(gfx::Size(100, 100));
4671 LayerImpl* clip_layer = scroll_layer->parent()->parent();
4672 clip_layer->SetBounds(gfx::Size(10, 20));
4673 host_impl_->SetRootLayerScrollOffsetDelegate(&scroll_delegate);
4675 // Draw first frame to clear any pending draws and check scroll.
4676 DrawFrame();
4677 CheckLayerScrollDelta(scroll_layer, gfx::Vector2dF(0.f, 0.f));
4678 EXPECT_FALSE(host_impl_->active_tree()->needs_update_draw_properties());
4680 // Set external scroll delta on delegate and notify LayerTreeHost.
4681 gfx::ScrollOffset scroll_offset(10.f, 10.f);
4682 host_impl_->OnRootLayerDelegatedScrollOffsetChanged(scroll_offset);
4684 // Check scroll delta reflected in layer.
4685 LayerTreeHostImpl::FrameData frame;
4686 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
4687 host_impl_->DrawLayers(&frame);
4688 host_impl_->DidDrawAllLayers(frame);
4689 EXPECT_FALSE(frame.has_no_damage);
4690 CheckLayerScrollDelta(scroll_layer, ScrollOffsetToVector2dF(scroll_offset));
4692 host_impl_->SetRootLayerScrollOffsetDelegate(NULL);
4695 TEST_F(LayerTreeHostImplTest, OverscrollRoot) {
4696 InputHandlerScrollResult scroll_result;
4697 SetupScrollAndContentsLayers(gfx::Size(100, 100));
4698 host_impl_->SetViewportSize(gfx::Size(50, 50));
4699 host_impl_->active_tree()->PushPageScaleFromMainThread(1.f, 0.5f, 4.f);
4700 DrawFrame();
4701 EXPECT_EQ(gfx::Vector2dF(), host_impl_->accumulated_root_overscroll());
4703 // In-bounds scrolling does not affect overscroll.
4704 EXPECT_EQ(InputHandler::SCROLL_STARTED,
4705 host_impl_->ScrollBegin(gfx::Point(), InputHandler::WHEEL));
4706 scroll_result = host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(0, 10));
4707 EXPECT_TRUE(scroll_result.did_scroll);
4708 EXPECT_FALSE(scroll_result.did_overscroll_root);
4709 EXPECT_EQ(gfx::Vector2dF(), scroll_result.unused_scroll_delta);
4710 EXPECT_EQ(gfx::Vector2dF(), host_impl_->accumulated_root_overscroll());
4712 // Overscroll events are reflected immediately.
4713 scroll_result = host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(0, 50));
4714 EXPECT_TRUE(scroll_result.did_scroll);
4715 EXPECT_TRUE(scroll_result.did_overscroll_root);
4716 EXPECT_EQ(gfx::Vector2dF(0, 10), scroll_result.unused_scroll_delta);
4717 EXPECT_EQ(gfx::Vector2dF(0, 10), host_impl_->accumulated_root_overscroll());
4718 EXPECT_EQ(scroll_result.accumulated_root_overscroll,
4719 host_impl_->accumulated_root_overscroll());
4721 // In-bounds scrolling resets accumulated overscroll for the scrolled axes.
4722 scroll_result = host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(0, -50));
4723 EXPECT_TRUE(scroll_result.did_scroll);
4724 EXPECT_FALSE(scroll_result.did_overscroll_root);
4725 EXPECT_EQ(gfx::Vector2dF(), scroll_result.unused_scroll_delta);
4726 EXPECT_EQ(gfx::Vector2dF(0, 0), host_impl_->accumulated_root_overscroll());
4727 EXPECT_EQ(scroll_result.accumulated_root_overscroll,
4728 host_impl_->accumulated_root_overscroll());
4730 scroll_result = host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(0, -10));
4731 EXPECT_FALSE(scroll_result.did_scroll);
4732 EXPECT_TRUE(scroll_result.did_overscroll_root);
4733 EXPECT_EQ(gfx::Vector2dF(0, -10), scroll_result.unused_scroll_delta);
4734 EXPECT_EQ(gfx::Vector2dF(0, -10), host_impl_->accumulated_root_overscroll());
4735 EXPECT_EQ(scroll_result.accumulated_root_overscroll,
4736 host_impl_->accumulated_root_overscroll());
4738 scroll_result = host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(10, 0));
4739 EXPECT_TRUE(scroll_result.did_scroll);
4740 EXPECT_FALSE(scroll_result.did_overscroll_root);
4741 EXPECT_EQ(gfx::Vector2dF(0, 0), scroll_result.unused_scroll_delta);
4742 EXPECT_EQ(gfx::Vector2dF(0, -10), host_impl_->accumulated_root_overscroll());
4743 EXPECT_EQ(scroll_result.accumulated_root_overscroll,
4744 host_impl_->accumulated_root_overscroll());
4746 scroll_result = host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(-15, 0));
4747 EXPECT_TRUE(scroll_result.did_scroll);
4748 EXPECT_TRUE(scroll_result.did_overscroll_root);
4749 EXPECT_EQ(gfx::Vector2dF(-5, 0), scroll_result.unused_scroll_delta);
4750 EXPECT_EQ(gfx::Vector2dF(-5, -10), host_impl_->accumulated_root_overscroll());
4751 EXPECT_EQ(scroll_result.accumulated_root_overscroll,
4752 host_impl_->accumulated_root_overscroll());
4754 scroll_result = host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(0, 60));
4755 EXPECT_TRUE(scroll_result.did_scroll);
4756 EXPECT_TRUE(scroll_result.did_overscroll_root);
4757 EXPECT_EQ(gfx::Vector2dF(0, 10), scroll_result.unused_scroll_delta);
4758 EXPECT_EQ(gfx::Vector2dF(-5, 10), host_impl_->accumulated_root_overscroll());
4759 EXPECT_EQ(scroll_result.accumulated_root_overscroll,
4760 host_impl_->accumulated_root_overscroll());
4762 scroll_result = host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(10, -60));
4763 EXPECT_TRUE(scroll_result.did_scroll);
4764 EXPECT_TRUE(scroll_result.did_overscroll_root);
4765 EXPECT_EQ(gfx::Vector2dF(0, -10), scroll_result.unused_scroll_delta);
4766 EXPECT_EQ(gfx::Vector2dF(0, -10), host_impl_->accumulated_root_overscroll());
4767 EXPECT_EQ(scroll_result.accumulated_root_overscroll,
4768 host_impl_->accumulated_root_overscroll());
4770 // Overscroll accumulates within the scope of ScrollBegin/ScrollEnd as long
4771 // as no scroll occurs.
4772 scroll_result = host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(0, -20));
4773 EXPECT_FALSE(scroll_result.did_scroll);
4774 EXPECT_TRUE(scroll_result.did_overscroll_root);
4775 EXPECT_EQ(gfx::Vector2dF(0, -20), scroll_result.unused_scroll_delta);
4776 EXPECT_EQ(gfx::Vector2dF(0, -30), host_impl_->accumulated_root_overscroll());
4777 EXPECT_EQ(scroll_result.accumulated_root_overscroll,
4778 host_impl_->accumulated_root_overscroll());
4780 scroll_result = host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(0, -20));
4781 EXPECT_FALSE(scroll_result.did_scroll);
4782 EXPECT_TRUE(scroll_result.did_overscroll_root);
4783 EXPECT_EQ(gfx::Vector2dF(0, -20), scroll_result.unused_scroll_delta);
4784 EXPECT_EQ(gfx::Vector2dF(0, -50), host_impl_->accumulated_root_overscroll());
4785 EXPECT_EQ(scroll_result.accumulated_root_overscroll,
4786 host_impl_->accumulated_root_overscroll());
4788 // Overscroll resets on valid scroll.
4789 scroll_result = host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(0, 10));
4790 EXPECT_TRUE(scroll_result.did_scroll);
4791 EXPECT_FALSE(scroll_result.did_overscroll_root);
4792 EXPECT_EQ(gfx::Vector2dF(0, 0), scroll_result.unused_scroll_delta);
4793 EXPECT_EQ(gfx::Vector2dF(0, 0), host_impl_->accumulated_root_overscroll());
4794 EXPECT_EQ(scroll_result.accumulated_root_overscroll,
4795 host_impl_->accumulated_root_overscroll());
4797 scroll_result = host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(0, -20));
4798 EXPECT_TRUE(scroll_result.did_scroll);
4799 EXPECT_TRUE(scroll_result.did_overscroll_root);
4800 EXPECT_EQ(gfx::Vector2dF(0, -10), scroll_result.unused_scroll_delta);
4801 EXPECT_EQ(gfx::Vector2dF(0, -10), host_impl_->accumulated_root_overscroll());
4802 EXPECT_EQ(scroll_result.accumulated_root_overscroll,
4803 host_impl_->accumulated_root_overscroll());
4805 host_impl_->ScrollEnd();
4809 TEST_F(LayerTreeHostImplTest, OverscrollChildWithoutBubbling) {
4810 // Scroll child layers beyond their maximum scroll range and make sure root
4811 // overscroll does not accumulate.
4812 InputHandlerScrollResult scroll_result;
4813 gfx::Size surface_size(10, 10);
4814 scoped_ptr<LayerImpl> root_clip =
4815 LayerImpl::Create(host_impl_->active_tree(), 4);
4816 root_clip->SetHasRenderSurface(true);
4818 scoped_ptr<LayerImpl> root =
4819 CreateScrollableLayer(1, surface_size, root_clip.get());
4821 scoped_ptr<LayerImpl> grand_child =
4822 CreateScrollableLayer(3, surface_size, root_clip.get());
4824 scoped_ptr<LayerImpl> child =
4825 CreateScrollableLayer(2, surface_size, root_clip.get());
4826 LayerImpl* grand_child_layer = grand_child.get();
4827 child->AddChild(grand_child.Pass());
4829 LayerImpl* child_layer = child.get();
4830 root->AddChild(child.Pass());
4831 root_clip->AddChild(root.Pass());
4832 child_layer->PushScrollOffsetFromMainThread(gfx::ScrollOffset(0, 3));
4833 grand_child_layer->PushScrollOffsetFromMainThread(gfx::ScrollOffset(0, 2));
4834 host_impl_->active_tree()->SetRootLayer(root_clip.Pass());
4835 host_impl_->active_tree()->DidBecomeActive();
4836 host_impl_->SetViewportSize(surface_size);
4837 DrawFrame();
4839 gfx::Vector2d scroll_delta(0, -10);
4840 EXPECT_EQ(InputHandler::SCROLL_STARTED,
4841 host_impl_->ScrollBegin(gfx::Point(),
4842 InputHandler::NON_BUBBLING_GESTURE));
4843 scroll_result = host_impl_->ScrollBy(gfx::Point(), scroll_delta);
4844 EXPECT_TRUE(scroll_result.did_scroll);
4845 EXPECT_FALSE(scroll_result.did_overscroll_root);
4846 EXPECT_EQ(gfx::Vector2dF(), host_impl_->accumulated_root_overscroll());
4847 host_impl_->ScrollEnd();
4849 // The next time we scroll we should only scroll the parent, but overscroll
4850 // should still not reach the root layer.
4851 scroll_delta = gfx::Vector2d(0, -30);
4852 EXPECT_EQ(InputHandler::SCROLL_STARTED,
4853 host_impl_->ScrollBegin(gfx::Point(5, 5),
4854 InputHandler::NON_BUBBLING_GESTURE));
4855 EXPECT_EQ(host_impl_->CurrentlyScrollingLayer(), grand_child_layer);
4856 EXPECT_EQ(gfx::Vector2dF(), host_impl_->accumulated_root_overscroll());
4857 scroll_result = host_impl_->ScrollBy(gfx::Point(), scroll_delta);
4858 EXPECT_TRUE(scroll_result.did_scroll);
4859 EXPECT_FALSE(scroll_result.did_overscroll_root);
4860 EXPECT_EQ(host_impl_->CurrentlyScrollingLayer(), child_layer);
4861 EXPECT_EQ(gfx::Vector2dF(), host_impl_->accumulated_root_overscroll());
4862 host_impl_->ScrollEnd();
4864 // After scrolling the parent, another scroll on the opposite direction
4865 // should scroll the child.
4866 scroll_delta = gfx::Vector2d(0, 70);
4867 EXPECT_EQ(InputHandler::SCROLL_STARTED,
4868 host_impl_->ScrollBegin(gfx::Point(5, 5),
4869 InputHandler::NON_BUBBLING_GESTURE));
4870 EXPECT_EQ(host_impl_->CurrentlyScrollingLayer(), grand_child_layer);
4871 scroll_result = host_impl_->ScrollBy(gfx::Point(), scroll_delta);
4872 EXPECT_TRUE(scroll_result.did_scroll);
4873 EXPECT_FALSE(scroll_result.did_overscroll_root);
4874 EXPECT_EQ(host_impl_->CurrentlyScrollingLayer(), grand_child_layer);
4875 EXPECT_EQ(gfx::Vector2dF(), host_impl_->accumulated_root_overscroll());
4876 host_impl_->ScrollEnd();
4880 TEST_F(LayerTreeHostImplTest, OverscrollChildEventBubbling) {
4881 // When we try to scroll a non-scrollable child layer, the scroll delta
4882 // should be applied to one of its ancestors if possible. Overscroll should
4883 // be reflected only when it has bubbled up to the root scrolling layer.
4884 InputHandlerScrollResult scroll_result;
4885 SetupScrollAndContentsLayers(gfx::Size(20, 20));
4886 DrawFrame();
4888 gfx::Vector2d scroll_delta(0, 8);
4889 EXPECT_EQ(InputHandler::SCROLL_STARTED,
4890 host_impl_->ScrollBegin(gfx::Point(5, 5), InputHandler::WHEEL));
4891 scroll_result = host_impl_->ScrollBy(gfx::Point(), scroll_delta);
4892 EXPECT_TRUE(scroll_result.did_scroll);
4893 EXPECT_FALSE(scroll_result.did_overscroll_root);
4894 EXPECT_EQ(gfx::Vector2dF(), host_impl_->accumulated_root_overscroll());
4895 scroll_result = host_impl_->ScrollBy(gfx::Point(), scroll_delta);
4896 EXPECT_TRUE(scroll_result.did_scroll);
4897 EXPECT_TRUE(scroll_result.did_overscroll_root);
4898 EXPECT_EQ(gfx::Vector2dF(0, 6), host_impl_->accumulated_root_overscroll());
4899 scroll_result = host_impl_->ScrollBy(gfx::Point(), scroll_delta);
4900 EXPECT_FALSE(scroll_result.did_scroll);
4901 EXPECT_TRUE(scroll_result.did_overscroll_root);
4902 EXPECT_EQ(gfx::Vector2dF(0, 14), host_impl_->accumulated_root_overscroll());
4903 host_impl_->ScrollEnd();
4907 TEST_F(LayerTreeHostImplTest, OverscrollAlways) {
4908 InputHandlerScrollResult scroll_result;
4909 LayerTreeSettings settings;
4910 CreateHostImpl(settings, CreateOutputSurface());
4912 LayerImpl* scroll_layer = SetupScrollAndContentsLayers(gfx::Size(50, 50));
4913 LayerImpl* clip_layer = scroll_layer->parent()->parent();
4914 clip_layer->SetBounds(gfx::Size(50, 50));
4915 host_impl_->SetViewportSize(gfx::Size(50, 50));
4916 host_impl_->active_tree()->PushPageScaleFromMainThread(1.f, 0.5f, 4.f);
4917 DrawFrame();
4918 EXPECT_EQ(gfx::Vector2dF(), host_impl_->accumulated_root_overscroll());
4920 // Even though the layer can't scroll the overscroll still happens.
4921 EXPECT_EQ(InputHandler::SCROLL_STARTED,
4922 host_impl_->ScrollBegin(gfx::Point(), InputHandler::WHEEL));
4923 scroll_result = host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(0, 10));
4924 EXPECT_FALSE(scroll_result.did_scroll);
4925 EXPECT_TRUE(scroll_result.did_overscroll_root);
4926 EXPECT_EQ(gfx::Vector2dF(0, 10), host_impl_->accumulated_root_overscroll());
4929 TEST_F(LayerTreeHostImplTest, NoOverscrollWhenNotAtEdge) {
4930 InputHandlerScrollResult scroll_result;
4931 SetupScrollAndContentsLayers(gfx::Size(200, 200));
4933 DrawFrame();
4935 // Edge glow effect should be applicable only upon reaching Edges
4936 // of the content. unnecessary glow effect calls shouldn't be
4937 // called while scrolling up without reaching the edge of the content.
4938 EXPECT_EQ(InputHandler::SCROLL_STARTED,
4939 host_impl_->ScrollBegin(gfx::Point(0, 0), InputHandler::WHEEL));
4940 scroll_result = host_impl_->ScrollBy(gfx::Point(), gfx::Vector2dF(0, 100));
4941 EXPECT_TRUE(scroll_result.did_scroll);
4942 EXPECT_FALSE(scroll_result.did_overscroll_root);
4943 EXPECT_EQ(gfx::Vector2dF().ToString(),
4944 host_impl_->accumulated_root_overscroll().ToString());
4945 scroll_result =
4946 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2dF(0, -2.30f));
4947 EXPECT_TRUE(scroll_result.did_scroll);
4948 EXPECT_FALSE(scroll_result.did_overscroll_root);
4949 EXPECT_EQ(gfx::Vector2dF().ToString(),
4950 host_impl_->accumulated_root_overscroll().ToString());
4951 host_impl_->ScrollEnd();
4952 // unusedrootDelta should be subtracted from applied delta so that
4953 // unwanted glow effect calls are not called.
4954 EXPECT_EQ(InputHandler::SCROLL_STARTED,
4955 host_impl_->ScrollBegin(gfx::Point(0, 0),
4956 InputHandler::NON_BUBBLING_GESTURE));
4957 EXPECT_EQ(InputHandler::SCROLL_STARTED, host_impl_->FlingScrollBegin());
4958 scroll_result = host_impl_->ScrollBy(gfx::Point(), gfx::Vector2dF(0, 20));
4959 EXPECT_TRUE(scroll_result.did_scroll);
4960 EXPECT_TRUE(scroll_result.did_overscroll_root);
4961 EXPECT_EQ(gfx::Vector2dF(0.000000f, 17.699997f).ToString(),
4962 host_impl_->accumulated_root_overscroll().ToString());
4964 scroll_result =
4965 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2dF(0.02f, -0.01f));
4966 EXPECT_FALSE(scroll_result.did_scroll);
4967 EXPECT_FALSE(scroll_result.did_overscroll_root);
4968 EXPECT_EQ(gfx::Vector2dF(0.000000f, 17.699997f).ToString(),
4969 host_impl_->accumulated_root_overscroll().ToString());
4970 host_impl_->ScrollEnd();
4971 // TestCase to check kEpsilon, which prevents minute values to trigger
4972 // gloweffect without reaching edge.
4973 EXPECT_EQ(InputHandler::SCROLL_STARTED,
4974 host_impl_->ScrollBegin(gfx::Point(0, 0), InputHandler::WHEEL));
4975 scroll_result =
4976 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2dF(-0.12f, 0.1f));
4977 EXPECT_FALSE(scroll_result.did_scroll);
4978 EXPECT_FALSE(scroll_result.did_overscroll_root);
4979 EXPECT_EQ(gfx::Vector2dF().ToString(),
4980 host_impl_->accumulated_root_overscroll().ToString());
4981 host_impl_->ScrollEnd();
4985 class BlendStateCheckLayer : public LayerImpl {
4986 public:
4987 static scoped_ptr<LayerImpl> Create(LayerTreeImpl* tree_impl,
4988 int id,
4989 ResourceProvider* resource_provider) {
4990 return make_scoped_ptr(
4991 new BlendStateCheckLayer(tree_impl, id, resource_provider));
4994 void AppendQuads(RenderPass* render_pass,
4995 AppendQuadsData* append_quads_data) override {
4996 quads_appended_ = true;
4998 gfx::Rect opaque_rect;
4999 if (contents_opaque())
5000 opaque_rect = quad_rect_;
5001 else
5002 opaque_rect = opaque_content_rect_;
5003 gfx::Rect visible_quad_rect = quad_rect_;
5005 SharedQuadState* shared_quad_state =
5006 render_pass->CreateAndAppendSharedQuadState();
5007 PopulateSharedQuadState(shared_quad_state);
5009 TileDrawQuad* test_blending_draw_quad =
5010 render_pass->CreateAndAppendDrawQuad<TileDrawQuad>();
5011 test_blending_draw_quad->SetNew(shared_quad_state,
5012 quad_rect_,
5013 opaque_rect,
5014 visible_quad_rect,
5015 resource_id_,
5016 gfx::RectF(0.f, 0.f, 1.f, 1.f),
5017 gfx::Size(1, 1),
5018 false,
5019 false);
5020 test_blending_draw_quad->visible_rect = quad_visible_rect_;
5021 EXPECT_EQ(blend_, test_blending_draw_quad->ShouldDrawWithBlending());
5022 EXPECT_EQ(has_render_surface_, !!render_surface());
5025 void SetExpectation(bool blend, bool has_render_surface) {
5026 blend_ = blend;
5027 has_render_surface_ = has_render_surface;
5028 quads_appended_ = false;
5031 bool quads_appended() const { return quads_appended_; }
5033 void SetQuadRect(const gfx::Rect& rect) { quad_rect_ = rect; }
5034 void SetQuadVisibleRect(const gfx::Rect& rect) { quad_visible_rect_ = rect; }
5035 void SetOpaqueContentRect(const gfx::Rect& rect) {
5036 opaque_content_rect_ = rect;
5039 private:
5040 BlendStateCheckLayer(LayerTreeImpl* tree_impl,
5041 int id,
5042 ResourceProvider* resource_provider)
5043 : LayerImpl(tree_impl, id),
5044 blend_(false),
5045 has_render_surface_(false),
5046 quads_appended_(false),
5047 quad_rect_(5, 5, 5, 5),
5048 quad_visible_rect_(5, 5, 5, 5),
5049 resource_id_(resource_provider->CreateResource(
5050 gfx::Size(1, 1),
5051 GL_CLAMP_TO_EDGE,
5052 ResourceProvider::TEXTURE_HINT_IMMUTABLE,
5053 RGBA_8888)) {
5054 resource_provider->AllocateForTesting(resource_id_);
5055 SetBounds(gfx::Size(10, 10));
5056 SetDrawsContent(true);
5059 bool blend_;
5060 bool has_render_surface_;
5061 bool quads_appended_;
5062 gfx::Rect quad_rect_;
5063 gfx::Rect opaque_content_rect_;
5064 gfx::Rect quad_visible_rect_;
5065 ResourceId resource_id_;
5068 TEST_F(LayerTreeHostImplTest, BlendingOffWhenDrawingOpaqueLayers) {
5070 scoped_ptr<LayerImpl> root =
5071 LayerImpl::Create(host_impl_->active_tree(), 1);
5072 root->SetBounds(gfx::Size(10, 10));
5073 root->SetDrawsContent(false);
5074 root->SetHasRenderSurface(true);
5075 host_impl_->active_tree()->SetRootLayer(root.Pass());
5077 LayerImpl* root = host_impl_->active_tree()->root_layer();
5079 root->AddChild(
5080 BlendStateCheckLayer::Create(host_impl_->active_tree(),
5082 host_impl_->resource_provider()));
5083 BlendStateCheckLayer* layer1 =
5084 static_cast<BlendStateCheckLayer*>(root->children()[0]);
5085 layer1->SetPosition(gfx::PointF(2.f, 2.f));
5087 LayerTreeHostImpl::FrameData frame;
5089 // Opaque layer, drawn without blending.
5090 layer1->SetContentsOpaque(true);
5091 layer1->SetExpectation(false, false);
5092 layer1->SetUpdateRect(gfx::Rect(layer1->bounds()));
5093 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
5094 host_impl_->DrawLayers(&frame);
5095 EXPECT_TRUE(layer1->quads_appended());
5096 host_impl_->DidDrawAllLayers(frame);
5098 // Layer with translucent content and painting, so drawn with blending.
5099 layer1->SetContentsOpaque(false);
5100 layer1->SetExpectation(true, false);
5101 layer1->SetUpdateRect(gfx::Rect(layer1->bounds()));
5102 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
5103 host_impl_->DrawLayers(&frame);
5104 EXPECT_TRUE(layer1->quads_appended());
5105 host_impl_->DidDrawAllLayers(frame);
5107 // Layer with translucent opacity, drawn with blending.
5108 layer1->SetContentsOpaque(true);
5109 layer1->SetOpacity(0.5f);
5110 layer1->SetExpectation(true, false);
5111 layer1->SetUpdateRect(gfx::Rect(layer1->bounds()));
5112 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
5113 host_impl_->DrawLayers(&frame);
5114 EXPECT_TRUE(layer1->quads_appended());
5115 host_impl_->DidDrawAllLayers(frame);
5117 // Layer with translucent opacity and painting, drawn with blending.
5118 layer1->SetContentsOpaque(true);
5119 layer1->SetOpacity(0.5f);
5120 layer1->SetExpectation(true, false);
5121 layer1->SetUpdateRect(gfx::Rect(layer1->bounds()));
5122 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
5123 host_impl_->DrawLayers(&frame);
5124 EXPECT_TRUE(layer1->quads_appended());
5125 host_impl_->DidDrawAllLayers(frame);
5127 layer1->AddChild(
5128 BlendStateCheckLayer::Create(host_impl_->active_tree(),
5130 host_impl_->resource_provider()));
5131 BlendStateCheckLayer* layer2 =
5132 static_cast<BlendStateCheckLayer*>(layer1->children()[0]);
5133 layer2->SetPosition(gfx::PointF(4.f, 4.f));
5135 // 2 opaque layers, drawn without blending.
5136 layer1->SetContentsOpaque(true);
5137 layer1->SetOpacity(1.f);
5138 layer1->SetExpectation(false, false);
5139 layer1->SetUpdateRect(gfx::Rect(layer1->bounds()));
5140 layer2->SetContentsOpaque(true);
5141 layer2->SetOpacity(1.f);
5142 layer2->SetExpectation(false, false);
5143 layer2->SetUpdateRect(gfx::Rect(layer1->bounds()));
5144 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
5145 host_impl_->DrawLayers(&frame);
5146 EXPECT_TRUE(layer1->quads_appended());
5147 EXPECT_TRUE(layer2->quads_appended());
5148 host_impl_->DidDrawAllLayers(frame);
5150 // Parent layer with translucent content, drawn with blending.
5151 // Child layer with opaque content, drawn without blending.
5152 layer1->SetContentsOpaque(false);
5153 layer1->SetExpectation(true, false);
5154 layer1->SetUpdateRect(gfx::Rect(layer1->bounds()));
5155 layer2->SetExpectation(false, false);
5156 layer2->SetUpdateRect(gfx::Rect(layer1->bounds()));
5157 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
5158 host_impl_->DrawLayers(&frame);
5159 EXPECT_TRUE(layer1->quads_appended());
5160 EXPECT_TRUE(layer2->quads_appended());
5161 host_impl_->DidDrawAllLayers(frame);
5163 // Parent layer with translucent content but opaque painting, drawn without
5164 // blending.
5165 // Child layer with opaque content, drawn without blending.
5166 layer1->SetContentsOpaque(true);
5167 layer1->SetExpectation(false, false);
5168 layer1->SetUpdateRect(gfx::Rect(layer1->bounds()));
5169 layer2->SetExpectation(false, false);
5170 layer2->SetUpdateRect(gfx::Rect(layer1->bounds()));
5171 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
5172 host_impl_->DrawLayers(&frame);
5173 EXPECT_TRUE(layer1->quads_appended());
5174 EXPECT_TRUE(layer2->quads_appended());
5175 host_impl_->DidDrawAllLayers(frame);
5177 // Parent layer with translucent opacity and opaque content. Since it has a
5178 // drawing child, it's drawn to a render surface which carries the opacity,
5179 // so it's itself drawn without blending.
5180 // Child layer with opaque content, drawn without blending (parent surface
5181 // carries the inherited opacity).
5182 layer1->SetContentsOpaque(true);
5183 layer1->SetOpacity(0.5f);
5184 layer1->SetHasRenderSurface(true);
5185 layer1->SetExpectation(false, true);
5186 layer1->SetUpdateRect(gfx::Rect(layer1->bounds()));
5187 layer2->SetExpectation(false, false);
5188 layer2->SetUpdateRect(gfx::Rect(layer1->bounds()));
5189 FakeLayerTreeHostImpl::RecursiveUpdateNumChildren(
5190 host_impl_->active_tree()->root_layer());
5191 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
5192 host_impl_->DrawLayers(&frame);
5193 EXPECT_TRUE(layer1->quads_appended());
5194 EXPECT_TRUE(layer2->quads_appended());
5195 host_impl_->DidDrawAllLayers(frame);
5196 layer1->SetHasRenderSurface(false);
5198 // Draw again, but with child non-opaque, to make sure
5199 // layer1 not culled.
5200 layer1->SetContentsOpaque(true);
5201 layer1->SetOpacity(1.f);
5202 layer1->SetExpectation(false, false);
5203 layer1->SetUpdateRect(gfx::Rect(layer1->bounds()));
5204 layer2->SetContentsOpaque(true);
5205 layer2->SetOpacity(0.5f);
5206 layer2->SetExpectation(true, false);
5207 layer2->SetUpdateRect(gfx::Rect(layer1->bounds()));
5208 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
5209 host_impl_->DrawLayers(&frame);
5210 EXPECT_TRUE(layer1->quads_appended());
5211 EXPECT_TRUE(layer2->quads_appended());
5212 host_impl_->DidDrawAllLayers(frame);
5214 // A second way of making the child non-opaque.
5215 layer1->SetContentsOpaque(true);
5216 layer1->SetOpacity(1.f);
5217 layer1->SetExpectation(false, false);
5218 layer1->SetUpdateRect(gfx::Rect(layer1->bounds()));
5219 layer2->SetContentsOpaque(false);
5220 layer2->SetOpacity(1.f);
5221 layer2->SetExpectation(true, false);
5222 layer2->SetUpdateRect(gfx::Rect(layer1->bounds()));
5223 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
5224 host_impl_->DrawLayers(&frame);
5225 EXPECT_TRUE(layer1->quads_appended());
5226 EXPECT_TRUE(layer2->quads_appended());
5227 host_impl_->DidDrawAllLayers(frame);
5229 // And when the layer says its not opaque but is painted opaque, it is not
5230 // blended.
5231 layer1->SetContentsOpaque(true);
5232 layer1->SetOpacity(1.f);
5233 layer1->SetExpectation(false, false);
5234 layer1->SetUpdateRect(gfx::Rect(layer1->bounds()));
5235 layer2->SetContentsOpaque(true);
5236 layer2->SetOpacity(1.f);
5237 layer2->SetExpectation(false, false);
5238 layer2->SetUpdateRect(gfx::Rect(layer1->bounds()));
5239 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
5240 host_impl_->DrawLayers(&frame);
5241 EXPECT_TRUE(layer1->quads_appended());
5242 EXPECT_TRUE(layer2->quads_appended());
5243 host_impl_->DidDrawAllLayers(frame);
5245 // Layer with partially opaque contents, drawn with blending.
5246 layer1->SetContentsOpaque(false);
5247 layer1->SetQuadRect(gfx::Rect(5, 5, 5, 5));
5248 layer1->SetQuadVisibleRect(gfx::Rect(5, 5, 5, 5));
5249 layer1->SetOpaqueContentRect(gfx::Rect(5, 5, 2, 5));
5250 layer1->SetExpectation(true, false);
5251 layer1->SetUpdateRect(gfx::Rect(layer1->bounds()));
5252 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
5253 host_impl_->DrawLayers(&frame);
5254 EXPECT_TRUE(layer1->quads_appended());
5255 host_impl_->DidDrawAllLayers(frame);
5257 // Layer with partially opaque contents partially culled, drawn with blending.
5258 layer1->SetContentsOpaque(false);
5259 layer1->SetQuadRect(gfx::Rect(5, 5, 5, 5));
5260 layer1->SetQuadVisibleRect(gfx::Rect(5, 5, 5, 2));
5261 layer1->SetOpaqueContentRect(gfx::Rect(5, 5, 2, 5));
5262 layer1->SetExpectation(true, false);
5263 layer1->SetUpdateRect(gfx::Rect(layer1->bounds()));
5264 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
5265 host_impl_->DrawLayers(&frame);
5266 EXPECT_TRUE(layer1->quads_appended());
5267 host_impl_->DidDrawAllLayers(frame);
5269 // Layer with partially opaque contents culled, drawn with blending.
5270 layer1->SetContentsOpaque(false);
5271 layer1->SetQuadRect(gfx::Rect(5, 5, 5, 5));
5272 layer1->SetQuadVisibleRect(gfx::Rect(7, 5, 3, 5));
5273 layer1->SetOpaqueContentRect(gfx::Rect(5, 5, 2, 5));
5274 layer1->SetExpectation(true, false);
5275 layer1->SetUpdateRect(gfx::Rect(layer1->bounds()));
5276 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
5277 host_impl_->DrawLayers(&frame);
5278 EXPECT_TRUE(layer1->quads_appended());
5279 host_impl_->DidDrawAllLayers(frame);
5281 // Layer with partially opaque contents and translucent contents culled, drawn
5282 // without blending.
5283 layer1->SetContentsOpaque(false);
5284 layer1->SetQuadRect(gfx::Rect(5, 5, 5, 5));
5285 layer1->SetQuadVisibleRect(gfx::Rect(5, 5, 2, 5));
5286 layer1->SetOpaqueContentRect(gfx::Rect(5, 5, 2, 5));
5287 layer1->SetExpectation(false, false);
5288 layer1->SetUpdateRect(gfx::Rect(layer1->bounds()));
5289 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
5290 host_impl_->DrawLayers(&frame);
5291 EXPECT_TRUE(layer1->quads_appended());
5292 host_impl_->DidDrawAllLayers(frame);
5295 class LayerTreeHostImplViewportCoveredTest : public LayerTreeHostImplTest {
5296 protected:
5297 LayerTreeHostImplViewportCoveredTest() :
5298 gutter_quad_material_(DrawQuad::SOLID_COLOR),
5299 child_(NULL),
5300 did_activate_pending_tree_(false) {}
5302 scoped_ptr<OutputSurface> CreateFakeOutputSurface(bool always_draw) {
5303 if (always_draw) {
5304 return FakeOutputSurface::CreateAlwaysDrawAndSwap3d();
5306 return FakeOutputSurface::Create3d();
5309 void SetupActiveTreeLayers() {
5310 host_impl_->active_tree()->set_background_color(SK_ColorGRAY);
5311 host_impl_->active_tree()->SetRootLayer(
5312 LayerImpl::Create(host_impl_->active_tree(), 1));
5313 host_impl_->active_tree()->root_layer()->SetHasRenderSurface(true);
5314 host_impl_->active_tree()->root_layer()->AddChild(
5315 BlendStateCheckLayer::Create(host_impl_->active_tree(),
5317 host_impl_->resource_provider()));
5318 child_ = static_cast<BlendStateCheckLayer*>(
5319 host_impl_->active_tree()->root_layer()->children()[0]);
5320 child_->SetExpectation(false, false);
5321 child_->SetContentsOpaque(true);
5324 // Expect no gutter rects.
5325 void TestLayerCoversFullViewport() {
5326 gfx::Rect layer_rect(viewport_size_);
5327 child_->SetPosition(layer_rect.origin());
5328 child_->SetBounds(layer_rect.size());
5329 child_->SetQuadRect(gfx::Rect(layer_rect.size()));
5330 child_->SetQuadVisibleRect(gfx::Rect(layer_rect.size()));
5332 LayerTreeHostImpl::FrameData frame;
5333 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
5334 ASSERT_EQ(1u, frame.render_passes.size());
5336 EXPECT_EQ(0u, CountGutterQuads(frame.render_passes[0]->quad_list));
5337 EXPECT_EQ(1u, frame.render_passes[0]->quad_list.size());
5338 ValidateTextureDrawQuads(frame.render_passes[0]->quad_list);
5340 VerifyQuadsExactlyCoverViewport(frame.render_passes[0]->quad_list);
5341 host_impl_->DidDrawAllLayers(frame);
5344 // Expect fullscreen gutter rect.
5345 void TestEmptyLayer() {
5346 gfx::Rect layer_rect(0, 0, 0, 0);
5347 child_->SetPosition(layer_rect.origin());
5348 child_->SetBounds(layer_rect.size());
5349 child_->SetQuadRect(gfx::Rect(layer_rect.size()));
5350 child_->SetQuadVisibleRect(gfx::Rect(layer_rect.size()));
5352 LayerTreeHostImpl::FrameData frame;
5353 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
5354 ASSERT_EQ(1u, frame.render_passes.size());
5356 EXPECT_EQ(1u, CountGutterQuads(frame.render_passes[0]->quad_list));
5357 EXPECT_EQ(1u, frame.render_passes[0]->quad_list.size());
5358 ValidateTextureDrawQuads(frame.render_passes[0]->quad_list);
5360 VerifyQuadsExactlyCoverViewport(frame.render_passes[0]->quad_list);
5361 host_impl_->DidDrawAllLayers(frame);
5364 // Expect four surrounding gutter rects.
5365 void TestLayerInMiddleOfViewport() {
5366 gfx::Rect layer_rect(500, 500, 200, 200);
5367 child_->SetPosition(layer_rect.origin());
5368 child_->SetBounds(layer_rect.size());
5369 child_->SetQuadRect(gfx::Rect(layer_rect.size()));
5370 child_->SetQuadVisibleRect(gfx::Rect(layer_rect.size()));
5372 LayerTreeHostImpl::FrameData frame;
5373 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
5374 ASSERT_EQ(1u, frame.render_passes.size());
5376 EXPECT_EQ(4u, CountGutterQuads(frame.render_passes[0]->quad_list));
5377 EXPECT_EQ(5u, frame.render_passes[0]->quad_list.size());
5378 ValidateTextureDrawQuads(frame.render_passes[0]->quad_list);
5380 VerifyQuadsExactlyCoverViewport(frame.render_passes[0]->quad_list);
5381 host_impl_->DidDrawAllLayers(frame);
5384 // Expect no gutter rects.
5385 void TestLayerIsLargerThanViewport() {
5386 gfx::Rect layer_rect(viewport_size_.width() + 10,
5387 viewport_size_.height() + 10);
5388 child_->SetPosition(layer_rect.origin());
5389 child_->SetBounds(layer_rect.size());
5390 child_->SetQuadRect(gfx::Rect(layer_rect.size()));
5391 child_->SetQuadVisibleRect(gfx::Rect(layer_rect.size()));
5393 LayerTreeHostImpl::FrameData frame;
5394 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
5395 ASSERT_EQ(1u, frame.render_passes.size());
5397 EXPECT_EQ(0u, CountGutterQuads(frame.render_passes[0]->quad_list));
5398 EXPECT_EQ(1u, frame.render_passes[0]->quad_list.size());
5399 ValidateTextureDrawQuads(frame.render_passes[0]->quad_list);
5401 host_impl_->DidDrawAllLayers(frame);
5404 void DidActivateSyncTree() override { did_activate_pending_tree_ = true; }
5406 void set_gutter_quad_material(DrawQuad::Material material) {
5407 gutter_quad_material_ = material;
5409 void set_gutter_texture_size(const gfx::Size& gutter_texture_size) {
5410 gutter_texture_size_ = gutter_texture_size;
5413 protected:
5414 size_t CountGutterQuads(const QuadList& quad_list) {
5415 size_t num_gutter_quads = 0;
5416 for (const auto& quad : quad_list) {
5417 num_gutter_quads += (quad->material == gutter_quad_material_) ? 1 : 0;
5419 return num_gutter_quads;
5422 void VerifyQuadsExactlyCoverViewport(const QuadList& quad_list) {
5423 LayerTestCommon::VerifyQuadsExactlyCoverRect(
5424 quad_list, gfx::Rect(DipSizeToPixelSize(viewport_size_)));
5427 // Make sure that the texture coordinates match their expectations.
5428 void ValidateTextureDrawQuads(const QuadList& quad_list) {
5429 for (const auto& quad : quad_list) {
5430 if (quad->material != DrawQuad::TEXTURE_CONTENT)
5431 continue;
5432 const TextureDrawQuad* texture_quad = TextureDrawQuad::MaterialCast(quad);
5433 gfx::SizeF gutter_texture_size_pixels = gfx::ScaleSize(
5434 gutter_texture_size_, host_impl_->device_scale_factor());
5435 EXPECT_EQ(texture_quad->uv_top_left.x(),
5436 texture_quad->rect.x() / gutter_texture_size_pixels.width());
5437 EXPECT_EQ(texture_quad->uv_top_left.y(),
5438 texture_quad->rect.y() / gutter_texture_size_pixels.height());
5439 EXPECT_EQ(
5440 texture_quad->uv_bottom_right.x(),
5441 texture_quad->rect.right() / gutter_texture_size_pixels.width());
5442 EXPECT_EQ(
5443 texture_quad->uv_bottom_right.y(),
5444 texture_quad->rect.bottom() / gutter_texture_size_pixels.height());
5448 gfx::Size DipSizeToPixelSize(const gfx::Size& size) {
5449 return gfx::ToRoundedSize(
5450 gfx::ScaleSize(size, host_impl_->device_scale_factor()));
5453 DrawQuad::Material gutter_quad_material_;
5454 gfx::Size gutter_texture_size_;
5455 gfx::Size viewport_size_;
5456 BlendStateCheckLayer* child_;
5457 bool did_activate_pending_tree_;
5460 TEST_F(LayerTreeHostImplViewportCoveredTest, ViewportCovered) {
5461 viewport_size_ = gfx::Size(1000, 1000);
5463 bool always_draw = false;
5464 CreateHostImpl(DefaultSettings(), CreateFakeOutputSurface(always_draw));
5466 host_impl_->SetViewportSize(DipSizeToPixelSize(viewport_size_));
5467 SetupActiveTreeLayers();
5468 TestLayerCoversFullViewport();
5469 TestEmptyLayer();
5470 TestLayerInMiddleOfViewport();
5471 TestLayerIsLargerThanViewport();
5474 TEST_F(LayerTreeHostImplViewportCoveredTest, ViewportCoveredScaled) {
5475 viewport_size_ = gfx::Size(1000, 1000);
5477 bool always_draw = false;
5478 CreateHostImpl(DefaultSettings(), CreateFakeOutputSurface(always_draw));
5480 host_impl_->SetDeviceScaleFactor(2.f);
5481 host_impl_->SetViewportSize(DipSizeToPixelSize(viewport_size_));
5482 SetupActiveTreeLayers();
5483 TestLayerCoversFullViewport();
5484 TestEmptyLayer();
5485 TestLayerInMiddleOfViewport();
5486 TestLayerIsLargerThanViewport();
5489 TEST_F(LayerTreeHostImplViewportCoveredTest, ActiveTreeGrowViewportInvalid) {
5490 viewport_size_ = gfx::Size(1000, 1000);
5492 bool always_draw = true;
5493 CreateHostImpl(DefaultSettings(), CreateFakeOutputSurface(always_draw));
5495 // Pending tree to force active_tree size invalid. Not used otherwise.
5496 host_impl_->CreatePendingTree();
5497 host_impl_->SetViewportSize(DipSizeToPixelSize(viewport_size_));
5498 EXPECT_TRUE(host_impl_->active_tree()->ViewportSizeInvalid());
5500 SetupActiveTreeLayers();
5501 TestEmptyLayer();
5502 TestLayerInMiddleOfViewport();
5503 TestLayerIsLargerThanViewport();
5506 TEST_F(LayerTreeHostImplViewportCoveredTest, ActiveTreeShrinkViewportInvalid) {
5507 viewport_size_ = gfx::Size(1000, 1000);
5509 bool always_draw = true;
5510 CreateHostImpl(DefaultSettings(), CreateFakeOutputSurface(always_draw));
5512 // Set larger viewport and activate it to active tree.
5513 host_impl_->CreatePendingTree();
5514 gfx::Size larger_viewport(viewport_size_.width() + 100,
5515 viewport_size_.height() + 100);
5516 host_impl_->SetViewportSize(DipSizeToPixelSize(larger_viewport));
5517 EXPECT_TRUE(host_impl_->active_tree()->ViewportSizeInvalid());
5518 host_impl_->ActivateSyncTree();
5519 EXPECT_TRUE(did_activate_pending_tree_);
5520 EXPECT_FALSE(host_impl_->active_tree()->ViewportSizeInvalid());
5522 // Shrink pending tree viewport without activating.
5523 host_impl_->CreatePendingTree();
5524 host_impl_->SetViewportSize(DipSizeToPixelSize(viewport_size_));
5525 EXPECT_TRUE(host_impl_->active_tree()->ViewportSizeInvalid());
5527 SetupActiveTreeLayers();
5528 TestEmptyLayer();
5529 TestLayerInMiddleOfViewport();
5530 TestLayerIsLargerThanViewport();
5533 class FakeDrawableLayerImpl: public LayerImpl {
5534 public:
5535 static scoped_ptr<LayerImpl> Create(LayerTreeImpl* tree_impl, int id) {
5536 return make_scoped_ptr(new FakeDrawableLayerImpl(tree_impl, id));
5538 protected:
5539 FakeDrawableLayerImpl(LayerTreeImpl* tree_impl, int id)
5540 : LayerImpl(tree_impl, id) {}
5543 // Only reshape when we know we are going to draw. Otherwise, the reshape
5544 // can leave the window at the wrong size if we never draw and the proper
5545 // viewport size is never set.
5546 TEST_F(LayerTreeHostImplTest, ReshapeNotCalledUntilDraw) {
5547 scoped_refptr<TestContextProvider> provider(TestContextProvider::Create());
5548 scoped_ptr<OutputSurface> output_surface(
5549 FakeOutputSurface::Create3d(provider));
5550 CreateHostImpl(DefaultSettings(), output_surface.Pass());
5552 scoped_ptr<LayerImpl> root =
5553 FakeDrawableLayerImpl::Create(host_impl_->active_tree(), 1);
5554 root->SetBounds(gfx::Size(10, 10));
5555 root->SetDrawsContent(true);
5556 root->SetHasRenderSurface(true);
5557 host_impl_->active_tree()->SetRootLayer(root.Pass());
5558 EXPECT_FALSE(provider->TestContext3d()->reshape_called());
5559 provider->TestContext3d()->clear_reshape_called();
5561 LayerTreeHostImpl::FrameData frame;
5562 host_impl_->SetViewportSize(gfx::Size(10, 10));
5563 host_impl_->SetDeviceScaleFactor(1.f);
5564 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
5565 host_impl_->DrawLayers(&frame);
5566 EXPECT_TRUE(provider->TestContext3d()->reshape_called());
5567 EXPECT_EQ(provider->TestContext3d()->width(), 10);
5568 EXPECT_EQ(provider->TestContext3d()->height(), 10);
5569 EXPECT_EQ(provider->TestContext3d()->scale_factor(), 1.f);
5570 host_impl_->DidDrawAllLayers(frame);
5571 provider->TestContext3d()->clear_reshape_called();
5573 host_impl_->SetViewportSize(gfx::Size(20, 30));
5574 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
5575 host_impl_->DrawLayers(&frame);
5576 EXPECT_TRUE(provider->TestContext3d()->reshape_called());
5577 EXPECT_EQ(provider->TestContext3d()->width(), 20);
5578 EXPECT_EQ(provider->TestContext3d()->height(), 30);
5579 EXPECT_EQ(provider->TestContext3d()->scale_factor(), 1.f);
5580 host_impl_->DidDrawAllLayers(frame);
5581 provider->TestContext3d()->clear_reshape_called();
5583 host_impl_->SetDeviceScaleFactor(2.f);
5584 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
5585 host_impl_->DrawLayers(&frame);
5586 EXPECT_TRUE(provider->TestContext3d()->reshape_called());
5587 EXPECT_EQ(provider->TestContext3d()->width(), 20);
5588 EXPECT_EQ(provider->TestContext3d()->height(), 30);
5589 EXPECT_EQ(provider->TestContext3d()->scale_factor(), 2.f);
5590 host_impl_->DidDrawAllLayers(frame);
5591 provider->TestContext3d()->clear_reshape_called();
5594 // Make sure damage tracking propagates all the way to the graphics context,
5595 // where it should request to swap only the sub-buffer that is damaged.
5596 TEST_F(LayerTreeHostImplTest, PartialSwapReceivesDamageRect) {
5597 scoped_refptr<TestContextProvider> context_provider(
5598 TestContextProvider::Create());
5599 context_provider->BindToCurrentThread();
5600 context_provider->TestContext3d()->set_have_post_sub_buffer(true);
5602 scoped_ptr<FakeOutputSurface> output_surface(
5603 FakeOutputSurface::Create3d(context_provider));
5604 FakeOutputSurface* fake_output_surface = output_surface.get();
5606 // This test creates its own LayerTreeHostImpl, so
5607 // that we can force partial swap enabled.
5608 LayerTreeSettings settings;
5609 settings.renderer_settings.partial_swap_enabled = true;
5610 scoped_ptr<LayerTreeHostImpl> layer_tree_host_impl =
5611 LayerTreeHostImpl::Create(
5612 settings, this, &proxy_, &stats_instrumentation_,
5613 &shared_bitmap_manager_, NULL, &task_graph_runner_, 0);
5614 layer_tree_host_impl->InitializeRenderer(output_surface.Pass());
5615 layer_tree_host_impl->WillBeginImplFrame(
5616 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE));
5617 layer_tree_host_impl->SetViewportSize(gfx::Size(500, 500));
5619 scoped_ptr<LayerImpl> root =
5620 FakeDrawableLayerImpl::Create(layer_tree_host_impl->active_tree(), 1);
5621 root->SetHasRenderSurface(true);
5622 scoped_ptr<LayerImpl> child =
5623 FakeDrawableLayerImpl::Create(layer_tree_host_impl->active_tree(), 2);
5624 child->SetPosition(gfx::PointF(12.f, 13.f));
5625 child->SetBounds(gfx::Size(14, 15));
5626 child->SetDrawsContent(true);
5627 root->SetBounds(gfx::Size(500, 500));
5628 root->SetDrawsContent(true);
5629 root->AddChild(child.Pass());
5630 layer_tree_host_impl->active_tree()->SetRootLayer(root.Pass());
5632 LayerTreeHostImpl::FrameData frame;
5634 // First frame, the entire screen should get swapped.
5635 EXPECT_EQ(DRAW_SUCCESS, layer_tree_host_impl->PrepareToDraw(&frame));
5636 layer_tree_host_impl->DrawLayers(&frame);
5637 layer_tree_host_impl->DidDrawAllLayers(frame);
5638 layer_tree_host_impl->SwapBuffers(frame);
5639 gfx::Rect expected_swap_rect(0, 0, 500, 500);
5640 EXPECT_EQ(expected_swap_rect.ToString(),
5641 fake_output_surface->last_swap_rect().ToString());
5643 // Second frame, only the damaged area should get swapped. Damage should be
5644 // the union of old and new child rects.
5645 // expected damage rect: gfx::Rect(26, 28);
5646 // expected swap rect: vertically flipped, with origin at bottom left corner.
5647 layer_tree_host_impl->active_tree()->root_layer()->children()[0]->SetPosition(
5648 gfx::PointF());
5649 EXPECT_EQ(DRAW_SUCCESS, layer_tree_host_impl->PrepareToDraw(&frame));
5650 layer_tree_host_impl->DrawLayers(&frame);
5651 host_impl_->DidDrawAllLayers(frame);
5652 layer_tree_host_impl->SwapBuffers(frame);
5654 // Make sure that partial swap is constrained to the viewport dimensions
5655 // expected damage rect: gfx::Rect(500, 500);
5656 // expected swap rect: flipped damage rect, but also clamped to viewport
5657 expected_swap_rect = gfx::Rect(0, 500-28, 26, 28);
5658 EXPECT_EQ(expected_swap_rect.ToString(),
5659 fake_output_surface->last_swap_rect().ToString());
5661 layer_tree_host_impl->SetViewportSize(gfx::Size(10, 10));
5662 // This will damage everything.
5663 layer_tree_host_impl->active_tree()->root_layer()->SetBackgroundColor(
5664 SK_ColorBLACK);
5665 EXPECT_EQ(DRAW_SUCCESS, layer_tree_host_impl->PrepareToDraw(&frame));
5666 layer_tree_host_impl->DrawLayers(&frame);
5667 host_impl_->DidDrawAllLayers(frame);
5668 layer_tree_host_impl->SwapBuffers(frame);
5670 expected_swap_rect = gfx::Rect(0, 0, 10, 10);
5671 EXPECT_EQ(expected_swap_rect.ToString(),
5672 fake_output_surface->last_swap_rect().ToString());
5675 TEST_F(LayerTreeHostImplTest, RootLayerDoesntCreateExtraSurface) {
5676 scoped_ptr<LayerImpl> root =
5677 FakeDrawableLayerImpl::Create(host_impl_->active_tree(), 1);
5678 scoped_ptr<LayerImpl> child =
5679 FakeDrawableLayerImpl::Create(host_impl_->active_tree(), 2);
5680 child->SetBounds(gfx::Size(10, 10));
5681 child->SetDrawsContent(true);
5682 root->SetBounds(gfx::Size(10, 10));
5683 root->SetDrawsContent(true);
5684 root->SetHasRenderSurface(true);
5685 root->AddChild(child.Pass());
5687 host_impl_->active_tree()->SetRootLayer(root.Pass());
5689 LayerTreeHostImpl::FrameData frame;
5691 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
5692 EXPECT_EQ(1u, frame.render_surface_layer_list->size());
5693 EXPECT_EQ(1u, frame.render_passes.size());
5694 host_impl_->DidDrawAllLayers(frame);
5697 class FakeLayerWithQuads : public LayerImpl {
5698 public:
5699 static scoped_ptr<LayerImpl> Create(LayerTreeImpl* tree_impl, int id) {
5700 return make_scoped_ptr(new FakeLayerWithQuads(tree_impl, id));
5703 void AppendQuads(RenderPass* render_pass,
5704 AppendQuadsData* append_quads_data) override {
5705 SharedQuadState* shared_quad_state =
5706 render_pass->CreateAndAppendSharedQuadState();
5707 PopulateSharedQuadState(shared_quad_state);
5709 SkColor gray = SkColorSetRGB(100, 100, 100);
5710 gfx::Rect quad_rect(bounds());
5711 gfx::Rect visible_quad_rect(quad_rect);
5712 SolidColorDrawQuad* my_quad =
5713 render_pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
5714 my_quad->SetNew(
5715 shared_quad_state, quad_rect, visible_quad_rect, gray, false);
5718 private:
5719 FakeLayerWithQuads(LayerTreeImpl* tree_impl, int id)
5720 : LayerImpl(tree_impl, id) {}
5723 class MockContext : public TestWebGraphicsContext3D {
5724 public:
5725 MOCK_METHOD1(useProgram, void(GLuint program));
5726 MOCK_METHOD5(uniform4f, void(GLint location,
5727 GLfloat x,
5728 GLfloat y,
5729 GLfloat z,
5730 GLfloat w));
5731 MOCK_METHOD4(uniformMatrix4fv, void(GLint location,
5732 GLsizei count,
5733 GLboolean transpose,
5734 const GLfloat* value));
5735 MOCK_METHOD4(drawElements, void(GLenum mode,
5736 GLsizei count,
5737 GLenum type,
5738 GLintptr offset));
5739 MOCK_METHOD1(enable, void(GLenum cap));
5740 MOCK_METHOD1(disable, void(GLenum cap));
5741 MOCK_METHOD4(scissor, void(GLint x,
5742 GLint y,
5743 GLsizei width,
5744 GLsizei height));
5747 class MockContextHarness {
5748 private:
5749 MockContext* context_;
5751 public:
5752 explicit MockContextHarness(MockContext* context)
5753 : context_(context) {
5754 context_->set_have_post_sub_buffer(true);
5756 // Catch "uninteresting" calls
5757 EXPECT_CALL(*context_, useProgram(_))
5758 .Times(0);
5760 EXPECT_CALL(*context_, drawElements(_, _, _, _))
5761 .Times(0);
5763 // These are not asserted
5764 EXPECT_CALL(*context_, uniformMatrix4fv(_, _, _, _))
5765 .WillRepeatedly(Return());
5767 EXPECT_CALL(*context_, uniform4f(_, _, _, _, _))
5768 .WillRepeatedly(Return());
5770 // Any un-sanctioned calls to enable() are OK
5771 EXPECT_CALL(*context_, enable(_))
5772 .WillRepeatedly(Return());
5774 // Any un-sanctioned calls to disable() are OK
5775 EXPECT_CALL(*context_, disable(_))
5776 .WillRepeatedly(Return());
5779 void MustDrawSolidQuad() {
5780 EXPECT_CALL(*context_, drawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0))
5781 .WillOnce(Return())
5782 .RetiresOnSaturation();
5784 EXPECT_CALL(*context_, useProgram(_))
5785 .WillOnce(Return())
5786 .RetiresOnSaturation();
5789 void MustSetScissor(int x, int y, int width, int height) {
5790 EXPECT_CALL(*context_, enable(GL_SCISSOR_TEST))
5791 .WillRepeatedly(Return());
5793 EXPECT_CALL(*context_, scissor(x, y, width, height))
5794 .Times(AtLeast(1))
5795 .WillRepeatedly(Return());
5798 void MustSetNoScissor() {
5799 EXPECT_CALL(*context_, disable(GL_SCISSOR_TEST))
5800 .WillRepeatedly(Return());
5802 EXPECT_CALL(*context_, enable(GL_SCISSOR_TEST))
5803 .Times(0);
5805 EXPECT_CALL(*context_, scissor(_, _, _, _))
5806 .Times(0);
5810 TEST_F(LayerTreeHostImplTest, NoPartialSwap) {
5811 scoped_ptr<MockContext> mock_context_owned(new MockContext);
5812 MockContext* mock_context = mock_context_owned.get();
5813 MockContextHarness harness(mock_context);
5815 // Run test case
5816 LayerTreeSettings settings = DefaultSettings();
5817 settings.renderer_settings.partial_swap_enabled = false;
5818 CreateHostImpl(settings,
5819 FakeOutputSurface::Create3d(mock_context_owned.Pass()));
5820 SetupRootLayerImpl(FakeLayerWithQuads::Create(host_impl_->active_tree(), 1));
5822 // Without partial swap, and no clipping, no scissor is set.
5823 harness.MustDrawSolidQuad();
5824 harness.MustSetNoScissor();
5826 LayerTreeHostImpl::FrameData frame;
5827 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
5828 host_impl_->DrawLayers(&frame);
5829 host_impl_->DidDrawAllLayers(frame);
5831 Mock::VerifyAndClearExpectations(&mock_context);
5833 // Without partial swap, but a layer does clip its subtree, one scissor is
5834 // set.
5835 host_impl_->active_tree()->root_layer()->SetMasksToBounds(true);
5836 harness.MustDrawSolidQuad();
5837 harness.MustSetScissor(0, 0, 10, 10);
5839 LayerTreeHostImpl::FrameData frame;
5840 host_impl_->active_tree()->BuildPropertyTreesForTesting();
5841 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
5842 host_impl_->DrawLayers(&frame);
5843 host_impl_->DidDrawAllLayers(frame);
5845 Mock::VerifyAndClearExpectations(&mock_context);
5848 TEST_F(LayerTreeHostImplTest, PartialSwap) {
5849 scoped_ptr<MockContext> context_owned(new MockContext);
5850 MockContext* mock_context = context_owned.get();
5851 MockContextHarness harness(mock_context);
5853 LayerTreeSettings settings = DefaultSettings();
5854 settings.renderer_settings.partial_swap_enabled = true;
5855 CreateHostImpl(settings, FakeOutputSurface::Create3d(context_owned.Pass()));
5856 SetupRootLayerImpl(FakeLayerWithQuads::Create(host_impl_->active_tree(), 1));
5858 // The first frame is not a partially-swapped one. No scissor should be set.
5859 harness.MustSetNoScissor();
5860 harness.MustDrawSolidQuad();
5862 LayerTreeHostImpl::FrameData frame;
5863 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
5864 host_impl_->DrawLayers(&frame);
5865 host_impl_->DidDrawAllLayers(frame);
5867 Mock::VerifyAndClearExpectations(&mock_context);
5869 // Damage a portion of the frame.
5870 host_impl_->active_tree()->root_layer()->SetUpdateRect(
5871 gfx::Rect(0, 0, 2, 3));
5873 // The second frame will be partially-swapped (the y coordinates are flipped).
5874 harness.MustSetScissor(0, 7, 2, 3);
5875 harness.MustDrawSolidQuad();
5877 LayerTreeHostImpl::FrameData frame;
5878 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
5879 host_impl_->DrawLayers(&frame);
5880 host_impl_->DidDrawAllLayers(frame);
5882 Mock::VerifyAndClearExpectations(&mock_context);
5885 static scoped_ptr<LayerTreeHostImpl> SetupLayersForOpacity(
5886 bool partial_swap,
5887 LayerTreeHostImplClient* client,
5888 Proxy* proxy,
5889 SharedBitmapManager* manager,
5890 TaskGraphRunner* task_graph_runner,
5891 RenderingStatsInstrumentation* stats_instrumentation) {
5892 scoped_refptr<TestContextProvider> provider(TestContextProvider::Create());
5893 scoped_ptr<OutputSurface> output_surface(
5894 FakeOutputSurface::Create3d(provider));
5895 provider->BindToCurrentThread();
5896 provider->TestContext3d()->set_have_post_sub_buffer(true);
5898 LayerTreeSettings settings;
5899 settings.renderer_settings.partial_swap_enabled = partial_swap;
5900 scoped_ptr<LayerTreeHostImpl> my_host_impl =
5901 LayerTreeHostImpl::Create(settings, client, proxy, stats_instrumentation,
5902 manager, nullptr, task_graph_runner, 0);
5903 my_host_impl->InitializeRenderer(output_surface.Pass());
5904 my_host_impl->WillBeginImplFrame(
5905 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE));
5906 my_host_impl->SetViewportSize(gfx::Size(100, 100));
5909 Layers are created as follows:
5911 +--------------------+
5912 | 1 |
5913 | +-----------+ |
5914 | | 2 | |
5915 | | +-------------------+
5916 | | | 3 |
5917 | | +-------------------+
5918 | | | |
5919 | +-----------+ |
5922 +--------------------+
5924 Layers 1, 2 have render surfaces
5926 scoped_ptr<LayerImpl> root =
5927 LayerImpl::Create(my_host_impl->active_tree(), 1);
5928 scoped_ptr<LayerImpl> child =
5929 LayerImpl::Create(my_host_impl->active_tree(), 2);
5930 scoped_ptr<LayerImpl> grand_child =
5931 FakeLayerWithQuads::Create(my_host_impl->active_tree(), 3);
5933 gfx::Rect root_rect(0, 0, 100, 100);
5934 gfx::Rect child_rect(10, 10, 50, 50);
5935 gfx::Rect grand_child_rect(5, 5, 150, 150);
5937 root->SetHasRenderSurface(true);
5938 root->SetPosition(root_rect.origin());
5939 root->SetBounds(root_rect.size());
5940 root->draw_properties().visible_layer_rect = root_rect;
5941 root->SetDrawsContent(false);
5942 root->render_surface()->SetContentRect(gfx::Rect(root_rect.size()));
5944 child->SetPosition(gfx::PointF(child_rect.x(), child_rect.y()));
5945 child->SetOpacity(0.5f);
5946 child->SetBounds(gfx::Size(child_rect.width(), child_rect.height()));
5947 child->draw_properties().visible_layer_rect = child_rect;
5948 child->SetDrawsContent(false);
5949 child->SetHasRenderSurface(true);
5951 grand_child->SetPosition(grand_child_rect.origin());
5952 grand_child->SetBounds(grand_child_rect.size());
5953 grand_child->draw_properties().visible_layer_rect = grand_child_rect;
5954 grand_child->SetDrawsContent(true);
5956 child->AddChild(grand_child.Pass());
5957 root->AddChild(child.Pass());
5959 my_host_impl->active_tree()->SetRootLayer(root.Pass());
5960 return my_host_impl.Pass();
5963 TEST_F(LayerTreeHostImplTest, ContributingLayerEmptyScissorPartialSwap) {
5964 TestSharedBitmapManager shared_bitmap_manager;
5965 TestTaskGraphRunner task_graph_runner;
5966 scoped_ptr<LayerTreeHostImpl> my_host_impl =
5967 SetupLayersForOpacity(true, this, &proxy_, &shared_bitmap_manager,
5968 &task_graph_runner, &stats_instrumentation_);
5970 LayerTreeHostImpl::FrameData frame;
5971 EXPECT_EQ(DRAW_SUCCESS, my_host_impl->PrepareToDraw(&frame));
5973 // Verify all quads have been computed
5974 ASSERT_EQ(2U, frame.render_passes.size());
5975 ASSERT_EQ(1U, frame.render_passes[0]->quad_list.size());
5976 ASSERT_EQ(1U, frame.render_passes[1]->quad_list.size());
5977 EXPECT_EQ(DrawQuad::SOLID_COLOR,
5978 frame.render_passes[0]->quad_list.front()->material);
5979 EXPECT_EQ(DrawQuad::RENDER_PASS,
5980 frame.render_passes[1]->quad_list.front()->material);
5982 my_host_impl->DrawLayers(&frame);
5983 my_host_impl->DidDrawAllLayers(frame);
5987 TEST_F(LayerTreeHostImplTest, ContributingLayerEmptyScissorNoPartialSwap) {
5988 TestSharedBitmapManager shared_bitmap_manager;
5989 TestTaskGraphRunner task_graph_runner;
5990 scoped_ptr<LayerTreeHostImpl> my_host_impl =
5991 SetupLayersForOpacity(false, this, &proxy_, &shared_bitmap_manager,
5992 &task_graph_runner, &stats_instrumentation_);
5994 LayerTreeHostImpl::FrameData frame;
5995 EXPECT_EQ(DRAW_SUCCESS, my_host_impl->PrepareToDraw(&frame));
5997 // Verify all quads have been computed
5998 ASSERT_EQ(2U, frame.render_passes.size());
5999 ASSERT_EQ(1U, frame.render_passes[0]->quad_list.size());
6000 ASSERT_EQ(1U, frame.render_passes[1]->quad_list.size());
6001 EXPECT_EQ(DrawQuad::SOLID_COLOR,
6002 frame.render_passes[0]->quad_list.front()->material);
6003 EXPECT_EQ(DrawQuad::RENDER_PASS,
6004 frame.render_passes[1]->quad_list.front()->material);
6006 my_host_impl->DrawLayers(&frame);
6007 my_host_impl->DidDrawAllLayers(frame);
6011 TEST_F(LayerTreeHostImplTest, LayersFreeTextures) {
6012 scoped_ptr<TestWebGraphicsContext3D> context =
6013 TestWebGraphicsContext3D::Create();
6014 TestWebGraphicsContext3D* context3d = context.get();
6015 scoped_ptr<OutputSurface> output_surface(
6016 FakeOutputSurface::Create3d(context.Pass()));
6017 CreateHostImpl(DefaultSettings(), output_surface.Pass());
6019 scoped_ptr<LayerImpl> root_layer =
6020 LayerImpl::Create(host_impl_->active_tree(), 1);
6021 root_layer->SetBounds(gfx::Size(10, 10));
6022 root_layer->SetHasRenderSurface(true);
6024 scoped_refptr<VideoFrame> softwareFrame =
6025 media::VideoFrame::CreateColorFrame(
6026 gfx::Size(4, 4), 0x80, 0x80, 0x80, base::TimeDelta());
6027 FakeVideoFrameProvider provider;
6028 provider.set_frame(softwareFrame);
6029 scoped_ptr<VideoLayerImpl> video_layer = VideoLayerImpl::Create(
6030 host_impl_->active_tree(), 4, &provider, media::VIDEO_ROTATION_0);
6031 video_layer->SetBounds(gfx::Size(10, 10));
6032 video_layer->SetDrawsContent(true);
6033 root_layer->AddChild(video_layer.Pass());
6035 scoped_ptr<IOSurfaceLayerImpl> io_surface_layer =
6036 IOSurfaceLayerImpl::Create(host_impl_->active_tree(), 5);
6037 io_surface_layer->SetBounds(gfx::Size(10, 10));
6038 io_surface_layer->SetDrawsContent(true);
6039 io_surface_layer->SetIOSurfaceProperties(1, gfx::Size(10, 10));
6040 root_layer->AddChild(io_surface_layer.Pass());
6042 host_impl_->active_tree()->SetRootLayer(root_layer.Pass());
6044 EXPECT_EQ(0u, context3d->NumTextures());
6046 LayerTreeHostImpl::FrameData frame;
6047 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
6048 host_impl_->DrawLayers(&frame);
6049 host_impl_->DidDrawAllLayers(frame);
6050 host_impl_->SwapBuffers(frame);
6052 EXPECT_GT(context3d->NumTextures(), 0u);
6054 // Kill the layer tree.
6055 host_impl_->active_tree()->SetRootLayer(
6056 LayerImpl::Create(host_impl_->active_tree(), 100));
6057 // There should be no textures left in use after.
6058 EXPECT_EQ(0u, context3d->NumTextures());
6061 class MockDrawQuadsToFillScreenContext : public TestWebGraphicsContext3D {
6062 public:
6063 MOCK_METHOD1(useProgram, void(GLuint program));
6064 MOCK_METHOD4(drawElements, void(GLenum mode,
6065 GLsizei count,
6066 GLenum type,
6067 GLintptr offset));
6070 TEST_F(LayerTreeHostImplTest, HasTransparentBackground) {
6071 scoped_ptr<MockDrawQuadsToFillScreenContext> mock_context_owned(
6072 new MockDrawQuadsToFillScreenContext);
6073 MockDrawQuadsToFillScreenContext* mock_context = mock_context_owned.get();
6075 // Run test case
6076 LayerTreeSettings settings = DefaultSettings();
6077 settings.renderer_settings.partial_swap_enabled = false;
6078 CreateHostImpl(settings,
6079 FakeOutputSurface::Create3d(mock_context_owned.Pass()));
6080 SetupRootLayerImpl(LayerImpl::Create(host_impl_->active_tree(), 1));
6081 host_impl_->active_tree()->set_background_color(SK_ColorWHITE);
6083 // Verify one quad is drawn when transparent background set is not set.
6084 host_impl_->active_tree()->set_has_transparent_background(false);
6085 EXPECT_CALL(*mock_context, useProgram(_))
6086 .Times(1);
6087 EXPECT_CALL(*mock_context, drawElements(_, _, _, _))
6088 .Times(1);
6089 LayerTreeHostImpl::FrameData frame;
6090 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
6091 host_impl_->DrawLayers(&frame);
6092 host_impl_->DidDrawAllLayers(frame);
6093 Mock::VerifyAndClearExpectations(&mock_context);
6095 // Verify no quads are drawn when transparent background is set.
6096 host_impl_->active_tree()->set_has_transparent_background(true);
6097 host_impl_->SetFullRootLayerDamage();
6098 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
6099 host_impl_->DrawLayers(&frame);
6100 host_impl_->DidDrawAllLayers(frame);
6101 Mock::VerifyAndClearExpectations(&mock_context);
6104 class LayerTreeHostImplTestWithDelegatingRenderer
6105 : public LayerTreeHostImplTest {
6106 protected:
6107 scoped_ptr<OutputSurface> CreateOutputSurface() override {
6108 return FakeOutputSurface::CreateDelegating3d();
6111 void DrawFrameAndTestDamage(const gfx::Rect& expected_damage) {
6112 bool expect_to_draw = !expected_damage.IsEmpty();
6114 LayerTreeHostImpl::FrameData frame;
6115 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
6117 if (!expect_to_draw) {
6118 // With no damage, we don't draw, and no quads are created.
6119 ASSERT_EQ(0u, frame.render_passes.size());
6120 } else {
6121 ASSERT_EQ(1u, frame.render_passes.size());
6123 // Verify the damage rect for the root render pass.
6124 const RenderPass* root_render_pass = frame.render_passes.back();
6125 EXPECT_EQ(expected_damage, root_render_pass->damage_rect);
6127 // Verify the root and child layers' quads are generated and not being
6128 // culled.
6129 ASSERT_EQ(2u, root_render_pass->quad_list.size());
6131 LayerImpl* child = host_impl_->active_tree()->root_layer()->children()[0];
6132 gfx::Rect expected_child_visible_rect(child->bounds());
6133 EXPECT_EQ(expected_child_visible_rect,
6134 root_render_pass->quad_list.front()->visible_rect);
6136 LayerImpl* root = host_impl_->active_tree()->root_layer();
6137 gfx::Rect expected_root_visible_rect(root->bounds());
6138 EXPECT_EQ(expected_root_visible_rect,
6139 root_render_pass->quad_list.ElementAt(1)->visible_rect);
6142 host_impl_->DrawLayers(&frame);
6143 host_impl_->DidDrawAllLayers(frame);
6144 EXPECT_EQ(expect_to_draw, host_impl_->SwapBuffers(frame));
6148 TEST_F(LayerTreeHostImplTestWithDelegatingRenderer, FrameIncludesDamageRect) {
6149 scoped_ptr<SolidColorLayerImpl> root =
6150 SolidColorLayerImpl::Create(host_impl_->active_tree(), 1);
6151 root->SetPosition(gfx::PointF());
6152 root->SetBounds(gfx::Size(10, 10));
6153 root->SetDrawsContent(true);
6154 root->SetHasRenderSurface(true);
6156 // Child layer is in the bottom right corner.
6157 scoped_ptr<SolidColorLayerImpl> child =
6158 SolidColorLayerImpl::Create(host_impl_->active_tree(), 2);
6159 child->SetPosition(gfx::PointF(9.f, 9.f));
6160 child->SetBounds(gfx::Size(1, 1));
6161 child->SetDrawsContent(true);
6162 root->AddChild(child.Pass());
6164 host_impl_->active_tree()->SetRootLayer(root.Pass());
6166 // Draw a frame. In the first frame, the entire viewport should be damaged.
6167 gfx::Rect full_frame_damage(host_impl_->DrawViewportSize());
6168 DrawFrameAndTestDamage(full_frame_damage);
6170 // The second frame has damage that doesn't touch the child layer. Its quads
6171 // should still be generated.
6172 gfx::Rect small_damage = gfx::Rect(0, 0, 1, 1);
6173 host_impl_->active_tree()->root_layer()->SetUpdateRect(small_damage);
6174 DrawFrameAndTestDamage(small_damage);
6176 // The third frame should have no damage, so no quads should be generated.
6177 gfx::Rect no_damage;
6178 DrawFrameAndTestDamage(no_damage);
6181 // TODO(reveman): Remove this test and the ability to prevent on demand raster
6182 // when delegating renderer supports PictureDrawQuads. crbug.com/342121
6183 TEST_F(LayerTreeHostImplTestWithDelegatingRenderer, PreventRasterizeOnDemand) {
6184 LayerTreeSettings settings;
6185 CreateHostImpl(settings, CreateOutputSurface());
6186 EXPECT_FALSE(host_impl_->GetRendererCapabilities().allow_rasterize_on_demand);
6189 class FakeMaskLayerImpl : public LayerImpl {
6190 public:
6191 static scoped_ptr<FakeMaskLayerImpl> Create(LayerTreeImpl* tree_impl,
6192 int id) {
6193 return make_scoped_ptr(new FakeMaskLayerImpl(tree_impl, id));
6196 void GetContentsResourceId(ResourceId* resource_id,
6197 gfx::Size* resource_size) const override {
6198 *resource_id = 0;
6201 private:
6202 FakeMaskLayerImpl(LayerTreeImpl* tree_impl, int id)
6203 : LayerImpl(tree_impl, id) {}
6206 class GLRendererWithSetupQuadForAntialiasing : public GLRenderer {
6207 public:
6208 using GLRenderer::ShouldAntialiasQuad;
6211 TEST_F(LayerTreeHostImplTest, FarAwayQuadsDontNeedAA) {
6212 // Due to precision issues (especially on Android), sometimes far
6213 // away quads can end up thinking they need AA.
6214 float device_scale_factor = 4.f / 3.f;
6215 host_impl_->SetDeviceScaleFactor(device_scale_factor);
6216 gfx::Size root_size(2000, 1000);
6217 gfx::Size device_viewport_size =
6218 gfx::ToCeiledSize(gfx::ScaleSize(root_size, device_scale_factor));
6219 host_impl_->SetViewportSize(device_viewport_size);
6221 host_impl_->CreatePendingTree();
6222 host_impl_->pending_tree()->PushPageScaleFromMainThread(1.f, 1.f / 16.f,
6223 16.f);
6225 scoped_ptr<LayerImpl> scoped_root =
6226 LayerImpl::Create(host_impl_->pending_tree(), 1);
6227 LayerImpl* root = scoped_root.get();
6228 root->SetHasRenderSurface(true);
6230 host_impl_->pending_tree()->SetRootLayer(scoped_root.Pass());
6232 scoped_ptr<LayerImpl> scoped_scrolling_layer =
6233 LayerImpl::Create(host_impl_->pending_tree(), 2);
6234 LayerImpl* scrolling_layer = scoped_scrolling_layer.get();
6235 root->AddChild(scoped_scrolling_layer.Pass());
6237 gfx::Size content_layer_bounds(100000, 100);
6238 gfx::Size pile_tile_size(3000, 3000);
6239 scoped_refptr<FakePicturePileImpl> pile(FakePicturePileImpl::CreateFilledPile(
6240 pile_tile_size, content_layer_bounds));
6242 scoped_ptr<FakePictureLayerImpl> scoped_content_layer =
6243 FakePictureLayerImpl::CreateWithRasterSource(host_impl_->pending_tree(),
6244 3, pile);
6245 LayerImpl* content_layer = scoped_content_layer.get();
6246 scrolling_layer->AddChild(scoped_content_layer.Pass());
6247 content_layer->SetBounds(content_layer_bounds);
6248 content_layer->SetDrawsContent(true);
6250 root->SetBounds(root_size);
6252 gfx::ScrollOffset scroll_offset(100000, 0);
6253 scrolling_layer->SetScrollClipLayer(root->id());
6254 scrolling_layer->PushScrollOffsetFromMainThread(scroll_offset);
6256 host_impl_->ActivateSyncTree();
6258 bool update_lcd_text = false;
6259 host_impl_->active_tree()->UpdateDrawProperties(update_lcd_text);
6260 ASSERT_EQ(1u, host_impl_->active_tree()->RenderSurfaceLayerList().size());
6262 LayerTreeHostImpl::FrameData frame;
6263 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
6265 ASSERT_EQ(1u, frame.render_passes.size());
6266 ASSERT_LE(1u, frame.render_passes[0]->quad_list.size());
6267 const DrawQuad* quad = frame.render_passes[0]->quad_list.front();
6269 bool clipped = false, force_aa = false;
6270 gfx::QuadF device_layer_quad = MathUtil::MapQuad(
6271 quad->shared_quad_state->quad_to_target_transform,
6272 gfx::QuadF(gfx::RectF(quad->shared_quad_state->visible_quad_layer_rect)),
6273 &clipped);
6274 EXPECT_FALSE(clipped);
6275 bool antialiased =
6276 GLRendererWithSetupQuadForAntialiasing::ShouldAntialiasQuad(
6277 device_layer_quad, clipped, force_aa);
6278 EXPECT_FALSE(antialiased);
6280 host_impl_->DrawLayers(&frame);
6281 host_impl_->DidDrawAllLayers(frame);
6285 class CompositorFrameMetadataTest : public LayerTreeHostImplTest {
6286 public:
6287 CompositorFrameMetadataTest()
6288 : swap_buffers_complete_(0) {}
6290 void DidSwapBuffersCompleteOnImplThread() override {
6291 swap_buffers_complete_++;
6294 int swap_buffers_complete_;
6297 TEST_F(CompositorFrameMetadataTest, CompositorFrameAckCountsAsSwapComplete) {
6298 SetupRootLayerImpl(FakeLayerWithQuads::Create(host_impl_->active_tree(), 1));
6300 LayerTreeHostImpl::FrameData frame;
6301 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
6302 host_impl_->DrawLayers(&frame);
6303 host_impl_->DidDrawAllLayers(frame);
6305 CompositorFrameAck ack;
6306 host_impl_->ReclaimResources(&ack);
6307 host_impl_->DidSwapBuffersComplete();
6308 EXPECT_EQ(swap_buffers_complete_, 1);
6311 class CountingSoftwareDevice : public SoftwareOutputDevice {
6312 public:
6313 CountingSoftwareDevice() : frames_began_(0), frames_ended_(0) {}
6315 SkCanvas* BeginPaint(const gfx::Rect& damage_rect) override {
6316 ++frames_began_;
6317 return SoftwareOutputDevice::BeginPaint(damage_rect);
6319 void EndPaint() override {
6320 SoftwareOutputDevice::EndPaint();
6321 ++frames_ended_;
6324 int frames_began_, frames_ended_;
6327 TEST_F(LayerTreeHostImplTest, ForcedDrawToSoftwareDeviceBasicRender) {
6328 // No main thread evictions in resourceless software mode.
6329 set_reduce_memory_result(false);
6330 CountingSoftwareDevice* software_device = new CountingSoftwareDevice();
6331 EXPECT_TRUE(CreateHostImpl(
6332 DefaultSettings(),
6333 FakeOutputSurface::CreateSoftware(make_scoped_ptr(software_device))));
6334 host_impl_->SetViewportSize(gfx::Size(50, 50));
6336 SetupScrollAndContentsLayers(gfx::Size(100, 100));
6338 const gfx::Transform external_transform;
6339 const gfx::Rect external_viewport;
6340 const gfx::Rect external_clip;
6341 const bool resourceless_software_draw = true;
6342 host_impl_->SetExternalDrawConstraints(external_transform,
6343 external_viewport,
6344 external_clip,
6345 external_viewport,
6346 external_transform,
6347 resourceless_software_draw);
6349 EXPECT_EQ(0, software_device->frames_began_);
6350 EXPECT_EQ(0, software_device->frames_ended_);
6352 DrawFrame();
6354 EXPECT_EQ(1, software_device->frames_began_);
6355 EXPECT_EQ(1, software_device->frames_ended_);
6357 // Call another API method that is likely to hit nullptr in this mode.
6358 scoped_refptr<base::trace_event::TracedValue> state =
6359 make_scoped_refptr(new base::trace_event::TracedValue());
6360 host_impl_->ActivationStateAsValueInto(state.get());
6363 TEST_F(LayerTreeHostImplTest,
6364 ForcedDrawToSoftwareDeviceSkipsUnsupportedLayers) {
6365 set_reduce_memory_result(false);
6366 EXPECT_TRUE(CreateHostImpl(DefaultSettings(),
6367 FakeOutputSurface::CreateSoftware(
6368 make_scoped_ptr(new CountingSoftwareDevice))));
6370 const gfx::Transform external_transform;
6371 const gfx::Rect external_viewport;
6372 const gfx::Rect external_clip;
6373 const bool resourceless_software_draw = true;
6374 host_impl_->SetExternalDrawConstraints(external_transform,
6375 external_viewport,
6376 external_clip,
6377 external_viewport,
6378 external_transform,
6379 resourceless_software_draw);
6381 // SolidColorLayerImpl will be drawn.
6382 scoped_ptr<SolidColorLayerImpl> root_layer =
6383 SolidColorLayerImpl::Create(host_impl_->active_tree(), 1);
6385 // VideoLayerImpl will not be drawn.
6386 FakeVideoFrameProvider provider;
6387 scoped_ptr<VideoLayerImpl> video_layer = VideoLayerImpl::Create(
6388 host_impl_->active_tree(), 2, &provider, media::VIDEO_ROTATION_0);
6389 video_layer->SetBounds(gfx::Size(10, 10));
6390 video_layer->SetDrawsContent(true);
6391 root_layer->AddChild(video_layer.Pass());
6392 SetupRootLayerImpl(root_layer.Pass());
6394 LayerTreeHostImpl::FrameData frame;
6395 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
6396 host_impl_->DrawLayers(&frame);
6397 host_impl_->DidDrawAllLayers(frame);
6399 EXPECT_EQ(1u, frame.will_draw_layers.size());
6400 EXPECT_EQ(host_impl_->active_tree()->root_layer(), frame.will_draw_layers[0]);
6403 // Checks that we have a non-0 default allocation if we pass a context that
6404 // doesn't support memory management extensions.
6405 TEST_F(LayerTreeHostImplTest, DefaultMemoryAllocation) {
6406 LayerTreeSettings settings;
6407 host_impl_ = LayerTreeHostImpl::Create(
6408 settings, this, &proxy_, &stats_instrumentation_, &shared_bitmap_manager_,
6409 &gpu_memory_buffer_manager_, &task_graph_runner_, 0);
6411 scoped_ptr<OutputSurface> output_surface(
6412 FakeOutputSurface::Create3d(TestWebGraphicsContext3D::Create()));
6413 host_impl_->InitializeRenderer(output_surface.Pass());
6414 EXPECT_LT(0ul, host_impl_->memory_allocation_limit_bytes());
6417 TEST_F(LayerTreeHostImplTest, RequireHighResWhenVisible) {
6418 ASSERT_TRUE(host_impl_->active_tree());
6420 // RequiresHighResToDraw is set when new output surface is used.
6421 EXPECT_TRUE(host_impl_->RequiresHighResToDraw());
6423 host_impl_->ResetRequiresHighResToDraw();
6425 host_impl_->SetVisible(false);
6426 EXPECT_FALSE(host_impl_->RequiresHighResToDraw());
6427 host_impl_->SetVisible(true);
6428 EXPECT_TRUE(host_impl_->RequiresHighResToDraw());
6429 host_impl_->SetVisible(false);
6430 EXPECT_TRUE(host_impl_->RequiresHighResToDraw());
6432 host_impl_->ResetRequiresHighResToDraw();
6434 EXPECT_FALSE(host_impl_->RequiresHighResToDraw());
6435 host_impl_->SetVisible(true);
6436 EXPECT_TRUE(host_impl_->RequiresHighResToDraw());
6439 TEST_F(LayerTreeHostImplTest, RequireHighResAfterGpuRasterizationToggles) {
6440 ASSERT_TRUE(host_impl_->active_tree());
6441 EXPECT_FALSE(host_impl_->use_gpu_rasterization());
6443 // RequiresHighResToDraw is set when new output surface is used.
6444 EXPECT_TRUE(host_impl_->RequiresHighResToDraw());
6446 host_impl_->ResetRequiresHighResToDraw();
6448 host_impl_->SetContentIsSuitableForGpuRasterization(true);
6449 host_impl_->SetHasGpuRasterizationTrigger(false);
6450 host_impl_->UpdateTreeResourcesForGpuRasterizationIfNeeded();
6451 EXPECT_FALSE(host_impl_->RequiresHighResToDraw());
6452 host_impl_->SetHasGpuRasterizationTrigger(true);
6453 host_impl_->UpdateTreeResourcesForGpuRasterizationIfNeeded();
6454 EXPECT_TRUE(host_impl_->RequiresHighResToDraw());
6455 host_impl_->SetHasGpuRasterizationTrigger(false);
6456 host_impl_->UpdateTreeResourcesForGpuRasterizationIfNeeded();
6457 EXPECT_TRUE(host_impl_->RequiresHighResToDraw());
6459 host_impl_->ResetRequiresHighResToDraw();
6461 EXPECT_FALSE(host_impl_->RequiresHighResToDraw());
6462 host_impl_->SetHasGpuRasterizationTrigger(true);
6463 host_impl_->UpdateTreeResourcesForGpuRasterizationIfNeeded();
6464 EXPECT_TRUE(host_impl_->RequiresHighResToDraw());
6467 class LayerTreeHostImplTestPrepareTiles : public LayerTreeHostImplTest {
6468 public:
6469 void SetUp() override {
6470 fake_host_impl_ =
6471 new FakeLayerTreeHostImpl(LayerTreeSettings(), &proxy_,
6472 &shared_bitmap_manager_, &task_graph_runner_);
6473 host_impl_.reset(fake_host_impl_);
6474 host_impl_->InitializeRenderer(CreateOutputSurface());
6475 host_impl_->SetViewportSize(gfx::Size(10, 10));
6478 FakeLayerTreeHostImpl* fake_host_impl_;
6481 TEST_F(LayerTreeHostImplTestPrepareTiles, PrepareTilesWhenInvisible) {
6482 fake_host_impl_->DidModifyTilePriorities();
6483 EXPECT_TRUE(fake_host_impl_->prepare_tiles_needed());
6484 fake_host_impl_->SetVisible(false);
6485 EXPECT_FALSE(fake_host_impl_->prepare_tiles_needed());
6488 TEST_F(LayerTreeHostImplTest, UIResourceManagement) {
6489 scoped_ptr<TestWebGraphicsContext3D> context =
6490 TestWebGraphicsContext3D::Create();
6491 TestWebGraphicsContext3D* context3d = context.get();
6492 scoped_ptr<FakeOutputSurface> output_surface = FakeOutputSurface::Create3d();
6493 CreateHostImpl(DefaultSettings(), output_surface.Pass());
6495 EXPECT_EQ(0u, context3d->NumTextures());
6497 UIResourceId ui_resource_id = 1;
6498 bool is_opaque = false;
6499 UIResourceBitmap bitmap(gfx::Size(1, 1), is_opaque);
6500 host_impl_->CreateUIResource(ui_resource_id, bitmap);
6501 EXPECT_EQ(1u, context3d->NumTextures());
6502 ResourceId id1 = host_impl_->ResourceIdForUIResource(ui_resource_id);
6503 EXPECT_NE(0u, id1);
6505 // Multiple requests with the same id is allowed. The previous texture is
6506 // deleted.
6507 host_impl_->CreateUIResource(ui_resource_id, bitmap);
6508 EXPECT_EQ(1u, context3d->NumTextures());
6509 ResourceId id2 = host_impl_->ResourceIdForUIResource(ui_resource_id);
6510 EXPECT_NE(0u, id2);
6511 EXPECT_NE(id1, id2);
6513 // Deleting invalid UIResourceId is allowed and does not change state.
6514 host_impl_->DeleteUIResource(-1);
6515 EXPECT_EQ(1u, context3d->NumTextures());
6517 // Should return zero for invalid UIResourceId. Number of textures should
6518 // not change.
6519 EXPECT_EQ(0u, host_impl_->ResourceIdForUIResource(-1));
6520 EXPECT_EQ(1u, context3d->NumTextures());
6522 host_impl_->DeleteUIResource(ui_resource_id);
6523 EXPECT_EQ(0u, host_impl_->ResourceIdForUIResource(ui_resource_id));
6524 EXPECT_EQ(0u, context3d->NumTextures());
6526 // Should not change state for multiple deletion on one UIResourceId
6527 host_impl_->DeleteUIResource(ui_resource_id);
6528 EXPECT_EQ(0u, context3d->NumTextures());
6531 TEST_F(LayerTreeHostImplTest, CreateETC1UIResource) {
6532 scoped_ptr<TestWebGraphicsContext3D> context =
6533 TestWebGraphicsContext3D::Create();
6534 TestWebGraphicsContext3D* context3d = context.get();
6535 CreateHostImpl(DefaultSettings(), FakeOutputSurface::Create3d());
6537 EXPECT_EQ(0u, context3d->NumTextures());
6539 gfx::Size size(4, 4);
6540 // SkImageInfo has no support for ETC1. The |info| below contains the right
6541 // total pixel size for the bitmap but not the right height and width. The
6542 // correct width/height are passed directly to UIResourceBitmap.
6543 SkImageInfo info =
6544 SkImageInfo::Make(4, 2, kAlpha_8_SkColorType, kPremul_SkAlphaType);
6545 skia::RefPtr<SkPixelRef> pixel_ref =
6546 skia::AdoptRef(SkMallocPixelRef::NewAllocate(info, 0, 0));
6547 pixel_ref->setImmutable();
6548 UIResourceBitmap bitmap(pixel_ref, size);
6549 UIResourceId ui_resource_id = 1;
6550 host_impl_->CreateUIResource(ui_resource_id, bitmap);
6551 EXPECT_EQ(1u, context3d->NumTextures());
6552 ResourceId id1 = host_impl_->ResourceIdForUIResource(ui_resource_id);
6553 EXPECT_NE(0u, id1);
6556 void ShutdownReleasesContext_Callback(scoped_ptr<CopyOutputResult> result) {
6559 TEST_F(LayerTreeHostImplTest, ShutdownReleasesContext) {
6560 scoped_refptr<TestContextProvider> context_provider =
6561 TestContextProvider::Create();
6563 CreateHostImpl(DefaultSettings(),
6564 FakeOutputSurface::Create3d(context_provider));
6566 SetupRootLayerImpl(LayerImpl::Create(host_impl_->active_tree(), 1));
6568 ScopedPtrVector<CopyOutputRequest> requests;
6569 requests.push_back(CopyOutputRequest::CreateRequest(
6570 base::Bind(&ShutdownReleasesContext_Callback)));
6572 host_impl_->active_tree()->root_layer()->PassCopyRequests(&requests);
6574 LayerTreeHostImpl::FrameData frame;
6575 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
6576 host_impl_->DrawLayers(&frame);
6577 host_impl_->DidDrawAllLayers(frame);
6579 // The CopyOutputResult's callback has a ref on the ContextProvider and a
6580 // texture in a texture mailbox.
6581 EXPECT_FALSE(context_provider->HasOneRef());
6582 EXPECT_EQ(1u, context_provider->TestContext3d()->NumTextures());
6584 host_impl_ = nullptr;
6586 // The CopyOutputResult's callback was cancelled, the CopyOutputResult
6587 // released, and the texture deleted.
6588 EXPECT_TRUE(context_provider->HasOneRef());
6589 EXPECT_EQ(0u, context_provider->TestContext3d()->NumTextures());
6592 TEST_F(LayerTreeHostImplTest, TouchFlingShouldNotBubble) {
6593 // When flinging via touch, only the child should scroll (we should not
6594 // bubble).
6595 gfx::Size surface_size(10, 10);
6596 gfx::Size content_size(20, 20);
6597 scoped_ptr<LayerImpl> root_clip =
6598 LayerImpl::Create(host_impl_->active_tree(), 3);
6599 root_clip->SetHasRenderSurface(true);
6601 scoped_ptr<LayerImpl> root =
6602 CreateScrollableLayer(1, content_size, root_clip.get());
6603 root->SetIsContainerForFixedPositionLayers(true);
6604 scoped_ptr<LayerImpl> child =
6605 CreateScrollableLayer(2, content_size, root_clip.get());
6607 root->AddChild(child.Pass());
6608 int root_id = root->id();
6609 root_clip->AddChild(root.Pass());
6611 host_impl_->SetViewportSize(surface_size);
6612 host_impl_->active_tree()->SetRootLayer(root_clip.Pass());
6613 host_impl_->active_tree()->SetViewportLayersFromIds(Layer::INVALID_ID, 3, 1,
6614 Layer::INVALID_ID);
6615 host_impl_->active_tree()->DidBecomeActive();
6616 DrawFrame();
6618 EXPECT_EQ(InputHandler::SCROLL_STARTED,
6619 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
6621 EXPECT_EQ(InputHandler::SCROLL_STARTED, host_impl_->FlingScrollBegin());
6623 gfx::Vector2d scroll_delta(0, 100);
6624 host_impl_->ScrollBy(gfx::Point(), scroll_delta);
6625 host_impl_->ScrollBy(gfx::Point(), scroll_delta);
6627 host_impl_->ScrollEnd();
6629 scoped_ptr<ScrollAndScaleSet> scroll_info =
6630 host_impl_->ProcessScrollDeltas();
6632 // Only the child should have scrolled.
6633 ASSERT_EQ(1u, scroll_info->scrolls.size());
6634 ExpectNone(*scroll_info.get(), root_id);
6638 TEST_F(LayerTreeHostImplTest, TouchFlingShouldLockToFirstScrolledLayer) {
6639 // Scroll a child layer beyond its maximum scroll range and make sure the
6640 // the scroll doesn't bubble up to the parent layer.
6641 gfx::Size surface_size(10, 10);
6642 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl_->active_tree(), 1);
6643 root->SetHasRenderSurface(true);
6644 scoped_ptr<LayerImpl> root_scrolling =
6645 CreateScrollableLayer(2, surface_size, root.get());
6647 scoped_ptr<LayerImpl> grand_child =
6648 CreateScrollableLayer(4, surface_size, root.get());
6649 grand_child->PushScrollOffsetFromMainThread(gfx::ScrollOffset(0, 2));
6651 scoped_ptr<LayerImpl> child =
6652 CreateScrollableLayer(3, surface_size, root.get());
6653 child->PushScrollOffsetFromMainThread(gfx::ScrollOffset(0, 4));
6654 child->AddChild(grand_child.Pass());
6656 root_scrolling->AddChild(child.Pass());
6657 root->AddChild(root_scrolling.Pass());
6658 host_impl_->active_tree()->SetRootLayer(root.Pass());
6659 host_impl_->active_tree()->DidBecomeActive();
6660 host_impl_->SetViewportSize(surface_size);
6661 DrawFrame();
6663 scoped_ptr<ScrollAndScaleSet> scroll_info;
6664 LayerImpl* child =
6665 host_impl_->active_tree()->root_layer()->children()[0]->children()[0];
6666 LayerImpl* grand_child = child->children()[0];
6668 gfx::Vector2d scroll_delta(0, -2);
6669 EXPECT_EQ(InputHandler::SCROLL_STARTED,
6670 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
6671 EXPECT_TRUE(host_impl_->ScrollBy(gfx::Point(), scroll_delta).did_scroll);
6673 // The grand child should have scrolled up to its limit.
6674 scroll_info = host_impl_->ProcessScrollDeltas();
6675 ASSERT_EQ(1u, scroll_info->scrolls.size());
6676 EXPECT_TRUE(
6677 ScrollInfoContains(*scroll_info, grand_child->id(), scroll_delta));
6678 EXPECT_EQ(host_impl_->CurrentlyScrollingLayer(), grand_child);
6680 // The child should have received the bubbled delta, but the locked
6681 // scrolling layer should remain set as the grand child.
6682 EXPECT_TRUE(host_impl_->ScrollBy(gfx::Point(), scroll_delta).did_scroll);
6683 scroll_info = host_impl_->ProcessScrollDeltas();
6684 ASSERT_EQ(2u, scroll_info->scrolls.size());
6685 EXPECT_TRUE(
6686 ScrollInfoContains(*scroll_info, grand_child->id(), scroll_delta));
6687 EXPECT_TRUE(ScrollInfoContains(*scroll_info, child->id(), scroll_delta));
6688 EXPECT_EQ(host_impl_->CurrentlyScrollingLayer(), grand_child);
6690 // The first |ScrollBy| after the fling should re-lock the scrolling
6691 // layer to the first layer that scrolled, which is the child.
6692 EXPECT_EQ(InputHandler::SCROLL_STARTED, host_impl_->FlingScrollBegin());
6693 EXPECT_TRUE(host_impl_->ScrollBy(gfx::Point(), scroll_delta).did_scroll);
6694 EXPECT_EQ(host_impl_->CurrentlyScrollingLayer(), child);
6696 // The child should have scrolled up to its limit.
6697 scroll_info = host_impl_->ProcessScrollDeltas();
6698 ASSERT_EQ(2u, scroll_info->scrolls.size());
6699 EXPECT_TRUE(
6700 ScrollInfoContains(*scroll_info, grand_child->id(), scroll_delta));
6701 EXPECT_TRUE(ScrollInfoContains(*scroll_info, child->id(),
6702 scroll_delta + scroll_delta));
6704 // As the locked layer is at it's limit, no further scrolling can occur.
6705 EXPECT_FALSE(host_impl_->ScrollBy(gfx::Point(), scroll_delta).did_scroll);
6706 EXPECT_EQ(host_impl_->CurrentlyScrollingLayer(), child);
6707 host_impl_->ScrollEnd();
6711 TEST_F(LayerTreeHostImplTest, WheelFlingShouldBubble) {
6712 // When flinging via wheel, the root should eventually scroll (we should
6713 // bubble).
6714 gfx::Size surface_size(10, 10);
6715 gfx::Size content_size(20, 20);
6716 scoped_ptr<LayerImpl> root_clip =
6717 LayerImpl::Create(host_impl_->active_tree(), 3);
6718 root_clip->SetHasRenderSurface(true);
6719 scoped_ptr<LayerImpl> root_scroll =
6720 CreateScrollableLayer(1, content_size, root_clip.get());
6721 int root_scroll_id = root_scroll->id();
6722 scoped_ptr<LayerImpl> child =
6723 CreateScrollableLayer(2, content_size, root_clip.get());
6725 root_scroll->AddChild(child.Pass());
6726 root_clip->AddChild(root_scroll.Pass());
6728 host_impl_->SetViewportSize(surface_size);
6729 host_impl_->active_tree()->SetRootLayer(root_clip.Pass());
6730 host_impl_->active_tree()->DidBecomeActive();
6731 DrawFrame();
6733 EXPECT_EQ(InputHandler::SCROLL_STARTED,
6734 host_impl_->ScrollBegin(gfx::Point(), InputHandler::WHEEL));
6736 EXPECT_EQ(InputHandler::SCROLL_STARTED, host_impl_->FlingScrollBegin());
6738 gfx::Vector2d scroll_delta(0, 100);
6739 host_impl_->ScrollBy(gfx::Point(), scroll_delta);
6740 host_impl_->ScrollBy(gfx::Point(), scroll_delta);
6742 host_impl_->ScrollEnd();
6744 scoped_ptr<ScrollAndScaleSet> scroll_info =
6745 host_impl_->ProcessScrollDeltas();
6747 // The root should have scrolled.
6748 ASSERT_EQ(2u, scroll_info->scrolls.size());
6749 EXPECT_TRUE(ScrollInfoContains(*scroll_info.get(), root_scroll_id,
6750 gfx::Vector2d(0, 10)));
6754 TEST_F(LayerTreeHostImplTest, ScrollUnknownNotOnAncestorChain) {
6755 // If we ray cast a scroller that is not on the first layer's ancestor chain,
6756 // we should return SCROLL_UNKNOWN.
6757 gfx::Size content_size(100, 100);
6758 SetupScrollAndContentsLayers(content_size);
6760 int scroll_layer_id = 2;
6761 LayerImpl* scroll_layer =
6762 host_impl_->active_tree()->LayerById(scroll_layer_id);
6763 scroll_layer->SetDrawsContent(true);
6765 int page_scale_layer_id = 5;
6766 LayerImpl* page_scale_layer =
6767 host_impl_->active_tree()->LayerById(page_scale_layer_id);
6769 int occluder_layer_id = 6;
6770 scoped_ptr<LayerImpl> occluder_layer =
6771 LayerImpl::Create(host_impl_->active_tree(), occluder_layer_id);
6772 occluder_layer->SetDrawsContent(true);
6773 occluder_layer->SetBounds(content_size);
6774 occluder_layer->SetPosition(gfx::PointF());
6776 // The parent of the occluder is *above* the scroller.
6777 page_scale_layer->AddChild(occluder_layer.Pass());
6779 DrawFrame();
6781 EXPECT_EQ(InputHandler::SCROLL_UNKNOWN,
6782 host_impl_->ScrollBegin(gfx::Point(), InputHandler::WHEEL));
6785 TEST_F(LayerTreeHostImplTest, ScrollUnknownScrollAncestorMismatch) {
6786 // If we ray cast a scroller this is on the first layer's ancestor chain, but
6787 // is not the first scroller we encounter when walking up from the layer, we
6788 // should also return SCROLL_UNKNOWN.
6789 gfx::Size content_size(100, 100);
6790 SetupScrollAndContentsLayers(content_size);
6792 int scroll_layer_id = 2;
6793 LayerImpl* scroll_layer =
6794 host_impl_->active_tree()->LayerById(scroll_layer_id);
6795 scroll_layer->SetDrawsContent(true);
6797 int occluder_layer_id = 6;
6798 scoped_ptr<LayerImpl> occluder_layer =
6799 LayerImpl::Create(host_impl_->active_tree(), occluder_layer_id);
6800 occluder_layer->SetDrawsContent(true);
6801 occluder_layer->SetBounds(content_size);
6802 occluder_layer->SetPosition(gfx::PointF(-10.f, -10.f));
6804 int child_scroll_clip_layer_id = 7;
6805 scoped_ptr<LayerImpl> child_scroll_clip =
6806 LayerImpl::Create(host_impl_->active_tree(), child_scroll_clip_layer_id);
6808 int child_scroll_layer_id = 8;
6809 scoped_ptr<LayerImpl> child_scroll = CreateScrollableLayer(
6810 child_scroll_layer_id, content_size, child_scroll_clip.get());
6812 child_scroll->SetPosition(gfx::PointF(10.f, 10.f));
6814 child_scroll->AddChild(occluder_layer.Pass());
6815 scroll_layer->AddChild(child_scroll.Pass());
6817 DrawFrame();
6819 EXPECT_EQ(InputHandler::SCROLL_UNKNOWN,
6820 host_impl_->ScrollBegin(gfx::Point(), InputHandler::WHEEL));
6823 TEST_F(LayerTreeHostImplTest, NotScrollInvisibleScroller) {
6824 gfx::Size content_size(100, 100);
6825 SetupScrollAndContentsLayers(content_size);
6827 LayerImpl* root = host_impl_->active_tree()->LayerById(1);
6829 int scroll_layer_id = 2;
6830 LayerImpl* scroll_layer =
6831 host_impl_->active_tree()->LayerById(scroll_layer_id);
6833 int child_scroll_layer_id = 7;
6834 scoped_ptr<LayerImpl> child_scroll =
6835 CreateScrollableLayer(child_scroll_layer_id, content_size, root);
6836 child_scroll->SetDrawsContent(false);
6838 scroll_layer->AddChild(child_scroll.Pass());
6840 DrawFrame();
6842 // We should not have scrolled |child_scroll| even though we technically "hit"
6843 // it. The reason for this is that if the scrolling the scroll would not move
6844 // any layer that is a drawn RSLL member, then we can ignore the hit.
6846 // Why SCROLL_STARTED? In this case, it's because we've bubbled out and
6847 // started scrolling the inner viewport.
6848 EXPECT_EQ(InputHandler::SCROLL_STARTED,
6849 host_impl_->ScrollBegin(gfx::Point(), InputHandler::WHEEL));
6851 EXPECT_EQ(2, host_impl_->CurrentlyScrollingLayer()->id());
6854 TEST_F(LayerTreeHostImplTest, ScrollInvisibleScrollerWithVisibleDescendent) {
6855 gfx::Size content_size(100, 100);
6856 SetupScrollAndContentsLayers(content_size);
6858 LayerImpl* root = host_impl_->active_tree()->LayerById(1);
6859 LayerImpl* root_scroll_layer = host_impl_->active_tree()->LayerById(2);
6861 scoped_ptr<LayerImpl> invisible_scroll_layer =
6862 CreateScrollableLayer(7, content_size, root);
6863 invisible_scroll_layer->SetDrawsContent(false);
6865 scoped_ptr<LayerImpl> child_layer =
6866 LayerImpl::Create(host_impl_->active_tree(), 8);
6867 child_layer->SetDrawsContent(false);
6869 scoped_ptr<LayerImpl> grand_child_layer =
6870 LayerImpl::Create(host_impl_->active_tree(), 9);
6871 grand_child_layer->SetDrawsContent(true);
6872 grand_child_layer->SetBounds(content_size);
6873 // Move the grand child so it's not hit by our test point.
6874 grand_child_layer->SetPosition(gfx::PointF(10.f, 10.f));
6876 child_layer->AddChild(grand_child_layer.Pass());
6877 invisible_scroll_layer->AddChild(child_layer.Pass());
6878 root_scroll_layer->AddChild(invisible_scroll_layer.Pass());
6880 DrawFrame();
6882 // We should have scrolled |invisible_scroll_layer| as it was hit and it has
6883 // a descendant which is a drawn RSLL member.
6884 EXPECT_EQ(InputHandler::SCROLL_STARTED,
6885 host_impl_->ScrollBegin(gfx::Point(), InputHandler::WHEEL));
6887 EXPECT_EQ(7, host_impl_->CurrentlyScrollingLayer()->id());
6890 TEST_F(LayerTreeHostImplTest, ScrollInvisibleScrollerWithVisibleScrollChild) {
6891 // This test case is very similar to the one above with one key difference:
6892 // the invisible scroller has a scroll child that is indeed draw contents.
6893 // If we attempt to initiate a gesture scroll off of the visible scroll child
6894 // we should still start the scroll child.
6895 gfx::Size content_size(100, 100);
6896 SetupScrollAndContentsLayers(content_size);
6898 LayerImpl* root = host_impl_->active_tree()->LayerById(1);
6900 int scroll_layer_id = 2;
6901 LayerImpl* scroll_layer =
6902 host_impl_->active_tree()->LayerById(scroll_layer_id);
6904 int scroll_child_id = 6;
6905 scoped_ptr<LayerImpl> scroll_child =
6906 LayerImpl::Create(host_impl_->active_tree(), scroll_child_id);
6907 scroll_child->SetDrawsContent(true);
6908 scroll_child->SetBounds(content_size);
6909 // Move the scroll child so it's not hit by our test point.
6910 scroll_child->SetPosition(gfx::PointF(10.f, 10.f));
6912 int invisible_scroll_layer_id = 7;
6913 scoped_ptr<LayerImpl> invisible_scroll =
6914 CreateScrollableLayer(invisible_scroll_layer_id, content_size, root);
6915 invisible_scroll->SetDrawsContent(false);
6917 int container_id = 8;
6918 scoped_ptr<LayerImpl> container =
6919 LayerImpl::Create(host_impl_->active_tree(), container_id);
6921 scoped_ptr<std::set<LayerImpl*>> scroll_children(new std::set<LayerImpl*>);
6922 scroll_children->insert(scroll_child.get());
6923 invisible_scroll->SetScrollChildren(scroll_children.release());
6925 scroll_child->SetScrollParent(invisible_scroll.get());
6927 container->AddChild(invisible_scroll.Pass());
6928 container->AddChild(scroll_child.Pass());
6930 scroll_layer->AddChild(container.Pass());
6932 DrawFrame();
6934 // We should have scrolled |child_scroll| even though it is invisible.
6935 // The reason for this is that if the scrolling the scroll would move a layer
6936 // that is a drawn RSLL member, then we should accept this hit.
6937 EXPECT_EQ(InputHandler::SCROLL_STARTED,
6938 host_impl_->ScrollBegin(gfx::Point(), InputHandler::WHEEL));
6940 EXPECT_EQ(7, host_impl_->CurrentlyScrollingLayer()->id());
6943 // Make sure LatencyInfo carried by LatencyInfoSwapPromise are passed
6944 // to CompositorFrameMetadata after SwapBuffers();
6945 TEST_F(LayerTreeHostImplTest, LatencyInfoPassedToCompositorFrameMetadata) {
6946 scoped_ptr<SolidColorLayerImpl> root =
6947 SolidColorLayerImpl::Create(host_impl_->active_tree(), 1);
6948 root->SetPosition(gfx::PointF());
6949 root->SetBounds(gfx::Size(10, 10));
6950 root->SetDrawsContent(true);
6951 root->SetHasRenderSurface(true);
6953 host_impl_->active_tree()->SetRootLayer(root.Pass());
6955 FakeOutputSurface* fake_output_surface =
6956 static_cast<FakeOutputSurface*>(host_impl_->output_surface());
6958 const std::vector<ui::LatencyInfo>& metadata_latency_before =
6959 fake_output_surface->last_sent_frame().metadata.latency_info;
6960 EXPECT_TRUE(metadata_latency_before.empty());
6962 ui::LatencyInfo latency_info;
6963 latency_info.AddLatencyNumber(
6964 ui::INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT, 0, 0);
6965 scoped_ptr<SwapPromise> swap_promise(
6966 new LatencyInfoSwapPromise(latency_info));
6967 host_impl_->active_tree()->QueuePinnedSwapPromise(swap_promise.Pass());
6968 host_impl_->SetNeedsRedraw();
6970 gfx::Rect full_frame_damage(host_impl_->DrawViewportSize());
6971 LayerTreeHostImpl::FrameData frame;
6972 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
6973 host_impl_->DrawLayers(&frame);
6974 host_impl_->DidDrawAllLayers(frame);
6975 EXPECT_TRUE(host_impl_->SwapBuffers(frame));
6977 const std::vector<ui::LatencyInfo>& metadata_latency_after =
6978 fake_output_surface->last_sent_frame().metadata.latency_info;
6979 EXPECT_EQ(1u, metadata_latency_after.size());
6980 EXPECT_TRUE(metadata_latency_after[0].FindLatency(
6981 ui::INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT, 0, NULL));
6984 TEST_F(LayerTreeHostImplTest, SelectionBoundsPassedToCompositorFrameMetadata) {
6985 int root_layer_id = 1;
6986 scoped_ptr<SolidColorLayerImpl> root =
6987 SolidColorLayerImpl::Create(host_impl_->active_tree(), root_layer_id);
6988 root->SetPosition(gfx::PointF());
6989 root->SetBounds(gfx::Size(10, 10));
6990 root->SetDrawsContent(true);
6991 root->SetHasRenderSurface(true);
6993 host_impl_->active_tree()->SetRootLayer(root.Pass());
6995 // Ensure the default frame selection bounds are empty.
6996 FakeOutputSurface* fake_output_surface =
6997 static_cast<FakeOutputSurface*>(host_impl_->output_surface());
6998 const ViewportSelection& selection_before =
6999 fake_output_surface->last_sent_frame().metadata.selection;
7000 EXPECT_EQ(ViewportSelectionBound(), selection_before.start);
7001 EXPECT_EQ(ViewportSelectionBound(), selection_before.end);
7003 // Plumb the layer-local selection bounds.
7004 gfx::PointF selection_top(5, 0);
7005 gfx::PointF selection_bottom(5, 5);
7006 LayerSelection selection;
7007 selection.start.type = SELECTION_BOUND_CENTER;
7008 selection.start.layer_id = root_layer_id;
7009 selection.start.edge_bottom = selection_bottom;
7010 selection.start.edge_top = selection_top;
7011 selection.end = selection.start;
7012 host_impl_->active_tree()->RegisterSelection(selection);
7014 // Trigger a draw-swap sequence.
7015 host_impl_->SetNeedsRedraw();
7017 gfx::Rect full_frame_damage(host_impl_->DrawViewportSize());
7018 LayerTreeHostImpl::FrameData frame;
7019 EXPECT_EQ(DRAW_SUCCESS, host_impl_->PrepareToDraw(&frame));
7020 host_impl_->DrawLayers(&frame);
7021 host_impl_->DidDrawAllLayers(frame);
7022 EXPECT_TRUE(host_impl_->SwapBuffers(frame));
7024 // Ensure the selection bounds have propagated to the frame metadata.
7025 const ViewportSelection& selection_after =
7026 fake_output_surface->last_sent_frame().metadata.selection;
7027 EXPECT_EQ(selection.start.type, selection_after.start.type);
7028 EXPECT_EQ(selection.end.type, selection_after.end.type);
7029 EXPECT_EQ(selection_bottom, selection_after.start.edge_bottom);
7030 EXPECT_EQ(selection_top, selection_after.start.edge_top);
7031 EXPECT_TRUE(selection_after.start.visible);
7032 EXPECT_TRUE(selection_after.start.visible);
7035 class SimpleSwapPromiseMonitor : public SwapPromiseMonitor {
7036 public:
7037 SimpleSwapPromiseMonitor(LayerTreeHost* layer_tree_host,
7038 LayerTreeHostImpl* layer_tree_host_impl,
7039 int* set_needs_commit_count,
7040 int* set_needs_redraw_count,
7041 int* forward_to_main_count)
7042 : SwapPromiseMonitor(layer_tree_host, layer_tree_host_impl),
7043 set_needs_commit_count_(set_needs_commit_count),
7044 set_needs_redraw_count_(set_needs_redraw_count),
7045 forward_to_main_count_(forward_to_main_count) {}
7047 ~SimpleSwapPromiseMonitor() override {}
7049 void OnSetNeedsCommitOnMain() override { (*set_needs_commit_count_)++; }
7051 void OnSetNeedsRedrawOnImpl() override { (*set_needs_redraw_count_)++; }
7053 void OnForwardScrollUpdateToMainThreadOnImpl() override {
7054 (*forward_to_main_count_)++;
7057 private:
7058 int* set_needs_commit_count_;
7059 int* set_needs_redraw_count_;
7060 int* forward_to_main_count_;
7063 TEST_F(LayerTreeHostImplTest, SimpleSwapPromiseMonitor) {
7064 int set_needs_commit_count = 0;
7065 int set_needs_redraw_count = 0;
7066 int forward_to_main_count = 0;
7069 scoped_ptr<SimpleSwapPromiseMonitor> swap_promise_monitor(
7070 new SimpleSwapPromiseMonitor(NULL,
7071 host_impl_.get(),
7072 &set_needs_commit_count,
7073 &set_needs_redraw_count,
7074 &forward_to_main_count));
7075 host_impl_->SetNeedsRedraw();
7076 EXPECT_EQ(0, set_needs_commit_count);
7077 EXPECT_EQ(1, set_needs_redraw_count);
7078 EXPECT_EQ(0, forward_to_main_count);
7081 // Now the monitor is destroyed, SetNeedsRedraw() is no longer being
7082 // monitored.
7083 host_impl_->SetNeedsRedraw();
7084 EXPECT_EQ(0, set_needs_commit_count);
7085 EXPECT_EQ(1, set_needs_redraw_count);
7086 EXPECT_EQ(0, forward_to_main_count);
7089 scoped_ptr<SimpleSwapPromiseMonitor> swap_promise_monitor(
7090 new SimpleSwapPromiseMonitor(NULL,
7091 host_impl_.get(),
7092 &set_needs_commit_count,
7093 &set_needs_redraw_count,
7094 &forward_to_main_count));
7095 host_impl_->SetNeedsRedrawRect(gfx::Rect(10, 10));
7096 EXPECT_EQ(0, set_needs_commit_count);
7097 EXPECT_EQ(2, set_needs_redraw_count);
7098 EXPECT_EQ(0, forward_to_main_count);
7102 scoped_ptr<SimpleSwapPromiseMonitor> swap_promise_monitor(
7103 new SimpleSwapPromiseMonitor(NULL,
7104 host_impl_.get(),
7105 &set_needs_commit_count,
7106 &set_needs_redraw_count,
7107 &forward_to_main_count));
7108 // Empty damage rect won't signal the monitor.
7109 host_impl_->SetNeedsRedrawRect(gfx::Rect());
7110 EXPECT_EQ(0, set_needs_commit_count);
7111 EXPECT_EQ(2, set_needs_redraw_count);
7112 EXPECT_EQ(0, forward_to_main_count);
7116 set_needs_commit_count = 0;
7117 set_needs_redraw_count = 0;
7118 forward_to_main_count = 0;
7119 scoped_ptr<SimpleSwapPromiseMonitor> swap_promise_monitor(
7120 new SimpleSwapPromiseMonitor(NULL,
7121 host_impl_.get(),
7122 &set_needs_commit_count,
7123 &set_needs_redraw_count,
7124 &forward_to_main_count));
7125 LayerImpl* scroll_layer = SetupScrollAndContentsLayers(gfx::Size(100, 100));
7127 // Scrolling normally should not trigger any forwarding.
7128 EXPECT_EQ(InputHandler::SCROLL_STARTED,
7129 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
7130 EXPECT_TRUE(
7131 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(0, 10)).did_scroll);
7132 host_impl_->ScrollEnd();
7134 EXPECT_EQ(0, set_needs_commit_count);
7135 EXPECT_EQ(1, set_needs_redraw_count);
7136 EXPECT_EQ(0, forward_to_main_count);
7138 // Scrolling with a scroll handler should defer the swap to the main
7139 // thread.
7140 scroll_layer->SetHaveScrollEventHandlers(true);
7141 EXPECT_EQ(InputHandler::SCROLL_STARTED,
7142 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
7143 EXPECT_TRUE(
7144 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(0, 10)).did_scroll);
7145 host_impl_->ScrollEnd();
7147 EXPECT_EQ(0, set_needs_commit_count);
7148 EXPECT_EQ(2, set_needs_redraw_count);
7149 EXPECT_EQ(1, forward_to_main_count);
7153 class LayerTreeHostImplWithTopControlsTest : public LayerTreeHostImplTest {
7154 public:
7155 void SetUp() override {
7156 LayerTreeSettings settings = DefaultSettings();
7157 CreateHostImpl(settings, CreateOutputSurface());
7158 host_impl_->active_tree()->set_top_controls_height(top_controls_height_);
7159 host_impl_->sync_tree()->set_top_controls_height(top_controls_height_);
7160 host_impl_->active_tree()->SetCurrentTopControlsShownRatio(1.f);
7163 protected:
7164 static const int top_controls_height_;
7167 const int LayerTreeHostImplWithTopControlsTest::top_controls_height_ = 50;
7169 TEST_F(LayerTreeHostImplWithTopControlsTest, NoIdleAnimations) {
7170 SetupScrollAndContentsLayers(gfx::Size(100, 100))
7171 ->PushScrollOffsetFromMainThread(gfx::ScrollOffset(0, 10));
7172 BeginFrameArgs begin_frame_args =
7173 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE);
7174 host_impl_->WillBeginImplFrame(begin_frame_args);
7175 host_impl_->Animate();
7176 EXPECT_FALSE(did_request_redraw_);
7177 host_impl_->DidFinishImplFrame();
7180 TEST_F(LayerTreeHostImplWithTopControlsTest, TopControlsHeightIsCommitted) {
7181 SetupScrollAndContentsLayers(gfx::Size(100, 100));
7182 EXPECT_FALSE(did_request_redraw_);
7183 host_impl_->CreatePendingTree();
7184 host_impl_->sync_tree()->set_top_controls_height(100);
7185 host_impl_->ActivateSyncTree();
7186 EXPECT_EQ(100, host_impl_->top_controls_manager()->TopControlsHeight());
7189 TEST_F(LayerTreeHostImplWithTopControlsTest,
7190 TopControlsStayFullyVisibleOnHeightChange) {
7191 SetupScrollAndContentsLayers(gfx::Size(100, 100));
7192 EXPECT_EQ(0.f, host_impl_->top_controls_manager()->ControlsTopOffset());
7194 host_impl_->CreatePendingTree();
7195 host_impl_->sync_tree()->set_top_controls_height(0);
7196 host_impl_->ActivateSyncTree();
7197 EXPECT_EQ(0.f, host_impl_->top_controls_manager()->ControlsTopOffset());
7199 host_impl_->CreatePendingTree();
7200 host_impl_->sync_tree()->set_top_controls_height(50);
7201 host_impl_->ActivateSyncTree();
7202 EXPECT_EQ(0.f, host_impl_->top_controls_manager()->ControlsTopOffset());
7205 TEST_F(LayerTreeHostImplWithTopControlsTest, TopControlsAnimationScheduling) {
7206 SetupScrollAndContentsLayers(gfx::Size(100, 100))
7207 ->PushScrollOffsetFromMainThread(gfx::ScrollOffset(0, 10));
7208 host_impl_->DidChangeTopControlsPosition();
7209 EXPECT_TRUE(did_request_animate_);
7210 EXPECT_TRUE(did_request_redraw_);
7213 TEST_F(LayerTreeHostImplWithTopControlsTest, ScrollHandledByTopControls) {
7214 InputHandlerScrollResult result;
7215 LayerImpl* scroll_layer = SetupScrollAndContentsLayers(gfx::Size(100, 200));
7216 host_impl_->SetViewportSize(gfx::Size(100, 100));
7217 host_impl_->top_controls_manager()->UpdateTopControlsState(
7218 BOTH, SHOWN, false);
7219 DrawFrame();
7221 EXPECT_EQ(InputHandler::SCROLL_STARTED,
7222 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
7223 EXPECT_EQ(0, host_impl_->top_controls_manager()->ControlsTopOffset());
7224 EXPECT_EQ(gfx::Vector2dF().ToString(),
7225 scroll_layer->CurrentScrollOffset().ToString());
7227 // Scroll just the top controls and verify that the scroll succeeds.
7228 const float residue = 10;
7229 float offset = top_controls_height_ - residue;
7230 result = host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(0, offset));
7231 EXPECT_EQ(result.unused_scroll_delta, gfx::Vector2d(0, 0));
7232 EXPECT_TRUE(result.did_scroll);
7233 EXPECT_FLOAT_EQ(-offset,
7234 host_impl_->top_controls_manager()->ControlsTopOffset());
7235 EXPECT_EQ(gfx::Vector2dF().ToString(),
7236 scroll_layer->CurrentScrollOffset().ToString());
7238 // Scroll across the boundary
7239 const float content_scroll = 20;
7240 offset = residue + content_scroll;
7241 result = host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(0, offset));
7242 EXPECT_TRUE(result.did_scroll);
7243 EXPECT_EQ(result.unused_scroll_delta, gfx::Vector2d(0, 0));
7244 EXPECT_EQ(-top_controls_height_,
7245 host_impl_->top_controls_manager()->ControlsTopOffset());
7246 EXPECT_EQ(gfx::Vector2dF(0, content_scroll).ToString(),
7247 scroll_layer->CurrentScrollOffset().ToString());
7249 // Now scroll back to the top of the content
7250 offset = -content_scroll;
7251 result = host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(0, offset));
7252 EXPECT_TRUE(result.did_scroll);
7253 EXPECT_EQ(result.unused_scroll_delta, gfx::Vector2d(0, 0));
7254 EXPECT_EQ(-top_controls_height_,
7255 host_impl_->top_controls_manager()->ControlsTopOffset());
7256 EXPECT_EQ(gfx::Vector2dF().ToString(),
7257 scroll_layer->CurrentScrollOffset().ToString());
7259 // And scroll the top controls completely into view
7260 offset = -top_controls_height_;
7261 result = host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(0, offset));
7262 EXPECT_TRUE(result.did_scroll);
7263 EXPECT_EQ(result.unused_scroll_delta, gfx::Vector2d(0, 0));
7264 EXPECT_EQ(0, host_impl_->top_controls_manager()->ControlsTopOffset());
7265 EXPECT_EQ(gfx::Vector2dF().ToString(),
7266 scroll_layer->CurrentScrollOffset().ToString());
7268 // And attempt to scroll past the end
7269 result = host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(0, offset));
7270 EXPECT_FALSE(result.did_scroll);
7271 EXPECT_EQ(result.unused_scroll_delta, gfx::Vector2d(0, -50));
7272 EXPECT_EQ(0, host_impl_->top_controls_manager()->ControlsTopOffset());
7273 EXPECT_EQ(gfx::Vector2dF().ToString(),
7274 scroll_layer->CurrentScrollOffset().ToString());
7276 host_impl_->ScrollEnd();
7279 TEST_F(LayerTreeHostImplWithTopControlsTest, WheelUnhandledByTopControls) {
7280 SetupScrollAndContentsLayers(gfx::Size(100, 200));
7281 host_impl_->SetViewportSize(gfx::Size(50, 100));
7282 host_impl_->active_tree()->set_top_controls_shrink_blink_size(true);
7283 host_impl_->top_controls_manager()->UpdateTopControlsState(BOTH, SHOWN,
7284 false);
7285 DrawFrame();
7287 LayerImpl* viewport_layer = host_impl_->InnerViewportScrollLayer();
7289 EXPECT_EQ(InputHandler::SCROLL_STARTED,
7290 host_impl_->ScrollBegin(gfx::Point(), InputHandler::WHEEL));
7291 EXPECT_EQ(0, host_impl_->top_controls_manager()->ControlsTopOffset());
7292 EXPECT_VECTOR_EQ(gfx::Vector2dF(), viewport_layer->CurrentScrollOffset());
7294 // Wheel scrolls should not affect the top controls, and should pass
7295 // directly through to the viewport.
7296 const float delta = top_controls_height_;
7297 EXPECT_TRUE(
7298 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(0, delta)).did_scroll);
7299 EXPECT_FLOAT_EQ(0, host_impl_->top_controls_manager()->ControlsTopOffset());
7300 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, delta),
7301 viewport_layer->CurrentScrollOffset());
7303 EXPECT_TRUE(
7304 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(0, delta)).did_scroll);
7305 EXPECT_FLOAT_EQ(0, host_impl_->top_controls_manager()->ControlsTopOffset());
7306 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, delta * 2),
7307 viewport_layer->CurrentScrollOffset());
7310 TEST_F(LayerTreeHostImplWithTopControlsTest, TopControlsAnimationAtOrigin) {
7311 LayerImpl* scroll_layer = SetupScrollAndContentsLayers(gfx::Size(100, 200));
7312 host_impl_->SetViewportSize(gfx::Size(100, 200));
7313 host_impl_->top_controls_manager()->UpdateTopControlsState(
7314 BOTH, SHOWN, false);
7315 DrawFrame();
7317 EXPECT_EQ(InputHandler::SCROLL_STARTED,
7318 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
7319 EXPECT_EQ(0, host_impl_->top_controls_manager()->ControlsTopOffset());
7320 EXPECT_EQ(gfx::Vector2dF().ToString(),
7321 scroll_layer->CurrentScrollOffset().ToString());
7323 // Scroll the top controls partially.
7324 const float residue = 35;
7325 float offset = top_controls_height_ - residue;
7326 EXPECT_TRUE(
7327 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(0, offset)).did_scroll);
7328 EXPECT_FLOAT_EQ(-offset,
7329 host_impl_->top_controls_manager()->ControlsTopOffset());
7330 EXPECT_EQ(gfx::Vector2dF().ToString(),
7331 scroll_layer->CurrentScrollOffset().ToString());
7333 did_request_redraw_ = false;
7334 did_request_animate_ = false;
7335 did_request_commit_ = false;
7337 // End the scroll while the controls are still offset from their limit.
7338 host_impl_->ScrollEnd();
7339 ASSERT_TRUE(host_impl_->top_controls_manager()->animation());
7340 EXPECT_TRUE(did_request_animate_);
7341 EXPECT_TRUE(did_request_redraw_);
7342 EXPECT_FALSE(did_request_commit_);
7344 // The top controls should properly animate until finished, despite the scroll
7345 // offset being at the origin.
7346 BeginFrameArgs begin_frame_args = CreateBeginFrameArgsForTesting(
7347 BEGINFRAME_FROM_HERE, base::TimeTicks::Now());
7348 while (did_request_animate_) {
7349 did_request_redraw_ = false;
7350 did_request_animate_ = false;
7351 did_request_commit_ = false;
7353 float old_offset =
7354 host_impl_->top_controls_manager()->ControlsTopOffset();
7356 begin_frame_args.frame_time += base::TimeDelta::FromMilliseconds(5);
7357 host_impl_->WillBeginImplFrame(begin_frame_args);
7358 host_impl_->Animate();
7359 EXPECT_EQ(gfx::Vector2dF().ToString(),
7360 scroll_layer->CurrentScrollOffset().ToString());
7362 float new_offset =
7363 host_impl_->top_controls_manager()->ControlsTopOffset();
7365 // No commit is needed as the controls are animating the content offset,
7366 // not the scroll offset.
7367 EXPECT_FALSE(did_request_commit_);
7369 if (new_offset != old_offset)
7370 EXPECT_TRUE(did_request_redraw_);
7372 if (new_offset != 0) {
7373 EXPECT_TRUE(host_impl_->top_controls_manager()->animation());
7374 EXPECT_TRUE(did_request_animate_);
7376 host_impl_->DidFinishImplFrame();
7378 EXPECT_FALSE(host_impl_->top_controls_manager()->animation());
7381 TEST_F(LayerTreeHostImplWithTopControlsTest, TopControlsAnimationAfterScroll) {
7382 LayerImpl* scroll_layer = SetupScrollAndContentsLayers(gfx::Size(100, 200));
7383 host_impl_->SetViewportSize(gfx::Size(100, 100));
7384 host_impl_->top_controls_manager()->UpdateTopControlsState(
7385 BOTH, SHOWN, false);
7386 float initial_scroll_offset = 50;
7387 scroll_layer->PushScrollOffsetFromMainThread(
7388 gfx::ScrollOffset(0, initial_scroll_offset));
7389 DrawFrame();
7391 EXPECT_EQ(InputHandler::SCROLL_STARTED,
7392 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
7393 EXPECT_EQ(0, host_impl_->top_controls_manager()->ControlsTopOffset());
7394 EXPECT_EQ(gfx::Vector2dF(0, initial_scroll_offset).ToString(),
7395 scroll_layer->CurrentScrollOffset().ToString());
7397 // Scroll the top controls partially.
7398 const float residue = 15;
7399 float offset = top_controls_height_ - residue;
7400 EXPECT_TRUE(
7401 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(0, offset)).did_scroll);
7402 EXPECT_FLOAT_EQ(-offset,
7403 host_impl_->top_controls_manager()->ControlsTopOffset());
7404 EXPECT_EQ(gfx::Vector2dF(0, initial_scroll_offset).ToString(),
7405 scroll_layer->CurrentScrollOffset().ToString());
7407 did_request_redraw_ = false;
7408 did_request_animate_ = false;
7409 did_request_commit_ = false;
7411 // End the scroll while the controls are still offset from the limit.
7412 host_impl_->ScrollEnd();
7413 ASSERT_TRUE(host_impl_->top_controls_manager()->animation());
7414 EXPECT_TRUE(did_request_animate_);
7415 EXPECT_TRUE(did_request_redraw_);
7416 EXPECT_FALSE(did_request_commit_);
7418 // Animate the top controls to the limit.
7419 BeginFrameArgs begin_frame_args = CreateBeginFrameArgsForTesting(
7420 BEGINFRAME_FROM_HERE, base::TimeTicks::Now());
7421 while (did_request_animate_) {
7422 did_request_redraw_ = false;
7423 did_request_animate_ = false;
7424 did_request_commit_ = false;
7426 float old_offset =
7427 host_impl_->top_controls_manager()->ControlsTopOffset();
7429 begin_frame_args.frame_time += base::TimeDelta::FromMilliseconds(5);
7430 host_impl_->WillBeginImplFrame(begin_frame_args);
7431 host_impl_->Animate();
7433 float new_offset =
7434 host_impl_->top_controls_manager()->ControlsTopOffset();
7436 if (new_offset != old_offset) {
7437 EXPECT_TRUE(did_request_redraw_);
7438 EXPECT_TRUE(did_request_commit_);
7440 host_impl_->DidFinishImplFrame();
7442 EXPECT_FALSE(host_impl_->top_controls_manager()->animation());
7443 EXPECT_EQ(-top_controls_height_,
7444 host_impl_->top_controls_manager()->ControlsTopOffset());
7447 TEST_F(LayerTreeHostImplWithTopControlsTest,
7448 TopControlsAnimationAfterMainThreadFlingStopped) {
7449 LayerImpl* scroll_layer = SetupScrollAndContentsLayers(gfx::Size(100, 200));
7450 host_impl_->SetViewportSize(gfx::Size(100, 100));
7451 host_impl_->top_controls_manager()->UpdateTopControlsState(BOTH, SHOWN,
7452 false);
7453 float initial_scroll_offset = 50;
7454 scroll_layer->PushScrollOffsetFromMainThread(
7455 gfx::ScrollOffset(0, initial_scroll_offset));
7456 DrawFrame();
7458 EXPECT_EQ(InputHandler::SCROLL_STARTED,
7459 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
7460 EXPECT_EQ(0, host_impl_->top_controls_manager()->ControlsTopOffset());
7461 EXPECT_EQ(gfx::Vector2dF(0, initial_scroll_offset).ToString(),
7462 scroll_layer->CurrentScrollOffset().ToString());
7464 // Scroll the top controls partially.
7465 const float residue = 15;
7466 float offset = top_controls_height_ - residue;
7467 EXPECT_TRUE(
7468 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(0, offset)).did_scroll);
7469 EXPECT_FLOAT_EQ(-offset,
7470 host_impl_->top_controls_manager()->ControlsTopOffset());
7471 EXPECT_EQ(gfx::Vector2dF(0, initial_scroll_offset).ToString(),
7472 scroll_layer->CurrentScrollOffset().ToString());
7474 did_request_redraw_ = false;
7475 did_request_animate_ = false;
7476 did_request_commit_ = false;
7478 // End the fling while the controls are still offset from the limit.
7479 host_impl_->MainThreadHasStoppedFlinging();
7480 ASSERT_TRUE(host_impl_->top_controls_manager()->animation());
7481 EXPECT_TRUE(did_request_animate_);
7482 EXPECT_TRUE(did_request_redraw_);
7483 EXPECT_FALSE(did_request_commit_);
7485 // Animate the top controls to the limit.
7486 BeginFrameArgs begin_frame_args = CreateBeginFrameArgsForTesting(
7487 BEGINFRAME_FROM_HERE, base::TimeTicks::Now());
7488 while (did_request_animate_) {
7489 did_request_redraw_ = false;
7490 did_request_animate_ = false;
7491 did_request_commit_ = false;
7493 float old_offset = host_impl_->top_controls_manager()->ControlsTopOffset();
7495 begin_frame_args.frame_time += base::TimeDelta::FromMilliseconds(5);
7496 host_impl_->WillBeginImplFrame(begin_frame_args);
7497 host_impl_->Animate();
7499 float new_offset = host_impl_->top_controls_manager()->ControlsTopOffset();
7501 if (new_offset != old_offset) {
7502 EXPECT_TRUE(did_request_redraw_);
7503 EXPECT_TRUE(did_request_commit_);
7505 host_impl_->DidFinishImplFrame();
7507 EXPECT_FALSE(host_impl_->top_controls_manager()->animation());
7508 EXPECT_EQ(-top_controls_height_,
7509 host_impl_->top_controls_manager()->ControlsTopOffset());
7512 TEST_F(LayerTreeHostImplWithTopControlsTest,
7513 TopControlsScrollDeltaInOverScroll) {
7514 // Verifies that the overscroll delta should not have accumulated in
7515 // the top controls if we do a hide and show without releasing finger.
7517 LayerImpl* scroll_layer = SetupScrollAndContentsLayers(gfx::Size(100, 200));
7518 host_impl_->SetViewportSize(gfx::Size(100, 100));
7519 host_impl_->top_controls_manager()->UpdateTopControlsState(BOTH, SHOWN,
7520 false);
7521 DrawFrame();
7523 EXPECT_EQ(InputHandler::SCROLL_STARTED,
7524 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
7525 EXPECT_EQ(0, host_impl_->top_controls_manager()->ControlsTopOffset());
7527 float offset = 50;
7528 EXPECT_TRUE(
7529 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(0, offset)).did_scroll);
7530 EXPECT_EQ(-offset, host_impl_->top_controls_manager()->ControlsTopOffset());
7531 EXPECT_EQ(gfx::Vector2dF().ToString(),
7532 scroll_layer->CurrentScrollOffset().ToString());
7534 EXPECT_TRUE(
7535 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(0, offset)).did_scroll);
7536 EXPECT_EQ(gfx::Vector2dF(0, offset).ToString(),
7537 scroll_layer->CurrentScrollOffset().ToString());
7539 EXPECT_TRUE(
7540 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(0, offset)).did_scroll);
7542 // Should have fully scrolled
7543 EXPECT_EQ(gfx::Vector2dF(0, scroll_layer->MaxScrollOffset().y()).ToString(),
7544 scroll_layer->CurrentScrollOffset().ToString());
7546 float overscrollamount = 10;
7548 // Overscroll the content
7549 EXPECT_FALSE(
7550 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(0, overscrollamount))
7551 .did_scroll);
7552 EXPECT_EQ(gfx::Vector2dF(0, 2 * offset).ToString(),
7553 scroll_layer->CurrentScrollOffset().ToString());
7554 EXPECT_EQ(gfx::Vector2dF(0, overscrollamount).ToString(),
7555 host_impl_->accumulated_root_overscroll().ToString());
7557 EXPECT_TRUE(host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(0, -2 * offset))
7558 .did_scroll);
7559 EXPECT_EQ(gfx::Vector2dF(0, 0).ToString(),
7560 scroll_layer->CurrentScrollOffset().ToString());
7561 EXPECT_EQ(-offset, host_impl_->top_controls_manager()->ControlsTopOffset());
7563 EXPECT_TRUE(
7564 host_impl_->ScrollBy(gfx::Point(), gfx::Vector2d(0, -offset)).did_scroll);
7565 EXPECT_EQ(gfx::Vector2dF(0, 0).ToString(),
7566 scroll_layer->CurrentScrollOffset().ToString());
7568 // Top controls should be fully visible
7569 EXPECT_EQ(0, host_impl_->top_controls_manager()->ControlsTopOffset());
7571 host_impl_->ScrollEnd();
7574 class LayerTreeHostImplVirtualViewportTest : public LayerTreeHostImplTest {
7575 public:
7576 void SetupVirtualViewportLayers(const gfx::Size& content_size,
7577 const gfx::Size& outer_viewport,
7578 const gfx::Size& inner_viewport) {
7579 LayerTreeImpl* layer_tree_impl = host_impl_->active_tree();
7580 const int kOuterViewportClipLayerId = 6;
7581 const int kOuterViewportScrollLayerId = 7;
7582 const int kInnerViewportScrollLayerId = 2;
7583 const int kInnerViewportClipLayerId = 4;
7584 const int kPageScaleLayerId = 5;
7586 scoped_ptr<LayerImpl> inner_scroll =
7587 LayerImpl::Create(layer_tree_impl, kInnerViewportScrollLayerId);
7588 inner_scroll->SetIsContainerForFixedPositionLayers(true);
7589 inner_scroll->PushScrollOffsetFromMainThread(gfx::ScrollOffset());
7591 scoped_ptr<LayerImpl> inner_clip =
7592 LayerImpl::Create(layer_tree_impl, kInnerViewportClipLayerId);
7593 inner_clip->SetBounds(inner_viewport);
7595 scoped_ptr<LayerImpl> page_scale =
7596 LayerImpl::Create(layer_tree_impl, kPageScaleLayerId);
7598 inner_scroll->SetScrollClipLayer(inner_clip->id());
7599 inner_scroll->SetBounds(outer_viewport);
7600 inner_scroll->SetPosition(gfx::PointF());
7602 scoped_ptr<LayerImpl> outer_clip =
7603 LayerImpl::Create(layer_tree_impl, kOuterViewportClipLayerId);
7604 outer_clip->SetBounds(outer_viewport);
7605 outer_clip->SetIsContainerForFixedPositionLayers(true);
7607 scoped_ptr<LayerImpl> outer_scroll =
7608 LayerImpl::Create(layer_tree_impl, kOuterViewportScrollLayerId);
7609 outer_scroll->SetScrollClipLayer(outer_clip->id());
7610 outer_scroll->PushScrollOffsetFromMainThread(gfx::ScrollOffset());
7611 outer_scroll->SetBounds(content_size);
7612 outer_scroll->SetPosition(gfx::PointF());
7614 scoped_ptr<LayerImpl> contents =
7615 LayerImpl::Create(layer_tree_impl, 8);
7616 contents->SetDrawsContent(true);
7617 contents->SetBounds(content_size);
7618 contents->SetPosition(gfx::PointF());
7620 outer_scroll->AddChild(contents.Pass());
7621 outer_clip->AddChild(outer_scroll.Pass());
7622 inner_scroll->AddChild(outer_clip.Pass());
7623 page_scale->AddChild(inner_scroll.Pass());
7624 inner_clip->AddChild(page_scale.Pass());
7626 inner_clip->SetHasRenderSurface(true);
7627 layer_tree_impl->SetRootLayer(inner_clip.Pass());
7628 layer_tree_impl->SetViewportLayersFromIds(
7629 Layer::INVALID_ID, kPageScaleLayerId, kInnerViewportScrollLayerId,
7630 kOuterViewportScrollLayerId);
7632 host_impl_->active_tree()->DidBecomeActive();
7636 TEST_F(LayerTreeHostImplVirtualViewportTest, ScrollBothInnerAndOuterLayer) {
7637 gfx::Size content_size = gfx::Size(100, 160);
7638 gfx::Size outer_viewport = gfx::Size(50, 80);
7639 gfx::Size inner_viewport = gfx::Size(25, 40);
7641 SetupVirtualViewportLayers(content_size, outer_viewport, inner_viewport);
7643 TestScrollOffsetDelegate scroll_delegate;
7644 host_impl_->SetRootLayerScrollOffsetDelegate(&scroll_delegate);
7646 LayerImpl* outer_scroll = host_impl_->OuterViewportScrollLayer();
7647 LayerImpl* inner_scroll = host_impl_->InnerViewportScrollLayer();
7648 DrawFrame();
7650 gfx::ScrollOffset inner_expected;
7651 gfx::ScrollOffset outer_expected;
7652 EXPECT_EQ(inner_expected, inner_scroll->CurrentScrollOffset());
7653 EXPECT_EQ(outer_expected, outer_scroll->CurrentScrollOffset());
7655 gfx::ScrollOffset current_offset(70.f, 100.f);
7657 host_impl_->OnRootLayerDelegatedScrollOffsetChanged(current_offset);
7658 EXPECT_EQ(gfx::ScrollOffset(25.f, 40.f), inner_scroll->MaxScrollOffset());
7659 EXPECT_EQ(gfx::ScrollOffset(50.f, 80.f), outer_scroll->MaxScrollOffset());
7661 // Outer viewport scrolls first. Then the rest is applied to the inner
7662 // viewport.
7663 EXPECT_EQ(gfx::ScrollOffset(20.f, 20.f),
7664 inner_scroll->CurrentScrollOffset());
7665 EXPECT_EQ(gfx::ScrollOffset(50.f, 80.f),
7666 outer_scroll->CurrentScrollOffset());
7670 TEST_F(LayerTreeHostImplVirtualViewportTest, FlingScrollBubblesToInner) {
7671 gfx::Size content_size = gfx::Size(200, 320);
7672 gfx::Size outer_viewport = gfx::Size(100, 160);
7673 gfx::Size inner_viewport = gfx::Size(50, 80);
7675 SetupVirtualViewportLayers(content_size, outer_viewport, inner_viewport);
7677 LayerImpl* outer_scroll = host_impl_->OuterViewportScrollLayer();
7678 LayerImpl* inner_scroll = host_impl_->InnerViewportScrollLayer();
7679 DrawFrame();
7681 gfx::Vector2dF inner_expected;
7682 gfx::Vector2dF outer_expected;
7683 EXPECT_VECTOR_EQ(inner_expected, inner_scroll->CurrentScrollOffset());
7684 EXPECT_VECTOR_EQ(outer_expected, outer_scroll->CurrentScrollOffset());
7686 // Scrolling the viewport always sets the outer scroll layer as the
7687 // currently scrolling layer.
7688 EXPECT_EQ(InputHandler::SCROLL_STARTED,
7689 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
7690 EXPECT_EQ(outer_scroll, host_impl_->CurrentlyScrollingLayer());
7691 EXPECT_EQ(InputHandler::SCROLL_STARTED, host_impl_->FlingScrollBegin());
7692 EXPECT_EQ(outer_scroll, host_impl_->CurrentlyScrollingLayer());
7694 gfx::Vector2d scroll_delta(inner_viewport.width() / 2.f,
7695 inner_viewport.height() / 2.f);
7696 host_impl_->ScrollBy(gfx::Point(), scroll_delta);
7697 inner_expected += gfx::Vector2dF(scroll_delta.x(), scroll_delta.y());
7698 EXPECT_EQ(host_impl_->CurrentlyScrollingLayer(), outer_scroll);
7700 host_impl_->ScrollEnd();
7701 EXPECT_EQ(nullptr, host_impl_->CurrentlyScrollingLayer());
7703 EXPECT_VECTOR_EQ(inner_expected, inner_scroll->CurrentScrollOffset());
7704 EXPECT_VECTOR_EQ(outer_expected, outer_scroll->CurrentScrollOffset());
7706 // Fling past the inner viewport boundry, make sure outer viewport scrolls.
7707 EXPECT_EQ(InputHandler::SCROLL_STARTED,
7708 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
7709 EXPECT_EQ(outer_scroll, host_impl_->CurrentlyScrollingLayer());
7710 EXPECT_EQ(InputHandler::SCROLL_STARTED, host_impl_->FlingScrollBegin());
7711 EXPECT_EQ(outer_scroll, host_impl_->CurrentlyScrollingLayer());
7713 host_impl_->ScrollBy(gfx::Point(), scroll_delta);
7714 inner_expected += gfx::Vector2dF(scroll_delta.x(), scroll_delta.y());
7715 EXPECT_EQ(outer_scroll, host_impl_->CurrentlyScrollingLayer());
7717 host_impl_->ScrollBy(gfx::Point(), scroll_delta);
7718 outer_expected += gfx::Vector2dF(scroll_delta.x(), scroll_delta.y());
7719 EXPECT_EQ(outer_scroll, host_impl_->CurrentlyScrollingLayer());
7721 host_impl_->ScrollEnd();
7722 EXPECT_EQ(nullptr, host_impl_->CurrentlyScrollingLayer());
7724 EXPECT_VECTOR_EQ(inner_expected, inner_scroll->CurrentScrollOffset());
7725 EXPECT_VECTOR_EQ(outer_expected, outer_scroll->CurrentScrollOffset());
7729 TEST_F(LayerTreeHostImplVirtualViewportTest,
7730 DiagonalScrollBubblesPerfectlyToInner) {
7731 gfx::Size content_size = gfx::Size(200, 320);
7732 gfx::Size outer_viewport = gfx::Size(100, 160);
7733 gfx::Size inner_viewport = gfx::Size(50, 80);
7735 SetupVirtualViewportLayers(content_size, outer_viewport, inner_viewport);
7737 LayerImpl* outer_scroll = host_impl_->OuterViewportScrollLayer();
7738 LayerImpl* inner_scroll = host_impl_->InnerViewportScrollLayer();
7739 DrawFrame();
7741 gfx::Vector2dF inner_expected;
7742 gfx::Vector2dF outer_expected;
7743 EXPECT_VECTOR_EQ(inner_expected, inner_scroll->CurrentScrollOffset());
7744 EXPECT_VECTOR_EQ(outer_expected, outer_scroll->CurrentScrollOffset());
7746 // Make sure the scroll goes to the inner viewport first.
7747 EXPECT_EQ(InputHandler::SCROLL_STARTED,
7748 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
7749 EXPECT_EQ(InputHandler::SCROLL_STARTED, host_impl_->FlingScrollBegin());
7750 EXPECT_TRUE(host_impl_->IsCurrentlyScrollingLayerAt(gfx::Point(),
7751 InputHandler::GESTURE));
7753 // Scroll near the edge of the outer viewport.
7754 gfx::Vector2d scroll_delta(inner_viewport.width() / 2.f,
7755 inner_viewport.height() / 2.f);
7756 host_impl_->ScrollBy(gfx::Point(), scroll_delta);
7757 inner_expected += scroll_delta;
7758 EXPECT_TRUE(host_impl_->IsCurrentlyScrollingLayerAt(gfx::Point(),
7759 InputHandler::GESTURE));
7761 EXPECT_VECTOR_EQ(inner_expected, inner_scroll->CurrentScrollOffset());
7762 EXPECT_VECTOR_EQ(outer_expected, outer_scroll->CurrentScrollOffset());
7764 // Now diagonal scroll across the outer viewport boundary in a single event.
7765 // The entirety of the scroll should be consumed, as bubbling between inner
7766 // and outer viewport layers is perfect.
7767 host_impl_->ScrollBy(gfx::Point(), gfx::ScaleVector2d(scroll_delta, 2));
7768 EXPECT_TRUE(host_impl_->IsCurrentlyScrollingLayerAt(gfx::Point(),
7769 InputHandler::GESTURE));
7770 outer_expected += scroll_delta;
7771 inner_expected += scroll_delta;
7772 host_impl_->ScrollEnd();
7773 EXPECT_FALSE(host_impl_->IsCurrentlyScrollingLayerAt(
7774 gfx::Point(), InputHandler::GESTURE));
7776 EXPECT_VECTOR_EQ(inner_expected, inner_scroll->CurrentScrollOffset());
7777 EXPECT_VECTOR_EQ(outer_expected, outer_scroll->CurrentScrollOffset());
7781 TEST_F(LayerTreeHostImplVirtualViewportTest,
7782 TouchFlingCanLockToViewportLayerAfterBubbling) {
7783 gfx::Size content_size = gfx::Size(100, 160);
7784 gfx::Size outer_viewport = gfx::Size(50, 80);
7785 gfx::Size inner_viewport = gfx::Size(25, 40);
7787 SetupVirtualViewportLayers(content_size, outer_viewport, inner_viewport);
7789 LayerImpl* outer_scroll = host_impl_->OuterViewportScrollLayer();
7790 LayerImpl* inner_scroll = host_impl_->InnerViewportScrollLayer();
7792 scoped_ptr<LayerImpl> child =
7793 CreateScrollableLayer(10, outer_viewport, outer_scroll);
7794 LayerImpl* child_scroll = child.get();
7795 outer_scroll->children()[0]->AddChild(child.Pass());
7797 DrawFrame();
7799 scoped_ptr<ScrollAndScaleSet> scroll_info;
7801 gfx::Vector2d scroll_delta(0, inner_viewport.height());
7802 EXPECT_EQ(InputHandler::SCROLL_STARTED,
7803 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
7804 EXPECT_TRUE(host_impl_->ScrollBy(gfx::Point(), scroll_delta).did_scroll);
7805 EXPECT_TRUE(host_impl_->IsCurrentlyScrollingLayerAt(gfx::Point(),
7806 InputHandler::GESTURE));
7808 // The child should have scrolled up to its limit.
7809 scroll_info = host_impl_->ProcessScrollDeltas();
7810 ASSERT_EQ(1u, scroll_info->scrolls.size());
7811 EXPECT_TRUE(
7812 ScrollInfoContains(*scroll_info, child_scroll->id(), scroll_delta));
7813 EXPECT_EQ(host_impl_->CurrentlyScrollingLayer(), child_scroll);
7815 // The first |ScrollBy| after the fling should re-lock the scrolling
7816 // layer to the first layer that scrolled, the inner viewport scroll layer.
7817 EXPECT_EQ(InputHandler::SCROLL_STARTED, host_impl_->FlingScrollBegin());
7818 EXPECT_TRUE(host_impl_->ScrollBy(gfx::Point(), scroll_delta).did_scroll);
7819 EXPECT_EQ(host_impl_->CurrentlyScrollingLayer(), inner_scroll);
7820 EXPECT_TRUE(host_impl_->IsCurrentlyScrollingLayerAt(gfx::Point(),
7821 InputHandler::GESTURE));
7823 // The inner viewport should have scrolled up to its limit.
7824 scroll_info = host_impl_->ProcessScrollDeltas();
7825 ASSERT_EQ(2u, scroll_info->scrolls.size());
7826 EXPECT_TRUE(
7827 ScrollInfoContains(*scroll_info, child_scroll->id(), scroll_delta));
7828 EXPECT_TRUE(
7829 ScrollInfoContains(*scroll_info, inner_scroll->id(), scroll_delta));
7831 // As the locked layer is at its limit, no further scrolling can occur.
7832 EXPECT_FALSE(host_impl_->ScrollBy(gfx::Point(), scroll_delta).did_scroll);
7833 EXPECT_EQ(host_impl_->CurrentlyScrollingLayer(), inner_scroll);
7834 host_impl_->ScrollEnd();
7835 EXPECT_FALSE(host_impl_->IsCurrentlyScrollingLayerAt(
7836 gfx::Point(), InputHandler::GESTURE));
7840 TEST_F(LayerTreeHostImplVirtualViewportTest,
7841 ScrollBeginEventThatTargetsViewportLayerSkipsHitTest) {
7842 gfx::Size content_size = gfx::Size(100, 160);
7843 gfx::Size outer_viewport = gfx::Size(50, 80);
7844 gfx::Size inner_viewport = gfx::Size(25, 40);
7846 SetupVirtualViewportLayers(content_size, outer_viewport, inner_viewport);
7848 LayerImpl* outer_scroll = host_impl_->OuterViewportScrollLayer();
7849 LayerImpl* inner_scroll = host_impl_->InnerViewportScrollLayer();
7851 scoped_ptr<LayerImpl> child =
7852 CreateScrollableLayer(10, outer_viewport, outer_scroll);
7853 LayerImpl* child_scroll = child.get();
7854 outer_scroll->children()[0]->AddChild(child.Pass());
7856 DrawFrame();
7858 EXPECT_EQ(InputHandler::SCROLL_STARTED,
7859 host_impl_->RootScrollBegin(InputHandler::GESTURE));
7860 EXPECT_EQ(host_impl_->CurrentlyScrollingLayer(), inner_scroll);
7861 host_impl_->ScrollEnd();
7862 EXPECT_EQ(InputHandler::SCROLL_STARTED,
7863 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
7864 EXPECT_EQ(host_impl_->CurrentlyScrollingLayer(), child_scroll);
7865 host_impl_->ScrollEnd();
7868 TEST_F(LayerTreeHostImplVirtualViewportTest,
7869 NoOverscrollWhenInnerViewportCantScroll) {
7870 InputHandlerScrollResult scroll_result;
7871 gfx::Size content_size = gfx::Size(100, 160);
7872 gfx::Size outer_viewport = gfx::Size(50, 80);
7873 gfx::Size inner_viewport = gfx::Size(25, 40);
7874 SetupVirtualViewportLayers(content_size, outer_viewport, inner_viewport);
7875 DrawFrame();
7877 // Make inner viewport unscrollable.
7878 LayerImpl* inner_scroll = host_impl_->InnerViewportScrollLayer();
7879 inner_scroll->set_user_scrollable_horizontal(false);
7880 inner_scroll->set_user_scrollable_vertical(false);
7882 // Ensure inner viewport doesn't react to scrolls (test it's unscrollable).
7883 EXPECT_VECTOR_EQ(gfx::Vector2dF(), inner_scroll->CurrentScrollOffset());
7884 EXPECT_EQ(InputHandler::SCROLL_STARTED,
7885 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
7886 scroll_result = host_impl_->ScrollBy(gfx::Point(), gfx::Vector2dF(0, 100));
7887 EXPECT_VECTOR_EQ(gfx::Vector2dF(), inner_scroll->CurrentScrollOffset());
7889 // When inner viewport is unscrollable, a fling gives zero overscroll.
7890 EXPECT_FALSE(scroll_result.did_overscroll_root);
7891 EXPECT_EQ(gfx::Vector2dF(), host_impl_->accumulated_root_overscroll());
7894 class LayerTreeHostImplWithImplicitLimitsTest : public LayerTreeHostImplTest {
7895 public:
7896 void SetUp() override {
7897 LayerTreeSettings settings = DefaultSettings();
7898 settings.max_memory_for_prepaint_percentage = 50;
7899 CreateHostImpl(settings, CreateOutputSurface());
7903 TEST_F(LayerTreeHostImplWithImplicitLimitsTest, ImplicitMemoryLimits) {
7904 // Set up a memory policy and percentages which could cause
7905 // 32-bit integer overflows.
7906 ManagedMemoryPolicy mem_policy(300 * 1024 * 1024); // 300MB
7908 // Verify implicit limits are calculated correctly with no overflows
7909 host_impl_->SetMemoryPolicy(mem_policy);
7910 EXPECT_EQ(host_impl_->global_tile_state().hard_memory_limit_in_bytes,
7911 300u * 1024u * 1024u);
7912 EXPECT_EQ(host_impl_->global_tile_state().soft_memory_limit_in_bytes,
7913 150u * 1024u * 1024u);
7916 TEST_F(LayerTreeHostImplTest, ExternalTransformReflectedInNextDraw) {
7917 const gfx::Size layer_size(100, 100);
7918 gfx::Transform external_transform;
7919 const gfx::Rect external_viewport(layer_size);
7920 const gfx::Rect external_clip(layer_size);
7921 const bool resourceless_software_draw = false;
7922 LayerImpl* layer = SetupScrollAndContentsLayers(layer_size);
7924 host_impl_->SetExternalDrawConstraints(external_transform,
7925 external_viewport,
7926 external_clip,
7927 external_viewport,
7928 external_transform,
7929 resourceless_software_draw);
7930 DrawFrame();
7931 EXPECT_TRANSFORMATION_MATRIX_EQ(
7932 external_transform, layer->draw_properties().target_space_transform);
7934 external_transform.Translate(20, 20);
7935 host_impl_->SetExternalDrawConstraints(external_transform,
7936 external_viewport,
7937 external_clip,
7938 external_viewport,
7939 external_transform,
7940 resourceless_software_draw);
7941 DrawFrame();
7942 EXPECT_TRANSFORMATION_MATRIX_EQ(
7943 external_transform, layer->draw_properties().target_space_transform);
7946 TEST_F(LayerTreeHostImplTest, ScrollAnimated) {
7947 SetupScrollAndContentsLayers(gfx::Size(100, 200));
7948 DrawFrame();
7950 base::TimeTicks start_time =
7951 base::TimeTicks() + base::TimeDelta::FromMilliseconds(100);
7953 BeginFrameArgs begin_frame_args =
7954 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE);
7956 EXPECT_EQ(InputHandler::SCROLL_STARTED,
7957 host_impl_->ScrollAnimated(gfx::Point(), gfx::Vector2d(0, 50)));
7959 LayerImpl* scrolling_layer = host_impl_->CurrentlyScrollingLayer();
7961 begin_frame_args.frame_time = start_time;
7962 host_impl_->WillBeginImplFrame(begin_frame_args);
7963 host_impl_->Animate();
7964 host_impl_->UpdateAnimationState(true);
7966 EXPECT_EQ(gfx::ScrollOffset(), scrolling_layer->CurrentScrollOffset());
7967 host_impl_->DidFinishImplFrame();
7969 begin_frame_args.frame_time =
7970 start_time + base::TimeDelta::FromMilliseconds(50);
7971 host_impl_->WillBeginImplFrame(begin_frame_args);
7972 host_impl_->Animate();
7973 host_impl_->UpdateAnimationState(true);
7975 float y = scrolling_layer->CurrentScrollOffset().y();
7976 EXPECT_TRUE(y > 1 && y < 49);
7978 // Update target.
7979 EXPECT_EQ(InputHandler::SCROLL_STARTED,
7980 host_impl_->ScrollAnimated(gfx::Point(), gfx::Vector2d(0, 50)));
7981 host_impl_->DidFinishImplFrame();
7983 begin_frame_args.frame_time =
7984 start_time + base::TimeDelta::FromMilliseconds(200);
7985 host_impl_->WillBeginImplFrame(begin_frame_args);
7986 host_impl_->Animate();
7987 host_impl_->UpdateAnimationState(true);
7989 y = scrolling_layer->CurrentScrollOffset().y();
7990 EXPECT_TRUE(y > 50 && y < 100);
7991 EXPECT_EQ(scrolling_layer, host_impl_->CurrentlyScrollingLayer());
7992 host_impl_->DidFinishImplFrame();
7994 begin_frame_args.frame_time =
7995 start_time + base::TimeDelta::FromMilliseconds(250);
7996 host_impl_->WillBeginImplFrame(begin_frame_args);
7997 host_impl_->Animate();
7998 host_impl_->UpdateAnimationState(true);
8000 EXPECT_VECTOR_EQ(gfx::ScrollOffset(0, 100),
8001 scrolling_layer->CurrentScrollOffset());
8002 EXPECT_EQ(NULL, host_impl_->CurrentlyScrollingLayer());
8003 host_impl_->DidFinishImplFrame();
8006 // Evolved from LayerTreeHostImplTest.ScrollAnimated.
8007 TEST_F(LayerTreeHostImplTimelinesTest, ScrollAnimated) {
8008 SetupScrollAndContentsLayers(gfx::Size(100, 200));
8009 DrawFrame();
8011 base::TimeTicks start_time =
8012 base::TimeTicks() + base::TimeDelta::FromMilliseconds(100);
8014 BeginFrameArgs begin_frame_args =
8015 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE);
8017 EXPECT_EQ(InputHandler::SCROLL_STARTED,
8018 host_impl_->ScrollAnimated(gfx::Point(), gfx::Vector2d(0, 50)));
8020 LayerImpl* scrolling_layer = host_impl_->CurrentlyScrollingLayer();
8022 begin_frame_args.frame_time = start_time;
8023 host_impl_->WillBeginImplFrame(begin_frame_args);
8024 host_impl_->Animate();
8025 host_impl_->UpdateAnimationState(true);
8027 EXPECT_EQ(gfx::ScrollOffset(), scrolling_layer->CurrentScrollOffset());
8028 host_impl_->DidFinishImplFrame();
8030 begin_frame_args.frame_time =
8031 start_time + base::TimeDelta::FromMilliseconds(50);
8032 host_impl_->WillBeginImplFrame(begin_frame_args);
8033 host_impl_->Animate();
8034 host_impl_->UpdateAnimationState(true);
8036 float y = scrolling_layer->CurrentScrollOffset().y();
8037 EXPECT_TRUE(y > 1 && y < 49);
8039 // Update target.
8040 EXPECT_EQ(InputHandler::SCROLL_STARTED,
8041 host_impl_->ScrollAnimated(gfx::Point(), gfx::Vector2d(0, 50)));
8042 host_impl_->DidFinishImplFrame();
8044 begin_frame_args.frame_time =
8045 start_time + base::TimeDelta::FromMilliseconds(200);
8046 host_impl_->WillBeginImplFrame(begin_frame_args);
8047 host_impl_->Animate();
8048 host_impl_->UpdateAnimationState(true);
8050 y = scrolling_layer->CurrentScrollOffset().y();
8051 EXPECT_TRUE(y > 50 && y < 100);
8052 EXPECT_EQ(scrolling_layer, host_impl_->CurrentlyScrollingLayer());
8053 host_impl_->DidFinishImplFrame();
8055 begin_frame_args.frame_time =
8056 start_time + base::TimeDelta::FromMilliseconds(250);
8057 host_impl_->WillBeginImplFrame(begin_frame_args);
8058 host_impl_->Animate();
8059 host_impl_->UpdateAnimationState(true);
8061 EXPECT_VECTOR_EQ(gfx::ScrollOffset(0, 100),
8062 scrolling_layer->CurrentScrollOffset());
8063 EXPECT_EQ(NULL, host_impl_->CurrentlyScrollingLayer());
8064 host_impl_->DidFinishImplFrame();
8067 TEST_F(LayerTreeHostImplTest, InvalidLayerNotAddedToRasterQueue) {
8068 host_impl_->CreatePendingTree();
8070 Region empty_invalidation;
8071 scoped_refptr<RasterSource> pile_with_tiles(
8072 FakePicturePileImpl::CreateFilledPileWithDefaultTileSize(
8073 gfx::Size(10, 10)));
8075 scoped_ptr<FakePictureLayerImpl> layer =
8076 FakePictureLayerImpl::Create(host_impl_->pending_tree(), 11);
8077 layer->SetBounds(gfx::Size(10, 10));
8078 layer->set_gpu_raster_max_texture_size(host_impl_->device_viewport_size());
8079 layer->SetDrawsContent(true);
8080 layer->tilings()->AddTiling(1.0f, pile_with_tiles);
8081 layer->UpdateRasterSource(pile_with_tiles, &empty_invalidation, nullptr);
8082 layer->tilings()->tiling_at(0)->set_resolution(
8083 TileResolution::HIGH_RESOLUTION);
8084 layer->tilings()->tiling_at(0)->CreateAllTilesForTesting();
8085 layer->tilings()->tiling_at(0)->ComputeTilePriorityRects(
8086 gfx::Rect(gfx::Size(10, 10)), 1.f, 1.0, Occlusion());
8087 host_impl_->pending_tree()->SetRootLayer(layer.Pass());
8089 FakePictureLayerImpl* root_layer = static_cast<FakePictureLayerImpl*>(
8090 host_impl_->pending_tree()->root_layer());
8092 root_layer->set_has_valid_tile_priorities(true);
8093 scoped_ptr<RasterTilePriorityQueue> non_empty_raster_priority_queue_all =
8094 host_impl_->BuildRasterQueue(TreePriority::SAME_PRIORITY_FOR_BOTH_TREES,
8095 RasterTilePriorityQueue::Type::ALL);
8096 EXPECT_FALSE(non_empty_raster_priority_queue_all->IsEmpty());
8098 root_layer->set_has_valid_tile_priorities(false);
8099 scoped_ptr<RasterTilePriorityQueue> empty_raster_priority_queue_all =
8100 host_impl_->BuildRasterQueue(TreePriority::SAME_PRIORITY_FOR_BOTH_TREES,
8101 RasterTilePriorityQueue::Type::ALL);
8102 EXPECT_TRUE(empty_raster_priority_queue_all->IsEmpty());
8105 TEST_F(LayerTreeHostImplTest, DidBecomeActive) {
8106 host_impl_->CreatePendingTree();
8107 host_impl_->ActivateSyncTree();
8108 host_impl_->CreatePendingTree();
8110 LayerTreeImpl* pending_tree = host_impl_->pending_tree();
8112 scoped_ptr<FakePictureLayerImpl> pending_layer =
8113 FakePictureLayerImpl::Create(pending_tree, 10);
8114 FakePictureLayerImpl* raw_pending_layer = pending_layer.get();
8115 pending_tree->SetRootLayer(pending_layer.Pass());
8116 ASSERT_EQ(raw_pending_layer, pending_tree->root_layer());
8118 EXPECT_EQ(0u, raw_pending_layer->did_become_active_call_count());
8119 pending_tree->DidBecomeActive();
8120 EXPECT_EQ(1u, raw_pending_layer->did_become_active_call_count());
8122 scoped_ptr<FakePictureLayerImpl> mask_layer =
8123 FakePictureLayerImpl::Create(pending_tree, 11);
8124 FakePictureLayerImpl* raw_mask_layer = mask_layer.get();
8125 raw_pending_layer->SetMaskLayer(mask_layer.Pass());
8126 ASSERT_EQ(raw_mask_layer, raw_pending_layer->mask_layer());
8128 EXPECT_EQ(1u, raw_pending_layer->did_become_active_call_count());
8129 EXPECT_EQ(0u, raw_mask_layer->did_become_active_call_count());
8130 pending_tree->DidBecomeActive();
8131 EXPECT_EQ(2u, raw_pending_layer->did_become_active_call_count());
8132 EXPECT_EQ(1u, raw_mask_layer->did_become_active_call_count());
8134 scoped_ptr<FakePictureLayerImpl> replica_layer =
8135 FakePictureLayerImpl::Create(pending_tree, 12);
8136 scoped_ptr<FakePictureLayerImpl> replica_mask_layer =
8137 FakePictureLayerImpl::Create(pending_tree, 13);
8138 FakePictureLayerImpl* raw_replica_mask_layer = replica_mask_layer.get();
8139 replica_layer->SetMaskLayer(replica_mask_layer.Pass());
8140 raw_pending_layer->SetReplicaLayer(replica_layer.Pass());
8141 ASSERT_EQ(raw_replica_mask_layer,
8142 raw_pending_layer->replica_layer()->mask_layer());
8144 EXPECT_EQ(2u, raw_pending_layer->did_become_active_call_count());
8145 EXPECT_EQ(1u, raw_mask_layer->did_become_active_call_count());
8146 EXPECT_EQ(0u, raw_replica_mask_layer->did_become_active_call_count());
8147 pending_tree->DidBecomeActive();
8148 EXPECT_EQ(3u, raw_pending_layer->did_become_active_call_count());
8149 EXPECT_EQ(2u, raw_mask_layer->did_become_active_call_count());
8150 EXPECT_EQ(1u, raw_replica_mask_layer->did_become_active_call_count());
8153 TEST_F(LayerTreeHostImplTest, WheelScrollWithPageScaleFactorOnInnerLayer) {
8154 LayerImpl* scroll_layer = SetupScrollAndContentsLayers(gfx::Size(100, 100));
8155 host_impl_->SetViewportSize(gfx::Size(50, 50));
8156 DrawFrame();
8158 EXPECT_EQ(scroll_layer, host_impl_->InnerViewportScrollLayer());
8160 float min_page_scale = 1.f, max_page_scale = 4.f;
8161 float page_scale_factor = 1.f;
8163 // The scroll deltas should have the page scale factor applied.
8165 host_impl_->active_tree()->PushPageScaleFromMainThread(
8166 page_scale_factor, min_page_scale, max_page_scale);
8167 host_impl_->active_tree()->SetPageScaleOnActiveTree(page_scale_factor);
8168 scroll_layer->SetScrollDelta(gfx::Vector2d());
8170 float page_scale_delta = 2.f;
8171 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE);
8172 host_impl_->PinchGestureBegin();
8173 host_impl_->PinchGestureUpdate(page_scale_delta, gfx::Point());
8174 host_impl_->PinchGestureEnd();
8175 host_impl_->ScrollEnd();
8177 gfx::Vector2dF scroll_delta(0, 5);
8178 EXPECT_EQ(InputHandler::SCROLL_STARTED,
8179 host_impl_->ScrollBegin(gfx::Point(), InputHandler::WHEEL));
8180 EXPECT_VECTOR_EQ(gfx::Vector2dF(), scroll_layer->CurrentScrollOffset());
8182 host_impl_->ScrollBy(gfx::Point(), scroll_delta);
8183 host_impl_->ScrollEnd();
8184 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 2.5),
8185 scroll_layer->CurrentScrollOffset());
8189 class LayerTreeHostImplCountingLostSurfaces : public LayerTreeHostImplTest {
8190 public:
8191 LayerTreeHostImplCountingLostSurfaces() : num_lost_surfaces_(0) {}
8192 void DidLoseOutputSurfaceOnImplThread() override { num_lost_surfaces_++; }
8194 protected:
8195 int num_lost_surfaces_;
8198 TEST_F(LayerTreeHostImplCountingLostSurfaces, TwiceLostSurface) {
8199 // Really we just need at least one client notification each time
8200 // we go from having a valid output surface to not having a valid output
8201 // surface.
8202 EXPECT_EQ(0, num_lost_surfaces_);
8203 host_impl_->DidLoseOutputSurface();
8204 EXPECT_EQ(1, num_lost_surfaces_);
8205 host_impl_->DidLoseOutputSurface();
8206 EXPECT_LE(1, num_lost_surfaces_);
8209 TEST_F(LayerTreeHostImplTest, RemoveUnreferencedRenderPass) {
8210 LayerTreeHostImpl::FrameData frame;
8211 frame.render_passes.push_back(RenderPass::Create());
8212 RenderPass* pass3 = frame.render_passes.back();
8213 frame.render_passes.push_back(RenderPass::Create());
8214 RenderPass* pass2 = frame.render_passes.back();
8215 frame.render_passes.push_back(RenderPass::Create());
8216 RenderPass* pass1 = frame.render_passes.back();
8218 pass1->SetNew(RenderPassId(1, 0), gfx::Rect(), gfx::Rect(), gfx::Transform());
8219 pass2->SetNew(RenderPassId(2, 0), gfx::Rect(), gfx::Rect(), gfx::Transform());
8220 pass3->SetNew(RenderPassId(3, 0), gfx::Rect(), gfx::Rect(), gfx::Transform());
8222 frame.render_passes_by_id[pass1->id] = pass1;
8223 frame.render_passes_by_id[pass2->id] = pass2;
8224 frame.render_passes_by_id[pass3->id] = pass3;
8226 // Add a quad to each pass so they aren't empty.
8227 SolidColorDrawQuad* color_quad;
8228 color_quad = pass1->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
8229 color_quad->material = DrawQuad::SOLID_COLOR;
8230 color_quad = pass2->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
8231 color_quad->material = DrawQuad::SOLID_COLOR;
8232 color_quad = pass3->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
8233 color_quad->material = DrawQuad::SOLID_COLOR;
8235 // pass3 is referenced by pass2.
8236 RenderPassDrawQuad* rpdq =
8237 pass2->CreateAndAppendDrawQuad<RenderPassDrawQuad>();
8238 rpdq->material = DrawQuad::RENDER_PASS;
8239 rpdq->render_pass_id = pass3->id;
8241 // But pass2 is not referenced by pass1. So pass2 and pass3 should be culled.
8242 FakeLayerTreeHostImpl::RemoveRenderPasses(&frame);
8243 EXPECT_EQ(1u, frame.render_passes_by_id.size());
8244 EXPECT_TRUE(frame.render_passes_by_id[RenderPassId(1, 0)]);
8245 EXPECT_FALSE(frame.render_passes_by_id[RenderPassId(2, 0)]);
8246 EXPECT_FALSE(frame.render_passes_by_id[RenderPassId(3, 0)]);
8247 EXPECT_EQ(1u, frame.render_passes.size());
8248 EXPECT_EQ(RenderPassId(1, 0), frame.render_passes[0]->id);
8251 TEST_F(LayerTreeHostImplTest, RemoveEmptyRenderPass) {
8252 LayerTreeHostImpl::FrameData frame;
8253 frame.render_passes.push_back(RenderPass::Create());
8254 RenderPass* pass3 = frame.render_passes.back();
8255 frame.render_passes.push_back(RenderPass::Create());
8256 RenderPass* pass2 = frame.render_passes.back();
8257 frame.render_passes.push_back(RenderPass::Create());
8258 RenderPass* pass1 = frame.render_passes.back();
8260 pass1->SetNew(RenderPassId(1, 0), gfx::Rect(), gfx::Rect(), gfx::Transform());
8261 pass2->SetNew(RenderPassId(2, 0), gfx::Rect(), gfx::Rect(), gfx::Transform());
8262 pass3->SetNew(RenderPassId(3, 0), gfx::Rect(), gfx::Rect(), gfx::Transform());
8264 frame.render_passes_by_id[pass1->id] = pass1;
8265 frame.render_passes_by_id[pass2->id] = pass2;
8266 frame.render_passes_by_id[pass3->id] = pass3;
8268 // pass1 is not empty, but pass2 and pass3 are.
8269 SolidColorDrawQuad* color_quad;
8270 color_quad = pass1->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
8271 color_quad->material = DrawQuad::SOLID_COLOR;
8273 // pass3 is referenced by pass2.
8274 RenderPassDrawQuad* rpdq =
8275 pass2->CreateAndAppendDrawQuad<RenderPassDrawQuad>();
8276 rpdq->material = DrawQuad::RENDER_PASS;
8277 rpdq->render_pass_id = pass3->id;
8279 // pass2 is referenced by pass1.
8280 rpdq = pass1->CreateAndAppendDrawQuad<RenderPassDrawQuad>();
8281 rpdq->material = DrawQuad::RENDER_PASS;
8282 rpdq->render_pass_id = pass2->id;
8284 // Since pass3 is empty it should be removed. Then pass2 is empty too, and
8285 // should be removed.
8286 FakeLayerTreeHostImpl::RemoveRenderPasses(&frame);
8287 EXPECT_EQ(1u, frame.render_passes_by_id.size());
8288 EXPECT_TRUE(frame.render_passes_by_id[RenderPassId(1, 0)]);
8289 EXPECT_FALSE(frame.render_passes_by_id[RenderPassId(2, 0)]);
8290 EXPECT_FALSE(frame.render_passes_by_id[RenderPassId(3, 0)]);
8291 EXPECT_EQ(1u, frame.render_passes.size());
8292 EXPECT_EQ(RenderPassId(1, 0), frame.render_passes[0]->id);
8293 // The RenderPassDrawQuad should be removed from pass1.
8294 EXPECT_EQ(1u, pass1->quad_list.size());
8295 EXPECT_EQ(DrawQuad::SOLID_COLOR, pass1->quad_list.ElementAt(0)->material);
8298 TEST_F(LayerTreeHostImplTest, DoNotRemoveEmptyRootRenderPass) {
8299 LayerTreeHostImpl::FrameData frame;
8300 frame.render_passes.push_back(RenderPass::Create());
8301 RenderPass* pass3 = frame.render_passes.back();
8302 frame.render_passes.push_back(RenderPass::Create());
8303 RenderPass* pass2 = frame.render_passes.back();
8304 frame.render_passes.push_back(RenderPass::Create());
8305 RenderPass* pass1 = frame.render_passes.back();
8307 pass1->SetNew(RenderPassId(1, 0), gfx::Rect(), gfx::Rect(), gfx::Transform());
8308 pass2->SetNew(RenderPassId(2, 0), gfx::Rect(), gfx::Rect(), gfx::Transform());
8309 pass3->SetNew(RenderPassId(3, 0), gfx::Rect(), gfx::Rect(), gfx::Transform());
8311 frame.render_passes_by_id[pass1->id] = pass1;
8312 frame.render_passes_by_id[pass2->id] = pass2;
8313 frame.render_passes_by_id[pass3->id] = pass3;
8315 // pass3 is referenced by pass2.
8316 RenderPassDrawQuad* rpdq =
8317 pass2->CreateAndAppendDrawQuad<RenderPassDrawQuad>();
8318 rpdq->material = DrawQuad::RENDER_PASS;
8319 rpdq->render_pass_id = pass3->id;
8321 // pass2 is referenced by pass1.
8322 rpdq = pass1->CreateAndAppendDrawQuad<RenderPassDrawQuad>();
8323 rpdq->material = DrawQuad::RENDER_PASS;
8324 rpdq->render_pass_id = pass2->id;
8326 // Since pass3 is empty it should be removed. Then pass2 is empty too, and
8327 // should be removed. Then pass1 is empty too, but it's the root so it should
8328 // not be removed.
8329 FakeLayerTreeHostImpl::RemoveRenderPasses(&frame);
8330 EXPECT_EQ(1u, frame.render_passes_by_id.size());
8331 EXPECT_TRUE(frame.render_passes_by_id[RenderPassId(1, 0)]);
8332 EXPECT_FALSE(frame.render_passes_by_id[RenderPassId(2, 0)]);
8333 EXPECT_FALSE(frame.render_passes_by_id[RenderPassId(3, 0)]);
8334 EXPECT_EQ(1u, frame.render_passes.size());
8335 EXPECT_EQ(RenderPassId(1, 0), frame.render_passes[0]->id);
8336 // The RenderPassDrawQuad should be removed from pass1.
8337 EXPECT_EQ(0u, pass1->quad_list.size());
8340 class FakeVideoFrameController : public VideoFrameController {
8341 public:
8342 void OnBeginFrame(const BeginFrameArgs& args) override {
8343 begin_frame_args_ = args;
8344 did_draw_frame_ = false;
8347 void DidDrawFrame() override { did_draw_frame_ = true; }
8349 const BeginFrameArgs& begin_frame_args() const { return begin_frame_args_; }
8351 bool did_draw_frame() const { return did_draw_frame_; }
8353 private:
8354 BeginFrameArgs begin_frame_args_;
8355 bool did_draw_frame_ = false;
8358 TEST_F(LayerTreeHostImplTest, AddVideoFrameControllerInsideFrame) {
8359 BeginFrameArgs begin_frame_args =
8360 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE);
8361 FakeVideoFrameController controller;
8363 host_impl_->WillBeginImplFrame(begin_frame_args);
8364 EXPECT_FALSE(controller.begin_frame_args().IsValid());
8365 host_impl_->AddVideoFrameController(&controller);
8366 EXPECT_TRUE(controller.begin_frame_args().IsValid());
8367 host_impl_->DidFinishImplFrame();
8369 EXPECT_FALSE(controller.did_draw_frame());
8370 LayerTreeHostImpl::FrameData frame;
8371 host_impl_->DidDrawAllLayers(frame);
8372 EXPECT_TRUE(controller.did_draw_frame());
8374 controller.OnBeginFrame(begin_frame_args);
8375 EXPECT_FALSE(controller.did_draw_frame());
8376 host_impl_->RemoveVideoFrameController(&controller);
8377 host_impl_->DidDrawAllLayers(frame);
8378 EXPECT_FALSE(controller.did_draw_frame());
8381 TEST_F(LayerTreeHostImplTest, AddVideoFrameControllerOutsideFrame) {
8382 BeginFrameArgs begin_frame_args =
8383 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE);
8384 FakeVideoFrameController controller;
8386 host_impl_->WillBeginImplFrame(begin_frame_args);
8387 host_impl_->DidFinishImplFrame();
8389 EXPECT_FALSE(controller.begin_frame_args().IsValid());
8390 host_impl_->AddVideoFrameController(&controller);
8391 EXPECT_FALSE(controller.begin_frame_args().IsValid());
8393 begin_frame_args = CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE);
8394 EXPECT_FALSE(controller.begin_frame_args().IsValid());
8395 host_impl_->WillBeginImplFrame(begin_frame_args);
8396 EXPECT_TRUE(controller.begin_frame_args().IsValid());
8398 EXPECT_FALSE(controller.did_draw_frame());
8399 LayerTreeHostImpl::FrameData frame;
8400 host_impl_->DidDrawAllLayers(frame);
8401 EXPECT_TRUE(controller.did_draw_frame());
8403 controller.OnBeginFrame(begin_frame_args);
8404 EXPECT_FALSE(controller.did_draw_frame());
8405 host_impl_->RemoveVideoFrameController(&controller);
8406 host_impl_->DidDrawAllLayers(frame);
8407 EXPECT_FALSE(controller.did_draw_frame());
8410 TEST_F(LayerTreeHostImplTest, GpuRasterizationStatusModes) {
8411 EXPECT_FALSE(host_impl_->use_gpu_rasterization());
8413 host_impl_->SetHasGpuRasterizationTrigger(true);
8414 host_impl_->SetContentIsSuitableForGpuRasterization(true);
8415 EXPECT_EQ(GpuRasterizationStatus::ON, host_impl_->gpu_rasterization_status());
8416 EXPECT_TRUE(host_impl_->use_gpu_rasterization());
8418 host_impl_->SetHasGpuRasterizationTrigger(false);
8419 host_impl_->SetContentIsSuitableForGpuRasterization(true);
8420 EXPECT_EQ(GpuRasterizationStatus::OFF_VIEWPORT,
8421 host_impl_->gpu_rasterization_status());
8422 EXPECT_FALSE(host_impl_->use_gpu_rasterization());
8424 host_impl_->SetHasGpuRasterizationTrigger(true);
8425 host_impl_->SetContentIsSuitableForGpuRasterization(false);
8426 EXPECT_EQ(GpuRasterizationStatus::OFF_CONTENT,
8427 host_impl_->gpu_rasterization_status());
8428 EXPECT_FALSE(host_impl_->use_gpu_rasterization());
8429 EXPECT_FALSE(host_impl_->use_msaa());
8431 scoped_ptr<TestWebGraphicsContext3D> context_with_msaa =
8432 TestWebGraphicsContext3D::Create();
8433 context_with_msaa->SetMaxSamples(8);
8435 LayerTreeSettings msaaSettings = GpuRasterizationEnabledSettings();
8436 msaaSettings.gpu_rasterization_msaa_sample_count = 4;
8437 EXPECT_TRUE(CreateHostImpl(
8438 msaaSettings, FakeOutputSurface::Create3d(context_with_msaa.Pass())));
8439 host_impl_->SetHasGpuRasterizationTrigger(true);
8440 host_impl_->SetContentIsSuitableForGpuRasterization(false);
8441 EXPECT_EQ(GpuRasterizationStatus::MSAA_CONTENT,
8442 host_impl_->gpu_rasterization_status());
8443 EXPECT_TRUE(host_impl_->use_gpu_rasterization());
8444 EXPECT_TRUE(host_impl_->use_msaa());
8446 LayerTreeSettings settings = DefaultSettings();
8447 settings.gpu_rasterization_enabled = false;
8448 EXPECT_TRUE(CreateHostImpl(settings, FakeOutputSurface::Create3d()));
8449 host_impl_->SetHasGpuRasterizationTrigger(true);
8450 host_impl_->SetContentIsSuitableForGpuRasterization(true);
8451 EXPECT_EQ(GpuRasterizationStatus::OFF_DEVICE,
8452 host_impl_->gpu_rasterization_status());
8453 EXPECT_FALSE(host_impl_->use_gpu_rasterization());
8455 settings.gpu_rasterization_forced = true;
8456 EXPECT_TRUE(CreateHostImpl(settings, FakeOutputSurface::Create3d()));
8458 host_impl_->SetHasGpuRasterizationTrigger(false);
8459 host_impl_->SetContentIsSuitableForGpuRasterization(false);
8460 EXPECT_EQ(GpuRasterizationStatus::ON_FORCED,
8461 host_impl_->gpu_rasterization_status());
8462 EXPECT_TRUE(host_impl_->use_gpu_rasterization());
8465 // A mock output surface which lets us detect calls to ForceReclaimResources.
8466 class MockReclaimResourcesOutputSurface : public FakeOutputSurface {
8467 public:
8468 static scoped_ptr<MockReclaimResourcesOutputSurface> Create3d() {
8469 return make_scoped_ptr(new MockReclaimResourcesOutputSurface(
8470 TestContextProvider::Create(), TestContextProvider::Create(), false));
8473 MOCK_METHOD0(ForceReclaimResources, void());
8475 protected:
8476 MockReclaimResourcesOutputSurface(
8477 scoped_refptr<ContextProvider> context_provider,
8478 scoped_refptr<ContextProvider> worker_context_provider,
8479 bool delegated_rendering)
8480 : FakeOutputSurface(context_provider,
8481 worker_context_provider,
8482 delegated_rendering) {}
8485 // Display::Draw (and the planned Display Scheduler) currently rely on resources
8486 // being reclaimed to block drawing between BeginCommit / Swap. This test
8487 // ensures that BeginCommit triggers ForceReclaimResources. See
8488 // crbug.com/489515.
8489 TEST_F(LayerTreeHostImplTest, BeginCommitReclaimsResources) {
8490 scoped_ptr<MockReclaimResourcesOutputSurface> output_surface(
8491 MockReclaimResourcesOutputSurface::Create3d());
8492 // Hold an unowned pointer to the output surface to use for mock expectations.
8493 MockReclaimResourcesOutputSurface* mock_output_surface = output_surface.get();
8495 CreateHostImpl(DefaultSettings(), output_surface.Pass());
8496 EXPECT_CALL(*mock_output_surface, ForceReclaimResources()).Times(1);
8497 host_impl_->BeginCommit();
8500 } // namespace
8501 } // namespace cc