blob: db00e2678a6ac04b755ac667b6e517d2fcc51c5e [file] [log] [blame]
#include "core/frame/FrameView.h"
#include "core/loader/DocumentLoader.h"
#include "core/loader/FrameLoader.h"
#include "platform/testing/URLTestHelpers.h"
#include "public/platform/Platform.h"
#include "public/platform/WebInputEvent.h"
#include "public/platform/WebURLLoaderMockFactory.h"
#include "public/web/WebCache.h"
#include "public/web/WebFrame.h"
#include "public/web/WebFrameClient.h"
#include "public/web/WebHistoryItem.h"
#include "public/web/WebScriptSource.h"
#include "public/web/WebSettings.h"
#include "public/web/WebView.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "web/WebLocalFrameImpl.h"
#include "web/WebViewImpl.h"
#include "web/tests/FrameTestHelpers.h"
namespace blink {
class ProgrammaticScrollTest : public testing::Test {
public:
ProgrammaticScrollTest() : m_baseURL("http://www.test.com/") {}
void TearDown() override {
Platform::current()->getURLLoaderMockFactory()->unregisterAllURLs();
WebCache::clear();
}
protected:
void registerMockedHttpURLLoad(const std::string& fileName) {
URLTestHelpers::registerMockedURLFromBaseURL(
WebString::fromUTF8(m_baseURL.c_str()),
WebString::fromUTF8(fileName.c_str()));
}
std::string m_baseURL;
FrameTestHelpers::TestWebFrameClient 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()->setScrollOffset(ScrollOffset(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()->setScrollOffset(ScrollOffset(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