GridSplitterTests.cs 4.3 KB

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