Browse Source

Wrote another another bunch of tests

flabbet 5 years ago
parent
commit
8db47e8a9c

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

@@ -192,7 +192,7 @@ namespace PixiEditor.Models.Controllers
             else
             {
                 GeneratePreviewLayer();
-                PreviewLayer.ApplyPixels(
+                PreviewLayer.SetPixels(
                     BitmapPixelChanges.FromSingleColoredArray(highlightArea, Color.FromArgb(77, 0, 0, 0)));
             }
         }

+ 3 - 3
PixiEditor/Models/Controllers/BitmapOperationsUtility.cs

@@ -37,7 +37,7 @@ namespace PixiEditor.Models.Controllers
                 old[i] = new LayerChange(
                     BitmapPixelChanges.FromArrays(pixels, oldValues[layers[i]]), i);
                 newChange[i] = new LayerChange(changes, i);
-                layers[i].ApplyPixels(changes);
+                layers[i].SetPixels(changes);
             }
 
             UndoManager.AddUndoChange(new Change("UndoChanges", old, newChange, "Deleted pixels"));
@@ -105,7 +105,7 @@ namespace PixiEditor.Models.Controllers
                 GetOldPixelsValues(change.PixelChanges.ChangedPixels.Keys.ToArray()),
                 change.LayerIndex);
 
-            layer.ApplyPixels(change.PixelChanges, false);
+            layer.SetPixels(change.PixelChanges, false);
             return oldPixelsValues;
         }
 
@@ -154,7 +154,7 @@ namespace PixiEditor.Models.Controllers
                 modifiedLayers = ((BitmapOperationTool) Manager.SelectedTool).Use(Manager.ActiveDocument.ActiveLayer,
                     mouseMove.ToArray(), Manager.PrimaryColor);
                 BitmapPixelChanges[] changes = modifiedLayers.Select(x => x.PixelChanges).ToArray();
-                Manager.PreviewLayer.ApplyPixels(BitmapPixelChanges.CombineOverride(changes));
+                Manager.PreviewLayer.SetPixels(BitmapPixelChanges.CombineOverride(changes));
                 _lastModifiedLayers = modifiedLayers;
             }
         }

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

@@ -276,7 +276,7 @@ namespace PixiEditor.Models.DataHolders
 
             var contentCenter = CoordinatesCalculator.GetCenterPoint(points.Coords1, points.Coords2);
             var documentCenter = CoordinatesCalculator.GetCenterPoint(new Coordinates(0, 0),
-                new Coordinates(Width - 1, Height - 1));
+                new Coordinates(Width, Height));
             Coordinates moveVector = new Coordinates(documentCenter.X - contentCenter.X, documentCenter.Y - contentCenter.Y);
 
             MoveOffsets(moveVector);

+ 2 - 0
PixiEditor/Models/DataHolders/NotifyableColor.cs

@@ -67,6 +67,8 @@ namespace PixiEditor.Models.DataHolders
             B = color.B;
         }
 
+        public NotifyableColor(){}
+
         public event EventHandler ColorChanged;
 
         public void SetArgb(byte a, byte r, byte g, byte b)

+ 2 - 2
PixiEditor/Models/DataHolders/Selection.cs

@@ -52,12 +52,12 @@ namespace PixiEditor.Models.DataHolders
                     break;
             }
 
-            SelectionLayer.ApplyPixels(BitmapPixelChanges.FromSingleColoredArray(selection, selectionColor));
+            SelectionLayer.SetPixels(BitmapPixelChanges.FromSingleColoredArray(selection, selectionColor));
         }
 
         public void Clear()
         {
-            SelectionLayer.Clear();
+            SelectionLayer = new Layer("_selectionLayer");
             SelectedPoints.Clear();
         }
     }

+ 3 - 3
PixiEditor/Models/Layers/Layer.cs

@@ -205,9 +205,9 @@ namespace PixiEditor.Models.Layers
         /// <param name="color">Color of pixel</param>
         /// <param name="dynamicResize">Resizes bitmap to fit content</param>
         /// <param name="applyOffset">Converts pixels coordinates to relative to bitmap</param>
-        public void ApplyPixel(Coordinates coordinates, Color color, bool dynamicResize = true, bool applyOffset = true)
+        public void SetPixel(Coordinates coordinates, Color color, bool dynamicResize = true, bool applyOffset = true)
         {
-            ApplyPixels(BitmapPixelChanges.FromSingleColoredArray(new []{ coordinates }, color), dynamicResize, applyOffset);
+            SetPixels(BitmapPixelChanges.FromSingleColoredArray(new []{ coordinates }, color), dynamicResize, applyOffset);
         }
 
         /// <summary>
@@ -216,7 +216,7 @@ namespace PixiEditor.Models.Layers
         /// <param name="pixels">Pixels to apply</param>
         /// <param name="dynamicResize">Resizes bitmap to fit content</param>
         /// <param name="applyOffset">Converts pixels coordinates to relative to bitmap</param>
-        public void ApplyPixels(BitmapPixelChanges pixels, bool dynamicResize = true, bool applyOffset = true)
+        public void SetPixels(BitmapPixelChanges pixels, bool dynamicResize = true, bool applyOffset = true)
         {
             if (pixels.ChangedPixels == null || pixels.ChangedPixels.Count == 0) return;
             if(dynamicResize)

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

@@ -50,7 +50,7 @@ namespace PixiEditor.Models.Tools.Tools
                     if (clone.GetPixel(relativeCords.X, relativeCords.Y) == colorToReplace)
                     {
                         changedCoords.Add(new Coordinates(cords.X, cords.Y));
-                        clone.ApplyPixel(new Coordinates(cords.X, cords.Y), newColor);
+                        clone.SetPixel(new Coordinates(cords.X, cords.Y), newColor);
                         stack.Push(new Coordinates(cords.X, cords.Y - 1));
                         stack.Push(new Coordinates(cords.X + 1, cords.Y));
                         stack.Push(new Coordinates(cords.X, cords.Y + 1));

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

@@ -183,7 +183,7 @@ namespace PixiEditor.Models.Tools.Tools
             if (!_clearedPixels.ContainsKey(layer) || _clearedPixels[layer] == false)
             {
                 ViewModelMain.Current.BitmapManager.ActiveDocument.Layers.First(x => x == layer)
-                    .ApplyPixels(BitmapPixelChanges.FromSingleColoredArray(selection, System.Windows.Media.Colors.Transparent));
+                    .SetPixels(BitmapPixelChanges.FromSingleColoredArray(selection, System.Windows.Media.Colors.Transparent));
 
                 _clearedPixels[layer] = true;
             }

+ 2 - 2
PixiEditor/ViewModels/ViewModelMain.cs

@@ -248,7 +248,7 @@ namespace PixiEditor.ViewModels
             {
                 _undoChanges = value;
                 for (int i = 0; i < value.Length; i++)
-                    BitmapManager.ActiveDocument.Layers[value[i].LayerIndex].ApplyPixels(value[i].PixelChanges);
+                    BitmapManager.ActiveDocument.Layers[value[i].LayerIndex].SetPixels(value[i].PixelChanges);
             }
         }
 
@@ -426,7 +426,7 @@ namespace PixiEditor.ViewModels
         public void Cut(object parameter)
         {
             Copy(null);
-            BitmapManager.ActiveLayer.ApplyPixels(
+            BitmapManager.ActiveLayer.SetPixels(
                 BitmapPixelChanges.FromSingleColoredArray(ActiveSelection.SelectedPoints.ToArray(),
                     Colors.Transparent));
         }

+ 1 - 1
PixiEditorTests/ModelsTests/ControllersTests/BitmapOperationsUtilityTests.cs

@@ -20,7 +20,7 @@ namespace PixiEditorTests.ModelsTests.ControllersTests
             Layer testLayer = new Layer("test layer", 10, 10);
             Coordinates[] cords = {new Coordinates(0, 0), new Coordinates(1, 1)};
             BitmapPixelChanges pixels = BitmapPixelChanges.FromSingleColoredArray(cords, Colors.Black);
-            testLayer.ApplyPixels(pixels);
+            testLayer.SetPixels(pixels);
 
             util.DeletePixels(new []{testLayer}, cords);
 

+ 2 - 2
PixiEditorTests/ModelsTests/ControllersTests/ClipboardControllerTests.cs

@@ -48,8 +48,8 @@ namespace PixiEditorTests.ModelsTests.ControllersTests
 
             Layer testLayer = new Layer("test layer", 10, 10);
             Layer testLayer2 = new Layer("test layer", 10, 10);
-            testLayer.ApplyPixel(new Coordinates(4,4), testColor);
-            testLayer2.ApplyPixel(new Coordinates(5,5), testColor);
+            testLayer.SetPixel(new Coordinates(4,4), testColor);
+            testLayer2.SetPixel(new Coordinates(5,5), testColor);
 
             ClipboardController.CopyToClipboard(new []{testLayer, testLayer2}, 
                 new []{new Coordinates(4,4), new Coordinates(5,5)}, 10, 10);

+ 1 - 1
PixiEditorTests/ModelsTests/ControllersTests/UndoManagerTests.cs

@@ -169,7 +169,7 @@ namespace PixiEditorTests.ModelsTests.ControllersTests
             UndoManager.UndoStack.Clear();
             UndoManager.RedoStack.Clear();
             ExampleProperty = 1;
-            TestPropClass = new TestPropertyClass();
+            TestPropClass = new TestPropertyClass {IntProperty = 0};
         }
     }
 

+ 150 - 0
PixiEditorTests/ModelsTests/DataHoldersTests/DocumentTests.cs

@@ -0,0 +1,150 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Windows.Media;
+using PixiEditor.Models.Controllers;
+using PixiEditor.Models.DataHolders;
+using PixiEditor.Models.Enums;
+using PixiEditor.Models.Position;
+using Xunit;
+
+namespace PixiEditorTests.ModelsTests.DataHoldersTests
+{
+    public class DocumentTests
+    {
+
+        [Theory]
+        [InlineData(10,10,20,20)]
+        [InlineData(1,2,5,8)]
+        [InlineData(20,20,10,10)] //TODO Anchor
+        public void TestResizeCanvasResizesProperly(int oldWidth, int oldHeight, int newWidth, int newHeight)
+        {
+            Document document = new Document(oldWidth, oldHeight);
+
+            document.ResizeCanvas(newWidth, newHeight, AnchorPoint.Top | AnchorPoint.Left);
+            Assert.Equal(newHeight, document.Height);
+            Assert.Equal(newWidth, document.Width);
+        }
+
+        [Theory]
+        [InlineData(10,10,20,20)]
+        [InlineData(5,8,10,16)]
+        public void TestResizeWorks(int oldWidth, int oldHeight, int newWidth, int newHeight)
+        {
+            Document document = new Document(oldWidth, oldHeight);
+
+            document.Resize(newWidth, newHeight);
+
+            Assert.Equal(newHeight, document.Height);
+            Assert.Equal(newWidth, document.Width);
+        }
+
+        [Theory]
+        [InlineData(10,10, 0, 0)]
+        [InlineData(50,50, 10, 49)]
+        public void TestThatClipCanvasWorksForSingleLayer(int initialWidth, int initialHeight,int additionalPixelX, int additionalPixelY)
+        {
+            Document document = new Document(initialWidth, initialHeight);
+            BitmapManager manager = new BitmapManager
+            {
+                ActiveDocument = document
+            };
+            manager.AddNewLayer("test");
+            manager.ActiveLayer.SetPixel(new Coordinates((int)Math.Ceiling(initialWidth / 2f), 
+                (int)Math.Ceiling(initialHeight / 2f)), Colors.Black);
+
+            manager.ActiveLayer.SetPixel(new Coordinates(additionalPixelX, additionalPixelY), Colors.Black);
+
+            document.ClipCanvas();
+            
+            Assert.Equal(manager.ActiveLayer.Width, document.Width);
+            Assert.Equal(manager.ActiveLayer.Height, document.Height);
+        }
+
+        [Theory]
+        [InlineData(10, 10, 0, 0)]
+        [InlineData(50, 50, 15, 23)]
+        [InlineData(3, 3, 1, 1)]
+        [InlineData(1, 1, 0, 0)]
+        public void TestThatClipCanvasWorksForMultipleLayers(int initialWidth, int initialHeight, int secondLayerPixelX, int secondLayerPixelY)
+        {
+            Document document = new Document(initialWidth, initialHeight);
+            BitmapManager manager = new BitmapManager
+            {
+                ActiveDocument = document
+            };
+            manager.AddNewLayer("test");
+            manager.ActiveLayer.SetPixel(new Coordinates((int)Math.Ceiling(initialWidth / 2f),
+                (int)Math.Ceiling(initialHeight / 2f)), Colors.Black); //Set pixel in center
+
+            manager.AddNewLayer("test2");
+
+            manager.ActiveLayer.SetPixel(new Coordinates(secondLayerPixelX, secondLayerPixelY), Colors.Black);
+
+            document.ClipCanvas();
+
+            int totalWidth = Math.Abs(manager.ActiveDocument.Layers[1].OffsetX +
+                             manager.ActiveDocument.Layers[1].Width - (manager.ActiveDocument.Layers[0].OffsetX +
+                             manager.ActiveDocument.Layers[0].Width)) + 1;
+
+            int totalHeight = Math.Abs(manager.ActiveDocument.Layers[1].OffsetY +
+                manager.ActiveDocument.Layers[1].Height - (manager.ActiveDocument.Layers[0].OffsetY +
+                                                          manager.ActiveDocument.Layers[0].Height)) + 1;
+
+            Assert.Equal(totalWidth, document.Width);
+            Assert.Equal(totalHeight, document.Height);
+        }
+
+        [Theory]
+        [InlineData(10,10)]
+        [InlineData(11,11)]
+        [InlineData(25,17)]
+        public void TestThatCenterContentCentersContentForSingleLayer(int docWidth, int docHeight)
+        {
+            Document doc = new Document(docWidth, docHeight);
+            BitmapManager manager = new BitmapManager
+            {
+                ActiveDocument = doc
+            };
+            manager.AddNewLayer("test");
+
+            manager.ActiveLayer.SetPixel(new Coordinates(0,0), Colors.Green);
+
+            doc.CenterContent();
+
+            Assert.Equal(Math.Floor(docWidth / 2f), manager.ActiveLayer.OffsetX);
+            Assert.Equal(Math.Floor(docHeight / 2f), manager.ActiveLayer.OffsetY);
+        }
+
+        [Theory]
+        [InlineData(10, 10)]
+        [InlineData(11, 11)]
+        [InlineData(25, 17)]
+        public void TestThatCenterContentCentersContentForMultipleLayers(int docWidth, int docHeight)
+        {
+            Document doc = new Document(docWidth, docHeight);
+            BitmapManager manager = new BitmapManager
+            {
+                ActiveDocument = doc
+            };
+            manager.AddNewLayer("test");
+            manager.ActiveLayer.SetPixel(new Coordinates(0, 0), Colors.Green);
+
+            manager.AddNewLayer("test2");
+            manager.ActiveLayer.SetPixel(new Coordinates(1, 1), Colors.Green);
+
+            doc.CenterContent();
+
+            int midWidth = (int)Math.Floor(docWidth / 2f);
+            int midHeight = (int)Math.Floor(docHeight / 2f);
+
+            Assert.Equal( midWidth - 1, manager.ActiveDocument.Layers[0].OffsetX);
+            Assert.Equal( midHeight - 1, manager.ActiveDocument.Layers[0].OffsetY);
+
+            Assert.Equal(midWidth, manager.ActiveDocument.Layers[1].OffsetX);
+            Assert.Equal(midHeight, manager.ActiveDocument.Layers[1].OffsetY);
+        }
+
+    }
+}

+ 57 - 0
PixiEditorTests/ModelsTests/DataHoldersTests/NotifyableColorTests.cs

@@ -0,0 +1,57 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.Windows.Media;
+using PixiEditor.Models.DataHolders;
+using Xunit;
+
+namespace PixiEditorTests.ModelsTests.DataHoldersTests
+{
+    public class NotifyableColorTests
+    {
+        [Fact]
+        public void TestThatSetArgbWorks()
+        {
+            NotifyableColor color = new NotifyableColor();
+            color.SetArgb(2,2,2,2);
+            Assert.Equal(2, color.A);
+            Assert.Equal(2, color.R);
+            Assert.Equal(2, color.G);
+            Assert.Equal(2, color.B);
+        }
+
+
+        [Theory]
+        [InlineData("A", 2)]
+        [InlineData("R", 2)]
+        [InlineData("G", 2)]
+        [InlineData("B", 2)]
+        public void TestThatPropertyChangeCalled(string prop, byte value)
+        {
+            NotifyableColor color = new NotifyableColor(Colors.Black);
+            var property = color.GetType().GetProperty(prop);
+
+            Assert.NotNull(property);
+            Assert.PropertyChanged(color, prop, () => property.SetValue(color, value));
+        }
+
+        [Theory]
+        [InlineData("A", 2)]
+        [InlineData("R", 2)]
+        [InlineData("G", 2)]
+        [InlineData("B", 2)]
+        public void TestThatEventCalled(string prop, byte value)
+        {
+            bool eventCalled = false;
+            NotifyableColor color = new NotifyableColor(Colors.Black);
+            var property = color.GetType().GetProperty(prop);
+
+            color.ColorChanged += (s,e) => eventCalled = true;
+            Assert.NotNull(property);
+
+            property.SetValue(color, value);
+
+            Assert.True(eventCalled);
+        }
+    }
+}

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

@@ -0,0 +1,64 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.Windows.Media.Imaging;
+using PixiEditor.Models.DataHolders;
+using PixiEditor.Models.Enums;
+using PixiEditor.Models.Position;
+using Xunit;
+
+namespace PixiEditorTests.ModelsTests.DataHoldersTests
+{
+    public class SelectionTests
+    {
+
+        [Fact]
+        public void TestThatSetSelectionNewSetsCorrectSelection()
+        {
+            Selection selection = new Selection(Array.Empty<Coordinates>());
+            Coordinates[] points = {new Coordinates(0, 0), new Coordinates(1, 1)};
+
+            selection.SetSelection(points, SelectionType.New);
+            selection.SetSelection(points, SelectionType.New); //Doing it twice, to check if it sets every time properly
+
+            Assert.Equal(points.Length, selection.SelectedPoints.Count);
+        }
+
+        [Fact]
+        public void TestThatSetSelectionAddSetsCorrectSelection()
+        {
+            Selection selection = new Selection(Array.Empty<Coordinates>());
+            Coordinates[] points = { new Coordinates(0, 0), new Coordinates(1, 1) };
+            Coordinates[] points2 = { new Coordinates(2, 4), new Coordinates(5, 7) };
+
+            selection.SetSelection(points, SelectionType.Add);
+            selection.SetSelection(points2, SelectionType.Add); //Doing it twice, to check if it sets every time properly
+
+            Assert.Equal(points.Length + points2.Length, selection.SelectedPoints.Count);
+        }
+
+        [Fact]
+        public void TestThatSetSelectionSubtractSetsCorrectSelection()
+        {
+            Selection selection = new Selection(Array.Empty<Coordinates>());
+            Coordinates[] points = { new Coordinates(0, 0), new Coordinates(1, 1) };
+            Coordinates[] points2 = { new Coordinates(1, 1)};
+
+            selection.SetSelection(points, SelectionType.Add);
+            selection.SetSelection(points2, SelectionType.Subtract); //Doing it twice, to check if it sets every time properly
+
+            Assert.Single(selection.SelectedPoints);
+        }
+
+        [Fact]
+        public void TestClearWorks()
+        {
+            Selection selection = new Selection(new []{new Coordinates(0,0), new Coordinates(5,7)});
+            selection.Clear();
+
+            Assert.Empty(selection.SelectedPoints);
+            Assert.Equal(0,selection.SelectionLayer.Width + selection.SelectionLayer.Height);
+        }
+
+    }
+}