AppsTestBase.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.Threading.Tasks; 6 using Microsoft.VisualStudio.TestTools.UnitTesting; 7 8 namespace Microsoft.CmdPal.Ext.Apps.UnitTests; 9 10 /// <summary> 11 /// Base class for Apps unit tests that provides common setup and teardown functionality. 12 /// </summary> 13 public abstract class AppsTestBase 14 { 15 /// <summary> 16 /// Gets the mock application cache used in tests. 17 /// </summary> 18 protected MockAppCache MockCache { get; private set; } = null!; 19 20 /// <summary> 21 /// Gets the AllAppsPage instance used in tests. 22 /// </summary> 23 protected AllAppsPage Page { get; private set; } = null!; 24 25 /// <summary> 26 /// Sets up the test environment before each test method. 27 /// </summary> 28 /// <returns>A task representing the asynchronous setup operation.</returns> 29 [TestInitialize] 30 public virtual async Task Setup() 31 { 32 MockCache = new MockAppCache(); 33 Page = new AllAppsPage(MockCache); 34 35 // Ensure initialization is complete 36 await MockCache.RefreshAsync(); 37 } 38 39 /// <summary> 40 /// Cleans up the test environment after each test method. 41 /// </summary> 42 [TestCleanup] 43 public virtual void Cleanup() 44 { 45 MockCache?.Dispose(); 46 } 47 48 /// <summary> 49 /// Forces synchronous initialization of the page for testing. 50 /// </summary> 51 protected void EnsurePageInitialized() 52 { 53 // Trigger BuildListItems by accessing items 54 _ = Page.GetItems(); 55 } 56 57 /// <summary> 58 /// Waits for page initialization with timeout. 59 /// </summary> 60 /// <param name="timeoutMs">The timeout in milliseconds.</param> 61 /// <returns>A task representing the asynchronous wait operation.</returns> 62 protected async Task WaitForPageInitializationAsync(int timeoutMs = 1000) 63 { 64 await MockCache.RefreshAsync(); 65 EnsurePageInitialized(); 66 } 67 }