1#include "config.h"
2
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>
19
20using namespace blink;
21
22namespace {
23
24class MockWebFrameClient : public WebFrameClient {
25};
26
27class ProgrammaticScrollTest : public testing::Test {
28public:
29    ProgrammaticScrollTest()
30        : m_baseURL("http://www.test.com/")
31    {
32    }
33
34    virtual void TearDown()
35    {
36        Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
37    }
38
39protected:
40
41    void registerMockedHttpURLLoad(const std::string& fileName)
42    {
43        URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseURL.c_str()), WebString::fromUTF8(fileName.c_str()));
44    }
45
46    std::string m_baseURL;
47    MockWebFrameClient m_mockWebFrameClient;
48};
49
50TEST_F(ProgrammaticScrollTest, RestoreScrollPositionAndViewStateWithScale)
51{
52    registerMockedHttpURLLoad("long_scroll.html");
53
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();
58
59    WebViewImpl* webViewImpl = toWebViewImpl(webView);
60    LocalFrame* frame = webViewImpl->mainFrameImpl()->frame();
61    frame->loader().setLoadType(FrameLoadTypeBackForward);
62
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));
68
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();
73
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());
78}
79
80TEST_F(ProgrammaticScrollTest, RestoreScrollPositionAndViewStateWithoutScale)
81{
82    registerMockedHttpURLLoad("long_scroll.html");
83
84    FrameTestHelpers::WebViewHelper webViewHelper;
85    WebView* webView = webViewHelper.initializeAndLoad(m_baseURL + "long_scroll.html", true, 0, 0);
86    webView->resize(WebSize(1000, 1000));
87    webView->layout();
88
89    WebViewImpl* webViewImpl = toWebViewImpl(webView);
90    LocalFrame* frame = webViewImpl->mainFrameImpl()->frame();
91    frame->loader().setLoadType(FrameLoadTypeBackForward);
92
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));
98
99    // FrameLoader::restoreScrollPositionAndViewState flows differently if scale is zero.
100    frame->loader().restoreScrollPositionAndViewState();
101
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());
106}
107
108}
109