1 #include "config.h" 2 3 #include <gtest/gtest.h> 4 #include "FrameTestHelpers.h" 5 #include "URLTestHelpers.h" 6 #include "WebFrame.h" 7 #include "WebFrameClient.h" 8 #include "WebFrameImpl.h" 9 #include "WebHistoryItem.h" 10 #include "WebInputEvent.h" 11 #include "WebScriptSource.h" 12 #include "WebSettings.h" 13 #include "WebView.h" 14 #include "WebViewImpl.h" 15 #include "core/frame/FrameView.h" 16 #include "core/rendering/RenderView.h" 17 #include "public/platform/Platform.h" 18 #include "public/platform/WebUnitTestSupport.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 Frame* frame = webViewImpl->mainFrameImpl()->frame(); 62 frame->loader().setLoadType(FrameLoadTypeBackForward); 63 64 // Scale and scroll the page and save that state. Then scale and scroll again and restore. 65 webViewImpl->setPageScaleFactor(2.0f, WebPoint(0, 200)); 66 frame->loader().saveDocumentAndScrollState(); 67 webViewImpl->setPageScaleFactor(3.0f, WebPoint(0, 300)); 68 // Flip back the wasScrolledByUser flag which was set to true by setPageScaleFactor 69 // because otherwise FrameLoader::restoreScrollPositionAndViewState does nothing. 70 frame->view()->setWasScrolledByUser(false); 71 frame->loader().restoreScrollPositionAndViewState(); 72 73 // Expect that both scroll and scale were restored, and that it was not a programmatic scroll. 74 EXPECT_EQ(2.0f, webViewImpl->pageScaleFactor()); 75 EXPECT_EQ(200, webViewImpl->mainFrameImpl()->scrollOffset().height); 76 EXPECT_TRUE(frame->view()->wasScrolledByUser()); 77 } 78 79 TEST_F(ProgrammaticScrollTest, RestoreScrollPositionAndViewStateWithoutScale) 80 { 81 registerMockedHttpURLLoad("long_scroll.html"); 82 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(); 87 88 WebViewImpl* webViewImpl = toWebViewImpl(webView); 89 Frame* frame = webViewImpl->mainFrameImpl()->frame(); 90 frame->loader().setLoadType(FrameLoadTypeBackForward); 91 92 // Scale and scroll the page and save that state, but then set scale to zero. Then scale and 93 // scroll again and restore. 94 webViewImpl->setPageScaleFactor(2.0f, WebPoint(0, 400)); 95 frame->loader().saveDocumentAndScrollState(); 96 webViewImpl->setPageScaleFactor(3.0f, WebPoint(0, 500)); 97 // Flip back the wasScrolledByUser flag which was set to true by setPageScaleFactor 98 // because otherwise FrameLoader::restoreScrollPositionAndViewState does nothing. 99 frame->view()->setWasScrolledByUser(false); 100 // FrameLoader::restoreScrollPositionAndViewState flows differently if scale is zero. 101 frame->loader().currentItem()->setPageScaleFactor(0.0f); 102 frame->loader().restoreScrollPositionAndViewState(); 103 104 // Expect that only the scroll position was restored, and that it was not a programmatic scroll. 105 EXPECT_EQ(3.0f, webViewImpl->pageScaleFactor()); 106 EXPECT_EQ(400, webViewImpl->mainFrameImpl()->scrollOffset().height); 107 EXPECT_TRUE(frame->view()->wasScrolledByUser()); 108 } 109 110 } 111