dojo.provide("tests.webmap.widget._Control");

dojo.require("doh.runner");
dojo.require("webmap.widget._Control");
dojo.require("tests.Util");

doh.register("tests.webmap.widget._Control", 
	[	
		{
			name: "postMixInProperties",
			setUp: function(){
				tests.Util.resetDom();
			},
			runTest: function(){
				var c = new webmap.widget._Control();
				tests.assertEqual(null, c.templatePath);
				tests.assertEqual(null, c.imagePath);
				tests.assertEqual(null, c._className);
				tests.assertEqual(null, c._subscriptions);
				tests.assertEqual(null, c._connections);
				c.postMixInProperties();
				tests.assertEqual(webmap.config.templatePath + "/" + c._className +".html", c.templatePath);
				tests.assertEqual(c.imagePath, webmap.config.imagePath);
				tests.assertEqual(c._className, "_Control");
				tests.assertEqual(c._subscriptions, new Array());
				tests.assertEqual(c._connections, new Array());
			}
		},
		{
			name: "postCreate",
			setUp: function(){
				tests.Util.resetDom();
			},
			runTest: function(){
				var div1 = document.createElement("div");
				var c1 = new webmap.widget._Control();
				c1.postMixInProperties();
				c1.id = "widget1";
				c1.domNode = div1;
				c1.postCreate();
				tests.assertEqual("widget1", div1.id);
				tests.assertEqual("webmap_Control", div1.className);
				if (dojo.isIE) tests.assertEqual("on", div1.unselectable);
				var div2 = document.createElement("div");
				div2.id = "div2";
				var c2 = new webmap.widget._Control();
				c2.postMixInProperties();
				c1.id = "widget2";
				c2.domNode = div2;
				c2.postCreate();
				tests.assertEqual("div2", div2.id);
			}
		},
		{
			name: "destroy",
			setUp: function(){
				tests.Util.resetDom();
			},
			runTest: function(){
				//TODO: test that connections are removed when there are no subscriptions and visa versa
				var div = document.createElement("div");
				div.id = "testDiv";
				document.body.appendChild(div);
				var c = new webmap.widget._Control();
				c.postMixInProperties();
				c.domNode = div;
				c.postCreate();
				c.someFunction = function(){};
				c.someOtherFunction = function(){};
				var disconnects = 0;
				var countDisconnects = function(){disconnects++;};
				var unsubscribes = 0;
				var countUnsubscribes = function(){unsubscribes++;};
				var conns = new Array();
				conns.push(dojo.connect(dojo, "disconnect", countDisconnects));
				conns.push(dojo.connect(dojo, "unsubscribe", countUnsubscribes));
				c._connections.push(dojo.connect(div, "onmouseover", c, "someFunction"));
				c._connections.push(dojo.connect(div, "onmouseout", c, "someFunction"));
				c._subscriptions.push(dojo.subscribe("someEvent", c, "someOtherFunction"));
				c._subscriptions.push(dojo.subscribe("someOtherEvent", c, "someOtherFunction"));
				c.destroy();
				tests.assertEqual(null, dojo.byId("testDiv"));
				tests.assertEqual(null, c.domNode);
				tests.assertEqual(null, c._connections);
				tests.assertEqual(null, c._subscriptions);
				tests.assertEqual(2, disconnects);
				tests.assertEqual(2, unsubscribes);
				for (var i = 0; i < conns.length; i++)
					dojo.disconnect(conns[i]);
			}
		}
	]
);