OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/bind.h" |
| 6 #include "base/command_line.h" |
| 7 #include "base/run_loop.h" |
| 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "cc/base/math_util.h" |
| 10 #include "content/browser/renderer_host/input/synthetic_gesture.h" |
| 11 #include "content/browser/renderer_host/input/synthetic_smooth_scroll_gesture.h" |
| 12 #include "content/browser/renderer_host/render_widget_host_impl.h" |
| 13 #include "content/browser/web_contents/web_contents_impl.h" |
| 14 #include "content/common/input/synthetic_gesture_params.h" |
| 15 #include "content/common/input/synthetic_smooth_scroll_gesture_params.h" |
| 16 #include "content/public/browser/render_view_host.h" |
| 17 #include "content/public/common/content_switches.h" |
| 18 #include "content/public/test/browser_test_utils.h" |
| 19 #include "content/public/test/content_browser_test.h" |
| 20 #include "content/public/test/content_browser_test_utils.h" |
| 21 #include "content/public/test/test_utils.h" |
| 22 #include "content/shell/browser/shell.h" |
| 23 |
| 24 namespace { |
| 25 |
| 26 const char kCompositedScrollingDataURL[] = |
| 27 "data:text/html;charset=utf-8," |
| 28 "<!DOCTYPE html>" |
| 29 "<meta name='viewport' content='width=device-width'/>" |
| 30 "<style>" |
| 31 "#scroller {" |
| 32 " width:500px;" |
| 33 " height:500px;" |
| 34 " overflow:scroll;" |
| 35 " transform: rotateX(-30deg);" |
| 36 "}" |
| 37 |
| 38 "#content {" |
| 39 " background-color:red;" |
| 40 " width:1000px;" |
| 41 " height:1000px;" |
| 42 "}" |
| 43 "</style>" |
| 44 "<div id='scroller'>" |
| 45 " <div id='content'>" |
| 46 " </div>" |
| 47 "</div>" |
| 48 "<script>" |
| 49 " document.title='ready';" |
| 50 "</script>"; |
| 51 |
| 52 } // namespace |
| 53 |
| 54 namespace content { |
| 55 |
| 56 |
| 57 class CompositedScrollingBrowserTest : public ContentBrowserTest { |
| 58 public: |
| 59 CompositedScrollingBrowserTest() {} |
| 60 ~CompositedScrollingBrowserTest() override {} |
| 61 |
| 62 void SetUpCommandLine(base::CommandLine* cmd) override { |
| 63 cmd->AppendSwitch(switches::kEnablePreferCompositingToLCDText); |
| 64 } |
| 65 |
| 66 RenderWidgetHostImpl* GetWidgetHost() { |
| 67 return RenderWidgetHostImpl::From( |
| 68 shell()->web_contents()->GetRenderViewHost()->GetWidget()); |
| 69 } |
| 70 |
| 71 void OnSyntheticGestureCompleted(SyntheticGesture::Result result) { |
| 72 EXPECT_EQ(SyntheticGesture::GESTURE_FINISHED, result); |
| 73 runner_->Quit(); |
| 74 } |
| 75 |
| 76 protected: |
| 77 void LoadURL() { |
| 78 const GURL data_url(kCompositedScrollingDataURL); |
| 79 NavigateToURL(shell(), data_url); |
| 80 |
| 81 RenderWidgetHostImpl* host = GetWidgetHost(); |
| 82 scoped_refptr<FrameWatcher> frame_watcher(new FrameWatcher()); |
| 83 host->GetProcess()->AddFilter(frame_watcher.get()); |
| 84 host->GetView()->SetSize(gfx::Size(400, 400)); |
| 85 |
| 86 base::string16 ready_title(base::ASCIIToUTF16("ready")); |
| 87 TitleWatcher watcher(shell()->web_contents(), ready_title); |
| 88 ignore_result(watcher.WaitAndGetTitle()); |
| 89 |
| 90 // We need to wait until at least one frame has been composited |
| 91 // otherwise the injection of the synthetic gestures may get |
| 92 // dropped because of MainThread/Impl thread sync of touch event |
| 93 // regions. |
| 94 frame_watcher->WaitFrames(1); |
| 95 } |
| 96 |
| 97 // ContentBrowserTest: |
| 98 int ExecuteScriptAndExtractInt(const std::string& script) { |
| 99 int value = 0; |
| 100 EXPECT_TRUE(content::ExecuteScriptAndExtractInt( |
| 101 shell()->web_contents(), |
| 102 "domAutomationController.send(" + script + ")", |
| 103 &value)); |
| 104 return value; |
| 105 } |
| 106 |
| 107 int GetScrollTop() { |
| 108 return ExecuteScriptAndExtractInt( |
| 109 "document.getElementById(\"scroller\").scrollTop"); |
| 110 } |
| 111 |
| 112 // Generate touch events for a synthetic scroll from |point| for |distance|. |
| 113 // Returns the distance scrolled. |
| 114 int DoTouchScroll(const gfx::Point& point, const gfx::Vector2d& distance) { |
| 115 EXPECT_EQ(0, GetScrollTop()); |
| 116 |
| 117 int scrollHeight = ExecuteScriptAndExtractInt( |
| 118 "document.getElementById('scroller').scrollHeight"); |
| 119 EXPECT_EQ(1000, scrollHeight); |
| 120 |
| 121 SyntheticSmoothScrollGestureParams params; |
| 122 params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; |
| 123 params.anchor = gfx::PointF(point); |
| 124 params.distances.push_back(-distance); |
| 125 |
| 126 runner_ = new MessageLoopRunner(); |
| 127 |
| 128 scoped_ptr<SyntheticSmoothScrollGesture> gesture( |
| 129 new SyntheticSmoothScrollGesture(params)); |
| 130 GetWidgetHost()->QueueSyntheticGesture( |
| 131 gesture.Pass(), |
| 132 base::Bind(&CompositedScrollingBrowserTest::OnSyntheticGestureCompleted, |
| 133 base::Unretained(this))); |
| 134 |
| 135 // Runs until we get the OnSyntheticGestureCompleted callback |
| 136 runner_->Run(); |
| 137 runner_ = NULL; |
| 138 |
| 139 return GetScrollTop(); |
| 140 } |
| 141 |
| 142 private: |
| 143 scoped_refptr<MessageLoopRunner> runner_; |
| 144 |
| 145 DISALLOW_COPY_AND_ASSIGN(CompositedScrollingBrowserTest); |
| 146 }; |
| 147 |
| 148 // Verify transforming a scroller doesn't prevent it from scrolling. See |
| 149 // crbug.com/543655 for a case where this was broken. |
| 150 // Disabled on MacOS because it doesn't support touch input. |
| 151 // Disabled on Android due to flakiness, see https://crbug.com/376668. |
| 152 #if defined(OS_MACOSX) || defined(OS_ANDROID) |
| 153 #define MAYBE_Scroll3DTransformedScroller DISABLED_Scroll3DTransformedScroller |
| 154 #else |
| 155 #define MAYBE_Scroll3DTransformedScroller Scroll3DTransformedScroller |
| 156 #endif |
| 157 IN_PROC_BROWSER_TEST_F(CompositedScrollingBrowserTest, |
| 158 MAYBE_Scroll3DTransformedScroller) { |
| 159 LoadURL(); |
| 160 int scrollDistance = |
| 161 DoTouchScroll(gfx::Point(50, 150), gfx::Vector2d(0, 100)); |
| 162 // The scroll distance is increased due to the rotation of the scroller. |
| 163 EXPECT_EQ(std::floor(100 / std::cos(cc::MathUtil::Deg2Rad(30.f))) - 1, |
| 164 scrollDistance); |
| 165 } |
| 166 |
| 167 } // namespace content |
OLD | NEW |