2014-05-07 05:20:36 -07:00
|
|
|
|
using Moq;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using Stylet;
|
2014-05-11 12:02:21 -07:00
|
|
|
|
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 { }
|
2014-12-02 04:52:58 -08:00
|
|
|
|
private Mock<IViewManagerConfig> viewManagerConfig;
|
2014-05-07 05:20:36 -07:00
|
|
|
|
|
2014-07-24 08:39:26 -07:00
|
|
|
|
private class AccessibleViewManager : ViewManager
|
|
|
|
|
{
|
2014-12-02 04:52:58 -08:00
|
|
|
|
public AccessibleViewManager(IViewManagerConfig config) : base(config) { }
|
2014-11-30 06:05:08 -08:00
|
|
|
|
|
2014-07-24 08:39:26 -07:00
|
|
|
|
public new UIElement CreateViewForModel(object model)
|
|
|
|
|
{
|
|
|
|
|
return base.CreateViewForModel(model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public new void BindViewToModel(UIElement view, object viewModel)
|
|
|
|
|
{
|
|
|
|
|
base.BindViewToModel(view, viewModel);
|
|
|
|
|
}
|
2015-01-04 08:13:58 -08:00
|
|
|
|
|
|
|
|
|
public new string ViewTypeNameForModelTypeName(string modelTypeName)
|
|
|
|
|
{
|
|
|
|
|
return base.ViewTypeNameForModelTypeName(modelTypeName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public new Type LocateViewForModel(Type modelType)
|
|
|
|
|
{
|
|
|
|
|
return base.LocateViewForModel(modelType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public new Type ViewTypeForViewName(string viewName)
|
|
|
|
|
{
|
|
|
|
|
return base.ViewTypeForViewName(viewName);
|
|
|
|
|
}
|
2014-07-24 08:39:26 -07:00
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
2014-12-02 04:52:58 -08:00
|
|
|
|
public CreatingAndBindingViewManager(IViewManagerConfig config) : base(config) { }
|
2014-11-30 06:05:08 -08:00
|
|
|
|
|
2015-01-14 12:30:51 -08:00
|
|
|
|
public 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;
|
2015-01-14 12:30:51 -08:00
|
|
|
|
public 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
|
|
|
|
|
{
|
2014-12-02 04:52:58 -08:00
|
|
|
|
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;
|
2014-05-26 11:08:58 -07:00
|
|
|
|
protected override Type LocateViewForModel(Type modelType)
|
2014-05-07 05:20:36 -07:00
|
|
|
|
{
|
|
|
|
|
return this.LocatedViewType;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-27 01:19:36 -08:00
|
|
|
|
private class ResolvingViewManager : ViewManager
|
|
|
|
|
{
|
|
|
|
|
public ResolvingViewManager(IViewManagerConfig config) : base(config) { }
|
|
|
|
|
|
|
|
|
|
public Type ViewType;
|
|
|
|
|
protected override Type ViewTypeForViewName(string viewName)
|
|
|
|
|
{
|
|
|
|
|
return ViewType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public new Type LocateViewForModel(Type modelType)
|
|
|
|
|
{
|
|
|
|
|
return base.LocateViewForModel(modelType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override string ViewTypeNameForModelTypeName(string modelTypeName)
|
|
|
|
|
{
|
|
|
|
|
return "testy";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-07 05:20:36 -07:00
|
|
|
|
|
|
|
|
|
private class TestView : UIElement
|
|
|
|
|
{
|
|
|
|
|
public bool InitializeComponentCalled;
|
|
|
|
|
public void InitializeComponent()
|
|
|
|
|
{
|
|
|
|
|
this.InitializeComponentCalled = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-04 08:13:58 -08:00
|
|
|
|
private AccessibleViewManager viewManager;
|
2014-05-07 05:20:36 -07:00
|
|
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
|
public void SetUp()
|
|
|
|
|
{
|
2014-12-02 04:52:58 -08:00
|
|
|
|
this.viewManagerConfig = new Mock<IViewManagerConfig>();
|
2015-01-04 08:13:58 -08:00
|
|
|
|
this.viewManager = new AccessibleViewManager(this.viewManagerConfig.Object);
|
2014-05-07 05:20:36 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void OnModelChangedDoesNothingIfNoChange()
|
|
|
|
|
{
|
|
|
|
|
var val = new object();
|
2014-05-26 10:48:25 -07:00
|
|
|
|
this.viewManager.OnModelChanged(null, val, val);
|
2014-05-07 05:20:36 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void OnModelChangedSetsNullIfNewValueNull()
|
|
|
|
|
{
|
|
|
|
|
var target = new ContentControl();
|
2014-05-26 10:48:25 -07:00
|
|
|
|
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);
|
2014-05-26 10:48:25 -07:00
|
|
|
|
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();
|
2014-12-02 04:52:58 -08:00
|
|
|
|
var viewManager = new CreatingAndBindingViewManager(this.viewManagerConfig.Object);
|
2014-05-07 05:20:36 -07:00
|
|
|
|
|
|
|
|
|
viewManager.View = view;
|
|
|
|
|
|
2014-05-26 10:48:25 -07:00
|
|
|
|
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]
|
2015-02-27 01:19:36 -08:00
|
|
|
|
public void CreateViewForModelReturnsNullIfViewNotFound()
|
2014-05-07 05:20:36 -07:00
|
|
|
|
{
|
2014-12-02 04:52:58 -08:00
|
|
|
|
var config = new Mock<IViewManagerConfig>();
|
|
|
|
|
config.Setup(x => x.GetInstance(typeof(C1))).Returns(null);
|
|
|
|
|
config.SetupGet(x => x.Assemblies).Returns(new List<Assembly>());
|
|
|
|
|
|
2015-01-04 08:13:58 -08:00
|
|
|
|
var viewManager = new AccessibleViewManager(config.Object);
|
2015-02-27 01:19:36 -08:00
|
|
|
|
Assert.IsNull(viewManager.ViewTypeForViewName("Test"));
|
2015-01-04 08:13:58 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void LocateViewForModelThrowsIfNameTranslationDoesntWork()
|
|
|
|
|
{
|
|
|
|
|
Assert.Throws<StyletViewLocationException>(() => this.viewManager.LocateViewForModel(typeof(C1)));
|
2014-05-07 05:20:36 -07:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-27 01:19:36 -08:00
|
|
|
|
[Test]
|
|
|
|
|
public void LocateViewForModelThrowsIfTypeLocationDoesntWork()
|
|
|
|
|
{
|
|
|
|
|
var config = new Mock<IViewManagerConfig>();
|
|
|
|
|
var viewManager = new ResolvingViewManager(config.Object);
|
|
|
|
|
viewManager.ViewType = null;
|
|
|
|
|
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
|
|
|
|
{
|
2014-12-02 04:52:58 -08:00
|
|
|
|
var config = new Mock<IViewManagerConfig>();
|
|
|
|
|
config.SetupGet(x => x.Assemblies).Returns(new List<Assembly>() { Assembly.GetExecutingAssembly() });
|
2015-01-04 08:13:58 -08:00
|
|
|
|
var viewManager = new AccessibleViewManager(config.Object);
|
2014-11-30 06:58:04 -08:00
|
|
|
|
var viewType = viewManager.LocateViewForModel(typeof(ViewManagerTestsViewModel));
|
2014-05-07 05:20:36 -07:00
|
|
|
|
Assert.AreEqual(typeof(ViewManagerTestsView), viewType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2015-02-22 08:47:34 -08:00
|
|
|
|
public void CreateViewForModelIfNecessaryThrowsIfViewIsNotConcreteUIElement()
|
2014-05-07 05:20:36 -07:00
|
|
|
|
{
|
2014-12-02 04:52:58 -08:00
|
|
|
|
var viewManager = new LocatingViewManager(this.viewManagerConfig.Object);
|
2014-05-07 05:20:36 -07:00
|
|
|
|
|
|
|
|
|
viewManager.LocatedViewType = typeof(I1);
|
2015-02-22 08:47:34 -08:00
|
|
|
|
Assert.Throws<StyletViewLocationException>(() => viewManager.CreateAndBindViewForModelIfNecessary(new object()));
|
2014-05-07 05:20:36 -07:00
|
|
|
|
|
|
|
|
|
viewManager.LocatedViewType = typeof(AC1);
|
2015-02-22 08:47:34 -08:00
|
|
|
|
Assert.Throws<StyletViewLocationException>(() => viewManager.CreateAndBindViewForModelIfNecessary(new object()));
|
2014-05-07 05:20:36 -07:00
|
|
|
|
|
|
|
|
|
viewManager.LocatedViewType = typeof(C1);
|
2015-02-22 08:47:34 -08:00
|
|
|
|
Assert.Throws<StyletViewLocationException>(() => viewManager.CreateAndBindViewForModelIfNecessary(new object()));
|
2014-05-07 05:20:36 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2015-02-22 08:47:34 -08:00
|
|
|
|
public void CreateAndBindViewForModelIfNecessaryCallsFetchesViewAndCallsInitializeComponent()
|
2014-05-07 05:20:36 -07:00
|
|
|
|
{
|
|
|
|
|
var view = new TestView();
|
2014-12-02 04:52:58 -08:00
|
|
|
|
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);
|
|
|
|
|
|
2015-02-22 08:47:34 -08:00
|
|
|
|
var returnedView = viewManager.CreateAndBindViewForModelIfNecessary(new object());
|
2014-05-07 05:20:36 -07:00
|
|
|
|
|
|
|
|
|
Assert.True(view.InitializeComponentCalled);
|
|
|
|
|
Assert.AreEqual(view, returnedView);
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-22 08:47:34 -08:00
|
|
|
|
[Test]
|
|
|
|
|
public void CreateAndBindViewForModelReturnsViewIfAlreadySet()
|
|
|
|
|
{
|
|
|
|
|
var view = new TestView();
|
|
|
|
|
var viewModel = new Mock<IViewAware>();
|
|
|
|
|
viewModel.SetupGet(x => x.View).Returns(view);
|
|
|
|
|
|
|
|
|
|
var returnedView = this.viewManager.CreateAndBindViewForModelIfNecessary(viewModel.Object);
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(view, returnedView);
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-07 05:20:36 -07:00
|
|
|
|
[Test]
|
|
|
|
|
public void CreateViewForModelDoesNotComplainIfNoInitializeComponentMethod()
|
|
|
|
|
{
|
|
|
|
|
var view = new UIElement();
|
2014-12-02 04:52:58 -08:00
|
|
|
|
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);
|
|
|
|
|
|
2015-02-22 08:47:34 -08:00
|
|
|
|
var returnedView = viewManager.CreateAndBindViewForModelIfNecessary(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();
|
2014-12-02 04:52:58 -08:00
|
|
|
|
var viewManager = new AccessibleViewManager(this.viewManagerConfig.Object);
|
2014-07-24 08:39:26 -07:00
|
|
|
|
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();
|
2014-12-02 04:52:58 -08:00
|
|
|
|
var viewManager = new AccessibleViewManager(this.viewManagerConfig.Object);
|
2014-07-24 08:39:26 -07:00
|
|
|
|
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>();
|
2014-12-02 04:52:58 -08:00
|
|
|
|
var viewManager = new AccessibleViewManager(this.viewManagerConfig.Object);
|
2014-07-24 08:39:26 -07:00
|
|
|
|
viewManager.BindViewToModel(view, model.Object);
|
2014-05-07 05:20:36 -07:00
|
|
|
|
|
|
|
|
|
model.Verify(x => x.AttachView(view));
|
|
|
|
|
}
|
2015-01-04 08:13:58 -08:00
|
|
|
|
|
2015-02-22 08:47:34 -08:00
|
|
|
|
|
|
|
|
|
|
2015-01-04 08:13:58 -08:00
|
|
|
|
[Test]
|
|
|
|
|
public void ViewNameResolutionWorksAsExpected()
|
|
|
|
|
{
|
|
|
|
|
var viewManager = new AccessibleViewManager(this.viewManagerConfig.Object);
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual("Root.Test.ThingView", viewManager.ViewTypeNameForModelTypeName("Root.Test.ThingViewModel"));
|
|
|
|
|
Assert.AreEqual("Root.Views.ThingView", viewManager.ViewTypeNameForModelTypeName("Root.ViewModels.ThingViewModel"));
|
|
|
|
|
Assert.AreEqual("Root.View.ThingView", viewManager.ViewTypeNameForModelTypeName("Root.ViewModel.ThingViewModel"));
|
|
|
|
|
Assert.AreEqual("Root.View.ViewModelThing", viewManager.ViewTypeNameForModelTypeName("Root.ViewModel.ViewModelThing"));
|
|
|
|
|
Assert.AreEqual("Root.ThingViews.ThingView", viewManager.ViewTypeNameForModelTypeName("Root.ThingViewModels.ThingViewModel"));
|
|
|
|
|
Assert.AreEqual("Root.ThingView.ThingView", viewManager.ViewTypeNameForModelTypeName("Root.ThingViewModel.ThingViewModel"));
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual("Root.ViewModelsNamespace.ThingView", viewManager.ViewTypeNameForModelTypeName("Root.ViewModelsNamespace.ThingViewModel"));
|
|
|
|
|
Assert.AreEqual("Root.ViewModelNamespace.ThingView", viewManager.ViewTypeNameForModelTypeName("Root.ViewModelNamespace.ThingViewModel"));
|
|
|
|
|
Assert.AreEqual("Root.NamespaceOfViews.ThingView", viewManager.ViewTypeNameForModelTypeName("Root.NamespaceOfViewModels.ThingViewModel"));
|
|
|
|
|
Assert.AreEqual("Root.NamespaceOfView.ThingView", viewManager.ViewTypeNameForModelTypeName("Root.NamespaceOfViewModel.ThingViewModel"));
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual("ViewModels.TestView", viewManager.ViewTypeNameForModelTypeName("ViewModels.TestViewModel"));
|
|
|
|
|
}
|
2014-05-07 05:20:36 -07:00
|
|
|
|
}
|
|
|
|
|
}
|