WindowTests.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System;
  2. using System.Linq;
  3. using System.Runtime.InteropServices;
  4. using Avalonia.Controls;
  5. using OpenQA.Selenium.Appium;
  6. using Xunit;
  7. using Xunit.Sdk;
  8. namespace Avalonia.IntegrationTests.Appium
  9. {
  10. [Collection("Default")]
  11. public class WindowTests
  12. {
  13. private readonly AppiumDriver<AppiumWebElement> _session;
  14. public WindowTests(TestAppFixture fixture)
  15. {
  16. _session = fixture.Session;
  17. var tabs = _session.FindElementByAccessibilityId("MainTabs");
  18. var tab = tabs.FindElementByName("Window");
  19. tab.Click();
  20. }
  21. [Theory]
  22. [MemberData(nameof(StartupLocationData))]
  23. public void StartupLocation(string? size, ShowWindowMode mode, WindowStartupLocation location)
  24. {
  25. var mainWindowHandle = GetCurrentWindowHandleHack();
  26. try
  27. {
  28. var sizeTextBox = _session.FindElementByAccessibilityId("ShowWindowSize");
  29. var modeComboBox = _session.FindElementByAccessibilityId("ShowWindowMode");
  30. var locationComboBox = _session.FindElementByAccessibilityId("ShowWindowLocation");
  31. var showButton = _session.FindElementByAccessibilityId("ShowWindow");
  32. if (size is not null)
  33. sizeTextBox.SendKeys(size);
  34. modeComboBox.Click();
  35. _session.FindElementByName(mode.ToString()).SendClick();
  36. locationComboBox.Click();
  37. _session.FindElementByName(location.ToString()).SendClick();
  38. showButton.Click();
  39. SwitchToNewWindowHack(oldWindowHandle: mainWindowHandle);
  40. var clientSize = Size.Parse(_session.FindElementByAccessibilityId("ClientSize").Text);
  41. var frameSize = Size.Parse(_session.FindElementByAccessibilityId("FrameSize").Text);
  42. var position = PixelPoint.Parse(_session.FindElementByAccessibilityId("Position").Text);
  43. var screenRect = PixelRect.Parse(_session.FindElementByAccessibilityId("ScreenRect").Text);
  44. var scaling = double.Parse(_session.FindElementByAccessibilityId("Scaling").Text);
  45. Assert.True(frameSize.Width >= clientSize.Width, "Expected frame width >= client width.");
  46. Assert.True(frameSize.Height > clientSize.Height, "Expected frame height > client height.");
  47. var frameRect = new PixelRect(position, PixelSize.FromSize(frameSize, scaling));
  48. switch (location)
  49. {
  50. case WindowStartupLocation.CenterScreen:
  51. {
  52. var expected = screenRect.CenterRect(frameRect);
  53. AssertCloseEnough(expected.Position, frameRect.Position);
  54. break;
  55. }
  56. }
  57. }
  58. finally
  59. {
  60. try
  61. {
  62. var closeButton = _session.FindElementByAccessibilityId("CloseWindow");
  63. closeButton.Click();
  64. SwitchToMainWindowHack(mainWindowHandle);
  65. }
  66. catch { }
  67. }
  68. }
  69. public static TheoryData<string?, ShowWindowMode, WindowStartupLocation> StartupLocationData()
  70. {
  71. var sizes = new[] { null, "400,300" };
  72. var data = new TheoryData<string?, ShowWindowMode, WindowStartupLocation>();
  73. foreach (var size in sizes)
  74. {
  75. foreach (var mode in Enum.GetValues<ShowWindowMode>())
  76. {
  77. foreach (var location in Enum.GetValues<WindowStartupLocation>())
  78. {
  79. if (!(location == WindowStartupLocation.CenterOwner && mode == ShowWindowMode.NonOwned))
  80. {
  81. data.Add(size, mode, location);
  82. }
  83. }
  84. }
  85. }
  86. return data;
  87. }
  88. private string? GetCurrentWindowHandleHack()
  89. {
  90. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  91. {
  92. // HACK: WinAppDriver only seems to switch to a newly opened window if the window has an owner,
  93. // otherwise the session remains targeting the previous window. Return the handle for the
  94. // current window so we know which window to switch to when another is opened.
  95. return _session.WindowHandles.Single();
  96. }
  97. return null;
  98. }
  99. private void SwitchToNewWindowHack(string? oldWindowHandle)
  100. {
  101. if (oldWindowHandle is not null)
  102. {
  103. var newWindowHandle = _session.WindowHandles.FirstOrDefault(x => x != oldWindowHandle);
  104. // HACK: Looks like WinAppDriver only adds window handles for non-owned windows, but luckily
  105. // non-owned windows is where we're having the problem, so if we find a window handle that
  106. // isn't the main window handle then switch to it.
  107. if (newWindowHandle is not null)
  108. _session.SwitchTo().Window(newWindowHandle);
  109. }
  110. }
  111. private void SwitchToMainWindowHack(string? mainWindowHandle)
  112. {
  113. if (mainWindowHandle is not null)
  114. _session.SwitchTo().Window(mainWindowHandle);
  115. }
  116. private static void AssertCloseEnough(PixelPoint expected, PixelPoint actual)
  117. {
  118. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  119. {
  120. // On win32, accurate frame information cannot be obtained until a window is shown but
  121. // WindowStartupLocation needs to be calculated before the window is shown, meaning that
  122. // the position of a centered window can be off by a bit. From initial testing, looks
  123. // like this shouldn't be more than 10 pixels.
  124. if (Math.Abs(expected.X - actual.X) > 10)
  125. throw new EqualException(expected, actual);
  126. if (Math.Abs(expected.Y - actual.Y) > 10)
  127. throw new EqualException(expected, actual);
  128. }
  129. else
  130. {
  131. Assert.Equal(expected, actual);
  132. }
  133. }
  134. public enum ShowWindowMode
  135. {
  136. NonOwned,
  137. Owned,
  138. Modal
  139. }
  140. }
  141. }