dojo.provide("tests.webmap.widget.Editor");

dojo.require("doh.runner");
dojo.require("webmap.widget.Editor");
dojo.require("tests.Util");
dojo.require("dijit.Toolbar");

var div;
var tb;
var tbw;
function doEditorSetup(){
	tests.Util.resetDom();
	div = document.createElement("div");
	document.body.appendChild(div);
	tb = document.createElement("div");
	document.body.appendChild(tb);
	tb.id = "tb";
	tbw = new dijit.Toolbar({}, tb);
}
doh.register("tests.webmap.widget.Editor", 
	[
		{
			name: "postCreate_set_toolbar_by_id",
			widget: null,
			call: webmap.widget.Editor.superclass.postCreate.call,
			callArgs: [],
			style: dojo.style,
			styleArgs: [],
			setUp: function(){return;
				doEditorSetup();
				var _this = this;
				webmap.widget.Editor.superclass.postCreate.call = function(a){
					_this.callArgs.push(a);
				};
				dojo.style = function(a0,a1,a2){
					_this.styleArgs.push([a0,a1,a2]);
				};
				this.widget = new webmap.widget.Editor({toolbar:"tb",
					document: {body:"body"}
				},div);
			},
			runTest: function(){return;
				tests.assertEqual(tbw, this.widget.toolbar);
				tests.assertEqual([this.widget], this.callArgs);
				tests.assertEqual([[this.widget.document.body,"margin","5px"]], this.styleArgs);
			},
			tearDown: function(){return;
				this.widget.destroy();
				dojo.style = this.style;
				webmap.widget.Editor.superclass.postCreate.call = this.call;
			}
		},
		{
			name: "postCreate_set_toolbar_by_dom",
			widget: null,
			call: webmap.widget.Editor.superclass.postCreate.call,
			callArgs: [],
			style: dojo.style,
			styleArgs: [],
			setUp: function(){
				doEditorSetup();
				var _this = this;
				webmap.widget.Editor.superclass.postCreate.call = function(a){
					_this.callArgs.push(a);
				};
				dojo.style = function(a0,a1,a2){
					_this.styleArgs.push([a0,a1,a2]);
				};
				this.widget = new webmap.widget.Editor({toolbar:tbw.domNode,
					document: {body:"body"}
				},div);
			},
			runTest: function(){
				tests.assertEqual(tbw, this.widget.toolbar);
				tests.assertEqual([this.widget], this.callArgs);
				tests.assertEqual([[this.widget.document.body,"margin","5px"]], this.styleArgs);
			},
			tearDown: function(){
				this.widget.destroy();
				dojo.style = this.style;
				webmap.widget.Editor.superclass.postCreate.call = this.call;
			}
		},
		{
			name: "postCreate_set_toolbar_by_widget",
			widget: null,
			call: webmap.widget.Editor.superclass.postCreate.call,
			callArgs: [],
			style: dojo.style,
			styleArgs: [],
			setUp: function(){
				doEditorSetup();
				var _this = this;
				webmap.widget.Editor.superclass.postCreate.call = function(a){
					_this.callArgs.push(a);
				};
				dojo.style = function(a0,a1,a2){
					_this.styleArgs.push([a0,a1,a2]);
				};
				this.widget = new webmap.widget.Editor({toolbar:tbw,
					document: {body:"body"}
				},div);
			},
			runTest: function(){
				tests.assertEqual(tbw, this.widget.toolbar);
				tests.assertEqual([this.widget], this.callArgs);
				tests.assertEqual([[this.widget.document.body,"margin","5px"]], this.styleArgs);
			},
			tearDown: function(){
				this.widget.destroy();
				dojo.style = this.style;
				webmap.widget.Editor.superclass.postCreate.call = this.call;
			}
		},
		{
			name: "onClick",
			widget: null,
			onClick: webmap.widget.Editor.superclass.onClick.call,
			clickArgs: [],
			activates: 0,
			setUp: function(){
				doEditorSetup();
				var _this = this;
				webmap.widget.Editor.superclass.onClick.call = function(a){
					_this.clickArgs.push(a);
				};
				this.widget = new webmap.widget.Editor({toolbar:tbw,
					activate: function(){_this.activates++;}
				},div);
			},
			runTest: function(){
				this.widget.onClick();
				tests.assertEqual([this.widget], this.clickArgs);
				tests.assertEqual(1, this.activates);
			},
			tearDown: function(){
				this.widget.destroy();
				webmap.widget.Editor.superclass.onClick.call = this.onClick;
			}
		},
		{
			name: "setBackgroundColor_is_calloutbox",
			widget: null,
			label: null,
			style: dojo.style,
			byId: dijit.byId,
			idArgs: [],
			styleArgs: [],
			isLblArgs: [],
			clrArgs: [],
			setUp: function(){
				doEditorSetup();
				var _this = this;
				this.label = {setPointerBackgroundColor:
					function(c){_this.clrArgs.push(c);}};
				this.widget = new webmap.widget.Editor({toolbar:tbw,
					_isLabelCalloutBox: function(a){_this.isLblArgs.push(a);return true;},
					document:{body:"body"}
				},div);
				dojo.style = function(a0,a1,a2){
					_this.styleArgs.push([a0,a1,a2]);
				};
				dijit.byId = function(id){
					_this.idArgs.push(id);
					return _this.label;
				};
			},
			runTest: function(){
				this.widget.setBackgroundColor("a color");
				tests.assertEqual([[this.widget.document.body,"backgroundColor","a color"]], this.styleArgs);
				tests.assertEqual([this.widget.id.replace(/\.editor/, "")], this.idArgs);
				tests.assertEqual([this.label], this.isLblArgs);
				tests.assertEqual(["a color"], this.clrArgs);
			},
			tearDown: function(){
				this.widget.destroy();
				dojo.style = this.style;
				dijit.byId = this.byId;
			}
		},
		{
			name: "setBackgroundColor_not_calloutbox",
			widget: null,
			label: null,
			style: dojo.style,
			byId: dijit.byId,
			idArgs: [],
			styleArgs: [],
			isLblArgs: [],
			clrArgs: [],
			setUp: function(){
				doEditorSetup();
				var _this = this;
				this.label = {setPointerBackgroundColor:
					function(c){_this.clrArgs.push(c);}};
				this.widget = new webmap.widget.Editor({toolbar:tbw,
					_isLabelCalloutBox: function(a){_this.isLblArgs.push(a);return false;},
					document:{body:"body"}
				},div);
				dojo.style = function(a0,a1,a2){
					_this.styleArgs.push([a0,a1,a2]);
				};
				dijit.byId = function(id){
					_this.idArgs.push(id);
					return _this.label;
				};
			},
			runTest: function(){
				this.widget.setBackgroundColor("a color");
				tests.assertEqual([[this.widget.document.body,"backgroundColor","a color"]], this.styleArgs);
				tests.assertEqual([this.widget.id.replace(/\.editor/, "")], this.idArgs);
				tests.assertEqual([this.label], this.isLblArgs);
				tests.assertEqual([], this.clrArgs);
			},
			tearDown: function(){
				this.widget.destroy();
				dojo.style = this.style;
				dijit.byId = this.byId;
			}
		},
		{
			name: "setBorderColor_is_calloutbox",
			widget: null,
			label: null,
			style: dojo.style,
			byId: dijit.byId,
			idArgs: [],
			styleArgs: [],
			isLblArgs: [],
			clrArgs: [],
			getConts: 0,
			setUp: function(){
				doEditorSetup();
				var _this = this;
				this.label = {setPointerBorderColor:
					function(c){_this.clrArgs.push(c);}};
				this.widget = new webmap.widget.Editor({toolbar:tbw,
					_isLabelCalloutBox: function(a){_this.isLblArgs.push(a);return true;},
					_getContainer: function(){_this.getConts++;return "container"},
					document:{body:"body"}
				},div);
				dojo.style = function(a0,a1,a2){
					_this.styleArgs.push([a0,a1,a2]);
				};
				dijit.byId = function(id){
					_this.idArgs.push(id);
					return _this.label;
				};
			},
			runTest: function(){
				this.widget.setBorderColor("a color");
				tests.assertEqual([["container","borderColor","a color"]], this.styleArgs);
				tests.assertEqual([this.widget.id.replace(/\.editor/, "")], this.idArgs);
				tests.assertEqual(1, this.getConts);
				tests.assertEqual([this.label], this.isLblArgs);
				tests.assertEqual(["a color"], this.clrArgs);
			},
			tearDown: function(){
				this.widget.destroy();
				dojo.style = this.style;
				dijit.byId = this.byId;
			}
		},
		{
			name: "setBorderColor_not_calloutbox",
			widget: null,
			label: null,
			style: dojo.style,
			byId: dijit.byId,
			idArgs: [],
			styleArgs: [],
			isLblArgs: [],
			clrArgs: [],
			getConts: 0,
			setUp: function(){
				doEditorSetup();
				var _this = this;
				this.label = {setPointerBorderColor:
					function(c){_this.clrArgs.push(c);}};
				this.widget = new webmap.widget.Editor({toolbar:tbw,
					_isLabelCalloutBox: function(a){_this.isLblArgs.push(a);return false;},
					_getContainer: function(){_this.getConts++;return "container"},
					document:{body:"body"}
				},div);
				dojo.style = function(a0,a1,a2){
					_this.styleArgs.push([a0,a1,a2]);
				};
				dijit.byId = function(id){
					_this.idArgs.push(id);
					return _this.label;
				};
			},
			runTest: function(){
				this.widget.setBorderColor("a color");
				tests.assertEqual([["container","borderColor","a color"]], this.styleArgs);
				tests.assertEqual([this.widget.id.replace(/\.editor/, "")], this.idArgs);
				tests.assertEqual(1, this.getConts);
				tests.assertEqual([this.label], this.isLblArgs);
				tests.assertEqual([], this.clrArgs);
			},
			tearDown: function(){
				this.widget.destroy();
				dojo.style = this.style;
				dijit.byId = this.byId;
			}
		},
		{
			name: "setBorderWidth_is_calloutbox",
			widget: null,
			label: null,
			style: dojo.style,
			byId: dijit.byId,
			idArgs: [],
			styleArgs: [],
			isLblArgs: [],
			widthArgs: [],
			getConts: 0,
			resizes: 0,
			setUp: function(){
				doEditorSetup();
				var _this = this;
				this.label = {
					setPointerBorderWidth: function(a){_this.widthArgs.push(a);},
					resize: function(){_this.resizes++;}
				};
				this.widget = new webmap.widget.Editor({toolbar:tbw,
					_isLabelCalloutBox: function(a){_this.isLblArgs.push(a);return true;},
					_getContainer: function(){_this.getConts++;return "container"}
				},div);
				dojo.style = function(a0,a1,a2){
					_this.styleArgs.push([a0,a1,a2]);
				};
				dijit.byId = function(id){
					_this.idArgs.push(id);
					return _this.label;
				};
			},
			runTest: function(){
				this.widget.setBorderWidth("a width");
				tests.assertEqual([["container","borderWidth","a widthpx"]], this.styleArgs);
				tests.assertEqual([this.widget.id.replace(/\.editor/, "")], this.idArgs);
				tests.assertEqual(1, this.getConts);
				tests.assertEqual(1, this.resizes);
				tests.assertEqual([this.label], this.isLblArgs);
				tests.assertEqual(["a width"], this.widthArgs);
			},
			tearDown: function(){
				this.widget.destroy();
				dojo.style = this.style;
				dijit.byId = this.byId;
			}
		},
		{
			name: "setBorderWidth_not_calloutbox",
			widget: null,
			label: null,
			style: dojo.style,
			byId: dijit.byId,
			idArgs: [],
			styleArgs: [],
			isLblArgs: [],
			widthArgs: [],
			getConts: 0,
			resizes: 0,
			setUp: function(){
				doEditorSetup();
				var _this = this;
				this.label = {
					setPointerBorderWidth: function(a){_this.widthArgs.push(a);},
					resize: function(){_this.resizes++;}
				};
				this.widget = new webmap.widget.Editor({toolbar:tbw,
					_isLabelCalloutBox: function(a){_this.isLblArgs.push(a);return false;},
					_getContainer: function(){_this.getConts++;return "container"}
				},div);
				dojo.style = function(a0,a1,a2){
					_this.styleArgs.push([a0,a1,a2]);
				};
				dijit.byId = function(id){
					_this.idArgs.push(id);
					return _this.label;
				};
			},
			runTest: function(){
				this.widget.setBorderWidth("a width");
				tests.assertEqual([["container","borderWidth","a widthpx"]], this.styleArgs);
				tests.assertEqual([this.widget.id.replace(/\.editor/, "")], this.idArgs);
				tests.assertEqual(1, this.getConts);
				tests.assertEqual(1, this.resizes);
				tests.assertEqual([this.label], this.isLblArgs);
				tests.assertEqual([], this.widthArgs);
			},
			tearDown: function(){
				this.widget.destroy();
				dojo.style = this.style;
				dijit.byId = this.byId;
			}
		},
		{
			name: "activate",
			widget:null,
			shows:0,
			resets:0,
			setUp: function(){
				doEditorSetup();
				var _this = this;
				this.widget = new webmap.widget.Editor({toolbar:tbw},div);
				this.widget.toolbar.show = function(){_this.shows++;};
				this.widget.toolbar.reset = function(){_this.resets++;};
				this.widget.toolbar.open = false;
				this.widget.toolbar.editor = null;
			},
			runTest: function(){
				this.widget.activate();
				tests.assertEqual(this.widget,this.widget.toolbar.editor);
				tests.assertEqual(1,this.shows);
				tests.assertEqual(1,this.resets);
				this.widget.toolbar.open = true;
				this.widget.toolbar.editor = null;
				this.widget.activate();
				tests.assertEqual(this.widget,this.widget.toolbar.editor);
				tests.assertEqual(1,this.shows);
				tests.assertEqual(2,this.resets);
			},
			tearDown: function(){
				this.widget.destroy();
			}
		},
		{
			name: "getWindow",
			widget:null,
			domNode: null,
			setUp: function(){
				doEditorSetup();
				var _this = this;
				this.widget = new webmap.widget.Editor({toolbar:tbw},div);
				this.domNode = this.widget.domNode;
				this.widget.domNode = {
					parentNode:{
						className:"",
						parentNode:"parentNode"
					}
				};
			},
			runTest: function(){
				tests.assertEqual(this.widget.domNode, this.widget.getWindow());
				this.widget.domNode.parentNode.className = "webmapLabelContent";
				tests.assertEqual(this.widget.domNode.parentNode.parentNode, this.widget.getWindow());
			},
			tearDown: function(){
				this.widget.domNode = this.domNode;
				this.widget.destroy();
			}
		},
		{
			name: "_getContainer",
			widget:null,
			domNode: null,
			setUp: function(){
				doEditorSetup();
				var _this = this;
				this.widget = new webmap.widget.Editor({toolbar:tbw},div);
				this.domNode = this.widget.domNode;
				this.widget.domNode = {
					parentNode:{
						className:"",
						parentNode:"parentNode"
					}
				};
			},
			runTest: function(){
				tests.assertEqual(this.widget.domNode, this.widget._getContainer());
				this.widget.domNode.parentNode.className = "webmapLabelContent";
				tests.assertEqual(this.widget.domNode.parentNode, this.widget._getContainer());
			},
			tearDown: function(){
				this.widget.domNode = this.domNode;
				this.widget.destroy();
			}
		},
		{
			name: "_isLabelCalloutBox",
			widget:null,
			setUp: function(){
				doEditorSetup();
				var _this = this;
				this.widget = new webmap.widget.Editor({toolbar:tbw},div);
			},
			runTest: function(){
				tests.assertTrue(this.widget._isLabelCalloutBox({declaredClass:"webmap.widget.LabelCalloutBox"}));
				tests.assertFalse(this.widget._isLabelCalloutBox({declaredClass:"NOT_webmap.widget.LabelCalloutBox"}));
			},
			tearDown: function(){
				//TODO: should the editor destroy its toolbar?
				tbw.destroy();
				this.widget.destroy();
			}
		}
	]
);