dojo.provide("tests.webmap.widget.MapEventControl");

dojo.require("doh.runner");
dojo.require("webmap.widget.MapEventControl");
dojo.require("tests.Util");
dojo.require("webmap.MapManager");

var div;
function doMapEventControlSetup(){
	tests.Util.resetDom();
	div = document.createElement("div");
	document.body.appendChild(div);
	webmap.undockedWin = null;
}

doh.register("tests.webmap.widget.MapEventControl", 
	[	
		{
			name: "postCreate_default_tool",
			setUp: function(){
				doMapEventControlSetup();
			},
			runTest: function(){return;
				var c = new webmap.widget.MapEventControl({}, div);
				tests.assertEqual(2, c._subscriptions.length);
				tests.assertEqual(3, c._connections.length);
				tests.assertEqual(webmap.config.defaultTool, c._tool);
				tests.assertEqual("none", dojo.style(c.domNode, "display"));
			}
		},
		{
			name: "postCreate_other_tool",
			setUp: function(){
				doMapEventControlSetup();
			},
			runTest: function(){
				var c = new webmap.widget.MapEventControl({_tool:"zoomIn"}, div);
				tests.assertEqual(2, c._subscriptions.length);
				tests.assertEqual(3, c._connections.length);
				tests.assertEqual("zoomIn", c._tool);
				tests.assertEqual("block", dojo.style(c.domNode, "display"));
				var url = tests.Util.getAbsolutePath(c.imagePath + "/zoomIn.cur");
				tests.assertEqual("url(" + url + "), -moz-zoom-in", dojo.style(c.domNode, "cursor"));
				c.destroy();
			}
		},
		{
			name: "postCreate_null_tool",
			setUp: function(){
				doMapEventControlSetup();
			},
			runTest: function(){
				var c = new webmap.widget.MapEventControl({_tool:null}, div);
				tests.assertEqual(2, c._subscriptions.length);
				tests.assertEqual(3, c._connections.length);
				tests.assertEqual(null, c._tool);
				tests.assertEqual("auto", dojo.style(c.domNode, "cursor"));
				tests.assertEqual("block", dojo.style(c.domNode, "display"));
				c.destroy();
			}
		},
		{
			name: "_resize",
			setUp: function(){
				doMapEventControlSetup();
			},
			runTest: function(){
				var c = new webmap.widget.MapEventControl({}, div);
				window.undockedWin = "not null";
				dojo.style(c.domNode, "width", "600px");
				dojo.style(c.domNode, "height", "400px");
				c._resize({w:800, h:600});
				tests.assertEqual(600, dojo.style(c.domNode, "width"));
				tests.assertEqual(400, dojo.style(c.domNode, "height"));
				window.undockedWin = null;
				c._resize({w:800, h:600});
				tests.assertEqual(800, dojo.style(c.domNode, "width"));
				tests.assertEqual(600, dojo.style(c.domNode, "height"));
				c.destroy();
			}
		},
		{
			name: "_setTool_dormant",
			setUp: function(){
				doMapEventControlSetup();
			},
			runTest: function(){
				var c = new webmap.widget.MapEventControl({}, div);
				window.undockedWin = "not null";
				var toolBefore = c._tool;
				c._setTool({tool:"randomTool"});
				tests.assertTrue("randomTool" != toolBefore);
				tests.assertEqual(toolBefore, c._tool);
				tests.assertEqual("none", dojo.style(c.domNode, "display"));
				window.undockedWin = null;
				c.destroy();
			}
		},
		{
			name: "_setTool_zoomIn",
			setUp: function(){
				doMapEventControlSetup();
			},
			runTest: function(){
				var c = new webmap.widget.MapEventControl({}, div);				
				window.undockedWin = null;
				var toolBefore = c._tool;
				c._setTool({tool:"zoomIn"});
				tests.assertTrue("zoomIn" != toolBefore);
				tests.assertEqual("zoomIn", c._tool);
				var url = tests.Util.getAbsolutePath(c.imagePath + "/zoomIn.cur");
				tests.assertEqual("url(" + url + "), -moz-zoom-in", dojo.style(c.domNode, "cursor"));
				tests.assertEqual("block", dojo.style(c.domNode, "display"));
				c.destroy();
			}
		},
		{
			name: "_setTool_identify",
			setUp: function(){
				doMapEventControlSetup();
			},
			runTest: function(){
				var c = new webmap.widget.MapEventControl({_tool:"zoomIn"}, div);				
				window.undockedWin = null;
				var toolBefore = c._tool;
				c._setTool({tool:"identify"});
				tests.assertEqual("zoomIn", toolBefore);
				tests.assertEqual("zoomIn", c._tool);
				var url = tests.Util.getAbsolutePath(c.imagePath + "/zoomIn.cur");
				tests.assertEqual("url(" + url + "), -moz-zoom-in", dojo.style(c.domNode, "cursor"));
				tests.assertEqual("none", dojo.style(c.domNode, "display"));
				c.destroy();
			}
		},
		{
			name: "_getCursor",
			setUp: function(){
				doMapEventControlSetup();
			},
			runTest: function(){
				var c = new webmap.widget.MapEventControl({}, div);				
				tests.assertEqual("url(" + c.imagePath + "zoomIn.cur), -moz-zoom-in", c._getCursor("zoomIn"));
				tests.assertEqual("url(" + c.imagePath + "zoomOut.cur), -moz-zoom-out", c._getCursor("zoomOut"));
				tests.assertEqual("url(" + c.imagePath + "pan.cur), -moz-grab", c._getCursor("pan"));
				tests.assertEqual("url(" + c.imagePath + "select.cur), copy", c._getCursor("select"));
				tests.assertEqual("url(" + c.imagePath + "identify.cur), crosshair", c._getCursor("identify"));
				tests.assertEqual("url(" + c.imagePath + "help.cur), help", c._getCursor("help"));
				tests.assertEqual("url(" + c.imagePath + "grabbing.cur), -moz-grabbing", c._getCursor("grabbing"));
				c.destroy();
			}
		},
		{
			name: "_doMouseDown_dormant",
			setUp: function(){
				doMapEventControlSetup();
			},
			runTest: function(){
				var c = new webmap.widget.MapEventControl({_tool:"zoomIn"}, div);	
				c._startX = "startX";			
				c._startY = "startY";			
				c._actionX = "actionX";			
				c._actionY = "actionY";		
				c._actionW = "actionW";			
				c._actionH = "actionH";		
				window.undockedWin = "not null";
				var getArgs = 0;
				c.countGetArgs = function(){getArgs++;};
				var conn = dojo.connect(c, "_getArgs", c, "countGetArgs");
				var pubs = 0;
				c.countPubs = function(){pubs++;};
				var s = dojo.subscribe("webmap.widget.MapEventControl.mouseDown", c, "countPubs");
				c._doMouseDown({layerX:100, layerY:200});
				tests.assertEqual("startX", c._startX);
				tests.assertEqual("startY", c._startY);
				tests.assertEqual("actionX", c._actionX);
				tests.assertEqual("actionY", c._actionY);
				tests.assertEqual("actionW", c._actionW);
				tests.assertEqual("actionH", c._actionH);
				tests.assertFalse(c._drag);
				tests.assertEqual(0, getArgs);
				tests.assertEqual(0, pubs);
				window.undockedWin = null;
			}
		},
		{
			name: "_doMouseDown",
			setUp: function(){
				doMapEventControlSetup();
			},
			runTest: function(){
				var c = new webmap.widget.MapEventControl({_tool:"zoomIn"}, div);	
				c._doMouseDown({layerX:200, layerY:100});
				tests.assertEqual(200, c._startX);
				tests.assertEqual(100, c._startY);
				tests.assertEqual(200, c._actionX);
				tests.assertEqual(100, c._actionY);
				tests.assertEqual(0, c._actionW);
				tests.assertEqual(0, c._actionH);
				tests.assertTrue(c._drag);
				var url = tests.Util.getAbsolutePath(c.imagePath + "/zoomIn.cur");
				tests.assertEqual("url(" + url + "), -moz-zoom-in", dojo.style(c.domNode, "cursor"));
				c._tool = "pan";
				c._drag = false;
				c._doMouseDown({layerX:100, layerY:200});
				tests.assertEqual(100, c._startX);
				tests.assertEqual(200, c._startY);
				tests.assertEqual(100, c._actionX);
				tests.assertEqual(200, c._actionY);
				tests.assertEqual(0, c._actionW);
				tests.assertEqual(0, c._actionH);
				tests.assertTrue(c._drag);
				var url = tests.Util.getAbsolutePath(c.imagePath + "/grabbing.cur");
				tests.assertEqual("url(" + url + "), -moz-grabbing", dojo.style(c.domNode, "cursor"));
				c.destroy();
			}
		},
		{
			name: "_doMouseDown_publish_event",
			setUp: function(){
				doMapEventControlSetup();
			},
			runTest: function(){
				var c = new webmap.widget.MapEventControl({_tool:"zoomIn"}, div);	
				var getArgs = 0;
				c.countGetArgs = function(){getArgs++;};
				var conn = dojo.connect(c, "_getArgs", c, "countGetArgs");
				var pubs = 0;
				c.countPubs = function(){pubs++;};
				var s = dojo.subscribe("webmap.widget.MapEventControl.mouseDown", c, "countPubs");
				c._doMouseDown({layerX:200, layerY:100});
				tests.assertEqual(1, getArgs);
				tests.assertEqual(1, pubs);
				c.destroy();
				dojo.disconnect(conn);
				dojo.unsubscribe(s);
			}
		},
		{
			name: "_doMouseMove_dormant",
			setUp: function(){
				doMapEventControlSetup();
			},
			runTest: function(){
				var c = new webmap.widget.MapEventControl({_tool:"zoomIn"}, div);	
				c._startX = "startX";			
				c._startY = "startY";			
				c._actionX = "actionX";			
				c._actionY = "actionY";		
				c._actionW = "actionW";			
				c._actionH = "actionH";		
				window.undockedWin = "not null";
				var getArgs = 0;
				c.countGetArgs = function(){getArgs++;};
				var conn = dojo.connect(c, "_getArgs", c, "countGetArgs");
				var pubs = 0;
				c.countPubs = function(){pubs++;};
				var s = dojo.subscribe("webmap.widget.MapEventControl.mouseDown", c, "countPubs");
				c._doMouseMove({layerX:100, layerY:200});
				tests.assertEqual("startX", c._startX);
				tests.assertEqual("startY", c._startY);
				tests.assertEqual("actionX", c._actionX);
				tests.assertEqual("actionY", c._actionY);
				tests.assertEqual("actionW", c._actionW);
				tests.assertEqual("actionH", c._actionH);
				tests.assertEqual(0, getArgs);
				tests.assertEqual(0, pubs);
				window.undockedWin = null;
			}
		},
		{
			name: "_doMouseMove_not_dragging",
			setUp: function(){
				doMapEventControlSetup();
			},
			runTest: function(){
				var c = new webmap.widget.MapEventControl({_tool:"zoomIn"}, div);	
				c._startX = "startX";			
				c._startY = "startY";			
				c._actionX = "actionX";			
				c._actionY = "actionY";		
				c._actionW = "actionW";			
				c._actionH = "actionH";	
				var getArgs = 0;
				c.countGetArgs = function(){getArgs++;};
				var conn = dojo.connect(c, "_getArgs", c, "countGetArgs");
				var pubs = 0;
				c.countPubs = function(){pubs++;};
				var s = dojo.subscribe("webmap.widget.MapEventControl.mouseDown", c, "countPubs");
				c._doMouseMove({layerX:100, layerY:200});
				tests.assertEqual("startX", c._startX);
				tests.assertEqual("startY", c._startY);
				tests.assertEqual("actionX", c._actionX);
				tests.assertEqual("actionY", c._actionY);
				tests.assertEqual("actionW", c._actionW);
				tests.assertEqual("actionH", c._actionH);
				tests.assertEqual(0, getArgs);
				tests.assertEqual(0, pubs);
			}
		},
		{
			name: "_doMouseMove_dragging_positive_deltas",
			setUp: function(){
				doMapEventControlSetup();
			},
			runTest: function(){
				var c = new webmap.widget.MapEventControl({_tool:"zoomIn"}, div);	
				c._drag = true;
				c._delta = {};
				c._startX = 100;			
				c._startY = 50;			
				c._actionX = 100;			
				c._actionY = 50;		
				c._actionW = 0;			
				c._actionH = 0;	
				c._doMouseMove({layerX:200, layerY:400});
				tests.assertEqual({x:100, y:350}, c._delta);
				tests.assertEqual(100, c._actionX);
				tests.assertEqual(50, c._actionY);
				tests.assertEqual(100, c._actionW);
				tests.assertEqual(350, c._actionH);
			}
		},
		{
			name: "_doMouseMove_dragging_negative_deltas",
			setUp: function(){
				doMapEventControlSetup();
			},
			runTest: function(){
				var c = new webmap.widget.MapEventControl({_tool:"zoomIn"}, div);	
				c._drag = true;
				c._delta = {};
				c._startX = 200;			
				c._startY = 500;			
				c._actionX = 200;			
				c._actionY = 500;		
				c._actionW = 0;			
				c._actionH = 0;	
				c._doMouseMove({layerX:100, layerY:400});
				tests.assertEqual({x:-100, y:-100}, c._delta);
				tests.assertEqual(100, c._actionX);
				tests.assertEqual(400, c._actionY);
				tests.assertEqual(100, c._actionW);
				tests.assertEqual(100, c._actionH);
			}
		},
		{
			name: "_doMouseMove_publish_event",
			setUp: function(){
				doMapEventControlSetup();
			},
			runTest: function(){
				var c = new webmap.widget.MapEventControl({_tool:"zoomIn"}, div);	
				c._drag = true;
				c._delta = {};
				c._startX = 200;			
				c._startY = 500;			
				c._actionX = 200;			
				c._actionY = 500;		
				c._actionW = 0;			
				c._actionH = 0;	
				var getArgs = 0;
				c._getArgs = function(){getArgs++;};
				var pubs = 0;
				var s = dojo.subscribe("webmap.widget.MapEventControl.dragging", function(){pubs++;});
				c._doMouseMove({layerX:200, layerY:100});
				//tests.assertEqual(1, getArgs);
				//tests.assertEqual(1, pubs);
				c.destroy();
				dojo.unsubscribe(s);
			}
		},
		{
			name: "_doMouseUp_dormant",
			setUp: function(){
				doMapEventControlSetup();
			},
			runTest: function(){
				var c = new webmap.widget.MapEventControl({_tool:"zoomIn"}, div);	
				window.undockedWin = "not null";
				c._drag = "dormant";
				var getArgs = 0;
				c._getArgs = function(){getArgs++;};
				var pubs = 0;
				var countPubs = function(){pubs++;};
				var s = dojo.subscribe("webmap.widget.MapEventControl.dragging", countPubs);
				c._doMouseUp();
				tests.assertEqual("dormant", c._drag);
				tests.assertEqual(0, getArgs);
				tests.assertEqual(0, pubs);
				window.undockedWin = null;
			}
		},
		{
			name: "_doMouseUp",
			setUp: function(){
				doMapEventControlSetup();
			},
			runTest: function(){
				var c = new webmap.widget.MapEventControl({_tool:"zoomIn"}, div);	
				c._drag = true;
				c._doMouseUp();
				var url = tests.Util.getAbsolutePath(c.imagePath + "/zoomIn.cur");
				tests.assertEqual("url(" + url + "), -moz-zoom-in", dojo.style(c.domNode, "cursor"));
				tests.assertFalse(c._drag);
				c._tool = "pan";
				c._drag = true;
				c._doMouseUp();
				var url = tests.Util.getAbsolutePath(c.imagePath + "/pan.cur");
				tests.assertEqual("url(" + url + "), -moz-grab", dojo.style(c.domNode, "cursor"));
				tests.assertFalse(c._drag);
				c.destroy();
			}
		},
		{
			name: "_doMouseUp_publish_event",
			setUp: function(){
				doMapEventControlSetup();
			},
			runTest: function(){
				var c = new webmap.widget.MapEventControl({_tool:"zoomIn"}, div);	
				var getArgs = 0;
				c.countGetArgs = function(){getArgs++;};
				var conn = dojo.connect(c, "_getArgs", c, "countGetArgs");
				var pubs = 0;
				c.countPubs = function(){pubs++;};
				var s = dojo.subscribe("webmap.widget.MapEventControl.mouseUp", c, "countPubs");
				c._doMouseUp();
				tests.assertEqual(1, getArgs);
				tests.assertEqual(1, pubs);
				c.destroy();
				dojo.disconnect(conn);
				dojo.unsubscribe(s);
			}
		},
		{
			name: "_getArgs",
			setUp: function(){
				doMapEventControlSetup();
			},
			runTest: function(){
				var c = new webmap.widget.MapEventControl({_tool:"zoomIn"}, div);
				c._tool = "tool";
				c._startX = "startX";
				c._startY = "startY";
				c.delta = "delta";
				c._actionX = "actionX";
				c._actionY = "actionY";
				c._actionW = "actionW";
				c._actionH = "actionH";
				var args = c._getArgs();
				tests.assertEqual(args.tool, c._tool);
				tests.assertEqual(args.startX, c._startX);
				tests.assertEqual(args.startY, c._startY);
				tests.assertEqual(args.delta, c._delta);
				tests.assertEqual(args.l, c._actionX);
				tests.assertEqual(args.t, c._actionY);
				tests.assertEqual(args.w, c._actionW);
				tests.assertEqual(args.h, c._actionH);
				c.destroy();
			}
		},
		{
			name: "MapEventArgs_getOrigin",
			setUp: function(){
				doMapEventControlSetup();
			},
			runTest: function(){
				var args = new webmap.widget.MapEventArgs();
				args.l = 10;
				args.t = 20;
				var expected = new gov.nyc.doitt.gis.service.map.domain.ImagePoint();
				expected.x = 10;
				expected.y = 20;
				tests.assertEqual(expected, args.getOrigin());
			}
		},
		{
			name: "MapEventArgs_getEnvelope",
			setUp: function(){
				doMapEventControlSetup();
			},
			runTest: function(){
				var args = new webmap.widget.MapEventArgs();
				args.l = 10;
				args.t = 20;
				args.w = 100;
				args.h = 200;
				var expected = new gov.nyc.doitt.gis.service.map.domain.ImageEnvelope();
				expected.minX = 10;
				expected.minY = 20;
				expected.maxX = 110;
				expected.maxY = 220;
				tests.assertEqual(expected, args.getEnvelope());
			}
		},
		{
			name: "MapEventArgs_getFromPoint",
			setUp: function(){
				doMapEventControlSetup();
			},
			runTest: function(){
				var args = new webmap.widget.MapEventArgs();
				args.startX = 10;
				args.startY = 20;
				var expected = new gov.nyc.doitt.gis.service.map.domain.ImagePoint();
				expected.x = 10;
				expected.y = 20;
				tests.assertEqual(expected, args.getFromPoint());
			}
		},
		{
			name: "MapEventArgs_getToPoint",
			setUp: function(){
				doMapEventControlSetup();
			},
			runTest: function(){
				var args = new webmap.widget.MapEventArgs();
				args.startX = 10;
				args.startY = 20;
				args.delta = {x:100, y:200};
				var expected = new gov.nyc.doitt.gis.service.map.domain.ImagePoint();
				expected.x = 110;
				expected.y = 220;
				tests.assertEqual(expected, args.getToPoint());
			}
		},
		{
			name: "MapEventArgs_isPointClick",
			setUp: function(){
				doMapEventControlSetup();
			},
			runTest: function(){
				var args = new webmap.widget.MapEventArgs();
				args.w = 10;
				args.h = 10;
				tests.assertTrue(args.isPointClick());
				args.w = 16;
				args.h = 16;
				tests.assertFalse(args.isPointClick());
				args.w = 20;
				args.h = 10;
				tests.assertFalse(args.isPointClick());
				args.w = 10;
				args.h = 20;
				tests.assertFalse(args.isPointClick());
				args.w = 16;
				args.h = 10;
				tests.assertFalse(args.isPointClick());
				args.w = 16;
				args.h = 20;
				tests.assertFalse(args.isPointClick());
			}
		}
	]
);