mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 11:48:06 +01:00
## Summary of the Pull Request This PR ensures that the list of recent searches in the Web Search extension is updated immediately after a new item is added or when settings controlling the number of items are changed. - Refactors the Web Search extension history to keep it in memory after being loaded at startup - Adds an event to notify subscribers when the history changes - Implements `IDisposable` to ensure that `WebSearchListPage` unsubscribes from the event - Moves responsibility for creating all list items to single class (`WebSearchListPage`) - Updated unit tests - ## PR Checklist - [x] Closes: #40548 - [ ] **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 - [x] **Localization:** All end-user-facing strings can be localized - [x] **Dev docs:** nothing - [x] **New binaries:** none - [x] **Documentation updated:** nope <!-- 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
142 lines
5.0 KiB
C#
142 lines
5.0 KiB
C#
// 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.Globalization;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.CmdPal.Ext.UnitTestBase;
|
|
using Microsoft.CmdPal.Ext.WebSearch.Commands;
|
|
using Microsoft.CmdPal.Ext.WebSearch.Helpers;
|
|
using Microsoft.CmdPal.Ext.WebSearch.Pages;
|
|
using Microsoft.CommandPalette.Extensions.Toolkit;
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
namespace Microsoft.CmdPal.Ext.WebSearch.UnitTests;
|
|
|
|
[TestClass]
|
|
public class QueryTests : CommandPaletteUnitTestBase
|
|
{
|
|
[TestMethod]
|
|
[DataRow("microsoft")]
|
|
[DataRow("windows")]
|
|
public async Task SearchInWebSearchPage(string query)
|
|
{
|
|
// Setup
|
|
var settings = new MockSettingsInterface();
|
|
|
|
var page = new WebSearchListPage(settings);
|
|
|
|
// Act
|
|
page.UpdateSearchText(string.Empty, query);
|
|
await Task.Delay(1000);
|
|
|
|
var listItem = page.GetItems();
|
|
Assert.IsNotNull(listItem);
|
|
Assert.AreEqual(1, listItem.Length);
|
|
|
|
var expectedItem = listItem.FirstOrDefault();
|
|
|
|
Assert.IsNotNull(expectedItem);
|
|
Assert.IsTrue(expectedItem.Subtitle.Contains("Search the web in"), $"Expected \"search the web in chrome/edge\" but got {expectedItem.Subtitle}");
|
|
Assert.AreEqual(query, expectedItem.Title);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task HistoryReturnsExpectedItems()
|
|
{
|
|
// Setup
|
|
var mockHistoryItems = new List<HistoryItem>
|
|
{
|
|
new HistoryItem("test search", DateTime.Parse("2024-01-01 13:00:00", CultureInfo.CurrentCulture)),
|
|
new HistoryItem("another search", DateTime.Parse("2024-01-02 13:00:00", CultureInfo.CurrentCulture)),
|
|
};
|
|
|
|
var settings = new MockSettingsInterface(mockHistory: mockHistoryItems, historyItemCount: 5);
|
|
|
|
var page = new WebSearchListPage(settings);
|
|
|
|
// Act
|
|
page.UpdateSearchText("abcdef", string.Empty);
|
|
await Task.Delay(1000);
|
|
|
|
var listItem = page.GetItems();
|
|
|
|
// Assert
|
|
Assert.IsNotNull(listItem);
|
|
Assert.AreEqual(2, listItem.Length);
|
|
|
|
foreach (var item in listItem)
|
|
{
|
|
Assert.IsNotNull(item);
|
|
Assert.IsNotEmpty(item.Title);
|
|
Assert.IsNotEmpty(item.Subtitle);
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task HistoryExceedingLimitReturnsMaxItems()
|
|
{
|
|
// Setup
|
|
var mockHistoryItems = new List<HistoryItem>
|
|
{
|
|
new HistoryItem("test search", DateTime.Parse("2024-01-01 13:00:00", CultureInfo.CurrentCulture)),
|
|
new HistoryItem("another search1", DateTime.Parse("2024-01-02 13:00:00", CultureInfo.CurrentCulture)),
|
|
new HistoryItem("another search2", DateTime.Parse("2024-01-03 13:00:00", CultureInfo.CurrentCulture)),
|
|
new HistoryItem("another search3", DateTime.Parse("2024-01-04 13:00:00", CultureInfo.CurrentCulture)),
|
|
new HistoryItem("another search4", DateTime.Parse("2024-01-05 13:00:00", CultureInfo.CurrentCulture)),
|
|
};
|
|
|
|
var settings = new MockSettingsInterface(mockHistory: mockHistoryItems, historyItemCount: 5);
|
|
|
|
var page = new WebSearchListPage(settings);
|
|
|
|
mockHistoryItems.Add(new HistoryItem("another search5", DateTime.Parse("2024-01-06 13:00:00", CultureInfo.CurrentCulture)));
|
|
|
|
// Act
|
|
page.UpdateSearchText("abcdef", string.Empty);
|
|
await Task.Delay(1000);
|
|
|
|
var listItem = page.GetItems();
|
|
|
|
// Assert
|
|
Assert.IsNotNull(listItem);
|
|
|
|
// Make sure only load five item.
|
|
Assert.AreEqual(5, listItem.Length);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task HistoryWhenSetToNoneReturnEmptyList()
|
|
{
|
|
// Setup
|
|
var mockHistoryItems = new List<HistoryItem>
|
|
{
|
|
new HistoryItem("test search", DateTime.Parse("2024-01-01 13:00:00", CultureInfo.CurrentCulture)),
|
|
new HistoryItem("another search1", DateTime.Parse("2024-01-02 13:00:00", CultureInfo.CurrentCulture)),
|
|
new HistoryItem("another search2", DateTime.Parse("2024-01-03 13:00:00", CultureInfo.CurrentCulture)),
|
|
new HistoryItem("another search3", DateTime.Parse("2024-01-04 13:00:00", CultureInfo.CurrentCulture)),
|
|
new HistoryItem("another search4", DateTime.Parse("2024-01-05 13:00:00", CultureInfo.CurrentCulture)),
|
|
new HistoryItem("another search5", DateTime.Parse("2024-01-06 13:00:00", CultureInfo.CurrentCulture)),
|
|
};
|
|
|
|
var settings = new MockSettingsInterface(mockHistory: mockHistoryItems, historyItemCount: 0);
|
|
|
|
var page = new WebSearchListPage(settings);
|
|
|
|
// Act
|
|
page.UpdateSearchText("abcdef", string.Empty);
|
|
await Task.Delay(1000);
|
|
|
|
var listItem = page.GetItems();
|
|
|
|
// Assert
|
|
Assert.IsNotNull(listItem);
|
|
|
|
// Make sure only load five item.
|
|
Assert.AreEqual(0, listItem.Length);
|
|
}
|
|
}
|