SelectorTests_Child.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System.Collections.Generic;
  2. using System.Threading.Tasks;
  3. using Avalonia.Controls;
  4. using Avalonia.LogicalTree;
  5. using Avalonia.Styling;
  6. using Xunit;
  7. namespace Avalonia.Base.UnitTests.Styling
  8. {
  9. public class SelectorTests_Child
  10. {
  11. [Fact]
  12. public void Child_Matches_Control_When_It_Is_Child_OfType()
  13. {
  14. var parent = new TestLogical1();
  15. var child = new TestLogical2();
  16. child.LogicalParent = parent;
  17. var selector = default(Selector).OfType<TestLogical1>().Child().OfType<TestLogical2>();
  18. Assert.Equal(SelectorMatchResult.AlwaysThisInstance, selector.Match(child).Result);
  19. }
  20. [Fact]
  21. public void Child_Doesnt_Match_Control_When_It_Is_Grandchild_OfType()
  22. {
  23. var grandparent = new TestLogical1();
  24. var parent = new TestLogical2();
  25. var child = new TestLogical3();
  26. parent.LogicalParent = grandparent;
  27. child.LogicalParent = parent;
  28. var selector = default(Selector).OfType<TestLogical1>().Child().OfType<TestLogical3>();
  29. Assert.Equal(SelectorMatchResult.NeverThisInstance, selector.Match(child).Result);
  30. }
  31. [Fact]
  32. public async Task Child_Matches_Control_When_It_Is_Child_OfType_And_Class()
  33. {
  34. var parent = new TestLogical1();
  35. var child = new TestLogical2();
  36. child.LogicalParent = parent;
  37. var selector = default(Selector).OfType<TestLogical1>().Class("foo").Child().OfType<TestLogical2>();
  38. var activator = selector.Match(child).Activator;
  39. var result = new List<bool>();
  40. Assert.False(await activator.Take(1));
  41. parent.Classes.Add("foo");
  42. Assert.True(await activator.Take(1));
  43. parent.Classes.Remove("foo");
  44. Assert.False(await activator.Take(1));
  45. }
  46. [Fact]
  47. public void Child_Doesnt_Match_Control_When_It_Has_No_Parent()
  48. {
  49. var control = new TestLogical3();
  50. var selector = default(Selector).OfType<TestLogical1>().Child().OfType<TestLogical3>();
  51. Assert.Equal(SelectorMatchResult.NeverThisInstance, selector.Match(control).Result);
  52. }
  53. [Fact]
  54. public void Child_Selector_Should_Have_Correct_String_Representation()
  55. {
  56. var selector = default(Selector).OfType<TestLogical1>().Child().OfType<TestLogical3>();
  57. Assert.Equal("TestLogical1 > TestLogical3", selector.ToString());
  58. }
  59. public abstract class TestLogical : Control
  60. {
  61. public ILogical LogicalParent
  62. {
  63. get => Parent;
  64. set => ((ISetLogicalParent)this).SetParent(value);
  65. }
  66. }
  67. public class TestLogical1 : TestLogical
  68. {
  69. }
  70. public class TestLogical2 : TestLogical
  71. {
  72. }
  73. public class TestLogical3 : TestLogical
  74. {
  75. }
  76. }
  77. }