SelectorTests_Descendent.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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.Linq;
  5. using System.Reactive;
  6. using System.Reactive.Linq;
  7. using System.Threading.Tasks;
  8. using Avalonia.Collections;
  9. using Avalonia.Controls;
  10. using Avalonia.Data;
  11. using Avalonia.LogicalTree;
  12. using Xunit;
  13. namespace Avalonia.Styling.UnitTests
  14. {
  15. public class SelectorTests_Descendant
  16. {
  17. [Fact]
  18. public void Descendant_Matches_Control_When_It_Is_Child_OfType()
  19. {
  20. var parent = new TestLogical1();
  21. var child = new TestLogical2();
  22. child.LogicalParent = parent;
  23. var selector = default(Selector).OfType<TestLogical1>().Descendant().OfType<TestLogical2>();
  24. Assert.True(selector.Match(child).ImmediateResult);
  25. }
  26. [Fact]
  27. public void Descendant_Matches_Control_When_It_Is_Descendant_OfType()
  28. {
  29. var grandparent = new TestLogical1();
  30. var parent = new TestLogical2();
  31. var child = new TestLogical3();
  32. parent.LogicalParent = grandparent;
  33. child.LogicalParent = parent;
  34. var selector = default(Selector).OfType<TestLogical1>().Descendant().OfType<TestLogical3>();
  35. Assert.True(selector.Match(child).ImmediateResult);
  36. }
  37. [Fact]
  38. public async Task Descendant_Matches_Control_When_It_Is_Descendant_OfType_And_Class()
  39. {
  40. var grandparent = new TestLogical1();
  41. var parent = new TestLogical2();
  42. var child = new TestLogical3();
  43. grandparent.Classes.Add("foo");
  44. parent.LogicalParent = grandparent;
  45. child.LogicalParent = parent;
  46. var selector = default(Selector).OfType<TestLogical1>().Class("foo").Descendant().OfType<TestLogical3>();
  47. var activator = selector.Match(child).ObservableResult;
  48. Assert.True(await activator.Take(1));
  49. }
  50. [Fact]
  51. public async Task Descendant_Doesnt_Match_Control_When_It_Is_Descendant_OfType_But_Wrong_Class()
  52. {
  53. var grandparent = new TestLogical1();
  54. var parent = new TestLogical2();
  55. var child = new TestLogical3();
  56. grandparent.Classes.Add("bar");
  57. parent.LogicalParent = grandparent;
  58. parent.Classes.Add("foo");
  59. child.LogicalParent = parent;
  60. var selector = default(Selector).OfType<TestLogical1>().Class("foo").Descendant().OfType<TestLogical3>();
  61. var activator = selector.Match(child).ObservableResult;
  62. Assert.False(await activator.Take(1));
  63. }
  64. [Fact]
  65. public async Task Descendant_Matches_Any_Ancestor()
  66. {
  67. var grandparent = new TestLogical1();
  68. var parent = new TestLogical1();
  69. var child = new TestLogical3();
  70. parent.LogicalParent = grandparent;
  71. child.LogicalParent = parent;
  72. var selector = default(Selector).OfType<TestLogical1>().Class("foo").Descendant().OfType<TestLogical3>();
  73. var activator = selector.Match(child).ObservableResult;
  74. Assert.False(await activator.Take(1));
  75. parent.Classes.Add("foo");
  76. Assert.True(await activator.Take(1));
  77. grandparent.Classes.Add("foo");
  78. Assert.True(await activator.Take(1));
  79. parent.Classes.Remove("foo");
  80. Assert.True(await activator.Take(1));
  81. grandparent.Classes.Remove("foo");
  82. Assert.False(await activator.Take(1));
  83. }
  84. [Fact]
  85. public void Descendant_Selector_Should_Have_Correct_String_Representation()
  86. {
  87. var selector = default(Selector).OfType<TestLogical1>().Class("foo").Descendant().OfType<TestLogical3>();
  88. Assert.Equal("TestLogical1.foo TestLogical3", selector.ToString());
  89. }
  90. public abstract class TestLogical : ILogical, IStyleable
  91. {
  92. public TestLogical()
  93. {
  94. Classes = new Classes();
  95. }
  96. public event EventHandler<AvaloniaPropertyChangedEventArgs> PropertyChanged;
  97. public event EventHandler<LogicalTreeAttachmentEventArgs> AttachedToLogicalTree;
  98. public event EventHandler<LogicalTreeAttachmentEventArgs> DetachedFromLogicalTree;
  99. public Classes Classes { get; }
  100. public string Name { get; set; }
  101. public bool IsAttachedToLogicalTree { get; }
  102. public IAvaloniaReadOnlyList<ILogical> LogicalChildren { get; set; }
  103. public ILogical LogicalParent { get; set; }
  104. public Type StyleKey { get; }
  105. public ITemplatedControl TemplatedParent { get; }
  106. IAvaloniaReadOnlyList<string> IStyleable.Classes => Classes;
  107. IObservable<IStyleable> IStyleable.StyleDetach { get; }
  108. public object GetValue(AvaloniaProperty property)
  109. {
  110. throw new NotImplementedException();
  111. }
  112. public T GetValue<T>(AvaloniaProperty<T> property)
  113. {
  114. throw new NotImplementedException();
  115. }
  116. public void SetValue(AvaloniaProperty property, object value, BindingPriority priority)
  117. {
  118. throw new NotImplementedException();
  119. }
  120. public void SetValue<T>(AvaloniaProperty<T> property, T value, BindingPriority priority = BindingPriority.LocalValue)
  121. {
  122. throw new NotImplementedException();
  123. }
  124. public IDisposable Bind(AvaloniaProperty property, IObservable<object> source, BindingPriority priority = BindingPriority.LocalValue)
  125. {
  126. throw new NotImplementedException();
  127. }
  128. public bool IsSet(AvaloniaProperty property)
  129. {
  130. throw new NotImplementedException();
  131. }
  132. public IDisposable Bind<T>(AvaloniaProperty<T> property, IObservable<T> source, BindingPriority priority = BindingPriority.LocalValue)
  133. {
  134. throw new NotImplementedException();
  135. }
  136. public void NotifyAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e)
  137. {
  138. throw new NotImplementedException();
  139. }
  140. public void NotifyDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e)
  141. {
  142. throw new NotImplementedException();
  143. }
  144. public void NotifyResourcesChanged(ResourcesChangedEventArgs e)
  145. {
  146. throw new NotImplementedException();
  147. }
  148. }
  149. public class TestLogical1 : TestLogical
  150. {
  151. }
  152. public class TestLogical2 : TestLogical
  153. {
  154. }
  155. public class TestLogical3 : TestLogical
  156. {
  157. }
  158. }
  159. }