Click here to Skip to main content
15,886,026 members
Articles / Programming Languages / C#

Article Two: Building a UI Platform in C# - Testing via UI Animation

,
Rate me:
Please Sign up or sign in to vote.
4.20/5 (7 votes)
3 Mar 20055 min read 51.3K   927   33  
Describes an implementation of UI animation for the support of Test-Driven Development.
// (c) 2003-2005 Datron, Inc.  All rights reserved. http://www.blix.net

using System;
using System.Threading;

namespace freeBlix
{
	public delegate void BeginInvokeDelegate();

	public class Player
	{		
		#region Fields
		private InstructionCollection _Instructions;
		private ControlSystem _ControlSystem;
		private StopWatch _StopWatch;

		private bool _AnimateCursor;
		private int _InitialSpeed;
		private int _Speed;
		private bool _InstantPlaying;
		private bool _Playing;

		private PlayerStartedDelegate _StartedDelegate;
		private PlayerCompletedDelegate _CompletedDelegate;
		private InstructionStartedDelegate _InstructionStartedDelegate;
		private InstructionCompletedDelegate _InstructionCompletedDelegate;
		#endregion

		#region Player(aControlSystem)
		public Player(ControlSystem aControlSystem)
		{
			_ControlSystem = aControlSystem;
		
			_Instructions = new InstructionCollection();

			_InitialSpeed = Instruction.DefaultSpeed;
			_Speed = Instruction.DefaultSpeed;
		}
		#endregion

		#region StopWatchBuzzHandler
		private void StopWatchBuzzHandler(StopWatch aStopWatch)
		{	
			_StopWatch.Stop();

			_ControlSystem.Form.BeginInvoke(new BeginInvokeDelegate(SystemWindowsFormBeginInvokeHandler));
		}
		#endregion

		#region SystemWindowsFormBeginInvokeHandler
		private void SystemWindowsFormBeginInvokeHandler()
		{
			if (_Instructions.Count > 0)
			{
				Instruction instruction = _Instructions[0];
				
				_Instructions.Remove(instruction);

				TriggerInstructionStarted(instruction);

				instruction.Play();
		
				TriggerInstructionCompleted(instruction);
					
				if (instruction.Speed == Instruction.DefaultSpeed)
					_StopWatch.Interval = _Speed;
				else
					_StopWatch.Interval = instruction.Speed;				
					
				_StopWatch.Start();
			}
			else
			{
				_StopWatch.Buzz -= new StopWatchDelegate(StopWatchBuzzHandler);
					
				_StopWatch = null;

				TriggerCompleted();
				
				_Playing = false;
			}
		}
		#endregion

		#region Add(aInstruction)
		public void Add(Instruction aInstruction)
		{
			_Instructions.Add(aInstruction);			
		
			aInstruction.ControlSystem = _ControlSystem;
		}
		#endregion
		#region Add(aInstruction, aSpeed)
		public void Add(Instruction aInstruction, int aSpeed)
		{
			aInstruction.Speed = aSpeed;

			_Instructions.Add(aInstruction);			
		
			aInstruction.ControlSystem = _ControlSystem;
		}
		#endregion
		#region Clear
		public void Clear()
		{
			if (!_Playing)
				_Instructions.Clear();
		}
		#endregion
		#region InstantPlay()
		public void InstantPlay()
		{
			_InstantPlaying = true;

			Play();

			TriggerCompleted();

			_InstantPlaying = false;
		}
		#endregion
		#region InstantPlay(aInstruction)
		public void InstantPlay(Instruction aInstruction)
		{
			_InstantPlaying = true;

			Add(aInstruction);

			Play();

			_InstantPlaying = false;
		}
		#endregion
		#region InstantPlay(aInstructions)
		public void InstantPlay(InstructionCollection aInstructions)
		{
			_InstantPlaying = true;

			foreach (Instruction instruction in aInstructions)
				Add(instruction);

			Play();

			_InstantPlaying = false;
		}
		#endregion
		#region Play
		public void Play()
		{
			#region Precondition
			if (_Instructions.Count == 0)
				throw new Exception(GetType().FullName + ".Play(): No instructions to play");
			#endregion

			TriggerStarted();

			if (!_InstantPlaying)
			{
				_Playing = true;
							
				_StopWatch = new StopWatch();
				_StopWatch.Buzz += new StopWatchDelegate(StopWatchBuzzHandler);

				_StopWatch.Start();	
			}
			else		
			{
				_Playing = true;

				try
				{
					foreach (Instruction instruction in _Instructions)
					{
						TriggerInstructionStarted(instruction);
						
						instruction.Play();

						TriggerInstructionCompleted(instruction);
					}

					_Instructions.Clear();
				}
				finally
				{
					_Playing = false;
				}
			}			
		}
		#endregion
		#region Block
		public void Block()
		{			
			while(_Playing)
			{
				System.Windows.Forms.Application.DoEvents();
			}
		}
		#endregion		
		
		#region AnimateCursor
		public bool AnimateCursor
		{
			get
			{
				return _AnimateCursor;
			}
			set
			{
				_AnimateCursor = value;
			}
		}
		#endregion
		#region HasInstructions
		public bool HasInstructions
		{
			get
			{
				return (_Instructions.Count > 0);
			}
		}
		#endregion
		#region InitialSpeed
		public int InitialSpeed
		{
			get
			{
				return _InitialSpeed;
			}
			set
			{
				_InitialSpeed = value;
			}
		}
		#endregion		
		#region Playing
		public bool Playing
		{
			get
			{
				return _Playing;
			}
		}
		#endregion
		#region Speed
		public int Speed
		{
			get
			{
				return _Speed;
			}
			set
			{
				_Speed = value;
				_InitialSpeed = _Speed;
			}
		}
		#endregion

		#region Started
		#region trigger Started
		private void TriggerStarted()
		{
			if (_StartedDelegate != null)
				_StartedDelegate(this);
		}
		#endregion
		#region event Started
		public event PlayerStartedDelegate Started
		{
			add
			{
				_StartedDelegate = (PlayerStartedDelegate) Delegator.Add(_StartedDelegate, value);
			}
			remove
			{
				_StartedDelegate = (PlayerStartedDelegate) Delegator.Remove(_StartedDelegate, value);
			}
		}
		#endregion
		#endregion		
		#region Completed
		#region trigger Completed
		private void TriggerCompleted()
		{
			if (_CompletedDelegate != null)
				_CompletedDelegate(this);
		}
		#endregion
		#region event Completed
		public event PlayerCompletedDelegate Completed
		{
			add
			{
				_CompletedDelegate = (PlayerCompletedDelegate) Delegator.Add(_CompletedDelegate, value);
			}
			remove
			{
				_CompletedDelegate = (PlayerCompletedDelegate) Delegator.Remove(_CompletedDelegate, value);
			}
		}
		#endregion
		#endregion		
		#region InstructionStarted
		#region trigger InstructionStarted
		private void TriggerInstructionStarted(Instruction aInstruction)
		{
			if (_InstructionStartedDelegate != null)
				_InstructionStartedDelegate(this, aInstruction);
		}
		#endregion
		#region event InstructionStarted
		public event InstructionStartedDelegate InstructionStarted
		{
			add
			{
				_InstructionStartedDelegate = (InstructionStartedDelegate) Delegator.Add(_InstructionStartedDelegate, value);
			}
			remove
			{
				_InstructionStartedDelegate = (InstructionStartedDelegate) Delegator.Remove(_InstructionStartedDelegate, value);
			}
		}
		#endregion
		#endregion		
		#region InstructionCompleted
		#region trigger InstructionCompleted
		private void TriggerInstructionCompleted(Instruction aInstruction)
		{
			if (_InstructionCompletedDelegate != null)
				_InstructionCompletedDelegate(this, aInstruction);
		}
		#endregion
		#region event InstructionCompleted
		public event InstructionCompletedDelegate InstructionCompleted
		{
			add
			{
				_InstructionCompletedDelegate = (InstructionCompletedDelegate) Delegator.Add(_InstructionCompletedDelegate, value);
			}
			remove
			{
				_InstructionCompletedDelegate = (InstructionCompletedDelegate) Delegator.Remove(_InstructionCompletedDelegate, value);
			}
		}
		#endregion
		#endregion		
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
CEO Sagerion, LLC
United States United States
I read About Face by Alan Cooper in 1995 and immediately recognized it as a founding document for the future of software. I also recognized we had a long, long way to go - and yes, even with the advent of iOS, we are still not there yet.

At my company, Sagerion (say-jair-ee-on), we can take a look at your planned or existing software and suggest ways of making it better - lots better. We can develop down-to-the-pixel blueprints showing exactly what our suggestions mean. We can help manage on-going development to make sure the top-notch user-experience we've suggested really does get built. Now, honestly, how often have you ever seen all those things happen?

You may or may not already have great development going on - but what does that matter if you don't have great design driving it?

Feel free to contact me at tom@sagerion.com, I would love to hear about your next ground-breaking project.

Written By
Founder Sagerion LLC
United States United States
www.filoshare.com
-It is a fresh and free distributed source control system.

Comments and Discussions