Browse Source

Fix all tests apart from ClipboardController

Equbuxu 3 years ago
parent
commit
34c6f90765

+ 3 - 0
PixiEditor/Models/DataHolders/Document/Document.Constructors.cs

@@ -1,6 +1,7 @@
 using PixiEditor.Models.Controllers;
 using PixiEditor.Models.Layers;
 using PixiEditor.ViewModels;
+using System;
 using System.Linq;
 
 namespace PixiEditor.Models.DataHolders
@@ -10,6 +11,8 @@ namespace PixiEditor.Models.DataHolders
         public Document(int width, int height)
             : this()
         {
+            if (width <= 0 || height <= 0)
+                throw new ArgumentException("Document dimensions must be greater than 0");
             Width = width;
             Height = height;
             Renderer = new LayerStackRenderer(layers, layerStructure, Width, Height);

+ 4 - 5
PixiEditor/Models/DataHolders/Document/Document.cs

@@ -237,11 +237,10 @@ namespace PixiEditor.Models.DataHolders
                 throw new ArgumentException("Not enough layers");
             }
 
-            Layer firstLayer = layers.First();
-            int smallestX = firstLayer.OffsetX;
-            int smallestY = firstLayer.OffsetY;
-            int biggestX = smallestX + firstLayer.Width;
-            int biggestY = smallestY + firstLayer.Height;
+            int smallestX = int.MaxValue;
+            int smallestY = int.MaxValue;
+            int biggestX = int.MinValue;
+            int biggestY = int.MinValue;
 
             foreach (Layer layer in layers)
             {

+ 5 - 14
PixiEditor/Models/IO/Importer.cs

@@ -40,21 +40,12 @@ namespace PixiEditor.Models.IO
         /// <param name="path">Path of image.</param>
         public static Surface ImportSurface(string path)
         {
-            try
-            {
-                using var image = SKImage.FromEncodedData(path);
-                Surface surface = new Surface(image.Width, image.Height);
-                surface.SkiaSurface.Canvas.DrawImage(image, new SKPoint(0, 0));
-                return surface;
-            }
-            catch (NotSupportedException)
-            {
+            using var image = SKImage.FromEncodedData(path);
+            if (image == null)
                 throw new CorruptedFileException();
-            }
-            catch (FileFormatException)
-            {
-                throw new CorruptedFileException();
-            }
+            Surface surface = new Surface(image.Width, image.Height);
+            surface.SkiaSurface.Canvas.DrawImage(image, new SKPoint(0, 0));
+            return surface;
         }
 
         public static WriteableBitmap ImportWriteableBitmap(string path)

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

@@ -98,7 +98,7 @@ namespace PixiEditor.Models.Layers
                 isVisible = value;
                 RaisePropertyChanged(nameof(IsVisibleUndoTriggerable));
                 RaisePropertyChanged(nameof(IsVisible));
-                ViewModelMain.Current.ToolsSubViewModel.TriggerCacheOutdated();
+                ViewModelMain.Current?.ToolsSubViewModel?.TriggerCacheOutdated();
                 InvokeLayerBitmapChange();
             }
         }
@@ -152,7 +152,7 @@ namespace PixiEditor.Models.Layers
             set
             {
                 RaisePropertyChanged(nameof(OpacityUndoTriggerable));
-                ViewModelMain.Current.ToolsSubViewModel.TriggerCacheOutdated();
+                ViewModelMain.Current?.ToolsSubViewModel?.TriggerCacheOutdated();
                 InvokeLayerBitmapChange();
             }
         }

+ 4 - 5
PixiEditor/Models/Position/CoordinatesCalculator.cs

@@ -1,5 +1,4 @@
 using PixiEditor.Models.DataHolders;
-using SkiaSharp;
 using System;
 using System.Collections.Generic;
 
@@ -90,7 +89,7 @@ namespace PixiEditor.Models.Position
             {
                 for (int x = 0; x < bitmap.Width; x++)
                 {
-                    if (bitmap.GetSRGBPixel(x, y) != SKColors.Transparent)
+                    if (bitmap.GetSRGBPixel(x, y).Alpha != 0)
                     {
                         return y;
                     }
@@ -106,7 +105,7 @@ namespace PixiEditor.Models.Position
             {
                 for (int y = 0; y < bitmap.Height; y++)
                 {
-                    if (bitmap.GetSRGBPixel(x, y) != SKColors.Transparent)
+                    if (bitmap.GetSRGBPixel(x, y).Alpha != 0)
                     {
                         return x;
                     }
@@ -122,7 +121,7 @@ namespace PixiEditor.Models.Position
             {
                 for (int x = bitmap.Width - 1; x >= 0; x--)
                 {
-                    if (bitmap.GetSRGBPixel(x, y) != SKColors.Transparent)
+                    if (bitmap.GetSRGBPixel(x, y).Alpha != 0)
                     {
                         return y;
                     }
@@ -138,7 +137,7 @@ namespace PixiEditor.Models.Position
             {
                 for (int y = bitmap.Height - 1; y >= 0; y--)
                 {
-                    if (bitmap.GetSRGBPixel(x, y) != SKColors.Transparent)
+                    if (bitmap.GetSRGBPixel(x, y).Alpha != 0)
                     {
                         return x;
                     }

+ 15 - 15
PixiEditorTests/ModelsTests/DataHoldersTests/LayerStructureTests.cs

@@ -1,6 +1,6 @@
-using System;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.Layers;
+using System;
 using Xunit;
 
 namespace PixiEditorTests.ModelsTests.DataHoldersTests
@@ -60,7 +60,7 @@ namespace PixiEditorTests.ModelsTests.DataHoldersTests
         [Fact]
         public void TestThatIsChildOfDetectsNestedGroupCorrectly()
         {
-            LayerStructure ls = new LayerStructure(new Document(0, 0));
+            LayerStructure ls = new LayerStructure(new Document(1, 1));
             Layer testLayer = new Layer("tst");
             ls.Groups.Add(new GuidStructureItem("group 1", testLayer.LayerGuid));
             ls.Groups[0].Subgroups.Add(new GuidStructureItem("group 1 nested", testLayer.LayerGuid));
@@ -72,7 +72,7 @@ namespace PixiEditorTests.ModelsTests.DataHoldersTests
         [Fact]
         public void TestThatIsChildOfDetectsNestedLayersCorrectly()
         {
-            var doc = new Document(0, 0);
+            var doc = new Document(1, 1);
             doc.Layers.Add(new Layer("tst"));
             Guid testLayerGuid = doc.Layers[0].LayerGuid;
             LayerStructure ls = new LayerStructure(doc);
@@ -86,7 +86,7 @@ namespace PixiEditorTests.ModelsTests.DataHoldersTests
         [Fact]
         public void TestThatGroupContainsOnlyLayerDetectsOnlySingleLayerCorrectly()
         {
-            var doc = new Document(0, 0);
+            var doc = new Document(1, 1);
             doc.Layers.Add(new Layer("layer"));
             var guid = doc.Layers[0].LayerGuid;
             doc.LayerStructure.AddNewGroup("layer group", guid);
@@ -96,7 +96,7 @@ namespace PixiEditorTests.ModelsTests.DataHoldersTests
         [Fact]
         public void TestThatGroupContainsOnlyLayerDetectsOnlySingleLayerThatIsNested()
         {
-            var doc = new Document(0, 0);
+            var doc = new Document(1, 1);
             doc.Layers.Add(new Layer("layer"));
             var guid = doc.Layers[0].LayerGuid;
             doc.LayerStructure.AddNewGroup("layer group", guid);
@@ -108,7 +108,7 @@ namespace PixiEditorTests.ModelsTests.DataHoldersTests
         [Fact]
         public void TestThatCloneReturnsSameLayerStructure()
         {
-            Document doc = new(0, 0);
+            Document doc = new(1, 1);
             doc.Layers.Add(new("Test"));
             doc.Layers.Add(new("Test2"));
             LayerStructure structure = new(doc);
@@ -124,7 +124,7 @@ namespace PixiEditorTests.ModelsTests.DataHoldersTests
         [Fact]
         public void TestThatGetGroupByGuidReturnsNullForNonExistingGroup()
         {
-            Document doc = new(0, 0);
+            Document doc = new(1, 1);
             doc.Layers.Add(new("Test"));
 
             Assert.Null(doc.LayerStructure.GetGroupByGuid(null));
@@ -134,7 +134,7 @@ namespace PixiEditorTests.ModelsTests.DataHoldersTests
         [Fact]
         public void TestThatGetGroupByGuidReturnsGroupCorrectly()
         {
-            Document doc = new(0, 0);
+            Document doc = new(1, 1);
             doc.Layers.Add(new("Test"));
             var group = doc.LayerStructure.AddNewGroup("Test group", doc.Layers[0].LayerGuid);
 
@@ -144,7 +144,7 @@ namespace PixiEditorTests.ModelsTests.DataHoldersTests
         [Fact]
         public void TestThatPreMoveReassignBoundsMakesNestedGroupEmptyAndRemovesItAndParent()
         {
-            Document doc = new(0, 0);
+            Document doc = new(1, 1);
             doc.Layers.Add(new("Test"));
             doc.LayerStructure.AddNewGroup("Test group", doc.Layers[0].LayerGuid);
             var group1 = doc.LayerStructure.AddNewGroup("Test group nested", doc.Layers[0].LayerGuid);
@@ -157,7 +157,7 @@ namespace PixiEditorTests.ModelsTests.DataHoldersTests
         [Fact]
         public void TestThatPostMoveReassignBoundsAssignsNewLayerToGroup()
         {
-            Document doc = new(0, 0);
+            Document doc = new(1, 1);
             doc.Layers.Add(new("Test"));
             doc.LayerStructure.AddNewGroup("Test group", doc.Layers[0].LayerGuid);
             var group1 = doc.LayerStructure.AddNewGroup("Test group nested", doc.Layers[0].LayerGuid);
@@ -180,7 +180,7 @@ namespace PixiEditorTests.ModelsTests.DataHoldersTests
         [Fact]
         public void TestThatAssignParentAssignsParent()
         {
-            Document doc = new(0, 0);
+            Document doc = new(1, 1);
             doc.Layers.Add(new Layer("Test"));
 
             var firstLayer = doc.Layers[0];
@@ -200,7 +200,7 @@ namespace PixiEditorTests.ModelsTests.DataHoldersTests
         [Fact]
         public void TestThatAssignParentDeAssignsParentOnNull()
         {
-            Document doc = new(0, 0);
+            Document doc = new(1, 1);
             doc.Layers.Add(new Layer("Test"));
 
             var firstLayer = doc.Layers[0];
@@ -211,7 +211,7 @@ namespace PixiEditorTests.ModelsTests.DataHoldersTests
 
             var layer = doc.Layers[^1];
 
-            doc.LayerStructure.AssignParent(layer.LayerGuid,  doc.LayerStructure.Groups[0].GroupGuid);
+            doc.LayerStructure.AssignParent(layer.LayerGuid, doc.LayerStructure.Groups[0].GroupGuid);
             doc.LayerStructure.AssignParent(layer.LayerGuid, null);
 
             Assert.Equal(firstLayer.LayerGuid, doc.LayerStructure.Groups[0].EndLayerGuid);
@@ -221,7 +221,7 @@ namespace PixiEditorTests.ModelsTests.DataHoldersTests
         [Fact]
         public void TestThatGetGroupLayersReturnsAllLayersInGroup()
         {
-            Document doc = new(0, 0);
+            Document doc = new(1, 1);
             doc.Layers.Add(new Layer("Test"));
             doc.Layers.Add(new Layer("Test 1"));
             doc.Layers.Add(new Layer("Test 2"));
@@ -240,4 +240,4 @@ namespace PixiEditorTests.ModelsTests.DataHoldersTests
             Assert.Contains(doc.Layers[2], layersInGroup);
         }
     }
-}
+}

+ 6 - 5
PixiEditorTests/ModelsTests/DataHoldersTests/SelectionTests.cs

@@ -1,9 +1,9 @@
-using System;
-using System.Collections.Generic;
-using PixiEditor.Helpers;
+using PixiEditor.Helpers;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.Enums;
 using PixiEditor.Models.Position;
+using System;
+using System.Collections.Generic;
 using Xunit;
 
 namespace PixiEditorTests.ModelsTests.DataHoldersTests
@@ -55,7 +55,8 @@ namespace PixiEditorTests.ModelsTests.DataHoldersTests
             selection.Clear();
 
             Assert.Empty(selection.SelectedPoints);
-            Assert.Equal(0, selection.SelectionLayer.Width + selection.SelectionLayer.Height);
+            Assert.Equal(1, selection.SelectionLayer.Width);
+            Assert.Equal(1, selection.SelectionLayer.Height);
         }
 
         [Fact]
@@ -76,4 +77,4 @@ namespace PixiEditorTests.ModelsTests.DataHoldersTests
             Assert.Equal(oldSelection, document.ActiveSelection.SelectedPoints);
         }
     }
-}
+}

+ 1 - 1
PixiEditorTests/ModelsTests/IO/ImporterTests.cs

@@ -34,7 +34,7 @@ namespace PixiEditorTests.ModelsTests.IO
         [Fact]
         public void TestThatImportImageImportsImage()
         {
-            SKColor color = new SKColor(255, 255, 0, 0);
+            SKColor color = new SKColor(255, 0, 0, 255);
             Surface image = Importer.ImportSurface(testImagePath);
 
             Assert.NotNull(image);

+ 2 - 2
PixiEditorTests/ModelsTests/LayersTests/LayerTests.cs

@@ -14,8 +14,8 @@ namespace PixiEditorTests.ModelsTests.LayersTests
             Layer layer = new Layer("layer");
 
             Assert.Equal("layer", layer.Name);
-            Assert.Equal(0, layer.Width);
-            Assert.Equal(0, layer.Height);
+            Assert.Equal(1, layer.Width);
+            Assert.Equal(1, layer.Height);
             Assert.Equal(1, layer.LayerBitmap.Width);
             Assert.Equal(1, layer.LayerBitmap.Height);
         }

+ 2 - 2
PixiEditorTests/ModelsTests/UndoTests/StorageBasedChangeTests.cs

@@ -26,8 +26,8 @@ namespace PixiEditorTests.ModelsTests.UndoTests
         public Document GenerateTestDocument()
         {
             Document testDocument = new Document(10, 10);
-            using Surface testBitmap = new Surface(10, 10);
-            using Surface testBitmap2 = new Surface(5, 8);
+            Surface testBitmap = new Surface(10, 10);
+            Surface testBitmap2 = new Surface(5, 8);
             testBitmap.SetSRGBPixel(0, 0, SKColors.Black);
             testBitmap2.SetSRGBPixel(4, 4, SKColors.Blue);
             Random random = new Random();