<html>
<head>
<STYLE>
pre { border:1px solid blue }
span { font-weight: bold }
.success { color: green; }
.failure { color: red; }
</STYLE>
</head>
<body id="body">

<script>
if (window.testRunner)
	testRunner.dumpAsText();

var requester = new XMLHttpRequest();
var processor = new XSLTProcessor();
var serializer = new XMLSerializer();

var sectionId = 0;
var testId = 0;

function addSectionHeader(name) {
	sectionId++;
	document.writeln("<br><span>" + sectionId + ".0 " + name + ":</span><br><br>");
	testId = 0;
}

function addResultExpectException(testname, func) {
	try {
		var result = func();
		addStringResult(testname, "****Failure**** (expected exception, instead got result: \"" + result + "\")", "failure");
	} catch (e) {
		// FIXME: probably should check exception type
		addSuccessResult(testname);
	}
}

function addResultExpectValueWhenSerialized(name, value, expectedValue) {
	var serializedValue;
	if (value)
		serializedValue = serializer.serializeToString(value);
	if (serializedValue == expectedValue)
		addSuccessResult(name);
	else
		addFailureResult(name, expectedValue, serializedValue);
}

function addResultExpectValue(name, value, expectedValue) {
	if (value == expectedValue)
		addSuccessResult(name);
	else
		addFailureResult(name, expectedValue, value);
}

function getXMLDocument(name)
{
	requester.open("GET", name, false);
	requester.send(null);
	return requester.responseXML;
}

function addStringResult(name, result, cssClass)
{
	testId++;
	var testNumberString = "" + sectionId + "." + testId
	var testIdString = "id" + testNumberString;
	document.writeln("<span>" + testNumberString + " " + name + ":</span>");
	document.writeln("<pre id=\"" + testIdString + "\" class=\"" + cssClass + "\">@@NO VALUE@@</pre>");
	document.getElementById(testIdString).firstChild.nodeValue = result;
}

function addFailureResult(name, expectedValue, actual)
{
	addStringResult(name, "****Failure**** (expected: \"" + expectedValue + "\" actual: \"" + actual + "\")", "failure");
}

function addSuccessResult(name)
{
	addStringResult(name, "Success", "success");
}

function addXMLResult(name, result)
{
	var str;
	if (result)
		str = serializer.serializeToString(result);
	addStringResult(name, str);
}

var xml = getXMLDocument("resources/xslt-text.xml");
addXMLResult("original xml", xml);
var xsl1 = getXMLDocument("resources/xslt-text.xsl");
addXMLResult("xsl1", xsl1);
var xsl2 = getXMLDocument("resources/xslt-enc.xsl");
addXMLResult("xsl2", xsl2);



// 1.0 void importStylesheet(in DOMNode style);
addSectionHeader("void importStylesheet(in DOMNode style)");
// 1.1 Import two different stylesheets (last one wins)

processor.importStylesheet(xsl1);
processor.importStylesheet(xsl2);
var newXML = processor.transformToDocument(xml);
addXMLResult("Import two different stylesheets", newXML);

// 1.2 Import same stylesheet twice

processor.reset();
processor.importStylesheet(xsl1);
processor.importStylesheet(xsl1);

var newXML = processor.transformToDocument(xml);
addXMLResult("Import same stylesheet twice", newXML);

// 1.3 Import undefined stylesheet

processor.reset();
var testname = "Import undefined stylesheet";
addResultExpectException(testname, function () { return processor.importStylesheet(undefined); } );

// 1.4 Import invalid stylesheet (not wellformed xlst)

processor.reset();
var testName = "Import invalid stylesheet";
addResultExpectException(testname, function () { return processor.importStylesheet(xml); } );

// 1.5 Don't import stylesheet

processor.reset();
var newXML = processor.transformToDocument(xml);
addResultExpectValueWhenSerialized("Don't import stylesheet", newXML, undefined);



// 2.0 DOMDocumentFragment transformToFragment(in DOMNode source, in DOMDocument output);

addSectionHeader("DOMDocumentFragment transformToFragment(in DOMNode source, in DOMDocument output)");

// 2.1 fragment with undefined source

processor.reset();
processor.importStylesheet(xsl1);
var ownerDocument = document.implementation.createDocument("", "test", null);
var testname = "fragment with undefined source";
addResultExpectException(testname, function () { return processor.transformToFragment(undefined, ownerDocument); } );

// 2.2 fragment with undefined output document

var testName = "fragment with undefined output document"
addResultExpectException(testName, () => { processor.transformToFragment(xml, undefined); });

// 2.3 use non-DOMDocument output parameter

var testname = "use non-DOMDocument output parameter";
addResultExpectException(testname, function () { return processor.transformToFragment(xml, "error"); } );

// 2.4 transform to same fragment twice

ownerDocument = document.implementation.createDocument("", "test", null);
var secondDocument = document.implementation.createDocument("", "test", null);
newFragment = processor.transformToFragment(xml, ownerDocument);
var secondFragment = processor.transformToFragment(xml, secondDocument);
addResultExpectValueWhenSerialized("transform to same fragment twice", secondFragment, serializer.serializeToString(newFragment));


// 2.5 transform to text
// 2.6 fragment using passed parameters
var paramXSL = getXMLDocument("resources/xslt-param.xsl");
var paramXML = getXMLDocument("resources/xslt-param.xml");

var paramProcessor = new XSLTProcessor;
paramProcessor.importStylesheet(paramXSL);
paramProcessor.setParameter(null, "param", "SUCCESS");

var ownerDocument = document.implementation.createDocument("", "test", null);
var fragment = paramProcessor.transformToFragment(paramXML, ownerDocument);
addResultExpectValueWhenSerialized("transformed fragment containing only text", fragment, "SUCCESS");
addResultExpectValue("fragment using passed parameters", fragment.firstChild.nodeValue, "SUCCESS");



// 3.0 DOMDocument transformToDocument(in DOMNode source);
addSectionHeader("DOMDocument transformToDocument(in DOMNode source)");

// 3.1 document from undefined source

processor.reset();
processor.importStylesheet(xsl1);
addResultExpectException("document from undefined source", function () { return processor.transformToDocument(undefined); } );

// 3.2 transform twice

processor.reset();
processor.importStylesheet(xsl1);
var newXML = processor.transformToDocument(xml);
var newerXML = processor.transformToDocument(xml);
addResultExpectValueWhenSerialized("transform twice", newXML, serializer.serializeToString(newerXML));




// 4.0 void setParameter(in DOMString namespaceURI, in DOMString localName, in Value value);
addSectionHeader("void setParameter(in DOMString namespaceURI, in DOMString localName, in Value value)");
processor.reset();

// 4.1 pass setParameter same localname, different namespaces
processor.setParameter("foo", "foo", "Success");
processor.setParameter("bar", "foo", "Failure");
var testname = "pass setParameter same localname, different namespaces";
addResultExpectValue(testname, processor.getParameter("foo", "foo"), "Success");

// 4.2 pass undefined namespace

processor.setParameter(undefined, "foo", "Success");
addResultExpectValue("pass undefined namespace", processor.getParameter(undefined, "foo"), "Success");

// 4.3 pass setParameter an undefined name

var testname = "pass setParameter an undefined name";
addResultExpectException(testname, function () { return processor.setParameter(undefined, undefined , "Success"); } );

// 4.4 pass undefined value

processor.setParameter(undefined, "foo", undefined);
var testname = "pass undefined value";


// 4.5 pass unsupported value to setParameter (object, for instance?)

var testname = "pass unsupported value to setParameter";
addResultExpectException(testname, function () { return processor.setParameter(undefined, "invalid" , new Array()); } );


// 5.0 Value getParameter(in DOMString namespaceURI, in DOMString localName);
addSectionHeader("Value getParameter(in DOMString namespaceURI, in DOMString localName)");

// 5.1 pass undefined name

var testname = "pass getParameter an undefined name";
addResultExpectException(testname, function () { return processor.getParameter(undefined, undefined); } );

// 5.2 pass getParameter a name which has not been set

var testname = "pass getParameter a name which has not been set";
addResultExpectValue(testname, processor.getParameter(undefined, "hasnotbeenset"), null);

// 5.3 verify getParameter actually gets (and set sets)
processor.setParameter(null, "test", "testValue");
var testname = "verify getParameter actually gets (and set sets)";
addResultExpectValue(testname, processor.getParameter(null, "test"), "testValue");



// 6.0 void removeParameter(in DOMString namespaceURI, in DOMString localName);
addSectionHeader("void removeParameter(in DOMString namespaceURI, in DOMString localName)");
// 6.1 pass removeParameter same localname, different namespaces

processor.setParameter("foo", "foo", "Success");
processor.setParameter("bar", "foo", "Failure");
var testname = "pass removeParameter same localname, different namespaces";
addResultExpectValue(testname, processor.getParameter("foo", "foo"), "Success");

// 6.2 pass undefined namespace

processor.setParameter(undefined, "test", "testValue");
processor.removeParameter(undefined, "test");
var testname = "verify removeParameter actually removes using undefined namespace";	
addResultExpectValue(testname, processor.getParameter(null, "test"), null);

// 6.3 pass removeParameter undefined name

processor.setParameter(undefined, "undefined", "Success");
processor.removeParameter(undefined, undefined);
var testname = "pass removeParameter undefined name";
addResultExpectValue(testname, processor.getParameter(undefined, "undefined"), null);

// 6.4 pass removeParameter a name which has not been set

processor.removeParameter(undefined, "neverbeensetforremove");
addResultExpectValue("pass removeParameter a name which has not been set", "Success", "Success");

// 6.5 verify removeParameter actually removes
processor.setParameter(null, "test", "testValue");
processor.removeParameter(null, "test");
var testname = "verify removeParameter actually removes";
addResultExpectValue(testname, processor.getParameter(null, "test"), null);



// 7.0 void clearParameters();
addSectionHeader("void clearParameters()");

// 7.1 call, verify that parameters have been cleared
processor.setParameter(null, "test", "testValue");
processor.clearParameters();
var testname = "verify that clearParameters does";
addResultExpectValue(testname, processor.getParameter(null, "test"), null);


// 8.0 void reset();
addSectionHeader("void reset()");
processor = new XSLTProcessor();
processor.importStylesheet(xsl1);
processor.setParameter(null, "test", "testValue");
processor.reset();

// 8.1 call, verify that parameters are cleared after reset()
var testname = "verify that parameters have been cleared";
addResultExpectValue(testname, processor.getParameter(null, "test"), null);

// 8.2 call, verify that stylesheet has been cleared after reset()
var testname = "verify that stylesheet has been cleared";
var resultDocument = processor.transformToDocument(xml);
addResultExpectValueWhenSerialized(testname, resultDocument, undefined);

</script>

</body>
</html>
