ComboBoxTests.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 ComboBoxTests
  15. {
  16. MouseTestHelper _helper = new MouseTestHelper();
  17. [Fact]
  18. public void Clicking_On_Control_Toggles_IsDropDownOpen()
  19. {
  20. var target = new ComboBox
  21. {
  22. Items = new[] { "Foo", "Bar" },
  23. };
  24. _helper.Down(target);
  25. _helper.Up(target);
  26. Assert.True(target.IsDropDownOpen);
  27. _helper.Down(target);
  28. Assert.False(target.IsDropDownOpen);
  29. }
  30. [Fact]
  31. public void SelectionBoxItem_Is_Rectangle_With_VisualBrush_When_Selection_Is_Control()
  32. {
  33. var items = new[] { new Canvas() };
  34. var target = new ComboBox
  35. {
  36. Items = items,
  37. SelectedIndex = 0,
  38. };
  39. var rectangle = target.GetValue(ComboBox.SelectionBoxItemProperty) as Rectangle;
  40. Assert.NotNull(rectangle);
  41. var brush = rectangle.Fill as VisualBrush;
  42. Assert.NotNull(brush);
  43. Assert.Same(items[0], brush.Visual);
  44. }
  45. [Fact]
  46. public void SelectionBoxItem_Rectangle_Is_Removed_From_Logical_Tree()
  47. {
  48. var target = new ComboBox
  49. {
  50. Items = new[] { new Canvas() },
  51. SelectedIndex = 0,
  52. Template = GetTemplate(),
  53. };
  54. var root = new TestRoot { Child = target };
  55. target.ApplyTemplate();
  56. target.Presenter.ApplyTemplate();
  57. var rectangle = target.GetValue(ComboBox.SelectionBoxItemProperty) as Rectangle;
  58. Assert.True(((ILogical)target).IsAttachedToLogicalTree);
  59. Assert.True(((ILogical)rectangle).IsAttachedToLogicalTree);
  60. rectangle.DetachedFromLogicalTree += (s, e) => { };
  61. root.Child = null;
  62. Assert.False(((ILogical)target).IsAttachedToLogicalTree);
  63. Assert.False(((ILogical)rectangle).IsAttachedToLogicalTree);
  64. }
  65. private FuncControlTemplate GetTemplate()
  66. {
  67. return new FuncControlTemplate<ComboBox>((parent, scope) =>
  68. {
  69. return new Panel
  70. {
  71. Name = "container",
  72. Children =
  73. {
  74. new ContentControl
  75. {
  76. [!ContentControl.ContentProperty] = parent[!ComboBox.SelectionBoxItemProperty],
  77. },
  78. new ToggleButton
  79. {
  80. Name = "toggle",
  81. }.RegisterInNameScope(scope),
  82. new Popup
  83. {
  84. Name = "PART_Popup",
  85. Child = new ItemsPresenter
  86. {
  87. Name = "PART_ItemsPresenter",
  88. [!ItemsPresenter.ItemsProperty] = parent[!ComboBox.ItemsProperty],
  89. }.RegisterInNameScope(scope)
  90. }.RegisterInNameScope(scope)
  91. }
  92. };
  93. });
  94. }
  95. }
  96. }