Merge pull request #323 from crazyjncsu/master
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / ScrollableControlTest.cs
1 //
2 // ScrollableControlTest.cs: Test cases for ScrollableControl.
3 //
4 // Author:
5 //   Gert Driesen (drieseng@users.sourceforge.net)
6 //
7 // (C) 2006 Gert Driesen
8 //
9
10 using System;
11 using System.Drawing;
12 using System.Windows.Forms;
13
14 using NUnit.Framework;
15
16 namespace MonoTests.System.Windows.Forms
17 {
18         [TestFixture]
19         public class ScrollableControlTest : TestHelper
20         {
21                 [Test]
22                 public void AutoScrollPositionTest ()
23                 {
24                         ScrollableControl sc;
25
26                         sc = new ScrollableControl ();
27                         sc.AutoScroll = true;
28                         
29                         sc.AutoScrollPosition = new Point (-25, -50);
30                         Assert.AreEqual (Point.Empty, sc.AutoScrollPosition, "#01");
31
32                         sc.AutoScrollPosition = new Point (2500, 5000);
33                         Assert.AreEqual (Point.Empty, sc.AutoScrollPosition, "#02");
34                         
35                         sc.AutoScrollPosition = new Point (25, 50);
36                         Assert.AreEqual (Point.Empty, sc.AutoScrollPosition, "#03");
37                         
38                         object o = sc.Handle;
39
40                         sc.AutoScrollPosition = new Point (-25, -50);
41                         Assert.AreEqual (Point.Empty, sc.AutoScrollPosition, "#04");
42
43                         sc.AutoScrollPosition = new Point (2500, 5000);
44                         Assert.AreEqual (Point.Empty, sc.AutoScrollPosition, "#05");
45
46                         sc.AutoScrollPosition = new Point (25, 50);
47                         Assert.AreEqual (Point.Empty, sc.AutoScrollPosition, "#06");
48                         
49                         sc.Size = new Size (200, 400);
50                         sc.Location = new Point (20, 40);
51
52                         sc.AutoScrollPosition = new Point (-25, -50);
53                         Assert.AreEqual (Point.Empty, sc.AutoScrollPosition, "#07");
54
55                         sc.AutoScrollPosition = new Point (2500, 5000);
56                         Assert.AreEqual (Point.Empty, sc.AutoScrollPosition, "#08");
57
58                         sc.AutoScrollPosition = new Point (25, 50);
59                         Assert.AreEqual (Point.Empty, sc.AutoScrollPosition, "#09");
60                         
61                         
62                         Control c1, c2;
63                         c1 = new Control ();
64                         c1.Location = new Point (-100, -200);
65                         c1.Size = new Size (10, 10);
66                         sc.Controls.Add (c1);
67
68                         c2 = new Control ();
69                         c2.Location = new Point (400, 800);
70                         c2.Size = new Size (10, 10);
71                         sc.Controls.Add (c2);
72
73                         Assert.AreEqual (new Rectangle (0, 0, 410, 810), sc.DisplayRectangle, "#10");
74                         
75                         sc.ScrollControlIntoView (c2);
76
77                         Assert.AreEqual (new Point (-226, -426), sc.AutoScrollPosition, "#11");
78                         Assert.AreEqual (new Rectangle (-226, -426, 410, 810), sc.DisplayRectangle, "#12");
79                         Assert.AreEqual (new Point (-326, -626), c1.Location, "#13");
80                         Assert.AreEqual (new Point (174, 374), c2.Location, "#14");
81                         
82                         sc.AutoScrollPosition = new Point (-25, -50);
83                         Assert.AreEqual (Point.Empty, sc.AutoScrollPosition, "#01");
84                         Assert.AreEqual (new Rectangle (0, 0, 410, 810), sc.DisplayRectangle, "#15");
85                         Assert.AreEqual (new Point (-100, -200), c1.Location, "#16");
86                         Assert.AreEqual (new Point (400, 800), c2.Location, "#17");
87
88                         sc.AutoScrollPosition = new Point (2500, 5000);
89                         Assert.AreEqual (new Point (-226, -426), sc.AutoScrollPosition, "#18");
90                         Assert.AreEqual (new Rectangle (-226, -426, 410, 810), sc.DisplayRectangle, "#19");
91                         Assert.AreEqual (new Point (-326, -626), c1.Location, "#20");
92                         Assert.AreEqual (new Point (174, 374), c2.Location, "#21");
93
94                         sc.AutoScrollPosition = new Point (25, 50);
95                         Assert.AreEqual (new Point (-25, -50), sc.AutoScrollPosition, "#22");
96                         Assert.AreEqual (new Rectangle (-25, -50, 410, 810), sc.DisplayRectangle, "#23");
97                         Assert.AreEqual (new Point (-125, -250), c1.Location, "#24");
98                         Assert.AreEqual (new Point (375, 750), c2.Location, "#25");
99                         
100                         sc.ScrollControlIntoView (c1);
101
102
103                         Assert.AreEqual (Point.Empty, sc.AutoScrollPosition, "#26");
104                         Assert.AreEqual (new Rectangle (0, 0, 410, 810), sc.DisplayRectangle, "#27");
105                         Assert.AreEqual (new Point (-100, -200), c1.Location, "#28");
106                         Assert.AreEqual (new Point (400, 800), c2.Location, "#29");
107
108                         sc.AutoScrollPosition = new Point (2500, 5000);
109                         Assert.AreEqual (new Point (-226, -426), sc.AutoScrollPosition, "#30");
110                         Assert.AreEqual (new Rectangle (-226, -426, 410, 810), sc.DisplayRectangle, "#31");
111                         Assert.AreEqual (new Point (-326, -626), c1.Location, "#32");
112                         Assert.AreEqual (new Point (174, 374), c2.Location, "#33");
113
114                         sc.AutoScrollPosition = new Point (25, 50);
115                         Assert.AreEqual (new Point (-25, -50), sc.AutoScrollPosition, "#34");
116                         Assert.AreEqual (new Rectangle (-25, -50, 410, 810), sc.DisplayRectangle, "#35");
117                         Assert.AreEqual (new Point (-125, -250), c1.Location, "#36");
118                         Assert.AreEqual (new Point (375, 750), c2.Location, "#37");
119                         
120                         sc.ScrollControlIntoView (c2);
121
122                         Assert.AreEqual (new Point (-226, -426), sc.AutoScrollPosition, "#38");
123                         Assert.AreEqual (new Rectangle (-226, -426, 410, 810), sc.DisplayRectangle, "#39");
124                         Assert.AreEqual (new Point (-326, -626), c1.Location, "#40");
125                         Assert.AreEqual (new Point (174, 374), c2.Location, "#41");
126
127                         sc.AutoScrollPosition = new Point (-25, -50);
128                         Assert.AreEqual (Point.Empty, sc.AutoScrollPosition, "#42");
129                         Assert.AreEqual (new Rectangle (0, 0, 410, 810), sc.DisplayRectangle, "#43");
130                         Assert.AreEqual (new Point (-100, -200), c1.Location, "#44");
131                         Assert.AreEqual (new Point (400, 800), c2.Location, "#45");
132
133                         sc.AutoScrollPosition = new Point (2500, 5000);
134                         Assert.AreEqual (new Point (-226, -426), sc.AutoScrollPosition, "#46");
135                         Assert.AreEqual (new Rectangle (-226, -426, 410, 810), sc.DisplayRectangle, "#47");
136                         Assert.AreEqual (new Point (-326, -626), c1.Location, "#48");
137                         Assert.AreEqual (new Point (174, 374), c2.Location, "#49");
138
139                         sc.AutoScrollPosition = new Point (25, 50);
140                         Assert.AreEqual (new Point (-25, -50), sc.AutoScrollPosition, "#50");
141                         Assert.AreEqual (new Rectangle (-25, -50, 410, 810), sc.DisplayRectangle, "#51");
142                         Assert.AreEqual (new Point (-125, -250), c1.Location, "#52");
143                         Assert.AreEqual (new Point (375, 750), c2.Location, "#53");
144                         
145                         
146                 }
147                 
148                 [Test]
149                 public void ResizeAnchoredTest ()
150                 {
151                         ScrollableControl sc = new ScrollableControl ();
152                         object h = sc.Handle;
153                         sc.Size = new Size (23, 45);
154                         Label lbl = new Label ();
155                         lbl.Anchor = AnchorStyles.Bottom | AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
156                         lbl.Size = sc.ClientSize;
157                         sc.Controls.Add (lbl);
158                         sc.Height *= 2;
159                         sc.Height *= 2;
160                         Assert.AreEqual (lbl.Location, Point.Empty, "#1");
161                         Assert.AreEqual (lbl.Size, sc.ClientSize, "#2");
162                         
163                         TestHelper.RemoveWarning (h);
164                 }
165                 [Test]
166                 public void AutoScroll ()
167                 {
168                         ScrollableControl sc = new ScrollableControl ();
169                         Assert.IsFalse (sc.AutoScroll, "#A1");
170                         Assert.AreEqual (0, sc.Controls.Count, "#A2");
171
172                         sc.AutoScroll = true;
173                         Assert.IsTrue(sc.AutoScroll, "#B1");
174                         Assert.AreEqual (0, sc.Controls.Count, "#B2");
175
176                         sc.AutoScroll = false;
177                         Assert.IsFalse (sc.AutoScroll, "#C1");
178                         Assert.AreEqual (0, sc.Controls.Count, "#C2");
179                 }
180
181                 [Test]
182                 public void AutoScrollMinSize ()
183                 {
184                         ScrollableControl sc = new ScrollableControl ();
185                         Assert.AreEqual (Size.Empty, sc.AutoScrollMinSize, "#A1");
186                         Assert.IsFalse (sc.AutoScroll, "#A2");
187
188                         sc.AutoScrollMinSize = Size.Empty;
189                         Assert.AreEqual (Size.Empty, sc.AutoScrollMinSize, "#B1");
190                         Assert.IsFalse (sc.AutoScroll, "#B2");
191
192                         sc.AutoScrollMinSize = new Size (10, 20);
193                         Assert.AreEqual (new Size (10, 20), sc.AutoScrollMinSize, "#C1");
194                         Assert.IsTrue (sc.AutoScroll, "#C2");
195
196                         sc.AutoScroll = false;
197                         Assert.AreEqual (new Size (10, 20), sc.AutoScrollMinSize, "#D1");
198                         Assert.IsFalse (sc.AutoScroll, "#D2");
199
200                         sc.AutoScrollMinSize = new Size (10, 20);
201                         Assert.AreEqual (new Size (10, 20), sc.AutoScrollMinSize, "#E1");
202                         Assert.IsFalse (sc.AutoScroll, "#E2");
203
204                         sc.AutoScrollMinSize = new Size (20, 20);
205                         Assert.AreEqual (new Size (20, 20), sc.AutoScrollMinSize, "#F1");
206                         Assert.IsTrue (sc.AutoScroll, "#F2");
207
208                         sc.AutoScroll = false;
209                         Assert.AreEqual (new Size (20, 20), sc.AutoScrollMinSize, "#G1");
210                         Assert.IsFalse (sc.AutoScroll, "#G2");
211
212                         sc.AutoScrollMinSize = Size.Empty;
213                         Assert.AreEqual (Size.Empty, sc.AutoScrollMinSize, "#H1");
214                         Assert.IsTrue (sc.AutoScroll, "#H2");
215
216                         sc.AutoScrollMinSize = new Size (10, 20);
217                         Assert.AreEqual (new Size (10, 20), sc.AutoScrollMinSize, "#I1");
218                         Assert.IsTrue (sc.AutoScroll, "#I2");
219
220                         sc.AutoScrollMinSize = Size.Empty;
221                         Assert.AreEqual (Size.Empty, sc.AutoScrollMinSize, "#J1");
222                         Assert.IsTrue (sc.AutoScroll, "#J2");
223                 }
224
225 #if NET_2_0
226                 [Test]
227                 public void MethodScrollToControl ()
228                 {
229                         if (TestHelper.RunningOnUnix)
230                                 Assert.Ignore ("Depends of scrollbar width, values are for Windows");
231
232                         Form f = new Form ();
233                         f.ShowInTaskbar = false;
234                         f.Show ();
235                         
236                         PublicScrollableControl sc = new PublicScrollableControl ();
237                         sc.Size = new Size (200, 200);
238                         sc.AutoScroll = true;
239                         
240                         f.Controls.Add (sc);
241                         
242                         Button b = new Button ();
243                         b.Top = 15;
244                         sc.Controls.Add (b);
245                         
246                         Button b2 = new Button ();
247                         b2.Top = 340;
248                         sc.Controls.Add (b2);
249                         
250                         Button b3 = new Button ();
251                         b3.Left = 280;
252                         sc.Controls.Add (b3);
253
254                         Assert.AreEqual (new Point (0, 0), sc.PublicScrollToControl (b), "A1");
255                         Assert.AreEqual (new Point (0, -180), sc.PublicScrollToControl (b2), "A2");
256                         Assert.AreEqual (new Point (-172, 0), sc.PublicScrollToControl (b3), "A3");
257
258                         sc.AutoScrollPosition = new Point (50, 70);
259
260                         Assert.AreEqual (new Point (0, -15), sc.PublicScrollToControl (b), "A4");
261                         Assert.AreEqual (new Point (0, -180), sc.PublicScrollToControl (b2), "A5");
262                         Assert.AreEqual (new Point (-172, 0), sc.PublicScrollToControl (b3), "A6");
263
264                         sc.AutoScrollPosition = new Point (150, 150);
265
266                         Assert.AreEqual (new Point (0, -15), sc.PublicScrollToControl (b), "A7");
267                         Assert.AreEqual (new Point (0, -180), sc.PublicScrollToControl (b2), "A8");
268                         Assert.AreEqual (new Point (-172, 0), sc.PublicScrollToControl (b3), "A9");
269                         
270                         f.Dispose ();
271                 }
272                 
273                 private class PublicScrollableControl : ScrollableControl
274                 {
275                         public Point PublicScrollToControl (Control activeControl)
276                         {
277                                 return base.ScrollToControl (activeControl);
278                         }
279                 }
280                 
281                 [Test]
282                 public void Padding ()
283                 {
284                         ScrollableControl c = new ScrollableControl ();
285                         c.Dock = DockStyle.Fill;
286                         c.Padding = new Padding (40);
287
288                         Assert.AreEqual (40, c.Padding.All, "A1");
289                         Assert.AreEqual (40, c.DockPadding.All, "A2");
290                         
291                         c.DockPadding.Right = 20;
292
293                         Assert.AreEqual (20, c.Padding.Right, "A3");
294                         Assert.AreEqual (20, c.DockPadding.Right, "A4");
295                         
296                         c.Padding = new Padding (40, 40, 40, 40);
297
298                         Assert.AreEqual (40, c.Padding.Right, "A5");
299                         Assert.AreEqual (40, c.DockPadding.Right, "A6");
300                         
301                         Form f = new Form ();
302                         f.Controls.Add (c);
303                         
304                         Button b = new Button ();
305                         c.Controls.Add (b);
306                         
307                         f.Show ();
308                         
309                         // Padding does not affect laying out the controls
310                         Assert.AreEqual (new Point (0, 0), b.Location, "A7");
311                         
312                         f.Close ();
313                         f.Dispose ();
314                 }
315
316                 // Even if padding is not directly affecting the layout, it can
317                 // cause the ScrollableControl instance to show the scrollbars
318                 // *after* the first access to DockPadding
319                 [Test]
320                 public void DockPaddingScroll ()
321                 {
322                         ScrollableControl scrollable = new ScrollableControl ();
323                         scrollable.Padding = new Padding (10);
324                         scrollable.Size = new Size (50, 50);
325                         scrollable.AutoScroll = true;
326
327                         Control c = new Control ();
328                         c.Size = scrollable.Size; // Same size as parent, shouldn' need scrollbars
329                         c.Parent = scrollable;
330
331                         Form f = new Form ();
332                         f.Controls.Add (scrollable);
333
334                         f.Show ();
335
336                         Assert.AreEqual (false, scrollable.VerticalScroll.Visible, "#A0");
337
338                         ScrollableControl.DockPaddingEdges dock_padding = scrollable.DockPadding;
339                         Assert.IsTrue (dock_padding != null, "#B0");
340
341                         // Refresh the layout, now that is affected by the creation of DockPadding
342                         scrollable.PerformLayout ();
343
344                         Assert.AreEqual (true, scrollable.VerticalScroll.Visible, "#C0");
345                 }
346 #endif
347
348                 // Tests Xamarin-2562
349                 [Test]
350                 public void DisplayRectangle_SamePadding ()
351                 {
352                         using (var c = new ScrollableControl ())
353                         {
354                                 c.Size = new Size (100, 100);
355                                 c.Padding = new Padding (4);
356                                 Assert.AreEqual (new Rectangle (0, 0, 100, 100), c.ClientRectangle);
357                                 Assert.AreEqual (new Rectangle (4, 4, 92, 92), c.DisplayRectangle);
358                         }
359                 }
360
361                 [Test]
362                 public void DisplayRectangle_DifferentPadding ()
363                 {
364                         using (var c = new ScrollableControl ())
365                         {
366                                 c.Size = new Size (100, 100);
367                                 c.Padding = new Padding (1, 2, 3, 4);
368                                 Assert.AreEqual (new Rectangle (0, 0, 100, 100), c.ClientRectangle);
369                                 Assert.AreEqual (new Rectangle (1, 2, 96, 94), c.DisplayRectangle);
370                         }
371                 }
372
373                 [Test]
374                 public void DockPadding()
375                 {
376                         using (var c = new ScrollableControl ())
377                         {
378                                 c.Padding = new Padding(4);
379                                 Assert.AreEqual (4, c.DockPadding.All);
380                         }
381                 }
382         }
383 }