فهرست منبع

Covered Layer with tests

flabbet 5 سال پیش
والد
کامیت
26112a8ba9

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

@@ -42,10 +42,9 @@ namespace PixiEditor.Models.DataHolders
             {
                 SerializableLayer serLayer = Layers[i];
                 Layer layer =
-                    new Layer(BitmapUtils.BytesToWriteableBitmap(serLayer.Width, serLayer.Height, serLayer.BitmapBytes))
+                    new Layer(serLayer.Name,BitmapUtils.BytesToWriteableBitmap(serLayer.Width, serLayer.Height, serLayer.BitmapBytes))
                     {
                         IsVisible = serLayer.IsVisible,
-                        Name = serLayer.Name,
                         Offset = new Thickness(serLayer.OffsetX, serLayer.OffsetY, 0, 0),
                         Opacity = serLayer.Opacity
                     };

+ 43 - 12
PixiEditor/Models/Layers/Layer.cs

@@ -111,8 +111,7 @@ namespace PixiEditor.Models.Layers
         public Layer(string name)
         {
             Name = name;
-            Layer layer = LayerGenerator.Generate(0, 0);
-            LayerBitmap = layer.LayerBitmap;
+            LayerBitmap = BitmapFactory.New(0, 0);
             Width = 0;
             Height = 0;
         }
@@ -120,18 +119,18 @@ namespace PixiEditor.Models.Layers
         public Layer(string name, int width, int height)
         {
             Name = name;
-            Layer layer = LayerGenerator.Generate(width, height);
-            LayerBitmap = layer.LayerBitmap;
+            LayerBitmap = BitmapFactory.New(width, height);
             Width = width;
             Height = height;
         }
 
 
-        public Layer(WriteableBitmap layerBitmap)
+        public Layer(string name, WriteableBitmap layerBitmap)
         {
+            Name = name;
             LayerBitmap = layerBitmap;
-            Width = (int) layerBitmap.Width;
-            Height = (int) layerBitmap.Height;
+            Width = layerBitmap.PixelWidth;
+            Height = layerBitmap.PixelHeight;
         }
 
         /// <summary>
@@ -140,17 +139,25 @@ namespace PixiEditor.Models.Layers
         /// <returns></returns>
         public Layer Clone()
         {
-            return new Layer(LayerBitmap.Clone())
+            return new Layer(Name, LayerBitmap.Clone())
             {
-                _isVisible = this._isVisible,
-                Name = this.Name,
+                IsVisible = this.IsVisible,
                 Offset = this.Offset,
                 MaxHeight = this.MaxHeight,
                 MaxWidth = this.MaxWidth,
-                Opacity = this.Opacity
+                Opacity = this.Opacity,
+                IsActive = this.IsActive,
+                IsRenaming = this.IsRenaming
             };
         }
 
+        /// <summary>
+        ///     Resizes bitmap with it's content using NearestNeighbor interpolation
+        /// </summary>
+        /// <param name="width">New width</param>
+        /// <param name="height">New height</param>
+        /// <param name="newMaxWidth">New layer maximum width, this should be document width</param>
+        /// <param name="newMaxHeight">New layer maximum height, this should be document height</param>
         public void Resize(int width, int height, int newMaxWidth, int newMaxHeight)
         {
             LayerBitmap = LayerBitmap.Resize(width, height, WriteableBitmapExtensions.Interpolation.NearestNeighbor);
@@ -242,6 +249,11 @@ namespace PixiEditor.Models.Layers
                 d => d.Value);
         }
 
+        /// <summary>
+        ///     Converts absolute coordinates array to relative to this layer coordinates array.
+        /// </summary>
+        /// <param name="nonRelativeCords">absolute coordinates array</param>
+        /// <returns></returns>
         public Coordinates[] ConvertToRelativeCoordinates(Coordinates[] nonRelativeCords)
         {
             Coordinates[] result = new Coordinates[nonRelativeCords.Length];
@@ -331,6 +343,9 @@ namespace PixiEditor.Models.Layers
             }
         }
 
+        /// <summary>
+        ///     Changes size of bitmap to fit content
+        /// </summary>
         public void ClipCanvas()
         {
             var points = GetEdgePoints();
@@ -392,11 +407,18 @@ namespace PixiEditor.Models.Layers
             }
         }
 
+        /// <summary>
+        ///     Clears bitmap
+        /// </summary>
         public void Clear()
         {
             LayerBitmap.Clear();
         }
 
+        /// <summary>
+        ///     Converts layer WriteableBitmap to byte array
+        /// </summary>
+        /// <returns></returns>
         public byte[] ConvertBitmapToBytes()
         {
             LayerBitmap.Lock();
@@ -405,7 +427,16 @@ namespace PixiEditor.Models.Layers
             return byteArray;
         }
 
-        public void ResizeCanvas(int offsetX, int offsetY, int offsetXSrc, int offsetYSrc, int newWidth, int newHeight)
+        /// <summary>
+        ///     Resizes canvas to new size with specified offset.
+        /// </summary>
+        /// <param name="offsetX"></param>
+        /// <param name="offsetY"></param>
+        /// <param name="offsetXSrc"></param>
+        /// <param name="offsetYSrc"></param>
+        /// <param name="newWidth"></param>
+        /// <param name="newHeight"></param>
+        private void ResizeCanvas(int offsetX, int offsetY, int offsetXSrc, int offsetYSrc, int newWidth, int newHeight)
         {
             int iteratorHeight = Height > newHeight ? newHeight : Height;
             int count = Width > newWidth ? newWidth : Width;

+ 0 - 31
PixiEditor/Models/Layers/LayerGenerator.cs

@@ -1,31 +0,0 @@
-using System.Windows.Media.Imaging;
-
-namespace PixiEditor.Models.Layers
-{
-    public static class LayerGenerator
-    {
-        /// <summary>
-        ///     Generating useable layer with image and bitmap
-        /// </summary>
-        /// <param name="imageWidth">Width of layer.</param>
-        /// <param name="imageHeight">Height of layer.</param>
-        /// <returns></returns>
-        public static Layer Generate(int imageWidth, int imageHeight)
-        {
-            return new Layer(GenerateBitmap(imageWidth, imageHeight));
-        }
-
-        /// <summary>
-        ///     Generates bitmap ready to work with
-        /// </summary>
-        /// <param name="bitmapWidth">Width of bitmap.</param>
-        /// <param name="imageHeight">Height of bitmap.</param>
-        /// <returns></returns>
-        private static WriteableBitmap GenerateBitmap(int bitmapWidth, int imageHeight)
-        {
-            WriteableBitmap bitmap = BitmapFactory.New(bitmapWidth, imageHeight);
-            bitmap.Clear(System.Windows.Media.Colors.Transparent);
-            return bitmap;
-        }
-    }
-}

+ 144 - 0
PixiEditorTests/ModelsTests/LayersTests/LayerTests.cs

@@ -0,0 +1,144 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.Windows.Media;
+using System.Windows.Media.Imaging;
+using PixiEditor.Models.DataHolders;
+using PixiEditor.Models.Layers;
+using PixiEditor.Models.Position;
+using Xunit;
+
+namespace PixiEditorTests.ModelsTests.LayersTests
+{
+    public class LayerTests
+    {
+        [Fact]
+        public void TestThatEmptyLayerGeneratesCorrectly()
+        {
+            Layer layer = new Layer("layer");
+
+            Assert.Equal("layer",layer.Name);
+            Assert.Equal(0,layer.Width);
+            Assert.Equal(0,layer.Height);
+            Assert.Equal(1, layer.LayerBitmap.PixelWidth);
+            Assert.Equal(1, layer.LayerBitmap.PixelHeight);
+        }
+
+        [Fact]
+        public void TestThatEmptyLayerWithSizeGeneratesCorrectly()
+        {
+            Layer layer = new Layer("layer", 10, 10);
+
+            Assert.Equal("layer", layer.Name);
+            Assert.Equal(10, layer.Width);
+            Assert.Equal(10, layer.Height);
+            Assert.Equal(10, layer.LayerBitmap.PixelWidth);
+            Assert.Equal(10, layer.LayerBitmap.PixelHeight);
+        }
+
+        [Fact]
+        public void TestThatLayerFromBitmapGeneratesCorrectly()
+        {
+            WriteableBitmap bmp = BitmapFactory.New(10, 10);
+            
+            Layer layer = new Layer("layer",bmp);
+
+            Assert.Equal("layer", layer.Name);
+            Assert.Equal(10, layer.Width);
+            Assert.Equal(10, layer.Height);
+            Assert.Equal(10, layer.LayerBitmap.PixelWidth);
+            Assert.Equal(10, layer.LayerBitmap.PixelHeight);
+        }
+
+        [Fact]
+        public void TestThatCloneClonesCorrectly()
+        {
+            Layer layer = new Layer("test", 5, 2);
+
+            var clone = layer.Clone();
+
+            Assert.Equal(layer.Name, clone.Name);
+            Assert.Equal(layer.Offset, clone.Offset);
+            Assert.Equal(layer.Width, clone.Width);
+            Assert.Equal(layer.Height, clone.Height);
+            Assert.Equal(layer.MaxHeight, clone.MaxHeight);
+            Assert.Equal(layer.MaxWidth, clone.MaxWidth);
+            Assert.Equal(layer.Opacity, clone.Opacity);
+            Assert.Equal(layer.IsVisible, clone.IsVisible);
+            Assert.Equal(layer.IsRenaming, clone.IsRenaming);
+            Assert.Equal(layer.ConvertBitmapToBytes(), clone.ConvertBitmapToBytes());
+        }
+
+        [Fact]
+        public void TestThatCloneIsMakingDeepCopyOfBitmap()
+        {
+            Layer layer = new Layer("test", 5, 2);
+
+            var clone = layer.Clone();
+
+            clone.LayerBitmap.SetPixel(0,0, Colors.Green); //Actually we are checking if modifying clone bitmap does not affect original
+
+            Assert.NotEqual(Colors.Green,layer.GetPixel(0,0));
+        }
+
+        [Fact]
+        public void TestThatResizeResizesBitmap()
+        {
+            Layer layer = new Layer("layer", 1,1);
+
+            layer.SetPixel(new Coordinates(0,0), Colors.Black);
+
+            layer.Resize(2,2, 2,2);
+
+            Assert.Equal(2,layer.Width);
+            Assert.Equal(2,layer.Height);
+            Assert.Equal(2, layer.MaxWidth);
+            Assert.Equal(2, layer.MaxHeight);
+
+            for (int y = 0; y < layer.Height; y++) //4 is new area of bitmap
+            {
+                for (int x = 0; x < layer.Width; x++)
+                {
+                    Assert.Equal(Colors.Black,layer.GetPixel(x,y));
+                }
+            }
+        }
+
+        [Fact]
+        public void TestThatGetPixelReturnsTransparentIfOutOfBounds()
+        {
+            Layer layer = new Layer("layer");
+
+            Assert.Equal(0, layer.GetPixel(-1, 999).A);
+        }
+
+        [Fact]
+        public void TestThatSetPixelsSetsPixels() //This also tests if Dynamic Resize works
+        {
+            Coordinates[] pixels = {new Coordinates(4, 2), new Coordinates(0, 0), new Coordinates(15, 2),};
+
+            Layer layer = new Layer("layer");
+
+            layer.SetPixels(BitmapPixelChanges.FromSingleColoredArray(pixels, Colors.Green));
+
+            for (int i = 0; i < pixels.Length; i++)
+            {
+                Assert.Equal(Colors.Green, layer.GetPixelWithOffset(pixels[i].X, pixels[i].Y));
+            }
+        }
+
+        [Fact]
+        public void TestThatClipCanvasResizesBitmapCorrectly()
+        {
+            Layer layer = new Layer("layer", 10, 10);
+            layer.SetPixel(new Coordinates(4,4), Colors.Blue);
+
+            layer.ClipCanvas();
+
+            Assert.Equal(1, layer.Width);
+            Assert.Equal(1, layer.Height);
+            Assert.Equal(Colors.Blue, layer.GetPixel(0,0));
+        }
+
+    }
+}