[CmdPal][UT] Refactor some cmdpal ext's ut and improve the test case (#40896)

<!-- Enter a brief description/summary of your PR here. What does it
fix/what does it change/how was it tested (even manually, if necessary)?
-->
## Summary of the Pull Request
1. Remove some AI generated nonsense case
2. Add ISettingsInterface for those ext for testing purpose.
3. Add query test.

<!-- Please review the items on the PR checklist before submitting-->
## PR Checklist

- [x] Closes: #40897
- [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

<!-- Provide a more detailed description of the PR, other things fixed,
or any additional comments/features here -->
## Detailed Description of the Pull Request / Additional comments

<!-- Describe how you validated the behavior. Add automated tests
wherever possible, but list manual validation steps taken as well -->
## Validation Steps Performed

---------

Co-authored-by: Yu Leng <yuleng@microsoft.com>
This commit is contained in:
Yu Leng
2025-08-12 18:27:10 +08:00
committed by GitHub
parent 3682f186e3
commit c23dcb0c5a
41 changed files with 639 additions and 542 deletions

View File

@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation // Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license. // The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information. // See the LICENSE file in the project root for more information.
@@ -6,12 +6,15 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using Microsoft.CmdPal.Ext.Calc.Helper; using Microsoft.CmdPal.Ext.Calc.Helper;
using Microsoft.CmdPal.Ext.UnitTestBase;
using Microsoft.CommandPalette.Extensions;
using Microsoft.CommandPalette.Extensions.Toolkit;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Microsoft.CmdPal.Ext.Calc.UnitTests; namespace Microsoft.CmdPal.Ext.Calc.UnitTests;
[TestClass] [TestClass]
public class ExtendedCalculatorParserTests public class ExtendedCalculatorParserTests : CommandPaletteUnitTestBase
{ {
[DataTestMethod] [DataTestMethod]
[DataRow(null)] [DataRow(null)]
@@ -28,7 +31,7 @@ public class ExtendedCalculatorParserTests
[DataRow("[10,10]")] // '[10,10]' is interpreted as array by mages engine [DataRow("[10,10]")] // '[10,10]' is interpreted as array by mages engine
public void Interpret_NoResult_WhenCalled(string input) public void Interpret_NoResult_WhenCalled(string input)
{ {
var settings = new SettingsManager(); var settings = new Settings();
var result = CalculateEngine.Interpret(settings, input, CultureInfo.CurrentCulture, out _); var result = CalculateEngine.Interpret(settings, input, CultureInfo.CurrentCulture, out _);
@@ -68,7 +71,7 @@ public class ExtendedCalculatorParserTests
[DynamicData(nameof(Interpret_NoErrors_WhenCalledWithRounding_Data))] [DynamicData(nameof(Interpret_NoErrors_WhenCalledWithRounding_Data))]
public void Interpret_NoErrors_WhenCalledWithRounding(string input, decimal expectedResult) public void Interpret_NoErrors_WhenCalledWithRounding(string input, decimal expectedResult)
{ {
var settings = new SettingsManager(); var settings = new Settings();
// Act // Act
// Using InvariantCulture since this is internal // Using InvariantCulture since this is internal
@@ -90,7 +93,7 @@ public class ExtendedCalculatorParserTests
public void Interpret_GreaterPrecision_WhenCalled(string input, decimal expectedResult) public void Interpret_GreaterPrecision_WhenCalled(string input, decimal expectedResult)
{ {
// Arrange // Arrange
var settings = new SettingsManager(); var settings = new Settings();
// Act // Act
// Using InvariantCulture since this is internal // Using InvariantCulture since this is internal
@@ -114,7 +117,7 @@ public class ExtendedCalculatorParserTests
{ {
// Arrange // Arrange
var cultureInfo = CultureInfo.GetCultureInfo(cultureName); var cultureInfo = CultureInfo.GetCultureInfo(cultureName);
var settings = new SettingsManager(); var settings = new Settings();
// Act // Act
var result = CalculateEngine.Interpret(settings, input, cultureInfo, out _); var result = CalculateEngine.Interpret(settings, input, cultureInfo, out _);
@@ -175,7 +178,7 @@ public class ExtendedCalculatorParserTests
public void Interpret_MustReturnResult_WhenResultIsZero(string input) public void Interpret_MustReturnResult_WhenResultIsZero(string input)
{ {
// Arrange // Arrange
var settings = new SettingsManager(); var settings = new Settings();
// Act // Act
// Using InvariantCulture since this is internal // Using InvariantCulture since this is internal
@@ -203,7 +206,7 @@ public class ExtendedCalculatorParserTests
public void Interpret_MustReturnExpectedResult_WhenCalled(string input, decimal expectedResult) public void Interpret_MustReturnExpectedResult_WhenCalled(string input, decimal expectedResult)
{ {
// Arrange // Arrange
var settings = new SettingsManager(); var settings = new Settings();
// Act // Act
// Using en-us culture to have a fixed number style // Using en-us culture to have a fixed number style
@@ -226,7 +229,7 @@ public class ExtendedCalculatorParserTests
{ {
// Arrange // Arrange
var translator = NumberTranslator.Create(new CultureInfo(sourceCultureName, false), new CultureInfo("en-US", false)); var translator = NumberTranslator.Create(new CultureInfo(sourceCultureName, false), new CultureInfo("en-US", false));
var settings = new SettingsManager(); var settings = new Settings();
// Act // Act
// Using en-us culture to have a fixed number style // Using en-us culture to have a fixed number style

View File

@@ -14,5 +14,6 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\ext\Microsoft.CmdPal.Ext.Calc\Microsoft.CmdPal.Ext.Calc.csproj" /> <ProjectReference Include="..\..\ext\Microsoft.CmdPal.Ext.Calc\Microsoft.CmdPal.Ext.Calc.csproj" />
<ProjectReference Include="..\Microsoft.CmdPal.Ext.UnitTestsBase\Microsoft.CmdPal.Ext.UnitTestBase.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -0,0 +1,86 @@
// 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.Linq;
using Microsoft.CmdPal.Ext.Calc.Helper;
using Microsoft.CmdPal.Ext.Calc.Pages;
using Microsoft.CmdPal.Ext.UnitTestBase;
using Microsoft.CommandPalette.Extensions.Toolkit;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Microsoft.CmdPal.Ext.Calc.UnitTests;
[TestClass]
public class QueryTests : CommandPaletteUnitTestBase
{
[DataTestMethod]
[DataRow("2+2", "4")]
[DataRow("5*3", "15")]
[DataRow("10/2", "5")]
[DataRow("sqrt(16)", "4")]
[DataRow("2^3", "8")]
public void TopLevelPageQueryTest(string input, string expectedResult)
{
var settings = new Settings();
var page = new CalculatorListPage(settings);
// Simulate query execution
page.UpdateSearchText(string.Empty, input);
var result = page.GetItems();
Assert.IsTrue(result.Length == 1, "Valid input should always return result");
var firstResult = result.FirstOrDefault();
Assert.IsNotNull(result);
Assert.IsTrue(
firstResult.Title.Contains(expectedResult),
$"Expected result to contain '{expectedResult}' but got '{firstResult.Title}'");
}
[TestMethod]
public void EmptyQueryTest()
{
var settings = new Settings();
var page = new CalculatorListPage(settings);
page.UpdateSearchText("abc", string.Empty);
var results = page.GetItems();
Assert.IsNotNull(results);
var firstItem = results.FirstOrDefault();
Assert.AreEqual("Type an equation...", firstItem.Title);
}
[TestMethod]
public void InvalidExpressionTest()
{
var settings = new Settings();
var page = new CalculatorListPage(settings);
// Simulate query execution
page.UpdateSearchText(string.Empty, "invalid expression");
var result = page.GetItems().FirstOrDefault();
Assert.AreEqual("Type an equation...", result.Title);
}
[DataTestMethod]
[DataRow("sin(60)", "-0.30481", CalculateEngine.TrigMode.Radians)]
[DataRow("sin(60)", "0.866025", CalculateEngine.TrigMode.Degrees)]
[DataRow("sin(60)", "0.809016", CalculateEngine.TrigMode.Gradians)]
public void TrigModeSettingsTest(string input, string expected, CalculateEngine.TrigMode trigMode)
{
var settings = new Settings(trigUnit: trigMode);
var page = new CalculatorListPage(settings);
page.UpdateSearchText(string.Empty, input);
var result = page.GetItems().FirstOrDefault();
Assert.IsNotNull(result);
Assert.IsTrue(result.Title.Contains(expected, System.StringComparison.Ordinal), $"Calc trigMode convert result isn't correct. Current result: {result.Title}");
}
}

View File

@@ -0,0 +1,35 @@
// 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 Microsoft.CmdPal.Ext.Calc.Helper;
namespace Microsoft.CmdPal.Ext.Calc.UnitTests;
public class Settings : ISettingsInterface
{
private readonly CalculateEngine.TrigMode trigUnit;
private readonly bool inputUseEnglishFormat;
private readonly bool outputUseEnglishFormat;
private readonly bool closeOnEnter;
public Settings(
CalculateEngine.TrigMode trigUnit = CalculateEngine.TrigMode.Radians,
bool inputUseEnglishFormat = false,
bool outputUseEnglishFormat = false,
bool closeOnEnter = true)
{
this.trigUnit = trigUnit;
this.inputUseEnglishFormat = inputUseEnglishFormat;
this.outputUseEnglishFormat = outputUseEnglishFormat;
this.closeOnEnter = closeOnEnter;
}
public CalculateEngine.TrigMode TrigUnit => trigUnit;
public bool InputUseEnglishFormat => inputUseEnglishFormat;
public bool OutputUseEnglishFormat => outputUseEnglishFormat;
public bool CloseOnEnter => closeOnEnter;
}

View File

@@ -0,0 +1,55 @@
// 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 Microsoft.CmdPal.Ext.Calc.Helper;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Microsoft.CmdPal.Ext.Calc.UnitTests;
[TestClass]
public class SettingsManagerTests
{
[TestMethod]
public void SettingsManagerInitializationTest()
{
// Act
var settingsManager = new SettingsManager();
// Assert
Assert.IsNotNull(settingsManager);
Assert.IsNotNull(settingsManager.Settings);
}
[TestMethod]
public void SettingsInterfaceTest()
{
// Act
ISettingsInterface settings = new SettingsManager();
// Assert
Assert.IsNotNull(settings);
Assert.IsTrue(settings.TrigUnit == CalculateEngine.TrigMode.Radians);
Assert.IsFalse(settings.InputUseEnglishFormat);
Assert.IsFalse(settings.OutputUseEnglishFormat);
Assert.IsTrue(settings.CloseOnEnter);
}
[TestMethod]
public void MockSettingsTest()
{
// Act
var settings = new Settings(
trigUnit: CalculateEngine.TrigMode.Degrees,
inputUseEnglishFormat: true,
outputUseEnglishFormat: true,
closeOnEnter: false);
// Assert
Assert.IsNotNull(settings);
Assert.AreEqual(CalculateEngine.TrigMode.Degrees, settings.TrigUnit);
Assert.IsTrue(settings.InputUseEnglishFormat);
Assert.IsTrue(settings.OutputUseEnglishFormat);
Assert.IsFalse(settings.CloseOnEnter);
}
}

View File

@@ -18,5 +18,6 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\ext\Microsoft.CmdPal.Ext.Registry\Microsoft.CmdPal.Ext.Registry.csproj" /> <ProjectReference Include="..\..\ext\Microsoft.CmdPal.Ext.Registry\Microsoft.CmdPal.Ext.Registry.csproj" />
<ProjectReference Include="..\Microsoft.CmdPal.Ext.UnitTestsBase\Microsoft.CmdPal.Ext.UnitTestBase.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -0,0 +1,74 @@
// 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.Linq;
using Microsoft.CmdPal.Ext.Registry.Helpers;
using Microsoft.CmdPal.Ext.UnitTestBase;
using Microsoft.CommandPalette.Extensions;
using Microsoft.CommandPalette.Extensions.Toolkit;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Microsoft.CmdPal.Ext.Registry.UnitTests;
[TestClass]
public class QueryTests : CommandPaletteUnitTestBase
{
[DataTestMethod]
[DataRow("HKLM", "HKEY_LOCAL_MACHINE")]
[DataRow("HKCU", "HKEY_CURRENT_USER")]
[DataRow("HKCR", "HKEY_CLASSES_ROOT")]
[DataRow("HKU", "HKEY_USERS")]
[DataRow("HKCC", "HKEY_CURRENT_CONFIG")]
public void TopLevelPageQueryTest(string input, string expectedKeyName)
{
var settings = new Settings();
var page = new RegistryListPage(settings);
var results = page.Query(input);
Assert.IsNotNull(results);
Assert.IsTrue(results.Count > 0, "No items matched the query.");
var firstItem = results.FirstOrDefault();
Assert.IsNotNull(firstItem, "No items matched the query.");
Assert.IsTrue(
firstItem.Title.Contains(expectedKeyName, System.StringComparison.OrdinalIgnoreCase),
$"Expected to match '{expectedKeyName}' but got '{firstItem.Title}'");
}
[TestMethod]
public void EmptyQueryTest()
{
var settings = new Settings();
var page = new RegistryListPage(settings);
var results = page.Query(string.Empty);
Assert.IsNotNull(results);
// Empty query should return all base keys
Assert.IsTrue(results.Count >= 5, "Expected at least 5 base registry keys.");
}
[TestMethod]
public void NullQueryTest()
{
var settings = new Settings();
var page = new RegistryListPage(settings);
var results = page.Query(null);
Assert.IsNotNull(results);
Assert.AreEqual(0, results.Count, "Null query should return empty results.");
}
[TestMethod]
public void InvalidBaseKeyTest()
{
var settings = new Settings();
var page = new RegistryListPage(settings);
var results = page.Query("INVALID_KEY");
Assert.IsNotNull(results);
Assert.AreEqual(0, results.Count, "Invalid query should return empty results.");
}
}

View File

@@ -0,0 +1,15 @@
// 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 Microsoft.CmdPal.Ext.Registry.Helpers;
namespace Microsoft.CmdPal.Ext.Registry.UnitTests;
public class Settings : ISettingsInterface
{
public Settings()
{
// Currently no specific settings for Registry extension
}
}

View File

@@ -142,11 +142,7 @@ public class QueryTests : CommandPaletteUnitTestBase
// UEFI Firmware Settings command should exist // UEFI Firmware Settings command should exist
Assert.IsNotNull(result); Assert.IsNotNull(result);
var firstItem = result.FirstOrDefault(); var firstItem = result.FirstOrDefault();
Assert.IsNotNull(firstItem, "No items matched the query."); var firstItemIsUefiCommand = firstItem?.Title.Contains("UEFI", StringComparison.OrdinalIgnoreCase) ?? false;
var containsFirmwareSettings = firstItem.Title.Contains("UEFI Firmware Settings", StringComparison.OrdinalIgnoreCase); Assert.AreEqual(hasCommand, firstItemIsUefiCommand, $"Expected to match (or not match) 'UEFI Firmware Settings' but got '{firstItem?.Title}'");
Assert.IsTrue(
containsFirmwareSettings == hasCommand,
$"Expected to match 'UEFI Firmware Settings' but got '{firstItem.Title}'");
} }
} }

View File

@@ -367,7 +367,7 @@ public class AvailableResultsListTests
public void UnixTimestampSecondsFormat() public void UnixTimestampSecondsFormat()
{ {
// Setup // Setup
string formatLabel = "Unix epoch time"; var formatLabel = "Unix epoch time";
DateTime timeValue = DateTime.Now.ToUniversalTime(); DateTime timeValue = DateTime.Now.ToUniversalTime();
var settings = new SettingsManager(); var settings = new SettingsManager();
var helperResults = AvailableResultsList.GetList(true, settings, null, null, timeValue); var helperResults = AvailableResultsList.GetList(true, settings, null, null, timeValue);
@@ -384,7 +384,7 @@ public class AvailableResultsListTests
public void UnixTimestampMillisecondsFormat() public void UnixTimestampMillisecondsFormat()
{ {
// Setup // Setup
string formatLabel = "Unix epoch time in milliseconds"; var formatLabel = "Unix epoch time in milliseconds";
DateTime timeValue = DateTime.Now.ToUniversalTime(); DateTime timeValue = DateTime.Now.ToUniversalTime();
var settings = new SettingsManager(); var settings = new SettingsManager();
var helperResults = AvailableResultsList.GetList(true, settings, null, null, timeValue); var helperResults = AvailableResultsList.GetList(true, settings, null, null, timeValue);
@@ -401,7 +401,7 @@ public class AvailableResultsListTests
public void WindowsFileTimeFormat() public void WindowsFileTimeFormat()
{ {
// Setup // Setup
string formatLabel = "Windows file time (Int64 number)"; var formatLabel = "Windows file time (Int64 number)";
DateTime timeValue = DateTime.Now; DateTime timeValue = DateTime.Now;
var settings = new SettingsManager(); var settings = new SettingsManager();
var helperResults = AvailableResultsList.GetList(true, settings, null, null, timeValue); var helperResults = AvailableResultsList.GetList(true, settings, null, null, timeValue);
@@ -418,7 +418,7 @@ public class AvailableResultsListTests
public void ValidateEraResult() public void ValidateEraResult()
{ {
// Setup // Setup
string formatLabel = "Era"; var formatLabel = "Era";
DateTime timeValue = DateTime.Now; DateTime timeValue = DateTime.Now;
var settings = new SettingsManager(); var settings = new SettingsManager();
var helperResults = AvailableResultsList.GetList(true, settings, null, null, timeValue); var helperResults = AvailableResultsList.GetList(true, settings, null, null, timeValue);
@@ -435,7 +435,7 @@ public class AvailableResultsListTests
public void ValidateEraAbbreviationResult() public void ValidateEraAbbreviationResult()
{ {
// Setup // Setup
string formatLabel = "Era abbreviation"; var formatLabel = "Era abbreviation";
DateTime timeValue = DateTime.Now; DateTime timeValue = DateTime.Now;
var settings = new SettingsManager(); var settings = new SettingsManager();
var helperResults = AvailableResultsList.GetList(true, settings, null, null, timeValue); var helperResults = AvailableResultsList.GetList(true, settings, null, null, timeValue);

View File

@@ -1,28 +0,0 @@
// 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 Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Microsoft.CmdPal.Ext.TimeDate.UnitTests;
[TestClass]
public class BasicTests
{
[TestMethod]
public void BasicTest()
{
// This is a basic test to verify the test project can run
Assert.IsTrue(true);
}
[TestMethod]
public void DateTimeTest()
{
// Test basic DateTime functionality
var now = DateTime.Now;
Assert.IsNotNull(now);
Assert.IsTrue(now > DateTime.MinValue);
}
}

View File

@@ -40,7 +40,7 @@ public class FallbackTimeDateItemTests
public void FallbackQueryTests(string query, string expectedTitle) public void FallbackQueryTests(string query, string expectedTitle)
{ {
// Setup // Setup
var settingsManager = new SettingsManager(); var settingsManager = new Settings();
DateTime now = new DateTime(2025, 7, 1, 12, 0, 0); // Fixed date for testing DateTime now = new DateTime(2025, 7, 1, 12, 0, 0); // Fixed date for testing
var fallbackItem = new FallbackTimeDateItem(settingsManager, now); var fallbackItem = new FallbackTimeDateItem(settingsManager, now);
@@ -66,7 +66,7 @@ public class FallbackTimeDateItemTests
public void InvalidQueryTests(string query) public void InvalidQueryTests(string query)
{ {
// Setup // Setup
var settingsManager = new SettingsManager(); var settingsManager = new Settings();
DateTime now = new DateTime(2025, 7, 1, 12, 0, 0); // Fixed date for testing DateTime now = new DateTime(2025, 7, 1, 12, 0, 0); // Fixed date for testing
var fallbackItem = new FallbackTimeDateItem(settingsManager, now); var fallbackItem = new FallbackTimeDateItem(settingsManager, now);
@@ -83,4 +83,26 @@ public class FallbackTimeDateItemTests
Assert.Fail($"UpdateQuery should not throw exceptions: {ex.Message}"); Assert.Fail($"UpdateQuery should not throw exceptions: {ex.Message}");
} }
} }
[DataTestMethod]
public void DisableFallbackItemTest()
{
// Setup
var settingsManager = new Settings(enableFallbackItems: false);
DateTime now = new DateTime(2025, 7, 1, 12, 0, 0); // Fixed date for testing
var fallbackItem = new FallbackTimeDateItem(settingsManager, now);
// Act & Assert - Test that UpdateQuery doesn't throw exceptions
try
{
fallbackItem.UpdateQuery("now");
Assert.AreEqual(string.Empty, fallbackItem.Title, "Title should be empty when disable fallback item");
Assert.AreEqual(string.Empty, fallbackItem.Subtitle, "Subtitle should be empty when disable fallback item");
}
catch (Exception ex)
{
Assert.Fail($"UpdateQuery should not throw exceptions: {ex.Message}");
}
}
} }

View File

@@ -1,127 +0,0 @@
// 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.Globalization;
using Microsoft.CmdPal.Ext.TimeDate.Helpers;
using Microsoft.CommandPalette.Extensions.Toolkit;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Microsoft.CmdPal.Ext.TimeDate.UnitTests;
[TestClass]
public class IconTests
{
private CultureInfo originalCulture;
private CultureInfo originalUiCulture;
[TestInitialize]
public void Setup()
{
// Set culture to 'en-us'
originalCulture = CultureInfo.CurrentCulture;
CultureInfo.CurrentCulture = new CultureInfo("en-us", false);
originalUiCulture = CultureInfo.CurrentUICulture;
CultureInfo.CurrentUICulture = new CultureInfo("en-us", false);
}
[TestCleanup]
public void CleanUp()
{
// Set culture to original value
CultureInfo.CurrentCulture = originalCulture;
CultureInfo.CurrentUICulture = originalUiCulture;
}
[TestMethod]
public void TimeDateCommandsProvider_HasIcon()
{
// Setup
var provider = new TimeDateCommandsProvider();
// Act
var icon = provider.Icon;
// Assert
Assert.IsNotNull(icon, "Provider should have an icon");
}
[TestMethod]
public void TimeDateCommandsProvider_TopLevelCommands_HaveIcons()
{
// Setup
var provider = new TimeDateCommandsProvider();
// Act
var commands = provider.TopLevelCommands();
// Assert
Assert.IsNotNull(commands);
Assert.IsTrue(commands.Length > 0, "Should have at least one top-level command");
foreach (var command in commands)
{
Assert.IsNotNull(command.Icon, "Each command should have an icon");
}
}
[TestMethod]
public void AvailableResults_HaveIcons()
{
// Setup
var settings = new SettingsManager();
// Act
var results = AvailableResultsList.GetList(true, settings);
// Assert
Assert.IsNotNull(results);
Assert.IsTrue(results.Count > 0, "Should have results");
foreach (var result in results)
{
Assert.IsNotNull(result.GetIconInfo(), $"Result '{result.Label}' should have an icon");
}
}
[DataTestMethod]
[DataRow(ResultIconType.Time, "\uE823")]
[DataRow(ResultIconType.Date, "\uE787")]
[DataRow(ResultIconType.DateTime, "\uEC92")]
public void ResultHelper_CreateListItem_PreservesIcon(ResultIconType resultIconType, string expectedIcon)
{
// Setup
var availableResult = new AvailableResult
{
Label = "Test Label",
Value = "Test Value",
IconType = resultIconType,
};
// Act
var listItem = availableResult.ToListItem();
var icon = listItem.Icon;
// Assert
Assert.IsNotNull(listItem);
Assert.IsNotNull(listItem.Icon, "ListItem should preserve the icon from AvailableResult");
Assert.AreEqual(expectedIcon, icon.Dark.Icon, $"Icon for {resultIconType} should match expected value");
}
[TestMethod]
public void Icons_AreNotEmpty()
{
// Setup
var settings = new SettingsManager();
var results = AvailableResultsList.GetList(true, settings);
// Act & Assert
foreach (var result in results)
{
Assert.IsNotNull(result.GetIconInfo(), $"Result '{result.Label}' should have an icon");
Assert.IsFalse(string.IsNullOrWhiteSpace(result.GetIconInfo().ToString()), $"Icon for '{result.Label}' should not be empty");
}
}
}

View File

@@ -19,5 +19,6 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\..\..\common\ManagedCommon\ManagedCommon.csproj" /> <ProjectReference Include="..\..\..\..\common\ManagedCommon\ManagedCommon.csproj" />
<ProjectReference Include="..\..\ext\Microsoft.CmdPal.Ext.TimeDate\Microsoft.CmdPal.Ext.TimeDate.csproj" /> <ProjectReference Include="..\..\ext\Microsoft.CmdPal.Ext.TimeDate\Microsoft.CmdPal.Ext.TimeDate.csproj" />
<ProjectReference Include="..\Microsoft.CmdPal.Ext.UnitTestsBase\Microsoft.CmdPal.Ext.UnitTestBase.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -6,13 +6,14 @@ using System;
using System.Globalization; using System.Globalization;
using System.Linq; using System.Linq;
using Microsoft.CmdPal.Ext.TimeDate.Helpers; using Microsoft.CmdPal.Ext.TimeDate.Helpers;
using Microsoft.CommandPalette.Extensions.Toolkit; using Microsoft.CmdPal.Ext.TimeDate.Pages;
using Microsoft.CmdPal.Ext.UnitTestBase;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Microsoft.CmdPal.Ext.TimeDate.UnitTests; namespace Microsoft.CmdPal.Ext.TimeDate.UnitTests;
[TestClass] [TestClass]
public class QueryTests public class QueryTests : CommandPaletteUnitTestBase
{ {
private CultureInfo originalCulture; private CultureInfo originalCulture;
private CultureInfo originalUiCulture; private CultureInfo originalUiCulture;
@@ -46,7 +47,7 @@ public class QueryTests
public void CountBasicQueries(string query, int expectedMinResultCount) public void CountBasicQueries(string query, int expectedMinResultCount)
{ {
// Setup // Setup
var settings = new SettingsManager(); var settings = new Settings();
// Act // Act
var results = TimeDateCalculator.ExecuteSearch(settings, query); var results = TimeDateCalculator.ExecuteSearch(settings, query);
@@ -58,30 +59,32 @@ public class QueryTests
} }
[DataTestMethod] [DataTestMethod]
[DataRow("time")] [DataRow("time", "time")]
[DataRow("date")] [DataRow("date", "date")]
[DataRow("year")] [DataRow("year", "year")]
[DataRow("now")] [DataRow("now", "now")]
[DataRow("current")] [DataRow("year", "year")]
[DataRow("")] public void BasicQueryTest(string input, string expectedMatchTerm)
[DataRow("now::10:10:10")] // Windows file time
public void AllQueriesReturnResults(string query)
{ {
// Setup var settings = new Settings();
var settings = new SettingsManager(); var page = new TimeDateExtensionPage(settings);
page.UpdateSearchText(string.Empty, input);
var resultLists = page.GetItems();
// Act var result = Query(input, resultLists);
var results = TimeDateCalculator.ExecuteSearch(settings, query);
// Assert Assert.IsNotNull(result);
Assert.IsNotNull(results); Assert.IsTrue(result.Length > 0, "No items matched the query.");
Assert.IsTrue(results.Count > 0, $"Query '{query}' should return at least one result");
var firstItem = result.FirstOrDefault();
Assert.IsNotNull(firstItem, "No items matched the query.");
Assert.IsTrue(
firstItem.Title.Contains(expectedMatchTerm, System.StringComparison.OrdinalIgnoreCase) ||
firstItem.Subtitle.Contains(expectedMatchTerm, System.StringComparison.OrdinalIgnoreCase),
$"Expected to match '{expectedMatchTerm}' in title or subtitle but got '{firstItem.Title}' - '{firstItem.Subtitle}'");
} }
[DataTestMethod] [DataTestMethod]
[DataRow("time", "Time")]
[DataRow("date", "Date")]
[DataRow("now", "Now")]
[DataRow("unix", "Unix epoch time")] [DataRow("unix", "Unix epoch time")]
[DataRow("unix epoch time in milli", "Unix epoch time in milliseconds")] [DataRow("unix epoch time in milli", "Unix epoch time in milliseconds")]
[DataRow("file", "Windows file time (Int64 number)")] [DataRow("file", "Windows file time (Int64 number)")]
@@ -98,12 +101,8 @@ public class QueryTests
[DataRow("month", "Month")] [DataRow("month", "Month")]
[DataRow("month of year", "Month of the year")] [DataRow("month of year", "Month of the year")]
[DataRow("month and d", "Month and day")] [DataRow("month and d", "Month and day")]
[DataRow("month and y", "Month and year")]
[DataRow("year", "Year")] [DataRow("year", "Year")]
[DataRow("era", "Era")]
[DataRow("era a", "Era abbreviation")]
[DataRow("universal", "Universal time format: YYYY-MM-DD hh:mm:ss")] [DataRow("universal", "Universal time format: YYYY-MM-DD hh:mm:ss")]
[DataRow("iso", "ISO 8601")]
[DataRow("rfc", "RFC1123")] [DataRow("rfc", "RFC1123")]
[DataRow("time::12:30", "Time")] [DataRow("time::12:30", "Time")]
[DataRow("date::10.10.2022", "Date")] [DataRow("date::10.10.2022", "Date")]
@@ -114,40 +113,19 @@ public class QueryTests
[DataRow("week num", "Week of the year (Calendar week, Week number)")] [DataRow("week num", "Week of the year (Calendar week, Week number)")]
[DataRow("days in mo", "Days in month")] [DataRow("days in mo", "Days in month")]
[DataRow("Leap y", "Leap year")] [DataRow("Leap y", "Leap year")]
public void CanFindFormatResult(string query, string expectedSubtitle) public void FormatDateQueryTest(string input, string expectedMatchTerm)
{ {
// Setup var settings = new Settings();
var settings = new SettingsManager(); var page = new TimeDateExtensionPage(settings);
page.UpdateSearchText(string.Empty, input);
var resultLists = page.GetItems();
// Act var firstItem = resultLists.FirstOrDefault();
var results = TimeDateCalculator.ExecuteSearch(settings, query); Assert.IsNotNull(firstItem, "No items matched the query.");
Assert.IsTrue(
// Assert firstItem.Title.Contains(expectedMatchTerm, System.StringComparison.OrdinalIgnoreCase) ||
var matchingResult = results.FirstOrDefault(x => x.Subtitle?.StartsWith(expectedSubtitle, StringComparison.CurrentCulture) == true); firstItem.Subtitle.Contains(expectedMatchTerm, System.StringComparison.OrdinalIgnoreCase),
Assert.IsNotNull(matchingResult, $"Could not find result with subtitle starting with '{expectedSubtitle}' for query '{query}'"); $"Expected to match '{expectedMatchTerm}' in title or subtitle but got '{firstItem.Title}' - '{firstItem.Subtitle}'");
}
[DataTestMethod]
[DataRow("12:30", "Time")]
[DataRow("10.10.2022", "Date")]
[DataRow("u1646408119", "Date and time")]
[DataRow("u+1646408119", "Date and time")]
[DataRow("u-1646408119", "Date and time")]
[DataRow("ums1646408119", "Date and time")]
[DataRow("ums+1646408119", "Date and time")]
[DataRow("ums-1646408119", "Date and time")]
[DataRow("ft637820085517321977", "Date and time")]
public void DateTimeNumberOnlyInput(string query, string expectedSubtitle)
{
// Setup
var settings = new SettingsManager();
// Act
var results = TimeDateCalculator.ExecuteSearch(settings, query);
// Assert
var matchingResult = results.FirstOrDefault(x => x.Subtitle?.StartsWith(expectedSubtitle, StringComparison.CurrentCulture) == true);
Assert.IsNotNull(matchingResult, $"Could not find result with subtitle starting with '{expectedSubtitle}' for query '{query}'");
} }
[DataTestMethod] [DataTestMethod]
@@ -157,24 +135,6 @@ public class QueryTests
[DataRow("time:eeee")] [DataRow("time:eeee")]
[DataRow("time::eeee")] [DataRow("time::eeee")]
[DataRow("time//eeee")] [DataRow("time//eeee")]
public void InvalidInputShowsErrorResults(string query)
{
// Setup
var settings = new SettingsManager();
// Act
var results = TimeDateCalculator.ExecuteSearch(settings, query);
// Assert
Assert.IsNotNull(results, $"Results should not be null for query '{query}'");
Assert.IsTrue(results.Count > 0, $"Query '{query}' should return at least one result");
// For invalid input, cmdpal returns an error result
var hasErrorResult = results.Any(r => r.Title?.StartsWith("Error: Invalid input", StringComparison.CurrentCulture) == true);
Assert.IsTrue(hasErrorResult, $"Query '{query}' should return an error result for invalid input");
}
[DataTestMethod]
[DataRow("ug1646408119")] // Invalid prefix [DataRow("ug1646408119")] // Invalid prefix
[DataRow("u9999999999999")] // Unix number + prefix is longer than 12 characters [DataRow("u9999999999999")] // Unix number + prefix is longer than 12 characters
[DataRow("ums999999999999999")] // Unix number in milliseconds + prefix is longer than 17 characters [DataRow("ums999999999999999")] // Unix number in milliseconds + prefix is longer than 17 characters
@@ -194,116 +154,33 @@ public class QueryTests
[DataRow("10.aa.22")] [DataRow("10.aa.22")]
[DataRow("12::55")] [DataRow("12::55")]
[DataRow("12:aa:55")] [DataRow("12:aa:55")]
public void InvalidNumberInputShowsErrorMessage(string query) public void InvalidInputShowsErrorResults(string query)
{ {
// Setup var settings = new Settings();
var settings = new SettingsManager(); var page = new TimeDateExtensionPage(settings);
page.UpdateSearchText(string.Empty, query);
// Act var results = page.GetItems();
var results = TimeDateCalculator.ExecuteSearch(settings, query);
// Assert // Assert
Assert.IsNotNull(results, $"Results should not be null for query '{query}'"); Assert.IsNotNull(results, $"Results should not be null for query '{query}'");
Assert.IsTrue(results.Count > 0, $"Should return at least one result (error message) for invalid query '{query}'"); Assert.IsTrue(results.Length > 0, $"Query '{query}' should return at least one result");
// Check if we get an error result var firstItem = results.FirstOrDefault();
var errorResult = results.FirstOrDefault(r => r.Title?.StartsWith("Error: Invalid input", StringComparison.CurrentCulture) == true); Assert.IsTrue(firstItem.Title.StartsWith("Error: Invalid input", StringComparison.CurrentCulture), $"Query '{query}' should return an error result for invalid input");
Assert.IsNotNull(errorResult, $"Should return an error result for invalid query '{query}'");
} }
[DataTestMethod] [DataTestMethod]
[DataRow("10.10aa")] // Input contains <Number>.<Number> (Can be part of a date.) [DataRow("")]
[DataRow("10:10aa")] // Input contains <Number>:<Number> (Can be part of a time.) [DataRow(null)]
[DataRow("10/10aa")] // Input contains <Number>/<Number> (Can be part of a date.) public void EmptyQueryReturnsAllResults(string input)
public void InvalidInputNotShowsErrorMessage(string query)
{ {
// Setup var settings = new Settings();
var settings = new SettingsManager(); var page = new TimeDateExtensionPage(settings);
page.UpdateSearchText("abc", input);
// Act var results = page.GetItems();
var results = TimeDateCalculator.ExecuteSearch(settings, query);
// Assert // Assert
Assert.IsNotNull(results, $"Results should not be null for query '{query}'"); Assert.IsTrue(results.Length > 0, $"Empty query should return results");
// These queries are ambiguous and cmdpal returns an error for them
// This test might need to be adjusted based on actual cmdpal behavior
if (results.Count > 0)
{
var hasErrorResult = results.Any(r => r.Title?.StartsWith("Error: Invalid input", StringComparison.CurrentCulture) == true);
// For these ambiguous inputs, cmdpal may return error results, which is acceptable
// We just verify that the system handles them gracefully (doesn't crash)
Assert.IsTrue(true, $"Query '{query}' handled gracefully");
}
}
[DataTestMethod]
[DataRow("time", "time", true)] // Full word match should work
[DataRow("date", "date", true)] // Full word match should work
[DataRow("now", "now", true)] // Full word match should work
[DataRow("year", "year", true)] // Full word match should work
[DataRow("abcdefg", "", false)] // Invalid query should return error
public void ValidateBehaviorOnSearchQueries(string query, string expectedMatchTerm, bool shouldHaveValidResults)
{
// Setup
var settings = new SettingsManager();
// Act
var results = TimeDateCalculator.ExecuteSearch(settings, query);
// Assert
Assert.IsNotNull(results, $"Results should not be null for query '{query}'");
Assert.IsTrue(results.Count > 0, $"Query '{query}' should return at least one result");
if (shouldHaveValidResults)
{
// Should have non-error results
var hasValidResult = results.Any(r => !r.Title?.StartsWith("Error: Invalid input", StringComparison.CurrentCulture) == true);
Assert.IsTrue(hasValidResult, $"Query '{query}' should return valid (non-error) results");
if (!string.IsNullOrEmpty(expectedMatchTerm))
{
var hasMatchingResult = results.Any(r =>
r.Title?.Contains(expectedMatchTerm, StringComparison.CurrentCultureIgnoreCase) == true ||
r.Subtitle?.Contains(expectedMatchTerm, StringComparison.CurrentCultureIgnoreCase) == true);
Assert.IsTrue(hasMatchingResult, $"Query '{query}' should return results containing '{expectedMatchTerm}'");
}
}
else
{
// Should have error results
var hasErrorResult = results.Any(r => r.Title?.StartsWith("Error: Invalid input", StringComparison.CurrentCulture) == true);
Assert.IsTrue(hasErrorResult, $"Query '{query}' should return error results for invalid input");
}
}
[TestMethod]
public void EmptyQueryReturnsAllResults()
{
// Setup
var settings = new SettingsManager();
// Act
var results = TimeDateCalculator.ExecuteSearch(settings, string.Empty);
// Assert
Assert.IsNotNull(results);
Assert.IsTrue(results.Count > 0, "Empty query should return all available results");
}
[TestMethod]
public void NullQueryReturnsAllResults()
{
// Setup
var settings = new SettingsManager();
// Act
var results = TimeDateCalculator.ExecuteSearch(settings, null);
// Assert
Assert.IsNotNull(results);
Assert.IsTrue(results.Count > 0, "Null query should return all available results");
} }
[DataTestMethod] [DataTestMethod]
@@ -312,39 +189,34 @@ public class QueryTests
[DataRow("iso utc", "ISO 8601 UTC")] [DataRow("iso utc", "ISO 8601 UTC")]
[DataRow("iso zone", "ISO 8601 with time zone")] [DataRow("iso zone", "ISO 8601 with time zone")]
[DataRow("iso utc zone", "ISO 8601 UTC with time zone")] [DataRow("iso utc zone", "ISO 8601 UTC with time zone")]
public void UTCRelatedQueries(string query, string expectedSubtitle) public void TimeZoneQuery(string query, string expectedSubtitle)
{ {
// Setup var settings = new Settings();
var settings = new SettingsManager(); var page = new TimeDateExtensionPage(settings);
page.UpdateSearchText(string.Empty, query);
// Act var resultsList = page.GetItems();
var results = TimeDateCalculator.ExecuteSearch(settings, query); var results = Query(query, resultsList);
// Assert // Assert
Assert.IsNotNull(results); Assert.IsNotNull(results);
Assert.IsTrue(results.Count > 0, $"Query '{query}' should return results"); var firstResult = results.FirstOrDefault();
Assert.IsTrue(firstResult.Subtitle.StartsWith(expectedSubtitle, StringComparison.CurrentCulture), $"Could not find result with subtitle starting with '{expectedSubtitle}' for query '{query}'");
var matchingResult = results.FirstOrDefault(x => x.Subtitle?.StartsWith(expectedSubtitle, StringComparison.CurrentCulture) == true);
Assert.IsNotNull(matchingResult, $"Could not find result with subtitle starting with '{expectedSubtitle}' for query '{query}'");
} }
[DataTestMethod] [DataTestMethod]
[DataRow("time::12:30:45")] [DataRow("time::12:30:45", "12:30 PM")]
[DataRow("date::2023-12-25")] [DataRow("date::2023-12-25", "12/25/2023")]
[DataRow("now::u1646408119")] [DataRow("now::u1646408119", "132908817190000000")]
[DataRow("current::ft637820085517321977")] public void DelimiterQueriesReturnResults(string query, string expectedResult)
public void DelimiterQueriesReturnResults(string query)
{ {
// Setup var settings = new Settings();
var settings = new SettingsManager(); var page = new TimeDateExtensionPage(settings);
page.UpdateSearchText(string.Empty, query);
// Act var resultsList = page.GetItems();
var results = TimeDateCalculator.ExecuteSearch(settings, query);
// Assert // Assert
Assert.IsNotNull(results); Assert.IsNotNull(resultsList);
var firstResult = resultsList.FirstOrDefault();
// Delimiter queries should return results even if parsing fails (error results) Assert.IsTrue(firstResult.Title.Contains(expectedResult, StringComparison.CurrentCulture), $"Delimiter query '{query}' result not match {expectedResult} current result {firstResult.Title}");
Assert.IsTrue(results.Count > 0, $"Delimiter query '{query}' should return at least one result");
} }
} }

View File

@@ -0,0 +1,46 @@
// 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.Collections.Generic;
using Microsoft.CmdPal.Ext.TimeDate.Helpers;
namespace Microsoft.CmdPal.Ext.TimeDate.UnitTests;
public class Settings : ISettingsInterface
{
private readonly int firstWeekOfYear;
private readonly int firstDayOfWeek;
private readonly bool enableFallbackItems;
private readonly bool timeWithSecond;
private readonly bool dateWithWeekday;
private readonly List<string> customFormats;
public Settings(
int firstWeekOfYear = -1,
int firstDayOfWeek = -1,
bool enableFallbackItems = true,
bool timeWithSecond = false,
bool dateWithWeekday = false,
List<string> customFormats = null)
{
this.firstWeekOfYear = firstWeekOfYear;
this.firstDayOfWeek = firstDayOfWeek;
this.enableFallbackItems = enableFallbackItems;
this.timeWithSecond = timeWithSecond;
this.dateWithWeekday = dateWithWeekday;
this.customFormats = customFormats ?? new List<string>();
}
public int FirstWeekOfYear => firstWeekOfYear;
public int FirstDayOfWeek => firstDayOfWeek;
public bool EnableFallbackItems => enableFallbackItems;
public bool TimeWithSecond => timeWithSecond;
public bool DateWithWeekday => dateWithWeekday;
public List<string> CustomFormats => customFormats;
}

View File

@@ -1,85 +0,0 @@
// 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.Globalization;
using Microsoft.CmdPal.Ext.TimeDate.Helpers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Microsoft.CmdPal.Ext.TimeDate.UnitTests;
[TestClass]
public class SettingsManagerTests
{
private CultureInfo originalCulture;
private CultureInfo originalUiCulture;
[TestInitialize]
public void Setup()
{
// Set culture to 'en-us'
originalCulture = CultureInfo.CurrentCulture;
CultureInfo.CurrentCulture = new CultureInfo("en-us", false);
originalUiCulture = CultureInfo.CurrentUICulture;
CultureInfo.CurrentUICulture = new CultureInfo("en-us", false);
}
[TestCleanup]
public void Cleanup()
{
// Restore original culture
CultureInfo.CurrentCulture = originalCulture;
CultureInfo.CurrentUICulture = originalUiCulture;
}
[TestMethod]
public void SettingsManagerInitializationTest()
{
// Act
var settingsManager = new SettingsManager();
// Assert
Assert.IsNotNull(settingsManager);
Assert.IsNotNull(settingsManager.Settings);
}
[TestMethod]
public void DefaultSettingsValidation()
{
// Act
var settingsManager = new SettingsManager();
// Assert - Check that properties are accessible
var enableFallback = settingsManager.EnableFallbackItems;
var timeWithSecond = settingsManager.TimeWithSecond;
var dateWithWeekday = settingsManager.DateWithWeekday;
var firstWeekOfYear = settingsManager.FirstWeekOfYear;
var firstDayOfWeek = settingsManager.FirstDayOfWeek;
var customFormats = settingsManager.CustomFormats;
Assert.IsNotNull(customFormats);
}
[TestMethod]
public void SettingsPropertiesAccessibilityTest()
{
// Setup
var settingsManager = new SettingsManager();
// Act & Assert - Verify all properties are accessible without exception
try
{
_ = settingsManager.EnableFallbackItems;
_ = settingsManager.TimeWithSecond;
_ = settingsManager.DateWithWeekday;
_ = settingsManager.FirstWeekOfYear;
_ = settingsManager.FirstDayOfWeek;
_ = settingsManager.CustomFormats;
}
catch (Exception ex)
{
Assert.Fail($"Settings properties should be accessible: {ex.Message}");
}
}
}

View File

@@ -20,5 +20,6 @@
<ProjectReference Include="..\..\..\..\common\ManagedCommon\ManagedCommon.csproj" /> <ProjectReference Include="..\..\..\..\common\ManagedCommon\ManagedCommon.csproj" />
<ProjectReference Include="..\..\ext\Microsoft.CmdPal.Ext.WindowWalker\Microsoft.CmdPal.Ext.WindowWalker.csproj" /> <ProjectReference Include="..\..\ext\Microsoft.CmdPal.Ext.WindowWalker\Microsoft.CmdPal.Ext.WindowWalker.csproj" />
<ProjectReference Include="..\..\extensionsdk\Microsoft.CommandPalette.Extensions.Toolkit\Microsoft.CommandPalette.Extensions.Toolkit.csproj" /> <ProjectReference Include="..\..\extensionsdk\Microsoft.CommandPalette.Extensions.Toolkit\Microsoft.CommandPalette.Extensions.Toolkit.csproj" />
<ProjectReference Include="..\Microsoft.CmdPal.Ext.UnitTestsBase\Microsoft.CmdPal.Ext.UnitTestBase.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -1,60 +0,0 @@
// 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.Reflection;
using Microsoft.CmdPal.Ext.WindowWalker.Helpers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Microsoft.CmdPal.Ext.WindowWalker.UnitTests;
[TestClass]
public class PluginSettingsTests
{
[DataTestMethod]
[DataRow("ResultsFromVisibleDesktopOnly")]
[DataRow("SubtitleShowPid")]
[DataRow("SubtitleShowDesktopName")]
[DataRow("ConfirmKillProcess")]
[DataRow("KillProcessTree")]
[DataRow("OpenAfterKillAndClose")]
[DataRow("HideKillProcessOnElevatedProcesses")]
[DataRow("HideExplorerSettingInfo")]
[DataRow("InMruOrder")]
public void DoesSettingExist(string name)
{
// Setup
Type settings = SettingsManager.Instance?.GetType();
// Act
var result = settings?.GetProperty(name, BindingFlags.Public | BindingFlags.Instance);
// Assert
Assert.IsNotNull(result);
}
[DataTestMethod]
[DataRow("ResultsFromVisibleDesktopOnly", false)]
[DataRow("SubtitleShowPid", false)]
[DataRow("SubtitleShowDesktopName", true)]
[DataRow("ConfirmKillProcess", true)]
[DataRow("KillProcessTree", false)]
[DataRow("OpenAfterKillAndClose", false)]
[DataRow("HideKillProcessOnElevatedProcesses", false)]
[DataRow("HideExplorerSettingInfo", true)]
[DataRow("InMruOrder", true)]
public void DefaultValues(string name, bool valueExpected)
{
// Setup
SettingsManager setting = SettingsManager.Instance;
// Act
PropertyInfo propertyInfo = setting?.GetType()?.GetProperty(name, BindingFlags.Public | BindingFlags.Instance);
var result = propertyInfo?.GetValue(setting);
// Assert
Assert.AreEqual(valueExpected, result);
}
}

View File

@@ -0,0 +1,60 @@
// 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 Microsoft.CmdPal.Ext.WindowWalker.Helpers;
namespace Microsoft.CmdPal.Ext.WindowWalker.UnitTests;
public class Settings : ISettingsInterface
{
private readonly bool resultsFromVisibleDesktopOnly;
private readonly bool subtitleShowPid;
private readonly bool subtitleShowDesktopName;
private readonly bool confirmKillProcess;
private readonly bool killProcessTree;
private readonly bool openAfterKillAndClose;
private readonly bool hideKillProcessOnElevatedProcesses;
private readonly bool hideExplorerSettingInfo;
private readonly bool inMruOrder;
public Settings(
bool resultsFromVisibleDesktopOnly = false,
bool subtitleShowPid = false,
bool subtitleShowDesktopName = true,
bool confirmKillProcess = true,
bool killProcessTree = false,
bool openAfterKillAndClose = false,
bool hideKillProcessOnElevatedProcesses = false,
bool hideExplorerSettingInfo = true,
bool inMruOrder = true)
{
this.resultsFromVisibleDesktopOnly = resultsFromVisibleDesktopOnly;
this.subtitleShowPid = subtitleShowPid;
this.subtitleShowDesktopName = subtitleShowDesktopName;
this.confirmKillProcess = confirmKillProcess;
this.killProcessTree = killProcessTree;
this.openAfterKillAndClose = openAfterKillAndClose;
this.hideKillProcessOnElevatedProcesses = hideKillProcessOnElevatedProcesses;
this.hideExplorerSettingInfo = hideExplorerSettingInfo;
this.inMruOrder = inMruOrder;
}
public bool ResultsFromVisibleDesktopOnly => resultsFromVisibleDesktopOnly;
public bool SubtitleShowPid => subtitleShowPid;
public bool SubtitleShowDesktopName => subtitleShowDesktopName;
public bool ConfirmKillProcess => confirmKillProcess;
public bool KillProcessTree => killProcessTree;
public bool OpenAfterKillAndClose => openAfterKillAndClose;
public bool HideKillProcessOnElevatedProcesses => hideKillProcessOnElevatedProcesses;
public bool HideExplorerSettingInfo => hideExplorerSettingInfo;
public bool InMruOrder => inMruOrder;
}

View File

@@ -12,21 +12,21 @@ namespace Microsoft.CmdPal.Ext.Calc;
public partial class CalculatorCommandProvider : CommandProvider public partial class CalculatorCommandProvider : CommandProvider
{ {
private static ISettingsInterface settings = new SettingsManager();
private readonly ListItem _listItem = new(new CalculatorListPage(settings)) private readonly ListItem _listItem = new(new CalculatorListPage(settings))
{ {
Subtitle = Resources.calculator_top_level_subtitle, Subtitle = Resources.calculator_top_level_subtitle,
MoreCommands = [new CommandContextItem(settings.Settings.SettingsPage)], MoreCommands = [new CommandContextItem(((SettingsManager)settings).Settings.SettingsPage)],
}; };
private readonly FallbackCalculatorItem _fallback = new(settings); private readonly FallbackCalculatorItem _fallback = new(settings);
private static SettingsManager settings = new();
public CalculatorCommandProvider() public CalculatorCommandProvider()
{ {
Id = "Calculator"; Id = "Calculator";
DisplayName = Resources.calculator_display_name; DisplayName = Resources.calculator_display_name;
Icon = Icons.CalculatorIcon; Icon = Icons.CalculatorIcon;
Settings = settings.Settings; Settings = ((SettingsManager)settings).Settings;
} }
public override ICommandItem[] TopLevelCommands() => [_listItem]; public override ICommandItem[] TopLevelCommands() => [_listItem];

View File

@@ -34,7 +34,7 @@ public static class CalculateEngine
/// Interpret /// Interpret
/// </summary> /// </summary>
/// <param name="cultureInfo">Use CultureInfo.CurrentCulture if something is user facing</param> /// <param name="cultureInfo">Use CultureInfo.CurrentCulture if something is user facing</param>
public static CalculateResult Interpret(SettingsManager settings, string input, CultureInfo cultureInfo, out string error) public static CalculateResult Interpret(ISettingsInterface settings, string input, CultureInfo cultureInfo, out string error)
{ {
error = default; error = default;

View File

@@ -0,0 +1,18 @@
// 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 Microsoft.CmdPal.Ext.Calc.Helper;
namespace Microsoft.CmdPal.Ext.Calc.Helper;
public interface ISettingsInterface
{
public CalculateEngine.TrigMode TrigUnit { get; }
public bool InputUseEnglishFormat { get; }
public bool OutputUseEnglishFormat { get; }
public bool CloseOnEnter { get; }
}

View File

@@ -12,7 +12,7 @@ namespace Microsoft.CmdPal.Ext.Calc.Helper;
public static partial class QueryHelper public static partial class QueryHelper
{ {
public static ListItem Query(string query, SettingsManager settings, bool isFallbackSearch, TypedEventHandler<object, object> handleSave = null) public static ListItem Query(string query, ISettingsInterface settings, bool isFallbackSearch, TypedEventHandler<object, object> handleSave = null)
{ {
ArgumentNullException.ThrowIfNull(query); ArgumentNullException.ThrowIfNull(query);
if (!isFallbackSearch) if (!isFallbackSearch)

View File

@@ -13,7 +13,7 @@ namespace Microsoft.CmdPal.Ext.Calc.Helper;
public static class ResultHelper public static class ResultHelper
{ {
public static ListItem CreateResult(decimal? roundedResult, CultureInfo inputCulture, CultureInfo outputCulture, string query, SettingsManager settings, TypedEventHandler<object, object> handleSave) public static ListItem CreateResult(decimal? roundedResult, CultureInfo inputCulture, CultureInfo outputCulture, string query, ISettingsInterface settings, TypedEventHandler<object, object> handleSave)
{ {
// Return null when the expression is not a valid calculator query. // Return null when the expression is not a valid calculator query.
if (roundedResult == null) if (roundedResult == null)

View File

@@ -8,7 +8,7 @@ using Microsoft.CommandPalette.Extensions.Toolkit;
namespace Microsoft.CmdPal.Ext.Calc.Helper; namespace Microsoft.CmdPal.Ext.Calc.Helper;
public class SettingsManager : JsonSettingsManager public class SettingsManager : JsonSettingsManager, ISettingsInterface
{ {
private static readonly string _namespace = "calculator"; private static readonly string _namespace = "calculator";

View File

@@ -23,7 +23,7 @@ namespace Microsoft.CmdPal.Ext.Calc.Pages;
public sealed partial class CalculatorListPage : DynamicListPage public sealed partial class CalculatorListPage : DynamicListPage
{ {
private readonly Lock _resultsLock = new(); private readonly Lock _resultsLock = new();
private readonly SettingsManager _settingsManager; private readonly ISettingsInterface _settingsManager;
private readonly List<ListItem> _items = []; private readonly List<ListItem> _items = [];
private readonly List<ListItem> history = []; private readonly List<ListItem> history = [];
private readonly ListItem _emptyItem; private readonly ListItem _emptyItem;
@@ -32,7 +32,7 @@ public sealed partial class CalculatorListPage : DynamicListPage
// We need to avoid the double calculation. This may cause some wierd behaviors. // We need to avoid the double calculation. This may cause some wierd behaviors.
private string skipQuerySearchText = string.Empty; private string skipQuerySearchText = string.Empty;
public CalculatorListPage(SettingsManager settings) public CalculatorListPage(ISettingsInterface settings)
{ {
_settingsManager = settings; _settingsManager = settings;
Icon = Icons.CalculatorIcon; Icon = Icons.CalculatorIcon;

View File

@@ -11,9 +11,9 @@ namespace Microsoft.CmdPal.Ext.Calc.Pages;
public sealed partial class FallbackCalculatorItem : FallbackCommandItem public sealed partial class FallbackCalculatorItem : FallbackCommandItem
{ {
private readonly CopyTextCommand _copyCommand = new(string.Empty); private readonly CopyTextCommand _copyCommand = new(string.Empty);
private readonly SettingsManager _settings; private readonly ISettingsInterface _settings;
public FallbackCalculatorItem(SettingsManager settings) public FallbackCalculatorItem(ISettingsInterface settings)
: base(new NoOpCommand(), Resources.calculator_title) : base(new NoOpCommand(), Resources.calculator_title)
{ {
Command = _copyCommand; Command = _copyCommand;

View File

@@ -0,0 +1,17 @@
// 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.Registry.Helpers;
public interface ISettingsInterface
{
// Add registry-specific settings methods here if needed
// For now, this can be empty if there are no settings for Registry
}

View File

@@ -0,0 +1,37 @@
// 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.IO;
using Microsoft.CommandPalette.Extensions.Toolkit;
namespace Microsoft.CmdPal.Ext.Registry.Helpers;
public class SettingsManager : JsonSettingsManager, ISettingsInterface
{
private static readonly string _namespace = "registry";
private static string Namespaced(string propertyName) => $"{_namespace}.{propertyName}";
internal static string SettingsJsonPath()
{
var directory = Utilities.BaseSettingsPath("Microsoft.CmdPal");
Directory.CreateDirectory(directory);
// now, the state is just next to the exe
return Path.Combine(directory, "settings.json");
}
public SettingsManager()
{
FilePath = SettingsJsonPath();
// Add settings here when needed
// Settings.Add(setting);
// Load settings from file upon initialization
LoadSettings();
Settings.SettingsChanged += (s, a) => this.SaveSettings();
}
}

View File

@@ -18,12 +18,14 @@ internal sealed partial class RegistryListPage : DynamicListPage
public static IconInfo RegistryIcon { get; } = new("\uE74C"); // OEM public static IconInfo RegistryIcon { get; } = new("\uE74C"); // OEM
private readonly CommandItem _emptyMessage; private readonly CommandItem _emptyMessage;
private readonly ISettingsInterface _settingsManager;
public RegistryListPage() public RegistryListPage(ISettingsInterface settingsManager)
{ {
Icon = Icons.RegistryIcon; Icon = Icons.RegistryIcon;
Name = Title = Resources.Registry_Page_Title; Name = Title = Resources.Registry_Page_Title;
Id = "com.microsoft.cmdpal.registry"; Id = "com.microsoft.cmdpal.registry";
_settingsManager = settingsManager;
_emptyMessage = new CommandItem() _emptyMessage = new CommandItem()
{ {
Icon = Icons.RegistryIcon, Icon = Icons.RegistryIcon,

View File

@@ -2,6 +2,7 @@
// The Microsoft Corporation licenses this file to you under the MIT license. // The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information. // See the LICENSE file in the project root for more information.
using Microsoft.CmdPal.Ext.Registry.Helpers;
using Microsoft.CmdPal.Ext.Registry.Properties; using Microsoft.CmdPal.Ext.Registry.Properties;
using Microsoft.CommandPalette.Extensions; using Microsoft.CommandPalette.Extensions;
using Microsoft.CommandPalette.Extensions.Toolkit; using Microsoft.CommandPalette.Extensions.Toolkit;
@@ -10,6 +11,8 @@ namespace Microsoft.CmdPal.Ext.Registry;
public partial class RegistryCommandsProvider : CommandProvider public partial class RegistryCommandsProvider : CommandProvider
{ {
private static readonly ISettingsInterface _settingsManager = new SettingsManager();
public RegistryCommandsProvider() public RegistryCommandsProvider()
{ {
Id = "Windows.Registry"; Id = "Windows.Registry";
@@ -20,7 +23,7 @@ public partial class RegistryCommandsProvider : CommandProvider
public override ICommandItem[] TopLevelCommands() public override ICommandItem[] TopLevelCommands()
{ {
return [ return [
new CommandItem(new RegistryListPage()) new CommandItem(new RegistryListPage(_settingsManager))
{ {
Title = "Registry", Title = "Registry",
Subtitle = "Navigate the Windows registry", Subtitle = "Navigate the Windows registry",

View File

@@ -16,10 +16,10 @@ namespace Microsoft.CmdPal.Ext.TimeDate;
internal sealed partial class FallbackTimeDateItem : FallbackCommandItem internal sealed partial class FallbackTimeDateItem : FallbackCommandItem
{ {
private readonly HashSet<string> _validOptions; private readonly HashSet<string> _validOptions;
private SettingsManager _settingsManager; private ISettingsInterface _settingsManager;
private DateTime? _timestamp; private DateTime? _timestamp;
public FallbackTimeDateItem(SettingsManager settings, DateTime? timestamp = null) public FallbackTimeDateItem(ISettingsInterface settings, DateTime? timestamp = null)
: base(new NoOpCommand(), Resources.Microsoft_plugin_timedate_fallback_display_title) : base(new NoOpCommand(), Resources.Microsoft_plugin_timedate_fallback_display_title)
{ {
Title = string.Empty; Title = string.Empty;

View File

@@ -22,7 +22,7 @@ internal static class AvailableResultsList
/// <param name="firstWeekOfYear">Required for UnitTest: Use custom first week of the year instead of the plugin setting.</param> /// <param name="firstWeekOfYear">Required for UnitTest: Use custom first week of the year instead of the plugin setting.</param>
/// <param name="firstDayOfWeek">Required for UnitTest: Use custom first day of the week instead the plugin setting.</param> /// <param name="firstDayOfWeek">Required for UnitTest: Use custom first day of the week instead the plugin setting.</param>
/// <returns>List of results</returns> /// <returns>List of results</returns>
internal static List<AvailableResult> GetList(bool isKeywordSearch, SettingsManager settings, bool? timeLongFormat = null, bool? dateLongFormat = null, DateTime? timestamp = null, CalendarWeekRule? firstWeekOfYear = null, DayOfWeek? firstDayOfWeek = null) internal static List<AvailableResult> GetList(bool isKeywordSearch, ISettingsInterface settings, bool? timeLongFormat = null, bool? dateLongFormat = null, DateTime? timestamp = null, CalendarWeekRule? firstWeekOfYear = null, DayOfWeek? firstDayOfWeek = null)
{ {
var results = new List<AvailableResult>(); var results = new List<AvailableResult>();
var calendar = CultureInfo.CurrentCulture.Calendar; var calendar = CultureInfo.CurrentCulture.Calendar;

View File

@@ -0,0 +1,26 @@
// 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.TimeDate.Helpers;
public interface ISettingsInterface
{
public int FirstWeekOfYear { get; }
public int FirstDayOfWeek { get; }
public bool EnableFallbackItems { get; }
public bool TimeWithSecond { get; }
public bool DateWithWeekday { get; }
public List<string> CustomFormats { get; }
}

View File

@@ -11,7 +11,7 @@ using Microsoft.CommandPalette.Extensions.Toolkit;
namespace Microsoft.CmdPal.Ext.TimeDate.Helpers; namespace Microsoft.CmdPal.Ext.TimeDate.Helpers;
public class SettingsManager : JsonSettingsManager public class SettingsManager : JsonSettingsManager, ISettingsInterface
{ {
// Line break character used in WinUI3 TextBox and TextBlock. // Line break character used in WinUI3 TextBox and TextBlock.
private const char TEXTBOXNEWLINE = '\r'; private const char TEXTBOXNEWLINE = '\r';

View File

@@ -27,7 +27,7 @@ public sealed partial class TimeDateCalculator
/// </summary> /// </summary>
/// <param name="query">Search query object</param> /// <param name="query">Search query object</param>
/// <returns>List of Wox <see cref="Result"/>s.</returns> /// <returns>List of Wox <see cref="Result"/>s.</returns>
public static List<ListItem> ExecuteSearch(SettingsManager settings, string query) public static List<ListItem> ExecuteSearch(ISettingsInterface settings, string query)
{ {
var isEmptySearchInput = string.IsNullOrWhiteSpace(query); var isEmptySearchInput = string.IsNullOrWhiteSpace(query);
List<AvailableResult> availableFormats = new List<AvailableResult>(); List<AvailableResult> availableFormats = new List<AvailableResult>();

View File

@@ -19,9 +19,9 @@ internal sealed partial class TimeDateExtensionPage : DynamicListPage
private IList<ListItem> _results = new List<ListItem>(); private IList<ListItem> _results = new List<ListItem>();
private bool _dataLoaded; private bool _dataLoaded;
private SettingsManager _settingsManager; private ISettingsInterface _settingsManager;
public TimeDateExtensionPage(SettingsManager settingsManager) public TimeDateExtensionPage(ISettingsInterface settingsManager)
{ {
Icon = Icons.TimeDateExtIcon; Icon = Icons.TimeDateExtIcon;
Title = Resources.Microsoft_plugin_timedate_main_page_title; Title = Resources.Microsoft_plugin_timedate_main_page_title;

View File

@@ -15,7 +15,7 @@ namespace Microsoft.CmdPal.Ext.TimeDate;
public partial class TimeDateCommandsProvider : CommandProvider public partial class TimeDateCommandsProvider : CommandProvider
{ {
private readonly CommandItem _command; private readonly CommandItem _command;
private static readonly SettingsManager _settingsManager = new(); private static readonly SettingsManager _settingsManager = new SettingsManager();
private static readonly CompositeFormat MicrosoftPluginTimedatePluginDescription = System.Text.CompositeFormat.Parse(Resources.Microsoft_plugin_timedate_plugin_description); private static readonly CompositeFormat MicrosoftPluginTimedatePluginDescription = System.Text.CompositeFormat.Parse(Resources.Microsoft_plugin_timedate_plugin_description);
private static readonly TimeDateExtensionPage _timeDateExtensionPage = new(_settingsManager); private static readonly TimeDateExtensionPage _timeDateExtensionPage = new(_settingsManager);
private readonly FallbackTimeDateItem _fallbackTimeDateItem = new(_settingsManager); private readonly FallbackTimeDateItem _fallbackTimeDateItem = new(_settingsManager);

View File

@@ -0,0 +1,26 @@
// 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.
namespace Microsoft.CmdPal.Ext.WindowWalker.Helpers;
public interface ISettingsInterface
{
public bool ResultsFromVisibleDesktopOnly { get; }
public bool SubtitleShowPid { get; }
public bool SubtitleShowDesktopName { get; }
public bool ConfirmKillProcess { get; }
public bool KillProcessTree { get; }
public bool OpenAfterKillAndClose { get; }
public bool HideKillProcessOnElevatedProcesses { get; }
public bool HideExplorerSettingInfo { get; }
public bool InMruOrder { get; }
}

View File

@@ -8,7 +8,7 @@ using Microsoft.CommandPalette.Extensions.Toolkit;
namespace Microsoft.CmdPal.Ext.WindowWalker.Helpers; namespace Microsoft.CmdPal.Ext.WindowWalker.Helpers;
public class SettingsManager : JsonSettingsManager public class SettingsManager : JsonSettingsManager, ISettingsInterface
{ {
private static readonly string _namespace = "windowWalker"; private static readonly string _namespace = "windowWalker";