dojo.provide("tests.webmap.widget.DockControl");

dojo.require("doh.runner");
dojo.require("webmap.widget.DockControl");
dojo.require("tests.Util");

var div;
function doDockControlSetup(){
	div = document.createElement("div");
	document.body.appendChild(div);
	div.appendChild(document.createElement("img"));
	dojo.addClass(div.firstChild, "webmapUnDockControl");
	
}
doh.register("tests.webmap.widget.DockControl", 
	[
		{
			name: "postCreate_isUndocked_false_ff",
			widget: null,
			call: webmap.widget.DockControl.superclass.postCreate.call,
			calls: 0,
			arg: null,
			resizes: 0,
			addOnUnload: dojo.addOnUnload,
			unloadFunc: null,
			setClass: 0,
			subscribe: dojo.subscribe,
			setUp: function(){
				if (dojo.isIE) return;
				tests.Util.resetDom();
				window.isUndocked = false;
				var _this = this;
				//TODO: GO BACK TO OTHER TESTS AND USE THIS METHOD FOR TESTING SUBSCRIPTIONS
				dojo.subscribe = function(msg,scope,func){return [msg,scope,func];};
				dojo.addOnUnload = function(arg){_this.unloadFunc = arg;};
				webmap.widget.DockControl.superclass.postCreate.call = function(arg){
					_this.calls++;
					_this.arg = arg;
				};
				this.widget = new webmap.widget.DockControl({
					_resize: function(){_this.resizes++;},
					_setUndockedClass: function(){_this.setClass++}
				}, null);
			},
			runTest: function(){
				if (dojo.isIE) return;
				tests.assertEqual(this.widget, this.arg);
				tests.assertEqual([["webmap.widget.MapControl.resized", 
					this.widget, "_resize"]], 
					this.widget._subscriptions);
				tests.assertEqual(1, this.calls);
				tests.assertEqual(1, this.resizes);
				tests.assertEqual(0, this.setClass);
				tests.assertEqual(dojo.toJson(function(){_this.destroy();}), 
					dojo.toJson(this.unloadFunc));
			},
			tearDown: function(){
				if (dojo.isIE) return;
				webmap.widget.DockControl.superclass.postCreate.call = this.call;
				dojo.addOnUnload = this.addOnUnload;
				dojo.subscribe = this.subscribe;
				this.widget.destroy();
			}
		},
		{
			name: "postCreate_isUndocked_true_ff",
			widget: null,
			call: webmap.widget.DockControl.superclass.postCreate.call,
			calls: 0,
			arg: null,
			resizes: 0,
			addOnUnload: dojo.addOnUnload,
			unloadFunc: null,
			setClass: 0,
			subscribe: dojo.subscribe,
			setUp: function(){
				if (dojo.isIE) return;
				tests.Util.resetDom();
				window.isUndocked = true;
				var _this = this;
				//TODO: GO BACK TO OTHER TESTS AND USE THIS METHOD FOR TESTING SUBSCRIPTIONS
				dojo.subscribe = function(msg,scope,func){return [msg,scope,func];};
				dojo.addOnUnload = function(arg){_this.unloadFunc = arg;};
				webmap.widget.DockControl.superclass.postCreate.call = function(arg){
					_this.calls++;
					_this.arg = arg;
				};
				this.widget = new webmap.widget.DockControl({
					_resize: function(){_this.resizes++;},
					_setUndockedClass: function(){_this.setClass++}
				}, null);
			},
			runTest: function(){
				if (dojo.isIE) return;
				tests.assertEqual(this.widget, this.arg);
				tests.assertEqual([["webmap.widget.MapControl.resized", 
					this.widget, "_resize"]], 
					this.widget._subscriptions);
				tests.assertEqual(1, this.calls);
				tests.assertEqual(1, this.resizes);
				tests.assertEqual(1, this.setClass);
				tests.assertEqual(dojo.toJson(function(){_this.destroy();}), 
					dojo.toJson(this.unloadFunc));
			},
			tearDown: function(){
				if (dojo.isIE) return;
				webmap.widget.DockControl.superclass.postCreate.call = this.call;
				dojo.addOnUnload = this.addOnUnload;
				dojo.subscribe = this.subscribe;
				this.widget.destroy();
				window.isUndocked = false;
			}
		},
		{
			name: "postCreate_isUndocked_false_ie",
			widget: null,
			call: webmap.widget.DockControl.superclass.postCreate.call,
			calls: 0,
			arg: null,
			resizes: 0,
			setClass: 0,
			subscribe: dojo.subscribe,
			connect: dojo.connect,
			setUp: function(){
				if (!dojo.isIE) return;
				tests.Util.resetDom();
				window.isUndocked = false;
				var _this = this;
				//TODO: GO BACK TO OTHER TESTS AND USE THIS METHOD FOR TESTING SUBSCRIPTIONS
				dojo.subscribe = function(msg,scope,func){return [msg,scope,func];};
				dojo.connect = function(obj,evt,func){return [msg,scope,dojo.toJson(func)];};
				webmap.widget.DockControl.superclass.postCreate.call = function(arg){
					_this.calls++;
					_this.arg = arg;
				};
				this.widget = new webmap.widget.DockControl({
					_resize: function(){_this.resizes++;},
					_setUndockedClass: function(){_this.setClass++}
				}, null);
			},
			runTest: function(){
				if (!dojo.isIE) return;
				tests.assertEqual(this.widget, this.arg);
				tests.assertEqual([["webmap.widget.MapControl.resized", 
					this.widget, "_resize"]], 
					this.widget._subscriptions);
				tests.assertEqual([[document.body, "beforeunload", 
					dojo.toJson(function(){_this.destroy();})]], 
					this.widget._subscriptions);
				tests.assertEqual(1, this.calls);
				tests.assertEqual(1, this.resizes);
				tests.assertEqual(0, this.setClass);
			},
			tearDown: function(){
				if (!dojo.isIE) return;
				webmap.widget.DockControl.superclass.postCreate.call = this.call;
				dojo.connect = this.connect;
				dojo.subscribe = this.subscribe;
				this.widget.destroy();
			}
		},
		{
			name: "postCreate_isUndocked_true_ie",
			widget: null,
			call: webmap.widget.DockControl.superclass.postCreate.call,
			calls: 0,
			arg: null,
			resizes: 0,
			setClass: 0,
			subscribe: dojo.subscribe,
			connect: dojo.connect,
			setUp: function(){
				if (!dojo.isIE) return;
				tests.Util.resetDom();
				window.isUndocked = false;
				var _this = this;
				//TODO: GO BACK TO OTHER TESTS AND USE THIS METHOD FOR TESTING SUBSCRIPTIONS
				dojo.subscribe = function(msg,scope,func){return [msg,scope,func];};
				dojo.connect = function(obj,evt,func){return [msg,scope,dojo.toJson(func)];};
				webmap.widget.DockControl.superclass.postCreate.call = function(arg){
					_this.calls++;
					_this.arg = arg;
				};
				this.widget = new webmap.widget.DockControl({
					_resize: function(){_this.resizes++;},
					_setUndockedClass: function(){_this.setClass++}
				}, null);
			},
			runTest: function(){
				if (!dojo.isIE) return;
				tests.assertEqual(this.widget, this.arg);
				tests.assertEqual([["webmap.widget.MapControl.resized", 
					this.widget, "_resize"]], 
					this.widget._subscriptions);
				tests.assertEqual([[document.body, "beforeunload", 
					dojo.toJson(function(){_this.destroy();})]], 
					this.widget._subscriptions);
				tests.assertEqual(1, this.calls);
				tests.assertEqual(1, this.resizes);
				tests.assertEqual(1, this.setClass);
			},
			tearDown: function(){
				if (!dojo.isIE) return;
				webmap.widget.DockControl.superclass.postCreate.call = this.call;
				dojo.connect = this.connect;
				dojo.subscribe = this.subscribe;
				this.widget.destroy();
			}
		},
		{
			name: "destroy",
			widget: null,
			count: 0,
			setUp: function(){
				tests.Util.resetDom();
				doDockControlSetup();
				var _this = this;
				window.undockedWin = {close: function(){_this.count++;}};
				this.widget = new webmap.widget.DockControl({}, div);
			},
			runTest: function(){
				this.widget.destroy();
				tests.assertEqual(1, this.count);
				tests.assertEqual(null, window.undockeWin);
				doDockControlSetup();
				this.widget = new webmap.widget.DockControl({}, div);
				tests.assertEqual(1, this.count);
				tests.assertEqual(null, window.undockeWin);
			}
		},
		{
			name: "_resize",
			widget: null,
			getViewport: dijit.getViewport,
			setUp: function(){
				tests.Util.resetDom();
				doDockControlSetup();
				dijit.getViewport = function(){return{w:800};};
				//resize is called after widget creation
				this.widget = new webmap.widget.DockControl({}, div);
			},
			runTest: function(){
				//width is specified by css
				tests.assertEqual(782, dojo.style(this.widget.buttons[0], "left"));
			},
			tearDown: function(){
				this.widget.destroy();
				dijit.getViewport = this.getViewport;
			}
		},
		{
			name: "_setUndockedClass",
			widget: null,
			setUp: function(){
				tests.Util.resetDom();
				doDockControlSetup();
				this.widget = new webmap.widget.DockControl({}, div);
			},
			runTest: function(){
				tests.assertEqual("webmapUnDockControl", this.widget.buttons[0].className);
				tests.assertEqual("Undock map window", this.widget.buttons[0].title);
				tests.assertEqual("Undock map window", this.widget.buttons[0].alt);
				this.widget._setUndockedClass();
				tests.assertEqual("webmapReDockControl", this.widget.buttons[0].className);
				tests.assertEqual("Re-dock map window", this.widget.buttons[0].title);
				tests.assertEqual("Re-dock map window", this.widget.buttons[0].alt);
			},
			tearDown: function(){
				this.widget.destroy();
			}
		},
		{
			name: "_doMouseOver_doMouseOut",
			widget: null,
			setUp: function(){
				tests.Util.resetDom();
				doDockControlSetup();
				this.widget = new webmap.widget.DockControl({}, div);
			},
			runTest: function(){
				tests.assertEqual("webmapUnDockControl", this.widget.buttons[0].className);
				this.widget._doMouseOver({target:this.widget.buttons[0]});
				tests.assertEqual("webmapUnDockControl webmapUnDockControlHover", this.widget.buttons[0].className);
				this.widget._doMouseOut({target:this.widget.buttons[0]});
				tests.assertEqual("webmapUnDockControl", this.widget.buttons[0].className);
			},
			tearDown: function(){
				this.widget.destroy();
			}
		},
		{
			name: "_doClick_isUndocked_true",
			widget: null,
			close: window.close,
			closes: 0,
			setUp: function(){
				tests.Util.resetDom();
				doDockControlSetup();
				var _this = this;
				window.close = function(){_this.closes++;};
				window.isUndocked = true;
				this.widget = new webmap.widget.DockControl({}, div);
			},
			runTest: function(){
				this.widget._doClick();
				tests.assertEqual(1, this.closes);
			},
			tearDown: function(){
				window.isUndocked = false;
				this.widget.destroy();
				window.close = this.close;
			}
		},
		{
			name: "_doClick_isUndocked_false",
			widget: null,
			close: window.close,
			open: window.open,
			closes: 0,
			opens: 0,
			openArgs: null,
			getViewport: dijit.getViewport,
			undockedWin: {isUndocked:false, close: function(){}},
			style: dojo.style,
			styleArgs: null,
			styles: 0,
			setUp: function(){
				tests.Util.resetDom();
				doDockControlSetup();
				var _this = this;
				window.close = function(){_this.closes++;};
				window.open = function(a0,a1,a2){
					_this.opens++;
					_this.openArgs = [a0,a1,a2];
					return _this.undockedWin;
				};
				dijit.getViewport = function(){return {w:800,h:600};};
				this.widget = new webmap.widget.DockControl({}, div);
				dojo.style = function(a0,a1,a2){
					_this.styles++;
					_this.styleArgs = [a0,a1,a2];
				};
			},
			runTest: function(){
				this.widget._doClick();
				tests.assertEqual(0, this.closes);
				var body = document.body;
				tests.assertTrue(body.className.indexOf("undocked") > -1);
				for (var i = 0; i < body.childNodes.length; i++)
					tests.assertTrue(body.childNodes[i].className.indexOf(" undocked") > -1);
				tests.assertEqual(1, this.opens);
				tests.assertEqual(["map.html", "webmapUndocked", webmap.config.undockedProperties], 
					this.openArgs);
				tests.assertEqual(this.undockedWin, window.undockedWin);
				tests.assertTrue(this.undockedWin.isUndocked);
				if (!dojo.isIE){
					tests.assertEqual(1, this.styles);
					var size = dijit.getViewport();
					tests.assertEqual([document.body, "backgroundPosition", 
						(Math.round(size.w/2) - 72) + "px " + (Math.round(size.h/2) - 33) + "px"], 
						this.styleArgs);
				}
			},
			tearDown: function(){
				this.widget.destroy();
				dojo.removeClass(document.body, "undocked");
				dijit.getViewport = this.getViewport;
				window.close = this.close;
				window.open = this.open;
				dojo.style = this.style;
				window.undockedWin = null;
			}
		}
	]
);