SelectorTests_Descendent.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Copyright (c) The Perspex 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 Perspex.Collections;
  9. using Perspex.Controls;
  10. using Xunit;
  11. namespace Perspex.Styling.UnitTests
  12. {
  13. public class SelectorTests_Descendent
  14. {
  15. [Fact]
  16. public void Descendent_Matches_Control_When_It_Is_Child_OfType()
  17. {
  18. var parent = new TestLogical1();
  19. var child = new TestLogical2();
  20. child.LogicalParent = parent;
  21. var selector = new Selector().OfType<TestLogical1>().Descendent().OfType<TestLogical2>();
  22. Assert.True(selector.Match(child).ImmediateResult);
  23. }
  24. [Fact]
  25. public void Descendent_Matches_Control_When_It_Is_Descendent_OfType()
  26. {
  27. var grandparent = new TestLogical1();
  28. var parent = new TestLogical2();
  29. var child = new TestLogical3();
  30. parent.LogicalParent = grandparent;
  31. child.LogicalParent = parent;
  32. var selector = new Selector().OfType<TestLogical1>().Descendent().OfType<TestLogical3>();
  33. Assert.True(selector.Match(child).ImmediateResult);
  34. }
  35. [Fact]
  36. public async Task Descendent_Matches_Control_When_It_Is_Descendent_OfType_And_Class()
  37. {
  38. var grandparent = new TestLogical1();
  39. var parent = new TestLogical2();
  40. var child = new TestLogical3();
  41. grandparent.Classes.Add("foo");
  42. parent.LogicalParent = grandparent;
  43. child.LogicalParent = parent;
  44. var selector = new Selector().OfType<TestLogical1>().Class("foo").Descendent().OfType<TestLogical3>();
  45. var activator = selector.Match(child).ObservableResult;
  46. Assert.True(await activator.Take(1));
  47. }
  48. [Fact]
  49. public async Task Descendent_Doesnt_Match_Control_When_It_Is_Descendent_OfType_But_Wrong_Class()
  50. {
  51. var grandparent = new TestLogical1();
  52. var parent = new TestLogical2();
  53. var child = new TestLogical3();
  54. grandparent.Classes.Add("bar");
  55. parent.LogicalParent = grandparent;
  56. parent.Classes.Add("foo");
  57. child.LogicalParent = parent;
  58. var selector = new Selector().OfType<TestLogical1>().Class("foo").Descendent().OfType<TestLogical3>();
  59. var activator = selector.Match(child).ObservableResult;
  60. Assert.False(await activator.Take(1));
  61. }
  62. [Fact]
  63. public async Task Descendent_Matches_Any_Ancestor()
  64. {
  65. var grandparent = new TestLogical1();
  66. var parent = new TestLogical1();
  67. var child = new TestLogical3();
  68. parent.LogicalParent = grandparent;
  69. child.LogicalParent = parent;
  70. var selector = new Selector().OfType<TestLogical1>().Class("foo").Descendent().OfType<TestLogical3>();
  71. var activator = selector.Match(child).ObservableResult;
  72. Assert.False(await activator.Take(1));
  73. parent.Classes.Add("foo");
  74. Assert.True(await activator.Take(1));
  75. grandparent.Classes.Add("foo");
  76. Assert.True(await activator.Take(1));
  77. parent.Classes.Remove("foo");
  78. Assert.True(await activator.Take(1));
  79. grandparent.Classes.Remove("foo");
  80. Assert.False(await activator.Take(1));
  81. }
  82. public abstract class TestLogical : ILogical, IStyleable
  83. {
  84. public TestLogical()
  85. {
  86. Classes = new Classes();
  87. }
  88. public event EventHandler<PerspexPropertyChangedEventArgs> PropertyChanged;
  89. public Classes Classes { get; }
  90. public string Name { get; set; }
  91. public bool IsAttachedToLogicalTree { get; }
  92. public IPerspexReadOnlyList<ILogical> LogicalChildren { get; set; }
  93. public ILogical LogicalParent { get; set; }
  94. public Type StyleKey { get; }
  95. public ITemplatedControl TemplatedParent { get; }
  96. IPerspexReadOnlyList<string> IStyleable.Classes => Classes;
  97. IObservable<Unit> IStyleable.StyleDetach { get; }
  98. public object GetValue(PerspexProperty property)
  99. {
  100. throw new NotImplementedException();
  101. }
  102. public T GetValue<T>(PerspexProperty<T> property)
  103. {
  104. throw new NotImplementedException();
  105. }
  106. public void SetValue(PerspexProperty property, object value, BindingPriority priority)
  107. {
  108. throw new NotImplementedException();
  109. }
  110. public void SetValue<T>(PerspexProperty<T> property, T value, BindingPriority priority = BindingPriority.LocalValue)
  111. {
  112. throw new NotImplementedException();
  113. }
  114. public IDisposable Bind(PerspexProperty property, IObservable<object> source, BindingPriority priority)
  115. {
  116. throw new NotImplementedException();
  117. }
  118. public bool IsSet(PerspexProperty property)
  119. {
  120. throw new NotImplementedException();
  121. }
  122. public IDisposable Bind<T>(PerspexProperty<T> property, IObservable<T> source, BindingPriority priority = BindingPriority.LocalValue)
  123. {
  124. throw new NotImplementedException();
  125. }
  126. }
  127. public class TestLogical1 : TestLogical
  128. {
  129. }
  130. public class TestLogical2 : TestLogical
  131. {
  132. }
  133. public class TestLogical3 : TestLogical
  134. {
  135. }
  136. }
  137. }