From cd99a2e8488e3376fb012fcd996d2933307a580c Mon Sep 17 00:00:00 2001 From: Garrett Vernon <70212190+garv5014@users.noreply.github.com> Date: Wed, 18 Oct 2023 01:07:48 -0600 Subject: [PATCH] [FZ Editor]Add tests to editor #197 + feedback (#29051) * here are the tests for the fancy zones * Wrote tests for GridLayoutModel * Move FancyZonesEditor tests to right place, tests for default layout model * fixed SettingTheVerticalLayoutShouldBeTheDefault test * removed coverlet in the test project * Fixes for comments on pr * squashed and updated for comments * Added the test to the pipeline --------- Co-authored-by: Drew Gordon Co-authored-by: Caleb Wightman --- .../ci/templates/build-powertoys-steps.yml | 1 + PowerToys.sln | 15 +++ .../DefaultLayoutsModelTests.cs | 59 ++++++++++++ .../GridLayoutModelTests.cs | 95 +++++++++++++++++++ .../UnitTests-FancyZonesEditor.csproj | 29 ++++++ .../UnitTests-FancyZonesEditor/Usings.cs | 5 + 6 files changed, 204 insertions(+) create mode 100644 src/modules/fancyzones/UnitTests-FancyZonesEditor/DefaultLayoutsModelTests.cs create mode 100644 src/modules/fancyzones/UnitTests-FancyZonesEditor/GridLayoutModelTests.cs create mode 100644 src/modules/fancyzones/UnitTests-FancyZonesEditor/UnitTests-FancyZonesEditor.csproj create mode 100644 src/modules/fancyzones/UnitTests-FancyZonesEditor/Usings.cs diff --git a/.pipelines/ci/templates/build-powertoys-steps.yml b/.pipelines/ci/templates/build-powertoys-steps.yml index b9bb635f88..ba7aec6f36 100644 --- a/.pipelines/ci/templates/build-powertoys-steps.yml +++ b/.pipelines/ci/templates/build-powertoys-steps.yml @@ -197,6 +197,7 @@ steps: **\UnitTests-PdfThumbnailProvider.dll **\Settings.UI.UnitTests.dll **\UnitTests-GcodePreviewHandler.dll + **\UnitTests-FancyZonesEditor.dll **\UnitTests-PdfPreviewHandler.dll **\UnitTests-PreviewHandlerCommon.dll **\Microsoft.PowerToys.Run.Plugin.Registry.UnitTests.dll diff --git a/PowerToys.sln b/PowerToys.sln index f0d4f78656..d629d970d4 100644 --- a/PowerToys.sln +++ b/PowerToys.sln @@ -537,6 +537,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CropAndLock", "src\modules\ EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CropAndLockModuleInterface", "src\modules\CropAndLock\CropAndLockModuleInterface\CropAndLockModuleInterface.vcxproj", "{3157FA75-86CF-4EE2-8F62-C43F776493C6}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTests-FancyZonesEditor", "src\modules\fancyzones\UnitTests-FancyZonesEditor\UnitTests-FancyZonesEditor.csproj", "{FC8EB78F-F061-4BD9-A3F6-507BEA965E2B}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|ARM64 = Debug|ARM64 @@ -2307,6 +2309,18 @@ Global {3157FA75-86CF-4EE2-8F62-C43F776493C6}.Release|x64.Build.0 = Release|x64 {3157FA75-86CF-4EE2-8F62-C43F776493C6}.Release|x86.ActiveCfg = Release|x64 {3157FA75-86CF-4EE2-8F62-C43F776493C6}.Release|x86.Build.0 = Release|x64 + {FC8EB78F-F061-4BD9-A3F6-507BEA965E2B}.Debug|ARM64.ActiveCfg = Debug|ARM64 + {FC8EB78F-F061-4BD9-A3F6-507BEA965E2B}.Debug|ARM64.Build.0 = Debug|ARM64 + {FC8EB78F-F061-4BD9-A3F6-507BEA965E2B}.Debug|x64.ActiveCfg = Debug|x64 + {FC8EB78F-F061-4BD9-A3F6-507BEA965E2B}.Debug|x64.Build.0 = Debug|x64 + {FC8EB78F-F061-4BD9-A3F6-507BEA965E2B}.Debug|x86.ActiveCfg = Debug|x64 + {FC8EB78F-F061-4BD9-A3F6-507BEA965E2B}.Debug|x86.Build.0 = Debug|x64 + {FC8EB78F-F061-4BD9-A3F6-507BEA965E2B}.Release|ARM64.ActiveCfg = Release|ARM64 + {FC8EB78F-F061-4BD9-A3F6-507BEA965E2B}.Release|ARM64.Build.0 = Release|ARM64 + {FC8EB78F-F061-4BD9-A3F6-507BEA965E2B}.Release|x64.ActiveCfg = Release|x64 + {FC8EB78F-F061-4BD9-A3F6-507BEA965E2B}.Release|x64.Build.0 = Release|x64 + {FC8EB78F-F061-4BD9-A3F6-507BEA965E2B}.Release|x86.ActiveCfg = Release|x64 + {FC8EB78F-F061-4BD9-A3F6-507BEA965E2B}.Release|x86.Build.0 = Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -2501,6 +2515,7 @@ Global {3B227528-4BA6-4CAF-B44A-A10C78A64849} = {4574FDD0-F61D-4376-98BF-E5A1262C11EC} {F5E1146E-B7B3-4E11-85FD-270A500BD78C} = {3B227528-4BA6-4CAF-B44A-A10C78A64849} {3157FA75-86CF-4EE2-8F62-C43F776493C6} = {3B227528-4BA6-4CAF-B44A-A10C78A64849} + {FC8EB78F-F061-4BD9-A3F6-507BEA965E2B} = {D1D6BC88-09AE-4FB4-AD24-5DED46A791DD} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {C3A2F9D1-7930-4EF4-A6FC-7EE0A99821D0} diff --git a/src/modules/fancyzones/UnitTests-FancyZonesEditor/DefaultLayoutsModelTests.cs b/src/modules/fancyzones/UnitTests-FancyZonesEditor/DefaultLayoutsModelTests.cs new file mode 100644 index 0000000000..7f495cb207 --- /dev/null +++ b/src/modules/fancyzones/UnitTests-FancyZonesEditor/DefaultLayoutsModelTests.cs @@ -0,0 +1,59 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. +using FancyZonesEditor.Models; + +namespace UnitTestsFancyZonesEditor; + +[TestClass] +public class DefaultLayoutsModelTests +{ + [TestMethod] + public void LayoutCapacityShouldBeNumberOfMonitorConfigurations() + { + var defaultLayoutsModel = new DefaultLayoutsModel(); + var expectedOptionCount = Enum.GetValues(typeof(MonitorConfigurationType)).Length; + + var actualCapacity = defaultLayoutsModel.Layouts.Capacity; + + Assert.AreEqual(expectedOptionCount, actualCapacity); + } + + [TestMethod] + public void OverridingLayoutClearsOldDefault() + { + var defaultLayoutsModel = new DefaultLayoutsModel(); + GridLayoutModel firstLayout = new GridLayoutModel(); + CanvasLayoutModel secondLayout = new CanvasLayoutModel("steve"); + + defaultLayoutsModel.Set(firstLayout, MonitorConfigurationType.Horizontal); + Assert.AreEqual(defaultLayoutsModel.Layouts[(int)MonitorConfigurationType.Horizontal], firstLayout); + + defaultLayoutsModel.Set(secondLayout, MonitorConfigurationType.Horizontal); + Assert.AreNotEqual(defaultLayoutsModel.Layouts[(int)MonitorConfigurationType.Horizontal], firstLayout); + Assert.AreEqual(defaultLayoutsModel.Layouts[(int)MonitorConfigurationType.Horizontal], secondLayout); + } + + [TestMethod] + public void SettingTheVerticalLayoutShouldBeTheDefault() + { + var defaultLayoutsModel = new DefaultLayoutsModel(); + GridLayoutModel firstLayout = new GridLayoutModel(); + defaultLayoutsModel.Set(firstLayout, MonitorConfigurationType.Horizontal); + defaultLayoutsModel.Set(firstLayout, MonitorConfigurationType.Vertical); + + Assert.AreEqual(defaultLayoutsModel.Layouts[(int)MonitorConfigurationType.Vertical], firstLayout); + } + + [TestMethod] + public void RestoringLayoutShouldSetLayouts() + { + var defaultLayoutsModel = new DefaultLayoutsModel(); + GridLayoutModel firstLayout = new GridLayoutModel(); + CanvasLayoutModel secondLayout = new CanvasLayoutModel("steve"); + var restoredLayouts = new List { firstLayout, secondLayout }; + defaultLayoutsModel.Restore(restoredLayouts); + + CollectionAssert.AreEqual(defaultLayoutsModel.Layouts, restoredLayouts); + } +} diff --git a/src/modules/fancyzones/UnitTests-FancyZonesEditor/GridLayoutModelTests.cs b/src/modules/fancyzones/UnitTests-FancyZonesEditor/GridLayoutModelTests.cs new file mode 100644 index 0000000000..60153dde69 --- /dev/null +++ b/src/modules/fancyzones/UnitTests-FancyZonesEditor/GridLayoutModelTests.cs @@ -0,0 +1,95 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using FancyZonesEditor.Models; + +namespace UnitTestsFancyZonesEditor; + +[TestClass] +public class GridLayoutModelTests +{ + [TestMethod] + public void EmptyGridLayoutModelIsNotValid() + { + GridLayoutModel gridLayoutModel = new GridLayoutModel(); + Assert.IsFalse(gridLayoutModel.IsModelValid()); + } + + [TestMethod] + public void GridLayoutModelWithInvalidRowAndColumnCountsIsNotValid() + { + GridLayoutModel gridLayoutModel = new GridLayoutModel(); + gridLayoutModel.Rows = 0; + gridLayoutModel.Columns = 0; + Assert.IsFalse(gridLayoutModel.IsModelValid()); + } + + [TestMethod] + public void GridLayoutModelWithInvalidRowPercentsIsNotValid() + { + GridLayoutModel gridLayoutModel = new GridLayoutModel(); + gridLayoutModel.Rows = 1; + gridLayoutModel.Columns = 1; + gridLayoutModel.RowPercents = new List { 0 }; // Invalid percentage + Assert.IsFalse(gridLayoutModel.IsModelValid()); + } + + [TestMethod] + public void GridLayoutModelWithInvalidColumnPercentsIsNotValid() + { + GridLayoutModel gridLayoutModel = new GridLayoutModel(); + gridLayoutModel.Rows = 1; + gridLayoutModel.Columns = 1; + gridLayoutModel.ColumnPercents = new List { 0 }; // Invalid percentage + Assert.IsFalse(gridLayoutModel.IsModelValid()); + } + + [TestMethod] + public void GridLayoutModelWithInvalidCellChildMapLengthIsNotValid() + { + GridLayoutModel gridLayoutModel = new GridLayoutModel(); + gridLayoutModel.Rows = 2; + gridLayoutModel.Columns = 2; + gridLayoutModel.CellChildMap = new int[2, 1]; // Invalid length + Assert.IsFalse(gridLayoutModel.IsModelValid()); + } + + [TestMethod] + public void GridLayoutModelWithInvalidZoneCountIsNotValid() + { + GridLayoutModel gridLayoutModel = new GridLayoutModel(); + gridLayoutModel.Rows = 2; + gridLayoutModel.Columns = 2; + gridLayoutModel.CellChildMap = new int[,] + { + { 1, 2 }, + { 3, 4 }, + }; // Invalid zone count + Assert.IsFalse(gridLayoutModel.IsModelValid()); + } + + [TestMethod] + public void GridLayoutModelWithValidPropertiesIsValid() + { + GridLayoutModel gridLayoutModel = new GridLayoutModel(); + + // Set valid row and column counts + gridLayoutModel.Rows = 2; + gridLayoutModel.Columns = 2; + + // Set valid percentages for rows and columns + // Should add up to 10000 + gridLayoutModel.RowPercents = new List { 5000, 5000 }; + gridLayoutModel.ColumnPercents = new List { 5000, 5000 }; + + // Set a valid CellChildMap + gridLayoutModel.CellChildMap = new int[,] + { + { 0, 1 }, + { 2, 3 }, + }; // corresponds to 4 zones + + Assert.IsTrue(gridLayoutModel.IsModelValid(), "GridLayoutModel with valid properties should be valid."); + } +} diff --git a/src/modules/fancyzones/UnitTests-FancyZonesEditor/UnitTests-FancyZonesEditor.csproj b/src/modules/fancyzones/UnitTests-FancyZonesEditor/UnitTests-FancyZonesEditor.csproj new file mode 100644 index 0000000000..a146ad62b2 --- /dev/null +++ b/src/modules/fancyzones/UnitTests-FancyZonesEditor/UnitTests-FancyZonesEditor.csproj @@ -0,0 +1,29 @@ + + + + net7.0-windows10.0.20348.0 + 10.0.19041.0 + 10.0.19041.0 + enable + false + enable + + false + true + ..\..\..\..\$(Platform)\$(Configuration)\tests\UnitTest-FancyZonesEditor\ + + + + + + + + + + + + + + + + diff --git a/src/modules/fancyzones/UnitTests-FancyZonesEditor/Usings.cs b/src/modules/fancyzones/UnitTests-FancyZonesEditor/Usings.cs new file mode 100644 index 0000000000..8df48eacab --- /dev/null +++ b/src/modules/fancyzones/UnitTests-FancyZonesEditor/Usings.cs @@ -0,0 +1,5 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +global using Microsoft.VisualStudio.TestTools.UnitTesting;