From 46d380c2b693cc831035d066fda02024fb2ffc3f Mon Sep 17 00:00:00 2001 From: Yu Leng <42196638+moooyo@users.noreply.github.com> Date: Wed, 30 Jul 2025 17:19:40 +0800 Subject: [PATCH] [CmdPal][UnitTest] Refactor system command unit test (#40874) ## Summary of the Pull Request Ok... The AI generated and migrated ut's quality is very poor. We need to refactor it. ## PR Checklist - [x] Closes: #40875 - [x] **Communication:** I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected - [x] **Tests:** Added/updated and all pass - [ ] **Localization:** All end-user-facing strings can be localized - [ ] **Dev docs:** Added/updated - [ ] **New binaries:** Added on the required places - [ ] [JSON for signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json) for new binaries - [ ] [WXS for installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs) for new binaries and localization folder - [ ] [YML for CI pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml) for new test projects - [ ] [YML for signed pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml) - [ ] **Documentation updated:** If checked, please file a pull request on [our docs repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys) and link it here: #xxx ## Detailed Description of the Pull Request / Additional comments ## Validation Steps Performed --------- Co-authored-by: Yu Leng (from Dev Box) --- PowerToys.sln | 21 ++- .../BasicTests.cs | 11 -- .../ImageTests.cs | 57 ++----- ...crosoft.CmdPal.Ext.System.UnitTests.csproj | 1 + .../QueryTests.cs | 147 ++++++++++++------ .../Settings.cs | 40 +++++ .../CommandPaletteUnitTestBase.cs | 27 ++++ .../Microsoft.CmdPal.Ext.UnitTestBase.csproj | 19 +++ .../FallbackSystemCommandItem.cs | 10 +- .../Helpers/Commands.cs | 14 +- .../Helpers/ISettingsInterface.cs | 24 +++ .../Helpers/SettingsManager.cs | 20 +-- .../Pages/SystemCommandPage.cs | 4 +- 13 files changed, 262 insertions(+), 133 deletions(-) create mode 100644 src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.System.UnitTests/Settings.cs create mode 100644 src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.UnitTestsBase/CommandPaletteUnitTestBase.cs create mode 100644 src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.UnitTestsBase/Microsoft.CmdPal.Ext.UnitTestBase.csproj create mode 100644 src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.System/Helpers/ISettingsInterface.cs diff --git a/PowerToys.sln b/PowerToys.sln index f4e07fe73b..4a94baa77f 100644 --- a/PowerToys.sln +++ b/PowerToys.sln @@ -784,6 +784,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.CmdPal.Ext.TimeDa EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.CmdPal.Ext.WindowWalker.UnitTests", "src\modules\cmdpal\Tests\Microsoft.CmdPal.Ext.WindowWalker.UnitTests\Microsoft.CmdPal.Ext.WindowWalker.UnitTests.csproj", "{E816D7B0-4688-4ECB-97CC-3D8E798F3829}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.CmdPal.Ext.UnitTestBase", "src\modules\cmdpal\Tests\Microsoft.CmdPal.Ext.UnitTestsBase\Microsoft.CmdPal.Ext.UnitTestBase.csproj", "{00D8659C-2068-40B6-8B86-759CD6284BBB}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|ARM64 = Debug|ARM64 @@ -2830,6 +2832,14 @@ Global {E816D7B0-4688-4ECB-97CC-3D8E798F3829}.Release|ARM64.Build.0 = Release|ARM64 {E816D7B0-4688-4ECB-97CC-3D8E798F3829}.Release|x64.ActiveCfg = Release|x64 {E816D7B0-4688-4ECB-97CC-3D8E798F3829}.Release|x64.Build.0 = Release|x64 + {00D8659C-2068-40B6-8B86-759CD6284BBB}.Debug|ARM64.ActiveCfg = Debug|ARM64 + {00D8659C-2068-40B6-8B86-759CD6284BBB}.Debug|ARM64.Build.0 = Debug|ARM64 + {00D8659C-2068-40B6-8B86-759CD6284BBB}.Debug|x64.ActiveCfg = Debug|x64 + {00D8659C-2068-40B6-8B86-759CD6284BBB}.Debug|x64.Build.0 = Debug|x64 + {00D8659C-2068-40B6-8B86-759CD6284BBB}.Release|ARM64.ActiveCfg = Release|ARM64 + {00D8659C-2068-40B6-8B86-759CD6284BBB}.Release|ARM64.Build.0 = Release|ARM64 + {00D8659C-2068-40B6-8B86-759CD6284BBB}.Release|x64.ActiveCfg = Release|x64 + {00D8659C-2068-40B6-8B86-759CD6284BBB}.Release|x64.Build.0 = Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -3117,11 +3127,6 @@ Global {D9BD324E-1D80-44AA-8E7B-73EB00944434} = {38BDB927-829B-4C65-9CD9-93FB05D66D65} {8EF25507-2575-4ADE-BF7E-D23376903AB8} = {3846508C-77EB-4034-A702-F8BB263C4F79} {070AC093-C9F2-20AD-0BCD-F318FC2761EA} = {B1234567-1234-1234-1234-123456789ABC} - {E816D7AC-4688-4ECB-97CC-3D8E798F3825} = {8EF25507-2575-4ADE-BF7E-D23376903AB8} - {E816D7AD-4688-4ECB-97CC-3D8E798F3826} = {8EF25507-2575-4ADE-BF7E-D23376903AB8} - {E816D7AE-4688-4ECB-97CC-3D8E798F3827} = {8EF25507-2575-4ADE-BF7E-D23376903AB8} - {E816D7AF-4688-4ECB-97CC-3D8E798F3828} = {8EF25507-2575-4ADE-BF7E-D23376903AB8} - {E816D7B0-4688-4ECB-97CC-3D8E798F3829} = {8EF25507-2575-4ADE-BF7E-D23376903AB8} {2C318EC3-BA86-4372-B1BC-DB0F33C208B2} = {322566EF-20DC-43A6-B9F8-616AF942579A} {BFFB607F-7C78-434B-86B9-DA4C8196A1B5} = {B6C42F16-73EB-477E-8B0D-4E6CF6C20AAC} {66E1534A-1587-42B2-912F-45C994D32904} = {89E20BCE-EB9C-46C8-8B50-E01A82E6FDC3} @@ -3139,6 +3144,12 @@ Global {806BF185-8B89-5BE1-9AA1-DA5BC9487DB9} = {264B412F-DB8B-4CF8-A74B-96998B183045} {F93C2817-C846-4259-84D8-B39A6B57C8DE} = {3527BF37-DFC5-4309-A032-29278CA21328} {8131151D-B0E9-4E18-84A5-E5F946C4480A} = {929C1324-22E8-4412-A9A8-80E85F3985A5} + {E816D7AC-4688-4ECB-97CC-3D8E798F3825} = {8EF25507-2575-4ADE-BF7E-D23376903AB8} + {E816D7AD-4688-4ECB-97CC-3D8E798F3826} = {8EF25507-2575-4ADE-BF7E-D23376903AB8} + {E816D7AE-4688-4ECB-97CC-3D8E798F3827} = {8EF25507-2575-4ADE-BF7E-D23376903AB8} + {E816D7AF-4688-4ECB-97CC-3D8E798F3828} = {8EF25507-2575-4ADE-BF7E-D23376903AB8} + {E816D7B0-4688-4ECB-97CC-3D8E798F3829} = {8EF25507-2575-4ADE-BF7E-D23376903AB8} + {00D8659C-2068-40B6-8B86-759CD6284BBB} = {8EF25507-2575-4ADE-BF7E-D23376903AB8} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {C3A2F9D1-7930-4EF4-A6FC-7EE0A99821D0} diff --git a/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.System.UnitTests/BasicTests.cs b/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.System.UnitTests/BasicTests.cs index 6a83623577..43662a2145 100644 --- a/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.System.UnitTests/BasicTests.cs +++ b/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.System.UnitTests/BasicTests.cs @@ -11,17 +11,6 @@ namespace Microsoft.CmdPal.Ext.System.UnitTests; [TestClass] public class BasicTests { - [TestMethod] - public void CommandsHelperTest() - { - // Setup & Act - var commands = Commands.GetSystemCommands(false, false, false, false); - - // Assert - Assert.IsNotNull(commands); - Assert.IsTrue(commands.Count > 0); - } - [TestMethod] public void IconsHelperTest() { diff --git a/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.System.UnitTests/ImageTests.cs b/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.System.UnitTests/ImageTests.cs index 4cb41d7c7d..38723b9bdb 100644 --- a/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.System.UnitTests/ImageTests.cs +++ b/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.System.UnitTests/ImageTests.cs @@ -16,56 +16,25 @@ namespace Microsoft.CmdPal.Ext.System.UnitTests; [TestClass] public class ImageTests { - [DataTestMethod] - [DataRow("shutdown", "ShutdownIcon")] - [DataRow("restart", "RestartIcon")] - [DataRow("sign out", "LogoffIcon")] - [DataRow("lock", "LockIcon")] - [DataRow("sleep", "SleepIcon")] - [DataRow("hibernate", "SleepIcon")] - [DataRow("recycle bin", "RecycleBinIcon")] - [DataRow("uefi firmware settings", "FirmwareSettingsIcon")] - [DataRow("IPv4 addr", "NetworkAdapterIcon")] - [DataRow("IPV6 addr", "NetworkAdapterIcon")] - [DataRow("MAC addr", "NetworkAdapterIcon")] - public void IconThemeDarkTest(string typedString, string expectedIconPropertyName) + [DataRow(true)] + [DataRow(false)] + [TestMethod] + public void IconThemeTest(bool isDarkIcon) { - var systemPage = new SystemCommandPage(new SettingsManager()); + var systemPage = new SystemCommandPage(new Settings()); + var commands = systemPage.GetItems(); - foreach (var item in systemPage.GetItems()) + foreach (var item in commands) { - if (item.Title.Contains(typedString, StringComparison.OrdinalIgnoreCase) || item.Subtitle.Contains(typedString, StringComparison.OrdinalIgnoreCase)) + var icon = item.Icon; + Assert.IsNotNull(icon, $"Icon for '{item.Title}' should not be null."); + if (isDarkIcon) { - var icon = item.Icon; - Assert.IsNotNull(icon, $"Icon for '{typedString}' should not be null."); - Assert.IsNotEmpty(icon.Dark.Icon, $"Icon for '{typedString}' should not be empty."); + Assert.IsNotEmpty(icon.Dark.Icon, $"Icon for '{item.Title}' should not be empty."); } - } - } - - [DataTestMethod] - [DataRow("shutdown", "ShutdownIcon")] - [DataRow("restart", "RestartIcon")] - [DataRow("sign out", "LogoffIcon")] - [DataRow("lock", "LockIcon")] - [DataRow("sleep", "SleepIcon")] - [DataRow("hibernate", "SleepIcon")] - [DataRow("recycle bin", "RecycleBinIcon")] - [DataRow("uefi firmware settings", "FirmwareSettingsIcon")] - [DataRow("IPv4 addr", "NetworkAdapterIcon")] - [DataRow("IPV6 addr", "NetworkAdapterIcon")] - [DataRow("MAC addr", "NetworkAdapterIcon")] - public void IconThemeLightTest(string typedString, string expectedIconPropertyName) - { - var systemPage = new SystemCommandPage(new SettingsManager()); - - foreach (var item in systemPage.GetItems()) - { - if (item.Title.Contains(typedString, StringComparison.OrdinalIgnoreCase) || item.Subtitle.Contains(typedString, StringComparison.OrdinalIgnoreCase)) + else { - var icon = item.Icon; - Assert.IsNotNull(icon, $"Icon for '{typedString}' should not be null."); - Assert.IsNotEmpty(icon.Light.Icon, $"Icon for '{typedString}' should not be empty."); + Assert.IsNotEmpty(icon.Light.Icon, $"Icon for '{item.Title}' should not be empty."); } } } diff --git a/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.System.UnitTests/Microsoft.CmdPal.Ext.System.UnitTests.csproj b/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.System.UnitTests/Microsoft.CmdPal.Ext.System.UnitTests.csproj index c62d404bc1..64c5884136 100644 --- a/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.System.UnitTests/Microsoft.CmdPal.Ext.System.UnitTests.csproj +++ b/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.System.UnitTests/Microsoft.CmdPal.Ext.System.UnitTests.csproj @@ -20,5 +20,6 @@ + diff --git a/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.System.UnitTests/QueryTests.cs b/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.System.UnitTests/QueryTests.cs index 2fa5469cbb..dc455dd0e4 100644 --- a/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.System.UnitTests/QueryTests.cs +++ b/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.System.UnitTests/QueryTests.cs @@ -5,12 +5,16 @@ using System; using System.Linq; using Microsoft.CmdPal.Ext.System.Helpers; +using Microsoft.CmdPal.Ext.System.Pages; +using Microsoft.CmdPal.Ext.UnitTestBase; +using Microsoft.CommandPalette.Extensions; +using Microsoft.CommandPalette.Extensions.Toolkit; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace Microsoft.CmdPal.Ext.System.UnitTests; [TestClass] -public class QueryTests +public class QueryTests : CommandPaletteUnitTestBase { [DataTestMethod] [DataRow("shutdown", "Shutdown")] @@ -19,87 +23,130 @@ public class QueryTests [DataRow("lock", "Lock")] [DataRow("sleep", "Sleep")] [DataRow("hibernate", "Hibernate")] - public void SystemCommandsTest(string typedString, string expectedCommand) + [DataRow("open recycle", "Open Recycle Bin")] + [DataRow("empty recycle", "Empty Recycle Bin")] + [DataRow("uefi", "UEFI Firmware Settings")] + public void TopLevelPageQueryTest(string input, string matchedTitle) { - // Setup - var commands = Commands.GetSystemCommands(false, false, false, false); + var settings = new Settings(); + var pages = new SystemCommandPage(settings); + var allCommands = pages.GetItems(); - // Act - var result = commands.Where(c => c.Title.Contains(expectedCommand, StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); + var result = Query(input, allCommands); - // Assert + // Empty recycle bin command should exist Assert.IsNotNull(result); - Assert.IsTrue(result.Title.Contains(expectedCommand, StringComparison.OrdinalIgnoreCase)); + + var firstItem = result.FirstOrDefault(); + + Assert.IsNotNull(firstItem, "No items matched the query."); + Assert.AreEqual(matchedTitle, firstItem.Title, $"Expected to match '{input}' but got '{firstItem.Title}'"); } [TestMethod] public void RecycleBinCommandTest() { - // Setup - var commands = Commands.GetSystemCommands(false, false, false, false); + var settings = new Settings(hideEmptyRecycleBin: true); + var pages = new SystemCommandPage(settings); + var allCommands = pages.GetItems(); - // Act - var result = commands.Where(c => c.Title.Contains("Recycle", StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); + var result = Query("recycle", allCommands); - // Assert + // Empty recycle bin command should exist Assert.IsNotNull(result); + + foreach (var item in result) + { + if (item.Title.Contains("Open Recycle Bin") || item.Title.Contains("Empty Recycle Bin")) + { + Assert.Fail("Recycle Bin commands should not be available when hideEmptyRecycleBin is true."); + } + } + + var firstItem = result.FirstOrDefault(); + Assert.IsNotNull(firstItem, "No items matched the query."); + Assert.IsTrue( + firstItem.Title.Contains("Recycle Bin", StringComparison.OrdinalIgnoreCase), + $"Expected to match 'Recycle Bin' but got '{firstItem.Title}'"); } [TestMethod] public void NetworkCommandsTest() { - // Test that network commands can be retrieved - try + var settings = new Settings(); + var pages = new SystemCommandPage(settings); + var allCommands = pages.GetItems(); + + var ipv4Result = Query("IPv4", allCommands); + + Assert.IsNotNull(ipv4Result); + Assert.IsTrue(ipv4Result.Length > 0, "No IPv4 commands matched the query."); + + var ipv6Result = Query("IPv6", allCommands); + Assert.IsNotNull(ipv6Result); + Assert.IsTrue(ipv6Result.Length > 0, "No IPv6 commands matched the query."); + + var macResult = Query("MAC", allCommands); + Assert.IsNotNull(macResult); + Assert.IsTrue(macResult.Length > 0, "No MAC commands matched the query."); + + var findDisconnectedMACResult = false; + foreach (var item in macResult) { - var networkPropertiesList = NetworkConnectionProperties.GetList(); - Assert.IsTrue(networkPropertiesList.Count >= 0); // Should not throw exceptions - } - catch (Exception ex) - { - Assert.Fail($"Network commands should not throw exceptions: {ex.Message}"); + if (item.Details.Body.Contains("Disconnected")) + { + findDisconnectedMACResult = true; + break; + } } + + Assert.IsTrue(findDisconnectedMACResult, "No disconnected MAC address found in the results."); } [TestMethod] - public void UefiCommandIsAvailableTest() + public void HideDisconnectedNetworkInfoTest() { - // Setup - var firmwareType = Win32Helpers.GetSystemFirmwareType(); - var isUefiMode = firmwareType == FirmwareType.Uefi; + var settings = new Settings(hideDisconnectedNetworkInfo: true); + var pages = new SystemCommandPage(settings); + var allCommands = pages.GetItems(); - // Act - var commands = Commands.GetSystemCommands(isUefiMode, false, false, false); - var uefiCommand = commands.Where(c => c.Title.Contains("UEFI", StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); + var macResult = Query("MAC", allCommands); + Assert.IsNotNull(macResult); + Assert.IsTrue(macResult.Length > 0, "No MAC commands matched the query."); - // Assert - if (isUefiMode) + var findDisconnectedMACResult = false; + foreach (var item in macResult) { - Assert.IsNotNull(uefiCommand); - } - else - { - // UEFI command may still exist but be disabled on non-UEFI systems - Assert.IsTrue(true); // Test environment independent + if (item.Details.Body.Contains("Disconnected")) + { + findDisconnectedMACResult = true; + break; + } } + + Assert.IsTrue(!findDisconnectedMACResult, "Disconnected MAC address found in the results."); } [TestMethod] - public void FirmwareTypeTest() + [DataRow(FirmwareType.Uefi, true)] + [DataRow(FirmwareType.Bios, false)] + [DataRow(FirmwareType.Max, false)] + [DataRow(FirmwareType.Unknown, false)] + public void FirmwareSettingsTest(FirmwareType firmwareType, bool hasCommand) { - // Test that GetSystemFirmwareType returns a valid enum value - var firmwareType = Win32Helpers.GetSystemFirmwareType(); - Assert.IsTrue(Enum.IsDefined(typeof(FirmwareType), firmwareType)); - } + var settings = new Settings(firmwareType: firmwareType); + var pages = new SystemCommandPage(settings); + var allCommands = pages.GetItems(); + var result = Query("UEFI", allCommands); - [TestMethod] - public void EmptyRecycleBinCommandTest() - { - // Test that empty recycle bin command exists - var commands = Commands.GetSystemCommands(false, false, false, false); - var result = commands.Where(c => c.Title.Contains("Empty", StringComparison.OrdinalIgnoreCase) && - c.Title.Contains("Recycle", StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); - - // Empty recycle bin command should exist + // UEFI Firmware Settings command should exist Assert.IsNotNull(result); + var firstItem = result.FirstOrDefault(); + Assert.IsNotNull(firstItem, "No items matched the query."); + var containsFirmwareSettings = firstItem.Title.Contains("UEFI Firmware Settings", StringComparison.OrdinalIgnoreCase); + + Assert.IsTrue( + containsFirmwareSettings == hasCommand, + $"Expected to match 'UEFI Firmware Settings' but got '{firstItem.Title}'"); } } diff --git a/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.System.UnitTests/Settings.cs b/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.System.UnitTests/Settings.cs new file mode 100644 index 0000000000..6e2a0dc221 --- /dev/null +++ b/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.System.UnitTests/Settings.cs @@ -0,0 +1,40 @@ +// 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 System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.CmdPal.Ext.System.Helpers; + +namespace Microsoft.CmdPal.Ext.System.UnitTests; + +public class Settings : ISettingsInterface +{ + private bool hideDisconnectedNetworkInfo; + private bool hideEmptyRecycleBin; + private bool showDialogToConfirmCommand; + private bool showSuccessMessageAfterEmptyingRecycleBin; + private FirmwareType firmwareType; + + public Settings(bool hideDisconnectedNetworkInfo = false, bool hideEmptyRecycleBin = false, bool showDialogToConfirmCommand = false, bool showSuccessMessageAfterEmptyingRecycleBin = false, FirmwareType firmwareType = FirmwareType.Uefi) + { + this.hideDisconnectedNetworkInfo = hideDisconnectedNetworkInfo; + this.hideEmptyRecycleBin = hideEmptyRecycleBin; + this.showDialogToConfirmCommand = showDialogToConfirmCommand; + this.showSuccessMessageAfterEmptyingRecycleBin = showSuccessMessageAfterEmptyingRecycleBin; + this.firmwareType = firmwareType; + } + + public bool HideDisconnectedNetworkInfo() => hideDisconnectedNetworkInfo; + + public bool HideEmptyRecycleBin() => hideEmptyRecycleBin; + + public bool ShowDialogToConfirmCommand() => showDialogToConfirmCommand; + + public bool ShowSuccessMessageAfterEmptyingRecycleBin() => showSuccessMessageAfterEmptyingRecycleBin; + + public FirmwareType GetSystemFirmwareType() => firmwareType; +} diff --git a/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.UnitTestsBase/CommandPaletteUnitTestBase.cs b/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.UnitTestsBase/CommandPaletteUnitTestBase.cs new file mode 100644 index 0000000000..a4da29e830 --- /dev/null +++ b/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.UnitTestsBase/CommandPaletteUnitTestBase.cs @@ -0,0 +1,27 @@ +// 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 System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.CommandPalette.Extensions; +using Microsoft.CommandPalette.Extensions.Toolkit; + +namespace Microsoft.CmdPal.Ext.UnitTestBase; + +public class CommandPaletteUnitTestBase +{ + private bool MatchesFilter(string filter, IListItem item) => StringMatcher.FuzzySearch(filter, item.Title).Success || StringMatcher.FuzzySearch(filter, item.Subtitle).Success; + + public IListItem[] Query(string query, IListItem[] candidates) + { + IListItem[] listItems = candidates + .Where(item => MatchesFilter(query, item)) + .ToArray(); + + return listItems; + } +} diff --git a/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.UnitTestsBase/Microsoft.CmdPal.Ext.UnitTestBase.csproj b/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.UnitTestsBase/Microsoft.CmdPal.Ext.UnitTestBase.csproj new file mode 100644 index 0000000000..ad01a7c507 --- /dev/null +++ b/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.UnitTestsBase/Microsoft.CmdPal.Ext.UnitTestBase.csproj @@ -0,0 +1,19 @@ + + + + + + false + Microsoft.CmdPal.Ext.UnitTestsBase + true + + + + + + + + + + + \ No newline at end of file diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.System/FallbackSystemCommandItem.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.System/FallbackSystemCommandItem.cs index 6dfe81af1b..6cb6e7a3ec 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.System/FallbackSystemCommandItem.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.System/FallbackSystemCommandItem.cs @@ -12,16 +12,16 @@ namespace Microsoft.CmdPal.Ext.System; internal sealed partial class FallbackSystemCommandItem : FallbackCommandItem { - public FallbackSystemCommandItem(SettingsManager settings) + public FallbackSystemCommandItem(ISettingsInterface settings) : base(new NoOpCommand(), Resources.Microsoft_plugin_ext_fallback_display_title) { Title = string.Empty; Subtitle = string.Empty; - var isBootedInUefiMode = Win32Helpers.GetSystemFirmwareType() == FirmwareType.Uefi; - var hideEmptyRB = settings.HideEmptyRecycleBin; - var confirmSystemCommands = settings.ShowDialogToConfirmCommand; - var showSuccessOnEmptyRB = settings.ShowSuccessMessageAfterEmptyingRecycleBin; + var isBootedInUefiMode = settings.GetSystemFirmwareType() == FirmwareType.Uefi; + var hideEmptyRB = settings.HideEmptyRecycleBin(); + var confirmSystemCommands = settings.ShowDialogToConfirmCommand(); + var showSuccessOnEmptyRB = settings.ShowSuccessMessageAfterEmptyingRecycleBin(); systemCommands = Commands.GetSystemCommands(isBootedInUefiMode, hideEmptyRB, confirmSystemCommands, showSuccessOnEmptyRB); } diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.System/Helpers/Commands.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.System/Helpers/Commands.cs index 70c1b7f8c6..f3ae6c4b33 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.System/Helpers/Commands.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.System/Helpers/Commands.cs @@ -136,7 +136,7 @@ internal static class Commands /// /// The tSettingsManager instance /// The list of available results - public static List GetNetworkConnectionResults(SettingsManager manager) + public static List GetNetworkConnectionResults(ISettingsInterface manager) { var results = new List(); @@ -151,7 +151,7 @@ internal static class Commands CompositeFormat sysIpv4DescriptionCompositeFormate = CompositeFormat.Parse(Resources.Microsoft_plugin_sys_ip4_description); CompositeFormat sysIpv6DescriptionCompositeFormate = CompositeFormat.Parse(Resources.Microsoft_plugin_sys_ip6_description); CompositeFormat sysMacDescriptionCompositeFormate = CompositeFormat.Parse(Resources.Microsoft_plugin_sys_mac_description); - var hideDisconnectedNetworkInfo = manager.HideDisconnectedNetworkInfo; + var hideDisconnectedNetworkInfo = manager.HideDisconnectedNetworkInfo(); foreach (NetworkConnectionProperties intInfo in networkPropertiesCache) { @@ -200,7 +200,7 @@ internal static class Commands return results; } - public static List GetAllCommands(SettingsManager manager) + public static List GetAllCommands(ISettingsInterface manager) { var list = new List(); var listLock = new object(); @@ -209,11 +209,11 @@ internal static class Commands // On global queries the first word/part has to be 'ip', 'mac' or 'address' for network results var networkConnectionResults = Commands.GetNetworkConnectionResults(manager); - var isBootedInUefiMode = Win32Helpers.GetSystemFirmwareType() == FirmwareType.Uefi; + var isBootedInUefiMode = manager.GetSystemFirmwareType() == FirmwareType.Uefi; - var hideEmptyRB = manager.HideEmptyRecycleBin; - var confirmSystemCommands = manager.ShowDialogToConfirmCommand; - var showSuccessOnEmptyRB = manager.ShowSuccessMessageAfterEmptyingRecycleBin; + var hideEmptyRB = manager.HideEmptyRecycleBin(); + var confirmSystemCommands = manager.ShowDialogToConfirmCommand(); + var showSuccessOnEmptyRB = manager.ShowSuccessMessageAfterEmptyingRecycleBin(); // normal system commands are fast and can be returned immediately var systemCommands = Commands.GetSystemCommands(isBootedInUefiMode, hideEmptyRB, confirmSystemCommands, showSuccessOnEmptyRB); diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.System/Helpers/ISettingsInterface.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.System/Helpers/ISettingsInterface.cs new file mode 100644 index 0000000000..1690007126 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.System/Helpers/ISettingsInterface.cs @@ -0,0 +1,24 @@ +// 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 System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Microsoft.CmdPal.Ext.System.Helpers; + +public interface ISettingsInterface +{ + public bool ShowDialogToConfirmCommand(); + + public bool ShowSuccessMessageAfterEmptyingRecycleBin(); + + public bool HideEmptyRecycleBin(); + + public bool HideDisconnectedNetworkInfo(); + + public FirmwareType GetSystemFirmwareType(); +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.System/Helpers/SettingsManager.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.System/Helpers/SettingsManager.cs index 9d51c42615..952b68d7aa 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.System/Helpers/SettingsManager.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.System/Helpers/SettingsManager.cs @@ -7,7 +7,7 @@ using Microsoft.CommandPalette.Extensions.Toolkit; namespace Microsoft.CmdPal.Ext.System.Helpers; -public class SettingsManager : JsonSettingsManager +public class SettingsManager : JsonSettingsManager, ISettingsInterface { private static readonly string _namespace = "system"; @@ -37,14 +37,6 @@ public class SettingsManager : JsonSettingsManager Resources.Microsoft_plugin_ext_settings_hideDisconnectedNetworkInfo, false); - public bool ShowDialogToConfirmCommand => _showDialogToConfirmCommand.Value; - - public bool ShowSuccessMessageAfterEmptyingRecycleBin => _showSuccessMessageAfterEmptyingRecycleBin.Value; - - public bool HideEmptyRecycleBin => _hideEmptyRecycleBin.Value; - - public bool HideDisconnectedNetworkInfo => _hideDisconnectedNetworkInfo.Value; - internal static string SettingsJsonPath() { var directory = Utilities.BaseSettingsPath("Microsoft.CmdPal"); @@ -54,6 +46,16 @@ public class SettingsManager : JsonSettingsManager return Path.Combine(directory, "settings.json"); } + public bool ShowDialogToConfirmCommand() => _showDialogToConfirmCommand.Value; + + public bool ShowSuccessMessageAfterEmptyingRecycleBin() => _showSuccessMessageAfterEmptyingRecycleBin.Value; + + public bool HideEmptyRecycleBin() => _hideEmptyRecycleBin.Value; + + public bool HideDisconnectedNetworkInfo() => _hideDisconnectedNetworkInfo.Value; + + public FirmwareType GetSystemFirmwareType() => Win32Helpers.GetSystemFirmwareType(); + public SettingsManager() { FilePath = SettingsJsonPath(); diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.System/Pages/SystemCommandPage.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.System/Pages/SystemCommandPage.cs index 3d02cdf18f..bb7deb1c9e 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.System/Pages/SystemCommandPage.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.System/Pages/SystemCommandPage.cs @@ -10,9 +10,9 @@ namespace Microsoft.CmdPal.Ext.System.Pages; public sealed partial class SystemCommandPage : ListPage { - private readonly SettingsManager _settingsManager; + private readonly ISettingsInterface _settingsManager; - public SystemCommandPage(SettingsManager settingsManager) + public SystemCommandPage(ISettingsInterface settingsManager) { Title = Resources.Microsoft_plugin_ext_system_page_title; Name = Resources.Microsoft_plugin_command_name_open;