SelectorTests_Descendent.cs 5.8 KB

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