WindowTests_MacOS.cs 18 KB

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