/ src / modules / fancyzones / FancyZonesEditor.UITests / ApplyLayoutTests.cs
ApplyLayoutTests.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.Collections.Generic;
  6  using System.Globalization;
  7  using FancyZonesEditor.Models;
  8  using FancyZonesEditorCommon.Data;
  9  using Microsoft.FancyZonesEditor.UnitTests.Utils;
 10  using Microsoft.PowerToys.UITest;
 11  using Microsoft.VisualStudio.TestTools.UnitTesting;
 12  using ModernWpf.Controls;
 13  using OpenQA.Selenium;
 14  using static Microsoft.FancyZonesEditor.UnitTests.Utils.FancyZonesEditorHelper;
 15  
 16  namespace Microsoft.FancyZonesEditor.UITests
 17  {
 18      [TestClass]
 19      public class ApplyLayoutTests : UITestBase
 20      {
 21          public ApplyLayoutTests()
 22              : base(PowerToysModule.FancyZone, WindowSize.UnSpecified)
 23          {
 24          }
 25  
 26          private static readonly EditorParameters.ParamsWrapper Parameters = new EditorParameters.ParamsWrapper
 27          {
 28              ProcessId = 1,
 29              SpanZonesAcrossMonitors = false,
 30              Monitors = new List<EditorParameters.NativeMonitorDataWrapper>
 31              {
 32                  new EditorParameters.NativeMonitorDataWrapper
 33                  {
 34                      Monitor = "monitor-1",
 35                      MonitorInstanceId = "instance-id-1",
 36                      MonitorSerialNumber = "serial-number-1",
 37                      MonitorNumber = 1,
 38                      VirtualDesktop = "{FF34D993-73F3-4B8C-AA03-73730A01D6A8}",
 39                      Dpi = 96,
 40                      LeftCoordinate = 0,
 41                      TopCoordinate = 0,
 42                      WorkAreaHeight = 1040,
 43                      WorkAreaWidth = 1920,
 44                      MonitorHeight = 1080,
 45                      MonitorWidth = 1920,
 46                      IsSelected = true,
 47                  },
 48                  new EditorParameters.NativeMonitorDataWrapper
 49                  {
 50                      Monitor = "monitor-2",
 51                      MonitorInstanceId = "instance-id-2",
 52                      MonitorSerialNumber = "serial-number-2",
 53                      MonitorNumber = 2,
 54                      VirtualDesktop = "{FF34D993-73F3-4B8C-AA03-73730A01D6A8}",
 55                      Dpi = 96,
 56                      LeftCoordinate = 1920,
 57                      TopCoordinate = 0,
 58                      WorkAreaHeight = 1040,
 59                      WorkAreaWidth = 1920,
 60                      MonitorHeight = 1080,
 61                      MonitorWidth = 1920,
 62                      IsSelected = false,
 63                  },
 64              },
 65          };
 66  
 67          private static readonly CustomLayouts.CustomLayoutListWrapper CustomLayoutsList = new CustomLayouts.CustomLayoutListWrapper
 68          {
 69              CustomLayouts = new List<CustomLayouts.CustomLayoutWrapper>
 70              {
 71                  new CustomLayouts.CustomLayoutWrapper
 72                  {
 73                      Uuid = "{E7807D0D-6223-4883-B15B-1F3883944C09}",
 74                      Type = CustomLayout.Canvas.TypeToString(),
 75                      Name = "Custom layout",
 76                      Info = new CustomLayouts().ToJsonElement(new CustomLayouts.CanvasInfoWrapper
 77                      {
 78                          RefHeight = 952,
 79                          RefWidth = 1500,
 80                          SensitivityRadius = 10,
 81                          Zones = new List<CustomLayouts.CanvasInfoWrapper.CanvasZoneWrapper> { },
 82                      }),
 83                  },
 84              },
 85          };
 86  
 87          private static readonly LayoutTemplates.TemplateLayoutsListWrapper TemplateLayoutsList = new LayoutTemplates.TemplateLayoutsListWrapper
 88          {
 89              LayoutTemplates = new List<LayoutTemplates.TemplateLayoutWrapper>
 90              {
 91                  new LayoutTemplates.TemplateLayoutWrapper
 92                  {
 93                      Type = LayoutType.Blank.TypeToString(),
 94                  },
 95                  new LayoutTemplates.TemplateLayoutWrapper
 96                  {
 97                      Type = LayoutType.Focus.TypeToString(),
 98                      ZoneCount = 10,
 99                  },
100                  new LayoutTemplates.TemplateLayoutWrapper
101                  {
102                      Type = LayoutType.Rows.TypeToString(),
103                      ZoneCount = 2,
104                      ShowSpacing = true,
105                      Spacing = 10,
106                      SensitivityRadius = 10,
107                  },
108                  new LayoutTemplates.TemplateLayoutWrapper
109                  {
110                      Type = LayoutType.Columns.TypeToString(),
111                      ZoneCount = 2,
112                      ShowSpacing = true,
113                      Spacing = 20,
114                      SensitivityRadius = 20,
115                  },
116                  new LayoutTemplates.TemplateLayoutWrapper
117                  {
118                      Type = LayoutType.Grid.TypeToString(),
119                      ZoneCount = 4,
120                      ShowSpacing = false,
121                      Spacing = 10,
122                      SensitivityRadius = 30,
123                  },
124                  new LayoutTemplates.TemplateLayoutWrapper
125                  {
126                      Type = LayoutType.PriorityGrid.TypeToString(),
127                      ZoneCount = 3,
128                      ShowSpacing = true,
129                      Spacing = 1,
130                      SensitivityRadius = 40,
131                  },
132              },
133          };
134  
135          [TestInitialize]
136          public void TestInitialize()
137          {
138              FancyZonesEditorHelper.Files.Restore();
139              EditorParameters editorParameters = new EditorParameters();
140              FancyZonesEditorHelper.Files.ParamsIOHelper.WriteData(editorParameters.Serialize(Parameters));
141  
142              LayoutTemplates layoutTemplates = new LayoutTemplates();
143              FancyZonesEditorHelper.Files.LayoutTemplatesIOHelper.WriteData(layoutTemplates.Serialize(TemplateLayoutsList));
144  
145              CustomLayouts customLayouts = new CustomLayouts();
146              FancyZonesEditorHelper.Files.CustomLayoutsIOHelper.WriteData(customLayouts.Serialize(CustomLayoutsList));
147  
148              DefaultLayouts defaultLayouts = new DefaultLayouts();
149              DefaultLayouts.DefaultLayoutsListWrapper defaultLayoutsListWrapper = new DefaultLayouts.DefaultLayoutsListWrapper
150              {
151                  DefaultLayouts = new List<DefaultLayouts.DefaultLayoutWrapper>
152                  {
153                      new DefaultLayouts.DefaultLayoutWrapper
154                      {
155                          MonitorConfiguration = MonitorConfigurationType.Horizontal.TypeToString(),
156                          Layout = new DefaultLayouts.DefaultLayoutWrapper.LayoutWrapper
157                          {
158                              Type = LayoutType.Focus.TypeToString(),
159                              ZoneCount = 4,
160                              ShowSpacing = true,
161                              Spacing = 5,
162                              SensitivityRadius = 20,
163                          },
164                      },
165                      new DefaultLayouts.DefaultLayoutWrapper
166                      {
167                          MonitorConfiguration = MonitorConfigurationType.Vertical.TypeToString(),
168                          Layout = new DefaultLayouts.DefaultLayoutWrapper.LayoutWrapper
169                          {
170                              Type = LayoutType.Custom.TypeToString(),
171                              Uuid = "{0D6D2F58-9184-4804-81E4-4E4CC3476DC1}",
172                              ZoneCount = 0,
173                              ShowSpacing = false,
174                              Spacing = 0,
175                              SensitivityRadius = 0,
176                          },
177                      },
178                  },
179              };
180              FancyZonesEditorHelper.Files.DefaultLayoutsIOHelper.WriteData(defaultLayouts.Serialize(defaultLayoutsListWrapper));
181  
182              LayoutHotkeys layoutHotkeys = new LayoutHotkeys();
183              LayoutHotkeys.LayoutHotkeysWrapper layoutHotkeysWrapper = new LayoutHotkeys.LayoutHotkeysWrapper
184              {
185                  LayoutHotkeys = new List<LayoutHotkeys.LayoutHotkeyWrapper> { },
186              };
187              FancyZonesEditorHelper.Files.LayoutHotkeysIOHelper.WriteData(layoutHotkeys.Serialize(layoutHotkeysWrapper));
188  
189              AppliedLayouts appliedLayouts = new AppliedLayouts();
190              AppliedLayouts.AppliedLayoutsListWrapper appliedLayoutsWrapper = new AppliedLayouts.AppliedLayoutsListWrapper
191              {
192                  AppliedLayouts = new List<AppliedLayouts.AppliedLayoutWrapper> { },
193              };
194              FancyZonesEditorHelper.Files.AppliedLayoutsIOHelper.WriteData(appliedLayouts.Serialize(appliedLayoutsWrapper));
195  
196              this.RestartScopeExe();
197          }
198  
199          [TestMethod]
200          public void ApplyCustomLayout()
201          {
202              var layout = CustomLayoutsList.CustomLayouts[0];
203              Assert.IsFalse(Session.Find<Element>(layout.Name).Selected);
204              Session.Find<Element>(layout.Name).Click();
205  
206              Assert.IsTrue(Session.Find<Element>(layout.Name).Selected);
207  
208              AppliedLayouts appliedLayouts = new AppliedLayouts();
209              var data = appliedLayouts.Read(appliedLayouts.File);
210              Assert.AreEqual(Parameters.Monitors.Count, data.AppliedLayouts.Count);
211              Assert.AreEqual(layout.Uuid, data.AppliedLayouts[0].AppliedLayout.Uuid);
212              Assert.AreEqual(Parameters.Monitors[0].MonitorNumber, data.AppliedLayouts[0].Device.MonitorNumber);
213          }
214  
215          [TestMethod]
216          public void ApplyTemplateLayout()
217          {
218              var layoutType = LayoutType.Columns;
219              var layout = TestConstants.TemplateLayoutNames[layoutType];
220              Assert.IsFalse(Session.Find<Element>(layout).Selected);
221              Session.Find<Element>(layout).Click();
222  
223              Assert.IsTrue(Session.Find<Element>(layout).Selected);
224  
225              AppliedLayouts appliedLayouts = new AppliedLayouts();
226              var data = appliedLayouts.Read(appliedLayouts.File);
227              Assert.AreEqual(Parameters.Monitors.Count, data.AppliedLayouts.Count);
228              Assert.AreEqual(layoutType.TypeToString(), data.AppliedLayouts[0].AppliedLayout.Type);
229              Assert.AreEqual(Parameters.Monitors[0].MonitorNumber, data.AppliedLayouts[0].Device.MonitorNumber);
230          }
231  
232          [TestMethod("FancyZonesEditor.Basic.ApplyLayoutsOnEachMonitor")]
233          [TestCategory("FancyZones Editor #10")]
234          public void ApplyLayoutsOnEachMonitor()
235          {
236              // apply the layout on the first monitor
237              var firstLayoutType = LayoutType.Columns;
238              var firstLayoutName = TestConstants.TemplateLayoutNames[firstLayoutType];
239              Session.Find<Element>(firstLayoutName).Click();
240              Assert.IsTrue(Session.Find<Element>(firstLayoutName)!.Selected);
241  
242              // apply the layout on the second monitor
243              Session.Find<Element>(PowerToys.UITest.By.AccessibilityId("Monitors")).Find<Element>("Monitor 2").Click();
244              var secondLayout = CustomLayoutsList.CustomLayouts[0];
245              Session.Find<Element>(secondLayout.Name).Click();
246              Assert.IsTrue(Session.Find<Element>(secondLayout.Name)!.Selected);
247  
248              // verify the layout on the first monitor wasn't changed
249              Session.Find<Element>(PowerToys.UITest.By.AccessibilityId("Monitors")).Find<Element>("Monitor 1").Click();
250              Assert.IsTrue(Session.Find<Element>(firstLayoutName)!.Selected);
251  
252              // verify the file
253              var appliedLayouts = new AppliedLayouts();
254              var data = appliedLayouts.Read(appliedLayouts.File);
255              Assert.AreEqual(Parameters.Monitors.Count, data.AppliedLayouts.Count);
256              Assert.AreEqual(firstLayoutType.TypeToString(), data.AppliedLayouts.Find(x => x.Device.MonitorNumber == 1).AppliedLayout.Type);
257              Assert.AreEqual(secondLayout.Uuid, data.AppliedLayouts.Find(x => x.Device.MonitorNumber == 2).AppliedLayout.Uuid);
258          }
259  
260          [TestMethod("FancyZonesEditor.Basic.ApplyTemplateWithDifferentParametersOnEachMonitor")]
261          [TestCategory("FancyZones Editor #10")]
262          public void ApplyTemplateWithDifferentParametersOnEachMonitor()
263          {
264              var layoutType = LayoutType.Columns;
265              var layoutName = TestConstants.TemplateLayoutNames[layoutType];
266  
267              // apply the layout on the first monitor, set parameters
268              Session.Find<Element>(layoutName).Click();
269              Session.Find<Element>(layoutName).Find<Button>(PowerToys.UITest.By.AccessibilityId(AccessibilityId.EditLayoutButton)).Click();
270              var slider = Session.Find<Custom>(PowerToys.UITest.By.AccessibilityId(AccessibilityId.TemplateZoneSlider));
271              Assert.IsNotNull(slider);
272              slider.SendKeys(Key.Right);
273              slider.SendKeys(Key.Right);
274              var expectedFirstLayoutZoneCount = int.Parse(slider.Text!, CultureInfo.InvariantCulture);
275              Session.Find<Button>(ElementName.Save).Click();
276  
277              // apply the layout on the second monitor, set different parameters
278              Session.Find<Element>(PowerToys.UITest.By.AccessibilityId("Monitors")).Find<Element>("Monitor 2").Click();
279              Session.Find<Element>(layoutName).Click();
280              Session.Find<Element>(layoutName).Find<Button>(PowerToys.UITest.By.AccessibilityId(AccessibilityId.EditLayoutButton)).Click();
281              slider = Session.Find<Custom>(PowerToys.UITest.By.AccessibilityId(AccessibilityId.TemplateZoneSlider));
282              Assert.IsNotNull(slider);
283              slider.SendKeys(Key.Left);
284              var expectedSecondLayoutZoneCount = int.Parse(slider.Text!, CultureInfo.InvariantCulture);
285              Session.Find<Button>(ElementName.Save).Click();
286  
287              // verify the layout on the first monitor wasn't changed
288              Session.Find<Element>(PowerToys.UITest.By.AccessibilityId("Monitors")).Find<Element>("Monitor 1").Click();
289              Session.Find<Element>(layoutName).Find<Button>(PowerToys.UITest.By.AccessibilityId(AccessibilityId.EditLayoutButton)).Click();
290              slider = Session.Find<Custom>(PowerToys.UITest.By.AccessibilityId(AccessibilityId.TemplateZoneSlider));
291              Assert.IsNotNull(slider);
292              Assert.AreEqual(expectedFirstLayoutZoneCount, int.Parse(slider.Text!, CultureInfo.InvariantCulture));
293              Session.Find<Button>(ElementName.Cancel).Click();
294  
295              // check the file
296              var appliedLayouts = new AppliedLayouts();
297              var data = appliedLayouts.Read(appliedLayouts.File);
298              Assert.AreEqual(Parameters.Monitors.Count, data.AppliedLayouts.Count);
299              Assert.AreEqual(layoutType.TypeToString(), data.AppliedLayouts.Find(x => x.Device.MonitorNumber == 1).AppliedLayout.Type);
300              Assert.AreEqual(expectedFirstLayoutZoneCount, data.AppliedLayouts.Find(x => x.Device.MonitorNumber == 1).AppliedLayout.ZoneCount);
301              Assert.AreEqual(layoutType.TypeToString(), data.AppliedLayouts.Find(x => x.Device.MonitorNumber == 2).AppliedLayout.Type);
302              Assert.AreEqual(expectedSecondLayoutZoneCount, data.AppliedLayouts.Find(x => x.Device.MonitorNumber == 2).AppliedLayout.ZoneCount);
303          }
304      }
305  }