ControlAutomationPeerTests.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using System;
  2. using System.Linq;
  3. using Avalonia.Automation.Peers;
  4. using Avalonia.Automation.Provider;
  5. using Avalonia.Controls.Presenters;
  6. using Avalonia.Controls.Templates;
  7. using Avalonia.Platform;
  8. using Avalonia.UnitTests;
  9. using Avalonia.VisualTree;
  10. using Xunit;
  11. #nullable enable
  12. namespace Avalonia.Controls.UnitTests.Automation
  13. {
  14. public class ControlAutomationPeerTests
  15. {
  16. public class Children
  17. {
  18. [Fact]
  19. public void Creates_Children_For_Controls_In_Visual_Tree()
  20. {
  21. var panel = new Panel
  22. {
  23. Children =
  24. {
  25. new Border(),
  26. new Border(),
  27. },
  28. };
  29. var target = CreatePeer(panel);
  30. Assert.Equal(
  31. panel.GetVisualChildren(),
  32. target.GetChildren().Cast<ControlAutomationPeer>().Select(x => x.Owner));
  33. }
  34. [Fact]
  35. public void Creates_Children_when_Controls_Attached_To_Visual_Tree()
  36. {
  37. var contentControl = new ContentControl
  38. {
  39. Template = new FuncControlTemplate<ContentControl>((o, ns) =>
  40. new ContentPresenter
  41. {
  42. Name = "PART_ContentPresenter",
  43. [!ContentPresenter.ContentProperty] = o[!ContentControl.ContentProperty],
  44. }),
  45. Content = new Border(),
  46. };
  47. var target = CreatePeer(contentControl);
  48. Assert.Empty(target.GetChildren());
  49. contentControl.Measure(Size.Infinity);
  50. Assert.Equal(1, target.GetChildren().Count);
  51. }
  52. [Fact]
  53. public void Updates_Children_When_VisualChildren_Added()
  54. {
  55. var panel = new Panel
  56. {
  57. Children =
  58. {
  59. new Border(),
  60. new Border(),
  61. },
  62. };
  63. var target = CreatePeer(panel);
  64. var children = target.GetChildren();
  65. Assert.Equal(2, children.Count);
  66. panel.Children.Add(new Decorator());
  67. children = target.GetChildren();
  68. Assert.Equal(3, children.Count);
  69. }
  70. [Fact]
  71. public void Updates_Children_When_VisualChildren_Removed()
  72. {
  73. var panel = new Panel
  74. {
  75. Children =
  76. {
  77. new Border(),
  78. new Border(),
  79. },
  80. };
  81. var target = CreatePeer(panel);
  82. var children = target.GetChildren();
  83. Assert.Equal(2, children.Count);
  84. panel.Children.RemoveAt(1);
  85. children = target.GetChildren();
  86. Assert.Equal(1, children.Count);
  87. }
  88. [Fact]
  89. public void Updates_Children_When_Visibility_Changes()
  90. {
  91. var panel = new Panel
  92. {
  93. Children =
  94. {
  95. new Border(),
  96. new Border(),
  97. },
  98. };
  99. var target = CreatePeer(panel);
  100. var children = target.GetChildren();
  101. Assert.Equal(2, children.Count);
  102. panel.Children[1].IsVisible = false;
  103. children = target.GetChildren();
  104. Assert.Equal(1, children.Count);
  105. panel.Children[1].IsVisible = true;
  106. children = target.GetChildren();
  107. Assert.Equal(2, children.Count);
  108. }
  109. }
  110. public class Parent
  111. {
  112. [Fact]
  113. public void Connects_Peer_To_Tree_When_GetParent_Called()
  114. {
  115. var border = new Border();
  116. var tree = new Decorator
  117. {
  118. Child = new Decorator
  119. {
  120. Child = border,
  121. }
  122. };
  123. // We're accessing Border directly without going via its ancestors. Because the tree
  124. // is built lazily, ensure that calling GetParent causes the ancestor tree to be built.
  125. var target = CreatePeer(border);
  126. var parentPeer = Assert.IsAssignableFrom<ControlAutomationPeer>(target.GetParent());
  127. Assert.Same(border.GetVisualParent(), parentPeer.Owner);
  128. }
  129. [Fact]
  130. public void Parent_Updated_When_Moved_To_Separate_Visual_Tree()
  131. {
  132. var border = new Border();
  133. var root1 = new Decorator { Child = border };
  134. var root2 = new Decorator();
  135. var target = CreatePeer(border);
  136. var parentPeer = Assert.IsAssignableFrom<ControlAutomationPeer>(target.GetParent());
  137. Assert.Same(root1, parentPeer.Owner);
  138. root1.Child = null;
  139. Assert.Null(target.GetParent());
  140. root2.Child = border;
  141. parentPeer = Assert.IsAssignableFrom<ControlAutomationPeer>(target.GetParent());
  142. Assert.Same(root2, parentPeer.Owner);
  143. }
  144. }
  145. private static AutomationPeer CreatePeer(Control control)
  146. {
  147. return ControlAutomationPeer.CreatePeerForElement(control);
  148. }
  149. private class TestControl : Control
  150. {
  151. protected override AutomationPeer OnCreateAutomationPeer()
  152. {
  153. return new TestAutomationPeer(this);
  154. }
  155. }
  156. private class AutomationTestRoot : TestRoot
  157. {
  158. protected override AutomationPeer OnCreateAutomationPeer()
  159. {
  160. return new TestRootAutomationPeer(this);
  161. }
  162. }
  163. private class TestAutomationPeer : ControlAutomationPeer
  164. {
  165. public TestAutomationPeer( Control owner)
  166. : base(owner)
  167. {
  168. }
  169. }
  170. private class TestRootAutomationPeer : ControlAutomationPeer, IRootProvider
  171. {
  172. public TestRootAutomationPeer(Control owner)
  173. : base(owner)
  174. {
  175. }
  176. public ITopLevelImpl PlatformImpl => throw new System.NotImplementedException();
  177. public event EventHandler? FocusChanged;
  178. public AutomationPeer GetFocus()
  179. {
  180. throw new System.NotImplementedException();
  181. }
  182. public AutomationPeer GetPeerFromPoint(Point p)
  183. {
  184. throw new System.NotImplementedException();
  185. }
  186. }
  187. }
  188. }