dojo.provide("tests.webmap.widget.Resizeable");

dojo.require("doh.runner");
dojo.require("webmap.widget.Resizeable");
dojo.require("tests.Util");

var div;
var hand;
function doResizeableSetup(){
	tests.Util.resetDom();
	div = document.createElement("div");
	document.body.appendChild(div);
	hand = document.createElement("div");
	div.appendChild(hand);
	dojo.style(div, "width", "200px");
	dojo.style(div, "height", "100px");
	dojo.style(div, "left", "20px");
	dojo.style(div, "top", "10px");
}

doh.register("tests.webmap.widget.Resizeable", 
	[
		{
			name: "startup",
			widget: null,
			connect: dojo.connect,
			connections: [],
			call: webmap.widget.Resizeable.superclass.startup.call,
			callArgs: [],
			resizes:0,
			setUp: function(){
				doResizeableSetup();
				var _this = this;
				dojo.connect = function(a0, a1, a2, a3){
					var c = _this.connect(a0, a1, a2, a3);
					_this.connections.push([a0, a1, a2, a3]);
					return c;
				};
				webmap.widget.Resizeable.superclass.startup.call = function(a){
					_this.callArgs.push(a);
				};
				this.widget = new webmap.widget.Resizeable({
					resize: function(){_this.resizes++;}
				});
			},
			runTest: function(){
				this.widget.startup();
				tests.assertEqual(3, this.widget._connections.length);
				tests.assertEqual([
					[this.widget.domNode, "onmousedown", this.widget, "_doDown"],
					[document, "onmouseup", this.widget, "_doUp"],
					[document, "onmousemove", this.widget, "_doMove"]],
					this.connections
				);
				tests.assertEqual([this.widget], this.callArgs);
				tests.assertEqual(1, this.resizes);
			},
			tearDown: function(){
				webmap.widget.Resizeable.superclass.startup.call = this.call;
				dojo.connect = this.connect;
				this.widget.destroy();
			}
		},
		{
			name: "destroy",
			widget: null,
			resizeDestroy: webmap.widget.Resizeable.superclass.destroy.call,
			resizeDestroyArgs: [],
			layoutDestroy: dijit.layout.LayoutContainer.superclass.destroy.call,
			layoutDestroyArgs: [],
			setUp: function(){
				doResizeableSetup();
				var _this = this;
				webmap.widget.Resizeable.superclass.destroy.call = function(a){
					_this.resizeDestroyArgs.push(a);
				};
				dijit.layout.LayoutContainer.superclass.destroy.call = function(a){
					_this.layoutDestroyArgs.push(a);
				};
				this.widget = new webmap.widget.Resizeable({}, div);
			},
			runTest: function(){
				this.widget.destroy();
				tests.assertEqual([this.widget], this.resizeDestroyArgs);
				tests.assertEqual([this.widget], this.layoutDestroyArgs);
			},
			tearDown: function(){
				webmap.widget.Resizeable.superclass.destroy.call = this.resizeDestroy;
				dijit.layout.LayoutContainer.superclass.destroy.call = this.layoutDestroy;
				this.widget.destroy();
			}
		},
		{
			name: "_getDirection",
			widget: null,
			setUp: function(){
				doResizeableSetup();
				this.widget = new webmap.widget.Resizeable({}, div);
				this.widget.startup();
			},
			runTest: function(){
				var h = this.widget.domNode.offsetHeight;
				var w = this.widget.domNode.offsetWidth;
				tests.assertEqual("nw", this.widget._getDirection({layerX:7, layerY:7}));
				tests.assertEqual("sw", this.widget._getDirection({layerX:7, layerY:h - 7}));
				tests.assertEqual("ne", this.widget._getDirection({layerX:w - 7, layerY:7}));
				tests.assertEqual("se", this.widget._getDirection({layerX:w - 7, layerY:h - 7}));
				tests.assertEqual("n", this.widget._getDirection({layerX:9, layerY:7}));
				tests.assertEqual("s", this.widget._getDirection({layerX:9, layerY:h - 7}));
				tests.assertEqual("w", this.widget._getDirection({layerX:7, layerY:9}));
				tests.assertEqual("e", this.widget._getDirection({layerX:w - 7, layerY:9}));
			},
			tearDown: function(){
				this.widget.destroy();
			}
		},
		{
			name: "_newMover",
			widget: null,
			setUp: function(){
				doResizeableSetup();
				var _this = this;
				this.widget = new webmap.widget.Resizeable({}, div);
			},
			runTest: function(){
				var mover = this.widget._newMover(div, hand);
				tests.assertEqual(this.widget.domNode, mover.node);
				tests.assertEqual(hand, mover.handle);
			},
			tearDown: function(){;
				this.widget.destroy();
			}
		},
		{
			name: "_doDown_resizeable_true_moveable_true_no_mover_isOnHandle_true",
			widget: null,
			evt: {clientX:20, clientY:20, returnValue:true, cancelBubble:false},
			dirArgs: [],
			moverArgs: [],
			onHandleArgs: [],
			moverDownArgs: [],
			stop: dojo.stopEvent,
			stopArgs: [],
			setUp: function(){
				doResizeableSetup();
				var _this = this;
				dojo.stopEvent = function(e){_this.stopArgs.push(e);};
				this.widget = new webmap.widget.Resizeable({
					handle:hand,
					_isOnHandle: function(x,y){_this.onHandleArgs.push([x,y]);return true;},
					_getDirection: function(e){_this.dirArgs.push(e);return "";},
					_newMover: function(n,h){
						_this.moverArgs.push([n,h]);
						return {
							onMouseDown: function(a){_this.moverDownArgs.push(a);}
						};
					}
				}, div);
				this.widget.startup();
			},
			runTest: function(){
				this.widget._doDown(this.evt);
				tests.assertEqual([this.evt], this.dirArgs);
				tests.assertEqual([[this.widget.domNode, this.widget.handle]], this.moverArgs);
				tests.assertEqual([[this.evt.clientX, this.evt.clientY]], this.onHandleArgs);
				tests.assertEqual([this.evt], this.moverDownArgs);
				tests.assertTrue(this.evt.returnValue);
				tests.assertFalse(this.evt.cancelBubble);
				tests.assertEqual([], this.stopArgs);
				tests.assertEqual(null, this.widget._resizeObj);
			},
			tearDown: function(){
				dojo.stopEvent = this.stop;
				this.widget.destroy();
			}
		},
		{
			name: "_doDown_resizeable_true_moveable_true_has_mover_isOnHandle_true",
			widget: null,
			evt: {clientX:20, clientY:20, returnValue:true, cancelBubble:false},
			dirArgs: [],
			moverArgs: [],
			onHandleArgs: [],
			moverDownArgs: [],
			stop: dojo.stopEvent,
			stopArgs: [],
			setUp: function(){
				doResizeableSetup();
				var _this = this;
				dojo.stopEvent = function(e){_this.stopArgs.push(e);};
				this.widget = new webmap.widget.Resizeable({
					handle:hand,
					_mover:{},
					_isOnHandle: function(x,y){_this.onHandleArgs.push([x,y]);return true;},
					_getDirection: function(e){_this.dirArgs.push(e);return "";},
					_newMover: function(n,h){
						_this.moverArgs.push([n,h]);
						return {
							onMouseDown: function(a){_this.moverDownArgs.push(a);}
						};
					}
				}, div);
				this.widget.startup();
			},
			runTest: function(){
				this.widget._doDown(this.evt);
				tests.assertEqual([this.evt], this.dirArgs);
				tests.assertEqual([], this.moverArgs);
				tests.assertEqual([], this.onHandleArgs);
				tests.assertEqual([], this.moverDownArgs);
				tests.assertTrue(this.evt.returnValue);
				tests.assertFalse(this.evt.cancelBubble);
				tests.assertEqual([], this.stopArgs);
				tests.assertEqual(null, this.widget._resizeObj);
			},
			tearDown: function(){
				dojo.stopEvent = this.stop;
				this.widget.destroy();
			}
		},
		{
			name: "_doDown_resizeable_true_moveable_true_has_mover_isOnHandle_false",
			widget: null,
			evt: {clientX:20, clientY:20, returnValue:true, cancelBubble:false},
			dirArgs: [],
			moverArgs: [],
			onHandleArgs: [],
			moverDownArgs: [],
			stop: dojo.stopEvent,
			stopArgs: [],
			moverDestroys: 0,
			setUp: function(){
				doResizeableSetup();
				var _this = this;
				dojo.stopEvent = function(e){_this.stopArgs.push(e);};
				this.widget = new webmap.widget.Resizeable({
					handle:hand,
					_mover:{destroy:function(){_this.moverDestroys++;}},
					_isOnHandle: function(x,y){_this.onHandleArgs.push([x,y]);return false;},
					_getDirection: function(e){_this.dirArgs.push(e);return "direction";},
					_newMover: function(n,h){
						_this.moverArgs.push([n,h]);
						return {
							onMouseDown: function(a){_this.moverDownArgs.push(a);}
						};
					}
				}, div);
				this.widget.startup();
			},
			runTest: function(){
				this.widget._doDown(this.evt);
				tests.assertEqual([this.evt], this.dirArgs);
				tests.assertEqual([], this.moverArgs);
				tests.assertEqual([], this.onHandleArgs);
				tests.assertEqual([], this.moverDownArgs);
				tests.assertEqual(1, this.moverDestroys);
				tests.assertFalse(this.evt.returnValue);
				tests.assertTrue(this.evt.cancelBubble);
				tests.assertEqual([this.evt], this.stopArgs);
				tests.assertEqual({
					dir:"direction",
					grabx:this.evt.clientX,
					graby:this.evt.clientY,
					width:this.widget.domNode.offsetWidth,
					height:this.widget.domNode.offsetHeight,
					left:this.widget.domNode.offsetLeft,
					top:this.widget.domNode.offsetTop
				}, this.widget._resizeObj);
			},
			tearDown: function(){
				dojo.stopEvent = this.stop;
				this.widget.destroy();
			}
		},
		{
			name: "_doDown_resizeable_false_moveable_true_has_mover_isOnHandle_false",
			widget: null,
			evt: {clientX:20, clientY:20, returnValue:true, cancelBubble:false},
			dirArgs: [],
			moverArgs: [],
			onHandleArgs: [],
			moverDownArgs: [],
			stop: dojo.stopEvent,
			stopArgs: [],
			moverDestroys: 0,
			setUp: function(){
				doResizeableSetup();
				var _this = this;
				dojo.stopEvent = function(e){_this.stopArgs.push(e);};
				this.widget = new webmap.widget.Resizeable({
					resizeable:false,
					handle:hand,
					_mover:{destroy:function(){_this.moverDestroys++;}},
					_isOnHandle: function(x,y){_this.onHandleArgs.push([x,y]);return false;},
					_getDirection: function(e){_this.dirArgs.push(e);return "direction";},
					_newMover: function(n,h){
						_this.moverArgs.push([n,h]);
						return {
							onMouseDown: function(a){_this.moverDownArgs.push(a);}
						};
					}
				}, div);
				this.widget.startup();
			},
			runTest: function(){
				this.widget._doDown(this.evt);
				tests.assertEqual([], this.dirArgs);
				tests.assertEqual([], this.moverArgs);
				tests.assertEqual([], this.onHandleArgs);
				tests.assertEqual([], this.moverDownArgs);
				tests.assertEqual(0, this.moverDestroys);
				tests.assertTrue(this.evt.returnValue);
				tests.assertFalse(this.evt.cancelBubble);
				tests.assertEqual([], this.stopArgs);
				tests.assertEqual(null, this.widget._resizeObj);
			},
			tearDown: function(){
				dojo.stopEvent = this.stop;
				this.widget.destroy();
			}
		},
		{
			name: "_doDown_resizeable_false_moveable_true_no_mover_isOnHandle_true",
			widget: null,
			evt: {clientX:20, clientY:20, returnValue:true, cancelBubble:false},
			dirArgs: [],
			moverArgs: [],
			onHandleArgs: [],
			moverDownArgs: [],
			stop: dojo.stopEvent,
			stopArgs: [],
			moverDestroys: 0,
			setUp: function(){
				doResizeableSetup();
				var _this = this;
				dojo.stopEvent = function(e){_this.stopArgs.push(e);};
				this.widget = new webmap.widget.Resizeable({
					resizeable:false,
					handle:hand,
					_isOnHandle: function(x,y){_this.onHandleArgs.push([x,y]);return true;},
					_getDirection: function(e){_this.dirArgs.push(e);return "direction";},
					_newMover: function(n,h){
						_this.moverArgs.push([n,h]);
						return {
							onMouseDown: function(a){_this.moverDownArgs.push(a);}
						};
					}
				}, div);
				this.widget.startup();
			},
			runTest: function(){
				this.widget._doDown(this.evt);
				tests.assertEqual([], this.dirArgs);
				tests.assertEqual([[this.widget.domNode, this.widget.handle]], this.moverArgs);
				tests.assertEqual([[this.evt.clientX, this.evt.clientY]], this.onHandleArgs);
				tests.assertEqual([this.evt], this.moverDownArgs);
				tests.assertEqual(0, this.moverDestroys);
				tests.assertTrue(this.evt.returnValue);
				tests.assertFalse(this.evt.cancelBubble);
				tests.assertEqual([], this.stopArgs);
				tests.assertEqual(null, this.widget._resizeObj);
			},
			tearDown: function(){
				dojo.stopEvent = this.stop;
				this.widget.destroy();
			}
		},
		{
			name: "_doDown_resizeable_false_moveable_true_no_mover_isOnHandle_false",
			widget: null,
			evt: {clientX:20, clientY:20, returnValue:true, cancelBubble:false},
			dirArgs: [],
			moverArgs: [],
			onHandleArgs: [],
			moverDownArgs: [],
			stop: dojo.stopEvent,
			stopArgs: [],
			moverDestroys: 0,
			setUp: function(){
				doResizeableSetup();
				var _this = this;
				dojo.stopEvent = function(e){_this.stopArgs.push(e);};
				this.widget = new webmap.widget.Resizeable({
					resizeable:false,
					handle:hand,
					_isOnHandle: function(x,y){_this.onHandleArgs.push([x,y]);return false;},
					_getDirection: function(e){_this.dirArgs.push(e);return "direction";},
					_newMover: function(n,h){
						_this.moverArgs.push([n,h]);
						return {
							onMouseDown: function(a){_this.moverDownArgs.push(a);}
						};
					}
				}, div);
				this.widget.startup();
			},
			runTest: function(){
				this.widget._doDown(this.evt);
				tests.assertEqual([], this.dirArgs);
				tests.assertEqual([[this.widget.domNode, this.widget.handle]], this.moverArgs);
				tests.assertEqual([[this.evt.clientX, this.evt.clientY]], this.onHandleArgs);
				tests.assertEqual([], this.moverDownArgs);
				tests.assertEqual(0, this.moverDestroys);
				tests.assertTrue(this.evt.returnValue);
				tests.assertFalse(this.evt.cancelBubble);
				tests.assertEqual([], this.stopArgs);
				tests.assertEqual(null, this.widget._resizeObj);
			},
			tearDown: function(){
				dojo.stopEvent = this.stop;
				this.widget.destroy();
			}
		},
		{
			name: "_doDown_resizeable_false_moveable_true_has_mover_isOnHandle_false",
			widget: null,
			evt: {clientX:20, clientY:20, returnValue:true, cancelBubble:false},
			dirArgs: [],
			moverArgs: [],
			onHandleArgs: [],
			moverDownArgs: [],
			stop: dojo.stopEvent,
			stopArgs: [],
			moverDestroys: 0,
			setUp: function(){
				doResizeableSetup();
				var _this = this;
				dojo.stopEvent = function(e){_this.stopArgs.push(e);};
				this.widget = new webmap.widget.Resizeable({
					resizeable:false,
					handle:hand,
					_mover: {destroy:function(){_this.moverDestroys++;}},
					_isOnHandle: function(x,y){_this.onHandleArgs.push([x,y]);return false;},
					_getDirection: function(e){_this.dirArgs.push(e);return "direction";},
					_newMover: function(n,h){
						_this.moverArgs.push([n,h]);
						return {
							onMouseDown: function(a){_this.moverDownArgs.push(a);}
						};
					}
				}, div);
				this.widget.startup();
			},
			runTest: function(){
				this.widget._doDown(this.evt);
				tests.assertEqual([], this.dirArgs);
				tests.assertEqual([], this.moverArgs);
				tests.assertEqual([], this.onHandleArgs);
				tests.assertEqual([], this.moverDownArgs);
				tests.assertEqual(0, this.moverDestroys);
				tests.assertTrue(this.evt.returnValue);
				tests.assertFalse(this.evt.cancelBubble);
				tests.assertEqual([], this.stopArgs);
				tests.assertEqual(null, this.widget._resizeObj);
			},
			tearDown: function(){
				dojo.stopEvent = this.stop;
				this.widget.destroy();
			}
		},
		{
			name: "_doUp",
			widget: null,
			setUp: function(){
				this.widget = new webmap.widget.Resizeable({
					resizeable:false,
					_resizeObj:"resizeObj"
				},div);
			},
			runTest: function(){
				this.widget._doUp();
				tests.assertEqual("resizeObj", this.widget._resizeObj);
				this.widget.resizeable = true;
				this.widget._doUp();
				tests.assertEqual(null, this.widget._resizeObj);
			},
			tearDown: function(){
				this.widget.destroy();
			}
		},
		{
			name: "_doMove_resizeable_true_has_resizeObj_n",
			widget: null,
			evt: {clientX:20, clientY:20, returnValue:true, cancelBubble:false},
			resizeObj: {
				dir:"n",  	
				grabx:null,     
				graby:null,
				width:null,
				height:null,
				left:null,
				top:null
			},
			evt: {clientX:20, clientY:20},
			dirArgs: [],
			resizeArgs: [],
			stop: dojo.stopEvent,
			stopArgs: [],
			setUp: function(){return;
				doResizeableSetup();
				var _this = this;
				dojo.stopEvent = function(e){_this.stopArgs.push(e);};
				this.widget = new webmap.widget.Resizeable({
					resizeable:true,
					_resizeObj:this.resizeObj,
					_getDirection: function(e){_this.dirArgs.push(e);return "n";},
					resize: function(r){_this.resizeArgs.push(r);}
				}, div);
				this.widget.startup();
			},
			runTest: function(){
				console.warn("Write me!"); try{notWritten++;}catch(ignore){}
				return;
				this.widget._doMove(this.evt);
				tests.assertEqual([this.evt], this.dirArgs);
				tests.assertEqual("auto", this.widget._prevCur);
				tests.assertEqual("n-resize", dojo.style(this.widget.domNode, "cursor"));
				tests.assertEqual([{"t": -8, "h": 8}], this.resizeArgs);
				tests.assertEqual([this.evt], this.stopArgs);
			},
			tearDown: function(){return;
				this.widget.destroy();
			}
		},
		{
			name: "_isOnHandle",
			widget: null,
			setUp: function(){
				this.widget = new webmap.widget.Resizeable({},div);
			},
			runTest: function(){
				console.warn("Write me!"); try{notWritten++;}catch(ignore){}
			},
			tearDown: function(){
				this.widget.destroy();
			}
		}
	]
);