SelectorTests_Child.cs 5.2 KB

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