SelectorTests_Child.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reactive;
  5. using System.Reactive.Linq;
  6. using System.Threading.Tasks;
  7. using Avalonia.Collections;
  8. using Avalonia.Controls;
  9. using Avalonia.Data;
  10. using Avalonia.LogicalTree;
  11. using Xunit;
  12. namespace Avalonia.Styling.UnitTests
  13. {
  14. public class SelectorTests_Child
  15. {
  16. [Fact]
  17. public void Child_Matches_Control_When_It_Is_Child_OfType()
  18. {
  19. var parent = new TestLogical1();
  20. var child = new TestLogical2();
  21. child.LogicalParent = parent;
  22. var selector = default(Selector).OfType<TestLogical1>().Child().OfType<TestLogical2>();
  23. Assert.Equal(SelectorMatchResult.AlwaysThisInstance, selector.Match(child).Result);
  24. }
  25. [Fact]
  26. public void Child_Doesnt_Match_Control_When_It_Is_Grandchild_OfType()
  27. {
  28. var grandparent = new TestLogical1();
  29. var parent = new TestLogical2();
  30. var child = new TestLogical3();
  31. parent.LogicalParent = grandparent;
  32. child.LogicalParent = parent;
  33. var selector = default(Selector).OfType<TestLogical1>().Child().OfType<TestLogical3>();
  34. Assert.Equal(SelectorMatchResult.NeverThisInstance, selector.Match(child).Result);
  35. }
  36. [Fact]
  37. public async Task Child_Matches_Control_When_It_Is_Child_OfType_And_Class()
  38. {
  39. var parent = new TestLogical1();
  40. var child = new TestLogical2();
  41. child.LogicalParent = parent;
  42. var selector = default(Selector).OfType<TestLogical1>().Class("foo").Child().OfType<TestLogical2>();
  43. var activator = selector.Match(child).Activator;
  44. var result = new List<bool>();
  45. Assert.False(await activator.Take(1));
  46. parent.Classes.Add("foo");
  47. Assert.True(await activator.Take(1));
  48. parent.Classes.Remove("foo");
  49. Assert.False(await activator.Take(1));
  50. }
  51. [Fact]
  52. public void Child_Doesnt_Match_Control_When_It_Has_No_Parent()
  53. {
  54. var control = new TestLogical3();
  55. var selector = default(Selector).OfType<TestLogical1>().Child().OfType<TestLogical3>();
  56. Assert.Equal(SelectorMatchResult.NeverThisInstance, selector.Match(control).Result);
  57. }
  58. [Fact]
  59. public void Child_Selector_Should_Have_Correct_String_Representation()
  60. {
  61. var selector = default(Selector).OfType<TestLogical1>().Child().OfType<TestLogical3>();
  62. Assert.Equal("TestLogical1 > TestLogical3", selector.ToString());
  63. }
  64. public abstract class TestLogical : Control
  65. {
  66. public ILogical LogicalParent
  67. {
  68. get => Parent;
  69. set => ((ISetLogicalParent)this).SetParent(value);
  70. }
  71. public void ClearValue(AvaloniaProperty property)
  72. {
  73. throw new NotImplementedException();
  74. }
  75. public void ClearValue<T>(AvaloniaProperty<T> property)
  76. {
  77. throw new NotImplementedException();
  78. }
  79. public void AddInheritanceChild(IAvaloniaObject child)
  80. {
  81. throw new NotImplementedException();
  82. }
  83. public void RemoveInheritanceChild(IAvaloniaObject child)
  84. {
  85. throw new NotImplementedException();
  86. }
  87. public void InheritanceParentChanged<T>(StyledPropertyBase<T> property, IAvaloniaObject oldParent, IAvaloniaObject newParent)
  88. {
  89. throw new NotImplementedException();
  90. }
  91. public void InheritedPropertyChanged<T>(AvaloniaProperty<T> property, Optional<T> oldValue, Optional<T> newValue)
  92. {
  93. throw new NotImplementedException();
  94. }
  95. public void ClearValue<T>(StyledPropertyBase<T> property)
  96. {
  97. throw new NotImplementedException();
  98. }
  99. public void ClearValue<T>(DirectPropertyBase<T> property)
  100. {
  101. throw new NotImplementedException();
  102. }
  103. public T GetValue<T>(StyledPropertyBase<T> property)
  104. {
  105. throw new NotImplementedException();
  106. }
  107. public T GetValue<T>(DirectPropertyBase<T> property)
  108. {
  109. throw new NotImplementedException();
  110. }
  111. public void SetValue<T>(StyledPropertyBase<T> property, T value, BindingPriority priority = BindingPriority.LocalValue)
  112. {
  113. throw new NotImplementedException();
  114. }
  115. public void SetValue<T>(DirectPropertyBase<T> property, T value)
  116. {
  117. throw new NotImplementedException();
  118. }
  119. public IDisposable Bind<T>(StyledPropertyBase<T> property, IObservable<BindingValue<T>> source, BindingPriority priority = BindingPriority.LocalValue)
  120. {
  121. throw new NotImplementedException();
  122. }
  123. public IDisposable Bind<T>(DirectPropertyBase<T> property, IObservable<BindingValue<T>> source)
  124. {
  125. throw new NotImplementedException();
  126. }
  127. }
  128. public class TestLogical1 : TestLogical
  129. {
  130. }
  131. public class TestLogical2 : TestLogical
  132. {
  133. }
  134. public class TestLogical3 : TestLogical
  135. {
  136. }
  137. }
  138. }