Stylet/StyletUnitTests/ViewManagerTests.cs

261 lines
8.5 KiB
C#
Raw Normal View History

2014-05-07 05:20:36 -07:00
using Moq;
using NUnit.Framework;
using Stylet;
using Stylet.Xaml;
2014-05-07 05:20:36 -07:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
namespace StyletUnitTests
{
public class ViewManagerTestsViewModel
{
}
public class ViewManagerTestsView
{
}
[TestFixture, RequiresSTA]
public class ViewManagerTests
{
private interface I1 { }
private abstract class AC1 { }
private class C1 { }
private Mock<IViewManagerConfig> viewManagerConfig;
2014-05-07 05:20:36 -07:00
private class AccessibleViewManager : ViewManager
{
public AccessibleViewManager(IViewManagerConfig config) : base(config) { }
2014-11-30 06:05:08 -08:00
public new UIElement CreateViewForModel(object model)
{
return base.CreateViewForModel(model);
}
public new void BindViewToModel(UIElement view, object viewModel)
{
base.BindViewToModel(view, viewModel);
}
}
2014-05-07 05:20:36 -07:00
private class CreatingAndBindingViewManager : ViewManager
{
public UIElement View;
public object RequestedModel;
2014-11-30 06:05:08 -08:00
public CreatingAndBindingViewManager(IViewManagerConfig config) : base(config) { }
2014-11-30 06:05:08 -08:00
protected override UIElement CreateViewForModel(object model)
2014-05-07 05:20:36 -07:00
{
this.RequestedModel = model;
return this.View;
}
public UIElement BindViewToModelView;
public object BindViewtoModelViewModel;
protected override void BindViewToModel(UIElement view, object viewModel)
2014-05-07 05:20:36 -07:00
{
this.BindViewToModelView = view;
this.BindViewtoModelViewModel = viewModel;
}
}
private class LocatingViewManager : ViewManager
{
public LocatingViewManager(IViewManagerConfig config) : base(config) { }
2014-11-30 06:05:08 -08:00
2014-05-07 05:20:36 -07:00
public Type LocatedViewType;
protected override Type LocateViewForModel(Type modelType)
2014-05-07 05:20:36 -07:00
{
return this.LocatedViewType;
}
}
private class TestView : UIElement
{
public bool InitializeComponentCalled;
public void InitializeComponent()
{
this.InitializeComponentCalled = true;
}
}
private class MyViewManager : ViewManager
{
public MyViewManager(IViewManagerConfig config) : base(config) { }
2014-11-30 06:05:08 -08:00
public new Type LocateViewForModel(Type modelType)
{
return base.LocateViewForModel(modelType);
}
}
private MyViewManager viewManager;
2014-05-07 05:20:36 -07:00
2014-05-10 12:11:02 -07:00
[TestFixtureSetUp]
public void FixtureSetUp()
{
Execute.Dispatcher = new SynchronousDispatcher();
2014-05-10 12:11:02 -07:00
}
2014-05-07 05:20:36 -07:00
[SetUp]
public void SetUp()
{
this.viewManagerConfig = new Mock<IViewManagerConfig>();
this.viewManager = new MyViewManager(this.viewManagerConfig.Object);
2014-05-07 05:20:36 -07:00
}
[Test]
public void OnModelChangedDoesNothingIfNoChange()
{
var val = new object();
this.viewManager.OnModelChanged(null, val, val);
2014-05-07 05:20:36 -07:00
}
[Test]
public void OnModelChangedSetsNullIfNewValueNull()
{
var target = new ContentControl();
this.viewManager.OnModelChanged(target, 5, null);
2014-05-07 05:20:36 -07:00
Assert.Null(target.Content);
}
[Test]
public void OnModelChangedUsesViewIfAlreadySet()
{
var target = new ContentControl();
var model = new Mock<IScreen>();
var view = new UIElement();
model.Setup(x => x.View).Returns(view);
this.viewManager.OnModelChanged(target, null, model.Object);
2014-05-07 05:20:36 -07:00
Assert.AreEqual(view, target.Content);
}
[Test]
public void OnModelChangedCreatesAndBindsView()
{
var target = new ContentControl();
var model = new object();
var view = new UIElement();
var viewManager = new CreatingAndBindingViewManager(this.viewManagerConfig.Object);
2014-05-07 05:20:36 -07:00
viewManager.View = view;
viewManager.OnModelChanged(target, null, model);
2014-05-07 05:20:36 -07:00
Assert.AreEqual(viewManager.RequestedModel, model);
Assert.AreEqual(viewManager.BindViewToModelView, view);
Assert.AreEqual(viewManager.BindViewtoModelViewModel, model);
Assert.AreEqual(view, target.Content);
}
[Test]
2014-05-10 12:11:02 -07:00
public void LocateViewForModelThrowsIfViewNotFound()
2014-05-07 05:20:36 -07:00
{
var config = new Mock<IViewManagerConfig>();
config.Setup(x => x.GetInstance(typeof(C1))).Returns(null);
config.SetupGet(x => x.Assemblies).Returns(new List<Assembly>());
var viewManager = new MyViewManager(config.Object);
Assert.Throws<StyletViewLocationException>(() => viewManager.LocateViewForModel(typeof(C1)));
2014-05-07 05:20:36 -07:00
}
[Test]
2014-05-10 12:11:02 -07:00
public void LocateViewForModelFindsViewForModel()
2014-05-07 05:20:36 -07:00
{
var config = new Mock<IViewManagerConfig>();
config.SetupGet(x => x.Assemblies).Returns(new List<Assembly>() { Assembly.GetExecutingAssembly() });
var viewManager = new MyViewManager(config.Object);
Execute.Dispatcher = new SynchronousDispatcher();
var viewType = viewManager.LocateViewForModel(typeof(ViewManagerTestsViewModel));
2014-05-07 05:20:36 -07:00
Assert.AreEqual(typeof(ViewManagerTestsView), viewType);
}
[Test]
public void CreateViewForModelThrowsIfViewIsNotConcreteUIElement()
{
var viewManager = new LocatingViewManager(this.viewManagerConfig.Object);
2014-05-07 05:20:36 -07:00
viewManager.LocatedViewType = typeof(I1);
Assert.Throws<StyletViewLocationException>(() => viewManager.CreateAndBindViewForModel(new object()));
2014-05-07 05:20:36 -07:00
viewManager.LocatedViewType = typeof(AC1);
Assert.Throws<StyletViewLocationException>(() => viewManager.CreateAndBindViewForModel(new object()));
2014-05-07 05:20:36 -07:00
viewManager.LocatedViewType = typeof(C1);
Assert.Throws<StyletViewLocationException>(() => viewManager.CreateAndBindViewForModel(new object()));
2014-05-07 05:20:36 -07:00
}
[Test]
public void CreateViewForModelCallsFetchesViewAndCallsInitializeComponent()
{
var view = new TestView();
var config = new Mock<IViewManagerConfig>();
config.Setup(x => x.GetInstance(typeof(TestView))).Returns(view);
var viewManager = new LocatingViewManager(config.Object);
2014-05-07 05:20:36 -07:00
viewManager.LocatedViewType = typeof(TestView);
var returnedView = viewManager.CreateAndBindViewForModel(new object());
2014-05-07 05:20:36 -07:00
Assert.True(view.InitializeComponentCalled);
Assert.AreEqual(view, returnedView);
}
[Test]
public void CreateViewForModelDoesNotComplainIfNoInitializeComponentMethod()
{
var view = new UIElement();
var config = new Mock<IViewManagerConfig>();
config.Setup(x => x.GetInstance(typeof(UIElement))).Returns(view);
var viewManager = new LocatingViewManager(config.Object);
2014-05-07 05:20:36 -07:00
viewManager.LocatedViewType = typeof(UIElement);
var returnedView = viewManager.CreateAndBindViewForModel(new object());
2014-05-07 05:20:36 -07:00
Assert.AreEqual(view, returnedView);
}
[Test]
public void BindViewToModelSetsActionTarget()
{
var view = new UIElement();
var model = new object();
var viewManager = new AccessibleViewManager(this.viewManagerConfig.Object);
viewManager.BindViewToModel(view, model);
2014-05-07 05:20:36 -07:00
Assert.AreEqual(model, View.GetActionTarget(view));
}
[Test]
public void BindViewToModelSetsDataContext()
{
var view = new FrameworkElement();
var model = new object();
var viewManager = new AccessibleViewManager(this.viewManagerConfig.Object);
viewManager.BindViewToModel(view, model);
2014-05-07 05:20:36 -07:00
Assert.AreEqual(model, view.DataContext);
}
[Test]
public void BindViewToModelAttachesView()
{
var view = new UIElement();
var model = new Mock<IViewAware>();
var viewManager = new AccessibleViewManager(this.viewManagerConfig.Object);
viewManager.BindViewToModel(view, model.Object);
2014-05-07 05:20:36 -07:00
model.Verify(x => x.AttachView(view));
}
}
}