QueryTests.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.Collections.Generic;
  7  using System.Globalization;
  8  using System.Linq;
  9  using System.Threading.Tasks;
 10  using Microsoft.CmdPal.Ext.UnitTestBase;
 11  using Microsoft.CmdPal.Ext.WebSearch.Commands;
 12  using Microsoft.CmdPal.Ext.WebSearch.Helpers;
 13  using Microsoft.CmdPal.Ext.WebSearch.Pages;
 14  using Microsoft.CommandPalette.Extensions.Toolkit;
 15  using Microsoft.VisualStudio.TestTools.UnitTesting;
 16  
 17  namespace Microsoft.CmdPal.Ext.WebSearch.UnitTests;
 18  
 19  [TestClass]
 20  public class QueryTests : CommandPaletteUnitTestBase
 21  {
 22      [TestMethod]
 23      [DataRow("microsoft")]
 24      [DataRow("windows")]
 25      public async Task SearchInWebSearchPage(string query)
 26      {
 27          // Setup
 28          var settings = new MockSettingsInterface();
 29          var browserInfoService = new MockBrowserInfoService();
 30  
 31          var page = new WebSearchListPage(settings, browserInfoService);
 32  
 33          // Act
 34          page.UpdateSearchText(string.Empty, query);
 35          await Task.Delay(1000);
 36  
 37          var listItem = page.GetItems();
 38          Assert.IsNotNull(listItem);
 39          Assert.AreEqual(1, listItem.Length);
 40  
 41          var expectedItem = listItem.FirstOrDefault();
 42  
 43          Assert.IsNotNull(expectedItem);
 44          Assert.IsTrue(expectedItem.Subtitle.Contains("Search the web in"), $"Expected \"search the web in chrome/edge\" but got {expectedItem.Subtitle}");
 45          Assert.AreEqual(query, expectedItem.Title);
 46      }
 47  
 48      [TestMethod]
 49      public async Task HistoryReturnsExpectedItems()
 50      {
 51          // Setup
 52          var mockHistoryItems = new List<HistoryItem>
 53          {
 54              new HistoryItem("test search", DateTime.Parse("2024-01-01 13:00:00", CultureInfo.CurrentCulture)),
 55              new HistoryItem("another search", DateTime.Parse("2024-01-02 13:00:00", CultureInfo.CurrentCulture)),
 56          };
 57  
 58          var settings = new MockSettingsInterface(mockHistory: mockHistoryItems, historyItemCount: 5);
 59          var browserInfoService = new MockBrowserInfoService();
 60  
 61          var page = new WebSearchListPage(settings, browserInfoService);
 62  
 63          // Act
 64          page.UpdateSearchText("abcdef", string.Empty);
 65          await Task.Delay(1000);
 66  
 67          var listItem = page.GetItems();
 68  
 69          // Assert
 70          Assert.IsNotNull(listItem);
 71          Assert.AreEqual(2, listItem.Length);
 72  
 73          foreach (var item in listItem)
 74          {
 75              Assert.IsNotNull(item);
 76              Assert.IsNotEmpty(item.Title);
 77              Assert.IsNotEmpty(item.Subtitle);
 78          }
 79      }
 80  
 81      [TestMethod]
 82      public async Task HistoryExceedingLimitReturnsMaxItems()
 83      {
 84          // Setup
 85          var mockHistoryItems = new List<HistoryItem>
 86          {
 87              new HistoryItem("test search", DateTime.Parse("2024-01-01 13:00:00", CultureInfo.CurrentCulture)),
 88              new HistoryItem("another search1", DateTime.Parse("2024-01-02 13:00:00", CultureInfo.CurrentCulture)),
 89              new HistoryItem("another search2", DateTime.Parse("2024-01-03 13:00:00", CultureInfo.CurrentCulture)),
 90              new HistoryItem("another search3", DateTime.Parse("2024-01-04 13:00:00", CultureInfo.CurrentCulture)),
 91              new HistoryItem("another search4", DateTime.Parse("2024-01-05 13:00:00", CultureInfo.CurrentCulture)),
 92          };
 93  
 94          var settings = new MockSettingsInterface(mockHistory: mockHistoryItems, historyItemCount: 5);
 95          var browserInfoService = new MockBrowserInfoService();
 96  
 97          var page = new WebSearchListPage(settings, browserInfoService);
 98  
 99          mockHistoryItems.Add(new HistoryItem("another search5", DateTime.Parse("2024-01-06 13:00:00", CultureInfo.CurrentCulture)));
100  
101          // Act
102          page.UpdateSearchText("abcdef", string.Empty);
103          await Task.Delay(1000);
104  
105          var listItem = page.GetItems();
106  
107          // Assert
108          Assert.IsNotNull(listItem);
109  
110          // Make sure only load five item.
111          Assert.AreEqual(5, listItem.Length);
112      }
113  
114      [TestMethod]
115      public async Task HistoryWhenSetToNoneReturnEmptyList()
116      {
117          // Setup
118          var mockHistoryItems = new List<HistoryItem>
119          {
120              new HistoryItem("test search", DateTime.Parse("2024-01-01 13:00:00", CultureInfo.CurrentCulture)),
121              new HistoryItem("another search1", DateTime.Parse("2024-01-02 13:00:00", CultureInfo.CurrentCulture)),
122              new HistoryItem("another search2", DateTime.Parse("2024-01-03 13:00:00", CultureInfo.CurrentCulture)),
123              new HistoryItem("another search3", DateTime.Parse("2024-01-04 13:00:00", CultureInfo.CurrentCulture)),
124              new HistoryItem("another search4", DateTime.Parse("2024-01-05 13:00:00", CultureInfo.CurrentCulture)),
125              new HistoryItem("another search5", DateTime.Parse("2024-01-06 13:00:00", CultureInfo.CurrentCulture)),
126          };
127  
128          var settings = new MockSettingsInterface(mockHistory: mockHistoryItems, historyItemCount: 0);
129          var browserInfoService = new MockBrowserInfoService();
130  
131          var page = new WebSearchListPage(settings, browserInfoService);
132  
133          // Act
134          page.UpdateSearchText("abcdef", string.Empty);
135          await Task.Delay(1000);
136  
137          var listItem = page.GetItems();
138  
139          // Assert
140          Assert.IsNotNull(listItem);
141  
142          // Make sure only load five item.
143          Assert.AreEqual(0, listItem.Length);
144      }
145  }