Dim.PercentTests.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #nullable disable
  2. namespace ViewBaseTests.Layout;
  3. public class DimPercentTests
  4. {
  5. [Fact]
  6. public void DimFactor_Calculate_ReturnsCorrectValue ()
  7. {
  8. var dim = new DimPercent (50);
  9. var result = dim.Calculate (0, 100, null, Dimension.None);
  10. Assert.Equal (50, result);
  11. }
  12. [Fact]
  13. public void DimPercent_Equals ()
  14. {
  15. int n1 = 0;
  16. int n2 = 0;
  17. Dim dim1 = Dim.Percent (n1);
  18. Dim dim2 = Dim.Percent (n2);
  19. Assert.Equal (dim1, dim2);
  20. n1 = n2 = 1;
  21. dim1 = Dim.Percent (n1);
  22. dim2 = Dim.Percent (n2);
  23. Assert.Equal (dim1, dim2);
  24. n1 = n2 = 50;
  25. dim1 = Dim.Percent (n1);
  26. dim2 = Dim.Percent (n2);
  27. Assert.Equal (dim1, dim2);
  28. n1 = n2 = 100;
  29. dim1 = Dim.Percent (n1);
  30. dim2 = Dim.Percent (n2);
  31. Assert.Equal (dim1, dim2);
  32. n1 = n2 = 30;
  33. dim1 = Dim.Percent (n1, DimPercentMode.Position);
  34. dim2 = Dim.Percent (n2, DimPercentMode.Position);
  35. Assert.Equal (dim1, dim2);
  36. n1 = n2 = 30;
  37. dim1 = Dim.Percent (n1);
  38. dim2 = Dim.Percent (n2, DimPercentMode.Position);
  39. Assert.NotEqual (dim1, dim2);
  40. n1 = 0;
  41. n2 = 1;
  42. dim1 = Dim.Percent (n1);
  43. dim2 = Dim.Percent (n2);
  44. Assert.NotEqual (dim1, dim2);
  45. n1 = 50;
  46. n2 = 150;
  47. dim1 = Dim.Percent (n1);
  48. dim2 = Dim.Percent (n2);
  49. Assert.NotEqual (dim1, dim2);
  50. }
  51. [Fact]
  52. public void TestEquality ()
  53. {
  54. var a = Dim.Percent (32);
  55. var b = Dim.Percent (32);
  56. Assert.True (a.Equals (b));
  57. Assert.True (a.GetHashCode () == b.GetHashCode ());
  58. }
  59. [Fact]
  60. public void DimPercent_Invalid_Throws ()
  61. {
  62. Dim dim = Dim.Percent (0);
  63. Assert.Throws<ArgumentOutOfRangeException> (() => dim = Dim.Percent (-1));
  64. //Assert.Throws<ArgumentException> (() => dim = Dim.Percent (101));
  65. Assert.Throws<ArgumentOutOfRangeException> (() => dim = Dim.Percent (-1000001));
  66. //Assert.Throws<ArgumentException> (() => dim = Dim.Percent (1000001));
  67. }
  68. [Theory]
  69. [InlineData (0, DimPercentMode.ContentSize, true, 12)]
  70. [InlineData (0, DimPercentMode.ContentSize, false, 12)]
  71. [InlineData (1, DimPercentMode.ContentSize, true, 12)]
  72. [InlineData (1, DimPercentMode.ContentSize, false, 12)]
  73. [InlineData (2, DimPercentMode.ContentSize, true, 12)]
  74. [InlineData (2, DimPercentMode.ContentSize, false, 12)]
  75. [InlineData (0, DimPercentMode.Position, true, 12)]
  76. [InlineData (0, DimPercentMode.Position, false, 12)]
  77. [InlineData (1, DimPercentMode.Position, true, 12)]
  78. [InlineData (1, DimPercentMode.Position, false, 12)]
  79. [InlineData (2, DimPercentMode.Position, true, 11)]
  80. [InlineData (2, DimPercentMode.Position, false, 11)]
  81. public void DimPercent_Position (int position, DimPercentMode mode, bool width, int expected)
  82. {
  83. var super = new View { Width = 25, Height = 25 };
  84. var view = new View
  85. {
  86. X = width ? position : 0,
  87. Y = width ? 0 : position,
  88. Width = width ? Dim.Percent (50, mode) : 1,
  89. Height = width ? 1 : Dim.Percent (50, mode)
  90. };
  91. super.Add (view);
  92. super.BeginInit ();
  93. super.EndInit ();
  94. super.LayoutSubViews ();
  95. Assert.Equal (25, super.Frame.Width);
  96. Assert.Equal (25, super.Frame.Height);
  97. if (width)
  98. {
  99. Assert.Equal (expected, view.Frame.Width);
  100. Assert.Equal (1, view.Frame.Height);
  101. }
  102. else
  103. {
  104. Assert.Equal (1, view.Frame.Width);
  105. Assert.Equal (expected, view.Frame.Height);
  106. }
  107. }
  108. [Theory]
  109. [InlineData (0, true)]
  110. [InlineData (0, false)]
  111. [InlineData (50, true)]
  112. [InlineData (50, false)]
  113. public void DimPercent_PlusOne (int startingDistance, bool testHorizontal)
  114. {
  115. var super = new View { Width = 100, Height = 100 };
  116. var view = new View
  117. {
  118. X = testHorizontal ? startingDistance : 0,
  119. Y = testHorizontal ? 0 : startingDistance,
  120. Width = testHorizontal ? Dim.Percent (50) + 1 : 1,
  121. Height = testHorizontal ? 1 : Dim.Percent (50) + 1
  122. };
  123. super.Add (view);
  124. super.BeginInit ();
  125. super.EndInit ();
  126. super.LayoutSubViews ();
  127. Assert.Equal (100, super.Frame.Width);
  128. Assert.Equal (100, super.Frame.Height);
  129. if (testHorizontal)
  130. {
  131. Assert.Equal (51, view.Frame.Width);
  132. Assert.Equal (1, view.Frame.Height);
  133. }
  134. else
  135. {
  136. Assert.Equal (1, view.Frame.Width);
  137. Assert.Equal (51, view.Frame.Height);
  138. }
  139. }
  140. [Theory]
  141. [InlineData (0)]
  142. [InlineData (1)]
  143. [InlineData (50)]
  144. [InlineData (100)]
  145. [InlineData (101)]
  146. public void DimPercent_SetsValue (int percent)
  147. {
  148. Dim dim = Dim.Percent (percent);
  149. Assert.Equal ($"Percent({percent},ContentSize)", dim.ToString ());
  150. }
  151. }