dojo.provide("tests.webmap.widget.LabelCalloutBox");

dojo.require("doh.runner");
dojo.require("webmap.widget.LabelCalloutBox");
dojo.require("tests.Util");

var div;
var cont;
var tb;
var tbw;
function doLabelCalloutBoxSetup(){
	tests.Util.resetDom();
	div = document.createElement("div");
	document.body.appendChild(div);
	cont = document.createElement("div");
	document.body.appendChild(cont);
	tb = document.createElement("div");
	document.body.appendChild(tb);
	tb.id = "tb";
	tbw = new dijit.Toolbar({}, tb);
}
doh.register("tests.webmap.widget.LabelCalloutBox", 
	[
		{
			name: "postCreate",
			widget: null,
			style: dojo.style,
			connect: dojo.connect,
			call: webmap.widget.LabelCalloutBox.superclass.postCreate.call,
			styleArgs: [],
			connArgs: [],
			callArgs: [],
			setUp: function(){
				doLabelCalloutBoxSetup();
				var _this = this;
				dojo.style = function(a0,a1,a2){_this.styleArgs.push([a0,a1,a2]);};
				dojo.connect = function(a0,a1,a2,a3){_this.connArgs.push([a0,a1,a2,a3]);return "test connection";};
				webmap.widget.LabelCalloutBox.superclass.postCreate.call = function(a){
					_this.callArgs.push(a);
				}
				this.widget = new webmap.widget.LabelCalloutBox({
					toolbar:tbw,
					_pointer:{destroy:function(){}},
					postMixInProperties: function(){},
					templateString:"<div></div>",
					contentNode:{domNode:cont},
					_connections: []
				},div);
			},
			runTest: function(){
				tests.assertEqual(this.widget.contentNode.domNode, this.widget.pointFromNode);
				tests.assertTrue(this.widget.contentNode.domNode.firstChild);
				tests.assertEqual([this.widget], this.callArgs);
				//first two connections are from postCreate
				//others are from construction of editor
				tests.assertEqual([this.widget.domNode, "onmouseover", this.widget, "doHover"], this.connArgs[0]);
				tests.assertEqual([this.widget.domNode, "onmouseout", this.widget, "doMouseout"], this.connArgs[1]);
				while (this.widget._connections.length > 0)
					tests.assertEqual("test connection", this.widget._connections.pop());
				tests.assertEqual(5, this.styleArgs.length);
				tests.assertEqual([this.widget.contentNode, "overflow", "hidden"], this.styleArgs[0]);
				tests.assertEqual([this.widget.contentNode, "visibility", "visible"], this.styleArgs[1]);
				tests.assertEqual([this.widget._editor.document.body, "margin", "5px"], this.styleArgs[2]);//called by editor contruction
				tests.assertEqual([this.widget.contentNode, "backgroundColor", undefined], this.styleArgs[3]);
				tests.assertEqual([this.widget._editor.document.body, "backgroundColor", undefined], this.styleArgs[4]);
				tests.assertEqual(this.widget.id + ".editor", this.widget._editor.id);
				tests.assertEqual(this.widget.toolbar, this.widget._editor.toolbar);
			},
			tearDown: function(){
				dojo.style = this.style;
				dojo.connect = this.connect;
				webmap.widget.LabelCalloutBox.superclass.postCreate.call = this.call;
				this.widget.destroy();
			}
		},
		{
			name: "setPointerBackgroundColor",
			widget: null,
			pointer: null,
			args:[],
			setUp: function(){
				doLabelCalloutBoxSetup();
				this.widget = new webmap.widget.LabelCalloutBox({x:1,y:1},div);
				this.pointer = this.widget._pointer;
				var _this = this;
				this.widget._pointer = {
					setPointerBackgroundColor:function(c){_this.args.push(c);}
				};
			},
			runTest: function(){
				this.widget.setPointerBackgroundColor("a color");
				tests.assertEqual(["a color"], this.args);
			},
			tearDown: function(){
				this.widget._pointer = this.pointer;
				this.widget.destroy();
			}
		},
		{
			name: "setPointerBorderColor",
			widget: null,
			pointer: null,
			args:[],
			setUp: function(){
				doLabelCalloutBoxSetup();
				this.widget = new webmap.widget.LabelCalloutBox({x:1,y:1},div);
				this.pointer = this.widget._pointer;
				var _this = this;
				this.widget._pointer = {
					setPointerBorderColor:function(c){_this.args.push(c);}
				};
			},
			runTest: function(){
				this.widget.setPointerBorderColor("a color");
				tests.assertEqual(["a color"], this.args);
			},
			tearDown: function(){
				this.widget._pointer = this.pointer;
				this.widget.destroy();
			}
		},
		{
			name: "setPointerBorderWidth",
			widget: null,
			pointer: null,
			args:[],
			setUp: function(){
				doLabelCalloutBoxSetup();
				this.widget = new webmap.widget.LabelCalloutBox({x:1,y:1},div);
				this.pointer = this.widget._pointer;
				var _this = this;
				this.widget._pointer = {
					setPointerBorderWidth:function(w){_this.args.push(w);}
				};
			},
			runTest: function(){
				this.widget.setPointerBorderWidth(3);
				tests.assertEqual([3], this.args);
			},
			tearDown: function(){
				this.widget._pointer = this.pointer;
				this.widget.destroy();
			}
		},
		{
			name: "doHover",
			widget: null,
			style: dojo.style,
			args:[],
			setUp: function(){
				doLabelCalloutBoxSetup();
				this.widget = new webmap.widget.LabelCalloutBox({x:1,y:1},div);
				var _this = this;
				dojo.style = function(a0,a1,a2){_this.args.push([a0,a1,a2]);};
			},
			runTest: function(){
				this.widget.doHover();
				tests.assertEqual([
					[this.widget.domNode, "border", "1px dashed lightgrey"],
					[this.widget.deleteNode, "display", "block"]
					], this.args);
			},
			tearDown: function(){
				dojo.style = this.style;
				this.widget.destroy();
			}
		},
		{
			name: "doMouseout",
			widget: null,
			style: dojo.style,
			args:[],
			setUp: function(){
				doLabelCalloutBoxSetup();
				this.widget = new webmap.widget.LabelCalloutBox({x:1,y:1},div);
				var _this = this;
				dojo.style = function(a0,a1,a2){_this.args.push([a0,a1,a2]);};
			},
			runTest: function(){
				this.widget.doMouseout();
				tests.assertEqual([
					[this.widget.domNode, "border", "none"],
					[this.widget.deleteNode, "display", "none"]
					], this.args);
			},
			tearDown: function(){
				dojo.style = this.style;
				this.widget.destroy();
			}
		},
		{
			name: "onAfterHide",
			widget: null,
			call: webmap.widget.LabelCalloutBox.superclass.onAfterHide.call,
			callArgs:[],
			toolbar: null,
			hides: 0,
			setUp: function(){
				doLabelCalloutBoxSetup();
				this.widget = new webmap.widget.LabelCalloutBox({x:1,y:1},div);
				var _this = this;
				webmap.widget.LabelCalloutBox.superclass.onAfterHide.call = function(a){
					_this.callArgs.push(a);
				};
				this.toolbar = this.widget._editor.toolbar;
				this.widget._editor.toolbar = {hide:function(){_this.hides++;}};
			},
			runTest: function(){
				this.widget.onAfterHide();
				tests.assertEqual([this.widget], this.callArgs);
				tests.assertEqual(1, this.hides);
			},
			tearDown: function(){
				webmap.widget.LabelCalloutBox.superclass.onAfterHide.call = this.call;
				this.widget._editor.toolbar = this.toolbar;
				this.widget.destroy();
			}
		},
		{
			name: "_hideToolbar",
			widget: null,
			editor: null,
			hides: 0,
			setUp: function(){
				doLabelCalloutBoxSetup();
				this.widget = new webmap.widget.LabelCalloutBox({x:1,y:1},div);
				var _this = this;
				this.editor = this.widget._editor;
				this.widget._editor = {toolbar:{hide:function(){_this.hides++;}}};
			},
			runTest: function(){
				this.widget._hideToolbar();
				tests.assertEqual(1, this.hides);
				this.widget._editor = {toolbar:{}};
				tests.assertEqual(1, this.hides);
				this.widget._editor = null;
				tests.assertEqual(1, this.hides);
			},
			tearDown: function(){
				this.widget._editor = this.editor;
				this.widget.destroy();
			}
		}
	]
);