ControlLocatorTests.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.Markup.Data;
  8. using Xunit;
  9. namespace Perspex.Markup.UnitTests.Data
  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_Find_Control_When_Tree_Changed()
  66. {
  67. TextBlock target1;
  68. TextBlock target2;
  69. TextBlock relativeTo;
  70. var root1 = new TestRoot
  71. {
  72. Child = new StackPanel
  73. {
  74. Children = new Controls.Controls
  75. {
  76. (relativeTo = new TextBlock
  77. {
  78. Name = "start"
  79. }),
  80. (target1 = new TextBlock { Name = "target" }),
  81. }
  82. }
  83. };
  84. var root2 = new TestRoot
  85. {
  86. Child = new StackPanel
  87. {
  88. Children = new Controls.Controls
  89. {
  90. (target2 = new TextBlock { Name = "target" }),
  91. }
  92. }
  93. };
  94. var locator = ControlLocator.Track(relativeTo, "target");
  95. var target = new TextBlock { Name = "target" };
  96. var result = new List<IControl>();
  97. using (locator.Subscribe(x => result.Add(x)))
  98. {
  99. ((StackPanel)root1.Child).Children.Remove(relativeTo);
  100. ((StackPanel)root2.Child).Children.Add(relativeTo);
  101. }
  102. Assert.Equal(new[] { target1, null, target2 }, result);
  103. Assert.Equal(0, root1.NameScopeRegisteredSubscribers);
  104. Assert.Equal(0, root1.NameScopeUnregisteredSubscribers);
  105. }
  106. }
  107. }