/ src / modules / cmdpal / Tests / Microsoft.CmdPal.Ext.UnitTestsBase / CommandPaletteUnitTestBase.cs
CommandPaletteUnitTestBase.cs
1 // Copyright (c) Microsoft Corporation 2 // The Microsoft Corporation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using System; 6 using System.Linq; 7 using System.Threading.Tasks; 8 using Microsoft.CommandPalette.Extensions; 9 using Microsoft.CommandPalette.Extensions.Toolkit; 10 using Windows.Foundation; 11 12 namespace Microsoft.CmdPal.Ext.UnitTestBase; 13 14 public class CommandPaletteUnitTestBase 15 { 16 private bool MatchesFilter(string filter, IListItem item) => 17 FuzzyStringMatcher.ScoreFuzzy(filter, item.Title) > 0 || 18 FuzzyStringMatcher.ScoreFuzzy(filter, item.Subtitle) > 0; 19 20 public IListItem[] Query(string query, IListItem[] candidates) 21 { 22 var listItems = candidates 23 .Where(item => MatchesFilter(query, item)) 24 .ToArray(); 25 26 return listItems; 27 } 28 29 public async Task UpdatePageAndWaitForItems(IDynamicListPage page, Action modification) 30 { 31 // Add an event handler for the ItemsChanged event, 32 // Then call the modification action, 33 // and wait for the event to be raised. 34 var tcs = new TaskCompletionSource<object>(); 35 36 TypedEventHandler<object, IItemsChangedEventArgs> handleItemsChanged = (object s, IItemsChangedEventArgs e) => 37 { 38 tcs.TrySetResult(e); 39 }; 40 41 page.ItemsChanged += handleItemsChanged; 42 modification(); 43 44 await tcs.Task; 45 } 46 }