<!DOCTYPE html>
<style>
    body {
        height: 2000px;
        width: 2000px;
    }
</style>

<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>

<script>
    var pageScaleFactor = 2;
    var scrollbarThicknessInCSSPixels;

    function viewport() {
        return window.visualViewport;
    }

    var test = async_test('Verify viewport dimensions');

    var doBeforeZooming = test.step_func(function() {
        scrollbarThicknessInCSSPixels = window.innerWidth - document.documentElement.clientWidth;
        window.scrollTo(100, 100);

        // Initial viewport dimensions.
        assert_equals(viewport().width, 800 - scrollbarThicknessInCSSPixels, "initial width");
        assert_equals(viewport().height, 600 - scrollbarThicknessInCSSPixels, "initial height");
        assert_equals(viewport().offsetLeft, 0, "initial offsetLeft");
        assert_equals(viewport().offsetTop, 0, "initial offsetTop");
        assert_equals(viewport().pageLeft, 100, "initial pageLeft");
        assert_equals(viewport().pageTop, 100, "initial pageTop");
        assert_equals(viewport().scale, 1, "initial scale");
    });

    var doAfterZooming = test.step_func(function() {
        scrollbarThicknessInCSSPixels /= pageScaleFactor;

        // Make the layout viewport have origin (100, 100).
        window.scrollTo(1500, 1500);
        window.scrollTo(100, 100);
        window.scrollTo(110, 110);
        assert_equals(viewport().width, 800 / pageScaleFactor - scrollbarThicknessInCSSPixels, "scaled width");
        assert_equals(viewport().height, 600 / pageScaleFactor - scrollbarThicknessInCSSPixels, "scaled height");
        assert_equals(viewport().offsetLeft, 10, "scrolled left viewport");
        assert_equals(viewport().offsetTop, 10, "scrolled top viewport");
        assert_equals(viewport().pageLeft, 110, "scrolled pageLeft");
        assert_equals(viewport().pageTop, 110, "scrolled pageTop");
        assert_equals(viewport().scale, 2, "scale");

        // Scroll the visual viewport.
        window.scrollTo(100, 120);
        assert_equals(viewport().offsetTop, 20, "set offsetTop");
        assert_equals(viewport().offsetLeft, 0, "set offsetLeft");

        test.done();
    });

    function getUIScript() {
        return `(function() {
            uiController.zoomToScale(${pageScaleFactor}, function() {
                uiController.uiScriptComplete(uiController.zoomScale);
            });
        })();`;
    }

    window.onload = function() {
        doBeforeZooming();
        testRunner.runUIScript(getUIScript(), function(zoomScale) {
            doAfterZooming();
        });
    };
</script>
