[Mono-winforms-list] Testing Visual Properties in MWF

Ritvik Mayank mritvik at novell.com
Fri Aug 12 11:54:28 EDT 2005


Hi Folks,
              Suresh and I were looking for a Framework that will help
us to test the Visual Properties of MWF on Mono. We have experimented
with the following method. It guides with online instruction which helps
the tester to examine the expected behavior and take the appropriate
step. Attached is the sample code and the screenshot.

Regards,
Ritvik
-------------- next part --------------
A non-text attachment was scrubbed...
Name: mwfvisualtest.JPG
Type: image/jpeg
Size: 26643 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/mono-winforms-list/attachments/20050812/7cbb391b/mwfvisualtest-0001.jpe
-------------- next part --------------
//
// TestForm.cs: TestForm subclass for visual tests
//
// Author:
//   Sureshkumar T (tsureshkumar at novell.com)
//   Ritvik Mayank (mritvik at novell.com)
//
// (C) 2005 Novell, Inc. (http://www.novell.com)
//

using System;
using System.Windows.Forms;

namespace MonoTests.System.Windows.Forms
{
	public class TestForm : Form
	{
		Button ok;
		Button cancel;
		GroupBox gInstructions;
		GroupBox gContainer;
		GroupBox gButtonBar;
		ListView lvInstructions;
		TextBox  txtResult;

		string	result;
		int counter = 0;

		public TestForm()
		{
		}

		public void Init (int width, int height) 
		{
			counter = 1;
			this.SetBounds (0, 0, width, height);
			gInstructions = new GroupBox ();
			gInstructions.Text = "Instructions";
			gInstructions.Dock = DockStyle.Fill;
			this.Controls.Add (gInstructions);

			lvInstructions = new ListView ();
			lvInstructions.View = View.Details;
			lvInstructions.Dock = DockStyle.Fill;
			lvInstructions.Columns.Add ("Id", -1, HorizontalAlignment.Left);
			lvInstructions.Columns.Add ("Follow", -1, HorizontalAlignment.Left);
			gInstructions.Controls.Add (lvInstructions);
	
			gContainer = new GroupBox ();
			gContainer.Dock = DockStyle.Bottom | DockStyle.Top;
			this.Controls.Add (gContainer);

			gButtonBar = new GroupBox ();
			gButtonBar.Dock = DockStyle.Bottom;
			gButtonBar.Height = 50;
			ok = new Button ();
			ok.Text = "Pass";
			ok.DialogResult = DialogResult.OK;
			ok.Dock = DockStyle.Right;
			ok.Click +=new EventHandler(ok_Click);
			gButtonBar.Controls.Add (ok);
			cancel = new Button ();
			cancel.DialogResult = DialogResult.Cancel;
			cancel.Dock = DockStyle.Right;
			cancel.Text = "Fail";
			cancel.Click +=new EventHandler(cancel_Click);
			gButtonBar.Controls.Add (cancel);

			txtResult = new TextBox ();
			txtResult.Dock = DockStyle.Left;
			txtResult.Multiline = true;
			txtResult.Width = 200;
			txtResult.Height = 100;
			gButtonBar.Controls.Add (txtResult);

			Label lbl = new Label ();
			lbl.Text = "Result (optional)";
			lbl.Dock = DockStyle.Left;
			gButtonBar.Controls.Add (lbl);

			this.Controls.Add (gButtonBar);
		}

		public GroupBox TestContainer 
		{
			get {return gContainer;}
		}

		public string Result 
		{
			get { return result; }
		}

		public void AddInstructions (string instruction)
		{
			ListViewItem item = new ListViewItem (counter.ToString ());
			item.SubItems.Add (instruction);
			lvInstructions.Items.AddRange (new ListViewItem [] { item} );
			counter++;
		}

		private void ok_Click(object sender, EventArgs e)
		{
			result = txtResult.Text;
		}

		private void cancel_Click(object sender, EventArgs e)
		{
			result = txtResult.Text;
		}
	}
}
-------------- next part --------------
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Reflection;
using NUnit.Framework;
using System.Threading;


namespace MonoTests.System.Windows.Forms
{

	[TestFixture]
	public class ImageListTest
	{
		TestForm testForm = null;
		GroupBox testFormContainer = null;
		[Test]
		[Category ("visual")]
		public void DrawMethodTest ()
		{
			testForm = new TestForm ();
			testForm.Init (500, 300);
			testForm.AddInstructions ("Click on load button");
			testForm.AddInstructions ("You should see a image below appearing");
			testForm.AddInstructions ("If appears, Click Pass, otherwise Click Fail");

			testFormContainer = testForm.TestContainer;
			Button load = new Button ();
			load.Text = "Load";
			load.Click += new EventHandler(load_Click);
			load.Dock = DockStyle.Bottom;
			testFormContainer.Controls.Add (load);
			
			DialogResult result = testForm.ShowDialog();
			if (result != DialogResult.OK)
				Assert.Fail (String.Format ("Test failed : {0}",
						testForm.Result));
		}
		
		private void load_Click(object sender, EventArgs e)
		{
			if (testForm == null || testFormContainer == null) 
			{
				Console.WriteLine ("here");
				return;
			}
			Graphics mygraphics = null;
			try 
			{
				ImageList myimagelist = new ImageList ();
				myimagelist.ImageSize = new Size(150, 60);
				Image myImage =	Image.FromFile("M.gif");
				myimagelist.Images.Add (myImage);
				mygraphics = Graphics.FromHwnd(testFormContainer.Handle);
				myimagelist.Draw(mygraphics, new Point(5, 5), 0);
				Console.WriteLine ("count = {0}", myimagelist.Images.Count);
			} 
			finally 
			{
				if (mygraphics != null)
					mygraphics.Dispose ();
			}
		}
	}
}


More information about the Mono-winforms-list mailing list