Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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 "public/web/WebScriptSource.h" | |
| 6 #include "testing/gtest/include/gtest/gtest.h" | |
| 7 #include "web/WebLocalFrameImpl.h" | |
| 8 #include "web/tests/sim/SimCompositor.h" | |
| 9 #include "web/tests/sim/SimDisplayItemList.h" | |
| 10 #include "web/tests/sim/SimRequest.h" | |
| 11 #include "web/tests/sim/SimTest.h" | |
| 12 | |
| 13 namespace blink { | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 class SmoothScrollTest : public SimTest {}; | |
| 18 | |
| 19 TEST_F(SmoothScrollTest, InstantScroll) { | |
| 20 v8::HandleScope HandleScope(v8::Isolate::GetCurrent()); | |
| 21 WebView().Resize(WebSize(800, 600)); | |
| 22 SimRequest request("https://example.com/test.html", "text/html"); | |
| 23 LoadURL("https://example.com/test.html"); | |
| 24 request.Complete( | |
| 25 "<div id='space' style='height: 1000px'></div>" | |
| 26 "<div id='content' style='height: 1000px'></div>"); | |
| 27 | |
| 28 Compositor().BeginFrame(); | |
| 29 ASSERT_EQ(Window().scrollY(), 0); | |
| 30 MainFrame().ExecuteScriptAndReturnValue( | |
| 31 WebScriptSource("document.getElementById('content').scrollIntoView();")); | |
| 32 | |
| 33 Compositor().BeginFrame(); | |
| 34 Element* content = GetDocument().getElementById("content"); | |
| 35 ASSERT_EQ(Window().scrollY(), content->OffsetTop()); | |
| 36 } | |
| 37 | |
| 38 TEST_F(SmoothScrollTest, SmoothScroll) { | |
| 39 v8::HandleScope HandleScope(v8::Isolate::GetCurrent()); | |
| 40 WebView().Resize(WebSize(800, 600)); | |
| 41 SimRequest request("https://example.com/test.html", "text/html"); | |
| 42 LoadURL("https://example.com/test.html"); | |
| 43 request.Complete( | |
| 44 "<div id='space' style='height: 1000px'></div>" | |
| 45 "<div id='content' style='height: 1000px'></div>"); | |
| 46 | |
| 47 Compositor().BeginFrame(); | |
| 48 ASSERT_EQ(Window().scrollY(), 0); | |
| 49 | |
| 50 MainFrame().ExecuteScriptAndReturnValue( | |
| 51 WebScriptSource("document.getElementById('content').scrollIntoView(" | |
| 52 "{block: 'start', behavior: 'smooth'});")); | |
| 53 | |
| 54 // Scrolling the container | |
| 55 Compositor().BeginFrame(); // update run_state_. | |
| 56 Compositor().BeginFrame(); // Set start_time = now. | |
| 57 Compositor().BeginFrame(0.2); | |
| 58 ASSERT_GT(Window().scrollY(), 250); | |
|
bokan
2017/05/15 17:15:28
These tests should be deterministic since we contr
sunyunjia
2017/05/19 16:24:29
Done.
| |
| 59 ASSERT_LT(Window().scrollY(), 350); | |
| 60 | |
| 61 // Finish scrolling the container | |
| 62 Compositor().BeginFrame(1); | |
| 63 Element* content = GetDocument().getElementById("content"); | |
| 64 ASSERT_EQ(Window().scrollY(), content->OffsetTop()); | |
| 65 } | |
| 66 | |
| 67 TEST_F(SmoothScrollTest, NestedContainer) { | |
| 68 v8::HandleScope HandleScope(v8::Isolate::GetCurrent()); | |
| 69 WebView().Resize(WebSize(800, 600)); | |
| 70 SimRequest request("https://example.com/test.html", "text/html"); | |
| 71 LoadURL("https://example.com/test.html"); | |
| 72 request.Complete( | |
| 73 "<div id='space' style='height: 1000px'></div>" | |
| 74 "<div id='container' style='height: 600px; overflow: scroll'>" | |
| 75 " <div id='space1' style='height: 1000px'></div>" | |
| 76 " <div id='content' style='height: 1000px'></div>" | |
| 77 "</div>"); | |
| 78 | |
| 79 Compositor().BeginFrame(); | |
| 80 Element* container = GetDocument().getElementById("container"); | |
| 81 | |
| 82 ASSERT_EQ(Window().scrollY(), 0); | |
| 83 ASSERT_EQ(container->scrollTop(), 0); | |
| 84 | |
| 85 MainFrame().ExecuteScriptAndReturnValue( | |
|
bokan
2017/05/15 17:15:28
Elliott's suggestion was to avoid calling things v
sunyunjia
2017/05/19 16:24:29
Done.
| |
| 86 WebScriptSource("document.getElementById('content').scrollIntoView(" | |
| 87 "{block: 'start', behavior: 'smooth'});")); | |
| 88 | |
| 89 // Scrolling the outer container | |
| 90 Compositor().BeginFrame(); // update run_state_. | |
| 91 Compositor().BeginFrame(); // Set start_time = now. | |
| 92 Compositor().BeginFrame(0.2); | |
| 93 ASSERT_GT(Window().scrollY(), 250); | |
| 94 ASSERT_LT(Window().scrollY(), 350); | |
| 95 ASSERT_EQ(container->scrollTop(), 0); | |
| 96 | |
| 97 // Finish scrolling the outer container | |
| 98 Compositor().BeginFrame(1); | |
| 99 ASSERT_EQ(Window().scrollY(), container->OffsetTop()); | |
| 100 ASSERT_LE(container->scrollTop(), 20); | |
| 101 | |
| 102 // Scrolling the inner container | |
| 103 Compositor().BeginFrame(); // Set start_time = now. | |
| 104 Compositor().BeginFrame(0.2); | |
| 105 ASSERT_GE(container->scrollTop(), 250); | |
| 106 ASSERT_LE(container->scrollTop(), 350); | |
| 107 | |
| 108 // Finish scrolling the inner container | |
| 109 Compositor().BeginFrame(1); | |
| 110 Element* content = GetDocument().getElementById("content"); | |
| 111 ASSERT_EQ(container->scrollTop(), | |
| 112 content->OffsetTop() - container->OffsetTop()); | |
| 113 } | |
| 114 | |
| 115 TEST_F(SmoothScrollTest, NewScrollIntoViewAbortsCurrentAnimation) { | |
|
bokan
2017/05/15 17:15:28
The test I'm most interested in is one that has so
sunyunjia
2017/05/19 16:24:29
Done.
| |
| 116 v8::HandleScope HandleScope(v8::Isolate::GetCurrent()); | |
| 117 WebView().Resize(WebSize(800, 600)); | |
| 118 SimRequest request("https://example.com/test.html", "text/html"); | |
| 119 LoadURL("https://example.com/test.html"); | |
| 120 request.Complete( | |
| 121 "<div id='content1' style='height: 1000px'></div>" | |
| 122 "<div id='content2' style='height: 1000px'></div>"); | |
| 123 | |
| 124 Compositor().BeginFrame(); | |
| 125 ASSERT_EQ(Window().scrollY(), 0); | |
| 126 MainFrame().ExecuteScriptAndReturnValue( | |
| 127 WebScriptSource("document.getElementById('content2').scrollIntoView(" | |
| 128 "{block: 'start', behavior: 'smooth'});")); | |
| 129 | |
| 130 Compositor().BeginFrame(); // update run_state_. | |
| 131 Compositor().BeginFrame(); // Set start_time = now. | |
| 132 Compositor().BeginFrame(0.2); | |
| 133 ASSERT_GT(Window().scrollY(), 250); | |
| 134 ASSERT_LT(Window().scrollY(), 350); | |
| 135 | |
| 136 MainFrame().ExecuteScriptAndReturnValue( | |
| 137 WebScriptSource("document.getElementById('content1').scrollIntoView();")); | |
| 138 Compositor().BeginFrame(); | |
| 139 Element* content1 = GetDocument().getElementById("content1"); | |
| 140 ASSERT_EQ(Window().scrollY(), content1->OffsetTop()); | |
| 141 } | |
| 142 | |
| 143 TEST_F(SmoothScrollTest, BlockAndInlineSettings) { | |
| 144 v8::HandleScope HandleScope(v8::Isolate::GetCurrent()); | |
| 145 WebView().Resize(WebSize(800, 600)); | |
| 146 SimRequest request("https://example.com/test.html", "text/html"); | |
| 147 LoadURL("https://example.com/test.html"); | |
| 148 request.Complete( | |
| 149 "<div id='container' style='height: 2500px; width: 2500px;'>" | |
| 150 "<div id='content' style='height: 500px; width: 500px;" | |
| 151 "margin-left: 1000px; margin-right: 1000px; margin-top: 1000px;" | |
| 152 "margin-bottom: 1000px'></div></div>"); | |
| 153 | |
| 154 int content_height = 500; | |
| 155 int content_width = 500; | |
| 156 int window_height = 600; | |
| 157 int window_width = 800; | |
| 158 | |
| 159 Compositor().BeginFrame(); | |
| 160 Element* content = GetDocument().getElementById("content"); | |
| 161 ASSERT_EQ(Window().scrollY(), 0); | |
| 162 MainFrame().ExecuteScriptAndReturnValue( | |
| 163 WebScriptSource("document.getElementById('content').scrollIntoView(" | |
| 164 "{block: 'nearest', inlinePosition: 'nearest'});")); | |
| 165 Compositor().BeginFrame(); | |
| 166 ASSERT_EQ(Window().scrollX(), | |
| 167 content->OffsetLeft() + content_width - window_width); | |
| 168 ASSERT_EQ(Window().scrollY(), | |
| 169 content->OffsetTop() + content_height - window_height); | |
| 170 | |
| 171 MainFrame().ExecuteScriptAndReturnValue( | |
| 172 WebScriptSource("document.getElementById('content').scrollIntoView(" | |
| 173 "{block: 'start', inlinePosition: 'start'});")); | |
| 174 Compositor().BeginFrame(); | |
| 175 ASSERT_EQ(Window().scrollX(), content->OffsetLeft()); | |
| 176 ASSERT_EQ(Window().scrollY(), content->OffsetTop()); | |
| 177 | |
| 178 MainFrame().ExecuteScriptAndReturnValue( | |
| 179 WebScriptSource("document.getElementById('content').scrollIntoView(" | |
| 180 "{block: 'center', inlinePosition: 'center'});")); | |
| 181 Compositor().BeginFrame(); | |
| 182 ASSERT_EQ(Window().scrollX(), | |
| 183 content->OffsetLeft() + (content_width - window_width) / 2); | |
| 184 ASSERT_EQ(Window().scrollY(), | |
| 185 content->OffsetTop() + (content_height - window_height) / 2); | |
| 186 | |
| 187 MainFrame().ExecuteScriptAndReturnValue( | |
| 188 WebScriptSource("document.getElementById('content').scrollIntoView(" | |
| 189 "{block: 'end', inlinePosition: 'end'});")); | |
| 190 Compositor().BeginFrame(); | |
| 191 ASSERT_EQ(Window().scrollX(), | |
| 192 content->OffsetLeft() + content_width - window_width); | |
| 193 ASSERT_EQ(Window().scrollY(), | |
| 194 content->OffsetTop() + content_height - window_height); | |
| 195 } | |
| 196 | |
| 197 } // namespace | |
| 198 | |
| 199 } // namespace blink | |
| OLD | NEW |