dojo.provide("tests.webmap.widget.Window");

dojo.require("doh.runner");
dojo.require("webmap.widget.Window");
dojo.require("tests.Util");

var div;
function doWindowSetup(){
	tests.Util.resetDom();
	div = document.createElement("div");
	document.body.appendChild(div);
	dojo.style(div, "left", "20px");
	dojo.style(div, "top", "30px");
	dojo.style(div, "width", "200px");
	dojo.style(div, "height", "100px");
}
doh.register("tests.webmap.widget.Window", 
	[
		{
			name: "postMixInProperties",
			widget: null,
			call: webmap.widget.Window.superclass.postMixInProperties.call,
			args: [],
			setUp: function(){
				doWindowSetup();
				var _this = this;
				webmap.widget.Window.superclass.postMixInProperties.call = function(a){_this.args.push(a);};
				this.widget = new webmap.widget.Window({
					templateString:"<div></div>", //overriding super class postMixInProperties so we need to inject a template
					title: "title"
				}, div);
			},
			runTest: function(){
				tests.assertEqual(this.widget.title, this.widget.titleText);
				tests.assertEqual([this.widget], this.args);
				this.widget.titleText = "";
				this.widget.postMixInProperties();
				tests.assertEqual(this.widget.title, this.widget.titleText);
				tests.assertEqual([this.widget,this.widget], this.args);
				this.widget.titleText = "titleText";
				this.widget.postMixInProperties();
				tests.assertEqual("titleText", this.widget.titleText);
				tests.assertEqual([this.widget,this.widget,this.widget], this.args);
			},
			tearDown: function(){
				webmap.widget.Window.superclass.postMixInProperties.call = this.call;
				this.widget.destroy();
			}
		},
		{
			name: "postCreate_no_handle",
			widget: null,
			call: webmap.widget.Window.superclass.postCreate.call,
			callArgs: [],
			hookArgs: [],
			contentArgs: [],
			setUp: function(){
				doWindowSetup();
				var _this = this;
				webmap.widget.Window.superclass.postCreate.call = function(a){_this.callArgs.push(a);};
				this.widget = new webmap.widget.Window({
					title:"title",
					titleText:"titleText",
					content:"content",
					hookUpWindowControlBox: function(a){_this.hookArgs.push(a);},
					setContent: function(a){_this.contentArgs.push(a);}
				}, div);
			},
			runTest: function(){
				tests.assertEqual(this.widget.titleText, this.widget.titleNode.innerHTML);
				tests.assertEqual([this.widget.content], this.contentArgs);
				tests.assertEqual([this.widget.closeable], this.hookArgs);
				tests.assertEqual([this.widget], this.callArgs);
				tests.assertEqual("auto", dojo.style(this.widget.contentNode, "overflow"));
				tests.assertEqual("hidden", dojo.style(this.widget.titleNode, "overflow"));
			},
			tearDown: function(){
				webmap.widget.Window.superclass.postCreate.call = this.call;
				this.widget.destroy();
			}
		},
		{
			name: "setTitle",
			widget: null,
			setUp: function(){
				doWindowSetup();
				this.widget = new webmap.widget.Window({title:"title"}, div);
			},
			runTest: function(){
				tests.assertEqual(this.widget.title, this.widget.titleNode.innerHTML);
				tests.assertEqual(this.widget.title, this.widget.titleNode.title);
				tests.assertEqual(this.widget.title, this.widget.domNode.title);
				//FIXME: this should be equal but contentNode title is not being set
				//tests.assertEqual(this.widget.title, this.widget.contentNode.title);
				tests.assertEqual(this.widget.title, this.widget.titleBarNode.title);
				this.widget.setTitle("new title");			
				tests.assertEqual("new title", this.widget.titleNode.innerHTML);
				tests.assertEqual("new title", this.widget.titleNode.title);
				tests.assertEqual("new title", this.widget.domNode.title);
				tests.assertEqual("new title", this.widget.contentNode.title);
				tests.assertEqual("new title", this.widget.titleBarNode.title);
				this.widget.setTitle("another title", "text");			
				tests.assertEqual("text", this.widget.titleNode.innerHTML);
				tests.assertEqual("another title", this.widget.titleNode.title);
				tests.assertEqual("another title", this.widget.domNode.title);
				tests.assertEqual("another title", this.widget.contentNode.title);
				tests.assertEqual("another title", this.widget.titleBarNode.title);
			},
			tearDown: function(){
				this.widget.destroy();
			}
		},
		{
			name: "setContent",
			widget: null,
			div1: document.createElement("div"),
			div2: document.createElement("div"),
			aWidget: null,
			setUp: function(){
				doWindowSetup();
				this.widget = new webmap.widget.Window({content:"content"}, div);
				this.div1.id = "div1";
				this.div2.id = "div2";
				document.body.appendChild(this.div1);
				document.body.appendChild(this.div2);
				this.aWidget = new dijit.layout.ContentPane({},document.createElement("div"));
			},
			runTest: function(){
				tests.assertEqual(this.widget.content, this.widget.contentNode.innerHTML);
				this.widget.setContent("new content");
				tests.assertEqual("new content", this.widget.contentNode.innerHTML);
				this.widget.setContent("div1");
				tests.assertEqual(this.div1, this.widget.contentNode.firstChild);
				this.widget.setContent(this.div2);
				tests.assertEqual(this.div2, this.widget.contentNode.firstChild);
				this.widget.setContent(this.aWidget);
				tests.assertEqual(this.aWidget.domNode, this.widget.contentNode.firstChild);	
			},
			tearDown: function(){
				this.aWidget.destroy();
				this.widget.destroy();
			}
		},
		{
			name: "getBox",
			widget: null,
			aWidget: null,
			setUp: function(){
				doWindowSetup();
				this.widget = new webmap.widget.Window({}, div);
				dojo.style(this.widget.domNode, "left", "20px");
				dojo.style(this.widget.domNode, "top", "30px");
				dojo.style(this.widget.domNode, "width", "200px");
				dojo.style(this.widget.domNode, "height", "100px");
			},
			runTest: function(){
				tests.assertEqual({l:20,t:30,w:200,h:100}, this.widget.getBox());
				dojo.style(this.widget.domNode, "display", "none");
				tests.assertEqual(null, this.widget.getBox());
			},
			tearDown: function(){
				this.widget.destroy();
			}
		}
	]
);