OrientationTests.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. namespace Terminal.Gui.ViewTests.OrientationTests;
  2. public class OrientationTests
  3. {
  4. private class CustomView : View, IOrientation
  5. {
  6. private readonly OrientationHelper _orientationHelper;
  7. public CustomView ()
  8. {
  9. _orientationHelper = new (this);
  10. Orientation = Orientation.Vertical;
  11. _orientationHelper.OrientationChanging += (sender, e) => OrientationChanging?.Invoke (this, e);
  12. _orientationHelper.OrientationChanged += (sender, e) => OrientationChanged?.Invoke (this, e);
  13. }
  14. public Orientation Orientation
  15. {
  16. get => _orientationHelper.Orientation;
  17. set => _orientationHelper.Orientation = value;
  18. }
  19. public event EventHandler<CancelEventArgs<Orientation>> OrientationChanging;
  20. public event EventHandler<EventArgs<Orientation>> OrientationChanged;
  21. public bool CancelOnOrientationChanging { get; set; }
  22. public bool OnOrientationChangingCalled { get; private set; }
  23. public bool OnOrientationChangedCalled { get; private set; }
  24. public bool OnOrientationChanging (Orientation currentOrientation, Orientation newOrientation)
  25. {
  26. OnOrientationChangingCalled = true;
  27. // Custom logic before orientation changes
  28. return CancelOnOrientationChanging; // Return true to cancel the change
  29. }
  30. public void OnOrientationChanged (Orientation newOrientation)
  31. {
  32. OnOrientationChangedCalled = true;
  33. // Custom logic after orientation has changed
  34. }
  35. }
  36. [Fact]
  37. public void Orientation_Change_IsSuccessful ()
  38. {
  39. // Arrange
  40. var customView = new CustomView ();
  41. var orientationChanged = false;
  42. customView.OrientationChanged += (sender, e) => orientationChanged = true;
  43. // Act
  44. customView.Orientation = Orientation.Horizontal;
  45. // Assert
  46. Assert.True (orientationChanged, "OrientationChanged event was not invoked.");
  47. Assert.Equal (Orientation.Horizontal, customView.Orientation);
  48. }
  49. [Fact]
  50. public void Orientation_Change_OrientationChanging_Set_Cancel_IsCancelled ()
  51. {
  52. // Arrange
  53. var customView = new CustomView ();
  54. customView.OrientationChanging += (sender, e) => e.Cancel = true; // Cancel the orientation change
  55. var orientationChanged = false;
  56. customView.OrientationChanged += (sender, e) => orientationChanged = true;
  57. // Act
  58. customView.Orientation = Orientation.Horizontal;
  59. // Assert
  60. Assert.False (orientationChanged, "OrientationChanged event was invoked despite cancellation.");
  61. Assert.Equal (Orientation.Vertical, customView.Orientation); // Assuming Vertical is the default orientation
  62. }
  63. [Fact]
  64. public void Orientation_Change_OnOrientationChanging_Return_True_IsCancelled ()
  65. {
  66. // Arrange
  67. var customView = new CustomView ();
  68. customView.CancelOnOrientationChanging = true; // Cancel the orientation change
  69. var orientationChanged = false;
  70. customView.OrientationChanged += (sender, e) => orientationChanged = true;
  71. // Act
  72. customView.Orientation = Orientation.Horizontal;
  73. // Assert
  74. Assert.False (orientationChanged, "OrientationChanged event was invoked despite cancellation.");
  75. Assert.Equal (Orientation.Vertical, customView.Orientation); // Assuming Vertical is the default orientation
  76. }
  77. [Fact]
  78. public void OrientationChanging_VirtualMethodCalledBeforeEvent ()
  79. {
  80. // Arrange
  81. var radioGroup = new CustomView ();
  82. bool eventCalled = false;
  83. radioGroup.OrientationChanging += (sender, e) =>
  84. {
  85. eventCalled = true;
  86. Assert.True (radioGroup.OnOrientationChangingCalled, "OnOrientationChanging was not called before the event.");
  87. };
  88. // Act
  89. radioGroup.Orientation = Orientation.Horizontal;
  90. // Assert
  91. Assert.True (eventCalled, "OrientationChanging event was not called.");
  92. }
  93. [Fact]
  94. public void OrientationChanged_VirtualMethodCalledBeforeEvent ()
  95. {
  96. // Arrange
  97. var radioGroup = new CustomView ();
  98. bool eventCalled = false;
  99. radioGroup.OrientationChanged += (sender, e) =>
  100. {
  101. eventCalled = true;
  102. Assert.True (radioGroup.OnOrientationChangedCalled, "OnOrientationChanged was not called before the event.");
  103. };
  104. // Act
  105. radioGroup.Orientation = Orientation.Horizontal;
  106. // Assert
  107. Assert.True (eventCalled, "OrientationChanged event was not called.");
  108. }
  109. }