Browse Source

Wrote another bunch of tests

flabbet 5 years ago
parent
commit
061f69d274

+ 1 - 1
PixiEditor/Models/Controllers/BitmapManager.cs

@@ -73,7 +73,7 @@ namespace PixiEditor.Models.Controllers
             MouseController.MousePositionChanged += Controller_MousePositionChanged;
             MouseController.MousePositionChanged += Controller_MousePositionChanged;
             MouseController.StoppedRecordingChanges += MouseController_StoppedRecordingChanges;
             MouseController.StoppedRecordingChanges += MouseController_StoppedRecordingChanges;
             BitmapOperations = new BitmapOperationsUtility(this);
             BitmapOperations = new BitmapOperationsUtility(this);
-            ReadonlyToolUtility = new ReadonlyToolUtility(this);
+            ReadonlyToolUtility = new ReadonlyToolUtility();
         }
         }
 
 
         public event EventHandler<LayersChangedEventArgs> LayersChanged;
         public event EventHandler<LayersChangedEventArgs> LayersChanged;

+ 9 - 0
PixiEditor/Models/Controllers/PixelChangesController.cs

@@ -13,6 +13,11 @@ namespace PixiEditor.Models.Controllers
         private Dictionary<int, LayerChange> LastChanges { get; set; }
         private Dictionary<int, LayerChange> LastChanges { get; set; }
         private Dictionary<int, LayerChange> LastOldValues { get; set; }
         private Dictionary<int, LayerChange> LastOldValues { get; set; }
 
 
+        /// <summary>
+        ///     Adds layer changes to controller
+        /// </summary>
+        /// <param name="changes">New changes</param>
+        /// <param name="oldValues">Old values of changes</param>
         public void AddChanges(LayerChange changes, LayerChange oldValues)
         public void AddChanges(LayerChange changes, LayerChange oldValues)
         {
         {
             if (changes.PixelChanges.ChangedPixels.Count > 0)
             if (changes.PixelChanges.ChangedPixels.Count > 0)
@@ -54,6 +59,10 @@ namespace PixiEditor.Models.Controllers
                     LastOldValues[layerChange.LayerIndex].PixelChanges.ChangedPixels.Add(change.Key, change.Value);
                     LastOldValues[layerChange.LayerIndex].PixelChanges.ChangedPixels.Add(change.Key, change.Value);
         }
         }
 
 
+        /// <summary>
+        ///     Returns all changes and deletes them from controller.
+        /// </summary>
+        /// <returns>Tuple array with new changes and old values</returns>
         public Tuple<LayerChange, LayerChange>[] PopChanges()
         public Tuple<LayerChange, LayerChange>[] PopChanges()
         {
         {
             //Maybe replace Tuple with custom data type
             //Maybe replace Tuple with custom data type

+ 0 - 7
PixiEditor/Models/Controllers/ReadonlyToolUtility.cs

@@ -5,13 +5,6 @@ namespace PixiEditor.Models.Controllers
 {
 {
     public class ReadonlyToolUtility
     public class ReadonlyToolUtility
     {
     {
-        public BitmapManager Manager { get; set; }
-
-        public ReadonlyToolUtility(BitmapManager manager)
-        {
-            Manager = manager;
-        }
-
         public void ExecuteTool(Coordinates[] mouseMove, ReadonlyTool tool)
         public void ExecuteTool(Coordinates[] mouseMove, ReadonlyTool tool)
         {
         {
             tool.Use(mouseMove);
             tool.Use(mouseMove);

+ 1 - 1
PixiEditor/Models/Controllers/UndoManager.cs

@@ -61,7 +61,7 @@ namespace PixiEditor.Models.Controllers
         {
         {
             _lastChangeWasUndo = true;
             _lastChangeWasUndo = true;
             Change change = RedoStack.Pop();
             Change change = RedoStack.Pop();
-            if (change.ReverseProcess == null)
+            if (change.Process == null)
                 SetPropertyValue(change.Root, change.Property, change.NewValue);
                 SetPropertyValue(change.Root, change.Property, change.NewValue);
             else
             else
                 change.Process(change.ProcessArguments);
                 change.Process(change.ProcessArguments);

+ 1 - 1
PixiEditor/Models/DataHolders/BitmapPixelChanges.cs

@@ -6,7 +6,7 @@ using PixiEditor.Exceptions;
 using PixiEditor.Helpers.Extensions;
 using PixiEditor.Helpers.Extensions;
 using PixiEditor.Models.Position;
 using PixiEditor.Models.Position;
 
 
-namespace PixiEditor.Models.Tools
+namespace PixiEditor.Models.DataHolders
 {
 {
     public struct BitmapPixelChanges
     public struct BitmapPixelChanges
     {
     {

+ 27 - 2
PixiEditor/Models/DataHolders/Change.cs

@@ -19,8 +19,16 @@ namespace PixiEditor.Models.DataHolders
         public object[] ReverseProcessArguments;
         public object[] ReverseProcessArguments;
         public object Root { get; set; }
         public object Root { get; set; }
 
 
-
-        public Change(string property, object oldValue, object newValue, string description = "", object root = null)
+        /// <summary>
+        ///     Creates new change for property based undo system
+        /// </summary>
+        /// <param name="property">Name of property</param>
+        /// <param name="oldValue">Old value of property</param>
+        /// <param name="newValue">New value of property</param>
+        /// <param name="description">Description of change</param>
+        /// <param name="root">Custom root for finding property</param>
+        public Change(string property, object oldValue, object newValue,
+            string description = "", object root = null)
         {
         {
             Property = property;
             Property = property;
             OldValue = oldValue;
             OldValue = oldValue;
@@ -29,6 +37,15 @@ namespace PixiEditor.Models.DataHolders
             Root = root;
             Root = root;
         }
         }
 
 
+        /// <summary>
+        ///     Creates new change for mixed reverse process based system with new value property based system
+        /// </summary>
+        /// <param name="property">Name of property, which new value will be applied to</param>
+        /// <param name="reverseProcess">Method with reversing value process</param>
+        /// <param name="reverseArguments">Arguments for reverse method</param>
+        /// <param name="newValue">New value of property</param>
+        /// <param name="description">Description of change</param>
+        /// <param name="root">Custom root for finding property</param>
         public Change(string property, Action<object[]> reverseProcess, object[] reverseArguments,
         public Change(string property, Action<object[]> reverseProcess, object[] reverseArguments,
             object newValue, string description = "", object root = null)
             object newValue, string description = "", object root = null)
         {
         {
@@ -40,6 +57,14 @@ namespace PixiEditor.Models.DataHolders
             Root = root;
             Root = root;
         }
         }
 
 
+        /// <summary>
+        ///     Creates new change for reverse process based system
+        /// </summary>
+        /// <param name="reverseProcess">Method with reversing value process</param>
+        /// <param name="reverseArguments">Arguments for reverse method</param>
+        /// <param name="process">Method with reversing the reversed value</param>
+        /// <param name="processArguments">Arguments for process method</param>
+        /// <param name="description">Description of change</param>
         public Change(Action<object[]> reverseProcess, object[] reverseArguments,
         public Change(Action<object[]> reverseProcess, object[] reverseArguments,
             Action<object[]> process, object[] processArguments, string description = "")
             Action<object[]> process, object[] processArguments, string description = "")
         {
         {

+ 1 - 0
PixiEditor/Models/Layers/Layer.cs

@@ -4,6 +4,7 @@ using System.Linq;
 using System.Windows;
 using System.Windows;
 using System.Windows.Media;
 using System.Windows.Media;
 using System.Windows.Media.Imaging;
 using System.Windows.Media.Imaging;
+using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.Position;
 using PixiEditor.Models.Position;
 using PixiEditor.Models.Tools;
 using PixiEditor.Models.Tools;
 
 

+ 65 - 0
PixiEditorTests/ModelsTests/ControllersTests/PixelChangesControllerTests.cs

@@ -0,0 +1,65 @@
+using System.Windows.Media;
+using PixiEditor.Models.Controllers;
+using PixiEditor.Models.DataHolders;
+using PixiEditor.Models.Position;
+using PixiEditor.Models.Tools;
+using Xunit;
+
+namespace PixiEditorTests.ModelsTests.ControllersTests
+{
+    public class PixelChangesControllerTests
+    {
+
+        [Fact]
+        public void TestThatPopChangesPopsChanges()
+        {
+            var controller = CreateBasicController();
+
+            var changes = controller.PopChanges();
+            Assert.NotEmpty(changes);
+            Assert.Null(controller.PopChanges());
+        }
+
+        [Fact]
+        public void TestThatAddChangesAddsAsNewChange()
+        {
+            var controller = CreateBasicController();
+            Coordinates[] cords = { new Coordinates(5, 3), new Coordinates(7, 2) };
+
+            controller.AddChanges(new LayerChange(
+                    BitmapPixelChanges.FromSingleColoredArray(cords, Colors.Black), 1),
+                new LayerChange(BitmapPixelChanges.FromSingleColoredArray(cords, Colors.Transparent), 1));
+
+            var changes = controller.PopChanges();
+            Assert.Equal(2, changes.Length);
+        }
+
+        [Fact]
+        public void TestThatAddChangesAddsToExistingChange()
+        {
+            Coordinates[] cords2 = { new Coordinates(2, 2), new Coordinates(5, 5) };
+            var controller = CreateBasicController();
+
+            controller.AddChanges(new LayerChange(
+                    BitmapPixelChanges.FromSingleColoredArray(cords2, Colors.Black), 0),
+                new LayerChange(BitmapPixelChanges.FromSingleColoredArray(cords2, Colors.Transparent), 0));
+
+            var changes = controller.PopChanges();
+            Assert.Single(changes);
+            Assert.Equal(4, changes[0].Item1.PixelChanges.ChangedPixels.Count);
+            Assert.Equal(4, changes[0].Item2.PixelChanges.ChangedPixels.Count);
+        }
+
+        private PixelChangesController CreateBasicController()
+        {
+            Coordinates[] cords = { new Coordinates(0, 0), new Coordinates(1, 1) };
+            PixelChangesController controller = new PixelChangesController();
+
+            controller.AddChanges(new LayerChange(
+                    BitmapPixelChanges.FromSingleColoredArray(cords, Colors.Black), 0),
+                new LayerChange(BitmapPixelChanges.FromSingleColoredArray(cords, Colors.Transparent), 0));
+            return controller;
+        }
+
+    }
+}

+ 40 - 0
PixiEditorTests/ModelsTests/ControllersTests/ReadonlyUtilityTests.cs

@@ -0,0 +1,40 @@
+using System;
+using System.Collections.Generic;
+using System.Security.Cryptography.X509Certificates;
+using System.Text;
+using PixiEditor.Models.Controllers;
+using PixiEditor.Models.Position;
+using PixiEditor.Models.Tools;
+using Xunit;
+
+namespace PixiEditorTests.ModelsTests.ControllersTests
+{
+    public class ReadonlyUtilityTests
+    {
+        [Fact]
+        public void TestThatExecuteToolExecutesTool()
+        {
+            bool toolUsed = false;
+
+            ReadonlyToolUtility util = new ReadonlyToolUtility();
+            util.ExecuteTool(new[]{new Coordinates(0,0)}, new TestReadonlyTool(() => toolUsed = true));
+            Assert.True(toolUsed);
+        }
+
+    }
+
+    public class TestReadonlyTool : ReadonlyTool
+    {
+        public Action ToolAction { get; set; }
+        public TestReadonlyTool(Action toolAction)
+        {
+            ToolAction = toolAction;
+        }
+
+        public override ToolType ToolType => ToolType.Select;
+        public override void Use(Coordinates[] pixels)
+        {
+            ToolAction();
+        }
+    }
+}

+ 110 - 7
PixiEditorTests/ModelsTests/ControllersTests/UndoManagerTests.cs

@@ -1,4 +1,5 @@
-using PixiEditor.Models.Controllers;
+using System;
+using PixiEditor.Models.Controllers;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.DataHolders;
 using Xunit;
 using Xunit;
 
 
@@ -8,6 +9,7 @@ namespace PixiEditorTests.ModelsTests.ControllersTests
     {
     {
 
 
         public int ExampleProperty { get; set; } = 1;
         public int ExampleProperty { get; set; } = 1;
+        public TestPropertyClass TestPropClass { get; set; } = new TestPropertyClass();
 
 
         [Fact]
         [Fact]
         public void TestSetRoot()
         public void TestSetRoot()
@@ -19,7 +21,7 @@ namespace PixiEditorTests.ModelsTests.ControllersTests
         [Fact]
         [Fact]
         public void TestAddToUndoStack()
         public void TestAddToUndoStack()
         {
         {
-            PrepareUnoManagerForTests();
+            PrepareUnoManagerForTest();
             UndoManager.AddUndoChange(new Change("ExampleProperty", ExampleProperty, ExampleProperty));
             UndoManager.AddUndoChange(new Change("ExampleProperty", ExampleProperty, ExampleProperty));
             Assert.True(UndoManager.UndoStack.Count == 1);
             Assert.True(UndoManager.UndoStack.Count == 1);
             Assert.True((int)UndoManager.UndoStack.Peek().OldValue == ExampleProperty);
             Assert.True((int)UndoManager.UndoStack.Peek().OldValue == ExampleProperty);
@@ -28,7 +30,7 @@ namespace PixiEditorTests.ModelsTests.ControllersTests
         [Fact]
         [Fact]
         public void TestThatUndoAddsToRedoStack()
         public void TestThatUndoAddsToRedoStack()
         {
         {
-            PrepareUnoManagerForTests();
+            PrepareUnoManagerForTest();
             UndoManager.AddUndoChange(new Change("ExampleProperty", ExampleProperty, ExampleProperty));
             UndoManager.AddUndoChange(new Change("ExampleProperty", ExampleProperty, ExampleProperty));
             UndoManager.Undo();
             UndoManager.Undo();
             Assert.True(UndoManager.RedoStack.Count == 1);
             Assert.True(UndoManager.RedoStack.Count == 1);
@@ -37,7 +39,7 @@ namespace PixiEditorTests.ModelsTests.ControllersTests
         [Fact]
         [Fact]
         public void TestUndo()
         public void TestUndo()
         {
         {
-            PrepareUnoManagerForTests();
+            PrepareUnoManagerForTest();
             UndoManager.AddUndoChange(new Change("ExampleProperty", ExampleProperty, 55));
             UndoManager.AddUndoChange(new Change("ExampleProperty", ExampleProperty, 55));
             ExampleProperty = 55;
             ExampleProperty = 55;
             UndoManager.Undo();
             UndoManager.Undo();
@@ -48,7 +50,7 @@ namespace PixiEditorTests.ModelsTests.ControllersTests
         [Fact]
         [Fact]
         public void TestThatRedoAddsToUndoStack()
         public void TestThatRedoAddsToUndoStack()
         {
         {
-            PrepareUnoManagerForTests();
+            PrepareUnoManagerForTest();
             UndoManager.AddUndoChange(new Change("ExampleProperty", ExampleProperty, ExampleProperty));
             UndoManager.AddUndoChange(new Change("ExampleProperty", ExampleProperty, ExampleProperty));
             UndoManager.Undo();
             UndoManager.Undo();
             UndoManager.Redo();
             UndoManager.Redo();
@@ -58,7 +60,7 @@ namespace PixiEditorTests.ModelsTests.ControllersTests
         [Fact]
         [Fact]
         public void TestRedo()
         public void TestRedo()
         {
         {
-            PrepareUnoManagerForTests();
+            PrepareUnoManagerForTest();
             ExampleProperty = 55;
             ExampleProperty = 55;
             UndoManager.AddUndoChange(new Change("ExampleProperty", 1, ExampleProperty));
             UndoManager.AddUndoChange(new Change("ExampleProperty", 1, ExampleProperty));
             UndoManager.Undo();
             UndoManager.Undo();
@@ -66,12 +68,113 @@ namespace PixiEditorTests.ModelsTests.ControllersTests
             Assert.True((int)UndoManager.UndoStack.Peek().NewValue == ExampleProperty);
             Assert.True((int)UndoManager.UndoStack.Peek().NewValue == ExampleProperty);
         }
         }
 
 
-        private void PrepareUnoManagerForTests()
+        [Fact]
+        public void TestThatUndoManagerUndoAndRedoWithCustomRootCorrectly()
+        {
+            PrepareUnoManagerForTest();
+            TestPropertyClass testProp = new TestPropertyClass();
+            int newVal = 5;
+            testProp.IntProperty = newVal;
+            UndoManager.AddUndoChange(new Change("IntProperty", 0, newVal, root: testProp));
+            Assert.Equal(newVal, testProp.IntProperty);
+            
+            UndoManager.Undo();
+
+            Assert.Equal(0, testProp.IntProperty);
+
+            UndoManager.Redo();
+
+            Assert.Equal(newVal, testProp.IntProperty);
+        }
+
+        [Fact]
+        public void TestThatMixedProcessOfUndoAndRedoWorks()
+        {
+            PrepareUnoManagerForTest();
+
+
+            int newVal = 5;
+
+
+            UndoManager.AddUndoChange(
+                new Change("ExampleProperty",
+                    ReverseProcess,
+                    new object[]{ExampleProperty},
+                    newVal));
+
+            ExampleProperty = newVal;
+
+            Assert.Equal(newVal, ExampleProperty);
+
+            UndoManager.Undo();
+
+            Assert.Equal(1, ExampleProperty);
+
+            UndoManager.Redo();
+
+            Assert.Equal(newVal, ExampleProperty);
+        }
+
+        [Fact]
+        public void TestThatProcessBasedUndoAndRedoWorks()
+        {
+            PrepareUnoManagerForTest();
+            int newVal = 5;
+            UndoManager.AddUndoChange(new Change(ReverseProcess, new object[]{ExampleProperty}, ReverseProcess, 
+                new object[]{newVal}));
+
+            ExampleProperty = newVal;
+
+            Assert.Equal(newVal, ExampleProperty);
+
+            UndoManager.Undo();
+
+            Assert.Equal(1, ExampleProperty);
+
+            UndoManager.Redo();
+
+            Assert.Equal(newVal, ExampleProperty);
+        }
+
+        [Fact]
+        public void TestThatNestedPropertyUndoWorks()
+        {
+            PrepareUnoManagerForTest();
+            int newVal = 5;
+
+            UndoManager.AddUndoChange(new Change("TestPropClass.IntProperty", TestPropClass.IntProperty, 
+                newVal));
+
+            TestPropClass.IntProperty = newVal;
+
+            Assert.Equal(newVal, TestPropClass.IntProperty);
+
+            UndoManager.Undo();
+
+            Assert.Equal(0, TestPropClass.IntProperty);
+
+            UndoManager.Redo();
+
+            Assert.Equal(newVal, TestPropClass.IntProperty);
+        }
+
+        private void ReverseProcess(object[] args)
+        {
+            ExampleProperty = (int)args[0];
+        }
+
+        private void PrepareUnoManagerForTest()
         {
         {
             UndoManager.SetMainRoot(this);
             UndoManager.SetMainRoot(this);
             UndoManager.UndoStack.Clear();
             UndoManager.UndoStack.Clear();
             UndoManager.RedoStack.Clear();
             UndoManager.RedoStack.Clear();
             ExampleProperty = 1;
             ExampleProperty = 1;
+            TestPropClass = new TestPropertyClass();
         }
         }
     }
     }
+
+    public class TestPropertyClass
+    {
+        public int IntProperty { get; set; } = 0;
+    }
 }
 }

+ 64 - 0
PixiEditorTests/ModelsTests/DataHoldersTests/BitmapPixelChangesTests.cs

@@ -0,0 +1,64 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.Windows.Media;
+using PixiEditor.Exceptions;
+using PixiEditor.Models.DataHolders;
+using PixiEditor.Models.Position;
+using PixiEditor.Models.Tools;
+using Xunit;
+
+namespace PixiEditorTests.ModelsTests.DataHoldersTests
+{
+    public class BitmapPixelChangesTests
+    {
+
+        [Fact]
+        public void TestThatFromSingleColoredArrayCreatesCorrectArray()
+        {
+            var color = Colors.Chocolate;
+            Coordinates[] cords = {new Coordinates(0, 0), new Coordinates(1, 0), new Coordinates(3, 2)};
+            var bmpChanges = BitmapPixelChanges.FromSingleColoredArray(cords, color);
+
+            Assert.All(bmpChanges.ChangedPixels.Values, changeColor => Assert.Equal(color, changeColor));
+            Assert.True(bmpChanges.WasBuiltAsSingleColored);
+        }
+
+        [Fact]
+        public void TestThatCombineCombineOverrideCombinesValues()
+        {
+            Coordinates[] cords1 = {new Coordinates(0, 0), new Coordinates(1, 0), new Coordinates(3, 2)};
+            Coordinates[] cords2 = {new Coordinates(3, 2), new Coordinates(0, 0), new Coordinates(5, 5)};
+            BitmapPixelChanges changes = BitmapPixelChanges.FromSingleColoredArray(cords1, Colors.Green);
+            BitmapPixelChanges changes2 = BitmapPixelChanges.FromSingleColoredArray(cords2, Colors.Red);
+
+            var output = BitmapPixelChanges.CombineOverride(new[] {changes, changes2});
+            Assert.Equal(4,output.ChangedPixels.Count);
+            Assert.Equal(Colors.Red, output.ChangedPixels[new Coordinates(3,2)]);
+            Assert.Equal(Colors.Red, output.ChangedPixels[new Coordinates(0,0)]);
+            Assert.Equal(Colors.Green, output.ChangedPixels[new Coordinates(1,0)]);
+        }
+
+        [Fact]
+        public void TestThatFromArraysThrowsError()
+        {
+            Assert.Throws<ArrayLengthMismatchException>
+                (() => BitmapPixelChanges.FromArrays(new[] {new Coordinates(0, 0)}, new[] {Colors.Red, Colors.Green}));
+        }
+
+        [Fact]
+        public void TestThatFormArraysWorks()
+        {
+            Coordinates[] coordinatesArray = {new Coordinates(0, 0), new Coordinates(2, 3), new Coordinates(5, 5)};
+            Color[] colorsArray = {Colors.Red, Colors.Green, Colors.Blue};
+            var result = BitmapPixelChanges.FromArrays(coordinatesArray, colorsArray);
+            for (int i = 0; i < coordinatesArray.Length; i++)
+            {
+                var cords = coordinatesArray[i];
+                Assert.Equal(colorsArray[i], result.ChangedPixels[cords]);
+            }
+            Assert.False(result.WasBuiltAsSingleColored);
+        }
+
+    }
+}