WindowTests_MacOS.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 Does_Not_Switch_Space_From_FullScreen_To_Main_Desktop_When_FullScreen_Window_Clicked()
  127. {
  128. // Issue #9565
  129. var mainWindow = _session.FindElementByAccessibilityId("MainWindow");
  130. AppiumWebElement windowState;
  131. // Open child window.
  132. using (OpenWindow(new PixelSize(200, 100), ShowWindowMode.Owned, WindowStartupLocation.Manual))
  133. {
  134. // Enter fullscreen
  135. mainWindow.FindElementByAccessibilityId("EnterFullscreen").Click();
  136. // Wait for fullscreen transition.
  137. Thread.Sleep(1000);
  138. // Make sure we entered fullscreen.
  139. mainWindow = _session.FindElementByAccessibilityId("MainWindow");
  140. windowState = mainWindow.FindElementByAccessibilityId("MainWindowState");
  141. Assert.Equal("FullScreen", windowState.Text);
  142. // Click on main window
  143. mainWindow.Click();
  144. // Failed here due to #9565: main window is no longer visible as the main space is now shown instead
  145. // of the fullscreen space.
  146. mainWindow.FindElementByAccessibilityId("ExitFullscreen").Click();
  147. // Wait for restore transition.
  148. Thread.Sleep(1000);
  149. }
  150. // Make sure we exited fullscreen.
  151. mainWindow = _session.FindElementByAccessibilityId("MainWindow");
  152. windowState = mainWindow.FindElementByAccessibilityId("MainWindowState");
  153. Assert.Equal("Normal", windowState.Text);
  154. }
  155. [PlatformFact(TestPlatforms.MacOS)]
  156. public void WindowOrder_NonOwned_Window_Does_Not_Stay_InFront_Of_Parent()
  157. {
  158. var mainWindow = _session.FindElementByAccessibilityId("MainWindow");
  159. using (OpenWindow(new PixelSize(800, 100), ShowWindowMode.NonOwned, WindowStartupLocation.Manual))
  160. {
  161. mainWindow.Click();
  162. var secondaryWindowIndex = GetWindowOrder("SecondaryWindow");
  163. Assert.Equal(2, secondaryWindowIndex);
  164. var sendToBack = _session.FindElementByAccessibilityId("SendToBack");
  165. sendToBack.Click();
  166. }
  167. }
  168. [PlatformFact(TestPlatforms.MacOS)]
  169. public void WindowOrder_Owned_Is_Correct_After_Closing_Window()
  170. {
  171. using (OpenWindow(new PixelSize(300, 500), ShowWindowMode.Owned, WindowStartupLocation.CenterOwner))
  172. {
  173. // Open a second child window, and close it.
  174. using (OpenWindow(new PixelSize(200, 200), ShowWindowMode.Owned, WindowStartupLocation.CenterOwner))
  175. {
  176. }
  177. var secondaryWindowIndex = GetWindowOrder("SecondaryWindow");
  178. Assert.Equal(1, secondaryWindowIndex);
  179. }
  180. }
  181. [PlatformFact(TestPlatforms.MacOS)]
  182. public void Parent_Window_Has_Disabled_ChromeButtons_When_Modal_Dialog_Shown()
  183. {
  184. var window = GetWindow("MainWindow");
  185. var (closeButton, miniaturizeButton, zoomButton) = window.GetChromeButtons();
  186. Assert.True(closeButton.Enabled);
  187. Assert.True(zoomButton.Enabled);
  188. Assert.True(miniaturizeButton.Enabled);
  189. using (OpenWindow(new PixelSize(200, 100), ShowWindowMode.Modal, WindowStartupLocation.CenterOwner))
  190. {
  191. Assert.False(closeButton.Enabled);
  192. Assert.False(zoomButton.Enabled);
  193. Assert.False(miniaturizeButton.Enabled);
  194. }
  195. }
  196. [PlatformFact(TestPlatforms.MacOS)]
  197. public void Minimize_Button_Is_Disabled_On_Modal_Dialog()
  198. {
  199. using (OpenWindow(new PixelSize(200, 100), ShowWindowMode.Modal, WindowStartupLocation.CenterOwner))
  200. {
  201. var secondaryWindow = GetWindow("SecondaryWindow");
  202. var (closeButton, miniaturizeButton, zoomButton) = secondaryWindow.GetChromeButtons();
  203. Assert.True(closeButton.Enabled);
  204. Assert.True(zoomButton.Enabled);
  205. Assert.False(miniaturizeButton.Enabled);
  206. }
  207. }
  208. [PlatformTheory(TestPlatforms.MacOS)]
  209. [InlineData(ShowWindowMode.Owned)]
  210. public void Minimize_Button_Disabled_Owned_Window(ShowWindowMode mode)
  211. {
  212. using (OpenWindow(new PixelSize(200, 100), mode, WindowStartupLocation.Manual))
  213. {
  214. var secondaryWindow = GetWindow("SecondaryWindow");
  215. var (_, miniaturizeButton, _) = secondaryWindow.GetChromeButtons();
  216. Assert.False(miniaturizeButton.Enabled);
  217. }
  218. }
  219. [PlatformTheory(TestPlatforms.MacOS)]
  220. [InlineData(ShowWindowMode.NonOwned)]
  221. public void Minimize_Button_Minimizes_Window(ShowWindowMode mode)
  222. {
  223. using (OpenWindow(new PixelSize(200, 100), mode, WindowStartupLocation.Manual))
  224. {
  225. var secondaryWindow = GetWindow("SecondaryWindow");
  226. var (_, miniaturizeButton, _) = secondaryWindow.GetChromeButtons();
  227. miniaturizeButton.Click();
  228. Thread.Sleep(1000);
  229. var hittable = _session.FindElementsByXPath("/XCUIElementTypeApplication/XCUIElementTypeWindow")
  230. .Select(x => x.GetAttribute("hittable")).ToList();
  231. Assert.Equal(new[] { "true", "false" }, hittable);
  232. _session.FindElementByAccessibilityId("RestoreAll").Click();
  233. Thread.Sleep(1000);
  234. hittable = _session.FindElementsByXPath("/XCUIElementTypeApplication/XCUIElementTypeWindow")
  235. .Select(x => x.GetAttribute("hittable")).ToList();
  236. Assert.Equal(new[] { "true", "true" }, hittable);
  237. }
  238. }
  239. [PlatformFact(TestPlatforms.MacOS)]
  240. public void Hidden_Child_Window_Is_Not_Reshown_When_Parent_Clicked()
  241. {
  242. var mainWindow = _session.FindElementByAccessibilityId("MainWindow");
  243. // We don't use dispose to close the window here, because it seems that hiding and re-showing a window
  244. // causes Appium to think it's a different window.
  245. OpenWindow(null, ShowWindowMode.Owned, WindowStartupLocation.Manual);
  246. var secondaryWindow = GetWindow("SecondaryWindow");
  247. var hideButton = secondaryWindow.FindElementByAccessibilityId("HideButton");
  248. hideButton.Click();
  249. var windows = _session.FindElementsByXPath("XCUIElementTypeWindow");
  250. Assert.Single(windows);
  251. mainWindow.Click();
  252. windows = _session.FindElementsByXPath("XCUIElementTypeWindow");
  253. Assert.Single(windows);
  254. _session.FindElementByAccessibilityId("RestoreAll").Click();
  255. // Close the window manually.
  256. secondaryWindow = GetWindow("SecondaryWindow");
  257. secondaryWindow.GetChromeButtons().close.Click();
  258. }
  259. [PlatformTheory(TestPlatforms.MacOS)]
  260. [InlineData(ShowWindowMode.NonOwned)]
  261. [InlineData(ShowWindowMode.Owned)]
  262. [InlineData(ShowWindowMode.Modal)]
  263. public void Window_Has_Disabled_Zoom_Button_When_CanResize_Is_False(ShowWindowMode mode)
  264. {
  265. using (OpenWindow(null, mode, WindowStartupLocation.Manual, canResize: false))
  266. {
  267. var secondaryWindow = GetWindow("SecondaryWindow");
  268. var (_, _, zoomButton) = secondaryWindow.GetChromeButtons();
  269. Assert.False(zoomButton.Enabled);
  270. }
  271. }
  272. private IDisposable OpenWindow(
  273. PixelSize? size,
  274. ShowWindowMode mode,
  275. WindowStartupLocation location,
  276. bool canResize = true)
  277. {
  278. var sizeTextBox = _session.FindElementByAccessibilityId("ShowWindowSize");
  279. var modeComboBox = _session.FindElementByAccessibilityId("ShowWindowMode");
  280. var locationComboBox = _session.FindElementByAccessibilityId("ShowWindowLocation");
  281. var canResizeCheckBox = _session.FindElementByAccessibilityId("ShowWindowCanResize");
  282. var showButton = _session.FindElementByAccessibilityId("ShowWindow");
  283. if (size.HasValue)
  284. sizeTextBox.SendKeys($"{size.Value.Width}, {size.Value.Height}");
  285. if (modeComboBox.GetComboBoxValue() != mode.ToString())
  286. {
  287. modeComboBox.Click();
  288. _session.FindElementByName(mode.ToString()).SendClick();
  289. }
  290. if (locationComboBox.GetComboBoxValue() != location.ToString())
  291. {
  292. locationComboBox.Click();
  293. _session.FindElementByName(location.ToString()).SendClick();
  294. }
  295. if (canResizeCheckBox.GetIsChecked() != canResize)
  296. canResizeCheckBox.Click();
  297. return showButton.OpenWindowWithClick();
  298. }
  299. private AppiumWebElement GetWindow(string identifier)
  300. {
  301. // The Avalonia a11y tree currently exposes two nested Window elements, this is a bug and should be fixed
  302. // but in the meantime use the `parent::' selector to return the parent "real" window.
  303. return _session.FindElementByXPath(
  304. $"XCUIElementTypeWindow//*[@identifier='{identifier}']/parent::XCUIElementTypeWindow");
  305. }
  306. private int GetWindowOrder(string identifier)
  307. {
  308. var window = GetWindow(identifier);
  309. var order = window.FindElementByXPath("//*[@identifier='Order']");
  310. return int.Parse(order.Text);
  311. }
  312. public enum ShowWindowMode
  313. {
  314. NonOwned,
  315. Owned,
  316. Modal
  317. }
  318. }
  319. }