From 314425e32e95227449ea852a9037aa229c5f6d3b Mon Sep 17 00:00:00 2001 From: Heiko <61519853+htcfreek@users.noreply.github.com> Date: Fri, 4 Feb 2022 17:56:31 +0100 Subject: [PATCH] [PT Run] System commands plugin: Update dev docs and UnitTests (#15832) * update UnitTests * dev docs * fix plugin name * fix spelling * fix path * improvement * fix table --- doc/devdocs/modules/launcher/plugins/sys.md | 23 ---------- .../modules/launcher/plugins/system.md | 46 +++++++++++++++++++ doc/devdocs/modules/launcher/readme.md | 2 +- .../ImageTests.cs | 4 ++ .../QueryTests.cs | 32 ++++++++++++- .../Main.cs | 9 ++-- 6 files changed, 87 insertions(+), 29 deletions(-) delete mode 100644 doc/devdocs/modules/launcher/plugins/sys.md create mode 100644 doc/devdocs/modules/launcher/plugins/system.md diff --git a/doc/devdocs/modules/launcher/plugins/sys.md b/doc/devdocs/modules/launcher/plugins/sys.md deleted file mode 100644 index f8e51d52a5..0000000000 --- a/doc/devdocs/modules/launcher/plugins/sys.md +++ /dev/null @@ -1,23 +0,0 @@ -# Sys Plugin - -As the name suggests, the Sys Plugin is used to directly run Windows system commands that have been entered by the user as a query. This is done by parsing the entry and validating the command, followed by executing it. - -* Shutdown -* Restart -* Sign Out -* Lock -* Sleep -* Hibernate -* Empty Recycle Bin - -![Image of Sys plugin](/doc/images/launcher/plugins/sys.gif) - -## [`Sys`](/src/modules/launcher/Plugins/Microsoft.Plugin.Sys/Main.cs) - -* Tries to parse the user input and returns a specific Windows system command by using a [`Result`](/src/modules/launcher/Wox.Plugin/Result.cs) list. - -* While parsing, the Sys plugin uses [`FuzzyMatch`](/src/modules/launcher/Wox.Infrastructure/StringMatcher.cs) to get characters matching a result in the list. - -### Score - -* [`CalculateSearchScore`](/src/modules/launcher/Wox.Infrastructure/StringMatcher.cs) A match found near the beginning of a string is scored more than a match found near the end. A match is scored more if the characters in the patterns are closer to each other, while the score is lower if they are more spread out. diff --git a/doc/devdocs/modules/launcher/plugins/system.md b/doc/devdocs/modules/launcher/plugins/system.md new file mode 100644 index 0000000000..16dd201621 --- /dev/null +++ b/doc/devdocs/modules/launcher/plugins/system.md @@ -0,0 +1,46 @@ +# Windows System Commands Plugin + +As the name suggests, the Windows System Commands Plugin is used to directly run Windows system commands that have been entered by the user as a query. This is done by parsing the entry and validating the command, followed by executing it. + +The user can change the behavior of the plugin (language, confirmation dialog, ...) with optional plugin settings. + +![Image of System Commands plugin](/doc/images/launcher/plugins/sys.gif) + +Available commands: +* Shutdown +* Restart +* Sign Out +* Lock +* Sleep +* Hibernate +* Empty Recycle Bin +* UEFI Firmware Settings (Only available on systems, that boot in UEFI mode.) + +## Optional plugin settings + +* We have the following settings that the user can configure to change the behavior of the plugin: + + | Key | Default value | Name/Description | + |--------------|-----------|------------| + | `ConfirmSystemCommands` | `false` | Show a dialog to confirm system commands | + | `LocalizeSystemCommands` | `true` | Use localized system commands instead of English ones | + +* The optional plugin settings are implemented via the [`ISettingProvider`](/src/modules/launcher/Wox.Plugin/ISettingProvider.cs) interface from `Wox.Plugin` project. All available settings for the plugin are defined in the [`Main`](/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.System/Main.cs) class of the plugin. + +## Technical details + +### [`Main`](/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.System/Main.cs) + +* Tries to parse the user input and returns a specific Windows system command by using a [`Result`](/src/modules/launcher/Wox.Plugin/Result.cs) list. + +* While parsing, the plugin uses [`FuzzyMatch`](/src/modules/launcher/Wox.Infrastructure/StringMatcher.cs) to get characters matching a result in the list. + +### UEFI command + +* The UEFI command is only available on systems, that boot in UEFI mode. + +* This is validated by checking the result of the method [`GetSystemFirmwareType`](/src/modules/launcher/Wox.Plugin/Common/Win32/Win32Helpers.cs), which uses the native method [`GetFirmwareType`](/src/modules/launcher/Wox.Plugin/Common/Win32/NativeMethods.cs) in `kernel32.dll`. + +### Score + +* [`CalculateSearchScore`](/src/modules/launcher/Wox.Infrastructure/StringMatcher.cs) A match found near the beginning of a string is scored more than a match found near the end. A match is scored more if the characters in the patterns are closer to each other, while the score is lower if they are more spread out. diff --git a/doc/devdocs/modules/launcher/readme.md b/doc/devdocs/modules/launcher/readme.md index 9ca90366c3..6d22cc6ed9 100644 --- a/doc/devdocs/modules/launcher/readme.md +++ b/doc/devdocs/modules/launcher/readme.md @@ -11,7 +11,7 @@ - [Program](/doc/devdocs/modules/launcher/plugins/program.md) - [Registry](/doc/devdocs/modules/launcher/plugins/registry.md) - [Shell](/doc/devdocs/modules/launcher/plugins/shell.md) - - [Sys](/doc/devdocs/modules/launcher/plugins/sys.md) + - [Windows System Commands](/doc/devdocs/modules/launcher/plugins/system.md) - [Uri](/doc/devdocs/modules/launcher/plugins/uri.md) - [Window Walker](/doc/devdocs/modules/launcher/plugins/windowwalker.md) - [Web Search](/doc/devdocs/modules/launcher/plugins/WebSearch.md) diff --git a/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.System.UnitTests/ImageTests.cs b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.System.UnitTests/ImageTests.cs index 684778fe1e..a0146b8677 100644 --- a/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.System.UnitTests/ImageTests.cs +++ b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.System.UnitTests/ImageTests.cs @@ -27,11 +27,13 @@ namespace Microsoft.PowerToys.Run.Plugin.System.UnitTests [DataRow("sleep", "Images\\sleep.dark.png")] [DataRow("hibernate", "Images\\sleep.dark.png")] [DataRow("empty recycle", "Images\\recyclebin.dark.png")] + [DataRow("uefi firmware settings", "Images\\firmwareSettings.dark.png")] public void IconThemeDarkTest(string typedString, string expectedResult) { // Setup Mock
main = new Mock
(); main.Object.IconTheme = "dark"; + main.Object.IsBootedInUefiMode = true; // Set to true that we can test, regardless of the environment we run on. Query expectedQuery = new Query(typedString); // Act @@ -49,11 +51,13 @@ namespace Microsoft.PowerToys.Run.Plugin.System.UnitTests [DataRow("sleep", "Images\\sleep.light.png")] [DataRow("hibernate", "Images\\sleep.light.png")] [DataRow("empty recycle", "Images\\recyclebin.light.png")] + [DataRow("uefi firmware settings", "Images\\firmwareSettings.light.png")] public void IconThemeLightTest(string typedString, string expectedResult) { // Setup Mock
main = new Mock
(); main.Object.IconTheme = "light"; + main.Object.IsBootedInUefiMode = true; // Set to true that we can test, regardless of the environment we run on. Query expectedQuery = new Query(typedString); // Act diff --git a/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.System.UnitTests/QueryTests.cs b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.System.UnitTests/QueryTests.cs index b5e3c33b09..0cd54e0561 100644 --- a/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.System.UnitTests/QueryTests.cs +++ b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.System.UnitTests/QueryTests.cs @@ -27,7 +27,7 @@ namespace Microsoft.PowerToys.Run.Plugin.System.UnitTests [DataRow("sleep", "Put computer to sleep")] [DataRow("hibernate", "Hibernate computer")] [DataRow("empty recycle", "Empty Recycle Bin")] - public void QueryResults(string typedString, string expectedResult) + public void EnvironmentIndependentQueryResults(string typedString, string expectedResult) { // Setup Mock
main = new Mock
(); @@ -39,5 +39,35 @@ namespace Microsoft.PowerToys.Run.Plugin.System.UnitTests // Assert Assert.AreEqual(expectedResult, result); } + + [TestMethod] + public void UefiCommandIsAvailableOnUefiSystems() + { + // Setup + Mock
main = new Mock
(); + main.Object.IsBootedInUefiMode = true; // Simulate system with UEFI. + Query expectedQuery = new Query("uefi firm"); + + // Act + var result = main.Object.Query(expectedQuery).FirstOrDefault().SubTitle; + + // Assert + Assert.AreEqual("Reboot computer into UEFI Firmware Settings (Requires administrative permissions.)", result); + } + + [TestMethod] + public void UefiCommandIsNotAvailableOnSystemsWithoutUefi() + { + // Setup + Mock
main = new Mock
(); + main.Object.IsBootedInUefiMode = false; // Simulate system without UEFI. + Query expectedQuery = new Query("uefi firm"); + + // Act + var result = main.Object.Query(expectedQuery).FirstOrDefault(); + + // Assert + Assert.IsNull(result); + } } } diff --git a/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.System/Main.cs b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.System/Main.cs index b1139d4374..a2a50c2713 100644 --- a/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.System/Main.cs +++ b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.System/Main.cs @@ -34,6 +34,8 @@ namespace Microsoft.PowerToys.Run.Plugin.System public string IconTheme { get; set; } + public bool IsBootedInUefiMode { get; set; } + public ICommand Command { get; set; } public string Name => Resources.Microsoft_plugin_sys_plugin_name; @@ -43,8 +45,6 @@ namespace Microsoft.PowerToys.Run.Plugin.System private bool _confirmSystemCommands; private bool _localizeSystemCommands; - private bool isBootedInUefiMode = NativeMethods.GetSystemFirmwareType() == NativeMethods.FirmwareTypes.Uefi; - public IEnumerable AdditionalOptions => new List() { new PluginAdditionalOption() @@ -66,10 +66,11 @@ namespace Microsoft.PowerToys.Run.Plugin.System _context = context; _context.API.ThemeChanged += OnThemeChanged; UpdateIconTheme(_context.API.GetCurrentTheme()); + IsBootedInUefiMode = NativeMethods.GetSystemFirmwareType() == NativeMethods.FirmwareTypes.Uefi; // Log info if the system hasn't boot in uefi mode. // (Because this is only going into the log we can ignore the fact that normally UEFI and BIOS are written upper case. No need to convert the enumeration value to upper case.) - if (!isBootedInUefiMode) + if (!IsBootedInUefiMode) { Wox.Plugin.Logger.Log.Info($"The UEFI command will not show to the user. The system has not booted in UEFI mode or the system does not have an UEFI firmware! (Detected type: {NativeMethods.GetSystemFirmwareType()})", typeof(Main)); } @@ -196,7 +197,7 @@ namespace Microsoft.PowerToys.Run.Plugin.System }); // UEFI command/result. It is only available on systems booted in UEFI mode. - if (isBootedInUefiMode) + if (IsBootedInUefiMode) { results.Add(new Result {