3 #include "core/frame/FrameView.h"
4 #include "core/rendering/RenderView.h"
5 #include "core/testing/URLTestHelpers.h"
6 #include "public/platform/Platform.h"
7 #include "public/platform/WebUnitTestSupport.h"
8 #include "public/web/WebFrame.h"
9 #include "public/web/WebFrameClient.h"
10 #include "public/web/WebHistoryItem.h"
11 #include "public/web/WebInputEvent.h"
12 #include "public/web/WebScriptSource.h"
13 #include "public/web/WebSettings.h"
14 #include "public/web/WebView.h"
15 #include "web/WebLocalFrameImpl.h"
16 #include "web/WebViewImpl.h"
17 #include "web/tests/FrameTestHelpers.h"
18 #include <gtest/gtest.h>
20 using namespace blink;
24 class MockWebFrameClient : public WebFrameClient {
27 class ProgrammaticScrollTest : public testing::Test {
29 ProgrammaticScrollTest()
30 : m_baseURL("http://www.test.com/")
34 virtual void TearDown()
36 Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
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));
59 WebViewImpl* webViewImpl = toWebViewImpl(webView);
60 LocalFrame* frame = webViewImpl->mainFrameImpl()->frame();
61 frame->loader().setLoadType(FrameLoadTypeBackForward);
63 webViewImpl->setPageScaleFactor(3.0f);
64 webViewImpl->setMainFrameScrollOffset(WebPoint(0, 500));
65 frame->view()->setWasScrolledByUser(false);
66 frame->loader().currentItem()->setPageScaleFactor(2);
67 frame->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 frame->view()->setWasScrolledByUser(false);
72 frame->loader().restoreScrollPositionAndViewState();
74 // Expect that both scroll and scale were restored, and that it was not a programmatic scroll.
75 EXPECT_EQ(2.0f, webViewImpl->pageScaleFactor());
76 EXPECT_EQ(200, webViewImpl->mainFrameImpl()->scrollOffset().height);
77 EXPECT_TRUE(frame->view()->wasScrolledByUser());
80 TEST_F(ProgrammaticScrollTest, RestoreScrollPositionAndViewStateWithoutScale)
82 registerMockedHttpURLLoad("long_scroll.html");
84 FrameTestHelpers::WebViewHelper webViewHelper;
85 WebView* webView = webViewHelper.initializeAndLoad(m_baseURL + "long_scroll.html", true, 0, 0);
86 webView->resize(WebSize(1000, 1000));
89 WebViewImpl* webViewImpl = toWebViewImpl(webView);
90 LocalFrame* frame = webViewImpl->mainFrameImpl()->frame();
91 frame->loader().setLoadType(FrameLoadTypeBackForward);
93 webViewImpl->setPageScaleFactor(3.0f);
94 webViewImpl->setMainFrameScrollOffset(WebPoint(0, 500));
95 frame->view()->setWasScrolledByUser(false);
96 frame->loader().currentItem()->setPageScaleFactor(0);
97 frame->loader().currentItem()->setScrollPoint(WebPoint(0, 400));
99 // FrameLoader::restoreScrollPositionAndViewState flows differently if scale is zero.
100 frame->loader().restoreScrollPositionAndViewState();
102 // Expect that only the scroll position was restored, and that it was not a programmatic scroll.
103 EXPECT_EQ(3.0f, webViewImpl->pageScaleFactor());
104 EXPECT_EQ(400, webViewImpl->mainFrameImpl()->scrollOffset().height);
105 EXPECT_TRUE(frame->view()->wasScrolledByUser());