SelectorTests_Child.cs 4.5 KB

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