Stylet/StyletUnitTests/ViewManagerTests.cs

385 lines
14 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.Reflection;
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 class AccessibleViewManager : ViewManager
{
public AccessibleViewManager(Func<Type, object> viewFactory, List<Assembly> viewAssemblies)
: base(viewFactory, viewAssemblies) { }
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);
}
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-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(Func<Type, object> viewFactory, List<Assembly> viewAssemblies)
: base(viewFactory, viewAssemblies) { }
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
{
public LocatingViewManager(Func<Type, object> viewFactory, List<Assembly> viewAssemblies)
: base(viewFactory, viewAssemblies) { }
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 ResolvingViewManager : ViewManager
{
public ResolvingViewManager(Func<Type, object> viewFactory, List<Assembly> viewAssemblies)
: base(viewFactory, viewAssemblies) { }
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;
}
}
private AccessibleViewManager viewManager;
2014-05-07 05:20:36 -07:00
[SetUp]
public void SetUp()
{
this.viewManager = new AccessibleViewManager(type => null, new List<Assembly>());
2014-05-07 05:20:36 -07:00
}
[Test]
public void ViewManagerRejectsNullViewAssemblies()
{
Assert.Throws<ArgumentNullException>(() => new ViewManager(type => null, null));
Assert.Throws<ArgumentNullException>(() => this.viewManager.ViewAssemblies = null);
}
[Test]
public void ViewManagerRejectsNullNamespaceTransformations()
{
Assert.Throws<ArgumentNullException>(() => this.viewManager.NamespaceTransformations = null);
}
[Test]
public void ViewManagerRejectsNullViewNameSuffix()
{
Assert.Throws<ArgumentNullException>(() => this.viewManager.ViewNameSuffix = null);
}
[Test]
public void ViewManagerRejectsNullViewModelNameSuffix()
{
Assert.Throws<ArgumentNullException>(() => this.viewManager.ViewModelNameSuffix = null);
}
[Test]
public void ViewManagerRejectsNullViewFactory()
{
Assert.Throws<ArgumentNullException>(() => new ViewManager(null, new List<Assembly>()));
Assert.Throws<ArgumentNullException>(() => this.viewManager.ViewFactory = null);
}
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(type => null, new List<Assembly>());
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]
public void OnModelChangedThrowsIfViewIsAWindow()
{
var target = new ContentControl();
var model = new object();
var view = new Window();
var viewManager = new CreatingAndBindingViewManager(type => null, new List<Assembly>());
viewManager.View = view;
Assert.Throws<StyletInvalidViewTypeException>(() => viewManager.OnModelChanged(target, null, model));
}
2014-05-07 05:20:36 -07:00
[Test]
public void CreateViewForModelReturnsNullIfViewNotFound()
2014-05-07 05:20:36 -07:00
{
var viewManager = new AccessibleViewManager(type => null, new List<Assembly>() { typeof(BootstrapperBase).Assembly, Assembly.GetExecutingAssembly() });
Assert.IsNull(viewManager.ViewTypeForViewName("Test"));
}
[Test]
public void LocateViewForModelThrowsIfNameTranslationDoesntWork()
{
Assert.Throws<StyletViewLocationException>(() => this.viewManager.LocateViewForModel(typeof(C1)));
2014-05-07 05:20:36 -07:00
}
[Test]
public void LocateViewForModelThrowsIfTypeLocationDoesntWork()
{
var viewManager = new ResolvingViewManager(type => null, new List<Assembly>());
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
{
var viewManager = new AccessibleViewManager(type => null, new List<Assembly>() { Assembly.GetExecutingAssembly() });
var viewType = viewManager.LocateViewForModel(typeof(ViewManagerTestsViewModel));
2014-05-07 05:20:36 -07:00
Assert.AreEqual(typeof(ViewManagerTestsView), viewType);
}
[Test]
public void CreateViewForModelIfNecessaryThrowsIfViewIsNotConcreteUIElement()
2014-05-07 05:20:36 -07:00
{
var viewManager = new LocatingViewManager(type => null, new List<Assembly>());
2014-05-07 05:20:36 -07:00
viewManager.LocatedViewType = typeof(I1);
Assert.Throws<StyletViewLocationException>(() => viewManager.CreateAndBindViewForModelIfNecessary(new object()));
2014-05-07 05:20:36 -07:00
viewManager.LocatedViewType = typeof(AC1);
Assert.Throws<StyletViewLocationException>(() => viewManager.CreateAndBindViewForModelIfNecessary(new object()));
2014-05-07 05:20:36 -07:00
viewManager.LocatedViewType = typeof(C1);
Assert.Throws<StyletViewLocationException>(() => viewManager.CreateAndBindViewForModelIfNecessary(new object()));
2014-05-07 05:20:36 -07:00
}
[Test]
public void CreateAndBindViewForModelIfNecessaryCallsFetchesViewAndCallsInitializeComponent()
2014-05-07 05:20:36 -07:00
{
var view = new TestView();
var viewManager = new LocatingViewManager(type => view, new List<Assembly>());
2014-05-07 05:20:36 -07:00
viewManager.LocatedViewType = typeof(TestView);
var returnedView = viewManager.CreateAndBindViewForModelIfNecessary(new object());
2014-05-07 05:20:36 -07:00
Assert.True(view.InitializeComponentCalled);
Assert.AreEqual(view, returnedView);
}
[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();
var viewManager = new LocatingViewManager(type => view, new List<Assembly>());
2014-05-07 05:20:36 -07:00
viewManager.LocatedViewType = typeof(UIElement);
var returnedView = viewManager.CreateAndBindViewForModelIfNecessary(new object());
2014-05-07 05:20:36 -07:00
Assert.AreEqual(view, returnedView);
}
[Test]
2015-09-24 05:02:54 -07:00
public void BindViewToModelDoesNotSetActionTarget()
2014-05-07 05:20:36 -07:00
{
var view = new UIElement();
var viewManager = new AccessibleViewManager(type => null, new List<Assembly>());
2015-09-24 05:02:54 -07:00
viewManager.BindViewToModel(view, new object());
2014-05-07 05:20:36 -07:00
2015-09-24 05:02:54 -07:00
Assert.AreEqual(View.InitialActionTarget, View.GetActionTarget(view));
2014-05-07 05:20:36 -07:00
}
[Test]
public void BindViewToModelSetsDataContext()
{
var view = new FrameworkElement();
var model = new object();
var viewManager = new AccessibleViewManager(type => null, new List<Assembly>());
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(type => null, new List<Assembly>());
viewManager.BindViewToModel(view, model.Object);
2014-05-07 05:20:36 -07:00
model.Verify(x => x.AttachView(view));
}
[Test]
public void ViewNameResolutionWorksAsExpected()
{
Assert.AreEqual("Root.Test.ThingView", this.viewManager.ViewTypeNameForModelTypeName("Root.Test.ThingViewModel"));
Assert.AreEqual("Root.Views.ThingView", this.viewManager.ViewTypeNameForModelTypeName("Root.ViewModels.ThingViewModel"));
Assert.AreEqual("Root.View.ThingView", this.viewManager.ViewTypeNameForModelTypeName("Root.ViewModel.ThingViewModel"));
Assert.AreEqual("Root.View.ViewModelThing", this.viewManager.ViewTypeNameForModelTypeName("Root.ViewModel.ViewModelThing"));
Assert.AreEqual("Root.ThingViews.ThingView", this.viewManager.ViewTypeNameForModelTypeName("Root.ThingViewModels.ThingViewModel"));
Assert.AreEqual("Root.ThingView.ThingView", this.viewManager.ViewTypeNameForModelTypeName("Root.ThingViewModel.ThingViewModel"));
Assert.AreEqual("Root.ViewModelsNamespace.ThingView", this.viewManager.ViewTypeNameForModelTypeName("Root.ViewModelsNamespace.ThingViewModel"));
Assert.AreEqual("Root.ViewModelNamespace.ThingView", this.viewManager.ViewTypeNameForModelTypeName("Root.ViewModelNamespace.ThingViewModel"));
Assert.AreEqual("Root.NamespaceOfViews.ThingView", this.viewManager.ViewTypeNameForModelTypeName("Root.NamespaceOfViewModels.ThingViewModel"));
Assert.AreEqual("Root.NamespaceOfView.ThingView", this.viewManager.ViewTypeNameForModelTypeName("Root.NamespaceOfViewModel.ThingViewModel"));
Assert.AreEqual("ViewModels.TestView", this.viewManager.ViewTypeNameForModelTypeName("ViewModels.TestViewModel"));
}
[Test]
public void ViewNameResolutionUsesConfig()
{
this.viewManager.ViewNameSuffix = "Viiiiew";
this.viewManager.ViewModelNameSuffix = "ViiiiiewModel";
Assert.AreEqual("Root.Test.ThingViiiiew", viewManager.ViewTypeNameForModelTypeName("Root.Test.ThingViiiiiewModel"));
}
[Test]
public void NamespaceTransformationsTransformsNamespace()
{
this.viewManager.NamespaceTransformations["Foo.Bar"] = "Baz.Yay";
Assert.AreEqual("Baz.Yay.ThingView", viewManager.ViewTypeNameForModelTypeName("Foo.Bar.ThingViewModel"));
Assert.AreEqual("Baz.Yay.Thing", viewManager.ViewTypeNameForModelTypeName("Foo.Bar.Thing"));
}
[Test]
public void NamespaceTransformationsTransformOnlyFirstMatch()
{
this.viewManager.NamespaceTransformations["Foo.Bar"] = "Baz.Yay";
this.viewManager.NamespaceTransformations["Baz.Yay"] = "One.Two";
Assert.AreEqual("Baz.Yay.ThingView", viewManager.ViewTypeNameForModelTypeName("Foo.Bar.ThingViewModel"));
Assert.AreEqual("One.Two.ThingView", viewManager.ViewTypeNameForModelTypeName("Baz.Yay.ThingViewModel"));
}
2014-05-07 05:20:36 -07:00
}
}