<!DOCTYPE html>
<html>
<head>
<script>

var actionitems;

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

function doClick() {
    for (var i = 0; i < actionitems.length; i++)
    {
        var title = actionitems[i].title;

        if (!title)
           break;

        title = title.replace(/_/g,'');

        if (title.match("Copy Image")) {
           actionitems[i].click();
           break;
        }
    }

    document.body.innerHTML = "PASS";

    window.testRunner.notifyDone();
}

function hideDiv() {
    document.getElementById("DIV").style.display="none";
}

function doTest() {
    if (!window.testRunner) {
        document.body.addEventListener('mousedown', function () {setTimeout(hideDiv, 100)}, false);
        return;
    }

    var image = document.getElementById("IMG");

    x = image.offsetLeft + 10;
    y = image.offsetTop + 10;

    eventSender.mouseMoveTo(x, y);
    actionitems = eventSender.contextClick();

    hideDiv();

    setTimeout(doClick, 10);
}

</script>
</head>
<body onload="doTest()">
This is an automated test case for bug <a href="https://bugs.webkit.org/show_bug.cgi?id=31721">31721</a><br>
If you wish to test manually, mouseover to image, activate context menu, wait for the image to disappear and then click copy image.<br>
There should be no crash.
<div ID="DIV">
    <img id="IMG" src="resources/apple.gif"/>
    </div>
</body>

</html>
