blob: 22685a1df949dfb5d70606fc2615394620591dac [file] [log] [blame]
#include "config.h"
#include "core/frame/FrameView.h"
#include "core/layout/LayoutView.h"
#include "core/loader/DocumentLoader.h"
#include "core/loader/FrameLoader.h"
#include "platform/testing/URLTestHelpers.h"
#include "public/platform/Platform.h"
#include "public/platform/WebUnitTestSupport.h"
#include "public/web/WebFrame.h"
#include "public/web/WebFrameClient.h"
#include "public/web/WebHistoryItem.h"
#include "public/web/WebInputEvent.h"
#include "public/web/WebScriptSource.h"
#include "public/web/WebSettings.h"
#include "public/web/WebView.h"
#include "web/WebLocalFrameImpl.h"
#include "web/WebViewImpl.h"
#include "web/tests/FrameTestHelpers.h"
#include <gtest/gtest.h>
namespace blink {
class MockWebFrameClient : public WebFrameClient {
};
class ProgrammaticScrollTest : public testing::Test {
public:
ProgrammaticScrollTest()
: m_baseURL("http://www.test.com/")
{
}
void TearDown() override
{
Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
}
protected:
void registerMockedHttpURLLoad(const std::string& fileName)
{
URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseURL.c_str()), WebString::fromUTF8(fileName.c_str()));
}
std::string m_baseURL;
MockWebFrameClient m_mockWebFrameClient;
};
TEST_F(ProgrammaticScrollTest, RestoreScrollPositionAndViewStateWithScale)
{
registerMockedHttpURLLoad("long_scroll.html");
FrameTestHelpers::WebViewHelper webViewHelper;
WebView* webView = webViewHelper.initializeAndLoad(m_baseURL + "long_scroll.html", true, 0, 0);
webView->resize(WebSize(1000, 1000));
webView->updateAllLifecyclePhases();
WebViewImpl* webViewImpl = toWebViewImpl(webView);
FrameLoader& loader = webViewImpl->mainFrameImpl()->frame()->loader();
loader.setLoadType(FrameLoadTypeBackForward);
webViewImpl->setPageScaleFactor(3.0f);
webViewImpl->mainFrame()->setScrollOffset(WebSize(0, 500));
loader.documentLoader()->initialScrollState().wasScrolledByUser = false;
loader.currentItem()->setPageScaleFactor(2);
loader.currentItem()->setScrollPoint(WebPoint(0, 200));
// Flip back the wasScrolledByUser flag which was set to true by setPageScaleFactor
// because otherwise FrameLoader::restoreScrollPositionAndViewState does nothing.
loader.documentLoader()->initialScrollState().wasScrolledByUser = false;
loader.restoreScrollPositionAndViewState();
// Expect that both scroll and scale were restored.
EXPECT_EQ(2.0f, webViewImpl->pageScaleFactor());
EXPECT_EQ(200, webViewImpl->mainFrameImpl()->scrollOffset().height);
}
TEST_F(ProgrammaticScrollTest, RestoreScrollPositionAndViewStateWithoutScale)
{
registerMockedHttpURLLoad("long_scroll.html");
FrameTestHelpers::WebViewHelper webViewHelper;
WebView* webView = webViewHelper.initializeAndLoad(m_baseURL + "long_scroll.html", true, 0, 0);
webView->resize(WebSize(1000, 1000));
webView->updateAllLifecyclePhases();
WebViewImpl* webViewImpl = toWebViewImpl(webView);
FrameLoader& loader = webViewImpl->mainFrameImpl()->frame()->loader();
loader.setLoadType(FrameLoadTypeBackForward);
webViewImpl->setPageScaleFactor(3.0f);
webViewImpl->mainFrame()->setScrollOffset(WebSize(0, 500));
loader.documentLoader()->initialScrollState().wasScrolledByUser = false;
loader.currentItem()->setPageScaleFactor(0);
loader.currentItem()->setScrollPoint(WebPoint(0, 400));
// FrameLoader::restoreScrollPositionAndViewState flows differently if scale is zero.
loader.restoreScrollPositionAndViewState();
// Expect that only the scroll position was restored.
EXPECT_EQ(3.0f, webViewImpl->pageScaleFactor());
EXPECT_EQ(400, webViewImpl->mainFrameImpl()->scrollOffset().height);
}
} // namespace blink