/ src / modules / Workspaces / WorkspacesEditorUITest / WorkspacesSettingsTests.cs
WorkspacesSettingsTests.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 Microsoft.PowerToys.UITest;
  6  using Microsoft.VisualStudio.TestTools.UnitTesting;
  7  
  8  namespace WorkspacesEditorUITest;
  9  
 10  [TestClass]
 11  public class WorkspacesSettingsTests : UITestBase
 12  {
 13      public WorkspacesSettingsTests()
 14          : base(PowerToysModule.PowerToysSettings, WindowSize.Medium)
 15      {
 16      }
 17  
 18      [TestMethod("WorkspacesSettings.LaunchFromSettings")]
 19      [TestCategory("Workspaces Settings UI")]
 20      public void TestLaunchEditorFromSettingsPage()
 21      {
 22          GoToSettingsPageAndEnable();
 23      }
 24  
 25      [TestMethod("WorkspacesSettings.ActivationShortcut")]
 26      [TestCategory("Workspaces Settings UI")]
 27      public void TestActivationShortcutCustomization()
 28      {
 29          GoToSettingsPageAndEnable();
 30  
 31          // Find the activation shortcut control
 32          var shortcutButton = Find<Button>("Activation shortcut");
 33          Assert.IsNotNull(shortcutButton, "Activation shortcut control should exist");
 34  
 35          // Test customizing the shortcut
 36          shortcutButton.Click();
 37  
 38          Task.Delay(1000).Wait();
 39  
 40          // Send new key combination (Win+Ctrl+W)
 41          SendKeys(Key.Win, Key.Ctrl, Key.W);
 42  
 43          var saveButton = Find<Button>("Save");
 44  
 45          Assert.IsNotNull(saveButton, "Save button should exist after editing shortcut");
 46  
 47          saveButton.Click();
 48  
 49          var helpText = shortcutButton.HelpText;
 50          Assert.AreEqual("Win + Ctrl + W", helpText, "Activation shortcut should be updated to Win + Ctrl + W");
 51      }
 52  
 53      [TestMethod("WorkspacesSettings.EnableToggle")]
 54      [TestCategory("Workspaces Settings UI")]
 55      public void TestEnableDisableModule()
 56      {
 57          GoToSettingsPageAndEnable();
 58  
 59          // Find the enable toggle
 60          var enableToggle = Find<ToggleSwitch>("Workspaces");
 61          Assert.IsNotNull(enableToggle, "Enable Workspaces toggle should exist");
 62  
 63          Assert.IsTrue(enableToggle.IsOn, "Enable Workspaces toggle should be in the 'on' state");
 64  
 65          // Toggle the state
 66          enableToggle.Click();
 67          Task.Delay(500).Wait();
 68  
 69          // Verify state changed
 70          Assert.IsFalse(enableToggle.IsOn, "Toggle state should change");
 71  
 72          // Verify related controls are enabled/disabled accordingly
 73          var launchButton = Find<Button>("Launch editor");
 74          Assert.IsFalse(launchButton.Enabled, "Launch editor button should be disabled when module is disabled");
 75      }
 76  
 77      [TestMethod("WorkspacesSettings.LaunchByActivationShortcut")]
 78      [TestCategory("Workspaces Settings UI")]
 79      [Ignore("Wait until settings & runner can be connected in framework")]
 80      public void TestLaunchEditorByActivationShortcut()
 81      {
 82          // Ensure module is enabled
 83          var enableToggle = Find<ToggleSwitch>("Workspaces");
 84          if (!enableToggle.IsOn)
 85          {
 86              enableToggle.Click();
 87              Thread.Sleep(500);
 88          }
 89  
 90          // Close settings window to test shortcut
 91          ExitScopeExe();
 92          Thread.Sleep(1000);
 93  
 94          // Default shortcut is Win+Ctrl+`
 95          SendKeys(Key.Win, Key.Ctrl, Key.W);
 96          Thread.Sleep(2000);
 97  
 98          // Verify editor opened
 99          Assert.IsTrue(WindowHelper.IsWindowOpen("Workspaces Editor"), "Workspaces Editor should open with activation shortcut");
100  
101          // Reopen settings for next tests
102          RestartScopeExe();
103          NavigateToWorkspacesSettings();
104      }
105  
106      [TestMethod("WorkspacesSettings.DisableModuleNoLaunch")]
107      [TestCategory("Workspaces Settings UI")]
108      [Ignore("Wait until settings & runner can be connected in framework")]
109      public void TestDisabledModuleDoesNotLaunchByShortcut()
110      {
111          // Disable the module
112          var enableToggle = Find<ToggleSwitch>("Workspaces");
113          if (enableToggle.IsOn)
114          {
115              enableToggle.Click();
116              Thread.Sleep(500);
117          }
118  
119          // Close settings to test shortcut
120          ExitScopeExe();
121          Thread.Sleep(1000);
122  
123          // Try to launch with shortcut
124          SendKeys(Key.Win, Key.Ctrl, Key.W);
125          Thread.Sleep(2000);
126  
127          // Verify editor did not open
128          Assert.IsFalse(WindowHelper.IsWindowOpen("Workspaces Editor"), "Workspaces Editor should not open when module is disabled");
129  
130          // Reopen settings and re-enable the module
131          RestartScopeExe();
132          NavigateToWorkspacesSettings();
133  
134          enableToggle = Find<ToggleSwitch>("Workspaces");
135          if (!enableToggle.IsOn)
136          {
137              enableToggle.Click();
138              Thread.Sleep(500);
139          }
140      }
141  
142      [TestMethod("WorkspacesSettings.QuickAccessLaunch")]
143      [TestCategory("Workspaces Settings UI")]
144      [Ignore("Wait until tray icon supported is in framework")]
145      public void TestLaunchFromQuickAccess()
146      {
147          // This test verifies the "quick access" mention in settings
148          // Look for any quick access related UI elements
149          var quickAccessInfo = FindAll(By.LinkText("quick access"));
150  
151          if (quickAccessInfo.Count > 0)
152          {
153              Assert.IsTrue(quickAccessInfo.Count > 0, "Quick access information should be present in settings");
154          }
155  
156          // Note: Actual system tray/quick access interaction would require
157          // more complex automation that might be platform-specific
158      }
159  
160      private void NavigateToWorkspacesSettings()
161      {
162          // Find and click Workspaces in the navigation
163          var workspacesNavItem = Find<NavigationViewItem>("Workspaces");
164          workspacesNavItem.Click();
165          Thread.Sleep(1000);
166      }
167  
168      private void GoToSettingsPageAndEnable()
169      {
170          if (this.FindAll<NavigationViewItem>("Workspaces").Count == 0)
171          {
172              this.Find<NavigationViewItem>("Windowing & Layouts").Click();
173          }
174  
175          this.Find<NavigationViewItem>("Workspaces").Click();
176  
177          var enableButton = this.Find<ToggleSwitch>("Workspaces");
178          Assert.IsNotNull(enableButton, "Enable Workspaces toggle should exist");
179  
180          if (!enableButton.IsOn)
181          {
182              enableButton.Click();
183              Task.Delay(500).Wait(); // Wait for the toggle animation and state change
184          }
185  
186          // Verify it's now enabled
187          Assert.IsTrue(enableButton.IsOn, "Enable Workspaces toggle should be in the 'on' state");
188      }
189  
190      private void AttachWorkspacesEditor()
191      {
192          Task.Delay(200).Wait();
193          this.Session.Attach(PowerToysModule.Workspaces);
194      }
195  }