ScrollableControlTest.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. //
  2. // ScrollableControlTest.cs: Test cases for ScrollableControl.
  3. //
  4. // Author:
  5. // Gert Driesen ([email protected])
  6. //
  7. // (C) 2006 Gert Driesen
  8. //
  9. using System;
  10. using System.Drawing;
  11. using System.Windows.Forms;
  12. using NUnit.Framework;
  13. namespace MonoTests.System.Windows.Forms
  14. {
  15. [TestFixture]
  16. public class ScrollableControlTest : TestHelper
  17. {
  18. [Test]
  19. public void AutoScrollPositionTest ()
  20. {
  21. ScrollableControl sc;
  22. sc = new ScrollableControl ();
  23. sc.AutoScroll = true;
  24. sc.AutoScrollPosition = new Point (-25, -50);
  25. Assert.AreEqual (Point.Empty, sc.AutoScrollPosition, "#01");
  26. sc.AutoScrollPosition = new Point (2500, 5000);
  27. Assert.AreEqual (Point.Empty, sc.AutoScrollPosition, "#02");
  28. sc.AutoScrollPosition = new Point (25, 50);
  29. Assert.AreEqual (Point.Empty, sc.AutoScrollPosition, "#03");
  30. object o = sc.Handle;
  31. sc.AutoScrollPosition = new Point (-25, -50);
  32. Assert.AreEqual (Point.Empty, sc.AutoScrollPosition, "#04");
  33. sc.AutoScrollPosition = new Point (2500, 5000);
  34. Assert.AreEqual (Point.Empty, sc.AutoScrollPosition, "#05");
  35. sc.AutoScrollPosition = new Point (25, 50);
  36. Assert.AreEqual (Point.Empty, sc.AutoScrollPosition, "#06");
  37. sc.Size = new Size (200, 400);
  38. sc.Location = new Point (20, 40);
  39. sc.AutoScrollPosition = new Point (-25, -50);
  40. Assert.AreEqual (Point.Empty, sc.AutoScrollPosition, "#07");
  41. sc.AutoScrollPosition = new Point (2500, 5000);
  42. Assert.AreEqual (Point.Empty, sc.AutoScrollPosition, "#08");
  43. sc.AutoScrollPosition = new Point (25, 50);
  44. Assert.AreEqual (Point.Empty, sc.AutoScrollPosition, "#09");
  45. Control c1, c2;
  46. c1 = new Control ();
  47. c1.Location = new Point (-100, -200);
  48. c1.Size = new Size (10, 10);
  49. sc.Controls.Add (c1);
  50. c2 = new Control ();
  51. c2.Location = new Point (400, 800);
  52. c2.Size = new Size (10, 10);
  53. sc.Controls.Add (c2);
  54. Assert.AreEqual (new Rectangle (0, 0, 410, 810), sc.DisplayRectangle, "#10");
  55. sc.ScrollControlIntoView (c2);
  56. Assert.AreEqual (new Point (-226, -426), sc.AutoScrollPosition, "#11");
  57. Assert.AreEqual (new Rectangle (-226, -426, 410, 810), sc.DisplayRectangle, "#12");
  58. Assert.AreEqual (new Point (-326, -626), c1.Location, "#13");
  59. Assert.AreEqual (new Point (174, 374), c2.Location, "#14");
  60. sc.AutoScrollPosition = new Point (-25, -50);
  61. Assert.AreEqual (Point.Empty, sc.AutoScrollPosition, "#01");
  62. Assert.AreEqual (new Rectangle (0, 0, 410, 810), sc.DisplayRectangle, "#15");
  63. Assert.AreEqual (new Point (-100, -200), c1.Location, "#16");
  64. Assert.AreEqual (new Point (400, 800), c2.Location, "#17");
  65. sc.AutoScrollPosition = new Point (2500, 5000);
  66. Assert.AreEqual (new Point (-226, -426), sc.AutoScrollPosition, "#18");
  67. Assert.AreEqual (new Rectangle (-226, -426, 410, 810), sc.DisplayRectangle, "#19");
  68. Assert.AreEqual (new Point (-326, -626), c1.Location, "#20");
  69. Assert.AreEqual (new Point (174, 374), c2.Location, "#21");
  70. sc.AutoScrollPosition = new Point (25, 50);
  71. Assert.AreEqual (new Point (-25, -50), sc.AutoScrollPosition, "#22");
  72. Assert.AreEqual (new Rectangle (-25, -50, 410, 810), sc.DisplayRectangle, "#23");
  73. Assert.AreEqual (new Point (-125, -250), c1.Location, "#24");
  74. Assert.AreEqual (new Point (375, 750), c2.Location, "#25");
  75. sc.ScrollControlIntoView (c1);
  76. Assert.AreEqual (Point.Empty, sc.AutoScrollPosition, "#26");
  77. Assert.AreEqual (new Rectangle (0, 0, 410, 810), sc.DisplayRectangle, "#27");
  78. Assert.AreEqual (new Point (-100, -200), c1.Location, "#28");
  79. Assert.AreEqual (new Point (400, 800), c2.Location, "#29");
  80. sc.AutoScrollPosition = new Point (2500, 5000);
  81. Assert.AreEqual (new Point (-226, -426), sc.AutoScrollPosition, "#30");
  82. Assert.AreEqual (new Rectangle (-226, -426, 410, 810), sc.DisplayRectangle, "#31");
  83. Assert.AreEqual (new Point (-326, -626), c1.Location, "#32");
  84. Assert.AreEqual (new Point (174, 374), c2.Location, "#33");
  85. sc.AutoScrollPosition = new Point (25, 50);
  86. Assert.AreEqual (new Point (-25, -50), sc.AutoScrollPosition, "#34");
  87. Assert.AreEqual (new Rectangle (-25, -50, 410, 810), sc.DisplayRectangle, "#35");
  88. Assert.AreEqual (new Point (-125, -250), c1.Location, "#36");
  89. Assert.AreEqual (new Point (375, 750), c2.Location, "#37");
  90. sc.ScrollControlIntoView (c2);
  91. Assert.AreEqual (new Point (-226, -426), sc.AutoScrollPosition, "#38");
  92. Assert.AreEqual (new Rectangle (-226, -426, 410, 810), sc.DisplayRectangle, "#39");
  93. Assert.AreEqual (new Point (-326, -626), c1.Location, "#40");
  94. Assert.AreEqual (new Point (174, 374), c2.Location, "#41");
  95. sc.AutoScrollPosition = new Point (-25, -50);
  96. Assert.AreEqual (Point.Empty, sc.AutoScrollPosition, "#42");
  97. Assert.AreEqual (new Rectangle (0, 0, 410, 810), sc.DisplayRectangle, "#43");
  98. Assert.AreEqual (new Point (-100, -200), c1.Location, "#44");
  99. Assert.AreEqual (new Point (400, 800), c2.Location, "#45");
  100. sc.AutoScrollPosition = new Point (2500, 5000);
  101. Assert.AreEqual (new Point (-226, -426), sc.AutoScrollPosition, "#46");
  102. Assert.AreEqual (new Rectangle (-226, -426, 410, 810), sc.DisplayRectangle, "#47");
  103. Assert.AreEqual (new Point (-326, -626), c1.Location, "#48");
  104. Assert.AreEqual (new Point (174, 374), c2.Location, "#49");
  105. sc.AutoScrollPosition = new Point (25, 50);
  106. Assert.AreEqual (new Point (-25, -50), sc.AutoScrollPosition, "#50");
  107. Assert.AreEqual (new Rectangle (-25, -50, 410, 810), sc.DisplayRectangle, "#51");
  108. Assert.AreEqual (new Point (-125, -250), c1.Location, "#52");
  109. Assert.AreEqual (new Point (375, 750), c2.Location, "#53");
  110. }
  111. [Test]
  112. public void ResizeAnchoredTest ()
  113. {
  114. ScrollableControl sc = new ScrollableControl ();
  115. object h = sc.Handle;
  116. sc.Size = new Size (23, 45);
  117. Label lbl = new Label ();
  118. lbl.Anchor = AnchorStyles.Bottom | AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
  119. lbl.Size = sc.ClientSize;
  120. sc.Controls.Add (lbl);
  121. sc.Height *= 2;
  122. sc.Height *= 2;
  123. Assert.AreEqual (lbl.Location, Point.Empty, "#1");
  124. Assert.AreEqual (lbl.Size, sc.ClientSize, "#2");
  125. TestHelper.RemoveWarning (h);
  126. }
  127. [Test]
  128. public void AutoScroll ()
  129. {
  130. ScrollableControl sc = new ScrollableControl ();
  131. Assert.IsFalse (sc.AutoScroll, "#A1");
  132. Assert.AreEqual (0, sc.Controls.Count, "#A2");
  133. sc.AutoScroll = true;
  134. Assert.IsTrue(sc.AutoScroll, "#B1");
  135. Assert.AreEqual (0, sc.Controls.Count, "#B2");
  136. sc.AutoScroll = false;
  137. Assert.IsFalse (sc.AutoScroll, "#C1");
  138. Assert.AreEqual (0, sc.Controls.Count, "#C2");
  139. }
  140. [Test]
  141. public void AutoScrollMinSize ()
  142. {
  143. ScrollableControl sc = new ScrollableControl ();
  144. Assert.AreEqual (Size.Empty, sc.AutoScrollMinSize, "#A1");
  145. Assert.IsFalse (sc.AutoScroll, "#A2");
  146. sc.AutoScrollMinSize = Size.Empty;
  147. Assert.AreEqual (Size.Empty, sc.AutoScrollMinSize, "#B1");
  148. Assert.IsFalse (sc.AutoScroll, "#B2");
  149. sc.AutoScrollMinSize = new Size (10, 20);
  150. Assert.AreEqual (new Size (10, 20), sc.AutoScrollMinSize, "#C1");
  151. Assert.IsTrue (sc.AutoScroll, "#C2");
  152. sc.AutoScroll = false;
  153. Assert.AreEqual (new Size (10, 20), sc.AutoScrollMinSize, "#D1");
  154. Assert.IsFalse (sc.AutoScroll, "#D2");
  155. sc.AutoScrollMinSize = new Size (10, 20);
  156. Assert.AreEqual (new Size (10, 20), sc.AutoScrollMinSize, "#E1");
  157. Assert.IsFalse (sc.AutoScroll, "#E2");
  158. sc.AutoScrollMinSize = new Size (20, 20);
  159. Assert.AreEqual (new Size (20, 20), sc.AutoScrollMinSize, "#F1");
  160. Assert.IsTrue (sc.AutoScroll, "#F2");
  161. sc.AutoScroll = false;
  162. Assert.AreEqual (new Size (20, 20), sc.AutoScrollMinSize, "#G1");
  163. Assert.IsFalse (sc.AutoScroll, "#G2");
  164. sc.AutoScrollMinSize = Size.Empty;
  165. Assert.AreEqual (Size.Empty, sc.AutoScrollMinSize, "#H1");
  166. Assert.IsTrue (sc.AutoScroll, "#H2");
  167. sc.AutoScrollMinSize = new Size (10, 20);
  168. Assert.AreEqual (new Size (10, 20), sc.AutoScrollMinSize, "#I1");
  169. Assert.IsTrue (sc.AutoScroll, "#I2");
  170. sc.AutoScrollMinSize = Size.Empty;
  171. Assert.AreEqual (Size.Empty, sc.AutoScrollMinSize, "#J1");
  172. Assert.IsTrue (sc.AutoScroll, "#J2");
  173. }
  174. #if NET_2_0
  175. [Test]
  176. public void MethodScrollToControl ()
  177. {
  178. if (TestHelper.RunningOnUnix)
  179. Assert.Ignore ("Depends of scrollbar width, values are for Windows");
  180. Form f = new Form ();
  181. f.ShowInTaskbar = false;
  182. f.Show ();
  183. PublicScrollableControl sc = new PublicScrollableControl ();
  184. sc.Size = new Size (200, 200);
  185. sc.AutoScroll = true;
  186. f.Controls.Add (sc);
  187. Button b = new Button ();
  188. b.Top = 15;
  189. sc.Controls.Add (b);
  190. Button b2 = new Button ();
  191. b2.Top = 340;
  192. sc.Controls.Add (b2);
  193. Button b3 = new Button ();
  194. b3.Left = 280;
  195. sc.Controls.Add (b3);
  196. Assert.AreEqual (new Point (0, 0), sc.PublicScrollToControl (b), "A1");
  197. Assert.AreEqual (new Point (0, -180), sc.PublicScrollToControl (b2), "A2");
  198. Assert.AreEqual (new Point (-172, 0), sc.PublicScrollToControl (b3), "A3");
  199. sc.AutoScrollPosition = new Point (50, 70);
  200. Assert.AreEqual (new Point (0, -15), sc.PublicScrollToControl (b), "A4");
  201. Assert.AreEqual (new Point (0, -180), sc.PublicScrollToControl (b2), "A5");
  202. Assert.AreEqual (new Point (-172, 0), sc.PublicScrollToControl (b3), "A6");
  203. sc.AutoScrollPosition = new Point (150, 150);
  204. Assert.AreEqual (new Point (0, -15), sc.PublicScrollToControl (b), "A7");
  205. Assert.AreEqual (new Point (0, -180), sc.PublicScrollToControl (b2), "A8");
  206. Assert.AreEqual (new Point (-172, 0), sc.PublicScrollToControl (b3), "A9");
  207. f.Dispose ();
  208. }
  209. private class PublicScrollableControl : ScrollableControl
  210. {
  211. public Point PublicScrollToControl (Control activeControl)
  212. {
  213. return base.ScrollToControl (activeControl);
  214. }
  215. }
  216. [Test]
  217. public void Padding ()
  218. {
  219. ScrollableControl c = new ScrollableControl ();
  220. c.Dock = DockStyle.Fill;
  221. c.Padding = new Padding (40);
  222. Assert.AreEqual (40, c.Padding.All, "A1");
  223. Assert.AreEqual (40, c.DockPadding.All, "A2");
  224. c.DockPadding.Right = 20;
  225. Assert.AreEqual (20, c.Padding.Right, "A3");
  226. Assert.AreEqual (20, c.DockPadding.Right, "A4");
  227. c.Padding = new Padding (40, 40, 40, 40);
  228. Assert.AreEqual (40, c.Padding.Right, "A5");
  229. Assert.AreEqual (40, c.DockPadding.Right, "A6");
  230. Form f = new Form ();
  231. f.Controls.Add (c);
  232. Button b = new Button ();
  233. c.Controls.Add (b);
  234. f.Show ();
  235. // Padding does not affect laying out the controls
  236. Assert.AreEqual (new Point (0, 0), b.Location, "A7");
  237. f.Close ();
  238. f.Dispose ();
  239. }
  240. #endif
  241. }
  242. }