SelectorTests_Descendent.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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<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<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. }
  154. public class TestLogical1 : TestLogical
  155. {
  156. }
  157. public class TestLogical2 : TestLogical
  158. {
  159. }
  160. public class TestLogical3 : TestLogical
  161. {
  162. }
  163. }
  164. }