/ src / modules / cmdpal / Tests / Microsoft.CmdPal.Ext.TimeDate.UnitTests / FallbackTimeDateItemTests.cs
FallbackTimeDateItemTests.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.Globalization;
  7  using Microsoft.CmdPal.Ext.TimeDate.Helpers;
  8  using Microsoft.VisualStudio.TestTools.UnitTesting;
  9  
 10  namespace Microsoft.CmdPal.Ext.TimeDate.UnitTests;
 11  
 12  [TestClass]
 13  public class FallbackTimeDateItemTests
 14  {
 15      private CultureInfo originalCulture;
 16      private CultureInfo originalUiCulture;
 17  
 18      [TestInitialize]
 19      public void Setup()
 20      {
 21          // Set culture to 'en-us'
 22          originalCulture = CultureInfo.CurrentCulture;
 23          CultureInfo.CurrentCulture = new CultureInfo("en-us", false);
 24          originalUiCulture = CultureInfo.CurrentUICulture;
 25          CultureInfo.CurrentUICulture = new CultureInfo("en-us", false);
 26      }
 27  
 28      [TestCleanup]
 29      public void Cleanup()
 30      {
 31          // Restore original culture
 32          CultureInfo.CurrentCulture = originalCulture;
 33          CultureInfo.CurrentUICulture = originalUiCulture;
 34      }
 35  
 36      [DataTestMethod]
 37      [DataRow("time", "12:00 PM")]
 38      [DataRow("date", "7/1/2025")]
 39      [DataRow("week", "27")]
 40      public void FallbackQueryTests(string query, string expectedTitle)
 41      {
 42          // Setup
 43          var settingsManager = new Settings();
 44          DateTime now = new DateTime(2025, 7, 1, 12, 0, 0); // Fixed date for testing
 45          var fallbackItem = new FallbackTimeDateItem(settingsManager, now);
 46  
 47          // Act & Assert - Test that UpdateQuery doesn't throw exceptions
 48          try
 49          {
 50              fallbackItem.UpdateQuery(query);
 51              Assert.IsTrue(
 52                  fallbackItem.Title.Contains(expectedTitle, StringComparison.OrdinalIgnoreCase),
 53                  $"Expected title to contain '{expectedTitle}', but got '{fallbackItem.Title}'");
 54              Assert.IsNotNull(fallbackItem.Subtitle, "Subtitle should not be null");
 55              Assert.IsNotNull(fallbackItem.Icon, "Icon should not be null");
 56          }
 57          catch (Exception ex)
 58          {
 59              Assert.Fail($"UpdateQuery should not throw exceptions: {ex.Message}");
 60          }
 61      }
 62  
 63      [DataTestMethod]
 64      [DataRow(null)]
 65      [DataRow("invalid input")]
 66      public void InvalidQueryTests(string query)
 67      {
 68          // Setup
 69          var settingsManager = new Settings();
 70          DateTime now = new DateTime(2025, 7, 1, 12, 0, 0); // Fixed date for testing
 71          var fallbackItem = new FallbackTimeDateItem(settingsManager, now);
 72  
 73          // Act & Assert - Test that UpdateQuery doesn't throw exceptions
 74          try
 75          {
 76              fallbackItem.UpdateQuery(query);
 77  
 78              Assert.AreEqual(string.Empty, fallbackItem.Title, "Title should be empty for invalid queries");
 79              Assert.AreEqual(string.Empty, fallbackItem.Subtitle, "Subtitle should be empty for invalid queries");
 80          }
 81          catch (Exception ex)
 82          {
 83              Assert.Fail($"UpdateQuery should not throw exceptions: {ex.Message}");
 84          }
 85      }
 86  
 87      [DataTestMethod]
 88      public void DisableFallbackItemTest()
 89      {
 90          // Setup
 91          var settingsManager = new Settings(enableFallbackItems: false);
 92          DateTime now = new DateTime(2025, 7, 1, 12, 0, 0); // Fixed date for testing
 93          var fallbackItem = new FallbackTimeDateItem(settingsManager, now);
 94  
 95          // Act & Assert - Test that UpdateQuery doesn't throw exceptions
 96          try
 97          {
 98              fallbackItem.UpdateQuery("now");
 99  
100              Assert.AreEqual(string.Empty, fallbackItem.Title, "Title should be empty when disable fallback item");
101              Assert.AreEqual(string.Empty, fallbackItem.Subtitle, "Subtitle should be empty when disable fallback item");
102          }
103          catch (Exception ex)
104          {
105              Assert.Fail($"UpdateQuery should not throw exceptions: {ex.Message}");
106          }
107      }
108  }