DropDownTests.cs 3.1 KB

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