/*******************************************************************************
 * _TestLibs.js:
 *		Component of the JSUnit-based test suite for the OpenAjax Hub.
 *
 *		This is the unit test for the required metadata that each implementation must provide.
 *		.
 * Copyright 2006-2007 OpenAjax Alliance
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 
 * use this file except in compliance with the License. You may obtain a copy 
 * of the License at http://www.apache.org/licenses/LICENSE-2.0 . Unless 
 * required by applicable law or agreed to in writing, software distributed 
 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 
 * specific language governing permissions and limitations under the License.
 *
 ******************************************************************************/

var _TestLibs_tests = [
  "lib1_1"
];

/************************************************************************
 * JSUnit scaffolding used by _TestLibs_jsunit_*.html files.
 * Not used for standalone (i.e., non-JSUnit) testing harness.
 ***********************************************************************/
function exposeTestFunctionNames(){
	 return ["test_Libs"];
}
function test_Libs() {
	for (var i = 0; i < _TestLibs_tests.length; i++) {
		OpenAjax.hub.reinit();
		var test = window[_TestLibs_tests[i]];
		test.run();
		assertTrue(test.details, test.pass);
	}
}
/********************** End of JSUnit scaffolding. **********************/


/*************************************************************************
 * Standalone testing scaffolding used by _TestPubSub_standalone.html
 ************************************************************************/
function runTests() {
	var result = '<?xml version "1.0" encoding "UTF-8"?>\n\r<tests>';
	result += t1_1_1.run(); result += "<br/>";
	result += t1_1_2.run(); result += "<br/>";
	result += t1_1_3.run(); result += "<br/>";
	result += t1_1_4.run(); result += "<br/>";
	result += t1_2_1.run(); result += "<br/>";
	result += t1_3_1.run(); result += "<br/>";
	result += t2_1.run(); result += "<br/>";
	result += t2_2.run(); result += "<br/>";
	result += t2_3.run(); result += "<br/>";
	result += t3_3.run(); result += "<br/>";
	result += t3_4.run(); result += "<br/>";
	result += '</tests>';
	return result;
}
function ShowValue(val)
{
        var testFrame = document.getElementById("myFrame");
        var doc = testFrame.contentDocument;
        if (doc == undefined || doc == null) 
            doc = testFrame.contentWindow.document;

        doc.open();
        doc.write(val);
        doc.close();
    
}
/************* End of standalone testing scaffolding.********************/


/************************************************************************
 * Unit tests.
 ***********************************************************************/

var lib1_1 = {
	name: "1.1", 
	description: "Library registration unit tests",
	result: "",
	expectedResult: "1.1: start, " +
					"OK: OpenAjax.hub.registerLibrary called, " +
					"OK: OpenAjax.hub.unregisterLibrary called, " +
					"OK: OpenAjax.hub.libraries has only one entry, " +
					"OK: OpenAjax.hub.registerLibrary called, " +
					"OK: OpenAjax.hub.unregisterLibrary called, " +
					"OK: OpenAjax.hub.libraries has only one entry, " +
					"OK: OpenAjax.hub.registerLibrary called, " +
					"OK: OpenAjax.hub.unregisterLibrary called, " +
					"OK: OpenAjax.hub.libraries has only one entry, " +
					"OK: OpenAjax.hub.registerLibrary called, " +
					"OK: OpenAjax.hub.libraries has 3 entries, " +
					"OK: OpenAjax.hub.libraries.foo has correct values, " +
					"OK: OpenAjax.hub.libraries.bar has correct values, " +
					""
}
lib1_1.makeArrayOfLibraryNames = function() {
	var arr = [];
	for (var prefix in OpenAjax.hub.libraries) {
		arr.push(prefix);
	}
	return arr;
}
lib1_1.run = function() {
	this.result = "1.1: start, ";

	/* Register, then un-register a library */
	try {
		OpenAjax.hub.registerLibrary("foo", "http://example.com/foo", "1.2");
		OpenAjax.hub.registerLibrary("bar", "http://example.com/bar", "0.9.2");
		this.result += "OK: OpenAjax.hub.registerLibrary called, ";
	} catch (e) {
		this.result += "FAIL: Unexpected exception '"+e+"', ";
	}
	try {
		OpenAjax.hub.unregisterLibrary("foo");
		OpenAjax.hub.unregisterLibrary("bar");
		this.result += "OK: OpenAjax.hub.unregisterLibrary called, ";
	} catch (e) {
		this.result += "FAIL: Unexpected exception '"+e+"', ";
	}
	if (this.makeArrayOfLibraryNames().length == 1) {
		this.result += "OK: OpenAjax.hub.libraries has only one entry, ";
	} else {
		this.result += "FAIL: OpenAjax.hub.libraries does not have only one entry, ";
	}

	/* Register two libraries, then un-register two libraries, FIFO */
	try {
		OpenAjax.hub.registerLibrary("foo", "http://example.com/foo", "1.2");
		OpenAjax.hub.registerLibrary("bar", "http://example.com/bar", "0.9.2");
		this.result += "OK: OpenAjax.hub.registerLibrary called, ";
	} catch (e) {
		this.result += "FAIL: Unexpected exception '"+e+"', ";
	}
	try {
		OpenAjax.hub.unregisterLibrary("foo");
		OpenAjax.hub.unregisterLibrary("bar");
		this.result += "OK: OpenAjax.hub.unregisterLibrary called, ";
	} catch (e) {
		this.result += "FAIL: Unexpected exception '"+e+"', ";
	}
	if (this.makeArrayOfLibraryNames().length == 1) {
		this.result += "OK: OpenAjax.hub.libraries has only one entry, ";
	} else {
		this.result += "FAIL: OpenAjax.hub.libraries does not have only one entry, ";
	}

	/* Register two libraries, then un-register two libraries, LIFO */
	try {
		OpenAjax.hub.registerLibrary("foo", "http://example.com/foo", "1.2");
		OpenAjax.hub.registerLibrary("bar", "http://example.com/bar", "0.9.2");
		this.result += "OK: OpenAjax.hub.registerLibrary called, ";
	} catch (e) {
		this.result += "FAIL: Unexpected exception '"+e+"', ";
	}
	try {
		OpenAjax.hub.unregisterLibrary("bar");
		OpenAjax.hub.unregisterLibrary("foo");
		this.result += "OK: OpenAjax.hub.unregisterLibrary called, ";
	} catch (e) {
		this.result += "FAIL: Unexpected exception '"+e+"', ";
	}
	if (this.makeArrayOfLibraryNames().length == 1) {
		this.result += "OK: OpenAjax.hub.libraries has only one entry, ";
	} else {
		this.result += "FAIL: OpenAjax.hub.libraries does not have only one entry, ";
	}

	/* Register two libraries, verify OpenAjax.hub.libraries has the correct values. */
	try {
		OpenAjax.hub.registerLibrary("foo", "http://example.com/foo", "1.2");
		OpenAjax.hub.registerLibrary("bar", "http://example.com/bar", "0.9.2");
		this.result += "OK: OpenAjax.hub.registerLibrary called, ";
	} catch (e) {
		this.result += "FAIL: Unexpected exception '"+e+"', ";
	}
	if (this.makeArrayOfLibraryNames().length == 3) {
		this.result += "OK: OpenAjax.hub.libraries has 3 entries, ";
	} else {
		this.result += "FAIL: OpenAjax.hub.libraries does not have 3 entries, ";
	}
	if (OpenAjax.hub.libraries.foo.prefix == "foo" &&
			OpenAjax.hub.libraries.foo.namespaceURI == "http://example.com/foo" &&
			OpenAjax.hub.libraries.foo.version == "1.2" &&
			OpenAjax.hub.libraries.foo.extradata == null) {
		this.result += "OK: OpenAjax.hub.libraries.foo has correct values, ";
	} else {
		this.result += "FAIL: OpenAjax.hub.libraries.foo does not have correct values, ";
	}
	if (OpenAjax.hub.libraries.bar.prefix == "bar" &&
			OpenAjax.hub.libraries.bar.namespaceURI == "http://example.com/bar" &&
			OpenAjax.hub.libraries.bar.version == "0.9.2" &&
			OpenAjax.hub.libraries.bar.extradata == null) {
		this.result += "OK: OpenAjax.hub.libraries.bar has correct values, ";
	} else {
		this.result += "FAIL: OpenAjax.hub.libraries.bar does not have correct values, ";
	}
	if (this.result == this.expectedResult) {
		this.pass = true;
		this.details = "<test><name>" + this.name + "</name><result>PASSED</result></test>";
	} else {
		this.pass = false;
		this.details = "<test><name>" + this.name + "</name><result>FAILED</result><details>" + this.result + "</details><expected>" + this.expectedResult + "</expected></test>";
	}

// FIXME: Need test for extraData

}

