CPKreuz 4 лет назад
Родитель
Сommit
36cc334913

+ 1 - 1
PixiEditor/Helpers/DependencyInjectionHelper.cs

@@ -22,7 +22,7 @@ namespace PixiEditor.Helpers
         /// <param name="obj">The object that should get injected</param>
         /// <param name="bindingFlags">The binding flags for the properties</param>
         public static void Inject<T>(this IServiceProvider services, T obj, BindingFlags bindingFlags)
-            => Inject(services, obj, bindingFlags, typeof(T));
+            => Inject(services, obj, bindingFlags, obj.GetType());
 
         public static void Inject(this IServiceProvider services, object obj, BindingFlags bindingFlags, Type type)
         {

+ 18 - 0
PixiEditor/Models/Tools/ToolBuilder.cs

@@ -5,6 +5,9 @@ using System.Reflection;
 
 namespace PixiEditor.Models.Tools
 {
+    /// <summary>
+    /// Handles Depdency Injection of tools
+    /// </summary>
     public class ToolBuilder
     {
         private readonly IServiceProvider services;
@@ -16,10 +19,16 @@ namespace PixiEditor.Models.Tools
             this.services = services;
         }
 
+        /// <summary>
+        /// Constructs a new tool of type <typeparamref name="T"/> and injects all services of <paramref name="services"/>
+        /// </summary>
         public static T BuildTool<T>(IServiceProvider services)
             where T : Tool, new()
             => (T)BuildTool(typeof(T), services);
 
+        /// <summary>
+        /// Constructs a new tool of type <paramref name="type"/> and injects all services of <paramref name="services"/>
+        /// </summary>
         public static Tool BuildTool(Type type, IServiceProvider services)
         {
             Tool tool = (Tool)type.GetConstructor(Type.EmptyTypes).Invoke(null);
@@ -31,10 +40,16 @@ namespace PixiEditor.Models.Tools
             return tool;
         }
 
+        /// <summary>
+        /// Adds a new tool of type <typeparamref name="T"/> to the building chain.
+        /// </summary>
         public ToolBuilder Add<T>()
             where T : Tool, new()
             => Add(typeof(T));
 
+        /// <summary>
+        /// Adds a new tool of type <paramref name="type"/> to the building chain.
+        /// </summary>
         public ToolBuilder Add(Type type)
         {
             toBuild.Add(type);
@@ -42,6 +57,9 @@ namespace PixiEditor.Models.Tools
             return this;
         }
 
+        /// <summary>
+        /// Builds all added tools.
+        /// </summary>
         public IEnumerable<Tool> Build()
         {
             List<Tool> tools = new List<Tool>();

+ 8 - 3
PixiEditor/ViewModels/ViewModelMain.cs

@@ -165,7 +165,7 @@ namespace PixiEditor.ViewModels
             DiscordViewModel = new DiscordViewModel(this, "764168193685979138");
             UpdateSubViewModel = new UpdateViewModel(this);
 
-            WindowSubViewModel = GetSubViewModel<WindowViewModel>(services);
+            WindowSubViewModel = GetSubViewModel<WindowViewModel>(services, false);
             StylusSubViewModel = GetSubViewModel<StylusViewModel>(services);
 
             AddDebugOnlyViewModels();
@@ -368,9 +368,14 @@ namespace PixiEditor.ViewModels
             }
         }
 
-        private T GetSubViewModel<T>(IServiceProvider services)
+        private T GetSubViewModel<T>(IServiceProvider services, bool isRequired = true)
         {
-            T subViewModel = services.GetRequiredService<T>();
+            T subViewModel = services.GetService<T>();
+
+            if (subViewModel is null && isRequired)
+            {
+                throw new InvalidOperationException($"No required view model for type '{typeof(T)}' has been registered.");
+            }
 
             if (subViewModel is ISettableOwner<ViewModelMain> settable)
             {

+ 27 - 5
PixiEditorTests/Helpers.cs

@@ -1,7 +1,10 @@
-using System;
-using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.DependencyInjection;
+using PixiEditor.Models.Controllers;
+using PixiEditor.Models.Tools;
 using PixiEditor.Models.UserPreferences;
 using PixiEditor.ViewModels;
+using PixiEditor.ViewModels.SubViewModels.Main;
+using System;
 
 namespace PixiEditorTests
 {
@@ -9,16 +12,35 @@ namespace PixiEditorTests
     {
         public static ViewModelMain MockedViewModelMain()
         {
-            IServiceProvider provider = MockedServiceProvider();
+            IServiceCollection provider = MockedServiceCollection();
 
             return new ViewModelMain(provider);
         }
 
-        public static IServiceProvider MockedServiceProvider()
+        public static IServiceCollection MockedServiceCollection()
         {
             return new ServiceCollection()
                 .AddSingleton<IPreferences>(new Mocks.PreferenceSettingsMock())
-                .BuildServiceProvider();
+                .AddSingleton<StylusViewModel>()
+                .AddSingleton<BitmapManager>()
+                .AddSingleton<ToolsViewModel>();
+        }
+
+        public static T BuildMockedTool<T>(bool requireViewModelMain = false)
+            where T : Tool, new()
+        {
+            IServiceProvider services;
+
+            if (requireViewModelMain)
+            {
+                services = MockedViewModelMain().Services;
+            }
+            else
+            {
+                services = MockedServiceCollection().BuildServiceProvider();
+            }
+
+            return ToolBuilder.BuildTool<T>(services);
         }
     }
 }

+ 2 - 1
PixiEditorTests/ModelsTests/ToolsTests/PenToolTests.cs

@@ -1,4 +1,5 @@
 using PixiEditor.Models.Position;
+using PixiEditor.Models.Tools;
 using PixiEditor.Models.Tools.Tools;
 using Xunit;
 
@@ -10,7 +11,7 @@ namespace PixiEditorTests.ModelsTests.ToolsTests
         [StaFact]
         public void TestThatPixelPerfectPenReturnsShapeWithoutLShapePixels()
         {
-            PenTool pen = new PenTool();
+            PenTool pen = Helpers.BuildMockedTool<PenTool>();
 
             Coordinates start = new Coordinates(0, 0);
             Coordinates end = new Coordinates(0, 0);

+ 3 - 2
PixiEditorTests/ModelsTests/ToolsTests/ZoomToolTests.cs

@@ -1,4 +1,5 @@
 using Microsoft.Extensions.DependencyInjection;
+using PixiEditor.Models.Tools;
 using PixiEditor.Models.Tools.Tools;
 using PixiEditor.Models.UserPreferences;
 using PixiEditor.ViewModels;
@@ -12,9 +13,9 @@ namespace PixiEditorTests.ModelsTests.ToolsTests
         [StaFact]
         public void TestThatZoomSetsActiveDocumentZoomPercentage()
         {
-            ViewModelMain vm = new ViewModelMain(new ServiceCollection().AddSingleton<IPreferences>(new Mocks.PreferenceSettingsMock()).BuildServiceProvider());
+            ViewModelMain vm = Helpers.MockedViewModelMain();
             vm.BitmapManager.ActiveDocument = new PixiEditor.Models.DataHolders.Document(10, 10);
-            ZoomTool zoomTool = new ZoomTool();
+            ZoomTool zoomTool = ToolBuilder.BuildTool<ZoomTool>(vm.Services);
             double zoom = 110;
             zoomTool.Zoom(zoom);
             Assert.Equal(zoom, vm.BitmapManager.ActiveDocument.ZoomPercentage);

+ 14 - 23
PixiEditorTests/ViewModelsTests/ViewModelMainTests.cs

@@ -18,19 +18,10 @@ namespace PixiEditorTests.ViewModelsTests
     [Collection("Application collection")]
     public class ViewModelMainTests
     {
-        public static IServiceProvider Services;
-
-        public ViewModelMainTests()
-        {
-            Services = new ServiceCollection()
-                .AddSingleton<IPreferences>(new Mocks.PreferenceSettingsMock())
-                .BuildServiceProvider();
-        }
-
         [StaFact]
         public void TestThatConstructorSetsUpControllersCorrectly()
         {
-            ViewModelMain viewModel = new ViewModelMain(Services);
+            ViewModelMain viewModel = Helpers.MockedViewModelMain();
 
             Assert.NotNull(viewModel.ChangesController);
             Assert.NotNull(viewModel.ShortcutController);
@@ -42,7 +33,7 @@ namespace PixiEditorTests.ViewModelsTests
         [StaFact]
         public void TestThatSwapColorsCommandSwapsColors()
         {
-            ViewModelMain viewModel = new ViewModelMain(Services);
+            ViewModelMain viewModel = Helpers.MockedViewModelMain();
 
             viewModel.ColorsSubViewModel.PrimaryColor = Colors.Black;
             viewModel.ColorsSubViewModel.SecondaryColor = Colors.White;
@@ -56,7 +47,7 @@ namespace PixiEditorTests.ViewModelsTests
         [StaFact]
         public void TestThatNewDocumentCreatesNewDocumentWithBaseLayer()
         {
-            ViewModelMain viewModel = new ViewModelMain(Services);
+            ViewModelMain viewModel = Helpers.MockedViewModelMain();
 
             viewModel.FileSubViewModel.NewDocument(5, 5);
 
@@ -67,7 +58,7 @@ namespace PixiEditorTests.ViewModelsTests
         [StaFact]
         public void TestThatMouseMoveCommandUpdatesCurrentCoordinates()
         {
-            ViewModelMain viewModel = new ViewModelMain(Services);
+            ViewModelMain viewModel = Helpers.MockedViewModelMain();
             viewModel.BitmapManager.ActiveDocument = new Document(10, 10);
 
             Assert.Equal(new Coordinates(0, 0), MousePositionConverter.CurrentCoordinates);
@@ -83,7 +74,7 @@ namespace PixiEditorTests.ViewModelsTests
         [StaFact]
         public void TestThatSelectToolCommandSelectsNewTool()
         {
-            ViewModelMain viewModel = new ViewModelMain(Services);
+            ViewModelMain viewModel = Helpers.MockedViewModelMain();
 
             Assert.Equal(typeof(MoveViewportTool), viewModel.BitmapManager.SelectedTool.GetType());
 
@@ -95,7 +86,7 @@ namespace PixiEditorTests.ViewModelsTests
         [StaFact]
         public void TestThatMouseUpCommandStopsRecordingMouseMovements()
         {
-            ViewModelMain viewModel = new ViewModelMain(Services);
+            ViewModelMain viewModel = Helpers.MockedViewModelMain();
 
             viewModel.BitmapManager.MouseController.StartRecordingMouseMovementChanges(true);
 
@@ -109,7 +100,7 @@ namespace PixiEditorTests.ViewModelsTests
         [StaFact]
         public void TestThatNewLayerCommandCreatesNewLayer()
         {
-            ViewModelMain viewModel = new ViewModelMain(Services);
+            ViewModelMain viewModel = Helpers.MockedViewModelMain();
 
             viewModel.BitmapManager.ActiveDocument = new Document(1, 1);
 
@@ -123,7 +114,7 @@ namespace PixiEditorTests.ViewModelsTests
         [StaFact]
         public void TestThatSaveDocumentCommandSavesFile()
         {
-            ViewModelMain viewModel = new ViewModelMain(Services);
+            ViewModelMain viewModel = Helpers.MockedViewModelMain();
             string fileName = "testFile.pixi";
 
             viewModel.BitmapManager.ActiveDocument = new Document(1, 1)
@@ -141,7 +132,7 @@ namespace PixiEditorTests.ViewModelsTests
         [StaFact]
         public void TestThatAddSwatchAddsNonDuplicateSwatch()
         {
-            ViewModelMain viewModel = new ViewModelMain(Services);
+            ViewModelMain viewModel = Helpers.MockedViewModelMain();
             viewModel.BitmapManager.ActiveDocument = new Document(1, 1);
 
             viewModel.ColorsSubViewModel.AddSwatch(Colors.Green);
@@ -161,10 +152,10 @@ namespace PixiEditorTests.ViewModelsTests
         [InlineData(120, 150)]
         public void TestThatSelectAllCommandSelectsWholeDocument(int docWidth, int docHeight)
         {
-            ViewModelMain viewModel = new ViewModelMain(Services)
-            {
-                BitmapManager = { ActiveDocument = new Document(docWidth, docHeight) }
-            };
+            ViewModelMain viewModel = Helpers.MockedViewModelMain();
+
+            viewModel.BitmapManager.ActiveDocument = new Document(docWidth, docHeight);
+
             viewModel.BitmapManager.ActiveDocument.AddNewLayer("layer");
 
             viewModel.SelectionSubViewModel.SelectAllCommand.Execute(null);
@@ -177,7 +168,7 @@ namespace PixiEditorTests.ViewModelsTests
         [StaFact]
         public void TestThatDocumentIsNotNullReturnsTrue()
         {
-            ViewModelMain viewModel = new ViewModelMain(Services);
+            ViewModelMain viewModel = Helpers.MockedViewModelMain();
 
             viewModel.BitmapManager.ActiveDocument = new Document(1, 1);