MockAppCache.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.Threading.Tasks;
  8  using Microsoft.CmdPal.Ext.Apps.Programs;
  9  
 10  namespace Microsoft.CmdPal.Ext.Apps.UnitTests;
 11  
 12  /// <summary>
 13  /// Mock implementation of IAppCache for unit testing.
 14  /// </summary>
 15  public class MockAppCache : IAppCache
 16  {
 17      private readonly List<Win32Program> _win32s = new();
 18      private readonly List<IUWPApplication> _uwps = new();
 19      private bool _disposed;
 20      private bool _shouldReload;
 21  
 22      /// <summary>
 23      /// Gets the collection of Win32 programs.
 24      /// </summary>
 25      public IList<Win32Program> Win32s => _win32s.AsReadOnly();
 26  
 27      /// <summary>
 28      /// Gets the collection of UWP applications.
 29      /// </summary>
 30      public IList<IUWPApplication> UWPs => _uwps.AsReadOnly();
 31  
 32      /// <summary>
 33      /// Determines whether the cache should be reloaded.
 34      /// </summary>
 35      /// <returns>True if cache should be reloaded, false otherwise.</returns>
 36      public bool ShouldReload() => _shouldReload;
 37  
 38      /// <summary>
 39      /// Resets the reload flag.
 40      /// </summary>
 41      public void ResetReloadFlag() => _shouldReload = false;
 42  
 43      /// <summary>
 44      /// Asynchronously refreshes the cache.
 45      /// </summary>
 46      /// <returns>A task representing the asynchronous refresh operation.</returns>
 47      public async Task RefreshAsync()
 48      {
 49          // Simulate minimal async operation for testing
 50          await Task.Delay(1);
 51      }
 52  
 53      /// <summary>
 54      /// Adds a Win32 program to the cache.
 55      /// </summary>
 56      /// <param name="program">The Win32 program to add.</param>
 57      /// <exception cref="ArgumentNullException">Thrown when program is null.</exception>
 58      public void AddWin32Program(Win32Program program)
 59      {
 60          ArgumentNullException.ThrowIfNull(program);
 61  
 62          _win32s.Add(program);
 63      }
 64  
 65      /// <summary>
 66      /// Adds a UWP application to the cache.
 67      /// </summary>
 68      /// <param name="app">The UWP application to add.</param>
 69      /// <exception cref="ArgumentNullException">Thrown when app is null.</exception>
 70      public void AddUWPApplication(IUWPApplication app)
 71      {
 72          ArgumentNullException.ThrowIfNull(app);
 73  
 74          _uwps.Add(app);
 75      }
 76  
 77      /// <summary>
 78      /// Clears all applications from the cache.
 79      /// </summary>
 80      public void ClearAll()
 81      {
 82          _win32s.Clear();
 83          _uwps.Clear();
 84      }
 85  
 86      /// <summary>
 87      /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
 88      /// </summary>
 89      public void Dispose()
 90      {
 91          Dispose(disposing: true);
 92          GC.SuppressFinalize(this);
 93      }
 94  
 95      /// <summary>
 96      /// Releases unmanaged and - optionally - managed resources.
 97      /// </summary>
 98      /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
 99      protected virtual void Dispose(bool disposing)
100      {
101          if (!_disposed)
102          {
103              if (disposing)
104              {
105                  // Clean up managed resources
106                  _win32s.Clear();
107                  _uwps.Clear();
108              }
109  
110              _disposed = true;
111          }
112      }
113  }