Browse Source

Wrote some tests

flabbet 5 years ago
parent
commit
2ca03d56fb

+ 6 - 1
PixiEditor/Models/IO/Exporter.cs

@@ -13,6 +13,11 @@ namespace PixiEditor.Models.IO
         public static Size FileDimensions;
         public static string SaveDocumentPath { get; set; }
 
+        /// <summary>
+        ///     Saves document as .pixi file that contains all document data
+        /// </summary>
+        /// <param name="document">Document to save</param>
+        /// <param name="updateWorkspacePath">Should editor remember dialog path for further saves</param>
         public static void SaveAsNewEditableFile(Document document, bool updateWorkspacePath = false)
         {
             SaveFileDialog dialog = new SaveFileDialog
@@ -62,7 +67,7 @@ namespace PixiEditor.Models.IO
         /// <param name="exportWidth">File width</param>
         /// <param name="exportHeight">File height</param>
         /// <param name="bitmap">Bitmap to save</param>
-        private static void SaveAsPng(string savePath, int exportWidth, int exportHeight, WriteableBitmap bitmap)
+        public static void SaveAsPng(string savePath, int exportWidth, int exportHeight, WriteableBitmap bitmap)
         {
             try
             {

+ 0 - 89
PixiEditor/Models/IO/FilesManager.cs

@@ -1,89 +0,0 @@
-using System;
-using System.Diagnostics;
-using System.IO;
-using Newtonsoft.Json;
-
-namespace PixiEditor.Models.IO
-{
-    public static class FilesManager
-    {
-        public static string TempFolderPath =>
-            Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"PixiEditor\Temp");
-
-        public static string RedoStackPath => Path.Combine(TempFolderPath, @"RedoStack");
-
-        public static string UndoStackPath => Path.Combine(TempFolderPath, @"UndoStack");
-
-        /// <summary>
-        ///     Saves object to file on disk using binary formatter
-        /// </summary>
-        /// <param name="obj">Object to be saved</param>
-        public static void SaveObjectToJsonFile<T>(T obj, string fileName) where T : new()
-        {
-            try
-            {
-                SaveSerializedObjectToFile(obj, fileName);
-            }
-            catch (IOException ex)
-            {
-                Debug.WriteLine(ex.Message);
-            }
-        }
-
-
-        public static void RemoveFile(string path)
-        {
-            File.Delete(path);
-        }
-
-        /// <summary>
-        ///     Removes all files from directory
-        /// </summary>
-        /// <param name="path"></param>
-        public static void ClearDirectory(string path)
-        {
-            string[] filesInDirectory = Directory.GetFiles(path);
-            for (int i = 0; i < filesInDirectory.Length; i++) File.Delete(filesInDirectory[i]);
-        }
-
-        private static void SaveSerializedObjectToFile(object obj, string filename)
-        {
-            using (TextWriter writer = new StreamWriter(filename, false))
-            {
-                var contentsToWriteToFile = JsonConvert.SerializeObject(obj);
-                writer.Write(contentsToWriteToFile);
-            }
-        }
-
-        public static T ReadObjectFromFile<T>(string filePath) where T : new()
-        {
-            using (TextReader reader = new StreamReader(filePath))
-            {
-                var fileContent = reader.ReadToEnd();
-                return JsonConvert.DeserializeObject<T>(fileContent);
-            }
-        }
-
-        /// <summary>
-        ///     Creates and cleares temp directories
-        /// </summary>
-        public static void InitializeTempDirectories()
-        {
-            CreateTempDirectories();
-            ClearTempDirectoriesContent();
-        }
-
-        private static void CreateTempDirectories()
-        {
-            Directory.CreateDirectory(TempFolderPath);
-            Directory.CreateDirectory(Path.Combine(TempFolderPath, "UndoStack"));
-            Directory.CreateDirectory(Path.Combine(TempFolderPath, "RedoStack"));
-        }
-
-        public static void ClearTempDirectoriesContent()
-        {
-            ClearDirectory(RedoStackPath);
-            ClearDirectory(UndoStackPath);
-        }
-    }
-}

+ 12 - 3
PixiEditor/Models/IO/Importer.cs

@@ -15,6 +15,16 @@ namespace PixiEditor.Models.IO
         /// <param name="height">New height of image.</param>
         /// <returns></returns>
         public static WriteableBitmap ImportImage(string path, int width, int height)
+        {
+            var wbmp = ImportImage(path).Resize(width, height, WriteableBitmapExtensions.Interpolation.NearestNeighbor);
+            return wbmp;
+        }
+
+        /// <summary>
+        ///     Imports image from path and resizes it to given dimensions
+        /// </summary>
+        /// <param name="path">Path of image.</param>
+        public static WriteableBitmap ImportImage(string path)
         {
             Uri uri = new Uri(path);
             BitmapImage bitmap = new BitmapImage();
@@ -22,9 +32,7 @@ namespace PixiEditor.Models.IO
             bitmap.UriSource = uri;
             bitmap.EndInit();
 
-            var wbmp = new WriteableBitmap(bitmap);
-            wbmp = wbmp.Resize(width, height, WriteableBitmapExtensions.Interpolation.NearestNeighbor);
-            return wbmp;
+            return new WriteableBitmap(bitmap);
         }
 
         public static Document ImportDocument(string path)
@@ -34,6 +42,7 @@ namespace PixiEditor.Models.IO
 
         public static bool IsSupportedFile(string path)
         {
+            path = path.ToLower();
             return path.EndsWith(".pixi") || path.EndsWith(".png") || path.EndsWith(".jpg") || path.EndsWith(".jpeg");
         }
     }

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

@@ -1,4 +1,5 @@
 using System;
+using System.Linq;
 
 namespace PixiEditor.Models.Layers
 {
@@ -29,5 +30,35 @@ namespace PixiEditor.Models.Layers
             MaxWidth = layer.MaxWidth;
             MaxHeight = layer.MaxHeight;
         }
+
+        public override bool Equals(object? obj)
+        {
+            if (obj == null || obj.GetType() != typeof(SerializableLayer)) return false;
+
+            SerializableLayer layer = (SerializableLayer) obj;
+
+            return Equals(layer);
+        }
+
+        protected bool Equals(SerializableLayer other)
+        {
+            return Name == other.Name && Width == other.Width && Height == other.Height && MaxWidth == other.MaxWidth && MaxHeight == other.MaxHeight && BitmapBytes.SequenceEqual(other.BitmapBytes) && IsVisible == other.IsVisible && OffsetX == other.OffsetX && OffsetY == other.OffsetY && Opacity.Equals(other.Opacity);
+        }
+
+        public override int GetHashCode()
+        {
+            var hashCode = new HashCode();
+            hashCode.Add(Name);
+            hashCode.Add(Width);
+            hashCode.Add(Height);
+            hashCode.Add(MaxWidth);
+            hashCode.Add(MaxHeight);
+            hashCode.Add(BitmapBytes);
+            hashCode.Add(IsVisible);
+            hashCode.Add(OffsetX);
+            hashCode.Add(OffsetY);
+            hashCode.Add(Opacity);
+            return hashCode.ToHashCode();
+        }
     }
 }

+ 1 - 4
PixiEditor/PixiEditor.csproj

@@ -47,11 +47,8 @@
     <PackageReference Include="Expression.Blend.Sdk">
       <Version>1.0.2</Version>
     </PackageReference>
-    <PackageReference Include="Extended.Wpf.Toolkit">
-      <Version>3.8.1</Version>
-    </PackageReference>
+    <PackageReference Include="Extended.Wpf.Toolkit" Version="3.8.2" />
     <PackageReference Include="MvvmLightLibs" Version="5.4.1.1" />
-    <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
     <PackageReference Include="System.Drawing.Common" Version="4.7.0" />
     <PackageReference Include="WriteableBitmapEx">
       <Version>1.6.5</Version>

+ 0 - 1
PixiEditor/ViewModels/ViewModelMain.cs

@@ -49,7 +49,6 @@ namespace PixiEditor.ViewModels
 
         public ViewModelMain()
         {
-            FilesManager.InitializeTempDirectories();
             BitmapManager = new BitmapManager();
             BitmapManager.BitmapOperations.BitmapChanged += BitmapUtility_BitmapChanged;
             BitmapManager.MouseController.StoppedRecordingChanges += MouseController_StoppedRecordingChanges;

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

@@ -50,7 +50,7 @@ namespace PixiEditorTests.ModelsTests.ControllersTests
             Assert.Equal(4, changes[0].Item2.PixelChanges.ChangedPixels.Count);
         }
 
-        private PixelChangesController CreateBasicController()
+        private static PixelChangesController CreateBasicController()
         {
             Coordinates[] cords = { new Coordinates(0, 0), new Coordinates(1, 1) };
             PixelChangesController controller = new PixelChangesController();

+ 1 - 1
PixiEditorTests/ModelsTests/DataHoldersTests/SerializableDocumentTests.cs

@@ -74,7 +74,7 @@ namespace PixiEditorTests.ModelsTests.DataHoldersTests
             }
         }
 
-        private Document GenerateSampleDocument()
+        private static Document GenerateSampleDocument()
         {
             Document document = new Document(10, 10);
             document.Layers.Add(new Layer("Test", 5, 8));

+ 48 - 0
PixiEditorTests/ModelsTests/IO/BinarySerializationTests.cs

@@ -0,0 +1,48 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Text;
+using System.Windows.Media;
+using PixiEditor.Models.DataHolders;
+using PixiEditor.Models.IO;
+using PixiEditor.Models.Layers;
+using Xunit;
+
+namespace PixiEditorTests.ModelsTests.IO
+{
+    public class BinarySerializationTests
+    {
+
+        private const string Path = "bstests.file";
+
+        [Fact]
+        public void TestThatWriteToBinaryFileCreatesFile()
+        {
+            SerializableDocument doc = new SerializableDocument(new Document(10,10));
+            BinarySerialization.WriteToBinaryFile(Path, doc);
+
+            Assert.True(File.Exists(Path));
+
+            File.Delete(Path);
+        }
+
+        [Fact]
+        public void TestThatReadFromBinaryFileReadsCorrectly()
+        {
+            Document document = new Document(10, 10);
+            document.Layers.Add(new Layer("yeet"));
+            document.Swatches.Add(Colors.Green);
+
+            SerializableDocument doc = new SerializableDocument(document);
+            BinarySerialization.WriteToBinaryFile(Path, doc);
+
+            var file = BinarySerialization.ReadFromBinaryFile<SerializableDocument>(Path);
+            
+            Assert.Equal(doc.Layers, file.Layers);
+            Assert.Equal(doc.Height, file.Height);
+            Assert.Equal(doc.Width, file.Width);
+            Assert.Equal(doc.Swatches, file.Swatches);
+        }
+
+    }
+}

+ 24 - 0
PixiEditorTests/ModelsTests/IO/ExporterTests.cs

@@ -0,0 +1,24 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Text;
+using System.Windows.Media.Imaging;
+using PixiEditor.Models.IO;
+using Xunit;
+
+namespace PixiEditorTests.ModelsTests.IO
+{
+    public class ExporterTests
+    {
+        private const string FilePath = "test.file";
+
+        [Fact]
+        public void TestThatSaveAsPngSavesFile()
+        {
+            Exporter.SaveAsPng(FilePath, 10, 10, BitmapFactory.New(10,10));
+            Assert.True(File.Exists(FilePath));
+
+            File.Delete(FilePath);
+        }
+    }
+}

+ 60 - 0
PixiEditorTests/ModelsTests/IO/ImporterTests.cs

@@ -0,0 +1,60 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Text;
+using System.Windows.Media;
+using System.Windows.Media.Imaging;
+using PixiEditor.Models.IO;
+using Xunit;
+
+namespace PixiEditorTests.ModelsTests.IO
+{
+    public class ImporterTests
+    {
+        private string _testImagePath;
+
+        //I am not testing ImportDocument, because it's just a wrapper for BinarySerialization which is tested.
+
+        public ImporterTests()
+        {
+            _testImagePath = $"{Environment.CurrentDirectory}\\..\\..\\..\\ModelsTests\\IO\\TestImage.png";
+        }
+
+        [Theory]
+        [InlineData("wubba.png")]
+        [InlineData("lubba.pixi")]
+        [InlineData("dub.jpeg")]
+        [InlineData("-.JPEG")]
+        [InlineData("dub.jpg")]
+        public void TestThatIsSupportedFile(string file)
+        {
+            Assert.True(Importer.IsSupportedFile(file));
+        }
+
+        [Fact]
+        public void TestThatImportImageImportsImage()
+        {
+            var color = Color.FromArgb(255, 255, 0, 0);
+            var image = Importer.ImportImage(_testImagePath);
+
+            Assert.NotNull(image);
+            Assert.Equal(5, image.PixelWidth);
+            Assert.Equal(5, image.PixelHeight);
+            Assert.Equal(color, image.GetPixel(0,0)); //Top left
+            Assert.Equal(color, image.GetPixel(4,4)); //Bottom right
+            Assert.Equal(color, image.GetPixel(0,4)); //Bottom left
+            Assert.Equal(color, image.GetPixel(4,0)); //Top right
+            Assert.Equal(color, image.GetPixel(2,2)); //Middle center
+        }
+
+        [Fact]
+        public void TestThatImportImageResizes()
+        {
+            var image = Importer.ImportImage(_testImagePath, 10, 10);
+
+            Assert.Equal(10, image.PixelWidth);
+            Assert.Equal(10, image.PixelHeight);
+        }
+
+    }
+}

BIN
PixiEditorTests/ModelsTests/IO/TestImage.png


+ 1 - 1
PixiEditorTests/PixiEditorTests.csproj

@@ -13,7 +13,7 @@
 
   <ItemGroup>
     <PackageReference Include="Codecov" Version="1.12.0" />
-    <PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8">
+    <PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.0.0">
       <PrivateAssets>all</PrivateAssets>
       <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
     </PackageReference>