module('apps.GridBagLayoutTest').requires('lively.TestFramework', 'apps.GridBagLayout', 'apps.GridBagLayoutMorphs').toRun(function () {

AsyncTestCase.subclass('apps.GridBagLayoutTest.GeneralTest', {
	setUp: function () {
		this.gridMorph = Morph.makeRectangle(pt(100, 100), pt(300,250));
		this.gridMorph.layoutManager = new GridBagLayoutManager(this.gridMorph);
		var world = WorldMorph.current();
		world.addMorph(this.gridMorph);
	},

	tearDown: function () {
		this.gridMorph.remove();
	},
	
    addRect: function () {
		morph = Morph.makeRectangle(pt(0,0), pt(50,30));
		this.gridMorph.addMorph(morph);
		return morph;
	},
	
	addRowAndColumn: function (position) {
		var manager;
		manager = this.gridMorph.layoutManager;
		manager.addRowAt(1);
		manager.addColAt(1);
		manager.relayout();
	},
	
	testInitialization: function () {
		var manager;
		manager = this.gridMorph.layoutManager;
		this.assert(manager.rows.length === 1);
		this.assert(manager.cols.length === 1);
		this.assert(manager.morphs.length === 1);
		this.assert(manager.morphs[0].length === 0);
		this.done();
	},
	
	testAddMorph: function () {
		this.delay(
			function () {
				var manager, morph;
				morph = this.addRect();
				
				manager = this.gridMorph.layoutManager;
				
				this.assert(manager.rows.length === 2);
				this.assert(manager.rows[0].fixed);
				this.assert(!manager.rows[1].fixed);
				this.assert(manager.rows[0].size === 30);

				this.assert(manager.cols.length === 2);
				this.assert(manager.cols[0].size === 50);
				this.assert(manager.cols[0].fixed);
				this.assert(!manager.cols[1].fixed);
				
				this.assert(manager.morphs.length === 2);
				this.assert(manager.morphs[0][0] === morph);
				
				this.assert(morph.gridLayoutSettings !== undefined);
				this.assert(morph.gridLayoutSettings.x === 0);
				this.assert(morph.gridLayoutSettings.y === 0);
				this.assert(morph.gridLayoutSettings.colSpan === 1);
				this.assert(morph.gridLayoutSettings.rowSpan === 1);

				this.done();
			},
			500
		);
	},
	
	testSpanning: function () {
		this.delay(
			function () {
				var manager, morph;
				morph = this.addRect();
				
				manager = this.gridMorph.layoutManager;
				
				manager.setColSpanForMorph(morph, 2);
				manager.setRowSpanForMorph(morph, 2);
				
				this.assert(morph.gridLayoutSettings.colSpan === 2);
				this.assert(morph.gridLayoutSettings.rowSpan === 2);
				
				this.assert(manager.morphs.length === 2);
				this.assert(manager.morphs[0][0] === morph);
				this.assert(manager.morphs[1][0] === morph);
				this.assert(manager.morphs[0][1] === morph);
				this.assert(manager.morphs[1][1] === morph);
				
				this.assert(manager.cols[0].fixed);
				this.assert(!manager.cols[1].fixed);
				
				this.assert(manager.rows[0].fixed);
				this.assert(!manager.rows[1].fixed);
				
				this.done();
			},
			500
		);
	}
});
}); // end of module