GridSplitterTests.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using Moq;
  2. using Perspex.Controls.Primitives;
  3. using Perspex.Input;
  4. using Perspex.Platform;
  5. using Xunit;
  6. namespace Perspex.Controls.UnitTests
  7. {
  8. public class GridSplitterTests
  9. {
  10. [Fact]
  11. public void Vertical_Stays_Within_Constraints()
  12. {
  13. var cursorFactoryImpl = new Mock<IStandardCursorFactory>();
  14. PerspexLocator.CurrentMutable.Bind<IStandardCursorFactory>().ToConstant(cursorFactoryImpl.Object);
  15. var control1 = new Border { [Grid.ColumnProperty] = 0 };
  16. var splitter = new GridSplitter
  17. {
  18. Orientation = Orientation.Vertical,
  19. [Grid.ColumnProperty] = 1,
  20. };
  21. var control2 = new Border { [Grid.ColumnProperty] = 2 };
  22. var columnDefinitions = new ColumnDefinitions()
  23. {
  24. new ColumnDefinition(1, GridUnitType.Star) {MinWidth = 10, MaxWidth = 190},
  25. new ColumnDefinition(GridLength.Auto),
  26. new ColumnDefinition(1, GridUnitType.Star) {MinWidth = 80, MaxWidth = 120},
  27. };
  28. var grid = new Grid()
  29. {
  30. ColumnDefinitions = columnDefinitions,
  31. Children = new Controls()
  32. {
  33. control1, splitter, control2
  34. }
  35. };
  36. var root = new TestRoot { Child = grid };
  37. Assert.Equal(splitter.Orientation, Orientation.Vertical);
  38. root.Measure(new Size(200, 100));
  39. root.Arrange(new Rect(0, 0, 200, 100));
  40. splitter.RaiseEvent(new VectorEventArgs
  41. {
  42. RoutedEvent = Thumb.DragDeltaEvent,
  43. Vector = new Vector(-100,0)
  44. });
  45. Assert.Equal(columnDefinitions[0].Width, new GridLength(80,GridUnitType.Star));
  46. Assert.Equal(columnDefinitions[2].Width, new GridLength(120,GridUnitType.Star));
  47. splitter.RaiseEvent(new VectorEventArgs
  48. {
  49. RoutedEvent = Thumb.DragDeltaEvent,
  50. Vector = new Vector(100, 0)
  51. });
  52. Assert.Equal(columnDefinitions[0].Width, new GridLength(120, GridUnitType.Star));
  53. Assert.Equal(columnDefinitions[2].Width, new GridLength(80, GridUnitType.Star));
  54. }
  55. [Fact]
  56. public void Horizontal_Stays_Within_Constraints()
  57. {
  58. var cursorFactoryImpl = new Mock<IStandardCursorFactory>();
  59. PerspexLocator.CurrentMutable.Bind<IStandardCursorFactory>().ToConstant(cursorFactoryImpl.Object);
  60. var control1 = new Border { [Grid.RowProperty] = 0 };
  61. var splitter = new GridSplitter
  62. {
  63. Orientation = Orientation.Horizontal,
  64. [Grid.RowProperty] = 1,
  65. };
  66. var control2 = new Border { [Grid.RowProperty] = 2 };
  67. var rowDefinitions = new RowDefinitions()
  68. {
  69. new RowDefinition(1, GridUnitType.Star) {MinHeight = 70, MaxHeight = 110},
  70. new RowDefinition(GridLength.Auto),
  71. new RowDefinition(1, GridUnitType.Star) { MinHeight = 10, MaxHeight = 140},
  72. };
  73. var grid = new Grid()
  74. {
  75. RowDefinitions = rowDefinitions,
  76. Children = new Controls()
  77. {
  78. control1, splitter, control2
  79. }
  80. };
  81. var root = new TestRoot { Child = grid };
  82. Assert.Equal(splitter.Orientation, Orientation.Horizontal);
  83. root.Measure(new Size(100, 200));
  84. root.Arrange(new Rect(0, 0, 100, 200));
  85. splitter.RaiseEvent(new VectorEventArgs
  86. {
  87. RoutedEvent = Thumb.DragDeltaEvent,
  88. Vector = new Vector(0, -100)
  89. });
  90. Assert.Equal(rowDefinitions[0].Height, new GridLength(70, GridUnitType.Star));
  91. Assert.Equal(rowDefinitions[2].Height, new GridLength(130, GridUnitType.Star));
  92. splitter.RaiseEvent(new VectorEventArgs
  93. {
  94. RoutedEvent = Thumb.DragDeltaEvent,
  95. Vector = new Vector(0, 100)
  96. });
  97. Assert.Equal(rowDefinitions[0].Height, new GridLength(110, GridUnitType.Star));
  98. Assert.Equal(rowDefinitions[2].Height, new GridLength(90, GridUnitType.Star));
  99. }
  100. }
  101. }