3 #include "core/frame/FrameView.h"
4 #include "core/rendering/RenderView.h"
5 #include "public/platform/Platform.h"
6 #include "public/platform/WebUnitTestSupport.h"
7 #include "public/web/WebFrame.h"
8 #include "public/web/WebFrameClient.h"
9 #include "public/web/WebHistoryItem.h"
10 #include "public/web/WebInputEvent.h"
11 #include "public/web/WebScriptSource.h"
12 #include "public/web/WebSettings.h"
13 #include "public/web/WebView.h"
14 #include "web/WebLocalFrameImpl.h"
15 #include "web/WebViewImpl.h"
16 #include "web/tests/FrameTestHelpers.h"
17 #include "web/tests/URLTestHelpers.h"
18 #include <gtest/gtest.h>
20 using namespace WebCore;
21 using namespace blink;
25 class MockWebFrameClient : public WebFrameClient {
28 class ProgrammaticScrollTest : public testing::Test {
30 ProgrammaticScrollTest()
31 : m_baseURL("http://www.test.com/")
35 virtual void TearDown()
37 Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
42 void registerMockedHttpURLLoad(const std::string& fileName)
44 URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseURL.c_str()), WebString::fromUTF8(fileName.c_str()));
47 std::string m_baseURL;
48 MockWebFrameClient m_mockWebFrameClient;
51 TEST_F(ProgrammaticScrollTest, RestoreScrollPositionAndViewStateWithScale)
53 registerMockedHttpURLLoad("long_scroll.html");
55 FrameTestHelpers::WebViewHelper webViewHelper;
56 WebView* webView = webViewHelper.initializeAndLoad(m_baseURL + "long_scroll.html", true, 0, 0);
57 webView->resize(WebSize(1000, 1000));
60 WebViewImpl* webViewImpl = toWebViewImpl(webView);
61 LocalFrame* frame = webViewImpl->mainFrameImpl()->frame();
62 frame->loader().setLoadType(FrameLoadTypeBackForward);
64 webViewImpl->setPageScaleFactor(3.0f);
65 webViewImpl->setMainFrameScrollOffset(WebPoint(0, 500));
66 frame->view()->setWasScrolledByUser(false);
67 frame->loader().currentItem()->setPageScaleFactor(2);
68 frame->loader().currentItem()->setScrollPoint(WebPoint(0, 200));
70 // Flip back the wasScrolledByUser flag which was set to true by setPageScaleFactor
71 // because otherwise FrameLoader::restoreScrollPositionAndViewState does nothing.
72 frame->view()->setWasScrolledByUser(false);
73 frame->loader().restoreScrollPositionAndViewState();
75 // Expect that both scroll and scale were restored, and that it was not a programmatic scroll.
76 EXPECT_EQ(2.0f, webViewImpl->pageScaleFactor());
77 EXPECT_EQ(200, webViewImpl->mainFrameImpl()->scrollOffset().height);
78 EXPECT_TRUE(frame->view()->wasScrolledByUser());
81 TEST_F(ProgrammaticScrollTest, RestoreScrollPositionAndViewStateWithoutScale)
83 registerMockedHttpURLLoad("long_scroll.html");
85 FrameTestHelpers::WebViewHelper webViewHelper;
86 WebView* webView = webViewHelper.initializeAndLoad(m_baseURL + "long_scroll.html", true, 0, 0);
87 webView->resize(WebSize(1000, 1000));
90 WebViewImpl* webViewImpl = toWebViewImpl(webView);
91 LocalFrame* frame = webViewImpl->mainFrameImpl()->frame();
92 frame->loader().setLoadType(FrameLoadTypeBackForward);
94 webViewImpl->setPageScaleFactor(3.0f);
95 webViewImpl->setMainFrameScrollOffset(WebPoint(0, 500));
96 frame->view()->setWasScrolledByUser(false);
97 frame->loader().currentItem()->setPageScaleFactor(0);
98 frame->loader().currentItem()->setScrollPoint(WebPoint(0, 400));
100 // FrameLoader::restoreScrollPositionAndViewState flows differently if scale is zero.
101 frame->loader().restoreScrollPositionAndViewState();
103 // Expect that only the scroll position was restored, and that it was not a programmatic scroll.
104 EXPECT_EQ(3.0f, webViewImpl->pageScaleFactor());
105 EXPECT_EQ(400, webViewImpl->mainFrameImpl()->scrollOffset().height);
106 EXPECT_TRUE(frame->view()->wasScrolledByUser());