WindowTests_MacOS.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using Avalonia.Controls;
  6. using OpenQA.Selenium;
  7. using OpenQA.Selenium.Appium;
  8. using OpenQA.Selenium.Interactions;
  9. using Xunit;
  10. namespace Avalonia.IntegrationTests.Appium
  11. {
  12. [Collection("Default")]
  13. public class WindowTests_MacOS
  14. {
  15. private readonly AppiumDriver<AppiumWebElement> _session;
  16. public WindowTests_MacOS(TestAppFixture fixture)
  17. {
  18. var retry = 0;
  19. _session = fixture.Session;
  20. for (;;)
  21. {
  22. try
  23. {
  24. var tabs = _session.FindElementByAccessibilityId("MainTabs");
  25. var tab = tabs.FindElementByName("Window");
  26. tab.Click();
  27. return;
  28. }
  29. catch (WebDriverException e) when (retry++ < 3)
  30. {
  31. // MacOS sometimes seems to need a bit of time to get itself back in order after switching out
  32. // of fullscreen.
  33. Thread.Sleep(1000);
  34. }
  35. }
  36. }
  37. [PlatformFact(TestPlatforms.MacOS)]
  38. public void WindowOrder_Modal_Dialog_Stays_InFront_Of_Parent()
  39. {
  40. var mainWindow = _session.FindElementByAccessibilityId("MainWindow");
  41. using (OpenWindow(new PixelSize(200, 100), ShowWindowMode.Modal, WindowStartupLocation.Manual))
  42. {
  43. mainWindow.Click();
  44. var windows = _session.FindElements(By.XPath("XCUIElementTypeWindow"));
  45. var mainWindowIndex = GetWindowOrder(windows, "MainWindow");
  46. var secondaryWindowIndex = GetWindowOrder(windows, "SecondaryWindow");
  47. Assert.Equal(0, secondaryWindowIndex);
  48. Assert.Equal(1, mainWindowIndex);
  49. }
  50. }
  51. [PlatformFact(TestPlatforms.MacOS)]
  52. public void WindowOrder_Modal_Dialog_Stays_InFront_Of_Parent_When_Clicking_Resize_Grip()
  53. {
  54. var mainWindow = FindWindow(_session, "MainWindow");
  55. using (OpenWindow(new PixelSize(200, 100), ShowWindowMode.Modal, WindowStartupLocation.Manual))
  56. {
  57. new Actions(_session)
  58. .MoveToElement(mainWindow, 100, 1)
  59. .ClickAndHold()
  60. .Perform();
  61. var windows = _session.FindElements(By.XPath("XCUIElementTypeWindow"));
  62. var mainWindowIndex = GetWindowOrder(windows, "MainWindow");
  63. var secondaryWindowIndex = GetWindowOrder(windows, "SecondaryWindow");
  64. new Actions(_session)
  65. .MoveToElement(mainWindow, 100, 1)
  66. .Release()
  67. .Perform();
  68. Assert.Equal(0, secondaryWindowIndex);
  69. Assert.Equal(1, mainWindowIndex);
  70. }
  71. }
  72. [PlatformFact(TestPlatforms.MacOS)]
  73. public void WindowOrder_Modal_Dialog_Stays_InFront_Of_Parent_When_In_Fullscreen()
  74. {
  75. var mainWindow = FindWindow(_session, "MainWindow");
  76. var buttons = mainWindow.GetChromeButtons();
  77. buttons.maximize.Click();
  78. Thread.Sleep(500);
  79. try
  80. {
  81. using (OpenWindow(new PixelSize(200, 100), ShowWindowMode.Modal, WindowStartupLocation.Manual))
  82. {
  83. var windows = _session.FindElements(By.XPath("XCUIElementTypeWindow"));
  84. var mainWindowIndex = GetWindowOrder(windows, "MainWindow");
  85. var secondaryWindowIndex = GetWindowOrder(windows, "SecondaryWindow");
  86. Assert.Equal(0, secondaryWindowIndex);
  87. Assert.Equal(1, mainWindowIndex);
  88. Thread.Sleep(5000);
  89. }
  90. }
  91. finally
  92. {
  93. _session.FindElementByAccessibilityId("ExitFullscreen").Click();
  94. }
  95. }
  96. [PlatformFact(TestPlatforms.MacOS)]
  97. public void WindowOrder_Owned_Dialog_Stays_InFront_Of_Parent()
  98. {
  99. var mainWindow = _session.FindElementByAccessibilityId("MainWindow");
  100. using (OpenWindow(new PixelSize(200, 100), ShowWindowMode.Owned, WindowStartupLocation.Manual))
  101. {
  102. mainWindow.Click();
  103. var windows = _session.FindElements(By.XPath("XCUIElementTypeWindow"));
  104. var mainWindowIndex = GetWindowOrder(windows, "MainWindow");
  105. var secondaryWindowIndex = GetWindowOrder(windows, "SecondaryWindow");
  106. Assert.Equal(0, secondaryWindowIndex);
  107. Assert.Equal(1, mainWindowIndex);
  108. }
  109. }
  110. [PlatformFact(TestPlatforms.MacOS)]
  111. public void WindowOrder_NonOwned_Window_Does_Not_Stay_InFront_Of_Parent()
  112. {
  113. var mainWindow = _session.FindElementByAccessibilityId("MainWindow");
  114. using (OpenWindow(new PixelSize(800, 100), ShowWindowMode.NonOwned, WindowStartupLocation.Manual))
  115. {
  116. mainWindow.Click();
  117. var windows = _session.FindElements(By.XPath("XCUIElementTypeWindow"));
  118. var mainWindowIndex = GetWindowOrder(windows, "MainWindow");
  119. var secondaryWindowIndex = GetWindowOrder(windows, "SecondaryWindow");
  120. Assert.Equal(1, secondaryWindowIndex);
  121. Assert.Equal(0, mainWindowIndex);
  122. var sendToBack = _session.FindElementByAccessibilityId("SendToBack");
  123. sendToBack.Click();
  124. }
  125. }
  126. [PlatformFact(TestPlatforms.MacOS)]
  127. public void Parent_Window_Has_Disabled_ChromeButtons_When_Modal_Dialog_Shown()
  128. {
  129. var window = FindWindow(_session, "MainWindow");
  130. var (closeButton, miniaturizeButton, zoomButton) = window.GetChromeButtons();
  131. Assert.True(closeButton.Enabled);
  132. Assert.True(zoomButton.Enabled);
  133. Assert.True(miniaturizeButton.Enabled);
  134. using (OpenWindow(new PixelSize(200, 100), ShowWindowMode.Modal, WindowStartupLocation.CenterOwner))
  135. {
  136. Assert.False(closeButton.Enabled);
  137. Assert.False(zoomButton.Enabled);
  138. Assert.False(miniaturizeButton.Enabled);
  139. }
  140. }
  141. [PlatformFact(TestPlatforms.MacOS)]
  142. public void Minimize_Button_Is_Disabled_On_Modal_Dialog()
  143. {
  144. using (OpenWindow(new PixelSize(200, 100), ShowWindowMode.Modal, WindowStartupLocation.CenterOwner))
  145. {
  146. var secondaryWindow = FindWindow(_session, "SecondaryWindow");
  147. var (closeButton, miniaturizeButton, zoomButton) = secondaryWindow.GetChromeButtons();
  148. Assert.True(closeButton.Enabled);
  149. Assert.True(zoomButton.Enabled);
  150. Assert.False(miniaturizeButton.Enabled);
  151. }
  152. }
  153. [PlatformTheory(TestPlatforms.MacOS)]
  154. [InlineData(ShowWindowMode.NonOwned)]
  155. [InlineData(ShowWindowMode.Owned)]
  156. public void Minimize_Button_Minimizes_Window(ShowWindowMode mode)
  157. {
  158. using (OpenWindow(new PixelSize(200, 100), mode, WindowStartupLocation.Manual))
  159. {
  160. var secondaryWindow = FindWindow(_session, "SecondaryWindow");
  161. var (_, miniaturizeButton, _) = secondaryWindow.GetChromeButtons();
  162. miniaturizeButton.Click();
  163. Thread.Sleep(1000);
  164. var hittable = _session.FindElementsByXPath("/XCUIElementTypeApplication/XCUIElementTypeWindow")
  165. .Select(x => x.GetAttribute("hittable")).ToList();
  166. Assert.Equal(new[] { "true", "false" }, hittable);
  167. _session.FindElementByAccessibilityId("RestoreAll").Click();
  168. Thread.Sleep(1000);
  169. hittable = _session.FindElementsByXPath("/XCUIElementTypeApplication/XCUIElementTypeWindow")
  170. .Select(x => x.GetAttribute("hittable")).ToList();
  171. Assert.Equal(new[] { "true", "true" }, hittable);
  172. }
  173. }
  174. private IDisposable OpenWindow(PixelSize? size, ShowWindowMode mode, WindowStartupLocation location)
  175. {
  176. var sizeTextBox = _session.FindElementByAccessibilityId("ShowWindowSize");
  177. var modeComboBox = _session.FindElementByAccessibilityId("ShowWindowMode");
  178. var locationComboBox = _session.FindElementByAccessibilityId("ShowWindowLocation");
  179. var showButton = _session.FindElementByAccessibilityId("ShowWindow");
  180. if (size.HasValue)
  181. sizeTextBox.SendKeys($"{size.Value.Width}, {size.Value.Height}");
  182. modeComboBox.Click();
  183. _session.FindElementByName(mode.ToString()).SendClick();
  184. locationComboBox.Click();
  185. _session.FindElementByName(location.ToString()).SendClick();
  186. return showButton.OpenWindowWithClick();
  187. }
  188. private static int GetWindowOrder(IReadOnlyCollection<AppiumWebElement> elements, string identifier)
  189. {
  190. return elements.TakeWhile(x =>
  191. x.FindElementByXPath("XCUIElementTypeWindow")?.GetAttribute("identifier") != identifier).Count();
  192. }
  193. private static AppiumWebElement FindWindow(AppiumDriver<AppiumWebElement> session, string identifier)
  194. {
  195. var windows = session.FindElementsByXPath("XCUIElementTypeWindow");
  196. return windows.First(x =>
  197. x.FindElementsByXPath("XCUIElementTypeWindow")
  198. .Any(y => y.GetAttribute("identifier") == identifier));
  199. }
  200. public enum ShowWindowMode
  201. {
  202. NonOwned,
  203. Owned,
  204. Modal
  205. }
  206. }
  207. }