I am currently working on my project, which is a WPF application similar to MSPaint. However, I do not draw with a pencil or something similar, but with objects (Rectangle, Circle, Triangle, etc.). I use Prism and the MVVM model to achieve testability and maintainability.
Now I have a problem. I have CanvasView.xaml, which (as the name suggests) is the canvas on which I draw. I applied custom Prism CommandBehaviors (i.e. MouseDownCommandBehavior) to provide a way to bind ViewModel commands to mouse actions on the canvas.
The basic setup is as follows:
public DelegateCommand<MouseEventArgs> MouseLeftButtonDownCommand { get; set; } public CanvasViewModel(ICanvasView view, IEventAggregator eventAggregator) : base(view) { m_View = view; m_EventAggregator = eventAggregator; m_EventAggregator.GetEvent<ToolboxSelectionChangedEvent>().Subscribe(OnToolboxSelectionChanged); MouseLeftButtonDownCommand = new DelegateCommand<MouseEventArgs>(OnMouseLeftButtonDown); } public void OnMouseLeftButtonDown(MouseEventArgs args) { Point position = m_View.GetPosition(args); if(SelectedObject!=null){ PaintObject po = SelectedObject.Clone(); Canvas.SetLeft(po,position.X); Canvas.SetTop(po,position.Y); PaintObjects.Add(po); } }
Some things missing from the code:
- PaintObjects is a collection of PaintObjects that the ItemsControl in the view binds to
- PaintObject is the base class for all PaintObjects used (Rectangle, Circle, Triangle, etc.).
- The selected object (of type PaintObject) is determined by the selection process in another Prism module (Toolbox)
The question is, how can I unit test the OnMouseLeftButtonDown method? The problem is that it relies heavily on MouseEventArgs, and I really don't know a good way to mock / drown MouseEventArgs.
source share