Browse Source

Covered 3 more files with unit tests

flabbet 5 years ago
parent
commit
070f52b362

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

@@ -8,7 +8,7 @@ using PixiEditor.Helpers;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.Enums;
 using PixiEditor.Models.Enums;
 using PixiEditor.Models.Events;
 using PixiEditor.Models.Events;
-using PixiEditor.Models.Images;
+using PixiEditor.Models.ImageManipulation;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Position;
 using PixiEditor.Models.Position;
 using PixiEditor.Models.Tools;
 using PixiEditor.Models.Tools;

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

@@ -5,7 +5,7 @@ using System.Windows.Input;
 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.DataHolders;
-using PixiEditor.Models.Images;
+using PixiEditor.Models.ImageManipulation;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Position;
 using PixiEditor.Models.Position;
 using PixiEditor.Models.Tools;
 using PixiEditor.Models.Tools;

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

@@ -3,7 +3,7 @@ using System.Linq;
 using System.Windows;
 using System.Windows;
 using System.Windows.Media.Imaging;
 using System.Windows.Media.Imaging;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.DataHolders;
-using PixiEditor.Models.Images;
+using PixiEditor.Models.ImageManipulation;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Position;
 using PixiEditor.Models.Position;
 using PixiEditor.ViewModels;
 using PixiEditor.ViewModels;

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

@@ -8,8 +8,8 @@ namespace PixiEditor.Models.Controllers
     public static class UndoManager
     public static class UndoManager
     {
     {
         private static bool _lastChangeWasUndo;
         private static bool _lastChangeWasUndo;
-        public static StackEx<Change> UndoStack { get; set; } = new StackEx<Change>();
-        public static StackEx<Change> RedoStack { get; set; } = new StackEx<Change>();
+        public static Stack<Change> UndoStack { get; set; } = new Stack<Change>();
+        public static Stack<Change> RedoStack { get; set; } = new Stack<Change>();
 
 
         public static bool CanUndo => UndoStack.Count > 0;
         public static bool CanUndo => UndoStack.Count > 0;
 
 

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

@@ -3,7 +3,7 @@ using System.Collections.ObjectModel;
 using System.Linq;
 using System.Linq;
 using System.Windows;
 using System.Windows;
 using System.Windows.Media;
 using System.Windows.Media;
-using PixiEditor.Models.Images;
+using PixiEditor.Models.ImageManipulation;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Layers;
 
 
 namespace PixiEditor.Models.DataHolders
 namespace PixiEditor.Models.DataHolders
@@ -35,7 +35,7 @@ namespace PixiEditor.Models.DataHolders
             return document;
             return document;
         }
         }
 
 
-        private ObservableCollection<Layer> ToLayers()
+        public ObservableCollection<Layer> ToLayers()
         {
         {
             ObservableCollection<Layer> layers = new ObservableCollection<Layer>();
             ObservableCollection<Layer> layers = new ObservableCollection<Layer>();
             for (int i = 0; i < Layers.Length; i++)
             for (int i = 0; i < Layers.Length; i++)

+ 0 - 53
PixiEditor/Models/DataHolders/StackEx.cs

@@ -1,53 +0,0 @@
-using System.Collections.Generic;
-
-namespace PixiEditor.Models.DataHolders
-{
-    public class StackEx<T>
-    {
-        public int Count => items.Count;
-
-        public T First => items[0];
-        private readonly List<T> items = new List<T>();
-
-        public void Clear()
-        {
-            items.Clear();
-        }
-
-        /// <summary>
-        ///     Returns top object without deleting it.
-        /// </summary>
-        /// <returns>Returns n - 1 item from stack.</returns>
-        public T Peek()
-        {
-            return items[items.Count - 1];
-        }
-
-        public void Push(T item)
-        {
-            items.Add(item);
-        }
-
-        public T Pop()
-        {
-            if (items.Count > 0)
-            {
-                T temp = items[items.Count - 1];
-                items.RemoveAt(items.Count - 1);
-                return temp;
-            }
-
-            return default;
-        }
-
-        public void PushToBottom(T item)
-        {
-            items.Insert(0, item);
-        }
-
-        public void Remove(int itemAtPosition)
-        {
-            items.RemoveAt(itemAtPosition);
-        }
-    }
-}

+ 15 - 12
PixiEditor/Models/Images/BitmapUtils.cs → PixiEditor/Models/ImageManipulation/BitmapUtils.cs

@@ -1,19 +1,24 @@
 using System;
 using System;
 using System.Collections.Generic;
 using System.Collections.Generic;
 using System.Drawing;
 using System.Drawing;
-using System.Linq;
 using System.Windows;
 using System.Windows;
 using System.Windows.Interop;
 using System.Windows.Interop;
 using System.Windows.Media.Imaging;
 using System.Windows.Media.Imaging;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Position;
 using PixiEditor.Models.Position;
-using PixiEditor.ViewModels;
 using Color = System.Windows.Media.Color;
 using Color = System.Windows.Media.Color;
 
 
-namespace PixiEditor.Models.Images
+namespace PixiEditor.Models.ImageManipulation
 {
 {
     public static class BitmapUtils
     public static class BitmapUtils
     {
     {
+        /// <summary>
+        ///     Converts pixel bytes to WriteableBitmap
+        /// </summary>
+        /// <param name="currentBitmapWidth">Width of bitmap</param>
+        /// <param name="currentBitmapHeight">Height of bitmap</param>
+        /// <param name="byteArray">Bitmap byte array</param>
+        /// <returns>WriteableBitmap</returns>
         public static WriteableBitmap BytesToWriteableBitmap(int currentBitmapWidth, int currentBitmapHeight,
         public static WriteableBitmap BytesToWriteableBitmap(int currentBitmapWidth, int currentBitmapHeight,
             byte[] byteArray)
             byte[] byteArray)
         {
         {
@@ -22,15 +27,13 @@ namespace PixiEditor.Models.Images
             return bitmap;
             return bitmap;
         }
         }
 
 
-        public static BitmapSource BitmapToBitmapSource(Bitmap bitmap)
-        {
-            return Imaging.CreateBitmapSourceFromHBitmap(
-                bitmap.GetHbitmap(),
-                IntPtr.Zero,
-                Int32Rect.Empty,
-                BitmapSizeOptions.FromEmptyOptions());
-        }
-
+        /// <summary>
+        ///     Converts layers bitmaps into one bitmap.
+        /// </summary>
+        /// <param name="layers">Layers to combine</param>
+        /// <param name="width">Width of final bitmap</param>
+        /// <param name="height">Height of final bitmap</param>
+        /// <returns>WriteableBitmap of layered bitmaps</returns>
         public static WriteableBitmap CombineLayers(Layer[] layers, int width, int height)
         public static WriteableBitmap CombineLayers(Layer[] layers, int width, int height)
         {
         {
             WriteableBitmap finalBitmap = BitmapFactory.New(width, height);
             WriteableBitmap finalBitmap = BitmapFactory.New(width, height);

+ 0 - 11
PixiEditor/Models/ImageManipulation/Morphology.cs

@@ -58,7 +58,6 @@ namespace PixiEditor.Models.ImageManipulation
             int minX = points.Min(x => x.X);
             int minX = points.Min(x => x.X);
             int minY = points.Min(x => x.Y);
             int minY = points.Min(x => x.Y);
             byte[,] array = new byte[dimensions.Item1 + margin * 2, dimensions.Item2 + margin * 2];
             byte[,] array = new byte[dimensions.Item1 + margin * 2, dimensions.Item2 + margin * 2];
-            //Debug.Write("----------\n");
 
 
             for (int y = 0; y < dimensions.Item2 + margin; y++)
             for (int y = 0; y < dimensions.Item2 + margin; y++)
             for (int x = 0; x < dimensions.Item1 + margin; x++)
             for (int x = 0; x < dimensions.Item1 + margin; x++)
@@ -66,16 +65,6 @@ namespace PixiEditor.Models.ImageManipulation
                 Coordinates cords = new Coordinates(x + minX, y + minY);
                 Coordinates cords = new Coordinates(x + minX, y + minY);
                 array[x + margin, y + margin] = points.Contains(cords) ? (byte) 1 : (byte) 0;
                 array[x + margin, y + margin] = points.Contains(cords) ? (byte) 1 : (byte) 0;
             }
             }
-
-            //for (int y = 0; y < array.GetLength(1); y++)
-            //{
-            //    for (int x = 0; x < array.GetLength(0); x++)
-            //    {
-            //        Debug.Write($"{array[x, y]} ");
-            //    }
-            //    Debug.Write("\n");
-            //}
-
             return array;
             return array;
         }
         }
 
 

+ 4 - 0
PixiEditor/Models/Layers/SerializableLayer.cs

@@ -8,6 +8,8 @@ namespace PixiEditor.Models.Layers
         public string Name { get; set; }
         public string Name { get; set; }
         public int Width { get; set; }
         public int Width { get; set; }
         public int Height { get; set; }
         public int Height { get; set; }
+        public int MaxWidth { get; set; }
+        public int MaxHeight { get; set; }
         public byte[] BitmapBytes { get; set; }
         public byte[] BitmapBytes { get; set; }
         public bool IsVisible { get; set; }
         public bool IsVisible { get; set; }
         public int OffsetX { get; set; }
         public int OffsetX { get; set; }
@@ -24,6 +26,8 @@ namespace PixiEditor.Models.Layers
             OffsetX = (int)layer.Offset.Left;
             OffsetX = (int)layer.Offset.Left;
             OffsetY = (int)layer.Offset.Top;
             OffsetY = (int)layer.Offset.Top;
             Opacity = layer.Opacity;
             Opacity = layer.Opacity;
+            MaxWidth = layer.MaxWidth;
+            MaxHeight = layer.MaxHeight;
         }
         }
     }
     }
 }
 }

+ 6 - 6
PixiEditor/Models/Position/Coordinates.cs

@@ -14,7 +14,7 @@
 
 
         public override string ToString()
         public override string ToString()
         {
         {
-            return $"x: {X}, y: {Y}";
+            return $"{X}, {Y}";
         }
         }
 
 
         public static bool operator ==(Coordinates c1, Coordinates c2)
         public static bool operator ==(Coordinates c1, Coordinates c2)
@@ -37,12 +37,12 @@
         {
         {
             unchecked
             unchecked
             {
             {
-                const int HashingBase = (int) 2166136261;
-                const int HashingMultiplier = 16777619;
+                const int hashingBase = (int) 2166136261;
+                const int hashingMultiplier = 16777619;
 
 
-                int hash = HashingBase;
-                hash = (hash * HashingMultiplier) ^ (!ReferenceEquals(null, X) ? X.GetHashCode() : 0);
-                hash = (hash * HashingMultiplier) ^ (!ReferenceEquals(null, Y) ? Y.GetHashCode() : 0);
+                int hash = hashingBase;
+                hash = (hash * hashingMultiplier) ^ (!ReferenceEquals(null, X) ? X.GetHashCode() : 0);
+                hash = (hash * hashingMultiplier) ^ (!ReferenceEquals(null, Y) ? Y.GetHashCode() : 0);
                 return hash;
                 return hash;
             }
             }
         }
         }

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

@@ -8,7 +8,7 @@ using PixiEditor.Helpers.Extensions;
 using PixiEditor.Models.Controllers;
 using PixiEditor.Models.Controllers;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.Enums;
 using PixiEditor.Models.Enums;
-using PixiEditor.Models.Images;
+using PixiEditor.Models.ImageManipulation;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Position;
 using PixiEditor.Models.Position;
 using PixiEditor.ViewModels;
 using PixiEditor.ViewModels;

+ 85 - 0
PixiEditorTests/ModelsTests/DataHoldersTests/SerializableDocumentTests.cs

@@ -0,0 +1,85 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Windows.Media;
+using System.Windows.Media.Imaging;
+using PixiEditor.Models.DataHolders;
+using PixiEditor.Models.Layers;
+using Xunit;
+
+namespace PixiEditorTests.ModelsTests.DataHoldersTests
+{
+    public class SerializableDocumentTests
+    {
+
+        [Fact]
+        public void TestThatSerializableDocumentCreatesCorrectly()
+        {
+            Document document = GenerateSampleDocument();
+            SerializableDocument doc = new SerializableDocument(document);
+
+            var swatch = document.Swatches.First();
+            Tuple<byte, byte, byte, byte> color = Tuple.Create(swatch.A, swatch.R, swatch.G, swatch.B);
+
+            Assert.Equal(document.Width, doc.Width);
+            Assert.Equal(document.Height, doc.Height);
+            Assert.Equal(color, doc.Swatches.First());
+            for (int i = 0; i < doc.Layers.Length; i++)
+            {
+                Assert.Equal(document.Layers[i].ConvertBitmapToBytes(), doc.Layers[i].BitmapBytes);
+                Assert.Equal(document.Layers[i].OffsetX, doc.Layers[i].OffsetX);
+                Assert.Equal(document.Layers[i].OffsetY, doc.Layers[i].OffsetY);
+                Assert.Equal(document.Layers[i].Width, doc.Layers[i].Width);
+                Assert.Equal(document.Layers[i].Height, doc.Layers[i].Height);
+                Assert.Equal(document.Layers[i].MaxWidth, doc.Layers[i].MaxWidth);
+                Assert.Equal(document.Layers[i].MaxHeight, doc.Layers[i].MaxHeight);
+                Assert.Equal(document.Layers[i].IsVisible, doc.Layers[i].IsVisible);
+                Assert.Equal(document.Layers[i].Opacity, doc.Layers[i].Opacity);
+            }
+        }
+
+        [Fact]
+        public void TestThatToDocumentConvertsCorrectly()
+        {
+            Document document = GenerateSampleDocument();
+            SerializableDocument doc = new SerializableDocument(document);
+
+            Document convertedDocument = doc.ToDocument();
+
+            Assert.Equal(document.Height, convertedDocument.Height);
+            Assert.Equal(document.Width, convertedDocument.Width);
+            Assert.Equal(document.Swatches, convertedDocument.Swatches);
+            Assert.Equal(document.Layers.Select(x=> x.LayerBitmap.ToByteArray()),
+                convertedDocument.Layers.Select(x=> x.LayerBitmap.ToByteArray()));
+        }
+
+        [Fact]
+        public void TestThatToLayersConvertsCorrectly()
+        {
+            Document document = GenerateSampleDocument();
+            SerializableDocument doc = new SerializableDocument(document);
+
+            var layers = doc.ToLayers();
+            for (int i = 0; i < layers.Count; i++)
+            {
+                Assert.Equal(document.Layers[i].LayerBitmap.ToByteArray(), layers[i].ConvertBitmapToBytes());
+                Assert.Equal(document.Layers[i].Height, layers[i].Height);
+                Assert.Equal(document.Layers[i].Width, layers[i].Width);
+                Assert.Equal(document.Layers[i].MaxHeight, layers[i].MaxHeight);
+                Assert.Equal(document.Layers[i].MaxWidth, layers[i].MaxWidth);
+                Assert.Equal(document.Layers[i].Offset, layers[i].Offset);
+                Assert.Equal(document.Layers[i].Opacity, layers[i].Opacity);
+                Assert.Equal(document.Layers[i].IsVisible, layers[i].IsVisible);
+            }
+        }
+
+        private Document GenerateSampleDocument()
+        {
+            Document document = new Document(10, 10);
+            document.Layers.Add(new Layer("Test", 5, 8));
+            document.Swatches.Add(Colors.Green);
+            return document;
+        }
+    }
+}

+ 98 - 0
PixiEditorTests/ModelsTests/ImageManipulationTests/BitmapUtilsTests.cs

@@ -0,0 +1,98 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Windows.Media;
+using System.Windows.Media.Imaging;
+using PixiEditor.Models.DataHolders;
+using PixiEditor.Models.ImageManipulation;
+using PixiEditor.Models.Layers;
+using PixiEditor.Models.Position;
+using Xunit;
+
+namespace PixiEditorTests.ModelsTests.ImageManipulationTests
+{
+    public class BitmapUtilsTests
+    {
+        [Fact]
+        public void TestBytesToWriteableBitmap()
+        {
+            int width = 10;
+            int height = 10;
+            Coordinates[] coloredPoints = {new Coordinates(0, 0), new Coordinates(3, 6), new Coordinates(9, 9)};
+            WriteableBitmap bmp = BitmapFactory.New(width, height);
+            for (int i = 0; i < coloredPoints.Length; i++)
+            {
+                bmp.SetPixel(coloredPoints[i].X, coloredPoints[i].Y, Colors.Green);
+            }
+
+            var byteArray = bmp.ToByteArray();
+
+            var convertedBitmap = BitmapUtils.BytesToWriteableBitmap(width, height, byteArray);
+
+            for (int i = 0; i < coloredPoints.Length; i++)
+            {
+                Assert.Equal(Colors.Green,convertedBitmap.GetPixel(coloredPoints[i].X, coloredPoints[i].Y));
+            }
+        }
+
+        [Fact]
+        public void TestThatCombineLayersReturnsCorrectBitmap()
+        {
+            Coordinates[] cords = {new Coordinates(0, 0), new Coordinates(1, 1)};
+            Layer[] layers = {new Layer("test", 2,2), new Layer("test2", 2, 2) };
+
+            layers[0].SetPixels(BitmapPixelChanges.FromSingleColoredArray(new []{cords[0]}, Colors.Green));
+
+            layers[1].SetPixels(BitmapPixelChanges.FromSingleColoredArray(new[] { cords[1] }, Colors.Red));
+
+            var outputBitmap = BitmapUtils.CombineLayers(layers, 2, 2);
+
+            Assert.Equal(Colors.Green, outputBitmap.GetPixel(0,0));
+            Assert.Equal(Colors.Red, outputBitmap.GetPixel(1,1));
+        }
+
+        [Fact]
+        public void TestThatCombineLayersReturnsCorrectBitmapWithSamePixels()
+        {
+            Coordinates[] cords = { new Coordinates(0, 0) };
+            Layer[] layers = { new Layer("test", 2, 2), new Layer("test2", 2, 2) };
+
+            layers[0].SetPixels(BitmapPixelChanges.FromSingleColoredArray(cords, Colors.Green));
+
+            layers[1].SetPixels(BitmapPixelChanges.FromSingleColoredArray(cords, Colors.Red));
+
+            var outputBitmap = BitmapUtils.CombineLayers(layers, 2, 2);
+
+            Assert.Equal(Colors.Red, outputBitmap.GetPixel(0, 0));
+        }
+
+        [Fact]
+        public void TestThatGetPixelsForSelectionReturnsCorrectPixels()
+        {
+            Coordinates[] cords = { new Coordinates(0, 0),
+                new Coordinates(1,1), new Coordinates(0, 1), new Coordinates(1, 0) };
+            Layer[] layers = { new Layer("test", 2, 2), new Layer("test2", 2, 2) };
+
+            layers[0].SetPixels(BitmapPixelChanges.FromSingleColoredArray(new []{cords[0]}, Colors.Green));
+            layers[1].SetPixels(BitmapPixelChanges.FromSingleColoredArray(new [] { cords[1] }, Colors.Red));
+
+            var output = BitmapUtils.GetPixelsForSelection(layers, cords);
+
+            List<Color> colors = new List<Color>();
+
+            foreach (var layerColor in output.ToArray())
+            {
+                foreach (var color in layerColor.Value)
+                {
+                    colors.Add(color);
+                }
+            }
+            Assert.Single(colors.Where(x=> x == Colors.Green));
+            Assert.Single(colors.Where(x=> x == Colors.Red));
+            Assert.Equal(6, colors.Count(x => x.A == 0)); //6 because layer is 4 pixels,
+                                                                                    //2 * 4 = 8, 2 other color pixels, so 8 - 2 = 6
+        }
+
+    }
+}

+ 44 - 0
PixiEditorTests/ModelsTests/ImageManipulationTests/TransformTests.cs

@@ -0,0 +1,44 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using PixiEditor.Models.ImageManipulation;
+using PixiEditor.Models.Position;
+using Xunit;
+
+namespace PixiEditorTests.ModelsTests.ImageManipulationTests
+{
+    public class TransformTests
+    {
+
+        [Theory]
+        [InlineData(0,0,1,1,1,1)]
+        [InlineData(1,1,0,0,-1,-1)]
+        [InlineData(5,5,4,6,-1,1)]
+        [InlineData(-15,-15,-16,-16,-1,-1)]
+        [InlineData(150,150,1150,1150,1000,1000)]
+        public void TestGetTranslation(int x1, int y1, int x2, int y2, int expectedX, int expectedY)
+        {
+            var translation = Transform.GetTranslation(new Coordinates(x1, y1), new Coordinates(x2, y2));
+            Assert.Equal(new Coordinates(expectedX, expectedY), translation);
+        }
+
+        [Theory]
+        [InlineData(0,0)]
+        [InlineData(1,1)]
+        [InlineData(5,2)]
+        [InlineData(50,150)]
+        [InlineData(-5,-52)]
+        public void TestTranslate(int vectorX, int vectorY)
+        {
+            Coordinates[] points = {new Coordinates(0, 0), new Coordinates(5, 5), new Coordinates(15,2)};
+            var translatedCords = Transform.Translate(points, new Coordinates(vectorX, vectorY));
+
+            for (int i = 0; i < points.Length; i++)
+            {
+                Assert.Equal(points[i].X + vectorX, translatedCords[i].X);
+                Assert.Equal(points[i].Y + vectorY, translatedCords[i].Y);
+            }
+        }
+
+    }
+}