WindowTests_MacOS.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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(DefaultAppFixture 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 fullScreen = mainWindow.FindElementByAccessibilityId("_XCUI:FullScreenWindow");
  72. fullScreen.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, Skip = "Flaky test")]
  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 windowChrome = window.GetChromeButtons();
  196. Assert.True(windowChrome.Close!.Enabled);
  197. Assert.True(windowChrome.FullScreen!.Enabled);
  198. Assert.True(windowChrome.Minimize!.Enabled);
  199. Assert.Null(windowChrome.Maximize);
  200. using (OpenWindow(new PixelSize(200, 100), ShowWindowMode.Modal, WindowStartupLocation.CenterOwner))
  201. {
  202. Assert.False(windowChrome.Close!.Enabled);
  203. Assert.False(windowChrome.FullScreen!.Enabled);
  204. Assert.False(windowChrome.Minimize!.Enabled);
  205. }
  206. }
  207. [PlatformFact(TestPlatforms.MacOS)]
  208. public void Minimize_Button_Is_Disabled_On_Modal_Dialog()
  209. {
  210. using (OpenWindow(new PixelSize(200, 100), ShowWindowMode.Modal, WindowStartupLocation.CenterOwner))
  211. {
  212. var secondaryWindow = GetWindow("SecondaryWindow");
  213. var windowChrome = secondaryWindow.GetChromeButtons();
  214. Assert.True(windowChrome.Close!.Enabled);
  215. Assert.True(windowChrome.Maximize!.Enabled);
  216. Assert.False(windowChrome.Minimize!.Enabled);
  217. }
  218. }
  219. [PlatformTheory(TestPlatforms.MacOS)]
  220. [InlineData(ShowWindowMode.Owned)]
  221. public void Minimize_Button_Disabled_Owned_Window(ShowWindowMode mode)
  222. {
  223. using (OpenWindow(new PixelSize(200, 100), mode, WindowStartupLocation.Manual))
  224. {
  225. var secondaryWindow = GetWindow("SecondaryWindow");
  226. var miniaturizeButton = secondaryWindow.FindElementByAccessibilityId("_XCUI:MinimizeWindow");
  227. Assert.False(miniaturizeButton.Enabled);
  228. }
  229. }
  230. [PlatformTheory(TestPlatforms.MacOS)]
  231. [InlineData(ShowWindowMode.NonOwned)]
  232. public void Minimize_Button_Minimizes_Window(ShowWindowMode mode)
  233. {
  234. using (OpenWindow(new PixelSize(200, 100), mode, WindowStartupLocation.Manual))
  235. {
  236. var secondaryWindow = GetWindow("SecondaryWindow");
  237. var miniaturizeButton = secondaryWindow.FindElementByAccessibilityId("_XCUI:MinimizeWindow");
  238. miniaturizeButton.Click();
  239. Thread.Sleep(1000);
  240. var hittable = _session.FindElementsByXPath("/XCUIElementTypeApplication/XCUIElementTypeWindow")
  241. .Select(x => x.GetAttribute("hittable")).ToList();
  242. Assert.Equal(new[] { "true", "false" }, hittable);
  243. _session.FindElementByAccessibilityId("RestoreAll").Click();
  244. Thread.Sleep(1000);
  245. hittable = _session.FindElementsByXPath("/XCUIElementTypeApplication/XCUIElementTypeWindow")
  246. .Select(x => x.GetAttribute("hittable")).ToList();
  247. Assert.Equal(new[] { "true", "true" }, hittable);
  248. }
  249. }
  250. [PlatformFact(TestPlatforms.MacOS)]
  251. public void Hidden_Child_Window_Is_Not_Reshown_When_Parent_Clicked()
  252. {
  253. var mainWindow = _session.FindElementByAccessibilityId("MainWindow");
  254. // We don't use dispose to close the window here, because it seems that hiding and re-showing a window
  255. // causes Appium to think it's a different window.
  256. OpenWindow(null, ShowWindowMode.Owned, WindowStartupLocation.Manual);
  257. var secondaryWindow = GetWindow("SecondaryWindow");
  258. var hideButton = secondaryWindow.FindElementByAccessibilityId("HideButton");
  259. hideButton.Click();
  260. var windows = _session.FindElementsByXPath("XCUIElementTypeWindow");
  261. Assert.Single(windows);
  262. mainWindow.Click();
  263. windows = _session.FindElementsByXPath("XCUIElementTypeWindow");
  264. Assert.Single(windows);
  265. _session.FindElementByAccessibilityId("RestoreAll").Click();
  266. // Close the window manually.
  267. secondaryWindow = GetWindow("SecondaryWindow");
  268. secondaryWindow.FindElementByAccessibilityId("_XCUI:CloseWindow").Click();
  269. }
  270. [PlatformTheory(TestPlatforms.MacOS)]
  271. [InlineData(ShowWindowMode.NonOwned)]
  272. [InlineData(ShowWindowMode.Owned)]
  273. [InlineData(ShowWindowMode.Modal)]
  274. public void Window_Has_Disabled_Zoom_Button_When_CanResize_Is_False(ShowWindowMode mode)
  275. {
  276. using (OpenWindow(null, mode, WindowStartupLocation.Manual, canResize: false))
  277. {
  278. var secondaryWindow = GetWindow("SecondaryWindow");
  279. var zoomButton = mode == ShowWindowMode.NonOwned ?
  280. secondaryWindow.FindElementByAccessibilityId("_XCUI:FullScreenWindow") :
  281. secondaryWindow.FindElementByAccessibilityId("_XCUI:ZoomWindow");
  282. Assert.False(zoomButton.Enabled);
  283. }
  284. }
  285. [PlatformFact(TestPlatforms.MacOS)]
  286. public void Toggling_SystemDecorations_Should_Preserve_ExtendClientArea()
  287. {
  288. // #10650
  289. using (OpenWindow(extendClientArea: true))
  290. {
  291. var secondaryWindow = GetWindow("SecondaryWindow");
  292. // The XPath of the title bar text _should_ be "XCUIElementTypeStaticText"
  293. // but Appium seems to put a fake node between the window and the title bar
  294. // https://stackoverflow.com/a/71914227/6448
  295. var titleBar = secondaryWindow.FindElementsByXPath("/*/XCUIElementTypeStaticText").Count;
  296. Assert.Equal(0, titleBar);
  297. secondaryWindow.FindElementByAccessibilityId("CurrentSystemDecorations").Click();
  298. _session.FindElementByAccessibilityId("SystemDecorationsNone").SendClick();
  299. secondaryWindow.FindElementByAccessibilityId("CurrentSystemDecorations").Click();
  300. _session.FindElementByAccessibilityId("SystemDecorationsFull").SendClick();
  301. titleBar = secondaryWindow.FindElementsByXPath("/*/XCUIElementTypeStaticText").Count;
  302. Assert.Equal(0, titleBar);
  303. }
  304. }
  305. [PlatformTheory(TestPlatforms.MacOS)]
  306. [InlineData(SystemDecorations.None)]
  307. [InlineData(SystemDecorations.BorderOnly)]
  308. [InlineData(SystemDecorations.Full)]
  309. public void ExtendClientArea_SystemDecorations_Shows_Correct_Buttons(SystemDecorations decorations)
  310. {
  311. // #10650
  312. using (OpenWindow(extendClientArea: true, systemDecorations: decorations))
  313. {
  314. var secondaryWindow = GetWindow("SecondaryWindow");
  315. try
  316. {
  317. var chrome = secondaryWindow.GetChromeButtons();
  318. if (decorations == SystemDecorations.Full)
  319. {
  320. Assert.NotNull(chrome.Close);
  321. Assert.NotNull(chrome.Minimize);
  322. Assert.NotNull(chrome.FullScreen);
  323. }
  324. else
  325. {
  326. Assert.Null(chrome.Close);
  327. Assert.Null(chrome.Minimize);
  328. Assert.Null(chrome.FullScreen);
  329. }
  330. }
  331. finally
  332. {
  333. if (decorations != SystemDecorations.Full)
  334. {
  335. secondaryWindow.FindElementByAccessibilityId("CurrentSystemDecorations").Click();
  336. _session.FindElementByAccessibilityId("SystemDecorationsFull").SendClick();
  337. }
  338. }
  339. }
  340. }
  341. private IDisposable OpenWindow(
  342. PixelSize? size = null,
  343. ShowWindowMode mode = ShowWindowMode.NonOwned,
  344. WindowStartupLocation location = WindowStartupLocation.Manual,
  345. bool canResize = true,
  346. SystemDecorations systemDecorations = SystemDecorations.Full,
  347. bool extendClientArea = false)
  348. {
  349. var sizeTextBox = _session.FindElementByAccessibilityId("ShowWindowSize");
  350. var modeComboBox = _session.FindElementByAccessibilityId("ShowWindowMode");
  351. var locationComboBox = _session.FindElementByAccessibilityId("ShowWindowLocation");
  352. var canResizeCheckBox = _session.FindElementByAccessibilityId("ShowWindowCanResize");
  353. var showButton = _session.FindElementByAccessibilityId("ShowWindow");
  354. var systemDecorationsComboBox = _session.FindElementByAccessibilityId("ShowWindowSystemDecorations");
  355. var extendClientAreaCheckBox = _session.FindElementByAccessibilityId("ShowWindowExtendClientAreaToDecorationsHint");
  356. if (size.HasValue)
  357. sizeTextBox.SendKeys($"{size.Value.Width}, {size.Value.Height}");
  358. if (modeComboBox.GetComboBoxValue() != mode.ToString())
  359. {
  360. modeComboBox.Click();
  361. _session.FindElementByName(mode.ToString()).SendClick();
  362. }
  363. if (locationComboBox.GetComboBoxValue() != location.ToString())
  364. {
  365. locationComboBox.Click();
  366. _session.FindElementByName(location.ToString()).SendClick();
  367. }
  368. if (canResizeCheckBox.GetIsChecked() != canResize)
  369. canResizeCheckBox.Click();
  370. if (systemDecorationsComboBox.GetComboBoxValue() != systemDecorations.ToString())
  371. {
  372. systemDecorationsComboBox.Click();
  373. _session.FindElementByName(systemDecorations.ToString()).SendClick();
  374. }
  375. if (extendClientAreaCheckBox.GetIsChecked() != extendClientArea)
  376. extendClientAreaCheckBox.Click();
  377. return showButton.OpenWindowWithClick();
  378. }
  379. private AppiumWebElement GetWindow(string identifier)
  380. {
  381. // The Avalonia a11y tree currently exposes two nested Window elements, this is a bug and should be fixed
  382. // but in the meantime use the `parent::' selector to return the parent "real" window.
  383. return _session.FindElementByXPath(
  384. $"XCUIElementTypeWindow//*[@identifier='{identifier}']/parent::XCUIElementTypeWindow");
  385. }
  386. private int GetWindowOrder(string identifier)
  387. {
  388. var window = GetWindow(identifier);
  389. var order = window.FindElementByXPath("//*[@identifier='CurrentOrder']");
  390. return int.Parse(order.Text);
  391. }
  392. public enum ShowWindowMode
  393. {
  394. NonOwned,
  395. Owned,
  396. Modal
  397. }
  398. }
  399. }