2008-11-06 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / DefaultLayoutTest.cs
1 using System;
2 using System.Drawing;
3 using System.Windows.Forms;
4
5 using NUnit.Framework;
6 #if NET_2_0
7 using System.Collections.Generic;
8 #endif
9 namespace MonoTests.System.Windows.Forms
10 {
11         [TestFixture]
12         public class DefaultLayoutTest : TestHelper
13         {
14                 int event_count;
15                 LayoutEventArgs most_recent_args;
16
17                 void p_Layout (object sender, LayoutEventArgs e)
18                 {
19                         event_count ++;
20                         most_recent_args = e;
21                 }
22
23                 [Test]
24                 public void AnchorLayoutEvents ()
25                 {
26                         Panel p;
27                         Button b;
28
29                         p = new Panel ();
30
31                         b = new Button ();
32                         p.Controls.Add (b);
33
34                         p.Layout += new LayoutEventHandler (p_Layout);
35
36                         /* set the button's anchor to something different */
37                         b.Anchor = AnchorStyles.Bottom;
38                         Assert.AreEqual (1, event_count, "1");
39                         Assert.AreEqual ("Anchor", most_recent_args.AffectedProperty, "2");
40
41                         /* reset it to something new with the panel's layout suspended */
42                         event_count = 0;
43                         p.SuspendLayout ();
44                         b.Anchor = AnchorStyles.Top;
45                         Assert.AreEqual (0, event_count, "3");
46                         p.ResumeLayout ();
47                         Assert.AreEqual (1, event_count, "4");
48                         Assert.AreEqual (null, most_recent_args.AffectedProperty, "5");
49
50                         /* with the anchor style set to something, resize the parent */
51                         event_count = 0;
52                         p.Size = new Size (500, 500);
53                         Assert.AreEqual (1, event_count, "6");
54                         Assert.AreEqual ("Bounds", most_recent_args.AffectedProperty, "7");
55
56                         /* now try it with layout suspended */
57                         event_count = 0;
58                         p.SuspendLayout ();
59                         p.Size = new Size (400, 400);
60                         Assert.AreEqual (0, event_count, "8");
61                         p.ResumeLayout ();
62                         Assert.AreEqual (1, event_count, "9");
63                         Assert.AreEqual (null, most_recent_args.AffectedProperty, "10");
64
65                         /* with the anchor style set to something, resize the child */
66                         event_count = 0;
67                         b.Size = new Size (100, 100);
68                         Assert.AreEqual (1, event_count, "11");
69                         Assert.AreEqual ("Bounds", most_recent_args.AffectedProperty, "12");
70
71                         /* and again with layout suspended */
72                         event_count = 0;
73                         p.SuspendLayout ();
74                         b.Size = new Size (200, 200);
75                         Assert.AreEqual (0, event_count, "13");
76                         p.ResumeLayout ();
77                         Assert.AreEqual (1, event_count, "14");
78                         Assert.AreEqual (null, most_recent_args.AffectedProperty, "15");
79                 }
80
81                 [Test]
82                 public void AnchorTopLeftTest ()
83                 {
84                         Form f = new Form ();
85                         f.ShowInTaskbar = false;
86
87                         f.Size = new Size (200, 200);
88
89                         Button b = new Button ();
90                         b.Size = new Size (100, 100);
91                         b.Anchor = AnchorStyles.Top | AnchorStyles.Left;
92
93                         f.Controls.Add (b);
94
95                         Assert.AreEqual (0, b.Left, "1");
96                         Assert.AreEqual (0, b.Top, "2");
97                         f.Size = new Size (300, 300);
98
99                         Assert.AreEqual (0, b.Left, "3");
100                         Assert.AreEqual (0, b.Top, "4");
101                         
102                         f.Dispose ();
103                 }
104
105                 [Test]
106                 public void AnchorTopRightTest ()
107                 {
108                         Form f = new Form ();
109                         f.ShowInTaskbar = false;
110
111                         f.Size = new Size (200, 200);
112
113                         Button b = new Button ();
114                         b.Size = new Size (100, 100);
115                         b.Anchor = AnchorStyles.Top | AnchorStyles.Right;
116
117                         f.Controls.Add (b);
118
119                         Assert.AreEqual (0, b.Left, "1");
120                         Assert.AreEqual (0, b.Top, "2");
121
122                         f.Size = new Size (300, 300);
123
124                         Assert.AreEqual (100, b.Left, "3");
125                         Assert.AreEqual (0, b.Top, "4");
126                         
127                         f.Dispose ();
128                 }
129
130                 [Test]
131                 public void AnchorLeftRightTest ()
132                 {
133                         Form f = new Form ();
134                         f.ShowInTaskbar = false;
135
136                         f.Size = new Size (200, 200);
137
138                         Button b = new Button ();
139                         b.Size = new Size (100, 100);
140                         b.Anchor = AnchorStyles.Left | AnchorStyles.Right;
141
142                         f.Controls.Add (b);
143
144                         Assert.AreEqual (0, b.Left, "1");
145                         Assert.AreEqual (100, b.Right, "2");
146
147                         f.Size = new Size (300, 300);
148
149                         Assert.AreEqual (0, b.Left, "3");
150                         Assert.AreEqual (200, b.Right, "4");
151                         
152                         f.Dispose ();
153                 }
154
155                 [Test]
156                 public void AnchorBottomLeftTest ()
157                 {
158                         Form f = new Form ();
159                         f.ShowInTaskbar = false;
160
161                         f.Size = new Size (200, 200);
162
163                         Button b = new Button ();
164                         b.Size = new Size (100, 100);
165                         b.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
166
167                         f.Controls.Add (b);
168
169                         Assert.AreEqual (0, b.Left, "1");
170                         Assert.AreEqual (0, b.Top, "2");
171
172                         f.Size = new Size (300, 300);
173
174                         Assert.AreEqual (0, b.Left, "3");
175                         Assert.AreEqual (100, b.Top, "4");
176                         
177                         f.Dispose ();
178                 }
179
180                 [Test]
181                 public void AnchorBottomRightTest ()
182                 {
183                         Form f = new Form ();
184                         f.ShowInTaskbar = false;
185
186                         f.Size = new Size (200, 200);
187
188                         Button b = new Button ();
189                         b.Size = new Size (100, 100);
190                         b.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
191
192                         f.Controls.Add (b);
193
194                         Assert.AreEqual (0, b.Left, "1");
195                         Assert.AreEqual (0, b.Top, "2");
196
197                         f.Size = new Size (300, 300);
198
199                         Assert.AreEqual (100, b.Left, "3");
200                         Assert.AreEqual (100, b.Top, "4");
201                         
202                         f.Dispose ();
203                 }
204
205                 [Test]
206                 public void AnchorTopBottomTest ()
207                 {
208                         Form f = new Form ();
209                         f.ShowInTaskbar = false;
210
211                         f.Size = new Size (200, 200);
212
213                         Button b = new Button ();
214                         b.Size = new Size (100, 100);
215                         b.Anchor = AnchorStyles.Top | AnchorStyles.Bottom;
216
217                         f.Controls.Add (b);
218
219                         Assert.AreEqual (0, b.Top, "1");
220                         Assert.AreEqual (100, b.Bottom, "2");
221
222                         f.Size = new Size (300, 300);
223
224                         Assert.AreEqual (0, b.Top, "3");
225                         Assert.AreEqual (200, b.Bottom, "4");
226                         
227                         f.Dispose ();
228                 }
229
230                 // Unit test version of the test case in bug #80336
231                 [Test]
232                 public void AnchorSuspendLayoutTest ()
233                 {
234                         Form f = new Form ();
235                         f.ShowInTaskbar = false;
236
237                         f.SuspendLayout ();
238
239                         Button b = new Button ();
240                         b.Size = new Size (100, 100);
241
242                         f.Controls.Add (b);
243
244                         f.Size = new Size (200, 200);
245
246                         b.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
247
248                         Assert.AreEqual (0, b.Top, "1");
249                         Assert.AreEqual (0, b.Left, "2");
250
251                         f.Size = new Size (300, 300);
252
253                         Assert.AreEqual (0, b.Top, "3");
254                         Assert.AreEqual (0, b.Left, "4");
255
256                         f.ResumeLayout();
257
258                         Assert.AreEqual (100, b.Top, "5");
259                         Assert.AreEqual (100, b.Left, "6");
260                         
261                         f.Dispose ();
262                 }
263
264                 // another variant of AnchorSuspendLayoutTest1, with
265                 // the SuspendLayout moved after the Anchor
266                 // assignment.
267                 [Test]
268                 public void AnchorSuspendLayoutTest2 ()
269                 {
270                         Form f = new Form ();
271                         f.ShowInTaskbar = false;
272
273                         Button b = new Button ();
274                         b.Size = new Size (100, 100);
275
276                         f.Controls.Add (b);
277
278                         f.Size = new Size (200, 200);
279
280                         b.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
281
282                         Assert.AreEqual (0, b.Top, "1");
283                         Assert.AreEqual (0, b.Left, "2");
284
285                         f.SuspendLayout ();
286
287                         f.Size = new Size (300, 300);
288
289                         Assert.AreEqual (0, b.Top, "3");
290                         Assert.AreEqual (0, b.Left, "4");
291
292                         f.ResumeLayout();
293
294                         Assert.AreEqual (100, b.Top, "5");
295                         Assert.AreEqual (100, b.Left, "6");
296                         
297                         f.Dispose ();
298                 }
299
300                 // yet another variant, this time with no Suspend/Resume.
301                 [Test]
302                 public void AnchorSuspendLayoutTest3 ()
303                 {
304                         Form f = new Form ();
305                         f.ShowInTaskbar = false;
306
307                         Button b = new Button ();
308                         b.Size = new Size (100, 100);
309
310                         f.Controls.Add (b);
311
312                         f.Size = new Size (200, 200);
313
314                         b.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
315
316                         Assert.AreEqual (0, b.Top, "1");
317                         Assert.AreEqual (0, b.Left, "2");
318
319                         f.Size = new Size (300, 300);
320
321                         Assert.AreEqual (100, b.Top, "5");
322                         Assert.AreEqual (100, b.Left, "6");
323                         
324                         f.Dispose ();
325                 }
326
327                 private string event_raised = string.Empty;
328
329                 [Test]
330                 public void TestAnchorDockInteraction ()
331                 {
332                         Panel p = new Panel ();
333                         p.DockChanged += new EventHandler (DockChanged_Handler);
334
335                         Assert.AreEqual (AnchorStyles.Top | AnchorStyles.Left, p.Anchor, "A1");
336                         Assert.AreEqual (DockStyle.None, p.Dock, "A2");
337
338                         p.Dock = DockStyle.Right;
339                         Assert.AreEqual (AnchorStyles.Top | AnchorStyles.Left, p.Anchor, "A3");
340                         Assert.AreEqual (DockStyle.Right, p.Dock, "A4");
341                         Assert.AreEqual ("DockStyleChanged", event_raised, "A5");
342                         event_raised = string.Empty;
343
344                         p.Anchor = AnchorStyles.Bottom;
345                         Assert.AreEqual (AnchorStyles.Bottom, p.Anchor, "A6");
346                         Assert.AreEqual (DockStyle.None, p.Dock, "A7");
347                         Assert.AreEqual (string.Empty, event_raised, "A8");
348
349                         p.Dock = DockStyle.Fill;
350                         Assert.AreEqual (AnchorStyles.Top | AnchorStyles.Left, p.Anchor, "A9");
351                         Assert.AreEqual (DockStyle.Fill, p.Dock, "A10");
352                         Assert.AreEqual ("DockStyleChanged", event_raised, "A11");
353                         event_raised = string.Empty;
354
355                         p.Anchor = AnchorStyles.Top | AnchorStyles.Left;
356                         Assert.AreEqual (AnchorStyles.Top | AnchorStyles.Left, p.Anchor, "A12");
357                         Assert.AreEqual (DockStyle.Fill, p.Dock, "A13");
358                         Assert.AreEqual (string.Empty, event_raised, "A14");
359
360                         p.Dock = DockStyle.None;
361                         Assert.AreEqual (AnchorStyles.Top | AnchorStyles.Left, p.Anchor, "A15");
362                         Assert.AreEqual (DockStyle.None, p.Dock, "A16");
363                         Assert.AreEqual ("DockStyleChanged", event_raised, "A17");
364                         event_raised = string.Empty;
365
366                         p.Anchor = AnchorStyles.Bottom;
367                         p.Dock = DockStyle.None;
368                         Assert.AreEqual (AnchorStyles.Bottom, p.Anchor, "A18");
369                         Assert.AreEqual (DockStyle.None, p.Dock, "A19");
370                         Assert.AreEqual (string.Empty, event_raised, "A20");
371                 }
372
373                 public void DockChanged_Handler (object sender, EventArgs e)
374                 {
375                         event_raised += "DockStyleChanged";
376                 }
377
378                 [Test]  // bug #80917
379                 [Category ("NotWorking")]
380                 public void BehaviorOverriddenDisplayRectangle ()
381                 {
382                         Control c = new Control ();
383                         c.Anchor |= AnchorStyles.Bottom;
384                         c.Size = new Size (100, 100);
385
386                         Form f = new DisplayRectangleForm ();
387                         f.Controls.Add (c);
388                         f.ShowInTaskbar = false;
389                         f.Show ();
390                         
391                         Assert.AreEqual (new Size (100, 100), c.Size, "A1");
392                         
393                         f.Dispose ();
394                 }
395
396                 private class DisplayRectangleForm : Form
397                 {
398                         public override Rectangle DisplayRectangle
399                         {
400                                 get { return Rectangle.Empty; }
401                         }
402                 }
403                 
404                 [Test]  // bug 80912
405                 public void AnchoredControlWithZeroWidthAndHeight ()
406                 {
407                         Form f = new Form ();
408                         f.ShowInTaskbar = false;
409                         
410                         Control c = new Control ();
411                         c.Anchor = AnchorStyles.Left | AnchorStyles.Right;
412                         f.Controls.Add (c);
413
414                         Assert.AreEqual (new Rectangle (0, 0, 0, 0), c.Bounds, "N1");
415                 }
416
417                 [Test] // bug 81694
418                 public void TestNestedControls ()
419                 {
420                         MainForm f = new MainForm ();
421                         f.ShowInTaskbar = false;
422                         
423                         f.Show ();
424                         Assert.AreEqual (new Rectangle (210, 212, 75, 23), f._userControl._button2.Bounds, "K1");
425                         
426                         f.Dispose ();
427                 }
428
429                 [Test] // bug 81695
430                 public void TestNestedControls2 ()
431                 {
432                         MainForm f = new MainForm ();
433                         f.ShowInTaskbar = false;
434
435                         f.Show ();
436                         
437                         Size s = f.Size;
438                         f.Size = new Size (10, 10);
439                         f.Size = s;
440                         
441                         Assert.AreEqual (new Rectangle (210, 212, 75, 23), f._userControl._button2.Bounds, "K1");
442
443                         f.Dispose ();
444                 }
445
446                 private class MainForm : Form
447                 {
448                         public UserControl1 _userControl;
449                         
450                         public MainForm ()
451                         {
452                                 SuspendLayout ();
453                                 // 
454                                 // _userControl
455                                 // 
456                                 _userControl = new UserControl1 ();
457                                 _userControl.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right);
458                                 _userControl.BackColor = Color.White;
459                                 _userControl.Location = new Point (8, 8);
460                                 _userControl.Size = new Size (288, 238);
461                                 _userControl.TabIndex = 0;
462                                 Controls.Add (_userControl);
463                                 // 
464                                 // MainForm
465                                 // 
466                                 ClientSize = new Size (304, 280);
467                                 Location = new Point (250, 100);
468                                 StartPosition = FormStartPosition.Manual;
469                                 Text = "bug #81694";
470                                 ResumeLayout (false);
471                         }
472                 }
473                 
474                 private class UserControl1 : UserControl
475                 {
476                         private Button _button1;
477                         public Button _button2;
478
479                         public UserControl1 ()
480                         {
481                                 SuspendLayout ();
482                                 // 
483                                 // _button1
484                                 // 
485                                 _button1 = new Button ();
486                                 _button1.Location = new Point (4, 4);
487                                 _button1.Size = new Size (75, 23);
488                                 _button1.TabIndex = 0;
489                                 _button1.Text = "Button1";
490                                 Controls.Add (_button1);
491                                 // 
492                                 // _button2
493                                 // 
494                                 _button2 = new Button ();
495                                 _button2.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right);
496                                 _button2.Location = new Point (210, 212);
497                                 _button2.Size = new Size (75, 23);
498                                 _button2.TabIndex = 1;
499                                 _button2.Text = "Button2";
500                                 Controls.Add (_button2);
501                                 // 
502                                 // UserControl1
503                                 // 
504                                 BackColor = Color.White;
505                                 ClientSize = new Size (288, 238);
506                                 ResumeLayout (false);
507                         }
508                 }
509
510 #if NET_2_0
511                 [Test]
512                 public void TestDockFillWithPadding ()
513                 {
514                         Form f = new Form ();
515                         f.ShowInTaskbar = false;
516                         f.Padding = new Padding (15, 15, 15, 15);
517
518                         Control c = new Control ();
519                         c.Dock = DockStyle.Fill;
520                         f.Controls.Add (c);
521
522                         f.Show ();
523                         Assert.AreEqual (new Size (f.ClientSize.Width - 30, f.ClientSize.Height - 30), c.Size, "K1");
524
525                         f.Dispose ();
526                 }
527 #endif
528
529                 [Test]
530                 public void Bug82762 ()
531                 {
532                         if (TestHelper.RunningOnUnix)
533                                 Assert.Ignore ("WM Size dependent");
534                                 
535                         Form f = new Form ();
536                         f.ShowInTaskbar = false;
537                         
538                         Button b = new Button ();
539                         b.Size = new Size (100, 100);
540                         b.Anchor = AnchorStyles.None;
541                         f.Controls.Add (b);
542                         
543                         f.Show ();
544                         
545                         Assert.AreEqual (new Rectangle (0, 0, 100, 100), b.Bounds, "A1");
546                         
547                         f.ClientSize = new Size (600, 600);
548
549                         Assert.AreEqual (new Rectangle (158, 168, 100, 100), b.Bounds, "A2");
550
551                         f.Close ();
552                         f.Dispose ();
553                 }
554                 
555                 [Test]
556                 public void Bug82805 ()
557                 {
558                         Control c1 = new Control ();
559                         c1.Size = new Size (100, 100);
560                         Control c2 = new Control ();
561                         c2.Size = new Size (100, 100);
562
563                         c2.SuspendLayout ();
564                         c1.Anchor = AnchorStyles.Left | AnchorStyles.Right;
565                         c2.Controls.Add (c1);
566                         c2.Size = new Size (200, 200);
567                         c2.ResumeLayout ();
568
569                         Assert.AreEqual (200, c1.Width, "A1");
570                 }
571                 
572 #if NET_2_0
573                 [Test]
574                 public void DockedAutoSizeControls ()
575                 {
576                         Form f = new Form ();
577                         f.ShowInTaskbar = false;
578                         
579                         Button b = new Button ();
580                         b.Text = "Yo";
581                         b.AutoSize = true;
582                         b.AutoSizeMode = AutoSizeMode.GrowAndShrink;
583                         b.Width = 200;
584                         b.Dock = DockStyle.Left;
585                         f.Controls.Add (b);
586                         
587                         f.Show ();
588                         
589                         if (b.Width >= 200)
590                                 Assert.Fail ("button should be less than 200 width: actual {0}", b.Width);
591                         
592                         f.Close ();
593                         f.Dispose ();
594                 }
595 #endif
596                 
597                 [Test]  // bug #81199
598                 public void NestedControls ()
599                 {
600                         Form f = new Form ();
601                         f.ShowInTaskbar = false;
602                         
603                         MyUserControl c = new MyUserControl ();
604                         c.Dock = DockStyle.Fill;
605                         c.Size = new Size (500, 500);
606                         
607                         f.SuspendLayout ();
608                         f.Controls.Add (c);
609                         f.ClientSize = new Size (500, 500);
610                         f.ResumeLayout (false);
611                         
612                         f.Show ();
613                         
614                         Assert.AreEqual (new Size (600, 600), c.lv.Size, "I1");
615                         f.Close ();
616                 }
617                 
618                 private class MyUserControl : UserControl
619                 {
620                         public ListView lv;
621                         
622                         public MyUserControl ()
623                         {
624                                 lv = new ListView ();
625                                 SuspendLayout ();
626                                 lv.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
627                                 lv.Size = new Size (300, 300);
628                                 
629                                 Controls.Add (lv);
630                                 Size = new Size (200, 200);
631                                 ResumeLayout (false);
632                         }
633                 }
634         }
635
636         [TestFixture]   
637         public class DockingTests : TestHelper
638         {
639                 Form form;
640                 Panel panel;
641
642                 int event_count;
643
644                 [SetUp]
645                 protected override void SetUp () {
646                         form = new Form ();
647                         form.ShowInTaskbar = false;
648                         form.Size = new Size (400, 400);
649                         panel = new Panel ();
650                         form.Controls.Add (panel);
651                         event_count = 0;
652                         base.SetUp ();
653                 }
654
655                 [TearDown]
656                 protected override void TearDown () {
657                         form.Dispose ();
658                         base.TearDown ();
659                 }
660
661                 void IncrementEventCount (object o, EventArgs args)
662                 {
663                         event_count++;
664                 }
665
666                 [Test]
667                 public void TestDockSizeChangedEvent ()
668                 {
669                         panel.SizeChanged += new EventHandler (IncrementEventCount);
670                         panel.Dock = DockStyle.Bottom;
671                         Assert.AreEqual (1, event_count);
672                 }
673
674                 [Test]
675                 public void TestDockLocationChangedEvent ()
676                 {
677                         panel.LocationChanged += new EventHandler (IncrementEventCount);
678                         panel.Dock = DockStyle.Bottom;
679                         Assert.AreEqual (1, event_count);
680                 }
681
682                 [Test]
683                 public void TestDockFillFirst ()
684                 {
685                         Form f = new Form ();
686                         f.ShowInTaskbar = false;
687
688                         Panel b1 = new Panel ();
689                         Panel b2 = new Panel ();
690
691                         b1.Dock = DockStyle.Fill;
692                         b2.Dock = DockStyle.Left;
693
694                         f.Controls.Add (b1);
695                         f.Controls.Add (b2);
696
697                         f.Show ();
698                         Assert.AreEqual (new Rectangle (b2.Width, 0, f.ClientRectangle.Width - b2.Width, f.ClientRectangle.Height), b1.Bounds, "A1");
699                         Assert.AreEqual (new Rectangle (0, 0, 200, f.ClientRectangle.Height), b2.Bounds, "A2");
700                         f.Dispose ();
701                 }
702
703                 [Test]
704                 public void TestDockFillLast ()
705                 {
706                         Form f = new Form ();
707                         f.ShowInTaskbar = false;
708
709                         Panel b1 = new Panel ();
710                         Panel b2 = new Panel ();
711
712                         b1.Dock = DockStyle.Fill;
713                         b2.Dock = DockStyle.Left;
714
715                         f.Controls.Add (b2);
716                         f.Controls.Add (b1);
717
718                         f.Show ();
719                         Assert.AreEqual (new Rectangle (0, 0, f.ClientRectangle.Width, f.ClientRectangle.Height), b1.Bounds, "B1");
720                         Assert.AreEqual (new Rectangle (0, 0, 200, f.ClientRectangle.Height), b2.Bounds, "B2");
721                         f.Dispose ();
722                 }
723                 
724                 [Test]  // bug #81397
725                 public void TestDockingWithCustomDisplayRectangle ()
726                 {
727                         MyControl mc = new MyControl ();
728                         mc.Size = new Size (200, 200);
729                         
730                         Control c = new Control ();
731                         c.Dock = DockStyle.Fill;
732                         
733                         mc.Controls.Add (c);
734                         
735                         Form f = new Form ();
736                         f.ShowInTaskbar = false;
737                         
738                         f.Controls.Add (mc);
739                         f.Show ();
740                         
741                         Assert.AreEqual (new Point (20, 20), c.Location, "A1");
742                         Assert.AreEqual (new Size (160, 160), c.Size, "A2");
743                         
744                         f.Dispose ();
745                 }
746                 
747                 private class MyControl : Control
748                 {
749                         public override Rectangle DisplayRectangle { 
750                                 get { return new Rectangle (20, 20, this.Width - 40, this.Height - 40); }
751                         }
752                 }
753                 [Test]
754                 public void ResettingDockToNone ()
755                 {
756                         Form f = new Form ();
757                         f.ShowInTaskbar = false;
758                         f.ClientSize = new Size (300, 300);
759                         
760                         Control c = new Control ();
761                         c.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
762                         
763                         f.Controls.Add (c);
764                         
765                         f.Show ();
766                         
767                         f.ClientSize = new Size (350, 350);
768
769                         Assert.IsTrue (c.Left > 0, string.Format ("A1: c.Left ({0}) must be greater than 0", c.Left));
770                         Assert.IsTrue (c.Top > 0, string.Format ("A2: c.Top ({0}) must be greater than 0", c.Top));
771                         
772                         c.Dock = DockStyle.None;
773                         Assert.IsTrue (c.Left > 0, string.Format ("A3: c.Left ({0}) must be greater than 0", c.Left));
774                         Assert.IsTrue (c.Top > 0, string.Format ("A4: c.Top ({0}) must be greater than 0", c.Top));
775                         
776                         f.ClientSize = new Size (400, 400);
777                         Assert.IsTrue (c.Left > 70, string.Format ("A5: c.Left ({0}) must be greater than 70", c.Left));
778                         Assert.IsTrue (c.Top > 70, string.Format ("A6: c.Top ({0}) must be greater than 70", c.Top));
779                         
780                         f.Dispose ();
781                 }
782         }
783
784         [TestFixture]   
785         public class UndockingTests : TestHelper
786         {
787                 class TestPanel : Panel {
788
789                         public void InvokeSetBoundsCore ()
790                         {
791                                 SetBoundsCore (37, 37, 37, 37, BoundsSpecified.All);
792                         }
793
794                         public void InvokeUpdateBounds ()
795                         {
796                                 UpdateBounds (37, 37, 37, 37);
797                         }
798                 }
799
800                 Form form;
801                 TestPanel panel;
802
803                 [SetUp]
804                 protected override void SetUp () {
805                         form = new Form ();
806                         form.ShowInTaskbar = false;
807                         form.Size = new Size (400, 400);
808                         panel = new TestPanel ();
809                         form.Controls.Add (panel);
810                         base.SetUp ();
811                 }
812
813                 [TearDown]
814                 protected override void TearDown ()
815                 {
816                         form.Dispose ();
817                         base.TearDown ();
818                 }
819
820                 [Test]
821                 public void TestUndockDefaultLocation ()
822                 {
823                         Point loc = panel.Location;
824                         panel.Dock = DockStyle.Bottom;
825                         panel.Dock = DockStyle.None;
826                         Assert.AreEqual (loc, panel.Location);
827                 }
828
829                 [Test]
830                 public void TestUndockDefaultLocationVisible ()
831                 {
832                         form.Show ();
833                         Point loc = panel.Location;
834                         panel.Dock = DockStyle.Bottom;
835                         panel.Dock = DockStyle.None;
836                         Assert.AreEqual (loc, panel.Location);
837                 }
838
839                 [Test]
840                 public void TestUndockExplicitLeft ()
841                 {
842                         panel.Left = 150;
843                         panel.Dock = DockStyle.Top;
844                         panel.Dock = DockStyle.None;
845                         Assert.AreEqual (150, panel.Left);
846                 }
847
848                 [Test]
849                 public void TestUndockExplicitTop ()
850                 {
851                         panel.Top = 150;
852                         panel.Dock = DockStyle.Top;
853                         panel.Dock = DockStyle.None;
854                         Assert.AreEqual (150, panel.Top);
855                 }
856
857                 [Test]
858                 public void TestUndockExplicitLocation ()
859                 {
860                         panel.Location = new Point (50, 50);
861                         Point loc = panel.Location;
862                         panel.Dock = DockStyle.Bottom;
863                         panel.Dock = DockStyle.None;
864                         Assert.AreEqual (loc, panel.Location);
865                 }
866
867                 [Test]
868                 public void TestUndockExplicitLeftVisible ()
869                 {
870                         form.Show ();
871                         panel.Left = 150;
872                         panel.Dock = DockStyle.Top;
873                         panel.Dock = DockStyle.None;
874                         Assert.AreEqual (150, panel.Left);
875                 }
876
877                 [Test]
878                 public void TestUndockExplicitTopVisible ()
879                 {
880                         form.Show ();
881                         panel.Top = 150;
882                         panel.Dock = DockStyle.Top;
883                         panel.Dock = DockStyle.None;
884                         Assert.AreEqual (150, panel.Top);
885                 }
886
887                 [Test]
888                 public void TestUndockExplicitLocationVisible ()
889                 {
890                         form.Show ();
891                         panel.Location = new Point (50, 50);
892                         Point loc = panel.Location;
893                         panel.Dock = DockStyle.Bottom;
894                         panel.Dock = DockStyle.None;
895                         Assert.AreEqual (loc, panel.Location);
896                 }
897
898                 [Test]
899                 public void TestUndockDefaultSize ()
900                 {
901                         Size sz = panel.Size;
902                         panel.Dock = DockStyle.Fill;
903                         panel.Dock = DockStyle.None;
904                         Assert.AreEqual (sz, panel.Size);
905                 }
906
907                 [Test]
908                 public void TestUndockExplicitHeight ()
909                 {
910                         panel.Height = 50;
911                         panel.Dock = DockStyle.Left;
912                         panel.Dock = DockStyle.None;
913                         Assert.AreEqual (50, panel.Height);
914                 }
915
916                 [Test]
917                 public void TestUndockExplicitSize ()
918                 {
919                         panel.Size = new Size (50, 50);
920                         Size sz = panel.Size;
921                         panel.Dock = DockStyle.Fill;
922                         panel.Dock = DockStyle.None;
923                         Assert.AreEqual (sz, panel.Size);
924                 }
925
926                 [Test]
927                 public void TestUndockExplicitWidth ()
928                 {
929                         panel.Width = 50;
930                         panel.Dock = DockStyle.Top;
931                         panel.Dock = DockStyle.None;
932                         Assert.AreEqual (50, panel.Width);
933                 }
934
935                 [Test]
936                 public void TestUndockExplicitHeightVisible ()
937                 {
938                         form.Show ();
939                         panel.Height = 50;
940                         panel.Dock = DockStyle.Left;
941                         panel.Dock = DockStyle.None;
942                         Assert.AreEqual (50, panel.Height);
943                 }
944
945                 [Test]
946                 public void TestUndockExplicitSizeVisible ()
947                 {
948                         form.Show ();
949                         panel.Size = new Size (50, 50);
950                         Size sz = panel.Size;
951                         panel.Dock = DockStyle.Fill;
952                         panel.Dock = DockStyle.None;
953                         Assert.AreEqual (sz, panel.Size);
954                 }
955
956                 [Test]
957                 public void TestUndockExplicitWidthVisible ()
958                 {
959                         form.Show ();
960                         panel.Width = 50;
961                         panel.Dock = DockStyle.Top;
962                         panel.Dock = DockStyle.None;
963                         Assert.AreEqual (50, panel.Width);
964                 }
965
966                 [Test]
967                 public void TestUndockSetBounds ()
968                 {
969                         panel.SetBounds (50, 50, 50, 50, BoundsSpecified.All);
970                         panel.Dock = DockStyle.Top;
971                         panel.Dock = DockStyle.None;
972                         Assert.AreEqual (50, panel.Height, "Height");
973                         Assert.AreEqual (50, panel.Left, "Left");
974                         Assert.AreEqual (50, panel.Top, "Top");
975                         Assert.AreEqual (50, panel.Width, "Width");
976                 }
977
978                 [Test]
979                 public void TestUndockSetBoundsVisible ()
980                 {
981                         form.Show ();
982                         panel.SetBounds (50, 50, 50, 50, BoundsSpecified.All);
983                         panel.Dock = DockStyle.Top;
984                         panel.Dock = DockStyle.None;
985                         Assert.AreEqual (50, panel.Height, "Height");
986                         Assert.AreEqual (50, panel.Left, "Left");
987                         Assert.AreEqual (50, panel.Top, "Top");
988                         Assert.AreEqual (50, panel.Width, "Width");
989                 }
990
991                 [Test]
992                 public void TestUndockSetBoundsCore ()
993                 {
994                         panel.InvokeSetBoundsCore ();
995                         panel.Dock = DockStyle.Top;
996                         panel.Dock = DockStyle.None;
997                         Assert.AreEqual (37, panel.Height, "Height");
998                         Assert.AreEqual (37, panel.Left, "Left");
999                         Assert.AreEqual (37, panel.Top, "Top");
1000                         Assert.AreEqual (37, panel.Width, "Width");
1001                 }
1002
1003                 [Test]
1004                 public void TestUndockSetBoundsCoreVisible ()
1005                 {
1006                         form.Show ();
1007                         panel.InvokeSetBoundsCore ();
1008                         panel.Dock = DockStyle.Top;
1009                         panel.Dock = DockStyle.None;
1010                         Assert.AreEqual (37, panel.Height, "Height");
1011                         Assert.AreEqual (37, panel.Left, "Left");
1012                         Assert.AreEqual (37, panel.Top, "Top");
1013                         Assert.AreEqual (37, panel.Width, "Width");
1014                 }
1015
1016                 [Test]
1017                 public void TestUndockUpdateBounds ()
1018                 {
1019                         panel.InvokeUpdateBounds ();
1020                         panel.Dock = DockStyle.Top;
1021                         panel.Dock = DockStyle.None;
1022                         Assert.AreEqual (37, panel.Height, "Height");
1023                         Assert.AreEqual (37, panel.Left, "Left");
1024                         Assert.AreEqual (37, panel.Top, "Top");
1025                         Assert.AreEqual (37, panel.Width, "Width");
1026                 }
1027
1028                 [Test]
1029                 public void TestUndockUpdateBoundsVisible ()
1030                 {
1031                         form.Show ();
1032                         panel.InvokeUpdateBounds ();
1033                         panel.Dock = DockStyle.Top;
1034                         panel.Dock = DockStyle.None;
1035                         Assert.AreEqual (37, panel.Height, "Height");
1036                         Assert.AreEqual (37, panel.Left, "Left");
1037                         Assert.AreEqual (37, panel.Top, "Top");
1038                         Assert.AreEqual (37, panel.Width, "Width");
1039                 }
1040
1041 #if NET_2_0
1042                 [Test]
1043                 public void AutoSizeGrowOnlyControls_ShrinkWhenDocked ()
1044                 {
1045                         // For most controls that are AutoSized and support the
1046                         // AutoSizeMode property, if they are set to GrowOnly,
1047                         // they should not shrink.  However, they will shrink 
1048                         // even in GrowOnly mode if they are docked.
1049                         // Button is one exception to this rule.
1050                         Form f = new Form ();
1051                         f.ClientSize = new Size (300, 300);
1052                         f.ShowInTaskbar = false;
1053                         f.Show ();
1054
1055                         List<Type> types = new List<Type> ();
1056                         types.Add (typeof (TableLayoutPanel));
1057                         types.Add (typeof (FlowLayoutPanel));
1058                         types.Add (typeof (ToolStripContentPanel));
1059                         types.Add (typeof (Panel));
1060                         types.Add (typeof (GroupBox));
1061                         types.Add (typeof (UserControl));
1062                         foreach (Type t in types) {
1063                                 Control c = t.GetConstructor (Type.EmptyTypes).Invoke (null) as Control;
1064                                 c.AutoSize = true;
1065                                 t.GetProperty ("AutoSizeMode").SetValue (c, AutoSizeMode.GrowOnly, null);
1066                                 c.Bounds = new Rectangle (5, 5, 100, 100);
1067                                 f.Controls.Add (c);
1068                                 Assert.AreEqual (100, c.Height, "1 " + t.Name);
1069                                 c.Dock = DockStyle.Top;
1070                                 Assert.IsFalse (100 == c.Height, "2 " + t.Name);
1071                                 f.Controls.Remove (c);
1072                                 c.Dispose ();
1073                         }
1074
1075                         f.Dispose ();
1076                 }
1077
1078                 [Test]
1079                 public void AutoSizeGrowOnlyButtons_DoNotShrinkWhenDocked ()
1080                 {
1081                         // For most controls that are AutoSized and support the
1082                         // AutoSizeMode property, if they are set to GrowOnly,
1083                         // they should not shrink.  However, they will shrink 
1084                         // even in GrowOnly mode if they are docked.
1085                         // Button is one exception to this rule.
1086                         Form f = new Form ();
1087                         f.ClientSize = new Size (300, 300);
1088                         f.ShowInTaskbar = false;
1089                         f.Show ();
1090
1091                         List<Type> types = new List<Type> ();
1092                         types.Add (typeof (Button));
1093                         foreach (Type t in types) {
1094                                 Control c = t.GetConstructor (Type.EmptyTypes).Invoke (null) as Control;
1095                                 c.AutoSize = true;
1096                                 t.GetProperty ("AutoSizeMode").SetValue (c, AutoSizeMode.GrowOnly, null);
1097                                 c.Bounds = new Rectangle (5, 5, 100, 100);
1098                                 f.Controls.Add (c);
1099                                 Assert.AreEqual (100, c.Height, "1 " + t.Name);
1100                                 c.Dock = DockStyle.Top;
1101                                 Assert.AreEqual (100, c.Height, "2 " + t.Name);
1102                                 f.Controls.Remove (c);
1103                                 c.Dispose ();
1104                         }
1105
1106                         f.Dispose ();
1107                 }
1108 #endif
1109         }
1110 }