SelectorTests_Descendent.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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.Equal(SelectorMatchResult.AlwaysThisInstance, selector.Match(child).Result);
  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.Equal(SelectorMatchResult.AlwaysThisInstance, selector.Match(child).Result);
  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).Activator;
  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).Activator;
  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).Activator;
  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<AvaloniaPropertyChangedEventArgs> InheritablePropertyChanged;
  98. public event EventHandler<LogicalTreeAttachmentEventArgs> AttachedToLogicalTree;
  99. public event EventHandler<LogicalTreeAttachmentEventArgs> DetachedFromLogicalTree;
  100. public Classes Classes { get; }
  101. public string Name { get; set; }
  102. public bool IsAttachedToLogicalTree { get; }
  103. public IAvaloniaReadOnlyList<ILogical> LogicalChildren { get; set; }
  104. public ILogical LogicalParent { get; set; }
  105. public Type StyleKey { get; }
  106. public ITemplatedControl TemplatedParent { get; }
  107. IAvaloniaReadOnlyList<string> IStyleable.Classes => Classes;
  108. IObservable<IStyleable> IStyleable.StyleDetach { get; }
  109. public object GetValue(AvaloniaProperty property)
  110. {
  111. throw new NotImplementedException();
  112. }
  113. public T GetValue<T>(AvaloniaProperty<T> property)
  114. {
  115. throw new NotImplementedException();
  116. }
  117. public void SetValue(AvaloniaProperty property, object value, BindingPriority priority)
  118. {
  119. throw new NotImplementedException();
  120. }
  121. public void SetValue<T>(AvaloniaProperty<T> property, T value, BindingPriority priority = BindingPriority.LocalValue)
  122. {
  123. throw new NotImplementedException();
  124. }
  125. public IDisposable Bind(AvaloniaProperty property, IObservable<BindingValue<object>> source, BindingPriority priority = BindingPriority.LocalValue)
  126. {
  127. throw new NotImplementedException();
  128. }
  129. public bool IsAnimating(AvaloniaProperty property)
  130. {
  131. throw new NotImplementedException();
  132. }
  133. public bool IsSet(AvaloniaProperty property)
  134. {
  135. throw new NotImplementedException();
  136. }
  137. public IDisposable Bind<T>(AvaloniaProperty<T> property, IObservable<BindingValue<T>> source, BindingPriority priority = BindingPriority.LocalValue)
  138. {
  139. throw new NotImplementedException();
  140. }
  141. public void NotifyAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e)
  142. {
  143. throw new NotImplementedException();
  144. }
  145. public void NotifyDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e)
  146. {
  147. throw new NotImplementedException();
  148. }
  149. public void NotifyResourcesChanged(ResourcesChangedEventArgs e)
  150. {
  151. throw new NotImplementedException();
  152. }
  153. public void ClearValue(AvaloniaProperty property)
  154. {
  155. throw new NotImplementedException();
  156. }
  157. public void ClearValue<T>(AvaloniaProperty<T> property)
  158. {
  159. throw new NotImplementedException();
  160. }
  161. public void AddInheritanceChild(IAvaloniaObject child)
  162. {
  163. throw new NotImplementedException();
  164. }
  165. public void RemoveInheritanceChild(IAvaloniaObject child)
  166. {
  167. throw new NotImplementedException();
  168. }
  169. public void InheritanceParentChanged<T>(StyledPropertyBase<T> property, IAvaloniaObject oldParent, IAvaloniaObject newParent)
  170. {
  171. throw new NotImplementedException();
  172. }
  173. public void InheritedPropertyChanged<T>(AvaloniaProperty<T> property, Optional<T> oldValue, Optional<T> newValue)
  174. {
  175. throw new NotImplementedException();
  176. }
  177. public void ClearValue<T>(StyledPropertyBase<T> property)
  178. {
  179. throw new NotImplementedException();
  180. }
  181. public void ClearValue<T>(DirectPropertyBase<T> property)
  182. {
  183. throw new NotImplementedException();
  184. }
  185. public T GetValue<T>(StyledPropertyBase<T> property)
  186. {
  187. throw new NotImplementedException();
  188. }
  189. public T GetValue<T>(DirectPropertyBase<T> property)
  190. {
  191. throw new NotImplementedException();
  192. }
  193. public void SetValue<T>(StyledPropertyBase<T> property, T value, BindingPriority priority = BindingPriority.LocalValue)
  194. {
  195. throw new NotImplementedException();
  196. }
  197. public void SetValue<T>(DirectPropertyBase<T> property, T value)
  198. {
  199. throw new NotImplementedException();
  200. }
  201. public IDisposable Bind<T>(StyledPropertyBase<T> property, IObservable<BindingValue<T>> source, BindingPriority priority = BindingPriority.LocalValue)
  202. {
  203. throw new NotImplementedException();
  204. }
  205. public IDisposable Bind<T>(DirectPropertyBase<T> property, IObservable<BindingValue<T>> source)
  206. {
  207. throw new NotImplementedException();
  208. }
  209. }
  210. public class TestLogical1 : TestLogical
  211. {
  212. }
  213. public class TestLogical2 : TestLogical
  214. {
  215. }
  216. public class TestLogical3 : TestLogical
  217. {
  218. }
  219. }
  220. }