WindowTests_MacOS.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. using System;
  2. using System.Linq;
  3. using System.Threading;
  4. using Avalonia.Controls;
  5. using OpenQA.Selenium.Appium;
  6. using OpenQA.Selenium.Interactions;
  7. using Xunit;
  8. namespace Avalonia.IntegrationTests.Appium
  9. {
  10. [Collection("Default")]
  11. public class WindowTests_MacOS : TestBase
  12. {
  13. public WindowTests_MacOS(DefaultAppFixture fixture)
  14. : base(fixture, "Window")
  15. {
  16. }
  17. [PlatformFact(TestPlatforms.MacOS)]
  18. public void WindowOrder_Modal_Dialog_Stays_InFront_Of_Parent()
  19. {
  20. var mainWindow = Session.FindElementByAccessibilityId("MainWindow");
  21. using (OpenWindow(new PixelSize(200, 100), ShowWindowMode.Modal, WindowStartupLocation.Manual))
  22. {
  23. mainWindow.Click();
  24. var secondaryWindowIndex = GetWindowOrder("SecondaryWindow");
  25. Thread.Sleep(300); // sync with timer
  26. Assert.Equal(1, secondaryWindowIndex);
  27. }
  28. }
  29. [PlatformFact(TestPlatforms.MacOS)]
  30. public void WindowOrder_Modal_Dialog_Stays_InFront_Of_Parent_When_Clicking_Resize_Grip()
  31. {
  32. var mainWindow = GetWindow("MainWindow");
  33. using (OpenWindow(new PixelSize(200, 100), ShowWindowMode.Modal, WindowStartupLocation.Manual))
  34. {
  35. var childWindow = GetWindow("SecondaryWindow");
  36. new Actions(Session)
  37. .MoveToElement(childWindow, 100, 1)
  38. .ClickAndHold()
  39. .Perform();
  40. var secondaryWindowIndex = GetWindowOrder("SecondaryWindow");
  41. new Actions(Session)
  42. .MoveToElement(childWindow, 100, 1)
  43. .Release()
  44. .Perform();
  45. Assert.Equal(1, secondaryWindowIndex);
  46. }
  47. }
  48. [PlatformFact(TestPlatforms.MacOS)]
  49. public void WindowOrder_Modal_Dialog_Stays_InFront_Of_Parent_When_In_Fullscreen()
  50. {
  51. var mainWindow = GetWindow("MainWindow");
  52. var fullScreen = mainWindow.FindElementByAccessibilityId("_XCUI:FullScreenWindow");
  53. fullScreen.Click();
  54. Thread.Sleep(500);
  55. try
  56. {
  57. using (OpenWindow(new PixelSize(200, 100), ShowWindowMode.Modal, WindowStartupLocation.Manual))
  58. {
  59. var secondaryWindowIndex = GetWindowOrder("SecondaryWindow");
  60. Assert.Equal(1, secondaryWindowIndex);
  61. }
  62. }
  63. finally
  64. {
  65. Session.FindElementByAccessibilityId("ExitFullscreen").Click();
  66. }
  67. }
  68. [PlatformFact(TestPlatforms.MacOS)]
  69. public void WindowOrder_Owned_Dialog_Stays_InFront_Of_Parent()
  70. {
  71. var mainWindow = Session.FindElementByAccessibilityId("MainWindow");
  72. using (OpenWindow(new PixelSize(200, 100), ShowWindowMode.Owned, WindowStartupLocation.Manual))
  73. {
  74. mainWindow.SendClick();
  75. var secondaryWindowIndex = GetWindowOrder("SecondaryWindow");
  76. Assert.Equal(1, secondaryWindowIndex);
  77. }
  78. }
  79. [PlatformFact(TestPlatforms.MacOS)]
  80. public void WindowOrder_Owned_Dialog_Stays_InFront_Of_FullScreen_Parent()
  81. {
  82. var mainWindow = Session.FindElementByAccessibilityId("MainWindow");
  83. // Enter fullscreen
  84. mainWindow.FindElementByAccessibilityId("EnterFullscreen").Click();
  85. // Wait for fullscreen transition.
  86. Thread.Sleep(1000);
  87. // Make sure we entered fullscreen.
  88. var windowState = mainWindow.FindElementByAccessibilityId("MainWindowState");
  89. Assert.Equal("FullScreen", windowState.Text);
  90. // Open child window.
  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. // Exit fullscreen by menu shortcut Command+R
  98. mainWindow.FindElementByAccessibilityId("ExitFullscreen").Click();
  99. // Wait for restore transition.
  100. Thread.Sleep(1000);
  101. // Make sure we exited fullscreen.
  102. mainWindow = Session.FindElementByAccessibilityId("MainWindow");
  103. windowState = mainWindow.FindElementByAccessibilityId("MainWindowState");
  104. Assert.Equal("Normal", windowState.Text);
  105. }
  106. [PlatformFact(TestPlatforms.MacOS)]
  107. public void WindowOrder_Owned_Dialog_Stays_InFront_Of_Parent_After_Modal_Closed()
  108. {
  109. using (OpenWindow(new PixelSize(200, 300), ShowWindowMode.Owned, WindowStartupLocation.Manual))
  110. {
  111. OpenWindow(null, ShowWindowMode.Modal, WindowStartupLocation.Manual).Dispose();
  112. var secondaryWindowIndex = GetWindowOrder("SecondaryWindow");
  113. Assert.Equal(1, secondaryWindowIndex);
  114. }
  115. }
  116. [PlatformFact(TestPlatforms.MacOS, Skip = "Flaky test")]
  117. public void Does_Not_Switch_Space_From_FullScreen_To_Main_Desktop_When_FullScreen_Window_Clicked()
  118. {
  119. // Issue #9565
  120. var mainWindow = Session.FindElementByAccessibilityId("MainWindow");
  121. AppiumWebElement windowState;
  122. // Open child window.
  123. using (OpenWindow(new PixelSize(200, 100), ShowWindowMode.Owned, WindowStartupLocation.Manual))
  124. {
  125. // Enter fullscreen
  126. mainWindow.FindElementByAccessibilityId("EnterFullscreen").Click();
  127. // Wait for fullscreen transition.
  128. Thread.Sleep(1000);
  129. // Make sure we entered fullscreen.
  130. mainWindow = Session.FindElementByAccessibilityId("MainWindow");
  131. windowState = mainWindow.FindElementByAccessibilityId("MainWindowState");
  132. Assert.Equal("FullScreen", windowState.Text);
  133. // Click on main window
  134. mainWindow.Click();
  135. // Failed here due to #9565: main window is no longer visible as the main space is now shown instead
  136. // of the fullscreen space.
  137. mainWindow.FindElementByAccessibilityId("ExitFullscreen").Click();
  138. // Wait for restore transition.
  139. Thread.Sleep(1000);
  140. }
  141. // Make sure we exited fullscreen.
  142. mainWindow = Session.FindElementByAccessibilityId("MainWindow");
  143. windowState = mainWindow.FindElementByAccessibilityId("MainWindowState");
  144. Assert.Equal("Normal", windowState.Text);
  145. }
  146. [PlatformFact(TestPlatforms.MacOS)]
  147. public void WindowOrder_NonOwned_Window_Does_Not_Stay_InFront_Of_Parent()
  148. {
  149. var mainWindow = Session.FindElementByAccessibilityId("MainWindow");
  150. using (OpenWindow(new PixelSize(800, 100), ShowWindowMode.NonOwned, WindowStartupLocation.Manual))
  151. {
  152. mainWindow.Click();
  153. var secondaryWindowIndex = GetWindowOrder("SecondaryWindow");
  154. Assert.Equal(2, secondaryWindowIndex);
  155. var sendToBack = Session.FindElementByAccessibilityId("SendToBack");
  156. sendToBack.Click();
  157. }
  158. }
  159. [PlatformFact(TestPlatforms.MacOS)]
  160. public void WindowOrder_Owned_Is_Correct_After_Closing_Window()
  161. {
  162. using (OpenWindow(new PixelSize(300, 500), ShowWindowMode.Owned, WindowStartupLocation.CenterOwner))
  163. {
  164. // Open a second child window, and close it.
  165. using (OpenWindow(new PixelSize(200, 200), ShowWindowMode.Owned, WindowStartupLocation.CenterOwner))
  166. {
  167. }
  168. var secondaryWindowIndex = GetWindowOrder("SecondaryWindow");
  169. Assert.Equal(1, secondaryWindowIndex);
  170. }
  171. }
  172. [PlatformFact(TestPlatforms.MacOS)]
  173. public void Parent_Window_Has_Disabled_ChromeButtons_When_Modal_Dialog_Shown()
  174. {
  175. var window = GetWindow("MainWindow");
  176. var windowChrome = window.GetSystemChromeButtons();
  177. Assert.True(windowChrome.Close!.Enabled);
  178. Assert.True(windowChrome.FullScreen!.Enabled);
  179. Assert.True(windowChrome.Minimize!.Enabled);
  180. Assert.Null(windowChrome.Maximize);
  181. using (OpenWindow(new PixelSize(200, 100), ShowWindowMode.Modal, WindowStartupLocation.CenterOwner))
  182. {
  183. Assert.False(windowChrome.Close!.Enabled);
  184. Assert.False(windowChrome.FullScreen!.Enabled);
  185. Assert.False(windowChrome.Minimize!.Enabled);
  186. }
  187. }
  188. [PlatformFact(TestPlatforms.MacOS)]
  189. public void Minimize_Button_Is_Disabled_On_Modal_Dialog()
  190. {
  191. using (OpenWindow(new PixelSize(200, 100), ShowWindowMode.Modal, WindowStartupLocation.CenterOwner))
  192. {
  193. var secondaryWindow = GetWindow("SecondaryWindow");
  194. var windowChrome = secondaryWindow.GetSystemChromeButtons();
  195. Assert.True(windowChrome.Close!.Enabled);
  196. Assert.True(windowChrome.Maximize!.Enabled);
  197. Assert.False(windowChrome.Minimize!.Enabled);
  198. }
  199. }
  200. [PlatformTheory(TestPlatforms.MacOS)]
  201. [InlineData(ShowWindowMode.Owned)]
  202. public void Minimize_Button_Disabled_Owned_Window(ShowWindowMode mode)
  203. {
  204. using (OpenWindow(new PixelSize(200, 100), mode, WindowStartupLocation.Manual))
  205. {
  206. var secondaryWindow = GetWindow("SecondaryWindow");
  207. var miniaturizeButton = secondaryWindow.FindElementByAccessibilityId("_XCUI:MinimizeWindow");
  208. Assert.False(miniaturizeButton.Enabled);
  209. }
  210. }
  211. [PlatformTheory(TestPlatforms.MacOS)]
  212. [InlineData(ShowWindowMode.NonOwned)]
  213. public void Minimize_Button_Minimizes_Window(ShowWindowMode mode)
  214. {
  215. using (OpenWindow(new PixelSize(200, 100), mode, WindowStartupLocation.Manual))
  216. {
  217. var secondaryWindow = GetWindow("SecondaryWindow");
  218. var miniaturizeButton = secondaryWindow.FindElementByAccessibilityId("_XCUI:MinimizeWindow");
  219. miniaturizeButton.Click();
  220. Thread.Sleep(1000);
  221. var hittable = Session.FindElementsByXPath("/XCUIElementTypeApplication/XCUIElementTypeWindow")
  222. .Select(x => x.GetAttribute("hittable")).ToList();
  223. Assert.Equal(new[] { "true", "false" }, hittable);
  224. Session.FindElementByAccessibilityId("RestoreAll").Click();
  225. Thread.Sleep(1000);
  226. hittable = Session.FindElementsByXPath("/XCUIElementTypeApplication/XCUIElementTypeWindow")
  227. .Select(x => x.GetAttribute("hittable")).ToList();
  228. Assert.Equal(new[] { "true", "true" }, hittable);
  229. }
  230. }
  231. [PlatformFact(TestPlatforms.MacOS)]
  232. public void Hidden_Child_Window_Is_Not_Reshown_When_Parent_Clicked()
  233. {
  234. var mainWindow = Session.FindElementByAccessibilityId("MainWindow");
  235. // We don't use dispose to close the window here, because it seems that hiding and re-showing a window
  236. // causes Appium to think it's a different window.
  237. OpenWindow(null, ShowWindowMode.Owned, WindowStartupLocation.Manual);
  238. var secondaryWindow = GetWindow("SecondaryWindow");
  239. var hideButton = secondaryWindow.FindElementByAccessibilityId("HideButton");
  240. hideButton.Click();
  241. var windows = Session.FindElementsByXPath("XCUIElementTypeWindow");
  242. Assert.Single(windows);
  243. mainWindow.Click();
  244. windows = Session.FindElementsByXPath("XCUIElementTypeWindow");
  245. Assert.Single(windows);
  246. Session.FindElementByAccessibilityId("RestoreAll").Click();
  247. // Close the window manually.
  248. secondaryWindow = GetWindow("SecondaryWindow");
  249. secondaryWindow.FindElementByAccessibilityId("_XCUI:CloseWindow").Click();
  250. }
  251. [PlatformFact(TestPlatforms.MacOS)]
  252. public void Toggling_SystemDecorations_Should_Preserve_ExtendClientArea()
  253. {
  254. // #10650
  255. using (OpenWindow(extendClientArea: true))
  256. {
  257. var secondaryWindow = GetWindow("SecondaryWindow");
  258. // The XPath of the title bar text _should_ be "XCUIElementTypeStaticText"
  259. // but Appium seems to put a fake node between the window and the title bar
  260. // https://stackoverflow.com/a/71914227/6448
  261. var titleBar = secondaryWindow.FindElementsByXPath("/*/XCUIElementTypeStaticText").Count;
  262. Assert.Equal(0, titleBar);
  263. secondaryWindow.FindElementByAccessibilityId("CurrentSystemDecorations").Click();
  264. Session.FindElementByAccessibilityId("SystemDecorationsNone").SendClick();
  265. secondaryWindow.FindElementByAccessibilityId("CurrentSystemDecorations").Click();
  266. Session.FindElementByAccessibilityId("SystemDecorationsFull").SendClick();
  267. titleBar = secondaryWindow.FindElementsByXPath("/*/XCUIElementTypeStaticText").Count;
  268. Assert.Equal(0, titleBar);
  269. }
  270. }
  271. [PlatformTheory(TestPlatforms.MacOS)]
  272. [InlineData(SystemDecorations.None)]
  273. [InlineData(SystemDecorations.BorderOnly)]
  274. [InlineData(SystemDecorations.Full)]
  275. public void ExtendClientArea_SystemDecorations_Shows_Correct_Buttons(SystemDecorations decorations)
  276. {
  277. // #10650
  278. using (OpenWindow(extendClientArea: true, systemDecorations: decorations))
  279. {
  280. var secondaryWindow = GetWindow("SecondaryWindow");
  281. try
  282. {
  283. var chrome = secondaryWindow.GetSystemChromeButtons();
  284. if (decorations == SystemDecorations.Full)
  285. {
  286. Assert.NotNull(chrome.Close);
  287. Assert.NotNull(chrome.Minimize);
  288. Assert.NotNull(chrome.FullScreen);
  289. }
  290. else
  291. {
  292. Assert.Null(chrome.Close);
  293. Assert.Null(chrome.Minimize);
  294. Assert.Null(chrome.FullScreen);
  295. }
  296. }
  297. finally
  298. {
  299. if (decorations != SystemDecorations.Full)
  300. {
  301. secondaryWindow.FindElementByAccessibilityId("CurrentSystemDecorations").Click();
  302. Session.FindElementByAccessibilityId("SystemDecorationsFull").SendClick();
  303. }
  304. }
  305. }
  306. }
  307. private IDisposable OpenWindow(
  308. PixelSize? size = null,
  309. ShowWindowMode mode = ShowWindowMode.NonOwned,
  310. WindowStartupLocation location = WindowStartupLocation.Manual,
  311. bool canResize = true,
  312. SystemDecorations systemDecorations = SystemDecorations.Full,
  313. bool extendClientArea = false)
  314. {
  315. var sizeTextBox = Session.FindElementByAccessibilityId("ShowWindowSize");
  316. var modeComboBox = Session.FindElementByAccessibilityId("ShowWindowMode");
  317. var locationComboBox = Session.FindElementByAccessibilityId("ShowWindowLocation");
  318. var canResizeCheckBox = Session.FindElementByAccessibilityId("ShowWindowCanResize");
  319. var showButton = Session.FindElementByAccessibilityId("ShowWindow");
  320. var systemDecorationsComboBox = Session.FindElementByAccessibilityId("ShowWindowSystemDecorations");
  321. var extendClientAreaCheckBox = Session.FindElementByAccessibilityId("ShowWindowExtendClientAreaToDecorationsHint");
  322. if (size.HasValue)
  323. sizeTextBox.SendKeys($"{size.Value.Width}, {size.Value.Height}");
  324. if (modeComboBox.GetComboBoxValue() != mode.ToString())
  325. {
  326. modeComboBox.Click();
  327. Session.FindElementByName(mode.ToString()).SendClick();
  328. }
  329. if (locationComboBox.GetComboBoxValue() != location.ToString())
  330. {
  331. locationComboBox.Click();
  332. Session.FindElementByName(location.ToString()).SendClick();
  333. }
  334. if (canResizeCheckBox.GetIsChecked() != canResize)
  335. canResizeCheckBox.Click();
  336. if (systemDecorationsComboBox.GetComboBoxValue() != systemDecorations.ToString())
  337. {
  338. systemDecorationsComboBox.Click();
  339. Session.FindElementByName(systemDecorations.ToString()).SendClick();
  340. }
  341. if (extendClientAreaCheckBox.GetIsChecked() != extendClientArea)
  342. extendClientAreaCheckBox.Click();
  343. return showButton.OpenWindowWithClick();
  344. }
  345. private AppiumWebElement GetWindow(string identifier)
  346. {
  347. return Session.GetWindowById(identifier);
  348. }
  349. private int GetWindowOrder(string identifier)
  350. {
  351. var window = GetWindow(identifier);
  352. var order = window.FindElementByXPath("//*[@identifier='CurrentOrder']");
  353. return int.Parse(order.Text);
  354. }
  355. public enum ShowWindowMode
  356. {
  357. NonOwned,
  358. Owned,
  359. Modal
  360. }
  361. }
  362. }