DropDownTests.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 Avalonia.Controls.Presenters;
  4. using Avalonia.Controls.Primitives;
  5. using Avalonia.Controls.Shapes;
  6. using Avalonia.Controls.Templates;
  7. using Avalonia.Input;
  8. using Avalonia.LogicalTree;
  9. using Avalonia.Media;
  10. using Avalonia.UnitTests;
  11. using Xunit;
  12. namespace Avalonia.Controls.UnitTests
  13. {
  14. public class DropDownTests
  15. {
  16. [Fact]
  17. public void Clicking_On_Control_Toggles_IsDropDownOpen()
  18. {
  19. var target = new DropDown
  20. {
  21. Items = new[] { "Foo", "Bar" },
  22. };
  23. target.RaiseEvent(new PointerPressedEventArgs
  24. {
  25. RoutedEvent = InputElement.PointerPressedEvent,
  26. });
  27. Assert.True(target.IsDropDownOpen);
  28. target.RaiseEvent(new PointerPressedEventArgs
  29. {
  30. RoutedEvent = InputElement.PointerPressedEvent,
  31. });
  32. Assert.False(target.IsDropDownOpen);
  33. }
  34. [Fact]
  35. public void SelectionBoxItem_Is_Rectangle_With_VisualBrush_When_Selection_Is_Control()
  36. {
  37. var items = new[] { new Canvas() };
  38. var target = new DropDown
  39. {
  40. Items = items,
  41. SelectedIndex = 0,
  42. };
  43. var rectangle = target.GetValue(DropDown.SelectionBoxItemProperty) as Rectangle;
  44. Assert.NotNull(rectangle);
  45. var brush = rectangle.Fill as VisualBrush;
  46. Assert.NotNull(brush);
  47. Assert.Same(items[0], brush.Visual);
  48. }
  49. [Fact]
  50. public void SelectionBoxItem_Rectangle_Is_Removed_From_Logical_Tree()
  51. {
  52. var target = new DropDown
  53. {
  54. Items = new[] { new Canvas() },
  55. SelectedIndex = 0,
  56. Template = GetTemplate(),
  57. };
  58. var root = new TestRoot { Child = target };
  59. target.ApplyTemplate();
  60. target.Presenter.ApplyTemplate();
  61. var rectangle = target.GetValue(DropDown.SelectionBoxItemProperty) as Rectangle;
  62. Assert.True(((ILogical)target).IsAttachedToLogicalTree);
  63. Assert.True(((ILogical)rectangle).IsAttachedToLogicalTree);
  64. rectangle.DetachedFromLogicalTree += (s, e) => { };
  65. root.Child = null;
  66. Assert.False(((ILogical)target).IsAttachedToLogicalTree);
  67. Assert.False(((ILogical)rectangle).IsAttachedToLogicalTree);
  68. }
  69. private FuncControlTemplate GetTemplate()
  70. {
  71. return new FuncControlTemplate<DropDown>(parent =>
  72. {
  73. return new Panel
  74. {
  75. Name = "container",
  76. Children = new Controls
  77. {
  78. new ContentControl
  79. {
  80. [!ContentControl.ContentProperty] = parent[!DropDown.SelectionBoxItemProperty],
  81. },
  82. new ToggleButton
  83. {
  84. Name = "toggle",
  85. },
  86. new Popup
  87. {
  88. Name = "PART_Popup",
  89. Child = new ItemsPresenter
  90. {
  91. Name = "PART_ItemsPresenter",
  92. [!ItemsPresenter.ItemsProperty] = parent[!DropDown.ItemsProperty],
  93. }
  94. }
  95. }
  96. };
  97. });
  98. }
  99. }
  100. }