WindowTests_MacOS.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using Avalonia.Controls;
  6. using Avalonia.Utilities;
  7. using OpenQA.Selenium;
  8. using OpenQA.Selenium.Appium;
  9. using OpenQA.Selenium.Interactions;
  10. using Xunit;
  11. namespace Avalonia.IntegrationTests.Appium
  12. {
  13. [Collection("Default")]
  14. public class WindowTests_MacOS
  15. {
  16. private readonly AppiumDriver<AppiumWebElement> _session;
  17. public WindowTests_MacOS(TestAppFixture fixture)
  18. {
  19. var retry = 0;
  20. _session = fixture.Session;
  21. for (;;)
  22. {
  23. try
  24. {
  25. var tabs = _session.FindElementByAccessibilityId("MainTabs");
  26. var tab = tabs.FindElementByName("Window");
  27. tab.Click();
  28. return;
  29. }
  30. catch (WebDriverException) when (retry++ < 3)
  31. {
  32. // MacOS sometimes seems to need a bit of time to get itself back in order after switching out
  33. // of fullscreen.
  34. Thread.Sleep(1000);
  35. }
  36. }
  37. }
  38. [PlatformFact(TestPlatforms.MacOS)]
  39. public void WindowOrder_Modal_Dialog_Stays_InFront_Of_Parent()
  40. {
  41. var mainWindow = _session.FindElementByAccessibilityId("MainWindow");
  42. using (OpenWindow(new PixelSize(200, 100), ShowWindowMode.Modal, WindowStartupLocation.Manual))
  43. {
  44. mainWindow.Click();
  45. var secondaryWindowIndex = GetWindowOrder("SecondaryWindow");
  46. Assert.Equal(1, secondaryWindowIndex);
  47. }
  48. }
  49. [PlatformFact(TestPlatforms.MacOS)]
  50. public void WindowOrder_Modal_Dialog_Stays_InFront_Of_Parent_When_Clicking_Resize_Grip()
  51. {
  52. var mainWindow = GetWindow("MainWindow");
  53. using (OpenWindow(new PixelSize(200, 100), ShowWindowMode.Modal, WindowStartupLocation.Manual))
  54. {
  55. new Actions(_session)
  56. .MoveToElement(mainWindow, 100, 1)
  57. .ClickAndHold()
  58. .Perform();
  59. var secondaryWindowIndex = GetWindowOrder("SecondaryWindow");
  60. new Actions(_session)
  61. .MoveToElement(mainWindow, 100, 1)
  62. .Release()
  63. .Perform();
  64. Assert.Equal(1, secondaryWindowIndex);
  65. }
  66. }
  67. [PlatformFact(TestPlatforms.MacOS)]
  68. public void WindowOrder_Modal_Dialog_Stays_InFront_Of_Parent_When_In_Fullscreen()
  69. {
  70. var mainWindow = GetWindow("MainWindow");
  71. var buttons = mainWindow.GetChromeButtons();
  72. buttons.maximize.Click();
  73. Thread.Sleep(500);
  74. try
  75. {
  76. using (OpenWindow(new PixelSize(200, 100), ShowWindowMode.Modal, WindowStartupLocation.Manual))
  77. {
  78. var secondaryWindowIndex = GetWindowOrder("SecondaryWindow");
  79. Assert.Equal(1, secondaryWindowIndex);
  80. }
  81. }
  82. finally
  83. {
  84. _session.FindElementByAccessibilityId("ExitFullscreen").Click();
  85. }
  86. }
  87. [PlatformFact(TestPlatforms.MacOS)]
  88. public void WindowOrder_Owned_Dialog_Stays_InFront_Of_Parent()
  89. {
  90. var mainWindow = _session.FindElementByAccessibilityId("MainWindow");
  91. using (OpenWindow(new PixelSize(200, 100), ShowWindowMode.Owned, WindowStartupLocation.Manual))
  92. {
  93. mainWindow.SendClick();
  94. var secondaryWindowIndex = GetWindowOrder("SecondaryWindow");
  95. Assert.Equal(1, secondaryWindowIndex);
  96. }
  97. }
  98. [PlatformFact(TestPlatforms.MacOS)]
  99. public void WindowOrder_Owned_Dialog_Stays_InFront_Of_FullScreen_Parent()
  100. {
  101. var mainWindow = _session.FindElementByAccessibilityId("MainWindow");
  102. // Enter fullscreen
  103. mainWindow.FindElementByAccessibilityId("EnterFullscreen").Click();
  104. // Wait for fullscreen transition.
  105. Thread.Sleep(1000);
  106. // Make sure we entered fullscreen.
  107. var windowState = mainWindow.FindElementByAccessibilityId("MainWindowState");
  108. Assert.Equal("FullScreen", windowState.Text);
  109. // Open child window.
  110. using (OpenWindow(new PixelSize(200, 100), ShowWindowMode.Owned, WindowStartupLocation.Manual))
  111. {
  112. mainWindow.SendClick();
  113. var secondaryWindowIndex = GetWindowOrder("SecondaryWindow");
  114. Assert.Equal(1, secondaryWindowIndex);
  115. }
  116. // Exit fullscreen by menu shortcut Command+R
  117. mainWindow.FindElementByAccessibilityId("ExitFullscreen").Click();
  118. // Wait for restore transition.
  119. Thread.Sleep(1000);
  120. // Make sure we exited fullscreen.
  121. mainWindow = _session.FindElementByAccessibilityId("MainWindow");
  122. windowState = mainWindow.FindElementByAccessibilityId("MainWindowState");
  123. Assert.Equal("Normal", windowState.Text);
  124. }
  125. [PlatformFact(TestPlatforms.MacOS)]
  126. public void WindowOrder_Owned_Dialog_Stays_InFront_Of_Parent_After_Modal_Closed()
  127. {
  128. using (OpenWindow(new PixelSize(200, 300), ShowWindowMode.Owned, WindowStartupLocation.Manual))
  129. {
  130. OpenWindow(null, ShowWindowMode.Modal, WindowStartupLocation.Manual).Dispose();
  131. var secondaryWindowIndex = GetWindowOrder("SecondaryWindow");
  132. Assert.Equal(1, secondaryWindowIndex);
  133. }
  134. }
  135. [PlatformFact(TestPlatforms.MacOS)]
  136. public void Does_Not_Switch_Space_From_FullScreen_To_Main_Desktop_When_FullScreen_Window_Clicked()
  137. {
  138. // Issue #9565
  139. var mainWindow = _session.FindElementByAccessibilityId("MainWindow");
  140. AppiumWebElement windowState;
  141. // Open child window.
  142. using (OpenWindow(new PixelSize(200, 100), ShowWindowMode.Owned, WindowStartupLocation.Manual))
  143. {
  144. // Enter fullscreen
  145. mainWindow.FindElementByAccessibilityId("EnterFullscreen").Click();
  146. // Wait for fullscreen transition.
  147. Thread.Sleep(1000);
  148. // Make sure we entered fullscreen.
  149. mainWindow = _session.FindElementByAccessibilityId("MainWindow");
  150. windowState = mainWindow.FindElementByAccessibilityId("MainWindowState");
  151. Assert.Equal("FullScreen", windowState.Text);
  152. // Click on main window
  153. mainWindow.Click();
  154. // Failed here due to #9565: main window is no longer visible as the main space is now shown instead
  155. // of the fullscreen space.
  156. mainWindow.FindElementByAccessibilityId("ExitFullscreen").Click();
  157. // Wait for restore transition.
  158. Thread.Sleep(1000);
  159. }
  160. // Make sure we exited fullscreen.
  161. mainWindow = _session.FindElementByAccessibilityId("MainWindow");
  162. windowState = mainWindow.FindElementByAccessibilityId("MainWindowState");
  163. Assert.Equal("Normal", windowState.Text);
  164. }
  165. [PlatformFact(TestPlatforms.MacOS)]
  166. public void WindowOrder_NonOwned_Window_Does_Not_Stay_InFront_Of_Parent()
  167. {
  168. var mainWindow = _session.FindElementByAccessibilityId("MainWindow");
  169. using (OpenWindow(new PixelSize(800, 100), ShowWindowMode.NonOwned, WindowStartupLocation.Manual))
  170. {
  171. mainWindow.Click();
  172. var secondaryWindowIndex = GetWindowOrder("SecondaryWindow");
  173. Assert.Equal(2, secondaryWindowIndex);
  174. var sendToBack = _session.FindElementByAccessibilityId("SendToBack");
  175. sendToBack.Click();
  176. }
  177. }
  178. [PlatformFact(TestPlatforms.MacOS)]
  179. public void WindowOrder_Owned_Is_Correct_After_Closing_Window()
  180. {
  181. using (OpenWindow(new PixelSize(300, 500), ShowWindowMode.Owned, WindowStartupLocation.CenterOwner))
  182. {
  183. // Open a second child window, and close it.
  184. using (OpenWindow(new PixelSize(200, 200), ShowWindowMode.Owned, WindowStartupLocation.CenterOwner))
  185. {
  186. }
  187. var secondaryWindowIndex = GetWindowOrder("SecondaryWindow");
  188. Assert.Equal(1, secondaryWindowIndex);
  189. }
  190. }
  191. [PlatformFact(TestPlatforms.MacOS)]
  192. public void Parent_Window_Has_Disabled_ChromeButtons_When_Modal_Dialog_Shown()
  193. {
  194. var window = GetWindow("MainWindow");
  195. var (closeButton, miniaturizeButton, zoomButton) = window.GetChromeButtons();
  196. Assert.True(closeButton.Enabled);
  197. Assert.True(zoomButton.Enabled);
  198. Assert.True(miniaturizeButton.Enabled);
  199. using (OpenWindow(new PixelSize(200, 100), ShowWindowMode.Modal, WindowStartupLocation.CenterOwner))
  200. {
  201. Assert.False(closeButton.Enabled);
  202. Assert.False(zoomButton.Enabled);
  203. Assert.False(miniaturizeButton.Enabled);
  204. }
  205. }
  206. [PlatformFact(TestPlatforms.MacOS)]
  207. public void Minimize_Button_Is_Disabled_On_Modal_Dialog()
  208. {
  209. using (OpenWindow(new PixelSize(200, 100), ShowWindowMode.Modal, WindowStartupLocation.CenterOwner))
  210. {
  211. var secondaryWindow = GetWindow("SecondaryWindow");
  212. var (closeButton, miniaturizeButton, zoomButton) = secondaryWindow.GetChromeButtons();
  213. Assert.True(closeButton.Enabled);
  214. Assert.True(zoomButton.Enabled);
  215. Assert.False(miniaturizeButton.Enabled);
  216. }
  217. }
  218. [PlatformTheory(TestPlatforms.MacOS)]
  219. [InlineData(ShowWindowMode.Owned)]
  220. public void Minimize_Button_Disabled_Owned_Window(ShowWindowMode mode)
  221. {
  222. using (OpenWindow(new PixelSize(200, 100), mode, WindowStartupLocation.Manual))
  223. {
  224. var secondaryWindow = GetWindow("SecondaryWindow");
  225. var (_, miniaturizeButton, _) = secondaryWindow.GetChromeButtons();
  226. Assert.False(miniaturizeButton.Enabled);
  227. }
  228. }
  229. [PlatformTheory(TestPlatforms.MacOS)]
  230. [InlineData(ShowWindowMode.NonOwned)]
  231. public void Minimize_Button_Minimizes_Window(ShowWindowMode mode)
  232. {
  233. using (OpenWindow(new PixelSize(200, 100), mode, WindowStartupLocation.Manual))
  234. {
  235. var secondaryWindow = GetWindow("SecondaryWindow");
  236. var (_, miniaturizeButton, _) = secondaryWindow.GetChromeButtons();
  237. miniaturizeButton.Click();
  238. Thread.Sleep(1000);
  239. var hittable = _session.FindElementsByXPath("/XCUIElementTypeApplication/XCUIElementTypeWindow")
  240. .Select(x => x.GetAttribute("hittable")).ToList();
  241. Assert.Equal(new[] { "true", "false" }, hittable);
  242. _session.FindElementByAccessibilityId("RestoreAll").Click();
  243. Thread.Sleep(1000);
  244. hittable = _session.FindElementsByXPath("/XCUIElementTypeApplication/XCUIElementTypeWindow")
  245. .Select(x => x.GetAttribute("hittable")).ToList();
  246. Assert.Equal(new[] { "true", "true" }, hittable);
  247. }
  248. }
  249. [PlatformFact(TestPlatforms.MacOS)]
  250. public void Hidden_Child_Window_Is_Not_Reshown_When_Parent_Clicked()
  251. {
  252. var mainWindow = _session.FindElementByAccessibilityId("MainWindow");
  253. // We don't use dispose to close the window here, because it seems that hiding and re-showing a window
  254. // causes Appium to think it's a different window.
  255. OpenWindow(null, ShowWindowMode.Owned, WindowStartupLocation.Manual);
  256. var secondaryWindow = GetWindow("SecondaryWindow");
  257. var hideButton = secondaryWindow.FindElementByAccessibilityId("HideButton");
  258. hideButton.Click();
  259. var windows = _session.FindElementsByXPath("XCUIElementTypeWindow");
  260. Assert.Single(windows);
  261. mainWindow.Click();
  262. windows = _session.FindElementsByXPath("XCUIElementTypeWindow");
  263. Assert.Single(windows);
  264. _session.FindElementByAccessibilityId("RestoreAll").Click();
  265. // Close the window manually.
  266. secondaryWindow = GetWindow("SecondaryWindow");
  267. secondaryWindow.GetChromeButtons().close.Click();
  268. }
  269. [PlatformTheory(TestPlatforms.MacOS)]
  270. [InlineData(ShowWindowMode.NonOwned)]
  271. [InlineData(ShowWindowMode.Owned)]
  272. [InlineData(ShowWindowMode.Modal)]
  273. public void Window_Has_Disabled_Zoom_Button_When_CanResize_Is_False(ShowWindowMode mode)
  274. {
  275. using (OpenWindow(null, mode, WindowStartupLocation.Manual, canResize: false))
  276. {
  277. var secondaryWindow = GetWindow("SecondaryWindow");
  278. var (_, _, zoomButton) = secondaryWindow.GetChromeButtons();
  279. Assert.False(zoomButton.Enabled);
  280. }
  281. }
  282. private IDisposable OpenWindow(
  283. PixelSize? size,
  284. ShowWindowMode mode,
  285. WindowStartupLocation location,
  286. bool canResize = true)
  287. {
  288. var sizeTextBox = _session.FindElementByAccessibilityId("ShowWindowSize");
  289. var modeComboBox = _session.FindElementByAccessibilityId("ShowWindowMode");
  290. var locationComboBox = _session.FindElementByAccessibilityId("ShowWindowLocation");
  291. var canResizeCheckBox = _session.FindElementByAccessibilityId("ShowWindowCanResize");
  292. var showButton = _session.FindElementByAccessibilityId("ShowWindow");
  293. if (size.HasValue)
  294. sizeTextBox.SendKeys($"{size.Value.Width}, {size.Value.Height}");
  295. if (modeComboBox.GetComboBoxValue() != mode.ToString())
  296. {
  297. modeComboBox.Click();
  298. _session.FindElementByName(mode.ToString()).SendClick();
  299. }
  300. if (locationComboBox.GetComboBoxValue() != location.ToString())
  301. {
  302. locationComboBox.Click();
  303. _session.FindElementByName(location.ToString()).SendClick();
  304. }
  305. if (canResizeCheckBox.GetIsChecked() != canResize)
  306. canResizeCheckBox.Click();
  307. return showButton.OpenWindowWithClick();
  308. }
  309. private AppiumWebElement GetWindow(string identifier)
  310. {
  311. // The Avalonia a11y tree currently exposes two nested Window elements, this is a bug and should be fixed
  312. // but in the meantime use the `parent::' selector to return the parent "real" window.
  313. return _session.FindElementByXPath(
  314. $"XCUIElementTypeWindow//*[@identifier='{identifier}']/parent::XCUIElementTypeWindow");
  315. }
  316. private int GetWindowOrder(string identifier)
  317. {
  318. var window = GetWindow(identifier);
  319. var order = window.FindElementByXPath("//*[@identifier='Order']");
  320. return int.Parse(order.Text);
  321. }
  322. public enum ShowWindowMode
  323. {
  324. NonOwned,
  325. Owned,
  326. Modal
  327. }
  328. }
  329. }