瀏覽代碼

Fixed tests

flabbet 4 年之前
父節點
當前提交
99cf31ce86

+ 6 - 7
PixiEditor/Models/Controllers/UndoManager.cs

@@ -21,16 +21,15 @@ namespace PixiEditor.Models.Controllers
 
         public UndoManager()
         {
-            SetMainRoot(ViewModelMain.Current.UndoSubViewModel);
+            if (ViewModelMain.Current != null && ViewModelMain.Current.UndoSubViewModel != null)
+            {
+                MainRoot = ViewModelMain.Current.UndoSubViewModel;
+            }
         }
 
-        /// <summary>
-        ///     Sets object(root) in which undo properties are stored.
-        /// </summary>
-        /// <param name="root">Parent object.</param>
-        public void SetMainRoot(object root)
+        public UndoManager(object mainRoot)
         {
-            MainRoot = root;
+            MainRoot = mainRoot;
         }
 
         /// <summary>

+ 11 - 6
PixiEditorTests/ModelsTests/ControllersTests/BitmapManagerTests.cs

@@ -25,7 +25,7 @@ namespace PixiEditorTests.ModelsTests.ControllersTests
             {
                 ActiveDocument = new Document(10, 10)
             };
-            bitmapManager.AddNewLayer(layerName);
+            bitmapManager.ActiveDocument.AddNewLayer(layerName);
             Assert.Single(bitmapManager.ActiveDocument.Layers);
             Assert.Equal(layerName, bitmapManager.ActiveDocument.ActiveLayer.Name);
             Assert.Equal(0, bitmapManager.ActiveDocument.ActiveLayer.Width + bitmapManager.ActiveDocument.ActiveLayer.Height);
@@ -38,10 +38,10 @@ namespace PixiEditorTests.ModelsTests.ControllersTests
             {
                 ActiveDocument = new Document(10, 10)
             };
-            bitmapManager.AddNewLayer("_");
-            bitmapManager.AddNewLayer("_1");
+            bitmapManager.ActiveDocument.AddNewLayer("_");
+            bitmapManager.ActiveDocument.AddNewLayer("_1");
             Assert.Equal(2, bitmapManager.ActiveDocument.Layers.Count);
-            bitmapManager.RemoveLayer(0);
+            bitmapManager.ActiveDocument.RemoveLayer(0);
             Assert.Single(bitmapManager.ActiveDocument.Layers);
         }
 
@@ -72,10 +72,15 @@ namespace PixiEditorTests.ModelsTests.ControllersTests
         {
             BitmapManager bitmapManager = new BitmapManager
             {
-                ActiveDocument = new Document(5, 5)
+                Documents = new System.Collections.ObjectModel.ObservableCollection<Document>()
+                {
+                    new Document(5, 5)
+                }
             };
 
-            bitmapManager.AddNewLayer("Layer");
+            bitmapManager.ActiveDocument = bitmapManager.Documents[0];
+
+            bitmapManager.ActiveDocument.AddNewLayer("Layer");
             bitmapManager.SetActiveTool(new MockedSinglePixelPen());
             bitmapManager.PrimaryColor = Colors.Green;
 

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

@@ -35,7 +35,7 @@ namespace PixiEditorTests.ModelsTests.ControllersTests
                 ActiveDocument = new Document(10, 10),
                 PrimaryColor = Colors.Black
             };
-            manager.AddNewLayer("Test layer", 10, 10);
+            manager.ActiveDocument.AddNewLayer("Test layer", 10, 10);
 
             BitmapOperationsUtility util = new BitmapOperationsUtility(manager);
 

+ 48 - 35
PixiEditorTests/ModelsTests/ControllersTests/UndoManagerTests.cs

@@ -19,75 +19,86 @@ namespace PixiEditorTests.ModelsTests.ControllersTests
         public void TestSetRoot()
         {
             PrepareUndoManagerForTest();
-            UndoManager.SetMainRoot(null);
-            UndoManager.SetMainRoot(this);
-            Assert.Equal(this, UndoManager.MainRoot);
+            UndoManager undoManager = new UndoManager(this);
+            Assert.Equal(this, undoManager.MainRoot);
         }
 
         [Fact]
         public void TestAddToUndoStack()
         {
             PrepareUndoManagerForTest();
-            UndoManager.AddUndoChange(new Change("ExampleProperty", ExampleProperty, ExampleProperty));
-            Assert.True(UndoManager.UndoStack.Count == 1);
-            Assert.True((int)UndoManager.UndoStack.Peek().OldValue == ExampleProperty);
+            UndoManager undoManager = new UndoManager(this);
+
+            undoManager.AddUndoChange(new Change("ExampleProperty", ExampleProperty, ExampleProperty));
+            Assert.True(undoManager.UndoStack.Count == 1);
+            Assert.True((int)undoManager.UndoStack.Peek().OldValue == ExampleProperty);
         }
 
         [Fact]
         public void TestThatUndoAddsToRedoStack()
         {
             PrepareUndoManagerForTest();
-            UndoManager.AddUndoChange(new Change("ExampleProperty", ExampleProperty, ExampleProperty));
-            UndoManager.Undo();
-            Assert.True(UndoManager.RedoStack.Count == 1);
+            UndoManager undoManager = new UndoManager(this);
+
+            undoManager.AddUndoChange(new Change("ExampleProperty", ExampleProperty, ExampleProperty));
+            undoManager.Undo();
+            Assert.True(undoManager.RedoStack.Count == 1);
         }
 
         [Fact]
         public void TestUndo()
         {
             PrepareUndoManagerForTest();
-            UndoManager.AddUndoChange(new Change("ExampleProperty", ExampleProperty, 55));
+            UndoManager undoManager = new UndoManager(this);
+
+            undoManager.AddUndoChange(new Change("ExampleProperty", ExampleProperty, 55));
             ExampleProperty = 55;
-            UndoManager.Undo();
-            Assert.True((int)UndoManager.RedoStack.Peek().OldValue == ExampleProperty);
+            undoManager.Undo();
+            Assert.True((int)undoManager.RedoStack.Peek().OldValue == ExampleProperty);
         }
 
         [Fact]
         public void TestThatRedoAddsToUndoStack()
         {
             PrepareUndoManagerForTest();
-            UndoManager.AddUndoChange(new Change("ExampleProperty", ExampleProperty, ExampleProperty));
-            UndoManager.Undo();
-            UndoManager.Redo();
-            Assert.True(UndoManager.UndoStack.Count == 1);
+            UndoManager undoManager = new UndoManager(this);
+
+            undoManager.AddUndoChange(new Change("ExampleProperty", ExampleProperty, ExampleProperty));
+            undoManager.Undo();
+            undoManager.Redo();
+            Assert.True(undoManager.UndoStack.Count == 1);
         }
 
         [Fact]
         public void TestRedo()
         {
             PrepareUndoManagerForTest();
+            UndoManager undoManager = new UndoManager(this);
+
             ExampleProperty = 55;
-            UndoManager.AddUndoChange(new Change("ExampleProperty", 1, ExampleProperty));
-            UndoManager.Undo();
-            UndoManager.Redo();
-            Assert.True((int)UndoManager.UndoStack.Peek().NewValue == ExampleProperty);
+            undoManager.AddUndoChange(new Change("ExampleProperty", 1, ExampleProperty));
+            undoManager.Undo();
+            undoManager.Redo();
+            Assert.True((int)undoManager.UndoStack.Peek().NewValue == ExampleProperty);
         }
 
         [Fact]
         public void TestThatUndoManagerUndoAndRedoWithCustomRootCorrectly()
         {
             PrepareUndoManagerForTest();
+            UndoManager undoManager = new UndoManager(this);
+
             TestPropertyClass testProp = new TestPropertyClass();
             int newVal = 5;
             testProp.IntProperty = newVal;
-            UndoManager.AddUndoChange(new Change("IntProperty", 0, newVal, root: testProp));
+            undoManager.AddUndoChange(new Change("IntProperty", 0, newVal, root: testProp));
             Assert.Equal(newVal, testProp.IntProperty);
 
-            UndoManager.Undo();
+            undoManager.Undo();
 
             Assert.Equal(0, testProp.IntProperty);
 
-            UndoManager.Redo();
+            undoManager.Redo();
 
             Assert.Equal(newVal, testProp.IntProperty);
         }
@@ -96,10 +107,11 @@ namespace PixiEditorTests.ModelsTests.ControllersTests
         public void TestThatMixedProcessOfUndoAndRedoWorks()
         {
             PrepareUndoManagerForTest();
+            UndoManager undoManager = new UndoManager(this);
 
             int newVal = 5;
 
-            UndoManager.AddUndoChange(
+            undoManager.AddUndoChange(
                 new Change(
                     "ExampleProperty",
                     ReverseProcess,
@@ -110,11 +122,11 @@ namespace PixiEditorTests.ModelsTests.ControllersTests
 
             Assert.Equal(newVal, ExampleProperty);
 
-            UndoManager.Undo();
+            undoManager.Undo();
 
             Assert.Equal(1, ExampleProperty);
 
-            UndoManager.Redo();
+            undoManager.Redo();
 
             Assert.Equal(newVal, ExampleProperty);
         }
@@ -123,8 +135,10 @@ namespace PixiEditorTests.ModelsTests.ControllersTests
         public void TestThatProcessBasedUndoAndRedoWorks()
         {
             PrepareUndoManagerForTest();
+            UndoManager undoManager = new UndoManager(this);
+
             int newVal = 5;
-            UndoManager.AddUndoChange(new Change(
+            undoManager.AddUndoChange(new Change(
                 ReverseProcess,
                 new object[] { ExampleProperty },
                 ReverseProcess,
@@ -134,11 +148,11 @@ namespace PixiEditorTests.ModelsTests.ControllersTests
 
             Assert.Equal(newVal, ExampleProperty);
 
-            UndoManager.Undo();
+            undoManager.Undo();
 
             Assert.Equal(1, ExampleProperty);
 
-            UndoManager.Redo();
+            undoManager.Redo();
 
             Assert.Equal(newVal, ExampleProperty);
         }
@@ -147,19 +161,21 @@ namespace PixiEditorTests.ModelsTests.ControllersTests
         public void TestThatNestedPropertyUndoWorks()
         {
             PrepareUndoManagerForTest();
+            UndoManager undoManager = new UndoManager(this);
+
             int newVal = 5;
 
-            UndoManager.AddUndoChange(new Change("TestPropClass.IntProperty", TestPropClass.IntProperty, newVal));
+            undoManager.AddUndoChange(new Change("TestPropClass.IntProperty", TestPropClass.IntProperty, newVal));
 
             TestPropClass.IntProperty = newVal;
 
             Assert.Equal(newVal, TestPropClass.IntProperty);
 
-            UndoManager.Undo();
+            undoManager.Undo();
 
             Assert.Equal(0, TestPropClass.IntProperty);
 
-            UndoManager.Redo();
+            undoManager.Redo();
 
             Assert.Equal(newVal, TestPropClass.IntProperty);
         }
@@ -171,9 +187,6 @@ namespace PixiEditorTests.ModelsTests.ControllersTests
 
         private void PrepareUndoManagerForTest()
         {
-            UndoManager.SetMainRoot(this);
-            UndoManager.UndoStack.Clear();
-            UndoManager.RedoStack.Clear();
             ExampleProperty = 1;
             TestPropClass = new TestPropertyClass { IntProperty = 0 };
         }

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

@@ -46,7 +46,7 @@ namespace PixiEditorTests.ModelsTests.DataHoldersTests
             {
                 ActiveDocument = document
             };
-            manager.AddNewLayer("test");
+            manager.ActiveDocument.AddNewLayer("test");
             manager.ActiveLayer.SetPixel(
                 new Coordinates(
                 (int)Math.Ceiling(initialWidth / 2f),
@@ -72,13 +72,13 @@ namespace PixiEditorTests.ModelsTests.DataHoldersTests
             {
                 ActiveDocument = document
             };
-            manager.AddNewLayer("test");
+            manager.ActiveDocument.AddNewLayer("test");
             manager.ActiveLayer.SetPixel(
                 new Coordinates(
                 (int)Math.Ceiling(initialWidth / 2f),
                 (int)Math.Ceiling(initialHeight / 2f)), Colors.Black); // Set pixel in center
 
-            manager.AddNewLayer("test2");
+            manager.ActiveDocument.AddNewLayer("test2");
 
             manager.ActiveLayer.SetPixel(new Coordinates(secondLayerPixelX, secondLayerPixelY), Colors.Black);
 
@@ -107,7 +107,7 @@ namespace PixiEditorTests.ModelsTests.DataHoldersTests
             {
                 ActiveDocument = doc
             };
-            manager.AddNewLayer("test");
+            manager.ActiveDocument.AddNewLayer("test");
 
             manager.ActiveLayer.SetPixel(new Coordinates(0, 0), Colors.Green);
 
@@ -128,10 +128,10 @@ namespace PixiEditorTests.ModelsTests.DataHoldersTests
             {
                 ActiveDocument = doc
             };
-            manager.AddNewLayer("test");
+            manager.ActiveDocument.AddNewLayer("test");
             manager.ActiveLayer.SetPixel(new Coordinates(0, 0), Colors.Green);
 
-            manager.AddNewLayer("test2");
+            manager.ActiveDocument.AddNewLayer("test2");
             manager.ActiveLayer.SetPixel(new Coordinates(1, 1), Colors.Green);
 
             doc.CenterContent();

+ 6 - 6
PixiEditorTests/ViewModelsTests/ViewModelMainTests.cs

@@ -18,7 +18,6 @@ namespace PixiEditorTests.ViewModelsTests
         {
             ViewModelMain viewModel = new ViewModelMain();
 
-            Assert.Equal(viewModel.UndoSubViewModel, UndoManager.MainRoot);
             Assert.NotNull(viewModel.ChangesController);
             Assert.NotNull(viewModel.ShortcutController);
             Assert.NotEmpty(viewModel.ShortcutController.Shortcuts);
@@ -112,9 +111,10 @@ namespace PixiEditorTests.ViewModelsTests
             ViewModelMain viewModel = new ViewModelMain();
             string fileName = "testFile.pixi";
 
-            viewModel.BitmapManager.ActiveDocument = new Document(1, 1);
-
-            Exporter.SaveDocumentPath = fileName;
+            viewModel.BitmapManager.ActiveDocument = new Document(1, 1)
+            {
+                DocumentFilePath = fileName
+            };
 
             viewModel.FileSubViewModel.SaveDocumentCommand.Execute(null);
 
@@ -150,13 +150,13 @@ namespace PixiEditorTests.ViewModelsTests
             {
                 BitmapManager = { ActiveDocument = new Document(docWidth, docHeight) }
             };
-            viewModel.BitmapManager.AddNewLayer("layer");
+            viewModel.BitmapManager.ActiveDocument.AddNewLayer("layer");
 
             viewModel.SelectionSubViewModel.SelectAllCommand.Execute(null);
 
             Assert.Equal(
                 viewModel.BitmapManager.ActiveDocument.Width * viewModel.BitmapManager.ActiveDocument.Height,
-                viewModel.SelectionSubViewModel.ActiveSelection.SelectedPoints.Count);
+                viewModel.BitmapManager.ActiveDocument.ActiveSelection.SelectedPoints.Count);
         }
 
         [StaFact]