AdornmentSubViewTests.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using UnitTests;
  2. using Xunit.Abstractions;
  3. namespace ViewBaseTests.Adornments;
  4. public class AdornmentSubViewTests (ITestOutputHelper output)
  5. {
  6. private readonly ITestOutputHelper _output = output;
  7. [Fact]
  8. public void Setting_Thickness_Causes_Adornment_SubView_Layout ()
  9. {
  10. var view = new View ();
  11. var subView = new View ();
  12. view.Padding!.Add (subView);
  13. view.BeginInit ();
  14. view.EndInit ();
  15. var raised = false;
  16. subView.SubViewLayout += LayoutStarted;
  17. view.Padding.Thickness = new (1, 2, 3, 4);
  18. view.Layout ();
  19. Assert.True (raised);
  20. return;
  21. void LayoutStarted (object? sender, LayoutEventArgs e)
  22. {
  23. raised = true;
  24. }
  25. }
  26. [Theory]
  27. [InlineData (0, 0, false)] // Padding has no thickness, so false
  28. [InlineData (0, 1, false)] // Padding has no thickness, so false
  29. [InlineData (1, 0, true)]
  30. [InlineData (1, 1, true)]
  31. [InlineData (2, 1, true)]
  32. public void Adornment_WithSubView_Finds (int viewPadding, int subViewPadding, bool expectedFound)
  33. {
  34. IApplication? app = Application.Create ();
  35. Runnable<bool> runnable = new ()
  36. {
  37. Width = 10,
  38. Height = 10
  39. };
  40. app.Begin (runnable);
  41. runnable.Padding!.Thickness = new (viewPadding);
  42. // Turn of TransparentMouse for the test
  43. runnable.Padding!.ViewportSettings = ViewportSettingsFlags.None;
  44. var subView = new View ()
  45. {
  46. X = 0,
  47. Y = 0,
  48. Width = 5,
  49. Height = 5
  50. };
  51. subView.Padding!.Thickness = new (subViewPadding);
  52. // Turn of TransparentMouse for the test
  53. subView.Padding!.ViewportSettings = ViewportSettingsFlags.None;
  54. runnable.Padding!.Add (subView);
  55. runnable.Layout ();
  56. View? foundView = runnable.GetViewsUnderLocation (new (0, 0), ViewportSettingsFlags.None).LastOrDefault ();
  57. bool found = foundView == subView || foundView == subView.Padding;
  58. Assert.Equal (expectedFound, found);
  59. }
  60. [Fact]
  61. public void Adornment_WithNonVisibleSubView_Finds_Adornment ()
  62. {
  63. IApplication? app = Application.Create ();
  64. Runnable<bool> runnable = new ()
  65. {
  66. Width = 10,
  67. Height = 10
  68. };
  69. app.Begin (runnable);
  70. runnable.Padding!.Thickness = new Thickness (1);
  71. var subView = new View ()
  72. {
  73. X = 0,
  74. Y = 0,
  75. Width = 1,
  76. Height = 1,
  77. Visible = false
  78. };
  79. runnable.Padding.Add (subView);
  80. runnable.Layout ();
  81. Assert.Equal (runnable.Padding, runnable.GetViewsUnderLocation (new Point (0, 0), ViewportSettingsFlags.None).LastOrDefault ());
  82. }
  83. [Fact]
  84. public void Button_With_Opaque_ShadowStyle_In_Border_Should_Draw_Shadow ()
  85. {
  86. // Arrange
  87. using IApplication app = Application.Create ();
  88. app.Init ("fake");
  89. app.Driver?.SetScreenSize (1, 4);
  90. app.Driver!.Force16Colors = true;
  91. using Runnable window = new ();
  92. window.Width = Dim.Fill ();
  93. window.Height = Dim.Fill ();
  94. window.Text = @"XXXXXX";
  95. window.SetScheme (new (new Attribute (Color.Black, Color.White)));
  96. // Setup padding with some thickness so we have space for the button
  97. window.Border!.Thickness = new (0, 3, 0, 0);
  98. // Add a button with a transparent shadow to the Padding adornment
  99. Button buttonInBorder = new ()
  100. {
  101. X = 0,
  102. Y = 0,
  103. Text = "B",
  104. NoDecorations = true,
  105. NoPadding = true,
  106. ShadowStyle = ShadowStyle.Opaque,
  107. };
  108. window.Border.Add (buttonInBorder);
  109. app.Begin (window);
  110. DriverAssert.AssertDriverOutputIs ("""
  111. \x1b[30m\x1b[107mB▝ \x1b[97m\x1b[40mX
  112. """,
  113. _output,
  114. app.Driver);
  115. }
  116. [Fact]
  117. public void Button_With_Opaque_ShadowStyle_In_Padding_Should_Draw_Shadow ()
  118. {
  119. // Arrange
  120. using IApplication app = Application.Create ();
  121. app.Init ("fake");
  122. app.Driver?.SetScreenSize (1, 4);
  123. app.Driver!.Force16Colors = true;
  124. using Runnable window = new ();
  125. window.Width = Dim.Fill ();
  126. window.Height = Dim.Fill ();
  127. window.Text = @"XXXXXX";
  128. window.SetScheme (new (new Attribute (Color.Black, Color.White)));
  129. // Setup padding with some thickness so we have space for the button
  130. window.Padding!.Thickness = new (0, 3, 0, 0);
  131. // Add a button with a transparent shadow to the Padding adornment
  132. Button buttonInPadding = new ()
  133. {
  134. X = 0,
  135. Y = 0,
  136. Text = "B",
  137. NoDecorations = true,
  138. NoPadding = true,
  139. ShadowStyle = ShadowStyle.Opaque,
  140. };
  141. window.Padding.Add (buttonInPadding);
  142. app.Begin (window);
  143. DriverAssert.AssertDriverOutputIs ("""
  144. \x1b[97m\x1b[40mB\x1b[30m\x1b[107m▝ \x1b[97m\x1b[40mX
  145. """,
  146. _output,
  147. app.Driver);
  148. }
  149. }