SelectorTests_Child.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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.Linq;
  6. using System.Reactive;
  7. using System.Reactive.Linq;
  8. using System.Threading.Tasks;
  9. using Avalonia.Collections;
  10. using Avalonia.Controls;
  11. using Avalonia.Data;
  12. using Avalonia.LogicalTree;
  13. using Xunit;
  14. namespace Avalonia.Styling.UnitTests
  15. {
  16. public class SelectorTests_Child
  17. {
  18. [Fact]
  19. public void Child_Matches_Control_When_It_Is_Child_OfType()
  20. {
  21. var parent = new TestLogical1();
  22. var child = new TestLogical2();
  23. child.LogicalParent = parent;
  24. var selector = default(Selector).OfType<TestLogical1>().Child().OfType<TestLogical2>();
  25. Assert.Equal(SelectorMatchResult.AlwaysThisInstance, selector.Match(child).Result);
  26. }
  27. [Fact]
  28. public void Child_Doesnt_Match_Control_When_It_Is_Grandchild_OfType()
  29. {
  30. var grandparent = new TestLogical1();
  31. var parent = new TestLogical2();
  32. var child = new TestLogical3();
  33. parent.LogicalParent = grandparent;
  34. child.LogicalParent = parent;
  35. var selector = default(Selector).OfType<TestLogical1>().Child().OfType<TestLogical3>();
  36. Assert.Equal(SelectorMatchResult.NeverThisInstance, selector.Match(child).Result);
  37. }
  38. [Fact]
  39. public async Task Child_Matches_Control_When_It_Is_Child_OfType_And_Class()
  40. {
  41. var parent = new TestLogical1();
  42. var child = new TestLogical2();
  43. child.LogicalParent = parent;
  44. var selector = default(Selector).OfType<TestLogical1>().Class("foo").Child().OfType<TestLogical2>();
  45. var activator = selector.Match(child).Activator;
  46. var result = new List<bool>();
  47. Assert.False(await activator.Take(1));
  48. parent.Classes.Add("foo");
  49. Assert.True(await activator.Take(1));
  50. parent.Classes.Remove("foo");
  51. Assert.False(await activator.Take(1));
  52. }
  53. [Fact]
  54. public void Child_Doesnt_Match_Control_When_It_Has_No_Parent()
  55. {
  56. var control = new TestLogical3();
  57. var selector = default(Selector).OfType<TestLogical1>().Child().OfType<TestLogical3>();
  58. Assert.Equal(SelectorMatchResult.NeverThisInstance, selector.Match(control).Result);
  59. }
  60. [Fact]
  61. public void Child_Selector_Should_Have_Correct_String_Representation()
  62. {
  63. var selector = default(Selector).OfType<TestLogical1>().Child().OfType<TestLogical3>();
  64. Assert.Equal("TestLogical1 > TestLogical3", selector.ToString());
  65. }
  66. public abstract class TestLogical : ILogical, IStyleable
  67. {
  68. public TestLogical()
  69. {
  70. Classes = new Classes();
  71. }
  72. public event EventHandler<AvaloniaPropertyChangedEventArgs> PropertyChanged;
  73. public event EventHandler<AvaloniaPropertyChangedEventArgs> InheritablePropertyChanged;
  74. public event EventHandler<LogicalTreeAttachmentEventArgs> AttachedToLogicalTree;
  75. public event EventHandler<LogicalTreeAttachmentEventArgs> DetachedFromLogicalTree;
  76. public Classes Classes { get; }
  77. public string Name { get; set; }
  78. public bool IsAttachedToLogicalTree { get; }
  79. public IAvaloniaReadOnlyList<ILogical> LogicalChildren { get; set; }
  80. public ILogical LogicalParent { get; set; }
  81. public Type StyleKey { get; }
  82. public ITemplatedControl TemplatedParent { get; }
  83. IObservable<IStyleable> IStyleable.StyleDetach { get; }
  84. IAvaloniaReadOnlyList<string> IStyleable.Classes => Classes;
  85. public object GetValue(AvaloniaProperty property)
  86. {
  87. throw new NotImplementedException();
  88. }
  89. public T GetValue<T>(AvaloniaProperty<T> property)
  90. {
  91. throw new NotImplementedException();
  92. }
  93. public void SetValue(AvaloniaProperty property, object value, BindingPriority priority)
  94. {
  95. throw new NotImplementedException();
  96. }
  97. public void SetValue<T>(AvaloniaProperty<T> property, T value, BindingPriority priority = BindingPriority.LocalValue)
  98. {
  99. throw new NotImplementedException();
  100. }
  101. public IDisposable Bind(AvaloniaProperty property, IObservable<BindingValue<object>> source, BindingPriority priority)
  102. {
  103. throw new NotImplementedException();
  104. }
  105. public bool IsAnimating(AvaloniaProperty property)
  106. {
  107. throw new NotImplementedException();
  108. }
  109. public bool IsSet(AvaloniaProperty property)
  110. {
  111. throw new NotImplementedException();
  112. }
  113. public IDisposable Bind<T>(AvaloniaProperty<T> property, IObservable<BindingValue<T>> source, BindingPriority priority = BindingPriority.LocalValue)
  114. {
  115. throw new NotImplementedException();
  116. }
  117. public void NotifyAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e)
  118. {
  119. throw new NotImplementedException();
  120. }
  121. public void NotifyDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e)
  122. {
  123. throw new NotImplementedException();
  124. }
  125. public void NotifyResourcesChanged(ResourcesChangedEventArgs e)
  126. {
  127. throw new NotImplementedException();
  128. }
  129. public void ClearValue(AvaloniaProperty property)
  130. {
  131. throw new NotImplementedException();
  132. }
  133. public void ClearValue<T>(AvaloniaProperty<T> property)
  134. {
  135. throw new NotImplementedException();
  136. }
  137. public void AddInheritanceChild(IAvaloniaObject child)
  138. {
  139. throw new NotImplementedException();
  140. }
  141. public void RemoveInheritanceChild(IAvaloniaObject child)
  142. {
  143. throw new NotImplementedException();
  144. }
  145. public void InheritanceParentChanged<T>(StyledPropertyBase<T> property, IAvaloniaObject oldParent, IAvaloniaObject newParent)
  146. {
  147. throw new NotImplementedException();
  148. }
  149. public void InheritedPropertyChanged<T>(AvaloniaProperty<T> property, Optional<T> oldValue, Optional<T> newValue)
  150. {
  151. throw new NotImplementedException();
  152. }
  153. public void ClearValue<T>(StyledPropertyBase<T> property)
  154. {
  155. throw new NotImplementedException();
  156. }
  157. public void ClearValue<T>(DirectPropertyBase<T> property)
  158. {
  159. throw new NotImplementedException();
  160. }
  161. public T GetValue<T>(StyledPropertyBase<T> property)
  162. {
  163. throw new NotImplementedException();
  164. }
  165. public T GetValue<T>(DirectPropertyBase<T> property)
  166. {
  167. throw new NotImplementedException();
  168. }
  169. public void SetValue<T>(StyledPropertyBase<T> property, T value, BindingPriority priority = BindingPriority.LocalValue)
  170. {
  171. throw new NotImplementedException();
  172. }
  173. public void SetValue<T>(DirectPropertyBase<T> property, T value)
  174. {
  175. throw new NotImplementedException();
  176. }
  177. public IDisposable Bind<T>(StyledPropertyBase<T> property, IObservable<BindingValue<T>> source, BindingPriority priority = BindingPriority.LocalValue)
  178. {
  179. throw new NotImplementedException();
  180. }
  181. public IDisposable Bind<T>(DirectPropertyBase<T> property, IObservable<BindingValue<T>> source)
  182. {
  183. throw new NotImplementedException();
  184. }
  185. }
  186. public class TestLogical1 : TestLogical
  187. {
  188. }
  189. public class TestLogical2 : TestLogical
  190. {
  191. }
  192. public class TestLogical3 : TestLogical
  193. {
  194. }
  195. }
  196. }