Browse Source

Wrote some ViewModelMain tests

flabbet 5 years ago
parent
commit
4f5a62f25a

+ 6 - 0
PixiEditor/Models/Controllers/BitmapOperationsUtility.cs

@@ -43,6 +43,12 @@ namespace PixiEditor.Models.Controllers
             UndoManager.AddUndoChange(new Change("UndoChanges", old, newChange, "Deleted pixels"));
         }
 
+        /// <summary>
+        ///     Executes tool Use() method with given parameters. NOTE: mouseMove is reversed inside function!
+        /// </summary>
+        /// <param name="newPos">Most recent coordinates</param>
+        /// <param name="mouseMove">Last mouse movement coordinates</param>
+        /// <param name="tool">Tool to execute</param>
         public void ExecuteTool(Coordinates newPos, List<Coordinates> mouseMove, BitmapOperationTool tool)
         {
             if (Manager.ActiveDocument != null && tool != null && tool.ToolType != ToolType.None)

+ 1 - 1
PixiEditor/Models/Tools/Tools/MoveTool.cs

@@ -64,7 +64,7 @@ namespace PixiEditor.Models.Tools.Tools
 
         public override void OnMouseUp() //This adds undo if there is no selection, reason why this isn't in AfterUndoAdded,
         {   //is because it doesn't fire if no pixel changes were made.
-            if (_currentSelection.Length == 0)
+            if (_currentSelection != null && _currentSelection.Length == 0)
             {
                 UndoManager.AddUndoChange(new Change(ApplyOffsets, new object[]{_startingOffsets}, 
                     ApplyOffsets, new object[] { GetOffsets(_affectedLayers)}, "Move layers"));

+ 4 - 4
PixiEditor/ViewModels/ViewModelBase.cs

@@ -5,21 +5,21 @@ using System.Windows.Input;
 
 namespace PixiEditor.ViewModels
 {
-    internal class ViewModelBase : INotifyPropertyChanged
+    public class ViewModelBase : INotifyPropertyChanged
     {
         public event PropertyChangedEventHandler PropertyChanged = delegate { };
 
-        internal void RaisePropertyChanged(string property)
+        protected void RaisePropertyChanged(string property)
         {
             if (property != null) PropertyChanged(this, new PropertyChangedEventArgs(property));
         }
 
-        internal void CloseButton(object parameter)
+        protected void CloseButton(object parameter)
         {
             ((Window) parameter).Close();
         }
 
-        internal void DragMove(object parameter)
+        protected void DragMove(object parameter)
         {
             Window popup = Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive);
             if (Mouse.LeftButton == MouseButtonState.Pressed) popup.DragMove();

+ 87 - 87
PixiEditor/ViewModels/ViewModelMain.cs

@@ -23,7 +23,7 @@ using PixiEditor.Models.Tools.Tools;
 
 namespace PixiEditor.ViewModels
 {
-    internal class ViewModelMain : ViewModelBase
+    public class ViewModelMain : ViewModelBase
     {
         private const string ConfirmationDialogMessage = "Document was modified. Do you want to save changes?";
 
@@ -41,90 +41,6 @@ namespace PixiEditor.ViewModels
 
         private bool _unsavedDocumentModified;
 
-        public ViewModelMain()
-        {
-            BitmapManager = new BitmapManager();
-            BitmapManager.BitmapOperations.BitmapChanged += BitmapUtility_BitmapChanged;
-            BitmapManager.MouseController.StoppedRecordingChanges += MouseController_StoppedRecordingChanges;
-            BitmapManager.DocumentChanged += BitmapManager_DocumentChanged;
-            ChangesController = new PixelChangesController();
-            SelectToolCommand = new RelayCommand(SetTool, DocumentIsNotNull);
-            OpenNewFilePopupCommand = new RelayCommand(OpenNewFilePopup);
-            MouseMoveCommand = new RelayCommand(MouseMove);
-            MouseDownCommand = new RelayCommand(MouseDown);
-            SaveFileCommand = new RelayCommand(SaveFile, CanSave);
-            UndoCommand = new RelayCommand(Undo, CanUndo);
-            RedoCommand = new RelayCommand(Redo, CanRedo);
-            MouseUpCommand = new RelayCommand(MouseUp);
-            OpenFileCommand = new RelayCommand(Open);
-            SetActiveLayerCommand = new RelayCommand(SetActiveLayer);
-            NewLayerCommand = new RelayCommand(NewLayer, CanCreateNewLayer);
-            DeleteLayerCommand = new RelayCommand(DeleteLayer, CanDeleteLayer);
-            MoveToBackCommand = new RelayCommand(MoveLayerToBack, CanMoveToBack);
-            MoveToFrontCommand = new RelayCommand(MoveLayerToFront, CanMoveToFront);
-            SwapColorsCommand = new RelayCommand(SwapColors);
-            KeyDownCommand = new RelayCommand(KeyDown);
-            RenameLayerCommand = new RelayCommand(RenameLayer);
-            DeselectCommand = new RelayCommand(Deselect, SelectionIsNotEmpty);
-            SelectAllCommand = new RelayCommand(SelectAll, CanSelectAll);
-            CopyCommand = new RelayCommand(Copy, SelectionIsNotEmpty);
-            DuplicateCommand = new RelayCommand(Duplicate, SelectionIsNotEmpty);
-            CutCommand = new RelayCommand(Cut, SelectionIsNotEmpty);
-            PasteCommand = new RelayCommand(Paste, CanPaste);
-            ClipCanvasCommand = new RelayCommand(ClipCanvas, DocumentIsNotNull);
-            DeletePixelsCommand = new RelayCommand(DeletePixels, SelectionIsNotEmpty);
-            OpenResizePopupCommand = new RelayCommand(OpenResizePopup, DocumentIsNotNull);
-            SelectColorCommand = new RelayCommand(SelectColor);
-            RemoveSwatchCommand = new RelayCommand(RemoveSwatch);
-            SaveDocumentCommand = new RelayCommand(SaveDocument, DocumentIsNotNull);
-            OnStartupCommand = new RelayCommand(OnStartup);
-            CloseWindowCommand = new RelayCommand(CloseWindow);
-            CenterContentCommand = new RelayCommand(CenterContent, DocumentIsNotNull);
-            ToolSet = new ObservableCollection<Tool>
-            {
-                new MoveTool(), new PenTool(), new SelectTool(), new FloodFill(), new LineTool(),
-                new CircleTool(), new RectangleTool(), new EarserTool(), new ColorPickerTool(), new BrightnessTool()
-            };
-            ShortcutController = new ShortcutController
-            {
-                Shortcuts = new List<Shortcut>
-                {
-                    new Shortcut(Key.B, SelectToolCommand, ToolType.Pen),
-                    new Shortcut(Key.X, SwapColorsCommand),
-                    new Shortcut(Key.O, OpenFileCommand, modifier: ModifierKeys.Control),
-                    new Shortcut(Key.E, SelectToolCommand, ToolType.Earser),
-                    new Shortcut(Key.O, SelectToolCommand, ToolType.ColorPicker),
-                    new Shortcut(Key.R, SelectToolCommand, ToolType.Rectangle),
-                    new Shortcut(Key.C, SelectToolCommand, ToolType.Circle),
-                    new Shortcut(Key.L, SelectToolCommand, ToolType.Line),
-                    new Shortcut(Key.G, SelectToolCommand, ToolType.Bucket),
-                    new Shortcut(Key.U, SelectToolCommand, ToolType.Brightness),
-                    new Shortcut(Key.V, SelectToolCommand, ToolType.Move),
-                    new Shortcut(Key.M, SelectToolCommand, ToolType.Select),
-                    new Shortcut(Key.Y, RedoCommand, modifier: ModifierKeys.Control),
-                    new Shortcut(Key.Z, UndoCommand),
-                    new Shortcut(Key.S, SaveFileCommand,
-                        modifier: ModifierKeys.Control | ModifierKeys.Shift | ModifierKeys.Alt),
-                    new Shortcut(Key.S, SaveDocumentCommand, modifier: ModifierKeys.Control),
-                    new Shortcut(Key.S, SaveDocumentCommand, "AsNew", ModifierKeys.Control | ModifierKeys.Shift),
-                    new Shortcut(Key.N, OpenNewFilePopupCommand, modifier: ModifierKeys.Control),
-                    new Shortcut(Key.D, DeselectCommand, modifier: ModifierKeys.Control),
-                    new Shortcut(Key.A, SelectAllCommand, modifier: ModifierKeys.Control),
-                    new Shortcut(Key.C, CopyCommand, modifier: ModifierKeys.Control),
-                    new Shortcut(Key.V, PasteCommand, modifier: ModifierKeys.Control),
-                    new Shortcut(Key.J, DuplicateCommand, modifier: ModifierKeys.Control),
-                    new Shortcut(Key.X, CutCommand, modifier: ModifierKeys.Control),
-                    new Shortcut(Key.Delete, DeletePixelsCommand),
-                    new Shortcut(Key.I, OpenResizePopupCommand, modifier: ModifierKeys.Control | ModifierKeys.Shift),
-                    new Shortcut(Key.C, OpenResizePopupCommand, "canvas", ModifierKeys.Control | ModifierKeys.Shift)
-                }
-            };
-            UndoManager.SetMainRoot(this);
-            SetActiveTool(ToolType.Move);
-            BitmapManager.PrimaryColor = PrimaryColor;
-            Current = this;
-        }
-
         public Action CloseAction { get; set; }
 
         public static ViewModelMain Current { get; set; }
@@ -224,7 +140,7 @@ namespace PixiEditor.ViewModels
 
         public ObservableCollection<Tool> ToolSet { get; set; }
 
-        public LayerChange[] UndoChanges
+        public LayerChange[] UndoChanges //This acts like UndoManager process, but it was implemented before process system, so it can be transformed into it
         {
             get => _undoChanges;
             set
@@ -260,6 +176,90 @@ namespace PixiEditor.ViewModels
             }
         }
 
+        public ViewModelMain()
+        {
+            BitmapManager = new BitmapManager();
+            BitmapManager.BitmapOperations.BitmapChanged += BitmapUtility_BitmapChanged;
+            BitmapManager.MouseController.StoppedRecordingChanges += MouseController_StoppedRecordingChanges;
+            BitmapManager.DocumentChanged += BitmapManager_DocumentChanged;
+            ChangesController = new PixelChangesController();
+            SelectToolCommand = new RelayCommand(SetTool, DocumentIsNotNull);
+            OpenNewFilePopupCommand = new RelayCommand(OpenNewFilePopup);
+            MouseMoveCommand = new RelayCommand(MouseMove);
+            MouseDownCommand = new RelayCommand(MouseDown);
+            SaveFileCommand = new RelayCommand(SaveFile, CanSave);
+            UndoCommand = new RelayCommand(Undo, CanUndo);
+            RedoCommand = new RelayCommand(Redo, CanRedo);
+            MouseUpCommand = new RelayCommand(MouseUp);
+            OpenFileCommand = new RelayCommand(Open);
+            SetActiveLayerCommand = new RelayCommand(SetActiveLayer);
+            NewLayerCommand = new RelayCommand(NewLayer, CanCreateNewLayer);
+            DeleteLayerCommand = new RelayCommand(DeleteLayer, CanDeleteLayer);
+            MoveToBackCommand = new RelayCommand(MoveLayerToBack, CanMoveToBack);
+            MoveToFrontCommand = new RelayCommand(MoveLayerToFront, CanMoveToFront);
+            SwapColorsCommand = new RelayCommand(SwapColors);
+            KeyDownCommand = new RelayCommand(KeyDown);
+            RenameLayerCommand = new RelayCommand(RenameLayer);
+            DeselectCommand = new RelayCommand(Deselect, SelectionIsNotEmpty);
+            SelectAllCommand = new RelayCommand(SelectAll, CanSelectAll);
+            CopyCommand = new RelayCommand(Copy, SelectionIsNotEmpty);
+            DuplicateCommand = new RelayCommand(Duplicate, SelectionIsNotEmpty);
+            CutCommand = new RelayCommand(Cut, SelectionIsNotEmpty);
+            PasteCommand = new RelayCommand(Paste, CanPaste);
+            ClipCanvasCommand = new RelayCommand(ClipCanvas, DocumentIsNotNull);
+            DeletePixelsCommand = new RelayCommand(DeletePixels, SelectionIsNotEmpty);
+            OpenResizePopupCommand = new RelayCommand(OpenResizePopup, DocumentIsNotNull);
+            SelectColorCommand = new RelayCommand(SelectColor);
+            RemoveSwatchCommand = new RelayCommand(RemoveSwatch);
+            SaveDocumentCommand = new RelayCommand(SaveDocument, DocumentIsNotNull);
+            OnStartupCommand = new RelayCommand(OnStartup);
+            CloseWindowCommand = new RelayCommand(CloseWindow);
+            CenterContentCommand = new RelayCommand(CenterContent, DocumentIsNotNull);
+            ToolSet = new ObservableCollection<Tool>
+            {
+                new MoveTool(), new PenTool(), new SelectTool(), new FloodFill(), new LineTool(),
+                new CircleTool(), new RectangleTool(), new EarserTool(), new ColorPickerTool(), new BrightnessTool()
+            };
+            ShortcutController = new ShortcutController
+            {
+                Shortcuts = new List<Shortcut>
+                {
+                    new Shortcut(Key.B, SelectToolCommand, ToolType.Pen),
+                    new Shortcut(Key.X, SwapColorsCommand),
+                    new Shortcut(Key.O, OpenFileCommand, modifier: ModifierKeys.Control),
+                    new Shortcut(Key.E, SelectToolCommand, ToolType.Earser),
+                    new Shortcut(Key.O, SelectToolCommand, ToolType.ColorPicker),
+                    new Shortcut(Key.R, SelectToolCommand, ToolType.Rectangle),
+                    new Shortcut(Key.C, SelectToolCommand, ToolType.Circle),
+                    new Shortcut(Key.L, SelectToolCommand, ToolType.Line),
+                    new Shortcut(Key.G, SelectToolCommand, ToolType.Bucket),
+                    new Shortcut(Key.U, SelectToolCommand, ToolType.Brightness),
+                    new Shortcut(Key.V, SelectToolCommand, ToolType.Move),
+                    new Shortcut(Key.M, SelectToolCommand, ToolType.Select),
+                    new Shortcut(Key.Y, RedoCommand, modifier: ModifierKeys.Control),
+                    new Shortcut(Key.Z, UndoCommand),
+                    new Shortcut(Key.S, SaveFileCommand,
+                        modifier: ModifierKeys.Control | ModifierKeys.Shift | ModifierKeys.Alt),
+                    new Shortcut(Key.S, SaveDocumentCommand, modifier: ModifierKeys.Control),
+                    new Shortcut(Key.S, SaveDocumentCommand, "AsNew", ModifierKeys.Control | ModifierKeys.Shift),
+                    new Shortcut(Key.N, OpenNewFilePopupCommand, modifier: ModifierKeys.Control),
+                    new Shortcut(Key.D, DeselectCommand, modifier: ModifierKeys.Control),
+                    new Shortcut(Key.A, SelectAllCommand, modifier: ModifierKeys.Control),
+                    new Shortcut(Key.C, CopyCommand, modifier: ModifierKeys.Control),
+                    new Shortcut(Key.V, PasteCommand, modifier: ModifierKeys.Control),
+                    new Shortcut(Key.J, DuplicateCommand, modifier: ModifierKeys.Control),
+                    new Shortcut(Key.X, CutCommand, modifier: ModifierKeys.Control),
+                    new Shortcut(Key.Delete, DeletePixelsCommand),
+                    new Shortcut(Key.I, OpenResizePopupCommand, modifier: ModifierKeys.Control | ModifierKeys.Shift),
+                    new Shortcut(Key.C, OpenResizePopupCommand, "canvas", ModifierKeys.Control | ModifierKeys.Shift)
+                }
+            };
+            UndoManager.SetMainRoot(this);
+            SetActiveTool(ToolType.Move);
+            BitmapManager.PrimaryColor = PrimaryColor;
+            Current = this;
+        }
+
         private void CenterContent(object property)
         {
             BitmapManager.ActiveDocument.CenterContent();
@@ -633,7 +633,7 @@ namespace PixiEditor.ViewModels
             }
         }
 
-        private void NewDocument(int width, int height, bool addBaseLayer = true)
+        public void NewDocument(int width, int height, bool addBaseLayer = true)
         {
             BitmapManager.ActiveDocument = new Document(width, height);
             if(addBaseLayer)

+ 0 - 4
PixiEditorTests/PixiEditorTests.csproj

@@ -27,10 +27,6 @@
     <PackageReference Include="Xunit.StaFact" Version="0.3.18" />
   </ItemGroup>
 
-  <ItemGroup>
-    <Folder Include="ViewModelsTests\" />
-  </ItemGroup>
-
   <ItemGroup>
     <ProjectReference Include="..\PixiEditor\PixiEditor.csproj" />
   </ItemGroup>

+ 120 - 0
PixiEditorTests/ViewModelsTests/ViewModelMainTests.cs

@@ -0,0 +1,120 @@
+using System.Windows;
+using System.Windows.Input;
+using System.Windows.Media;
+using PixiEditor;
+using PixiEditor.Models.Controllers;
+using PixiEditor.Models.DataHolders;
+using PixiEditor.Models.Position;
+using PixiEditor.Models.Tools;
+using PixiEditor.ViewModels;
+using Xunit;
+
+namespace PixiEditorTests.ViewModelsTests
+{
+    public class ViewModelMainTests
+    {
+
+        public ViewModelMainTests()
+        {
+            if (Application.Current == null)
+            {
+                var app = new App();
+                app.InitializeComponent();
+            }
+        }
+
+        [StaFact]
+        public void TestThatConstructorSetsUpControllersCorrectly()
+        {
+            ViewModelMain viewModel = new ViewModelMain();
+
+            Assert.Equal(viewModel, UndoManager.MainRoot);
+            Assert.NotNull(viewModel.ChangesController);
+            Assert.NotNull(viewModel.ShortcutController);
+            Assert.NotEmpty(viewModel.ShortcutController.Shortcuts);
+            Assert.NotNull(viewModel.BitmapManager);
+            Assert.Equal(viewModel, ViewModelMain.Current);
+        }
+
+        [StaFact]
+        public void TestThatSwapColorsCommandSwapsColors()
+        {
+            ViewModelMain viewModel = new ViewModelMain();
+
+            viewModel.PrimaryColor = Colors.Black;
+            viewModel.SecondaryColor = Colors.White;
+
+            viewModel.SwapColorsCommand.Execute(null);
+
+            Assert.Equal(Colors.White, viewModel.PrimaryColor);
+            Assert.Equal(Colors.Black, viewModel.SecondaryColor);
+        }
+
+        [StaFact]
+        public void TestThatNewDocumentCreatesNewDocumentWithBaseLayer()
+        {
+            ViewModelMain viewModel = new ViewModelMain();
+
+            viewModel.NewDocument(5,5);
+
+            Assert.NotNull(viewModel.BitmapManager.ActiveDocument);
+            Assert.Single(viewModel.BitmapManager.ActiveDocument.Layers);
+        }
+
+        [StaFact]
+        public void TestThatMouseMoveCommandUpdatesCurrentCoordinates()
+        {
+            ViewModelMain viewModel = new ViewModelMain();
+
+            Assert.Equal(new Coordinates(0, 0), MousePositionConverter.CurrentCoordinates);
+
+            viewModel.MouseXOnCanvas = 5;
+            viewModel.MouseYOnCanvas = 5;
+
+            viewModel.MouseMoveCommand.Execute(null);
+
+            Assert.Equal(new Coordinates(5,5), MousePositionConverter.CurrentCoordinates);
+        }
+
+        [StaFact]
+        public void TestThatSelectToolCommandSelectsNewTool()
+        {
+            ViewModelMain viewModel = new ViewModelMain();
+
+            Assert.Equal(ToolType.Move,viewModel.BitmapManager.SelectedTool.ToolType);
+
+            viewModel.SelectToolCommand.Execute(ToolType.Line);
+
+            Assert.Equal(ToolType.Line, viewModel.BitmapManager.SelectedTool.ToolType);
+        }
+
+        [StaFact]
+        public void TestThatMouseUpCommandStopsRecordingMouseMovements()
+        {
+            ViewModelMain viewModel = new ViewModelMain();
+
+            viewModel.BitmapManager.MouseController.StartRecordingMouseMovementChanges(true);
+
+            Assert.True(viewModel.BitmapManager.MouseController.IsRecordingChanges);
+
+            viewModel.MouseUpCommand.Execute(null);
+
+            Assert.False(viewModel.BitmapManager.MouseController.IsRecordingChanges);
+        }
+
+        [StaFact]
+        public void TestThatNewLayerCommandCreatesNewLayer()
+        {
+            ViewModelMain viewModel = new ViewModelMain();
+
+            viewModel.BitmapManager.ActiveDocument = new Document(1,1);
+
+            Assert.Empty(viewModel.BitmapManager.ActiveDocument.Layers);
+
+            viewModel.NewLayerCommand.Execute(null);
+
+            Assert.Single(viewModel.BitmapManager.ActiveDocument.Layers);
+        }
+
+    }
+}