SelectorTests_Child.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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.Collections.Generic;
  5. using System.Linq;
  6. using System.Reactive;
  7. using System.Reactive.Linq;
  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_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 = new Selector().OfType<TestLogical1>().Child().OfType<TestLogical2>();
  23. Assert.True(selector.Match(child).ImmediateResult);
  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 = new Selector().OfType<TestLogical1>().Child().OfType<TestLogical3>();
  34. Assert.False(selector.Match(child).ImmediateResult);
  35. }
  36. [Fact]
  37. public async void 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 = new Selector().OfType<TestLogical1>().Class("foo").Child().OfType<TestLogical2>();
  43. var activator = selector.Match(child).ObservableResult;
  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 = new Selector().OfType<TestLogical1>().Child().OfType<TestLogical3>();
  56. Assert.False(selector.Match(control).ImmediateResult);
  57. }
  58. public abstract class TestLogical : ILogical, IStyleable
  59. {
  60. public TestLogical()
  61. {
  62. Classes = new Classes();
  63. }
  64. public event EventHandler<PerspexPropertyChangedEventArgs> PropertyChanged;
  65. public Classes Classes { get; }
  66. public string Name { get; set; }
  67. public bool IsAttachedToLogicalTree { get; }
  68. public IPerspexReadOnlyList<ILogical> LogicalChildren { get; set; }
  69. public ILogical LogicalParent { get; set; }
  70. public Type StyleKey { get; }
  71. public ITemplatedControl TemplatedParent { get; }
  72. IObservable<Unit> IStyleable.StyleDetach { get; }
  73. IPerspexReadOnlyList<string> IStyleable.Classes => Classes;
  74. public object GetValue(PerspexProperty property)
  75. {
  76. throw new NotImplementedException();
  77. }
  78. public T GetValue<T>(PerspexProperty<T> property)
  79. {
  80. throw new NotImplementedException();
  81. }
  82. public void SetValue(PerspexProperty property, object value, BindingPriority priority)
  83. {
  84. throw new NotImplementedException();
  85. }
  86. public void SetValue<T>(PerspexProperty<T> property, T value, BindingPriority priority = BindingPriority.LocalValue)
  87. {
  88. throw new NotImplementedException();
  89. }
  90. public IDisposable Bind(PerspexProperty property, IObservable<object> source, BindingPriority priority)
  91. {
  92. throw new NotImplementedException();
  93. }
  94. public bool IsSet(PerspexProperty property)
  95. {
  96. throw new NotImplementedException();
  97. }
  98. public IDisposable Bind<T>(PerspexProperty<T> property, IObservable<T> source, BindingPriority priority = BindingPriority.LocalValue)
  99. {
  100. throw new NotImplementedException();
  101. }
  102. }
  103. public class TestLogical1 : TestLogical
  104. {
  105. }
  106. public class TestLogical2 : TestLogical
  107. {
  108. }
  109. public class TestLogical3 : TestLogical
  110. {
  111. }
  112. }
  113. }