<!DOCTYPE html>
<html id="html_element">
<head id="head_element">
<script src="../resources/js-test-pre.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>
<script>
    description("This tests page style selection.");

    function appendStyle(styleString)
    {
        var styleElement = document.createElement("style");
        styleElement.innerHTML = styleString;
        document.getElementById("head_element").appendChild(styleElement);
    }

    if (window.testRunner) {
        testRunner.dumpAsText();

        debug("Default margin is auto.");
        shouldBeEqualToString("internals.pageProperty('margin-left', 0)", "auto");
        shouldBeEqualToString("internals.pageProperty('margin-left', 1)", "auto");
        shouldBeEqualToString("internals.pageProperty('margin-left', 2)", "auto");
        shouldBeEqualToString("internals.pageProperty('margin-left', 3)", "auto");
        shouldBeEqualToString("internals.pageProperty('margin-left', 4)", "auto");

        debug("Set margin to 200px via user style sheet.");
        testRunner.addUserStyleSheet("@page { margin:200px; }", true);
        shouldBeEqualToString("internals.pageProperty('margin-left', 0)", "200");
        shouldBeEqualToString("internals.pageProperty('margin-left', 1)", "200");
        shouldBeEqualToString("internals.pageProperty('margin-left', 2)", "200");
        shouldBeEqualToString("internals.pageProperty('margin-left', 3)", "200");
        shouldBeEqualToString("internals.pageProperty('margin-left', 4)", "200");

        debug("Set margin to 100px.");
        appendStyle("@page { margin:100px; }");
        shouldBeEqualToString("internals.pageProperty('margin-left', 0)", "100");
        shouldBeEqualToString("internals.pageProperty('margin-left', 1)", "100");
        shouldBeEqualToString("internals.pageProperty('margin-left', 2)", "100");
        shouldBeEqualToString("internals.pageProperty('margin-left', 3)", "100");
        shouldBeEqualToString("internals.pageProperty('margin-left', 4)", "100");

        debug("Set margin for right pages to 50px. The first page is a right page.");
        appendStyle("@page :right { margin:50px; }");
        shouldBeEqualToString("internals.pageProperty('margin-left', 0)", "50");
        shouldBeEqualToString("internals.pageProperty('margin-left', 1)", "100");
        shouldBeEqualToString("internals.pageProperty('margin-left', 2)", "50");
        shouldBeEqualToString("internals.pageProperty('margin-left', 3)", "100");
        shouldBeEqualToString("internals.pageProperty('margin-left', 4)", "50");

        debug("Set margin for left pages to 75px.");
        appendStyle("@page :left { margin:75px; }");
        shouldBeEqualToString("internals.pageProperty('margin-left', 0)", "50");
        shouldBeEqualToString("internals.pageProperty('margin-left', 1)", "75");
        shouldBeEqualToString("internals.pageProperty('margin-left', 2)", "50");
        shouldBeEqualToString("internals.pageProperty('margin-left', 3)", "75");
        shouldBeEqualToString("internals.pageProperty('margin-left', 4)", "50");

        debug("Set margin for the first page to 125px.");
        appendStyle("@page :first { margin:125px; }");
        shouldBeEqualToString("internals.pageProperty('margin-left', 0)", "125");
        shouldBeEqualToString("internals.pageProperty('margin-left', 1)", "75");
        shouldBeEqualToString("internals.pageProperty('margin-left', 2)", "50");
        shouldBeEqualToString("internals.pageProperty('margin-left', 3)", "75");
        shouldBeEqualToString("internals.pageProperty('margin-left', 4)", "50");

        debug("Change the document direction to RTL. This makes the first page a left page.");
        document.getElementById("html_element").setAttribute("dir", "rtl");
        shouldBeEqualToString("internals.pageProperty('margin-left', 0)", "125");
        shouldBeEqualToString("internals.pageProperty('margin-left', 1)", "50");
        shouldBeEqualToString("internals.pageProperty('margin-left', 2)", "75");
        shouldBeEqualToString("internals.pageProperty('margin-left', 3)", "50");
        shouldBeEqualToString("internals.pageProperty('margin-left', 4)", "75");

        debug("Check font update and line height override.");
        appendStyle("@page { line-height:1em; }");
        appendStyle("@page { font-family:Futura; }");
        appendStyle("@page { font:15px/6em Arial; }");
        appendStyle("@page { zoom:200%; }");
        shouldBeEqualToString("internals.pageProperty('line-height', 0)", "180");
        shouldBeEqualToString("internals.pageProperty('font-family', 0)", "Arial");
        shouldBeEqualToString("internals.pageProperty('font-size', 0)", "30");
        appendStyle("@page { zoom:100%; }");

        debug("Check @media rules.");
        appendStyle("@page { size:100px 200px; }");
        shouldBeEqualToString("internals.pageProperty('size', 0)", "100 200");
        appendStyle("@media print { @page { size:150px 250px; } }");
        shouldBeEqualToString("internals.pageProperty('size', 0)", "150 250");
        appendStyle("@media screen { @page { size:300px 500px; } }");
        shouldBeEqualToString("internals.pageProperty('size', 0)", "150 250");

    } else {
        testFailed("This test can be run only with window.testRunner");
    }
</script>
<script src="../resources/js-test-post.js"></script>
</body>
</html>
