ControlLocatorTests.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Reactive.Linq;
  6. using System.Threading.Tasks;
  7. using Avalonia.Controls;
  8. using Avalonia.UnitTests;
  9. using Xunit;
  10. namespace Avalonia.Markup.UnitTests
  11. {
  12. public class ControlLocatorTests
  13. {
  14. [Fact]
  15. public async Task Track_By_Name_Should_Find_Control_Added_Earlier()
  16. {
  17. TextBlock target;
  18. TextBlock relativeTo;
  19. var root = new TestRoot
  20. {
  21. Child = new StackPanel
  22. {
  23. Children = new Controls.Controls
  24. {
  25. (target = new TextBlock { Name = "target" }),
  26. (relativeTo = new TextBlock { Name = "start" }),
  27. }
  28. }
  29. };
  30. var locator = ControlLocator.Track(relativeTo, "target");
  31. var result = await locator.Take(1);
  32. Assert.Same(target, result);
  33. Assert.Equal(0, root.NameScopeRegisteredSubscribers);
  34. Assert.Equal(0, root.NameScopeUnregisteredSubscribers);
  35. }
  36. [Fact]
  37. public void Track_By_Name_Should_Find_Control_Added_Later()
  38. {
  39. StackPanel panel;
  40. TextBlock relativeTo;
  41. var root = new TestRoot
  42. {
  43. Child = (panel = new StackPanel
  44. {
  45. Children = new Controls.Controls
  46. {
  47. (relativeTo = new TextBlock
  48. {
  49. Name = "start"
  50. }),
  51. }
  52. })
  53. };
  54. var locator = ControlLocator.Track(relativeTo, "target");
  55. var target = new TextBlock { Name = "target" };
  56. var result = new List<IControl>();
  57. using (locator.Subscribe(x => result.Add(x)))
  58. {
  59. panel.Children.Add(target);
  60. }
  61. Assert.Equal(new[] { null, target }, result);
  62. Assert.Equal(0, root.NameScopeRegisteredSubscribers);
  63. Assert.Equal(0, root.NameScopeUnregisteredSubscribers);
  64. }
  65. [Fact]
  66. public void Track_By_Name_Should_Track_Removal_And_Readd()
  67. {
  68. StackPanel panel;
  69. TextBlock target;
  70. TextBlock relativeTo;
  71. var root = new TestRoot
  72. {
  73. Child = panel = new StackPanel
  74. {
  75. Children = new Controls.Controls
  76. {
  77. (target = new TextBlock { Name = "target" }),
  78. (relativeTo = new TextBlock { Name = "start" }),
  79. }
  80. }
  81. };
  82. var locator = ControlLocator.Track(relativeTo, "target");
  83. var result = new List<IControl>();
  84. locator.Subscribe(x => result.Add(x));
  85. var other = new TextBlock { Name = "target" };
  86. panel.Children.Remove(target);
  87. panel.Children.Add(other);
  88. Assert.Equal(new[] { target, null, other }, result);
  89. }
  90. [Fact]
  91. public void Track_By_Name_Should_Find_Control_When_Tree_Changed()
  92. {
  93. TextBlock target1;
  94. TextBlock target2;
  95. TextBlock relativeTo;
  96. var root1 = new TestRoot
  97. {
  98. Child = new StackPanel
  99. {
  100. Children = new Controls.Controls
  101. {
  102. (relativeTo = new TextBlock
  103. {
  104. Name = "start"
  105. }),
  106. (target1 = new TextBlock { Name = "target" }),
  107. }
  108. }
  109. };
  110. var root2 = new TestRoot
  111. {
  112. Child = new StackPanel
  113. {
  114. Children = new Controls.Controls
  115. {
  116. (target2 = new TextBlock { Name = "target" }),
  117. }
  118. }
  119. };
  120. var locator = ControlLocator.Track(relativeTo, "target");
  121. var target = new TextBlock { Name = "target" };
  122. var result = new List<IControl>();
  123. using (locator.Subscribe(x => result.Add(x)))
  124. {
  125. ((StackPanel)root1.Child).Children.Remove(relativeTo);
  126. ((StackPanel)root2.Child).Children.Add(relativeTo);
  127. }
  128. Assert.Equal(new[] { target1, null, target2 }, result);
  129. Assert.Equal(0, root1.NameScopeRegisteredSubscribers);
  130. Assert.Equal(0, root1.NameScopeUnregisteredSubscribers);
  131. }
  132. }
  133. }