<html>
<head>
<title>Original Title</title>
<link rel="shortcut icon" type="image/x-icon" href="http://test.com/oldfavicon.ico"/>
<script src="../../resources/js-test-pre.js"></script>
<script>

function setFavIcon(iconURL) {
    var docHead = document.getElementsByTagName("head")[0];
    var links = docHead.getElementsByTagName("link");
    for (var i = 0; i < links.length; ++i) {
        var link = links[i];
        if (link.type=="image/x-icon" && link.rel=="shortcut icon") {
            docHead.removeChild(link);
            break; // Assuming only one match at most.
        }
    }
    var link = document.createElement("link");
    link.type = "image/x-icon";
    link.rel = "shortcut icon";
    link.href = iconURL;
    docHead.appendChild(link);
}

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

    iconURL = document.getElementsByTagName("head")[0].getElementsByTagName("link")[0].href;
    debug('Original iconURL is: ' + iconURL);

    // change icon to new icon
    newURL = 'http://test.com/newfavicon.ico';
    debug('Setting new icon URL to: ' + newURL);
    setFavIcon(newURL);
    iconURL = document.getElementsByTagName("link")[0].href;
    debug('New iconURL is: ' + iconURL);

    // change icon back to old icon and ensure it changes properly
    oldURL = 'http://test.com/oldfavicon.ico';
    debug('Setting icon URL back to: ' + oldURL);
    setFavIcon(oldURL);
    iconURL = document.getElementsByTagName("link")[0].href;
    debug('Original iconURL is still: ' + iconURL);

    // check that the URL list in the document is as we expect
    var expectedURLs = "http://test.com/oldfavicon.ico";
    var iconURLs = window.internals.shortcutIconURLs();
    if (expectedURLs == iconURLs[0])
        testPassed('URL list matches expected');
    else
        testFailed('URL list does not match expected');
}

</script>
</head>
<body onload='runTests();'>
</div>
</body>
</html>
