Browse Source

Fixed tests

flabbet 4 years ago
parent
commit
8a29f94238

+ 1 - 1
PixiEditor/Helpers/Extensions/ParserHelpers.cs

@@ -25,7 +25,7 @@ namespace PixiEditor.Helpers.Extensions
 
         public static ObservableCollection<Layer> ToLayers(this Parser.SerializableDocument serializableDocument)
         {
-            ObservableCollection<Layer> layers = new ObservableCollection<Layer>();
+            ObservableCollection<Layer> layers = new();
             for (int i = 0; i < serializableDocument.Layers.Length; i++)
             {
                 Parser.SerializableLayer serLayer = serializableDocument.Layers[i];

+ 0 - 60
PixiEditor/Models/DataHolders/SerializableDocument.cs

@@ -1,60 +0,0 @@
-using System;
-using System.Collections.ObjectModel;
-using System.Linq;
-using System.Windows;
-using System.Windows.Media;
-using PixiEditor.Models.ImageManipulation;
-using PixiEditor.Models.Layers;
-
-namespace PixiEditor.Models.DataHolders
-{
-    [Serializable]
-    public class SerializableDocument
-    {
-        public SerializableDocument(Document document)
-        {
-            Width = document.Width;
-            Height = document.Height;
-            Layers = document.Layers.Select(x => new SerializableLayer(x)).ToArray();
-            Swatches = document.Swatches.Select(x => new Tuple<byte, byte, byte, byte>(x.A, x.R, x.G, x.B)).ToArray();
-        }
-
-        public int Width { get; set; }
-
-        public int Height { get; set; }
-
-        public SerializableLayer[] Layers { get; set; }
-
-        public Tuple<byte, byte, byte, byte>[] Swatches { get; set; }
-
-        public Document ToDocument()
-        {
-            Document document = new Document(Width, Height)
-            {
-                Layers = ToLayers(),
-                Swatches = new ObservableCollection<Color>(Swatches.Select(x =>
-                    Color.FromArgb(x.Item1, x.Item2, x.Item3, x.Item4)))
-            };
-            return document;
-        }
-
-        public ObservableCollection<Layer> ToLayers()
-        {
-            ObservableCollection<Layer> layers = new ObservableCollection<Layer>();
-            for (int i = 0; i < Layers.Length; i++)
-            {
-                SerializableLayer serLayer = Layers[i];
-                Layer layer =
-                    new Layer(serLayer.Name, BitmapUtils.BytesToWriteableBitmap(serLayer.Width, serLayer.Height, serLayer.BitmapBytes))
-                    {
-                        IsVisible = serLayer.IsVisible,
-                        Offset = new Thickness(serLayer.OffsetX, serLayer.OffsetY, 0, 0),
-                        Opacity = serLayer.Opacity
-                    };
-                layers.Add(layer);
-            }
-
-            return layers;
-        }
-    }
-}

+ 34 - 0
PixiEditor/Models/DataHolders/SerializableGuidStructureItem.cs

@@ -0,0 +1,34 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace PixiEditor.Models.DataHolders
+{
+    [Serializable]
+    public class SerializableGuidStructureItem
+    {
+        public Guid GroupGuid { get; set; }
+
+        public string Name { get; set; }
+
+        public Guid StartLayerGuid { get; set; }
+
+        public Guid EndLayerGuid { get; set; }
+
+        public SerializableGuidStructureItem[] Subgroups { get; set; }
+
+        public SerializableGuidStructureItem Parent { get; set; }
+
+        public SerializableGuidStructureItem(Guid groupGuid, string name, Guid startLayerGuid, Guid endLayerGuid, SerializableGuidStructureItem[] subgroups, SerializableGuidStructureItem parent)
+        {
+            GroupGuid = groupGuid;
+            Name = name;
+            StartLayerGuid = startLayerGuid;
+            EndLayerGuid = endLayerGuid;
+            Subgroups = subgroups;
+            Parent = parent;
+        }
+    }
+}

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

@@ -1,77 +0,0 @@
-using System;
-using System.Linq;
-
-namespace PixiEditor.Models.Layers
-{
-    [Serializable]
-    public class SerializableLayer
-    {
-        public SerializableLayer(Layer layer)
-        {
-            Name = layer.Name;
-            Width = layer.Width;
-            Height = layer.Height;
-            BitmapBytes = layer.ConvertBitmapToBytes();
-            IsVisible = layer.IsVisible;
-            OffsetX = (int)layer.Offset.Left;
-            OffsetY = (int)layer.Offset.Top;
-            Opacity = layer.Opacity;
-            MaxWidth = layer.MaxWidth;
-            MaxHeight = layer.MaxHeight;
-        }
-
-        public string Name { get; set; }
-
-        public int Width { get; set; }
-
-        public int Height { get; set; }
-
-        public int MaxWidth { get; set; }
-
-        public int MaxHeight { get; set; }
-
-        public byte[] BitmapBytes { get; set; }
-
-        public bool IsVisible { get; set; }
-
-        public int OffsetX { get; set; }
-
-        public int OffsetY { get; set; }
-
-        public float Opacity { get; set; }
-
-        public override bool Equals(object obj)
-        {
-            if (obj == null || obj.GetType() != typeof(SerializableLayer))
-            {
-                return false;
-            }
-
-            SerializableLayer layer = (SerializableLayer)obj;
-
-            return Equals(layer);
-        }
-
-        public override int GetHashCode()
-        {
-            HashCode hashCode = default(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();
-        }
-
-        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);
-        }
-    }
-}

+ 4 - 1
PixiEditor/ViewModels/SubViewModels/Main/LayersViewModel.cs

@@ -141,7 +141,10 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             Guid lastActiveLayerGuid = doc.ActiveLayerGuid;
 
             doc.AddNewLayer($"New Layer {Owner.BitmapManager.ActiveDocument.Layers.Count}");
-            doc.MoveLayerInStructure(doc.Layers[^1].LayerGuid, lastActiveLayerGuid, true);
+            if (doc.Layers.Count > 1)
+            {
+                doc.MoveLayerInStructure(doc.Layers[^1].LayerGuid, lastActiveLayerGuid, true);
+            }
         }
 
         public bool CanCreateNewLayer(object parameter)

+ 43 - 25
PixiEditor/Views/UserControls/LayerGroupControl.xaml.cs

@@ -23,6 +23,9 @@ namespace PixiEditor.Views.UserControls
             set { SetValue(GroupGuidProperty, value); }
         }
 
+        private const string LayerGroupControlDataName = "PixiEditor.Views.UserControls.LayerGroupControl";
+        private const string LayerContainerDataName = "PixiEditor.Views.UserControls.LayerStructureItemContainer";
+
         // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
         public static readonly DependencyProperty GroupGuidProperty =
             DependencyProperty.Register("GroupGuid", typeof(Guid), typeof(LayerGroupControl), new PropertyMetadata(Guid.NewGuid()));
@@ -148,42 +151,57 @@ namespace PixiEditor.Views.UserControls
 
         private void HandleDrop(IDataObject dataObj, bool above)
         {
-            Guid referenceLayer = above ? (Guid)GroupData.EndLayerGuid : (Guid)GroupData.StartLayerGuid;
+            Guid referenceLayer = above ? GroupData.EndLayerGuid : GroupData.StartLayerGuid;
 
-            if (dataObj.GetDataPresent("PixiEditor.Views.UserControls.LayerStructureItemContainer"))
+            if (dataObj.GetDataPresent(LayerContainerDataName))
             {
-                var data = (LayerStructureItemContainer)dataObj.GetData("PixiEditor.Views.UserControls.LayerStructureItemContainer");
-                Guid group = data.Layer.LayerGuid;
-
-                data.LayerCommandsViewModel.Owner.BitmapManager.ActiveDocument.MoveLayerInStructure(group, referenceLayer, above);
-                data.LayerCommandsViewModel.Owner.BitmapManager.ActiveDocument.LayerStructure.AssignParent(group, GroupData.Parent.GroupGuid);
+                HandleLayerDrop(dataObj, above, referenceLayer);
             }
 
-            if (dataObj.GetDataPresent("PixiEditor.Views.UserControls.LayerGroupControl"))
+            if (dataObj.GetDataPresent(LayerGroupControlDataName))
             {
-                var data = (LayerGroupControl)dataObj.GetData("PixiEditor.Views.UserControls.LayerGroupControl");
-                var document = data.LayersViewModel.Owner.BitmapManager.ActiveDocument;
-
-                Guid group = data.GroupGuid;
+                HandleGroupControlDrop(dataObj, referenceLayer, above);
+            }
+        }
 
-                if(group == GroupGuid || document.LayerStructure.IsChildOf(GroupData, data.GroupData))
-                {
-                    return;
-                }
+        private void HandleLayerDrop(IDataObject dataObj, bool above, Guid referenceLayer)
+        {
+            var data = (LayerStructureItemContainer)dataObj.GetData(LayerContainerDataName);
+            Guid group = data.Layer.LayerGuid;
 
-                int modifier = above ? 1 : -1;
+            data.LayerCommandsViewModel.Owner.BitmapManager.ActiveDocument.MoveLayerInStructure(group, referenceLayer, above);
+            data.LayerCommandsViewModel.Owner.BitmapManager.ActiveDocument.LayerStructure.AssignParent(group, GroupData.Parent.GroupGuid);
+        }
 
-                Layer layer = document.Layers.First(x => x.LayerGuid == referenceLayer);
-                int indexOfReferenceLayer = Math.Clamp(document.Layers.IndexOf(layer) + modifier, 0, document.Layers.Count);
+        private void HandleGroupControlDrop(IDataObject dataObj, Guid referenceLayer, bool above)
+        {
+            var data = (LayerGroupControl)dataObj.GetData(LayerGroupControlDataName);
+            var document = data.LayersViewModel.Owner.BitmapManager.ActiveDocument;
 
-                Layer tempLayer = new("_temp");
-                document.Layers.Insert(indexOfReferenceLayer, tempLayer);
+            Guid group = data.GroupGuid;
 
-                document.LayerStructure.AssignParent(tempLayer.LayerGuid, GroupData.Parent.GroupGuid);
-                document.MoveGroupInStructure(group, tempLayer.LayerGuid, above);
-                document.LayerStructure.AssignParent(tempLayer.LayerGuid, null);
-                document.RemoveLayer(tempLayer, false);
+            if (group == GroupGuid || document.LayerStructure.IsChildOf(GroupData, data.GroupData))
+            {
+                return;
             }
+
+            int modifier = above ? 1 : 0;
+
+            Layer layer = document.Layers.First(x => x.LayerGuid == referenceLayer);
+            int indexOfReferenceLayer = Math.Clamp(document.Layers.IndexOf(layer) + modifier, 0, document.Layers.Count);
+            MoveGroupWithTempLayer(above, document, group, indexOfReferenceLayer);
+        }
+
+        private void MoveGroupWithTempLayer(bool above, Models.DataHolders.Document document, Guid group, int indexOfReferenceLayer)
+        {
+            // The trick here is to insert a temp layer, assign group to it, then delete it.
+            Layer tempLayer = new("_temp");
+            document.Layers.Insert(indexOfReferenceLayer, tempLayer);
+
+            document.LayerStructure.AssignParent(tempLayer.LayerGuid, GroupData?.Parent?.GroupGuid);
+            document.MoveGroupInStructure(group, tempLayer.LayerGuid, above);
+            document.LayerStructure.AssignParent(tempLayer.LayerGuid, null);
+            document.RemoveLayer(tempLayer, false);
         }
 
         private void Grid_Drop_Top(object sender, DragEventArgs e)

+ 13 - 6
PixiEditorTests/ModelsTests/DataHoldersTests/DocumentTests.cs

@@ -243,7 +243,7 @@ namespace PixiEditorTests.ModelsTests.DataHoldersTests
         [InlineData(3, 1, 1)]
         [InlineData(3, 2, -2)]
         [InlineData(10, 9, -5)]
-        public void TestThatMoveLayerIndexByWorks(int layersAmount, int index, int amount)
+        public void TestThatMoveLayerInStructureWorks(int layersAmount, int index, int amount)
         {
             Document document = new Document(10, 10);
             for (int i = 0; i < layersAmount; i++)
@@ -252,20 +252,24 @@ namespace PixiEditorTests.ModelsTests.DataHoldersTests
             }
 
             Guid oldGuid = document.Layers[index].LayerGuid;
-            document.MoveLayerIndexBy(index, amount);
+            Guid referenceGuid = document.Layers[index + amount].LayerGuid;
+            document.MoveLayerInStructure(oldGuid, referenceGuid, amount > 0);
 
             Assert.Equal(oldGuid, document.Layers[index + amount].LayerGuid);
         }
 
         [Fact]
-        public void TestThatMoveLayerIndexByUndoProcessWorks()
+        public void TestThatMoveLayerInStructureUndoProcessWorks()
         {
             Document document = new Document(10, 10);
 
             document.AddNewLayer("Test");
             document.AddNewLayer("Test2");
 
-            document.MoveLayerIndexBy(0, 1);
+            Guid oldGuid = document.Layers[0].LayerGuid;
+            Guid referenceGuid = document.Layers[1].LayerGuid;
+
+            document.MoveLayerInStructure(oldGuid, referenceGuid);
 
             document.UndoManager.Undo();
 
@@ -274,14 +278,17 @@ namespace PixiEditorTests.ModelsTests.DataHoldersTests
         }
 
         [Fact]
-        public void TestThatMoveLayerIndexByRedoProcessWorks()
+        public void TestThatMoveLayerInStructureRedoProcessWorks()
         {
             Document document = new Document(10, 10);
 
             document.AddNewLayer("Test");
             document.AddNewLayer("Test2");
 
-            document.MoveLayerIndexBy(0, 1);
+            Guid oldGuid = document.Layers[0].LayerGuid;
+            Guid referenceGuid = document.Layers[1].LayerGuid;
+
+            document.MoveLayerInStructure(oldGuid, referenceGuid, true);
 
             document.UndoManager.Undo();
             document.UndoManager.Redo();

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

@@ -1,83 +0,0 @@
-using System;
-using System.Linq;
-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);
-
-            Color 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);
-
-            System.Collections.ObjectModel.ObservableCollection<Layer> 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 static Document GenerateSampleDocument()
-        {
-            Document document = new Document(10, 10);
-            document.Layers.Add(new Layer("Test", 5, 8));
-            document.Swatches.Add(Colors.Green);
-            return document;
-        }
-    }
-}

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

@@ -1,43 +0,0 @@
-using System.IO;
-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);
-
-            SerializableDocument 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);
-        }
-    }
-}

+ 7 - 7
PixiEditorTests/ModelsTests/LayerStructureTests/LayerStructureCloneTests.cs

@@ -1,10 +1,10 @@
-using PixiEditor.Models.DataHolders;
-using PixiEditor.Models.Layers;
-using System;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
+using PixiEditor.Models.DataHolders;
+using PixiEditor.Models.Layers;
 using Xunit;
 
 namespace PixiEditorTests.ModelsTests.LayerStructureTests
@@ -19,11 +19,11 @@ namespace PixiEditorTests.ModelsTests.LayerStructureTests
             LayerStructure structure = new(doc);
             structure.AddNewGroup("Test group", doc.Layers[0].LayerGuid);
 
-            var clone = structure.Clone();
+            var clone = structure.CloneGroups();
 
-            Assert.Equal(doc, clone.Owner);
-            Assert.Single(clone.Groups);
-            Assert.Equal(structure.Groups[0].GroupGuid, clone.Groups[0].GroupGuid);
+            Assert.Equal(structure.Groups, clone);
+            Assert.Single(clone);
+            Assert.Equal(structure.Groups[0].GroupGuid, clone[0].GroupGuid);
         }
     }
 }