SelectorTests_Child.cs 5.4 KB

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