ControlLocatorTests.cs 4.8 KB

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