<!DOCTYPE html>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>CSSMediaRule functions test</title>
        <style id="style1">
            @media all { .test { color: green; } }
        </style>
        <script>
            function log(message) {
                var item = document.createElement("li");
                item.appendChild(document.createTextNode(message));
                document.getElementById("console").appendChild(item);
            }

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

                var styleSheet = document.getElementById('style1').sheet;
                var mediaRule = styleSheet.cssRules[0];

                // CSSMediaRule.insertRule(rule, index) tests

                // Test that insertRule works.
                try {
                    var index = mediaRule.insertRule(".test2 { color: blue; }", mediaRule.cssRules.length);
                    log("PASS: No exception raised! New rule inserted successfully.");
                } catch (e) {
                    log("FAIL: no exception should have been thrown! Type of thrown exception was: " + e);
                }

                // Test that insertRule raises an exception for indexes greater than the length of the list.
                try {
                    var index = mediaRule.insertRule("p {color: red; }", mediaRule.cssRules.length + 1);
                    log("FAIL: an exception should have been thrown!");
                } catch (e) {
                    if (e.code == 1)
                        log("PASS: Exception raised successfully. Type: " + e);
                    else
                        log("FAIL: wrong exception type thrown. " + e + " was thrown, should of been 'IndexSizeError (DOM Exception 1): The index is not in the allowed range.'.");
                }

                // Test that insertRule raises an exception for indexes less than 0.
                try {
                    var index = mediaRule.insertRule("p {color: red; }", -1);
                    log("FAIL: an exception should have been thrown!");
                } catch (e) {
                    if (e.code == 1)
                        log("PASS: Exception raised successfully. Type: " + e);
                    else
                        log("FAIL: wrong exception type thrown. " + e + " was thrown, should of been 'IndexSizeError (DOM Exception 1): The index is not in the allowed range.'.");
                }

                // Test that insertRule raises an exception for malformed rules.
                try {
                    var index = mediaRule.insertRule("badbeef }{", mediaRule.cssRules.length);
                    log("FAIL: an exception should have been thrown!");
                } catch (e) {
                    if (e.code == 12)
                        log("PASS: Exception raised successfully. Type: " + e);
                    else
                        log("FAIL: wrong exception type thrown. " + e + " was thrown, should of been SyntaxError (DOM Exception 12): The string did not match the expected pattern.!");
                }

                // Test that insertRule raises an exception for illegally placed rules.
                try {
                    // ImportRule illegal inside a MediaRule.
                    var index = mediaRule.insertRule("@import url(sheet.css);", mediaRule.cssRules.length);
                    log("FAIL: an exception should have been thrown!");
                } catch (e) {
                    if (e.code == 3)
                        log("PASS: Exception raised successfully. Type: " + e);
                    else
                        log("FAIL: wrong exception type thrown. " + e.code + " was thrown, should of been HierarchyRequestError (DOM Exception 3): The operation would yield an incorrect node tree.!");
                }
                try {
                    // CharsetRule illegal inside a MediaRule.
                    var index = mediaRule.insertRule("@charset \"ISO-8859-1\";", mediaRule.cssRules.length);
                    log("FAIL: an exception should have been thrown!");
                } catch (e) {
                    // FIXME: this should throw a HIERARCHY_REQUEST_ERR, not a SYNTAX_ERR.
                    if (e.code == 12)
                        log("PASS: Exception raised successfully. Type: " + e);
                    else
                        log("FAIL: wrong exception type thrown. " + e + " was thrown, should of been SyntaxError (DOM Exception 12): The string did not match the expected pattern.!");
                }
                try {
                    // Nested MediaRule illegal inside a MediaRule.
                    var index = mediaRule.insertRule("@media screen { p { color: red; } };", mediaRule.cssRules.length);
                    log("FAIL: an exception should have been thrown!");
                } catch (e) {
                    // FIXME: this should throw a HIERARCHY_REQUEST_ERR, not a SYNTAX_ERR.
                    if (e.code == 12)
                        log("PASS: Exception raised successfully. Type: " + e);
                    else
                        log("FAIL: wrong exception type thrown. " + e + " was thrown, should of been SyntaxError (DOM Exception 12): The string did not match the expected pattern.!");
                }


                // CSSMediaRule.deleteRule(index) tests

                // Test that deleteRule works.
                try {
                    mediaRule.deleteRule(mediaRule.cssRules.length - 1);
                    log("PASS: No exception raised! Rule at position 'length - 1' deleted successfully.");
                } catch (e) {
                    log("FAIL: no exception should have been thrown! Type of thrown exception was: " + e);
                }

                // Test that deleteRule raises an exception for specified indexes not corresponding to a 
                // rule in the media rule list.
                try {
                    mediaRule.deleteRule(mediaRule.cssRules.length);
                    log("FAIL: an exception should have been thrown!");
                } catch (e) {
                    if (e.code == 1)
                        log("PASS: Exception raised successfully. Type: " + e);
                    else
                        log("FAIL: wrong exception type thrown. " + e + " was thrown, should of been 'IndexSizeError (DOM Exception 1): The index is not in the allowed range.'.");
                }
                try {
                    mediaRule.deleteRule(-1);
                    log("FAIL: an exception should have been thrown!");
                } catch (e) {
                    if (e.code == 1)
                        log("PASS: Exception raised successfully. Type: " + e);
                    else
                        log("FAIL: wrong exception type thrown. " + e + " was thrown, should of been 'IndexSizeError (DOM Exception 1): The index is not in the allowed range.'.");
                }
            }
        </script>
    </head>
    <body onload="test();">
        <p>This tests the insertRule(rule, index) and deleteRule(index) methods of the CSSMediaRule interface.  It has passed if
            all of the output below begins with the text "PASS".  
        <ol id="console">
        </ol>
    </body>
</html>
