AllAppsPageTests.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.VisualStudio.TestTools.UnitTesting;
 9  
10  namespace Microsoft.CmdPal.Ext.Apps.UnitTests;
11  
12  [TestClass]
13  public class AllAppsPageTests : AppsTestBase
14  {
15      [TestMethod]
16      public void AllAppsPage_Constructor_ThrowsOnNullAppCache()
17      {
18          // Act & Assert
19          Assert.ThrowsException<ArgumentNullException>(() => new AllAppsPage(null!));
20      }
21  
22      [TestMethod]
23      public void AllAppsPage_WithMockCache_InitializesSuccessfully()
24      {
25          // Arrange
26          var mockCache = new MockAppCache();
27  
28          // Act
29          var page = new AllAppsPage(mockCache);
30  
31          // Assert
32          Assert.IsNotNull(page);
33          Assert.IsNotNull(page.Name);
34          Assert.IsNotNull(page.Icon);
35      }
36  
37      [TestMethod]
38      public async Task AllAppsPage_GetItems_ReturnsEmptyWithEmptyCache()
39      {
40          // Act - Wait for initialization to complete
41          await WaitForPageInitializationAsync();
42          var items = Page.GetItems();
43  
44          // Assert
45          Assert.IsNotNull(items);
46          Assert.AreEqual(0, items.Length);
47      }
48  
49      [TestMethod]
50      public async Task AllAppsPage_GetItems_ReturnsAppsFromCacheAsync()
51      {
52          // Arrange
53          var mockCache = new MockAppCache();
54          var win32App = TestDataHelper.CreateTestWin32Program("Notepad", "C:\\Windows\\System32\\notepad.exe");
55          var uwpApp = TestDataHelper.CreateTestUWPApplication("Calculator");
56  
57          mockCache.AddWin32Program(win32App);
58          mockCache.AddUWPApplication(uwpApp);
59  
60          var page = new AllAppsPage(mockCache);
61  
62          // Wait a bit for initialization to complete
63          await Task.Delay(100);
64  
65          // Act
66          var items = page.GetItems();
67  
68          // Assert
69          Assert.IsNotNull(items);
70          Assert.AreEqual(2, items.Length);
71  
72          // we need to loop the items to ensure we got the correct ones
73          Assert.IsTrue(items.Any(i => i.Title == "Notepad"));
74          Assert.IsTrue(items.Any(i => i.Title == "Calculator"));
75      }
76  
77      [TestMethod]
78      public async Task AllAppsPage_GetPinnedApps_ReturnsEmptyWhenNoAppsArePinned()
79      {
80          // Arrange
81          var mockCache = new MockAppCache();
82          var app = TestDataHelper.CreateTestWin32Program("TestApp", "C:\\TestApp.exe");
83          mockCache.AddWin32Program(app);
84  
85          var page = new AllAppsPage(mockCache);
86  
87          // Wait a bit for initialization to complete
88          await Task.Delay(100);
89  
90          // Act
91          var pinnedApps = page.GetPinnedApps();
92  
93          // Assert
94          Assert.IsNotNull(pinnedApps);
95          Assert.AreEqual(0, pinnedApps.Length);
96      }
97  }