chromium/third_party/blink/web_tests/fast/events/change-frame-focus.html

<html>
<head>
    <script>
        function log(msg)
        {
            document.getElementById("log").appendChild(
                document.createTextNode(msg + "\n"));
        }
        
        function setFrameHandler(frameId)
        {
            if (frameId == "") {
                window.onfocus = function()
                {
                    log("main frame focused.");
                }
            
                window.onblur = function()
                {
                    log("main frame blurred.");
                }
            } else {
                var frameWin = document.getElementById(frameId).contentWindow;
                frameWin.onfocus = function()
                {
                    log(frameId + " focused.");
                }

                frameWin.onblur = function()
                {
                    log(frameId + " blurred.");
                }
            }
        }

        function setFrameFocus(frameId)
        {
            if (frameId == "") {
                window.focus();
            } else {
                var frameWin = document.getElementById(frameId).contentWindow;
                frameWin.focus();
            }
        }

        function testMainFrameToIFrameFocus()
        {
            log("\nTest: main frame to iframe.");
            setFrameFocus("");
            setFrameFocus("iframe1");
        }

        function testIFrameToMainFrameFocus()
        {
            log("\nTest: iframe to main frame.");
            setFrameFocus("iframe1");
            setFrameFocus("");
        }

        function testIFrameToIFrameFocus()
        {
            log("\nTest: iframe1 to iframe2.");
            setFrameFocus("iframe1");
            setFrameFocus("iframe2");
            setFrameFocus("iframe1");
        }

        function testFrame1OnBlurSetFrame2Focus()
        {
            var frameWin1 = document.getElementById("iframe1").contentWindow;
            frameWin1.onfocus = function()
            {
                log("iframe1 focused.");
            }

            frameWin1.onblur = function()
            {
                log("iframe1 blurred.");
                // This set focus request will be ignored because the FocusController
                // is in the middle of changing focused frame.
                setFrameFocus("iframe2");
            }

            log("\nTest: iframe1 onblur sets iframe2 focus.");
            setFrameFocus("");
            setFrameFocus("iframe1");
            setFrameFocus("");
        }

        function testFrame1OnBlurSetFrame1Focus()
        {
            var frameWin1 = document.getElementById("iframe1").contentWindow;
            frameWin1.onfocus = function()
            {
                log("iframe1 focused.");
            }

            frameWin1.onblur = function()
            {
                log("iframe1 blurred.");
                // This set focus request will be ignored because the FocusController
                // is in the middle of changing focused frame.
                setFrameFocus("iframe1");
            }

            log("\nTest: iframe1 onblur sets iframe1 focus.");
            setFrameFocus("");
            setFrameFocus("iframe1");
            setFrameFocus("");
        }

        function testFrame1OnFocusSetFrame2Focus()
        {
            var frameWin1 = document.getElementById("iframe1").contentWindow;
            frameWin1.onfocus = function()
            {
                log("iframe1 focused.");
                // This set focus request will be ignored because the FocusController
                // is in the middle of changing focused frame.
                setFrameFocus("iframe2");
            }

            frameWin1.onblur = function()
            {
                log("iframe1 blurred.");
            }

            log("\nTest: iframe1 onfocus sets iframe2 focus.");
            setFrameFocus("");
            setFrameFocus("iframe1");
        }

        function testFrame1OnFocusSetFrame1Focus()
        {
            var frameWin1 = document.getElementById("iframe1").contentWindow;
            frameWin1.onfocus = function()
            {
                log("iframe1 focused.");
                // This set focus request will be ignored because the FocusController
                // is in the middle of changing focused frame.
                setFrameFocus("iframe1");
            }

            frameWin1.onblur = function()
            {
                log("iframe1 blurred.");
            }

            log("\nTest: iframe1 onfocus sets iframe1 focus.");
            setFrameFocus("");
            setFrameFocus("iframe1");
        }

        function test()
        {
            if (window.testRunner) {
                testRunner.dumpAsText();
            }

            setFrameHandler("");
            setFrameHandler("iframe1");
            setFrameHandler("iframe2");

            // Test the correct frame is focused when switching focus from
            // one frame to another.
            testMainFrameToIFrameFocus();
            testIFrameToMainFrameFocus();
            testIFrameToIFrameFocus();
            
            // New setting focus request will be ignored if the focus controller
            // is in the middle of switching focused frame (onblur, onfocus events).
            testFrame1OnBlurSetFrame2Focus();
            testFrame1OnBlurSetFrame1Focus();
            testFrame1OnFocusSetFrame2Focus();
            testFrame1OnFocusSetFrame1Focus();
            
            // Restore iframe1 onfocus and onblur handlers.
            setFrameHandler("iframe1");
        }

    </script>
</head>
<body onload="test()">
    <iframe id="iframe1" style="width: 100px; height: 100px;"></iframe>
    <iframe id="iframe2" style="width: 100px; height: 100px;"></iframe>
    <p>Test the focus controller working properly when switching focused frame. Here are the cases tested:
    <br>
    <br>-. Correct frame is focused when switching focus from one frame to another:
    <br>1. main frame -> iframe
    <br>2. iframe to main frame
    <br>3. iframe 1 to iframe 2
    <br>
    <br>-. New setting focus request will be ignored if the focus controller is in the middle of switching focused frame (onblur, onfocus events):
    <br>1. iframe 1 onblur sets iframe 2 focus.
    <br>2. iframe 1 onblur sets iframe 1 focus.
    <br>3. iframe 1 onfocus sets iframe 2 focus.
    <br>4. iframe 1 onfocus sets iframe 1 focus.
    </p>
    <pre id="log"></pre>
</body>
</html>