Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / web / tests / ProgrammaticScrollTest.cpp
blobdaff861f70e1cbd49c383505dfa3e38b00a43a6d
1 #include "config.h"
3 #include "core/frame/FrameView.h"
4 #include "core/layout/LayoutView.h"
5 #include "core/loader/DocumentLoader.h"
6 #include "core/loader/FrameLoader.h"
7 #include "platform/testing/URLTestHelpers.h"
8 #include "public/platform/Platform.h"
9 #include "public/platform/WebUnitTestSupport.h"
10 #include "public/web/WebFrame.h"
11 #include "public/web/WebFrameClient.h"
12 #include "public/web/WebHistoryItem.h"
13 #include "public/web/WebInputEvent.h"
14 #include "public/web/WebScriptSource.h"
15 #include "public/web/WebSettings.h"
16 #include "public/web/WebView.h"
17 #include "web/WebLocalFrameImpl.h"
18 #include "web/WebViewImpl.h"
19 #include "web/tests/FrameTestHelpers.h"
20 #include <gtest/gtest.h>
22 namespace blink {
24 class MockWebFrameClient : public WebFrameClient {
27 class ProgrammaticScrollTest : public testing::Test {
28 public:
29 ProgrammaticScrollTest()
30 : m_baseURL("http://www.test.com/")
34 void TearDown() override
36 Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
39 protected:
41 void registerMockedHttpURLLoad(const std::string& fileName)
43 URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseURL.c_str()), WebString::fromUTF8(fileName.c_str()));
46 std::string m_baseURL;
47 MockWebFrameClient m_mockWebFrameClient;
50 TEST_F(ProgrammaticScrollTest, RestoreScrollPositionAndViewStateWithScale)
52 registerMockedHttpURLLoad("long_scroll.html");
54 FrameTestHelpers::WebViewHelper webViewHelper;
55 WebView* webView = webViewHelper.initializeAndLoad(m_baseURL + "long_scroll.html", true, 0, 0);
56 webView->resize(WebSize(1000, 1000));
57 webView->layout();
59 WebViewImpl* webViewImpl = toWebViewImpl(webView);
60 FrameLoader& loader = webViewImpl->mainFrameImpl()->frame()->loader();
61 loader.setLoadType(FrameLoadTypeBackForward);
63 webViewImpl->setPageScaleFactor(3.0f);
64 webViewImpl->mainFrame()->setScrollOffset(WebSize(0, 500));
65 loader.documentLoader()->initialScrollState().wasScrolledByUser = false;
66 loader.currentItem()->setPageScaleFactor(2);
67 loader.currentItem()->setScrollPoint(WebPoint(0, 200));
69 // Flip back the wasScrolledByUser flag which was set to true by setPageScaleFactor
70 // because otherwise FrameLoader::restoreScrollPositionAndViewState does nothing.
71 loader.documentLoader()->initialScrollState().wasScrolledByUser = false;
72 loader.restoreScrollPositionAndViewState();
74 // Expect that both scroll and scale were restored.
75 EXPECT_EQ(2.0f, webViewImpl->pageScaleFactor());
76 EXPECT_EQ(200, webViewImpl->mainFrameImpl()->scrollOffset().height);
79 TEST_F(ProgrammaticScrollTest, RestoreScrollPositionAndViewStateWithoutScale)
81 registerMockedHttpURLLoad("long_scroll.html");
83 FrameTestHelpers::WebViewHelper webViewHelper;
84 WebView* webView = webViewHelper.initializeAndLoad(m_baseURL + "long_scroll.html", true, 0, 0);
85 webView->resize(WebSize(1000, 1000));
86 webView->layout();
88 WebViewImpl* webViewImpl = toWebViewImpl(webView);
89 FrameLoader& loader = webViewImpl->mainFrameImpl()->frame()->loader();
90 loader.setLoadType(FrameLoadTypeBackForward);
92 webViewImpl->setPageScaleFactor(3.0f);
93 webViewImpl->mainFrame()->setScrollOffset(WebSize(0, 500));
94 loader.documentLoader()->initialScrollState().wasScrolledByUser = false;
95 loader.currentItem()->setPageScaleFactor(0);
96 loader.currentItem()->setScrollPoint(WebPoint(0, 400));
98 // FrameLoader::restoreScrollPositionAndViewState flows differently if scale is zero.
99 loader.restoreScrollPositionAndViewState();
101 // Expect that only the scroll position was restored.
102 EXPECT_EQ(3.0f, webViewImpl->pageScaleFactor());
103 EXPECT_EQ(400, webViewImpl->mainFrameImpl()->scrollOffset().height);
106 } // namespace blink