<html>
<head>
<script src="../../resources/js-test-pre.js"></script>
<style type="text/css">
.box {
  display: box;
  border: 1px solid black;
  margin-bottom: 0.5em;
}
.boxheader {
  font-weight: bold;
  color: maroon;
}
pre {
  margin-left: 2em;
}
</style>
</head>
<body>

<div id="description"></div>
<div id="console"></div>

<script>
description("Tests XMLHttpRequest 'document' loading with the .responseType and .response attributes.");

var xhr = 0;

function load() {
    testPassed('DONE LOADING');
    testPassed('received response object of type : ' + typeof xhr.response + ".");

    // Make sure exception is thrown if responseType is set too late in the loading process.
    // .responseType was previously set to "document".  Let's try setting it to "arraybuffer".
    try {
        xhr.responseType = "arraybuffer";
    } catch(e) {
        testPassed("exception correctly thrown when xhr.responseType is set to valid value too late in the loading process : " + e + ".");
    }

    // Check that .response is a Document
    if (xhr.response.documentElement.tagName == 'doc')
        testPassed("xhr.response is a Document.");
    else
        testFailed("xhr.response should be a Document.");
    
    // .response is really just an alias to .responseXML when .responseType is set to "document".
    // Make sure they're the same.
    if (xhr.response == xhr.responseXML)
        testPassed("xhr.response == xhr.responseXML.");
    else
        testFailed("xhr.response == xhr.responseXML.");

    xhr = null;
    finishJSTest();
}

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

    xhr = new XMLHttpRequest();
    xhr.onload = load;
    xhr.open("GET", "resources/xmlhttprequest-get-data.xml", true);
        
    try {
        if ("responseType" in xhr)
            testPassed("responseType property exists.");
        else
            testFailed("responseType property does not exist.");
        

        if ("response" in xhr)
            testPassed("response property exists.");
        else
            testFailed("response property does not exist.");

        // Make sure we can set responseType to "document" before send() is called.
        try {
            xhr.responseType = "document";
            if (xhr.responseType == "document")
                testPassed("xhr.responseType has been correctly set to 'document'.");
        } catch(e) {
            testFailed("unable to set xhr.responseType to 'document' " + e + ".");
        }
    } catch(e) {
        testFailed("Caught exception " + e + ".");
    }

    xhr.send(null);
    window.jsTestIsAsync = true;
}

runTest();

</script>

<script src="../../resources/js-test-post.js"></script>

</body>
</html>
