Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright (C) 2015 Google Inc. All rights reserved. | |
| 3 * | |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions are | |
| 6 * met: | |
| 7 * | |
| 8 * * Redistributions of source code must retain the above copyright | |
| 9 * notice, this list of conditions and the following disclaimer. | |
| 10 * * Redistributions in binary form must reproduce the above | |
| 11 * copyright notice, this list of conditions and the following disclaimer | |
| 12 * in the documentation and/or other materials provided with the | |
| 13 * distribution. | |
| 14 * * Neither the name of Google Inc. nor the names of its | |
| 15 * contributors may be used to endorse or promote products derived from | |
| 16 * this software without specific prior written permission. | |
| 17 * | |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 29 */ | |
| 30 #include "config.h" | |
| 31 | |
| 32 #include "core/frame/LocalFrame.h" | |
| 33 #include "core/rendering/RenderView.h" | |
| 34 #include "core/testing/URLTestHelpers.h" | |
| 35 #include "public/platform/Platform.h" | |
| 36 #include "public/platform/WebUnitTestSupport.h" | |
| 37 #include "public/web/WebSettings.h" | |
| 38 #include "web/WebLocalFrameImpl.h" | |
| 39 #include "web/tests/FrameTestHelpers.h" | |
| 40 | |
| 41 #include <gmock/gmock.h> | |
| 42 #include <gtest/gtest.h> | |
| 43 | |
| 44 using namespace blink; | |
| 45 | |
| 46 namespace { | |
| 47 | |
| 48 // These tests cover top controls scrolling on main-thread. | |
| 49 // The animation for completing a partial show/hide is done in compositor so | |
| 50 // it is not covered here. | |
| 51 class TopControlsTest : public testing::Test { | |
| 52 public: | |
| 53 TopControlsTest() | |
| 54 : m_baseURL("http://www.test.com/") | |
| 55 { | |
| 56 registerMockedHttpURLLoad("large-div.html"); | |
| 57 } | |
| 58 | |
| 59 ~TopControlsTest() | |
| 60 { | |
| 61 Platform::current()->unitTestSupport()->unregisterAllMockedURLs(); | |
| 62 } | |
| 63 | |
| 64 WebViewImpl* initialize() | |
|
aelias_OOO_until_Jul13
2015/02/13 02:03:17
You don't have most of the typical Android-specifi
bokan
2015/02/13 12:35:25
The bot-only Mac failures are indeed the result of
majidvp
2015/02/18 21:03:57
Added missing android configuration settings. Addi
| |
| 65 { | |
| 66 // Load a page with large body and set viewport size to 400x400 to ensur e | |
| 67 // main frame is scrollable. | |
| 68 m_helper.initializeAndLoad(m_baseURL + "large-div.html"); | |
| 69 webViewImpl()->resize(IntSize(400, 400)); | |
| 70 return webViewImpl(); | |
| 71 } | |
| 72 | |
| 73 void registerMockedHttpURLLoad(const std::string& fileName) | |
| 74 { | |
| 75 URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseU RL.c_str()), WebString::fromUTF8(fileName.c_str())); | |
| 76 } | |
| 77 | |
| 78 WebGestureEvent generateEvent(WebInputEvent::Type type, int deltaX = 0, int deltaY = 0) | |
| 79 { | |
| 80 WebGestureEvent event; | |
| 81 event.type = type; | |
| 82 event.x = 100; | |
| 83 event.y = 100; | |
| 84 if (type == WebInputEvent::GestureScrollUpdate) { | |
| 85 event.data.scrollUpdate.deltaX = deltaX; | |
| 86 event.data.scrollUpdate.deltaY = deltaY; | |
| 87 } | |
| 88 return event; | |
| 89 } | |
| 90 | |
| 91 void verticalScroll(float deltaY) | |
| 92 { | |
| 93 webViewImpl()->handleInputEvent(generateEvent(WebInputEvent::GestureScro llBegin)); | |
| 94 webViewImpl()->handleInputEvent(generateEvent(WebInputEvent::GestureScro llUpdate, 0, deltaY)); | |
| 95 webViewImpl()->handleInputEvent(generateEvent(WebInputEvent::GestureScro llEnd)); | |
| 96 } | |
| 97 | |
| 98 WebViewImpl* webViewImpl() const { return m_helper.webViewImpl(); } | |
| 99 LocalFrame* frame() const { return m_helper.webViewImpl()->mainFrameImpl()-> frame(); } | |
| 100 | |
| 101 private: | |
| 102 std::string m_baseURL; | |
| 103 FrameTestHelpers::WebViewHelper m_helper; | |
| 104 }; | |
| 105 | |
| 106 #define EXPECT_POINT_EQ(expected, actual) \ | |
| 107 do { \ | |
| 108 EXPECT_DOUBLE_EQ((expected).x(), (actual).x()); \ | |
| 109 EXPECT_DOUBLE_EQ((expected).y(), (actual).y()); \ | |
| 110 } while (false) | |
| 111 | |
| 112 // Disable these tests on Mac OSX until further investigation. | |
| 113 // Local build on Mac is OK but the bot fails. This is not an issue as | |
| 114 // Top Controls are currently only used on Android. | |
| 115 #if !OS(MACOSX) | |
| 116 | |
| 117 // Scrolling down should hide top controls. | |
| 118 TEST_F(TopControlsTest, HideOnScrollDown) | |
| 119 { | |
| 120 WebViewImpl* webView = initialize(); | |
| 121 // initialize top controls to be shown. | |
| 122 webView->setTopControlsHeight(50.f, true); | |
| 123 webView->topControls()->setShownRatio(1); | |
| 124 | |
| 125 webView->handleInputEvent(generateEvent(WebInputEvent::GestureScrollBegin)); | |
| 126 EXPECT_FLOAT_EQ(50.f, webView->topControls()->contentOffset()); | |
| 127 | |
| 128 // Top controls should be scrolled partially and page should not scroll. | |
| 129 webView->handleInputEvent(generateEvent(WebInputEvent::GestureScrollUpdate, 0, -20.f)); | |
| 130 EXPECT_FLOAT_EQ(30.f, webView->topControls()->contentOffset()); | |
| 131 EXPECT_POINT_EQ(IntPoint(0, 0), frame()->view()->scrollPosition()); | |
| 132 | |
| 133 // Top controls should consume 30px and become hidden. Excess scroll should be consumed by the page. | |
| 134 webView->handleInputEvent(generateEvent(WebInputEvent::GestureScrollUpdate, 0, -40.f)); | |
| 135 EXPECT_FLOAT_EQ(0.f, webView->topControls()->contentOffset()); | |
| 136 EXPECT_POINT_EQ(IntPoint(0, 10), frame()->view()->scrollPosition()); | |
| 137 | |
| 138 // Only page should consume scroll | |
| 139 webView->handleInputEvent(generateEvent(WebInputEvent::GestureScrollUpdate, 0, -20.f)); | |
| 140 EXPECT_FLOAT_EQ(0.f, webView->topControls()->contentOffset()); | |
| 141 EXPECT_POINT_EQ(IntPoint(0, 30), frame()->view()->scrollPosition()); | |
| 142 } | |
| 143 | |
| 144 // Scrolling up should show top controls. | |
| 145 TEST_F(TopControlsTest, ShowOnScrollUp) | |
| 146 { | |
| 147 WebViewImpl* webView = initialize(); | |
| 148 // initialize top controls to be hidden. | |
| 149 webView->setTopControlsHeight(50.f, false); | |
| 150 webView->topControls()->setShownRatio(0); | |
| 151 | |
| 152 webView->handleInputEvent(generateEvent(WebInputEvent::GestureScrollBegin)); | |
| 153 EXPECT_FLOAT_EQ(0.f, webView->topControls()->contentOffset()); | |
| 154 | |
| 155 webView->handleInputEvent(generateEvent(WebInputEvent::GestureScrollUpdate, 0, 10.f)); | |
| 156 EXPECT_FLOAT_EQ(10.f, webView->topControls()->contentOffset()); | |
| 157 EXPECT_POINT_EQ(IntPoint(0, 0), frame()->view()->scrollPosition()); | |
| 158 | |
| 159 webView->handleInputEvent(generateEvent(WebInputEvent::GestureScrollUpdate, 0, 50.f)); | |
| 160 EXPECT_FLOAT_EQ(50.f, webView->topControls()->contentOffset()); | |
| 161 EXPECT_POINT_EQ(IntPoint(0, 0), frame()->view()->scrollPosition()); | |
| 162 } | |
| 163 | |
| 164 // Scrolling up after previous scroll downs should cause top controls to be | |
| 165 // shown only after all previously scrolled down amount is compensated. | |
| 166 TEST_F(TopControlsTest, ScrollDownThenUp) | |
| 167 { | |
| 168 WebViewImpl* webView = initialize(); | |
| 169 // initialize top controls to be shown and position page at 100px. | |
| 170 webView->setTopControlsHeight(50.f, true); | |
| 171 webView->topControls()->setShownRatio(1); | |
| 172 frame()->view()->setScrollPosition(IntPoint(0, 100)); | |
| 173 | |
| 174 webView->handleInputEvent(generateEvent(WebInputEvent::GestureScrollBegin)); | |
| 175 EXPECT_FLOAT_EQ(50.f, webView->topControls()->contentOffset()); | |
| 176 | |
| 177 // Scroll down to completely hide top controls. Excess deltaY (100px) should be consumed by the page. | |
| 178 webView->handleInputEvent(generateEvent(WebInputEvent::GestureScrollUpdate, 0, -150.f)); | |
| 179 EXPECT_FLOAT_EQ(0.f, webView->topControls()->contentOffset()); | |
| 180 EXPECT_POINT_EQ(IntPoint(0, 200), frame()->view()->scrollPosition()); | |
| 181 | |
| 182 // Scroll up and ensure the top controls does not move until we recover 100p x previously scrolled. | |
| 183 webView->handleInputEvent(generateEvent(WebInputEvent::GestureScrollUpdate, 0, 40.f)); | |
| 184 EXPECT_FLOAT_EQ(0.f, webView->topControls()->contentOffset()); | |
| 185 EXPECT_POINT_EQ(IntPoint(0, 160), frame()->view()->scrollPosition()); | |
| 186 | |
| 187 webView->handleInputEvent(generateEvent(WebInputEvent::GestureScrollUpdate, 0, 60.f)); | |
| 188 EXPECT_FLOAT_EQ(0.f, webView->topControls()->contentOffset()); | |
| 189 EXPECT_POINT_EQ(IntPoint(0, 100), frame()->view()->scrollPosition()); | |
| 190 | |
| 191 // Now we have hit the threshold so further scroll up should be consumed by top controls. | |
| 192 webView->handleInputEvent(generateEvent(WebInputEvent::GestureScrollUpdate, 0, 30.f)); | |
| 193 EXPECT_FLOAT_EQ(30.f, webView->topControls()->contentOffset()); | |
| 194 EXPECT_POINT_EQ(IntPoint(0, 100), frame()->view()->scrollPosition()); | |
| 195 | |
| 196 // Once top control is fully shown then page should consume any excess scrol l. | |
| 197 webView->handleInputEvent(generateEvent(WebInputEvent::GestureScrollUpdate, 0, 70.f)); | |
| 198 EXPECT_FLOAT_EQ(50.f, webView->topControls()->contentOffset()); | |
| 199 EXPECT_POINT_EQ(IntPoint(0, 50), frame()->view()->scrollPosition()); | |
| 200 } | |
| 201 | |
| 202 // Scrolling down should always cause visible top controls to start hiding even | |
| 203 // if we have been scrolling up previously. | |
| 204 TEST_F(TopControlsTest, ScrollUpThenDown) | |
| 205 { | |
| 206 WebViewImpl* webView = initialize(); | |
| 207 // initialize top controls to be hidden and position page at 100px. | |
| 208 webView->setTopControlsHeight(50.f, false); | |
| 209 webView->topControls()->setShownRatio(0); | |
| 210 frame()->view()->setScrollPosition(IntPoint(0, 100)); | |
| 211 | |
| 212 webView->handleInputEvent(generateEvent(WebInputEvent::GestureScrollBegin)); | |
| 213 EXPECT_FLOAT_EQ(0.f, webView->topControls()->contentOffset()); | |
| 214 | |
| 215 // Scroll up to completely show top controls. Excess deltaY (50px) should be consumed by the page. | |
| 216 webView->handleInputEvent(generateEvent(WebInputEvent::GestureScrollUpdate, 0, 100.f)); | |
| 217 EXPECT_FLOAT_EQ(50.f, webView->topControls()->contentOffset()); | |
| 218 EXPECT_POINT_EQ(IntPoint(0, 50), frame()->view()->scrollPosition()); | |
| 219 | |
| 220 // Scroll down and ensure only top controls is scrolled | |
| 221 webView->handleInputEvent(generateEvent(WebInputEvent::GestureScrollUpdate, 0, -40.f)); | |
| 222 EXPECT_FLOAT_EQ(10.f, webView->topControls()->contentOffset()); | |
| 223 EXPECT_POINT_EQ(IntPoint(0, 50), frame()->view()->scrollPosition()); | |
| 224 | |
| 225 webView->handleInputEvent(generateEvent(WebInputEvent::GestureScrollUpdate, 0, -60.f)); | |
| 226 EXPECT_FLOAT_EQ(0.f, webView->topControls()->contentOffset()); | |
| 227 EXPECT_POINT_EQ(IntPoint(0, 100), frame()->view()->scrollPosition()); | |
| 228 } | |
| 229 | |
| 230 // Top controls should ignore scroll events when pinch is in progress. | |
| 231 TEST_F(TopControlsTest, IgnoreScrollDuringPinch) | |
| 232 { | |
| 233 WebViewImpl* webView = initialize(); | |
| 234 // initialize top controls to be shown. | |
| 235 webView->setTopControlsHeight(50.f, false); | |
| 236 webView->topControls()->setShownRatio(1); | |
| 237 | |
| 238 webView->handleInputEvent(generateEvent(WebInputEvent::GestureScrollBegin)); | |
| 239 webView->handleInputEvent(generateEvent(WebInputEvent::GestureScrollUpdate, 0, -30.f)); | |
| 240 EXPECT_FLOAT_EQ(20.f, webView->topControls()->contentOffset()); | |
| 241 | |
| 242 // Ignore scroll update during a pinch. | |
| 243 webView->handleInputEvent(generateEvent(WebInputEvent::GesturePinchBegin)); | |
| 244 webView->handleInputEvent(generateEvent(WebInputEvent::GestureScrollUpdate, 0, -10.f)); | |
| 245 EXPECT_FLOAT_EQ(20.f, webView->topControls()->contentOffset()); | |
| 246 webView->handleInputEvent(generateEvent(WebInputEvent::GesturePinchEnd)); | |
| 247 EXPECT_FLOAT_EQ(20.f, webView->topControls()->contentOffset()); | |
| 248 | |
| 249 webView->handleInputEvent(generateEvent(WebInputEvent::GestureScrollUpdate, 0, -20.f)); | |
| 250 EXPECT_FLOAT_EQ(0.f, webView->topControls()->contentOffset()); | |
| 251 } | |
| 252 | |
| 253 // Top controls should not consume horizontal scroll. | |
| 254 TEST_F(TopControlsTest, HorizontalScroll) | |
| 255 { | |
| 256 WebViewImpl* webView = initialize(); | |
| 257 // initialize top controls to be shown. | |
| 258 webView->setTopControlsHeight(50.f, false); | |
| 259 webView->topControls()->setShownRatio(1); | |
| 260 | |
| 261 webView->handleInputEvent(generateEvent(WebInputEvent::GestureScrollBegin)); | |
| 262 EXPECT_FLOAT_EQ(50.f, webView->topControls()->contentOffset()); | |
| 263 | |
| 264 // Top controls should not consume horizontal scroll. | |
| 265 webView->handleInputEvent(generateEvent(WebInputEvent::GestureScrollUpdate, -110.f, -100.f)); | |
| 266 EXPECT_FLOAT_EQ(0.f, webView->topControls()->contentOffset()); | |
| 267 EXPECT_POINT_EQ(IntPoint(110, 50), frame()->view()->scrollPosition()); | |
| 268 | |
| 269 webView->handleInputEvent(generateEvent(WebInputEvent::GestureScrollUpdate, -40.f, 0)); | |
| 270 EXPECT_FLOAT_EQ(0.f, webView->topControls()->contentOffset()); | |
| 271 EXPECT_POINT_EQ(IntPoint(150, 50), frame()->view()->scrollPosition()); | |
| 272 } | |
| 273 | |
| 274 // Top controls visibility should remain consistent when height is changed. | |
| 275 TEST_F(TopControlsTest, HeightChangeMaintainsVisibility) | |
| 276 { | |
| 277 WebViewImpl* webView = initialize(); | |
| 278 webView->setTopControlsHeight(20.f, false); | |
| 279 webView->topControls()->setShownRatio(0); | |
| 280 | |
| 281 webView->setTopControlsHeight(20.f, false); | |
| 282 EXPECT_FLOAT_EQ(0.f, webView->topControls()->contentOffset()); | |
| 283 | |
| 284 webView->setTopControlsHeight(40.f, false); | |
| 285 EXPECT_FLOAT_EQ(0.f, webView->topControls()->contentOffset()); | |
| 286 | |
| 287 // Scroll up to show top controls. | |
| 288 verticalScroll(40.f); | |
| 289 EXPECT_FLOAT_EQ(40.f, webView->topControls()->contentOffset()); | |
| 290 | |
| 291 // Changing a height of a fully shown top controls should correctly adjust c ontent offset | |
| 292 webView->setTopControlsHeight(30.f, false); | |
| 293 EXPECT_FLOAT_EQ(30.f, webView->topControls()->contentOffset()); | |
| 294 } | |
| 295 | |
| 296 // Top controls visibility should remain consistent when height is changed. | |
| 297 TEST_F(TopControlsTest, ZeroHeightMeansNoEffect) | |
| 298 { | |
| 299 WebViewImpl* webView = initialize(); | |
| 300 webView->setTopControlsHeight(0, false); | |
| 301 webView->topControls()->setShownRatio(0); | |
| 302 frame()->view()->setScrollPosition(IntPoint(0, 100)); | |
| 303 | |
| 304 | |
| 305 EXPECT_FLOAT_EQ(0.f, webView->topControls()->contentOffset()); | |
| 306 | |
| 307 verticalScroll(20.f); | |
| 308 EXPECT_FLOAT_EQ(0.f, webView->topControls()->contentOffset()); | |
| 309 EXPECT_POINT_EQ(IntPoint(0, 80), frame()->view()->scrollPosition()); | |
| 310 | |
| 311 verticalScroll(-30.f); | |
| 312 EXPECT_FLOAT_EQ(0.f, webView->topControls()->contentOffset()); | |
| 313 EXPECT_POINT_EQ(IntPoint(0, 110), frame()->view()->scrollPosition()); | |
| 314 | |
| 315 webView->topControls()->setShownRatio(1); | |
| 316 EXPECT_FLOAT_EQ(0.f, webView->topControls()->contentOffset()); | |
| 317 } | |
| 318 | |
| 319 // Top controls should honor its constraints | |
|
aelias_OOO_until_Jul13
2015/02/13 02:03:17
Please also add a test case that top controls are
majidvp
2015/02/18 21:03:57
Done.
| |
| 320 TEST_F(TopControlsTest, StateConstraints) | |
| 321 { | |
| 322 WebViewImpl* webView = initialize(); | |
| 323 webView->setTopControlsHeight(50.f, false); | |
| 324 frame()->view()->setScrollPosition(IntPoint(0, 100)); | |
| 325 | |
| 326 // Only shown state is permitted so controls cannot hide | |
| 327 webView->topControls()->setShownRatio(1); | |
| 328 webView->updateTopControlsState(WebTopControlsShown, WebTopControlsShown, fa lse); | |
| 329 verticalScroll(-20.f); | |
| 330 EXPECT_FLOAT_EQ(50, webView->topControls()->contentOffset()); | |
| 331 EXPECT_POINT_EQ(IntPoint(0, 120), frame()->view()->scrollPosition()); | |
| 332 | |
| 333 // Only hidden state is permitted so controls cannot show | |
| 334 webView->topControls()->setShownRatio(0); | |
| 335 webView->updateTopControlsState(WebTopControlsHidden, WebTopControlsHidden, false); | |
| 336 verticalScroll(30.f); | |
| 337 EXPECT_FLOAT_EQ(0, webView->topControls()->contentOffset()); | |
| 338 EXPECT_POINT_EQ(IntPoint(0, 90), frame()->view()->scrollPosition()); | |
| 339 | |
| 340 // Both state is permitted so controls can show or hide | |
| 341 webView->topControls()->setShownRatio(0); | |
| 342 webView->updateTopControlsState(WebTopControlsBoth, WebTopControlsBoth, fals e); | |
| 343 verticalScroll(50.f); | |
| 344 EXPECT_FLOAT_EQ(50, webView->topControls()->contentOffset()); | |
| 345 EXPECT_POINT_EQ(IntPoint(0, 90), frame()->view()->scrollPosition()); | |
| 346 | |
| 347 verticalScroll(-50.f); | |
| 348 EXPECT_FLOAT_EQ(0, webView->topControls()->contentOffset()); | |
| 349 EXPECT_POINT_EQ(IntPoint(0, 90), frame()->view()->scrollPosition()); | |
| 350 } | |
| 351 | |
| 352 #endif | |
| 353 } // namespace | |
| OLD | NEW |