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