2008-12-17 Bill Holmes <billholmes54@gmail.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
754 #if NET_2_0
755                 [Test]
756                 public void DockingPreferredSize ()
757                 {
758                         Form f = new Form ();
759                         f.ShowInTaskbar = false;
760                         f.ClientSize = new Size (300, 300);
761
762                         C1 c1 = new C1 ();
763                         c1.Size = new Size (100, 100);
764                         c1.Dock = DockStyle.Left;
765
766                         f.Controls.Add (c1);
767                         f.Show ();
768
769                         Assert.AreEqual (new Size (100, 300), c1.Size, "A1");
770
771                         f.Controls.Clear ();
772                         C2 c2 = new C2 ();
773                         c2.Size = new Size (100, 100);
774                         c2.Dock = DockStyle.Left;
775
776                         f.Controls.Add (c2);
777                         Assert.AreEqual (new Size (100, 300), c1.Size, "A2");
778
779                         f.Dispose ();
780                 }
781
782                 private class C1 : Panel
783                 {
784                         public override Size GetPreferredSize (Size proposedSize)
785                         {
786                                 Console.WriteLine ("HOYO!");
787                                 return new Size (200, 200);
788                         }
789                 }
790
791                 private class C2 : Panel
792                 {
793                         public override Size GetPreferredSize (Size proposedSize)
794                         {
795                                 return Size.Empty;
796                         }
797                 }
798 #endif
799
800                 [Test]
801                 public void ResettingDockToNone ()
802                 {
803                         Form f = new Form ();
804                         f.ShowInTaskbar = false;
805                         f.ClientSize = new Size (300, 300);
806                         
807                         Control c = new Control ();
808                         c.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
809                         
810                         f.Controls.Add (c);
811                         
812                         f.Show ();
813                         
814                         f.ClientSize = new Size (350, 350);
815
816                         Assert.IsTrue (c.Left > 0, string.Format ("A1: c.Left ({0}) must be greater than 0", c.Left));
817                         Assert.IsTrue (c.Top > 0, string.Format ("A2: c.Top ({0}) must be greater than 0", c.Top));
818                         
819                         c.Dock = DockStyle.None;
820                         Assert.IsTrue (c.Left > 0, string.Format ("A3: c.Left ({0}) must be greater than 0", c.Left));
821                         Assert.IsTrue (c.Top > 0, string.Format ("A4: c.Top ({0}) must be greater than 0", c.Top));
822                         
823                         f.ClientSize = new Size (400, 400);
824                         Assert.IsTrue (c.Left > 70, string.Format ("A5: c.Left ({0}) must be greater than 70", c.Left));
825                         Assert.IsTrue (c.Top > 70, string.Format ("A6: c.Top ({0}) must be greater than 70", c.Top));
826                         
827                         f.Dispose ();
828                 }
829         }
830
831         [TestFixture]   
832         public class UndockingTests : TestHelper
833         {
834                 class TestPanel : Panel {
835
836                         public void InvokeSetBoundsCore ()
837                         {
838                                 SetBoundsCore (37, 37, 37, 37, BoundsSpecified.All);
839                         }
840
841                         public void InvokeUpdateBounds ()
842                         {
843                                 UpdateBounds (37, 37, 37, 37);
844                         }
845                 }
846
847                 Form form;
848                 TestPanel panel;
849
850                 [SetUp]
851                 protected override void SetUp () {
852                         form = new Form ();
853                         form.ShowInTaskbar = false;
854                         form.Size = new Size (400, 400);
855                         panel = new TestPanel ();
856                         form.Controls.Add (panel);
857                         base.SetUp ();
858                 }
859
860                 [TearDown]
861                 protected override void TearDown ()
862                 {
863                         form.Dispose ();
864                         base.TearDown ();
865                 }
866
867                 [Test]
868                 public void TestUndockDefaultLocation ()
869                 {
870                         Point loc = panel.Location;
871                         panel.Dock = DockStyle.Bottom;
872                         panel.Dock = DockStyle.None;
873                         Assert.AreEqual (loc, panel.Location);
874                 }
875
876                 [Test]
877                 public void TestUndockDefaultLocationVisible ()
878                 {
879                         form.Show ();
880                         Point loc = panel.Location;
881                         panel.Dock = DockStyle.Bottom;
882                         panel.Dock = DockStyle.None;
883                         Assert.AreEqual (loc, panel.Location);
884                 }
885
886                 [Test]
887                 public void TestUndockExplicitLeft ()
888                 {
889                         panel.Left = 150;
890                         panel.Dock = DockStyle.Top;
891                         panel.Dock = DockStyle.None;
892                         Assert.AreEqual (150, panel.Left);
893                 }
894
895                 [Test]
896                 public void TestUndockExplicitTop ()
897                 {
898                         panel.Top = 150;
899                         panel.Dock = DockStyle.Top;
900                         panel.Dock = DockStyle.None;
901                         Assert.AreEqual (150, panel.Top);
902                 }
903
904                 [Test]
905                 public void TestUndockExplicitLocation ()
906                 {
907                         panel.Location = new Point (50, 50);
908                         Point loc = panel.Location;
909                         panel.Dock = DockStyle.Bottom;
910                         panel.Dock = DockStyle.None;
911                         Assert.AreEqual (loc, panel.Location);
912                 }
913
914                 [Test]
915                 public void TestUndockExplicitLeftVisible ()
916                 {
917                         form.Show ();
918                         panel.Left = 150;
919                         panel.Dock = DockStyle.Top;
920                         panel.Dock = DockStyle.None;
921                         Assert.AreEqual (150, panel.Left);
922                 }
923
924                 [Test]
925                 public void TestUndockExplicitTopVisible ()
926                 {
927                         form.Show ();
928                         panel.Top = 150;
929                         panel.Dock = DockStyle.Top;
930                         panel.Dock = DockStyle.None;
931                         Assert.AreEqual (150, panel.Top);
932                 }
933
934                 [Test]
935                 public void TestUndockExplicitLocationVisible ()
936                 {
937                         form.Show ();
938                         panel.Location = new Point (50, 50);
939                         Point loc = panel.Location;
940                         panel.Dock = DockStyle.Bottom;
941                         panel.Dock = DockStyle.None;
942                         Assert.AreEqual (loc, panel.Location);
943                 }
944
945                 [Test]
946                 public void TestUndockDefaultSize ()
947                 {
948                         Size sz = panel.Size;
949                         panel.Dock = DockStyle.Fill;
950                         panel.Dock = DockStyle.None;
951                         Assert.AreEqual (sz, panel.Size);
952                 }
953
954                 [Test]
955                 public void TestUndockExplicitHeight ()
956                 {
957                         panel.Height = 50;
958                         panel.Dock = DockStyle.Left;
959                         panel.Dock = DockStyle.None;
960                         Assert.AreEqual (50, panel.Height);
961                 }
962
963                 [Test]
964                 public void TestUndockExplicitSize ()
965                 {
966                         panel.Size = new Size (50, 50);
967                         Size sz = panel.Size;
968                         panel.Dock = DockStyle.Fill;
969                         panel.Dock = DockStyle.None;
970                         Assert.AreEqual (sz, panel.Size);
971                 }
972
973                 [Test]
974                 public void TestUndockExplicitWidth ()
975                 {
976                         panel.Width = 50;
977                         panel.Dock = DockStyle.Top;
978                         panel.Dock = DockStyle.None;
979                         Assert.AreEqual (50, panel.Width);
980                 }
981
982                 [Test]
983                 public void TestUndockExplicitHeightVisible ()
984                 {
985                         form.Show ();
986                         panel.Height = 50;
987                         panel.Dock = DockStyle.Left;
988                         panel.Dock = DockStyle.None;
989                         Assert.AreEqual (50, panel.Height);
990                 }
991
992                 [Test]
993                 public void TestUndockExplicitSizeVisible ()
994                 {
995                         form.Show ();
996                         panel.Size = new Size (50, 50);
997                         Size sz = panel.Size;
998                         panel.Dock = DockStyle.Fill;
999                         panel.Dock = DockStyle.None;
1000                         Assert.AreEqual (sz, panel.Size);
1001                 }
1002
1003                 [Test]
1004                 public void TestUndockExplicitWidthVisible ()
1005                 {
1006                         form.Show ();
1007                         panel.Width = 50;
1008                         panel.Dock = DockStyle.Top;
1009                         panel.Dock = DockStyle.None;
1010                         Assert.AreEqual (50, panel.Width);
1011                 }
1012
1013                 [Test]
1014                 public void TestUndockSetBounds ()
1015                 {
1016                         panel.SetBounds (50, 50, 50, 50, BoundsSpecified.All);
1017                         panel.Dock = DockStyle.Top;
1018                         panel.Dock = DockStyle.None;
1019                         Assert.AreEqual (50, panel.Height, "Height");
1020                         Assert.AreEqual (50, panel.Left, "Left");
1021                         Assert.AreEqual (50, panel.Top, "Top");
1022                         Assert.AreEqual (50, panel.Width, "Width");
1023                 }
1024
1025                 [Test]
1026                 public void TestUndockSetBoundsVisible ()
1027                 {
1028                         form.Show ();
1029                         panel.SetBounds (50, 50, 50, 50, BoundsSpecified.All);
1030                         panel.Dock = DockStyle.Top;
1031                         panel.Dock = DockStyle.None;
1032                         Assert.AreEqual (50, panel.Height, "Height");
1033                         Assert.AreEqual (50, panel.Left, "Left");
1034                         Assert.AreEqual (50, panel.Top, "Top");
1035                         Assert.AreEqual (50, panel.Width, "Width");
1036                 }
1037
1038                 [Test]
1039                 public void TestUndockSetBoundsCore ()
1040                 {
1041                         panel.InvokeSetBoundsCore ();
1042                         panel.Dock = DockStyle.Top;
1043                         panel.Dock = DockStyle.None;
1044                         Assert.AreEqual (37, panel.Height, "Height");
1045                         Assert.AreEqual (37, panel.Left, "Left");
1046                         Assert.AreEqual (37, panel.Top, "Top");
1047                         Assert.AreEqual (37, panel.Width, "Width");
1048                 }
1049
1050                 [Test]
1051                 public void TestUndockSetBoundsCoreVisible ()
1052                 {
1053                         form.Show ();
1054                         panel.InvokeSetBoundsCore ();
1055                         panel.Dock = DockStyle.Top;
1056                         panel.Dock = DockStyle.None;
1057                         Assert.AreEqual (37, panel.Height, "Height");
1058                         Assert.AreEqual (37, panel.Left, "Left");
1059                         Assert.AreEqual (37, panel.Top, "Top");
1060                         Assert.AreEqual (37, panel.Width, "Width");
1061                 }
1062
1063                 [Test]
1064                 public void TestUndockUpdateBounds ()
1065                 {
1066                         panel.InvokeUpdateBounds ();
1067                         panel.Dock = DockStyle.Top;
1068                         panel.Dock = DockStyle.None;
1069                         Assert.AreEqual (37, panel.Height, "Height");
1070                         Assert.AreEqual (37, panel.Left, "Left");
1071                         Assert.AreEqual (37, panel.Top, "Top");
1072                         Assert.AreEqual (37, panel.Width, "Width");
1073                 }
1074
1075                 [Test]
1076                 public void TestUndockUpdateBoundsVisible ()
1077                 {
1078                         form.Show ();
1079                         panel.InvokeUpdateBounds ();
1080                         panel.Dock = DockStyle.Top;
1081                         panel.Dock = DockStyle.None;
1082                         Assert.AreEqual (37, panel.Height, "Height");
1083                         Assert.AreEqual (37, panel.Left, "Left");
1084                         Assert.AreEqual (37, panel.Top, "Top");
1085                         Assert.AreEqual (37, panel.Width, "Width");
1086                 }
1087
1088 #if NET_2_0
1089                 [Test]
1090                 public void AutoSizeGrowOnlyControls_ShrinkWhenDocked ()
1091                 {
1092                         // For most controls that are AutoSized and support the
1093                         // AutoSizeMode property, if they are set to GrowOnly,
1094                         // they should not shrink.  However, they will shrink 
1095                         // even in GrowOnly mode if they are docked.
1096                         // Button is one exception to this rule.
1097                         Form f = new Form ();
1098                         f.ClientSize = new Size (300, 300);
1099                         f.ShowInTaskbar = false;
1100                         f.Show ();
1101
1102                         List<Type> types = new List<Type> ();
1103                         types.Add (typeof (TableLayoutPanel));
1104                         types.Add (typeof (FlowLayoutPanel));
1105                         types.Add (typeof (ToolStripContentPanel));
1106                         types.Add (typeof (Panel));
1107                         types.Add (typeof (GroupBox));
1108                         types.Add (typeof (UserControl));
1109                         foreach (Type t in types) {
1110                                 Control c = t.GetConstructor (Type.EmptyTypes).Invoke (null) as Control;
1111                                 c.AutoSize = true;
1112                                 t.GetProperty ("AutoSizeMode").SetValue (c, AutoSizeMode.GrowOnly, null);
1113                                 c.Bounds = new Rectangle (5, 5, 100, 100);
1114                                 f.Controls.Add (c);
1115                                 Assert.AreEqual (100, c.Height, "1 " + t.Name);
1116                                 c.Dock = DockStyle.Top;
1117                                 Assert.IsFalse (100 == c.Height, "2 " + t.Name);
1118                                 f.Controls.Remove (c);
1119                                 c.Dispose ();
1120                         }
1121
1122                         f.Dispose ();
1123                 }
1124
1125                 [Test]
1126                 public void AutoSizeGrowOnlyButtons_DoNotShrinkWhenDocked ()
1127                 {
1128                         // For most controls that are AutoSized and support the
1129                         // AutoSizeMode property, if they are set to GrowOnly,
1130                         // they should not shrink.  However, they will shrink 
1131                         // even in GrowOnly mode if they are docked.
1132                         // Button is one exception to this rule.
1133                         Form f = new Form ();
1134                         f.ClientSize = new Size (300, 300);
1135                         f.ShowInTaskbar = false;
1136                         f.Show ();
1137
1138                         List<Type> types = new List<Type> ();
1139                         types.Add (typeof (Button));
1140                         foreach (Type t in types) {
1141                                 Control c = t.GetConstructor (Type.EmptyTypes).Invoke (null) as Control;
1142                                 c.AutoSize = true;
1143                                 t.GetProperty ("AutoSizeMode").SetValue (c, AutoSizeMode.GrowOnly, null);
1144                                 c.Bounds = new Rectangle (5, 5, 100, 100);
1145                                 f.Controls.Add (c);
1146                                 Assert.AreEqual (100, c.Height, "1 " + t.Name);
1147                                 c.Dock = DockStyle.Top;
1148                                 Assert.AreEqual (100, c.Height, "2 " + t.Name);
1149                                 f.Controls.Remove (c);
1150                                 c.Dispose ();
1151                         }
1152
1153                         f.Dispose ();
1154                 }
1155
1156                 [Test]
1157                 public void AnchoredAutoSizedControls_SizeInCorrectDirection ()
1158                 {
1159                         Form f = new Form ();
1160                         f.ClientSize = new Size (300, 300);
1161                         f.ShowInTaskbar = false;
1162
1163                         Panel p1 = new Panel ();
1164                         p1.Bounds = new Rectangle (150, 150, 0, 0);
1165                         p1.Anchor = AnchorStyles.Top | AnchorStyles.Left;
1166                         p1.AutoSize = true;
1167                         f.Controls.Add (p1);
1168
1169                         Panel p2 = new Panel ();
1170                         p2.Bounds = new Rectangle (150, 150, 0, 0);
1171                         p2.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
1172                         p2.AutoSize = true;
1173                         f.Controls.Add (p2);
1174
1175                         Panel p3 = new Panel ();
1176                         p3.Bounds = new Rectangle (150, 150, 0, 0);
1177                         p3.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right;
1178                         p3.AutoSize = true;
1179                         f.Controls.Add (p3);
1180
1181                         Panel p4 = new Panel ();
1182                         p4.Bounds = new Rectangle (150, 150, 0, 0);
1183                         p4.Anchor = AnchorStyles.None;
1184                         p4.AutoSize = true;
1185                         f.Controls.Add (p4);
1186
1187                         f.Show ();
1188                         // cause the panels to grow
1189                         p1.Controls.Add (new TextBox ());
1190                         p2.Controls.Add (new TextBox ());
1191                         p3.Controls.Add (new TextBox ());
1192                         p4.Controls.Add (new TextBox ());
1193                         f.PerformLayout ();
1194
1195                         Assert.AreEqual (150, p1.Top, "1");
1196                         Assert.AreEqual (150, p1.Left, "2");
1197                         Assert.AreEqual (150, p2.Bottom, "3");
1198                         Assert.AreEqual (150, p2.Right, "4");
1199                         Assert.AreEqual (150, p3.Top, "5");
1200                         Assert.AreEqual (150, p3.Left, "6");
1201                         Assert.AreEqual (150, p4.Top, "7");
1202                         Assert.AreEqual (150, p4.Left, "8");
1203
1204                         f.Dispose ();
1205                 }
1206 #endif
1207         }
1208 }