2007-09-11 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / ControlTest.cs
1 //
2 // Copyright (c) 2005 Novell, Inc.
3 //
4 // Authors:
5 //      Ritvik Mayank (mritvik@novell.com)
6 //              Stefan Noack (noackstefan@googlemail.com)
7 //
8
9 using System;
10 using System.Collections;
11 using InvalidEnumArgumentException = System.ComponentModel.InvalidEnumArgumentException;
12 using System.Drawing;
13 using System.Reflection;
14 using System.Runtime.Remoting;
15 using System.Threading;
16 using System.Windows.Forms;
17 #if NET_2_0
18 using System.Windows.Forms.Layout;
19 #endif
20
21 using NUnit.Framework;
22
23 namespace MonoTests.System.Windows.Forms
24 {
25         [TestFixture]
26         public class ControlTest
27         {
28                 
29 #if NET_2_0
30                 [Test]
31                 public void AutoSizeTest ()
32                 {
33                         ControlAutoSizeTester c = new ControlAutoSizeTester (new Size (23, 17), AutoSizeMode.GrowAndShrink);
34                         
35                         Form f = new Form();
36                         f.Size = new Size (200, 200);
37                         c.Parent = f;
38                         f.Show();
39                         
40                         Size s = new Size (42, 42);
41                         c.Size = s;
42                         
43                         Point l = new Point (10, 10);
44                         c.Location = l;
45                         
46                         //Check wether normal size setting is OK
47                         Assert.AreEqual (s, c.Size, "#S1");
48                         
49                         //Check wether size remains without GetPreferredSize implemented even when AutoSize turned on.
50                         c.AutoSize = true;
51                         f.PerformLayout();
52                         Assert.AreEqual (s, c.Size, "#S2");
53                         
54                         //Simulate a Control implementing GetPreferredSize
55                         c.UseCustomPrefSize = true;
56                         f.PerformLayout();
57                         
58                         //Check wether size shrinks to preferred size
59                         Assert.AreEqual (c.CustomPrefSize, c.Size, "#S3");
60                         //Check wether Location stays constant
61                         Assert.AreEqual (l, c.Location, "#L1");
62                         
63                         //Check wether Dock is respected
64                         c.Dock = DockStyle.Bottom;
65                         Assert.AreEqual (f.ClientSize.Width, c.Width, "#D1");
66                         
67                         //Check wether size shrinks to preferred size again
68                         c.Dock = DockStyle.None;
69                         Assert.AreEqual (c.CustomPrefSize, c.Size, "#S4");
70                         
71                         //Check wether Anchor is respected for adjusting Locatioon
72                         c.Anchor = AnchorStyles.Bottom;
73                         f.Height += 50;
74                         Assert.AreEqual (l.Y + 50, c.Top, "#A1");
75                         //Check wether size is still OK
76                         Assert.AreEqual (c.CustomPrefSize, c.Size, "#S5");
77                         
78                         
79                         //just tidy up
80                         c.Anchor = AnchorStyles.Top | AnchorStyles.Left;
81                         c.Location = l;
82                         
83                         //Check wether shrinking to zero is possible 
84                         c.CustomPrefSize = new Size (0, 0);
85                         f.PerformLayout();
86                         Assert.AreEqual (c.CustomPrefSize, c.Size, "#S6");
87                         
88                         //Check wether MinimumSize is honored
89                         c.MinimumSize = new Size (10, 12);
90                         c.CustomPrefSize = new Size (5, 5);
91                         f.PerformLayout();
92                         Assert.AreEqual (c.MinimumSize, c.Size, "#S7");
93                         c.MinimumSize = new Size (0, 0);
94                         
95                         //Check wether MaximumSize is honored
96                         c.MaximumSize = new Size (100, 120); 
97                         c.CustomPrefSize = new Size (500, 500);
98                         f.PerformLayout();
99                         Assert.AreEqual (c.MaximumSize, c.Size, "#S8");
100                         
101                         //Check wether shrinking does not happen when GrowOnly
102                         c.AutoSize = false;
103                         s = new Size (23, 23);
104                         c.Size = s;
105                         c.CustomPrefSize = new Size (5, 5);
106                         c.AutoSizeMode = AutoSizeMode.GrowOnly;
107                         c.AutoSize = true;
108                         f.PerformLayout();
109                         Assert.AreEqual (s, c.Size, "#S9");
110                         f.Close ();
111                 }
112                 
113                 public class ControlAutoSizeTester : Control {
114                         
115
116                         public ControlAutoSizeTester (Size customPrefSize, AutoSizeMode autoSizeMode)
117                         {
118                                 custom_prefsize = customPrefSize;
119                                 AutoSizeMode = autoSizeMode;
120                         }
121                         
122                         public AutoSizeMode AutoSizeMode {
123                                 set {
124                                         base.SetAutoSizeMode (value);
125                                 }
126                         }
127
128                         private bool use_custom_prefsize = false;
129                         public bool UseCustomPrefSize {
130                                 set {
131                                         use_custom_prefsize = value;
132                                 }
133                         }
134                         
135                         private Size custom_prefsize;
136                         
137                         public Size CustomPrefSize {
138                                 get {
139                                         return custom_prefsize;
140                                 }
141                                 set {
142                                         custom_prefsize = value;
143                                 }
144                         }
145                         
146                         
147                         public override Size GetPreferredSize (Size proposedSize)
148                         {
149                                 if (use_custom_prefsize) {
150                                         return custom_prefsize;
151                                 } else {        
152                                         return base.GetPreferredSize(proposedSize);
153                                 }
154                         }
155                         
156                 }
157 #endif
158                 
159                 [Test]
160                 public void Bug82748 ()
161                 {
162                         Form f = new Form ();
163                         f.ShowInTaskbar = false;
164                         
165                         Control c = new Control ();
166                         c.Size = new Size (100, 100);
167                         
168                         Control c2 = new Control ();
169                         c2.Size = c.Size;
170                         c2.Controls.Add (c);
171                         
172                         c.Anchor = AnchorStyles.Right;
173                         
174                         f.Controls.Add (c);
175                         
176                         f.Show ();
177                         
178                         Assert.AreEqual (0, c.Left, "A1");
179                         
180                         f.Close ();
181                         f.Dispose ();
182                 }
183                 
184                 [Test]
185                 public void InvokeTestParentHandle ()
186                 {
187                         Control child, parent;
188                         
189                         parent = new Control ();
190                         child = new Control ();
191                         parent.Controls.Add (child);
192                         parent.Visible = true;
193                         parent.CreateControl ();
194                         child.Invoke (new CrossAppDomainDelegate (dummy)) ;
195                 }
196                 
197                 public void dummy ()
198                 {
199                 }
200                 
201                 public class ControlStylesTester : Control {
202                         private WindowStyles style;
203                         private WindowStyles ex_style;
204                         private bool or_styles;
205
206                         public ControlStylesTester (WindowStyles Style, WindowStyles ExStyle, bool OrStyles)
207                         {
208                                 style = Style;
209                                 ex_style = ExStyle;
210                                 or_styles = OrStyles;
211                         }
212
213                         protected override CreateParams CreateParams {
214                                 get {
215                                         CreateParams result = base.CreateParams;
216                                         if (or_styles) {
217                                                 result.Style |= (int) style;
218                                                 result.ExStyle |= (int) ex_style;
219                                         } else {
220                                                 result.Style = (int) style;
221                                                 result.ExStyle = (int) ex_style;
222                                         }
223                                         return result;
224                                 }
225                         }
226                 }
227                 
228                 [Test]
229                 public void ControlSizeTest ()
230                 {
231                         ControlStylesTester c = new ControlStylesTester (WindowStyles.WS_CHILD, 0, true);
232                         Assert.AreEqual ("{X=0,Y=0}", c.Location.ToString (), "#L1");
233                         Assert.AreEqual ("{Width=0, Height=0}", c.Size.ToString (), "#S1");
234                 }
235
236 #if NET_2_0
237                 [Test]
238                 public void CaptureTest ()
239                 {
240                         Form frm = new Form ();
241                         ControlOverrideLogger log = new ControlOverrideLogger ();
242                         frm.Controls.Add (log);
243                         log.Visible = true;
244                         log.Capture = true;
245                         log.Capture = false;
246                         log.BackColor = Color.Blue;
247                         log.Size = new Size (100, 100);
248                         
249                         frm.Show ();
250                         Application.DoEvents ();
251                         
252                         frm.Dispose ();
253                         Assert.IsTrue (log.Log.ToString ().IndexOf ("OnMouseCaptureChanged") > -1, "#01");
254                         
255                         log = new ControlOverrideLogger ();
256                         log.Capture = true;
257                         log.Capture = false;
258                         Assert.IsTrue (log.Log.ToString ().IndexOf ("OnMouseCaptureChanged") > -1, "#02");
259
260                         log = new ControlOverrideLogger ();
261                         log.Capture = true;
262                         Assert.IsTrue (log.IsHandleCreated, "#03");
263                         Assert.IsTrue (log.Log.ToString ().IndexOf ("OnMouseCaptureChanged") == -1, "#04");
264                         
265                         log = new ControlOverrideLogger ();
266                         log.Capture = false;
267                         Assert.IsFalse (log.IsHandleCreated, "#05");
268                         Assert.IsTrue (log.Log.ToString ().IndexOf ("OnMouseCaptureChanged") == -1, "#06");
269                 }
270 #endif
271         
272                 public class OnPaintTester : Form
273                 {
274                         int counter;
275                         int total;
276                         ArrayList list = new ArrayList ();
277                         public bool Recursive;
278                         public bool TestRefresh;
279                         public bool TestInvalidate;
280                         public bool TestUpdate;
281 #if NET_2_0
282                         public new bool DoubleBuffered {
283                                 get {
284                                         return base.DoubleBuffered;
285                                 }
286                                 set {
287                                         base.DoubleBuffered = value;
288                                 }
289                         }
290 #endif
291                         protected override void OnPaint (PaintEventArgs pevent)
292                         {
293                                 Assert.IsFalse (list.Contains (pevent.Graphics), "OnPaintTester.OnPaint: Got the same Graphics twice");
294                                 list.Add (pevent.Graphics);
295                                 
296                                 if (total > 10)
297                                         return;
298                                 Recursive = counter > 0 || Recursive;
299                                 counter++;
300                                 if (counter < 2) {
301                                         if (TestRefresh)
302                                                 Refresh ();
303                                         else if (TestInvalidate)
304                                                 Invalidate ();
305                                         else {
306                                                 Update ();
307                                         }
308                                 }
309                                 base.OnPaint (pevent);
310                                 counter--;
311                                 total++;
312                         }
313                         public new void Show ()
314                         {
315                                 base.Show ();
316                                 Application.DoEvents ();
317                         }
318                 }
319
320                 [Test]
321                 public void ControlCollectionTest ()
322                 {
323                         Form frm = new Form ();
324                         frm.IsMdiContainer = true;
325                         Form child = new Form ();
326                         Control.ControlCollection c = new Control.ControlCollection (frm);
327                         child.MdiParent = frm;
328                         c.Add (child);
329                 }
330
331                 [Test]
332                 [ExpectedException (typeof (ArgumentException))]
333                 public void ControlCollectionExceptionTest ()
334                 {
335                         Form frm = new Form ();
336                         frm.IsMdiContainer = true;
337                         Form child = new Form ();
338                         Control.ControlCollection c = new Control.ControlCollection (frm);
339                         //child.MdiParent = frm;
340                         c.Add (child);
341                 }
342                 
343                 [Test]
344                 [Category ("NotWorking")]
345                 public void OnPaintTest ()
346                 {
347                         using (OnPaintTester t = new OnPaintTester ()) {
348                                 t.TestRefresh = true;
349                                 t.Show ();
350                                 Assert.IsTrue (t.Recursive, "#1");
351                         }
352
353                         using (OnPaintTester t = new OnPaintTester ()) {
354                                 t.TestUpdate = true;
355                                 t.Show ();
356                                 Assert.IsFalse (t.Recursive, "#2");
357                         }
358
359                         using (OnPaintTester t = new OnPaintTester ()) {
360                                 t.TestInvalidate = true;
361                                 t.Show ();
362                                 Assert.IsFalse (t.Recursive, "#3");
363                         }
364                 }
365 #if NET_2_0
366                 [Test]
367                 [Category ("Interactive")]
368                 public void OnPaintDoubleBufferedTest ()
369                 {
370                         using (OnPaintTester t = new OnPaintTester ()) {
371                                 t.DoubleBuffered = true;
372                                 t.TestRefresh = true;
373                                 t.Show ();
374                                 Assert.IsTrue (t.Recursive, "#1");
375                         }
376
377                         using (OnPaintTester t = new OnPaintTester ()) {
378                                 t.DoubleBuffered = true;
379                                 t.TestUpdate = true;
380                                 t.Show ();
381                                 Assert.IsFalse (t.Recursive, "#2");
382                         }
383
384                         using (OnPaintTester t = new OnPaintTester ()) {
385                                 t.DoubleBuffered = true;
386                                 t.TestInvalidate = true;
387                                 t.Show ();
388                                 Assert.IsFalse (t.Recursive, "#3");
389                         }
390                 }
391 #endif
392                 public class PaintEventForm : Form
393                 {
394                         public ArrayList overrides = new ArrayList ();
395                         public ArrayList events = new ArrayList ();
396
397                         public PaintEventForm ()
398                         {
399                                 Paint += new PaintEventHandler (DoubleBufferEventForm_Paint);
400                         }
401                         public bool GetControlStyle (ControlStyles style)
402                         {
403                                 return base.GetStyle (style);
404                         }
405
406                         public void SetControlStyle (ControlStyles style, bool value)
407                         {
408                                 base.SetStyle (style, value);
409                         }
410                         
411                         void DoubleBufferEventForm_Paint (object sender, PaintEventArgs e)
412                         {
413                                 events.Add("Paint");
414                         }
415                         
416                         protected override void OnPaintBackground (PaintEventArgs e)
417                         {
418                                 base.OnPaintBackground (e);
419                                 overrides.Add("OnPaintBackground");
420                         }
421
422                         protected override void OnPaint (PaintEventArgs pevent)
423                         {
424                                 base.OnPaint (pevent);
425                                 overrides.Add("OnPaint");
426                         }
427                 }
428
429                 [Test]
430                 [Ignore ("Can't find a reliable way to generate a paint message on Windows.")]
431                 public void EventStyleTest ()
432                 {
433 #if NET_2_0
434                         using (PaintEventForm f = new PaintEventForm ()) {
435                                 f.Show ();
436                                 f.SetControlStyle (ControlStyles.OptimizedDoubleBuffer, true);
437                                 f.Refresh ();
438                                 Assert.IsTrue (f.overrides.Contains ("OnPaintBackground"), "#A1");
439                                 Assert.IsTrue (f.overrides.Contains ("OnPaint"), "#A2");
440                                 Assert.IsTrue (f.events.Contains ("Paint"), "#A3");
441                         }
442 #endif
443                         using (PaintEventForm f = new PaintEventForm ()) {
444                                 f.Show ();
445                                 f.SetControlStyle (ControlStyles.DoubleBuffer, true);
446                                 f.Refresh ();
447                                 Assert.IsTrue (f.overrides.Contains ("OnPaintBackground"), "#B1");
448                                 Assert.IsTrue (f.overrides.Contains ("OnPaint"), "#B2");
449                                 Assert.IsTrue (f.events.Contains ("Paint"), "#B3");
450                         }
451
452                         using (PaintEventForm f = new PaintEventForm ()) {
453                                 f.Show ();
454                                 f.SetControlStyle (ControlStyles.AllPaintingInWmPaint, true);
455                                 f.Refresh ();
456                                 Assert.IsTrue (f.overrides.Contains ("OnPaintBackground"), "#C1");
457                                 Assert.IsTrue (f.overrides.Contains ("OnPaint"), "#C2");
458                                 Assert.IsTrue (f.events.Contains ("Paint"), "#C3");
459                         }
460
461                         using (PaintEventForm f = new PaintEventForm ()) {
462                                 f.Show ();
463                                 f.SetControlStyle (ControlStyles.UserPaint, true);
464                                 f.Refresh ();
465                                 Assert.IsTrue (f.overrides.Contains ("OnPaintBackground"), "#D1");
466                                 Assert.IsTrue (f.overrides.Contains ("OnPaint"), "#D2");
467                                 Assert.IsTrue (f.events.Contains ("Paint"), "#D3");
468                         }
469
470                         using (PaintEventForm f = new PaintEventForm ()) {
471                                 f.Show ();
472                                 f.SetControlStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
473                                 f.Refresh ();
474                                 Assert.IsTrue (f.overrides.Contains ("OnPaintBackground"), "#E1");
475                                 Assert.IsTrue (f.overrides.Contains ("OnPaint"), "#E2");
476                                 Assert.IsTrue (f.events.Contains ("Paint"), "#E3");
477                         }
478
479                         using (PaintEventForm f = new PaintEventForm ()) {
480                                 f.Show ();
481                                 f.SetControlStyle (ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);
482                                 f.Refresh ();
483                                 Assert.IsTrue (f.overrides.Contains ("OnPaintBackground"), "#F1");
484                                 Assert.IsTrue (f.overrides.Contains ("OnPaint"), "#F2");
485                                 Assert.IsTrue (f.events.Contains ("Paint"), "#F3");
486                         }
487
488                         using (PaintEventForm f = new PaintEventForm ()) {
489                                 f.Show ();
490                                 f.SetControlStyle (ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
491                                 f.Refresh ();
492                                 Assert.IsTrue (f.overrides.Contains ("OnPaintBackground"), "#G1");
493                                 Assert.IsTrue (f.overrides.Contains ("OnPaint"), "#G2");
494                                 Assert.IsTrue (f.events.Contains ("Paint"), "#G3");
495                         }
496
497                         using (PaintEventForm f = new PaintEventForm ()) {
498                                 f.Show ();
499                                 f.SetControlStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer, true);
500                                 f.Refresh ();
501                                 Assert.IsTrue (f.overrides.Contains ("OnPaintBackground"), "#H1");
502                                 Assert.IsTrue (f.overrides.Contains ("OnPaint"), "#H2");
503                                 Assert.IsTrue (f.events.Contains ("Paint"), "#H3");
504                         }
505                 }
506 #if NET_2_0
507                 public class DoubleBufferedForm : Form
508                 {
509                         public bool painted;
510                         public bool failed;
511                         public DoubleBufferedForm ()
512                         {
513                                 this.DoubleBuffered = true;
514                         }
515
516                         protected override void OnPaint (PaintEventArgs e)
517                         {
518                                 if (failed || painted)
519                                         return;
520                                 painted = true;
521                                 Height = Height + 1;
522                                 try {
523                                         e.Graphics.DrawString (Size.ToString (), Font, Brushes.AliceBlue, new Point (2, 2));
524                                 } catch (Exception exception) {
525                                         Console.WriteLine (exception.StackTrace);
526                                         failed = true;
527                                 }
528                         }
529                 }
530
531                 public class DoubleBufferControl : Control
532                 {
533                         public bool IsDoubleBuffered
534                         {
535                                 get { return base.DoubleBuffered; }
536                                 set { base.DoubleBuffered = value; }
537                         }
538
539                         public bool GetControlStyle (ControlStyles style)
540                         {
541                                 return base.GetStyle (style);
542                         }
543
544                         public void SetControlStyle (ControlStyles style, bool value)
545                         {
546                                 base.SetStyle (style, value);
547                         }
548                 }
549
550                 [Test]
551                 [Ignore ("Can't find a reliable way to generate a paint message on Windows.")]
552                 public void DoubleBufferTest ()
553                 {
554                         DoubleBufferedForm f = new DoubleBufferedForm ();
555                         f.ShowInTaskbar = false;
556                         f.Show ();
557                         f.Refresh ();
558                         
559                         Assert.IsFalse (f.failed, "#01");
560                         Assert.IsTrue (f.painted, "The control was never painted, so please check the test");
561                         f.Close ();
562                 }
563
564                 [Test]
565                 public void DoubleBufferedTest ()
566                 {
567                         DoubleBufferControl c = new DoubleBufferControl ();
568                         Assert.IsFalse (c.IsDoubleBuffered, "#A1");
569                         Assert.IsTrue (c.GetControlStyle (ControlStyles.AllPaintingInWmPaint), "#A2");
570                         Assert.IsFalse (c.GetControlStyle (ControlStyles.DoubleBuffer), "#A3");
571                         Assert.IsFalse (c.GetControlStyle (ControlStyles.OptimizedDoubleBuffer), "#A4");
572                         Assert.IsTrue (c.GetControlStyle (ControlStyles.UserPaint), "#A5");
573
574                         c.SetControlStyle (ControlStyles.OptimizedDoubleBuffer, true);
575                         Assert.IsTrue (c.IsDoubleBuffered, "#B1");
576                         Assert.IsTrue (c.GetControlStyle (ControlStyles.AllPaintingInWmPaint), "#B2");
577                         Assert.IsFalse (c.GetControlStyle (ControlStyles.DoubleBuffer), "#B3");
578                         Assert.IsTrue (c.GetControlStyle (ControlStyles.OptimizedDoubleBuffer), "#B4");
579                         Assert.IsTrue (c.GetControlStyle (ControlStyles.UserPaint), "#A5");
580
581                         c.SetControlStyle (ControlStyles.AllPaintingInWmPaint, false);
582                         c.SetControlStyle (ControlStyles.UserPaint, false);
583                         Assert.IsTrue (c.IsDoubleBuffered, "#C1");
584                         Assert.IsFalse (c.GetControlStyle (ControlStyles.AllPaintingInWmPaint), "#C2");
585                         Assert.IsFalse (c.GetControlStyle (ControlStyles.DoubleBuffer), "#C3");
586                         Assert.IsTrue (c.GetControlStyle (ControlStyles.OptimizedDoubleBuffer), "#C4");
587                         Assert.IsFalse (c.GetControlStyle (ControlStyles.UserPaint), "#C5");
588
589                         c.SetControlStyle (ControlStyles.OptimizedDoubleBuffer, false);
590                         Assert.IsFalse (c.IsDoubleBuffered, "#D1");
591                         Assert.IsFalse (c.GetControlStyle (ControlStyles.AllPaintingInWmPaint), "#D2");
592                         Assert.IsFalse (c.GetControlStyle (ControlStyles.DoubleBuffer), "#D3");
593                         Assert.IsFalse (c.GetControlStyle (ControlStyles.OptimizedDoubleBuffer), "#D4");
594                         Assert.IsFalse (c.GetControlStyle (ControlStyles.UserPaint), "#D5");
595
596                         c.SetControlStyle (ControlStyles.DoubleBuffer, true);
597                         Assert.IsFalse (c.IsDoubleBuffered, "#E1");
598                         Assert.IsFalse (c.GetControlStyle (ControlStyles.AllPaintingInWmPaint), "#E2");
599                         Assert.IsTrue (c.GetControlStyle (ControlStyles.DoubleBuffer), "#E3");
600                         Assert.IsFalse (c.GetControlStyle (ControlStyles.OptimizedDoubleBuffer), "#E4");
601                         Assert.IsFalse (c.GetControlStyle (ControlStyles.UserPaint), "#E5");
602
603                         c.SetControlStyle (ControlStyles.DoubleBuffer, false);
604                         Assert.IsFalse (c.IsDoubleBuffered, "#F1");
605                         Assert.IsFalse (c.GetControlStyle (ControlStyles.AllPaintingInWmPaint), "#F2");
606                         Assert.IsFalse (c.GetControlStyle (ControlStyles.DoubleBuffer), "#F3");
607                         Assert.IsFalse (c.GetControlStyle (ControlStyles.OptimizedDoubleBuffer), "#F4");
608                         Assert.IsFalse (c.GetControlStyle (ControlStyles.UserPaint), "#F5");
609
610                         c.IsDoubleBuffered = true;
611                         Assert.IsTrue (c.IsDoubleBuffered, "#G1");
612                         Assert.IsTrue (c.GetControlStyle (ControlStyles.AllPaintingInWmPaint), "#G2");
613                         Assert.IsFalse (c.GetControlStyle (ControlStyles.DoubleBuffer), "#G3");
614                         Assert.IsTrue (c.GetControlStyle (ControlStyles.OptimizedDoubleBuffer), "#G4");
615                         Assert.IsFalse (c.GetControlStyle (ControlStyles.UserPaint), "#G5");
616
617                         c.SetControlStyle (ControlStyles.AllPaintingInWmPaint, true);
618                         c.SetControlStyle (ControlStyles.OptimizedDoubleBuffer, true);
619                         c.SetControlStyle (ControlStyles.DoubleBuffer, true);
620                         c.SetControlStyle (ControlStyles.UserPaint, true);
621                         c.IsDoubleBuffered = false;
622                         Assert.IsFalse (c.IsDoubleBuffered, "#H1");
623                         Assert.IsTrue (c.GetControlStyle (ControlStyles.AllPaintingInWmPaint), "#H2");
624                         Assert.IsTrue (c.GetControlStyle (ControlStyles.DoubleBuffer), "#H3");
625                         Assert.IsFalse (c.GetControlStyle (ControlStyles.OptimizedDoubleBuffer), "#H4");
626                         Assert.IsTrue (c.GetControlStyle (ControlStyles.UserPaint), "#H5");
627                 }
628
629                 [Test]
630                 public void DoubleBufferedStyleTest ()
631                 {
632                         DoubleBufferControl c = new DoubleBufferControl ();
633                         TestControlStyle.CheckStyles (c, "#A1", ControlStyles.UserPaint, ControlStyles.StandardClick, ControlStyles.Selectable, ControlStyles.StandardDoubleClick, ControlStyles.AllPaintingInWmPaint, ControlStyles.UseTextForAccessibility);
634
635                         c.IsDoubleBuffered = true;
636                         TestControlStyle.CheckStyles (c, "#A2", ControlStyles.UserPaint, ControlStyles.StandardClick, ControlStyles.Selectable, ControlStyles.StandardDoubleClick, ControlStyles.AllPaintingInWmPaint, ControlStyles.UseTextForAccessibility, ControlStyles.OptimizedDoubleBuffer);
637
638                         c.IsDoubleBuffered = false;
639                         TestControlStyle.CheckStyles (c, "#A3", ControlStyles.UserPaint, ControlStyles.StandardClick, ControlStyles.Selectable, ControlStyles.StandardDoubleClick, ControlStyles.AllPaintingInWmPaint, ControlStyles.UseTextForAccessibility);
640
641                         c = new DoubleBufferControl ();
642                         foreach (ControlStyles style in Enum.GetValues (typeof(ControlStyles))) {
643                                 c.SetControlStyle (style, false);
644                         }
645
646                         TestControlStyle.CheckStyles (c, "#B1");
647
648                         c.IsDoubleBuffered = true;
649                         TestControlStyle.CheckStyles (c, "#B2", ControlStyles.OptimizedDoubleBuffer, ControlStyles.AllPaintingInWmPaint);
650
651                         c.IsDoubleBuffered = false;
652                         TestControlStyle.CheckStyles (c, "#B3", ControlStyles.AllPaintingInWmPaint);
653
654                 }
655 #endif
656
657                 class Helper {
658                         public static void TestAccessibility(Control c, string Default, string Description, string Name, AccessibleRole Role)
659                         {
660                                 Assert.IsNotNull (c.AccessibilityObject, "Acc1");
661                                 Assert.AreEqual (Default, c.AccessibleDefaultActionDescription, "Acc2");
662                                 Assert.AreEqual (Description, c.AccessibleDescription, "Acc3");
663                                 Assert.AreEqual (Name, c.AccessibleName, "Acc4");
664                                 Assert.AreEqual (Role, c.AccessibleRole, "Acc5");
665                         }
666
667                         public static string TestControl(Control container, Control start, bool forward) {
668                                 Control ctl;
669
670                                 ctl = container.GetNextControl(start, forward);
671
672                                 if (ctl == null) {
673                                         return null;
674                                 }
675
676                                 return ctl.Text;
677                         }
678                 }
679
680                 [Test]
681                 public void CreatedTest ()
682                 {
683                         Control c = new Control ();
684                         Assert.IsFalse (c.Created, "A1");
685                 }
686
687                 [Test]
688                 [Category ("NotWorking")]
689                 public void CreatedAccessibilityTest ()
690                 {
691                         Control c = new Control ();
692                         Assert.IsFalse (c.Created, "A1");
693
694                         Helper.TestAccessibility(c, null, null, null, AccessibleRole.Default);
695
696                         Assert.IsTrue (c.Created, "A2");
697
698                         c.Dispose ();
699
700                         Assert.IsFalse (c.Created, "A3");
701                 }
702
703                 [Test]
704                 [Category ("NotWorking")]
705                 public void BoundsTest ()
706                 {
707                         Control c = new Control ();
708                         Assert.IsTrue (c.Bounds.IsEmpty, "A1");
709                         Assert.IsTrue (c.Size.IsEmpty, "A2");
710                         Assert.IsTrue (c.ClientSize.IsEmpty, "A3");
711                         Assert.IsTrue (c.ClientRectangle.IsEmpty, "A4");
712
713                         Assert.AreEqual (((IWin32Window)c).Handle, c.Handle, "A5");
714
715                         /* this part fails on linux because we can't allocate X windows which are 0x0,
716                            and the Control bounds directly reflect the size of the X window */
717
718                         Assert.IsTrue (c.Bounds.IsEmpty, "A6");
719                         Assert.IsTrue (c.Size.IsEmpty, "A7");
720                         Assert.IsTrue (c.ClientSize.IsEmpty, "A8");
721                         Assert.IsTrue (c.ClientRectangle.IsEmpty, "A9");
722                 }
723
724                 [Test]
725                 [Ignore ("Depends on specific DPI")]
726                 public void FontHeightTest ()
727                 {
728                         MockControl c = new MockControl ();
729                         Assert.AreEqual (13, c.font_height);
730                 }
731
732                 [Test]
733                 public void FontTest ()
734                 {
735                         Control c = new Control ();
736                         Assert.IsFalse (c.Font.Bold, "#A1");
737                         //Assert.AreEqual ("Microsoft Sans Serif", c.Font.FontFamily.Name, "#A2");
738                         Assert.IsFalse (c.Font.Italic, "#A3");
739                         //Assert.AreEqual ("Microsoft Sans Serif", c.Font.Name, "#A4");
740                         Assert.AreEqual (8.25, c.Font.Size, "#A5");
741                         Assert.AreEqual (8.25, c.Font.SizeInPoints, "#A6");
742                         Assert.IsFalse (c.Font.Strikeout, "#A7");
743                         Assert.IsFalse (c.Font.Underline, "#A8");
744                         Assert.AreEqual (GraphicsUnit.Point, c.Font.Unit, "#A9");
745 #if NET_2_0
746                         Assert.IsTrue (c.Font.IsSystemFont, "#A10");
747 #endif
748
749                         c.Font = new Font (c.Font.FontFamily, 3, FontStyle.Italic);
750                         Assert.IsFalse (c.Font.Bold, "#B1");
751                         //Assert.AreEqual ("Microsoft Sans Serif", c.Font.FontFamily.Name, "#B2");
752                         Assert.IsTrue (c.Font.Italic, "#B3");
753                         //Assert.AreEqual ("Microsoft Sans Serif", c.Font.Name, "#B4");
754                         Assert.AreEqual (3, c.Font.Size, "#B5");
755                         Assert.AreEqual (3, c.Font.SizeInPoints, "#B6");
756                         Assert.IsFalse (c.Font.Strikeout, "#B7");
757                         Assert.IsFalse (c.Font.Underline, "#B8");
758                         Assert.AreEqual (GraphicsUnit.Point, c.Font.Unit, "#B9");
759 #if NET_2_0
760                         Assert.AreEqual (false, c.Font.IsSystemFont, "#B10");
761 #endif
762                 }
763
764                 [Test]
765                 [Category ("NotWorking")] // on Unix mapping is done to Bitstream Vera Sans
766                 public void FontTest_Names ()
767                 {
768                         Control c = new Control ();
769                         Assert.AreEqual ("Microsoft Sans Serif", c.Font.FontFamily.Name, "#1");
770                         Assert.AreEqual ("Microsoft Sans Serif", c.Font.Name, "#2");
771                 }
772
773                 [Test]
774                 public void PubPropTest()
775                 {
776                         Control c = new Control();
777
778                         Assert.IsFalse (c.AllowDrop , "A1");
779                         Assert.AreEqual(AnchorStyles.Top | AnchorStyles.Left, c.Anchor, "A2");
780
781                         Assert.AreEqual ("Control", c.BackColor.Name , "B1");
782                         Assert.IsNull (c.BackgroundImage, "B2");
783                         Assert.IsNull (c.BindingContext, "B3");
784 #if NET_2_0
785                         Assert.AreEqual (ImageLayout.Tile, c.BackgroundImageLayout, "B4");
786 #endif
787
788                         Assert.IsFalse (c.CanFocus, "C1");
789                         Assert.IsTrue (c.CanSelect, "C2");
790                         Assert.IsFalse (c.Capture, "C3");
791                         Assert.IsTrue (c.CausesValidation, "C4");
792
793                         Assert.IsNotNull (c.CompanyName, "C7");
794                         Assert.IsNull (c.Container, "C8");
795                         Assert.IsFalse (c.ContainsFocus, "C9");
796                         Assert.IsNull (c.ContextMenu, "C10");
797                         Assert.AreEqual (0, c.Controls.Count, "C11");
798                         Assert.IsFalse (c.Created, "C12");
799                         Assert.AreEqual (Cursors.Default, c.Cursor, "C13");
800
801                         Assert.IsNotNull(c.DataBindings, "D1");
802                         Assert.AreEqual("Control", Control.DefaultBackColor.Name, "D2");
803                         Assert.AreEqual("ControlText", Control.DefaultForeColor.Name, "D3");
804                         Assert.AreEqual(FontStyle.Regular, Control.DefaultFont.Style, "D4");
805                         Assert.AreEqual (new Rectangle(0, 0, 0, 0), c.DisplayRectangle , "D5");
806                         Assert.IsFalse (c.Disposing, "D6");
807                         Assert.AreEqual(DockStyle.None, c.Dock, "D7");
808
809                         Assert.IsTrue (c.Enabled, "E1");
810
811                         Assert.IsFalse  (c.Focused, "F1");
812                         Assert.AreEqual (FontStyle.Regular, c.Font.Style, "F2");
813                         Assert.AreEqual (SystemColors.ControlText, c.ForeColor, "F3");
814
815                         Assert.IsFalse  (c.HasChildren, "H2");
816
817                         Assert.AreEqual (ImeMode.NoControl, c.ImeMode, "I1");
818                         Assert.IsFalse (c.InvokeRequired, "I2");
819                         Assert.IsFalse (c.IsAccessible, "I3");
820                         Assert.IsFalse (c.IsDisposed, "I4");
821                         Assert.IsFalse (c.IsHandleCreated, "I5");
822
823                         Assert.AreEqual(Point.Empty, c.Location, "L2");
824
825 #if NET_2_0
826                         Assert.IsTrue(c.MaximumSize.IsEmpty);
827                         Assert.IsTrue(c.MinimumSize.IsEmpty);
828 #endif
829                         Assert.AreEqual (Keys.None, Control.ModifierKeys, "M1");
830                         Assert.IsTrue (Control.MousePosition.X >= 0 && Control.MousePosition.Y >= 0, "M2");
831                         Assert.AreEqual (MouseButtons.None, Control.MouseButtons, "M3");
832
833                         Assert.AreEqual("", c.Name, "N1");
834                         c.Name = "Control Name";
835                         Assert.AreEqual("Control Name", c.Name, "N2");
836
837                         Assert.IsNull (c.Parent, "P1");
838                         Assert.IsNotNull (c.ProductName, "P2");
839                         Assert.IsTrue (c.ProductName != "", "P3");
840                         Assert.IsNotNull (c.ProductVersion, "P4");
841                         Assert.IsTrue (c.ProductVersion != "", "P5");
842
843                         Assert.IsFalse (c.RecreatingHandle, "R1");
844                         Assert.IsNull (c.Region, "R2");
845                         Assert.AreEqual (RightToLeft.No, c.RightToLeft, "R4");
846
847                         Assert.IsNull (c.Site, "S1");
848
849                         Assert.AreEqual (0, c.TabIndex , "T1");
850                         Assert.IsTrue (c.TabStop, "T2");
851                         Assert.IsNull (c.Tag, "T3");
852                         Assert.AreEqual ("", c.Text, "T4");
853
854                         Assert.IsTrue (c.Visible, "V1");
855                 }
856
857                 [Test]
858                 public void SizeChangeTest ()
859                 {
860                         Form f = new Form ();
861                         Control c = new Control ();
862                         f.Controls.Add(c);
863                         f.Show();
864                         c.Resize += new EventHandler(SizeChangedTest_ResizeHandler);
865                         c.Tag = true;
866                         c.Size = c.Size;
867                         Assert.AreEqual (true, (bool) c.Tag, "#1");
868                         f.Close ();
869                 }
870
871                 private void SizeChangedTest_ResizeHandler (object sender, EventArgs e)
872                 {
873                         ((Control) sender).Tag = false;
874                 }
875
876                 [Test]
877                 public void NegativeHeightTest ()
878                 {
879                         Control c = new Control ();
880                         IntPtr handle = c.Handle;
881                         c.Resize += new EventHandler(NegativeHeightTest_ResizeHandler);
882                         c.Tag = -2;
883                         c.Height = 2;
884                         c.Height = -2;
885                         Assert.AreEqual (0, (int) c.Tag, "#1");
886                         c.Dispose ();
887                         Assert.AreEqual (handle, handle, "Removes warning.");
888                 }
889                 
890                 private void NegativeHeightTest_ResizeHandler (object sender, EventArgs e)
891                 {
892                         Control c = (Control) sender;
893                         c.Tag = c.Height;
894                 }
895                 
896                 [Test]
897                 public void TopLevelControlTest () {
898                         Control c = new Control ();
899
900                         Assert.AreEqual(null, c.TopLevelControl, "T1");
901
902                         Panel p = new Panel ();
903
904                         p.Controls.Add (c);
905
906                         Assert.AreEqual(null, c.TopLevelControl, "T2");
907
908                         Form f = new Form ();
909                         f.ShowInTaskbar = false;
910
911                         f.Controls.Add (p);
912
913                         Assert.AreEqual (f, c.TopLevelControl, "T3");
914                         Assert.AreEqual (f, f.TopLevelControl, "T4");
915                 }
916
917                 [Test]
918                 public void RelationTest() {
919                         Control c1;
920                         Control c2;
921
922                         c1 = new Control();
923                         c2 = new Control();
924
925                         Assert.AreEqual(true , c1.Visible , "Rel1");
926                         Assert.AreEqual(false, c1.Contains(c2) , "Rel2");
927                         Assert.AreEqual("System.Windows.Forms.Control", c1.ToString() , "Rel3");
928
929                         c1.Controls.Add(c2);
930                         Assert.AreEqual(true , c2.Visible , "Rel4");
931                         Assert.AreEqual(true, c1.Contains(c2) , "Rel5");
932
933                         c1.Anchor = AnchorStyles.Top;
934                         c1.SuspendLayout ();
935                         c1.Anchor = AnchorStyles.Left ;
936                         c1.ResumeLayout ();
937                         Assert.AreEqual(AnchorStyles.Left , c1.Anchor, "Rel6");
938
939                         c1.SetBounds(10, 20, 30, 40) ;
940                         Assert.AreEqual(new Rectangle(10, 20, 30, 40), c1.Bounds, "Rel7");
941
942                         Assert.AreEqual(c1, c2.Parent, "Rel8");
943                 }
944
945                 [Test]
946                 public void AnchorDockTest ()
947                 {
948                         Control c = new Control ();
949
950                         Assert.AreEqual (DockStyle.None, c.Dock, "1");
951                         Assert.AreEqual (AnchorStyles.Top | AnchorStyles.Left, c.Anchor, "2");
952
953                         c.Dock = DockStyle.Top;
954                         Assert.AreEqual (DockStyle.Top, c.Dock, "3");
955                         Assert.AreEqual (AnchorStyles.Top | AnchorStyles.Left, c.Anchor, "4");
956
957                         c.Anchor = AnchorStyles.Top;
958                         Assert.AreEqual (DockStyle.None, c.Dock, "5");
959                         Assert.AreEqual (AnchorStyles.Top, c.Anchor, "6");
960                 }
961
962                 [Test]
963                 [Category ("NotWorking")]
964                 public void TabOrder()
965                 {
966                         Form            form;
967                         //Control               active;
968
969                         Label           label1 = new Label();           // To test non-tabstop items as well
970                         Label           label2 = new Label();
971
972                         GroupBox        group1 = new GroupBox();
973                         GroupBox        group2 = new GroupBox();
974                         GroupBox        group3 = new GroupBox();
975
976                         TextBox         text1 = new TextBox();
977
978                         RadioButton     radio11 = new RadioButton();
979                         RadioButton     radio12 = new RadioButton();
980                         RadioButton     radio13 = new RadioButton();
981                         RadioButton     radio14 = new RadioButton();
982                         RadioButton     radio21 = new RadioButton();
983                         RadioButton     radio22 = new RadioButton();
984                         RadioButton     radio23 = new RadioButton();
985                         RadioButton     radio24 = new RadioButton();
986                         RadioButton     radio31 = new RadioButton();
987                         RadioButton     radio32 = new RadioButton();
988                         RadioButton     radio33 = new RadioButton();
989                         RadioButton     radio34 = new RadioButton();
990
991                         form = new Form();
992                         form.ShowInTaskbar = false;
993
994                         form.ClientSize = new Size (520, 520);
995                         Assert.AreEqual(new Size(520, 520), form.ClientSize, "Tab1");
996
997                         form.Text = "SWF Taborder Test App Form";
998                         Assert.AreEqual("SWF Taborder Test App Form", form.Text, "Tab2");
999
1000                         label1.Location = new Point(10, 10);
1001                         Assert.AreEqual(new Point(10, 10), label1.Location, "Tab3");
1002                         label1.Text = "Label1";
1003                         form.Controls.Add(label1);
1004
1005                         label2.Location = new Point(200, 10);
1006                         label2.Text = "Label2";
1007                         form.Controls.Add(label2);
1008
1009                         group1.Text = "Group1";
1010                         group2.Text = "Group2";
1011                         group3.Text = "Group3";
1012
1013                         group1.Size = new Size(200, 400);
1014                         group2.Size = new Size(200, 400);
1015                         group3.Size = new Size(180, 180);
1016                         Assert.AreEqual(new Size(180, 180), group3.Size, "Tab4");
1017
1018                         group1.Location = new Point(10, 40);
1019                         group2.Location = new Point(220, 40);
1020                         group3.Location = new Point(10, 210);
1021
1022                         group1.TabIndex = 30;
1023                         Assert.AreEqual(30, group1.TabIndex, "Tab5");
1024                         group1.TabStop = true;
1025
1026                         // Don't assign, test automatic assignment
1027                         //group2.TabIndex = 0;
1028                         group2.TabStop = true;
1029                         Assert.AreEqual(0, group2.TabIndex, "Tab6");
1030
1031                         group3.TabIndex = 35;
1032                         group3.TabStop = true;
1033
1034                         // Test default tab index
1035                         Assert.AreEqual(0, radio11.TabIndex, "Tab7");
1036
1037                         text1.Text = "Edit Control";
1038
1039                         radio11.Text = "Radio 1-1 [Tab1]";
1040                         radio12.Text = "Radio 1-2 [Tab2]";
1041                         radio13.Text = "Radio 1-3 [Tab3]";
1042                         radio14.Text = "Radio 1-4 [Tab4]";
1043
1044                         radio21.Text = "Radio 2-1 [Tab4]";
1045                         radio22.Text = "Radio 2-2 [Tab3]";
1046                         radio23.Text = "Radio 2-3 [Tab2]";
1047                         radio24.Text = "Radio 2-4 [Tab1]";
1048
1049                         radio31.Text = "Radio 3-1 [Tab1]";
1050                         radio32.Text = "Radio 3-2 [Tab3]";
1051                         radio33.Text = "Radio 3-3 [Tab2]";
1052                         radio34.Text = "Radio 3-4 [Tab4]";
1053
1054                         // We don't assign TabIndex for radio1X; test automatic assignment
1055                         text1.TabStop = true;
1056                         radio11.TabStop = true;
1057
1058                         radio21.TabIndex = 4;
1059                         radio22.TabIndex = 3;
1060                         radio23.TabIndex = 2;
1061                         radio24.TabIndex = 1;
1062                         radio24.TabStop = true;
1063
1064                         radio31.TabIndex = 11;
1065                         radio31.TabStop = true;
1066                         radio32.TabIndex = 13;
1067                         radio33.TabIndex = 12;
1068                         radio34.TabIndex = 14;
1069
1070                         text1.Location = new Point(10, 100);
1071
1072                         radio11.Location = new Point(10, 20);
1073                         radio12.Location = new Point(10, 40);
1074                         radio13.Location = new Point(10, 60);
1075                         radio14.Location = new Point(10, 80);
1076
1077                         radio21.Location = new Point(10, 20);
1078                         radio22.Location = new Point(10, 40);
1079                         radio23.Location = new Point(10, 60);
1080                         radio24.Location = new Point(10, 80);
1081
1082                         radio31.Location = new Point(10, 20);
1083                         radio32.Location = new Point(10, 40);
1084                         radio33.Location = new Point(10, 60);
1085                         radio34.Location = new Point(10, 80);
1086
1087                         text1.Size = new Size(150, text1.PreferredHeight);
1088
1089                         radio11.Size = new Size(150, 20);
1090                         radio12.Size = new Size(150, 20);
1091                         radio13.Size = new Size(150, 20);
1092                         radio14.Size = new Size(150, 20);
1093
1094                         radio21.Size = new Size(150, 20);
1095                         radio22.Size = new Size(150, 20);
1096                         radio23.Size = new Size(150, 20);
1097                         radio24.Size = new Size(150, 20);
1098
1099                         radio31.Size = new Size(150, 20);
1100                         radio32.Size = new Size(150, 20);
1101                         radio33.Size = new Size(150, 20);
1102                         radio34.Size = new Size(150, 20);
1103
1104                         group1.Controls.Add(text1);
1105
1106                         group1.Controls.Add(radio11);
1107                         group1.Controls.Add(radio12);
1108                         group1.Controls.Add(radio13);
1109                         group1.Controls.Add(radio14);
1110
1111                         group2.Controls.Add(radio21);
1112                         group2.Controls.Add(radio22);
1113                         group2.Controls.Add(radio23);
1114                         group2.Controls.Add(radio24);
1115
1116                         group3.Controls.Add(radio31);
1117                         group3.Controls.Add(radio32);
1118                         group3.Controls.Add(radio33);
1119                         group3.Controls.Add(radio34);
1120
1121                         form.Controls.Add(group1);
1122                         form.Controls.Add(group2);
1123                         group2.Controls.Add(group3);
1124
1125                         // Perform some tests, the TabIndex stuff below will alter the outcome
1126                         Assert.AreEqual(null, Helper.TestControl(group2, radio34, true), "Tab8");
1127                         Assert.AreEqual(31, group2.TabIndex, "Tab9");
1128
1129                         // Does the taborder of containers and non-selectable things change behaviour?
1130                         label1.TabIndex = 5;
1131                         label2.TabIndex = 4;
1132                         group1.TabIndex = 3;
1133                         group2.TabIndex = 1;
1134
1135                         // Start verification
1136                         Assert.AreEqual(null, Helper.TestControl(group2, radio34, true), "Tab10");
1137                         Assert.AreEqual(radio24.Text, Helper.TestControl(group2, group2, true), "Tab11");
1138                         Assert.AreEqual(radio31.Text, Helper.TestControl(group2, group3, true), "Tab12");
1139                         Assert.AreEqual(null, Helper.TestControl(group1, radio14, true), "Tab13");
1140                         Assert.AreEqual(radio23.Text, Helper.TestControl(group2, radio24, true), "Tab14");
1141                         Assert.AreEqual(group3.Text, Helper.TestControl(group2, radio21, true), "Tab15");
1142                         Assert.AreEqual(radio13.Text, Helper.TestControl(form, radio12, true), "Tab16");
1143                         Assert.AreEqual(label2.Text, Helper.TestControl(form, radio14, true), "Tab17");
1144                         Assert.AreEqual(group1.Text, Helper.TestControl(form, radio34, true), "Tab18");
1145                         Assert.AreEqual(radio23.Text, Helper.TestControl(group2, radio24, true), "Tab19");
1146
1147                         // Sanity checks
1148                         Assert.AreEqual(null, Helper.TestControl(radio11, radio21, true), "Tab20");
1149                         Assert.AreEqual(text1.Text, Helper.TestControl(group1, radio21, true), "Tab21");
1150
1151                         Assert.AreEqual(radio14.Text, Helper.TestControl(form, label2, false), "Tab22");
1152                         Assert.AreEqual(radio21.Text, Helper.TestControl(group2, group3, false), "Tab23");
1153
1154                         Assert.AreEqual(4, radio21.TabIndex, "Tab24");
1155                         Assert.AreEqual(1, radio11.TabIndex, "Tab25");
1156                         Assert.AreEqual(3, radio13.TabIndex, "Tab26");
1157                         Assert.AreEqual(35, group3.TabIndex, "Tab27");
1158                         Assert.AreEqual(1, group2.TabIndex, "Tab28");
1159
1160                         Assert.AreEqual(label1.Text, Helper.TestControl(form, form, false), "Tab29");
1161                         Assert.AreEqual(radio14.Text, Helper.TestControl(group1, group1, false), "Tab30");
1162                         Assert.AreEqual(radio34.Text, Helper.TestControl(group3, group3, false), "Tab31");
1163
1164                         Assert.AreEqual(null, Helper.TestControl(label1, label1, false), "Tab31");
1165                         Assert.AreEqual(null, Helper.TestControl(radio11, radio21, false), "Tab32");
1166                         form.Dispose ();
1167                 }
1168
1169                 [Test]
1170                 public void ScaleTest()
1171                 {
1172                         Control r1 = new Control();
1173
1174                         r1.Width = 40;
1175                         r1.Height = 20;
1176                         r1.Scale(2);
1177                         Assert.AreEqual(80, r1.Width, "Scale1");
1178                         Assert.AreEqual(40, r1.Height, "Scale2");
1179                 }
1180
1181 #if NET_2_0
1182                 [Test]
1183                 public void ScaleChildrenTest ()
1184                 {
1185                         ScaleChildrenControl c = new ScaleChildrenControl ();
1186                         Assert.AreEqual (true, c.PublicScaleChildren, "A1");
1187                 }
1188                 
1189                 private class ScaleChildrenControl : Control
1190                 {
1191                         public bool PublicScaleChildren {
1192                                 get { return base.ScaleChildren; }
1193                         }
1194                 }
1195                 
1196                 [Test]
1197                 public void ScaleControlTest ()
1198                 {
1199                         ScaleControl c = new ScaleControl ();
1200                         
1201                         c.Location = new Point (5, 10);
1202                         c.Size = new Size (15, 20);
1203                         
1204                         Assert.AreEqual (new Rectangle (5, 10, 15, 20), c.Bounds, "A1");
1205
1206                         c.PublicScaleControl (new SizeF (1.5f, 1.3f), BoundsSpecified.All);
1207                         Assert.AreEqual (new Rectangle (8, 13, 22, 26), c.Bounds, "A2");
1208
1209                         c.PublicScaleControl (new SizeF (2f, 1.5f), BoundsSpecified.Location);
1210                         Assert.AreEqual (new Rectangle (16, 20, 22, 26), c.Bounds, "A3");
1211
1212                         c.PublicScaleControl (new SizeF (1.5f, 2f), BoundsSpecified.Size);
1213                         Assert.AreEqual (new Rectangle (16, 20, 33, 52), c.Bounds, "A4");
1214
1215                         c.PublicScaleControl (new SizeF (1.5f, 1.5f), BoundsSpecified.Width);
1216                         Assert.AreEqual (new Rectangle (16, 20, 50, 52), c.Bounds, "A5");
1217
1218                         c.PublicScaleControl (new SizeF (1.5f, 1.3f), BoundsSpecified.None);
1219                         Assert.AreEqual (new Rectangle (16, 20, 50, 52), c.Bounds, "A6");
1220                         
1221                         // Test with ScaleChildren
1222                         c = new ScaleControl ();
1223
1224                         c.Location = new Point (5, 10);
1225                         c.Size = new Size (50, 50);
1226                         
1227                         Control c2 = new Control ();
1228                         c2.Location = new Point (15, 15);
1229                         c2.Size = new Size (25, 25);
1230                         c.Controls.Add (c2);
1231
1232                         Assert.AreEqual (new Rectangle (5, 10, 50, 50), c.Bounds, "B1");
1233                         Assert.AreEqual (new Rectangle (15, 15, 25, 25), c2.Bounds, "B2");
1234
1235                         c.scale_children = false;
1236
1237                         c.PublicScaleControl (new SizeF (2f, 2f), BoundsSpecified.All);
1238                         Assert.AreEqual (new Rectangle (10, 20, 100, 100), c.Bounds, "B3");
1239                         Assert.AreEqual (new Rectangle (15, 15, 25, 25), c2.Bounds, "B4");
1240
1241                         c.scale_children = true;
1242
1243                         // Will not scale children in ScaleControl
1244                         c.PublicScaleControl (new SizeF (2f, 2f), BoundsSpecified.All);
1245                         Assert.AreEqual (new Rectangle (20, 40, 200, 200), c.Bounds, "B5");
1246                         Assert.AreEqual (new Rectangle (15, 15, 25, 25), c2.Bounds, "B6");
1247                         
1248                         // Does scale children in Scale
1249                         c.Scale (new SizeF (2f, 2f));
1250                         Assert.AreEqual (new Rectangle (40, 80, 400, 400), c.Bounds, "B7");
1251                         Assert.AreEqual (new Rectangle (30, 30, 50, 50), c2.Bounds, "B8");
1252                 }
1253                 
1254                 [Test]
1255                 public void GetScaledBoundsTest ()
1256                 {
1257                         ScaleControl c = new ScaleControl ();
1258                         
1259                         Rectangle r = new Rectangle (10, 20, 30, 40);
1260
1261                         Assert.AreEqual (new Rectangle (20, 10, 60, 20), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.All), "A1");
1262                         Assert.AreEqual (new Rectangle (20, 10, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Location), "A2");
1263                         Assert.AreEqual (new Rectangle (10, 20, 60, 20), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Size), "A3");
1264                         Assert.AreEqual (new Rectangle (10, 20, 30, 20), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Height), "A4");
1265                         Assert.AreEqual (new Rectangle (20, 20, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.X), "A5");
1266                         Assert.AreEqual (new Rectangle (10, 20, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.None), "A6");
1267                         
1268                         c.PublicSetTopLevel (true);
1269
1270                         Assert.AreEqual (new Rectangle (10, 20, 60, 20), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.All), "A7");
1271                         Assert.AreEqual (new Rectangle (10, 20, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Location), "A8");
1272                         Assert.AreEqual (new Rectangle (10, 20, 60, 20), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Size), "A9");
1273                         Assert.AreEqual (new Rectangle (10, 20, 30, 20), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Height), "A10");
1274                         Assert.AreEqual (new Rectangle (10, 20, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.X), "A11");
1275                         Assert.AreEqual (new Rectangle (10, 20, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.None), "A12");
1276
1277                         c = new ScaleControl ();
1278                         c.PublicSetStyle (ControlStyles.FixedHeight, true);
1279                         c.PublicSetStyle (ControlStyles.FixedWidth, true);
1280
1281                         Assert.AreEqual (new Rectangle (20, 10, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.All), "A13");
1282                         Assert.AreEqual (new Rectangle (20, 10, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Location), "A14");
1283                         Assert.AreEqual (new Rectangle (10, 20, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Size), "A15");
1284                         Assert.AreEqual (new Rectangle (10, 20, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Height), "A16");
1285                         Assert.AreEqual (new Rectangle (20, 20, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.X), "A17");
1286                         Assert.AreEqual (new Rectangle (10, 20, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.None), "A18");
1287                 }
1288                 
1289                 private class ScaleControl : Control
1290                 {
1291                         public bool scale_children = true;
1292                         
1293                         public void PublicScaleControl (SizeF factor, BoundsSpecified specified)
1294                         {
1295                                 base.ScaleControl (factor, specified);
1296                         }
1297
1298                         public Rectangle PublicGetScaledBounds (Rectangle bounds, SizeF factor, BoundsSpecified specified)
1299                         {
1300                                 return base.GetScaledBounds (bounds, factor, specified);
1301                         }
1302                         
1303                         public void PublicSetStyle (ControlStyles flag, bool value)
1304                         {
1305                                 base.SetStyle (flag, value);
1306                         }
1307                         
1308                         public void PublicSetTopLevel (bool value)
1309                         {
1310                                 base.SetTopLevel (value);
1311                         }
1312                         
1313                         protected override bool ScaleChildren {
1314                                 get { return scale_children; }
1315                         }
1316                 }
1317 #endif
1318
1319                 class TestWindowTarget : IWindowTarget
1320                 {
1321                         public void OnHandleChange (IntPtr newHandle) {
1322                         }
1323
1324                         public void OnMessage (ref Message m) {
1325                         }
1326                 }
1327
1328                 [Test]
1329                 public void WindowTargetTest()
1330                 {
1331                         Control c = new Control ();
1332                         Assert.IsNotNull (c.WindowTarget, "WindowTarget1");
1333                         c.WindowTarget = null;
1334                         Assert.IsNull (c.WindowTarget, "WindowTarget2");
1335
1336                         IWindowTarget existing_target = c.WindowTarget;
1337                         IWindowTarget new_target = new TestWindowTarget ();
1338                         c.WindowTarget = new_target;
1339                         Assert.AreSame (new_target, c.WindowTarget, "WindowTarget3");
1340                         
1341                         TestHelper.RemoveWarning (existing_target);
1342                 }
1343
1344                 [Test]
1345                 public void TextTest()
1346                 {
1347                         Control r1 = new Control();
1348                         r1.Text = "Hi" ;
1349                         Assert.AreEqual("Hi" , r1.Text , "Text1");
1350
1351                         r1.ResetText();
1352                         Assert.AreEqual("" , r1.Text , "Text2");
1353                 }
1354
1355                 [Test]
1356                 public void PubMethodTest7()
1357                 {
1358                         Control r1 = new Control();
1359                         r1.RightToLeft = RightToLeft.Yes ;
1360                         r1.ResetRightToLeft() ;
1361                         Assert.AreEqual(RightToLeft.No , r1.RightToLeft , "#81");
1362                         r1.ImeMode = ImeMode.Off ;
1363                         r1.ResetImeMode () ;
1364                         Assert.AreEqual(ImeMode.NoControl , r1.ImeMode , "#82");
1365                         r1.ForeColor= SystemColors.GrayText ;
1366                         r1.ResetForeColor() ;
1367                         Assert.AreEqual(SystemColors.ControlText , r1.ForeColor , "#83");
1368                         //r1.Font = Font.FromHdc();
1369                         r1.ResetFont () ;
1370                         //Assert.AreEqual(FontFamily.GenericSansSerif , r1.Font , "#83");
1371                         r1.Cursor = Cursors.Hand ;
1372                         r1.ResetCursor () ;
1373                         Assert.AreEqual(Cursors.Default , r1.Cursor , "#83");
1374                         //r1.DataBindings = System.Windows.Forms.Binding ;
1375                         //r1.ResetBindings() ;
1376                         //Assert.AreEqual(ControlBindingsCollection , r1.DataBindings  , "#83");
1377                         r1.BackColor = Color.Black ;
1378                         r1.ResetBackColor() ;
1379                         Assert.AreEqual( SystemColors.Control , r1.BackColor  , "#84");
1380                         r1.BackColor = Color.Black ;
1381                         r1.Refresh() ;
1382                         Assert.AreEqual( null , r1.Region , "#85");
1383                         Rectangle M = new Rectangle(10, 20, 30 ,40);
1384                         r1.RectangleToScreen(M) ;
1385                         Assert.AreEqual( null , r1.Region , "#86");
1386                 }
1387
1388                 [Test]
1389                 public void ScreenClientCoords()
1390                 {
1391                         Label l;
1392                         Point p1;
1393                         Point p2;
1394                         Point p3;
1395
1396                         l = new Label();
1397                         l.Left = 10;
1398                         l.Top  = 12;
1399                         l.Visible = true;
1400                         p1 = new Point (10,10);
1401                         p2 = l.PointToScreen(p1);
1402                         p3 = l.PointToClient(p2);
1403
1404                         Assert.AreEqual (p1, p3, "SC1");
1405                 }
1406
1407                 [Test]
1408                 public void ContainsTest ()
1409                 {
1410                         Control t = new Control ();
1411                         Control s = new Control ();
1412
1413                         t.Controls.Add (s);
1414
1415                         Assert.AreEqual (true, t.Contains (s), "Con1");
1416                         Assert.AreEqual (false, s.Contains (t), "Con2");
1417                         Assert.AreEqual (false, s.Contains (null), "Con3");
1418                         Assert.AreEqual (false, t.Contains (new Control ()), "Con4");
1419                 }
1420
1421                 [Test]
1422                 public void IsHandleCreated_NotVisible ()
1423                 {
1424                         Control c = new Control ();
1425                         c.Visible = false;
1426
1427                         Form form = new Form ();
1428                         form.ShowInTaskbar = false;
1429                         form.Controls.Add (c);
1430                         form.Show ();
1431
1432                         Assert.IsFalse (c.IsHandleCreated, "#1");
1433                         c.Visible = true;
1434                         Assert.IsTrue (c.IsHandleCreated, "#2");
1435                         c.Visible = false;
1436                         Assert.IsTrue (c.IsHandleCreated, "#3");
1437                         form.Close ();
1438                 }
1439
1440                 class OnCreateControlTest : Control {
1441                         public bool reached = false;
1442                         protected override void OnCreateControl () {
1443                                 reached = true;
1444                         }
1445                 }
1446
1447                 [Test]
1448                 public void CreateControlVisibleTest ()
1449                 {
1450                         OnCreateControlTest test = new OnCreateControlTest ();
1451                         test.Visible = false;
1452                         Assert.IsFalse (test.IsHandleCreated, "0");
1453                         Assert.IsFalse (test.Visible, "1");
1454                         test.Visible = true;
1455                         Assert.IsTrue (test.Visible, "2");
1456                         Assert.IsTrue (test.reached, "3");
1457                 }
1458
1459
1460                 [Test]
1461                 public void CreateGraphicsTest ()
1462                 {
1463                         Graphics g = null;
1464                         Pen p = null;
1465
1466                         try {
1467                                 Control c = new Control ();
1468                                 c.SetBounds (0,0, 20, 20);
1469                                 g = c.CreateGraphics ();
1470                                 Assert.IsNotNull (g, "Graph1");
1471                         } finally {
1472                                 if (p != null)
1473                                         p.Dispose ();
1474                                 if (g != null)
1475                                         g.Dispose ();
1476                         }
1477                 }
1478
1479                 bool delegateCalled = false;
1480                 public delegate void TestDelegate ();
1481
1482                 public void delegate_call () {
1483                         delegateCalled = true;
1484
1485                         TestHelper.RemoveWarning (delegateCalled);
1486                 }
1487
1488                 [Test]
1489                 [ExpectedException(typeof(InvalidOperationException))]
1490                 public void InvokeException1 () {
1491                         Control c = new Control ();
1492                         IAsyncResult result;
1493
1494                         result = c.BeginInvoke (new TestDelegate (delegate_call));
1495                         c.EndInvoke (result);
1496                 }
1497
1498                 [Test]
1499                 public void FindFormTest () {
1500                         Form f = new Form ();
1501
1502                         f.ShowInTaskbar = false;
1503                         f.Name = "form";
1504                         Control c = null;
1505
1506                         try {
1507                                 f.Controls.Add (c = new Control ());
1508                                 Assert.AreEqual (f.Name, c.FindForm ().Name, "Find1");
1509
1510                                 f.Controls.Remove (c);
1511
1512                                 GroupBox g = new GroupBox ();
1513                                 g.Name = "box";
1514                                 f.Controls.Add (g);
1515                                 g.Controls.Add (c);
1516
1517                                 Assert.AreEqual (f.Name, f.FindForm ().Name, "Find2");
1518
1519                                 g.Controls.Remove (c);
1520                                 Assert.IsNull(c.FindForm (), "Find3");
1521
1522                         } finally {
1523                                 if (c != null)
1524                                         c.Dispose ();
1525                                 if (f != null)
1526                                         f.Dispose ();
1527                         }
1528                 }
1529
1530                 [Test]
1531                 public void FocusTest ()
1532                 {
1533                         Form f = null;
1534                         Button c = null, d = null;
1535
1536                         try {
1537                                 f = new Form ();
1538                                 f.ShowInTaskbar = false;
1539                                 f.Visible = true;
1540                                 c = new Button ();
1541                                 c.Visible = true;
1542                                 f.Controls.Add (c);
1543
1544                                 d = new Button ();
1545                                 d.Visible = false;
1546                                 f.Controls.Add (d);
1547
1548                                 Assert.IsTrue (c.CanFocus, "Focus1");
1549                                 Assert.IsFalse (c.Focused, "Focus2");
1550                                 c.Focus ();
1551                                 Assert.IsTrue (c.Focused, "Focus3");
1552                                 d.Focus ();
1553                                 Assert.IsFalse (d.Focused, "Focus4");
1554
1555                                 d.Visible = true;
1556                                 d.Focus ();
1557                                 Assert.IsTrue (d.Focused, "Focus5");
1558                                 Assert.IsFalse (c.Focused, "Focus6");
1559
1560                                 c.Enabled = false;
1561                                 Assert.IsFalse (c.Focused, "Focus7");
1562                         } finally {
1563                                 if (f != null)
1564                                         f.Dispose ();
1565                                 if (c != null)
1566                                         c.Dispose ();
1567                                 if (d != null)
1568                                         d.Dispose ();
1569                         }
1570                 }
1571
1572                 [Test]
1573                 public void FromHandleTest ()
1574                 {
1575                         Control c1 = null;
1576                         Control c2 = null;
1577
1578                         try {
1579                                 c1 = new Control ();
1580                                 c2 = new Control ();
1581
1582                                 c1.Name = "parent";
1583                                 c2.Name = "child";
1584                                 c1.Controls.Add(c2);
1585
1586                                 // Handle
1587                                 Assert.AreEqual (c1.Name, Control.FromHandle (c1.Handle).Name, "Handle1");
1588                                 Assert.IsNull (Control.FromHandle (IntPtr.Zero), "Handle2");
1589
1590                                 // ChildHandle
1591                                 Assert.AreEqual (c1.Name, Control.FromChildHandle (c1.Handle).Name, "Handle3");
1592                                 Assert.IsNull (Control.FromChildHandle (IntPtr.Zero), "Handle4");
1593
1594
1595                         } finally {
1596                                 if (c1 != null)
1597                                         c1.Dispose ();
1598
1599                                 if (c2 != null)
1600                                         c2.Dispose ();
1601                         }
1602                 }
1603
1604                 [Test]
1605                 public void GetChildAtPointTest ()
1606                 {
1607                         Control c = null, d = null;
1608                         TransparentControl e = null;
1609
1610                         try {
1611                                 c = new Control ();
1612                                 c.Name = "c1";
1613                                 c.SetBounds (0, 0, 100, 100);
1614
1615                                 d = new Control ();
1616                                 d.Name = "d1";
1617                                 d.SetBounds (10, 10, 40, 40);
1618                                 c.Controls.Add (d);
1619
1620                                 e = new TransparentControl ();
1621                                 e.Name = "e1";
1622                                 e.SetBounds (55, 55, 10, 10);
1623
1624                                 Control l = c.GetChildAtPoint (new Point (15, 15));
1625                                 Assert.AreEqual (d.Name, l.Name, "Child1");
1626                                 Assert.IsFalse (e.Name == l.Name, "Child2");
1627
1628                                 l = c.GetChildAtPoint (new Point (57, 57));
1629                                 Assert.AreEqual (null, l, "Child3");
1630
1631                                 l = c.GetChildAtPoint (new Point (10, 10));
1632                                 Assert.AreEqual (d.Name, l.Name, "Child4");
1633
1634                                 // GetChildAtPointSkip is not implemented and the following test is breaking for Net_2_0 profile
1635 #if NET_2_0
1636                                 c.Controls.Add (e);
1637                                 e.Visible = false;
1638                                 l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Invisible);
1639                                 Assert.IsNull (l, "Child5");
1640
1641                                 e.Visible = true;
1642                                 l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Invisible);
1643                                 Assert.AreSame (e.Name, l.Name, "Child6");
1644
1645                                 e.Enabled = false;
1646                                 l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Disabled);
1647                                 Assert.IsNull (l, "Child7");
1648
1649                                 e.Enabled = true;
1650                                 l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Disabled);
1651                                 Assert.AreSame (e.Name, l.Name, "Child8");
1652
1653                                 
1654                                 e.BackColor = Color.Transparent;
1655                                 l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Transparent);
1656                                 Assert.IsNull (l, "Child9");
1657
1658                                 e.BackColor = Color.Green;
1659                                 l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Transparent);
1660                                 Assert.AreSame (e.Name, l.Name, "Child10");
1661
1662 #endif // NET_2_0
1663                         } finally {
1664                                 if (c != null)
1665                                         c.Dispose ();
1666                                 if (d != null)
1667                                         d.Dispose ();
1668                         }
1669                 }
1670
1671                 private class TransparentControl : Control
1672                 {
1673                         public TransparentControl ()
1674                         {
1675                                 SetStyle (ControlStyles.SupportsTransparentBackColor, true);
1676                         }
1677
1678                         protected override CreateParams CreateParams
1679                         {
1680                                 get
1681                                 {
1682                                         CreateParams cp = base.CreateParams;
1683                                         cp.ExStyle |= 0x00000020;
1684                                         return cp;
1685                                 }
1686                         }
1687                 }
1688                 
1689                 [Test]
1690                 public void ResetFontTest ()
1691                 {
1692                         Control c = new Control ();
1693                         c.Font = new Font (c.Font.FontFamily, 3, FontStyle.Italic);
1694                         c.ResetFont ();
1695
1696                         Assert.IsFalse (c.Font.Bold, "#1");
1697                         //Assert.AreEqual ("Microsoft Sans Serif", c.Font.FontFamily.Name, "#2");
1698                         Assert.IsFalse (c.Font.Italic, "#3");
1699                         //Assert.AreEqual ("Microsoft Sans Serif", c.Font.Name, "#4");
1700                         Assert.AreEqual (8.25, c.Font.Size, "#5");
1701                         Assert.AreEqual (8.25, c.Font.SizeInPoints, "#6");
1702                         Assert.IsFalse (c.Font.Strikeout, "#7");
1703                         Assert.IsFalse (c.Font.Underline, "#8");
1704                         Assert.AreEqual (GraphicsUnit.Point, c.Font.Unit, "#9");
1705 #if NET_2_0
1706                         Assert.AreEqual (true, c.Font.IsSystemFont, "#10");
1707 #endif
1708                 }
1709
1710                 public class LayoutTestControl : Control {
1711                         public int LayoutCount;
1712
1713                         public LayoutTestControl () : base() {
1714                                 LayoutCount = 0;
1715                         }
1716
1717                         protected override void OnLayout(LayoutEventArgs levent) {
1718                                 LayoutCount++;
1719                                 base.OnLayout (levent);
1720                         }
1721                 }
1722
1723                 [Test]
1724                 public void LayoutTest() {
1725                         LayoutTestControl c;
1726
1727                         c = new LayoutTestControl();
1728
1729                         c.SuspendLayout();
1730                         c.SuspendLayout();
1731                         c.SuspendLayout();
1732                         c.SuspendLayout();
1733
1734                         c.ResumeLayout(true);
1735                         c.PerformLayout();
1736                         c.ResumeLayout(true);
1737                         c.PerformLayout();
1738                         c.ResumeLayout(true);
1739                         c.PerformLayout();
1740                         c.ResumeLayout(true);
1741                         c.PerformLayout();
1742                         c.ResumeLayout(true);
1743                         c.PerformLayout();
1744                         c.ResumeLayout(true);
1745                         c.PerformLayout();
1746                         c.ResumeLayout(true);
1747                         c.PerformLayout();
1748                         c.SuspendLayout();
1749                         c.PerformLayout();
1750
1751                         Assert.AreEqual(5, c.LayoutCount, "Layout Suspend/Resume locking does not bottom out at 0");
1752                 }
1753
1754                 [Test]
1755                 [ExpectedException(typeof(ArgumentException))]
1756                 public void TransparentBackgroundTest1() {
1757                         Control c;
1758
1759                         c = new Control();
1760                         c.BackColor = Color.Transparent;
1761                 }
1762
1763                 [Test]
1764                 public void TransparentBackgroundTest2() {
1765                         Panel   c;
1766
1767                         c = new Panel();
1768                         c.BackColor = Color.Transparent;
1769                         Assert.AreEqual(Color.Transparent, c.BackColor, "Transparent background not set");
1770                 }
1771
1772                 [Test]
1773                 public void TransparentBackgroundTest3() {
1774                         Control c;
1775
1776                         c = new Control();
1777                         c.BackColor = Color.Empty;
1778                         Assert.AreEqual(Control.DefaultBackColor, c.BackColor, "Setting empty color failed");
1779                 }
1780
1781                 [Test]
1782                 public void Dock_Value_Invalid ()
1783                 {
1784                         Control c = new Control ();
1785                         try {
1786                                 c.Dock = (DockStyle) 666;
1787                                 Assert.Fail ("#1");
1788                         } catch (InvalidEnumArgumentException ex) {
1789                                 Assert.AreEqual (typeof (InvalidEnumArgumentException), ex.GetType (), "#2");
1790                                 Assert.IsNotNull (ex.Message, "#3");
1791                                 Assert.IsNotNull (ex.ParamName, "#4");
1792                                 Assert.AreEqual ("value", ex.ParamName, "#5");
1793                                 Assert.IsNull (ex.InnerException, "#6");
1794                         }
1795                 }
1796
1797                 [Test]
1798                 public void EnabledTest1() {
1799                         Control child;
1800                         Control parent;
1801                         Control grandma;
1802
1803                         grandma = new Control();
1804                         parent = new Control();
1805                         child = new Control();
1806
1807                         grandma.Controls.Add(parent);
1808                         parent.Controls.Add(child);
1809                         grandma.Enabled = false;
1810                         Assert.AreEqual(grandma.Enabled, child.Enabled, "Child did not inherit disabled state");
1811                 }
1812
1813                 int EnabledCalledCount = 0;
1814                 private void EnabledTest2EnabledChanged(object sender, EventArgs e) {
1815                         EnabledCalledCount++;
1816                 }
1817
1818                 [Test]
1819                 public void EnabledTest2() {
1820                         // Check nesting of enabled calls
1821                         // OnEnabled is not called for disabled child controls
1822                         Control child;
1823                         Control parent;
1824                         Control grandma;
1825
1826                         EnabledCalledCount = 0;
1827
1828                         grandma = new Control();
1829                         parent = new Control();
1830                         child = new Control();
1831                         child.EnabledChanged += new EventHandler(EnabledTest2EnabledChanged);
1832
1833                         grandma.Controls.Add(parent);
1834                         parent.Controls.Add(child);
1835                         grandma.Enabled = false;
1836
1837                         Assert.AreEqual(1, EnabledCalledCount, "Child Enabled Event not properly fired");
1838                         grandma.Enabled = true;
1839                         Assert.AreEqual(2, EnabledCalledCount, "Child Enabled Event not properly fired");
1840                         child.Enabled = false;
1841                         grandma.Enabled = false;
1842                         Assert.AreEqual(3, EnabledCalledCount, "Child Enabled Event not properly fired");
1843                 }
1844
1845                 [Test]
1846                 public void ControlsRemoveNullTest ()
1847                 {
1848                         Control c = new Control ();
1849                         c.Controls.Remove (null);
1850                 }
1851
1852                 [Test]
1853                 public void ControlsAddNullTest ()
1854                 {
1855                         Control c = new Control ();
1856                         c.Controls.Add (null);
1857                 }
1858
1859                 [Test]
1860                 [ExpectedException (typeof (ArgumentNullException))]
1861                 public void ControlsSetChildIndexNullTest ()
1862                 {
1863                         Control c = new Control ();
1864                         c.Controls.SetChildIndex (null, 1);
1865                 }
1866
1867                 [Test]
1868                 [ExpectedException (typeof (ArgumentNullException))]
1869                 public void ControlsAddRangeNullTest ()
1870                 {
1871                         Control c = new Control ();
1872                         c.Controls.AddRange (null);
1873                 }
1874
1875                 [Test]
1876                 public void ControlsAddRangeNullElementTest ()
1877                 {
1878                         Control c = new Control ();
1879                         Control[] subcontrols = new Control[2];
1880                         subcontrols[0] = new Control ();
1881                         subcontrols[1] = null;
1882
1883                         c.Controls.AddRange (subcontrols);
1884                 }
1885
1886                 [Test]
1887                 public void RegionTest () {
1888                         Form f = new Form ();
1889                         f.ShowInTaskbar = false;
1890                         Control c = new Control ();
1891                         f.Controls.Add (c);
1892                         Assert.IsNull (c.Region, "#A1");
1893                         f.Show ();
1894                         Assert.IsNull (c.Region, "#A2");
1895                         c.Region = null;
1896                         Assert.IsNull (c.Region, "#A3");
1897                         f.Dispose ();
1898
1899                         Region region = new Region ();
1900                         f = new Form ();
1901                         f.ShowInTaskbar = false;
1902                         c = new Control ();
1903                         f.Controls.Add (c);
1904                         c.Region = region;
1905                         Assert.IsNotNull (c.Region, "#B1");
1906                         Assert.AreSame (region, c.Region, "#B2");
1907                         f.Show ();
1908                         c.Region = null;
1909                         Assert.IsNull (c.Region, "#B3");
1910
1911                         f.Dispose ();
1912                 }
1913
1914                 [Test] // bug #80280
1915                 public void Validated_Multiple_Containers ()
1916                 {
1917                         Form form = new Form ();
1918                         form.ShowInTaskbar = false;
1919
1920                         UserControl control1 = new UserControl();
1921                         UserControl container1 = new UserControl();
1922                         control1.Tag = true;
1923                         control1.Validated += new EventHandler (Control_ValidatedHandler);
1924                         container1.Controls.Add(control1);
1925                         form.Controls.Add (container1);
1926
1927                         UserControl container2 = new UserControl();
1928                         UserControl control2 = new UserControl();
1929                         container2.Controls.Add(control2);
1930                         form.Controls.Add (container2);
1931
1932                         Assert.IsTrue ((bool) control1.Tag, "#1");
1933                         control1.Select();
1934                         Assert.IsTrue ((bool) control1.Tag, "#2");
1935                         control2.Select();
1936                         Assert.IsFalse ((bool) control1.Tag, "#3");
1937
1938                         form.Dispose ();
1939                 }
1940
1941                 private void Control_ValidatedHandler (object sender, EventArgs e)
1942                 {
1943                         ((Control) sender).Tag = false;
1944                 }
1945
1946 #if NET_2_0
1947                 [Test]
1948                 public void UseWaitCursorTest ()
1949                 {
1950                         Control c = new Control ();
1951                         Assert.IsFalse (c.UseWaitCursor, "#1");
1952                         c.Cursor = Cursors.Hand;
1953                         c.UseWaitCursor = true;
1954                         Assert.AreEqual (Cursors.WaitCursor, c.Cursor, "#2");
1955                         c.UseWaitCursor = false;
1956                         Assert.AreEqual (Cursors.Hand, c.Cursor, "#3");
1957                         
1958                         c.UseWaitCursor = true;
1959                         c.Cursor = Cursors.Help;
1960                         Assert.AreEqual (Cursors.WaitCursor, c.Cursor, "#4");
1961                         Assert.AreEqual (true, c.UseWaitCursor, "#5");
1962                         
1963                         c.UseWaitCursor = false;
1964                         Assert.AreEqual (Cursors.Help, c.Cursor, "#6");
1965                 }
1966
1967                 [Test] // bug #80621, #81125
1968                 public void DontCallSizeFromClientSize ()
1969                 {
1970                         SizeControl sc = new SizeControl ();
1971                         
1972                         Assert.AreEqual (0, sc.size_from_client_size_count, "A1");
1973                         
1974                         sc.ClientSize = new Size (300, 300);
1975                         Assert.AreEqual (0, sc.size_from_client_size_count, "A2");
1976                         
1977                         SizeForm sf = new SizeForm ();
1978                         sf.ShowInTaskbar = false;
1979                         sf.Show ();
1980                         
1981                         Assert.AreEqual (0, sc.size_from_client_size_count, "A3");
1982
1983                         sc.ClientSize = new Size (300, 300);
1984                         Assert.AreEqual (0, sc.size_from_client_size_count, "A4");      
1985                         
1986                         sf.Dispose ();  
1987                 }
1988                 
1989                 private class SizeControl : Control
1990                 {
1991                         public int size_from_client_size_count = 0;
1992                         
1993                         protected override Size SizeFromClientSize (Size clientSize)
1994                         {
1995                                 size_from_client_size_count++;
1996                                 return base.SizeFromClientSize (clientSize);
1997                         }
1998                 }
1999
2000                 private class SizeForm : Form
2001                 {
2002                         public int size_from_client_size_count = 0;
2003
2004                         protected override Size SizeFromClientSize (Size clientSize)
2005                         {
2006                                 size_from_client_size_count++;
2007                                 return base.SizeFromClientSize (clientSize);
2008                         }
2009                 }
2010 #endif
2011
2012                 public class MockControl : Control
2013                 {
2014                         public int font_height {
2015                                 get { return base.FontHeight; }
2016                                 set { base.FontHeight = value; }
2017                         }
2018                 }
2019
2020                 const int WM_KEYDOWN = 0x0100;
2021                 const int WM_CHAR = 0x0102;
2022                 const int WM_SYSCHAR = 0x0106;
2023                 const int WM_KEYUP = 0x0101;
2024
2025 #if NET_2_0
2026                 [Test]
2027                 public void MethodPreProcessControlMessage ()
2028                 {
2029                         bool testing_callstack = false;
2030
2031                         MyControl c = new MyControl ();
2032                         Message m = new Message ();
2033                         m.HWnd = c.Handle;
2034                         m.Msg = WM_KEYDOWN;
2035                         m.WParam = (IntPtr)Keys.Down;
2036                         m.LParam = IntPtr.Zero;
2037
2038                         if (testing_callstack) Console.WriteLine ("Start");
2039                         Assert.AreEqual (PreProcessControlState.MessageNotNeeded, c.PreProcessControlMessage (ref m), "A1");
2040                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2041
2042                         c.SetState (State.OnPreviewKeyDown);
2043                         if (testing_callstack) Console.WriteLine ("Start");
2044                         Assert.AreEqual (PreProcessControlState.MessageNeeded, c.PreProcessControlMessage (ref m), "A2");
2045                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2046
2047                         c.SetState (State.ProcessCmdKey);
2048                         if (testing_callstack) Console.WriteLine ("Start");
2049                         Assert.AreEqual (PreProcessControlState.MessageProcessed, c.PreProcessControlMessage (ref m), "A3");
2050                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2051
2052                         c.SetState (State.IsInputKey);
2053                         if (testing_callstack) Console.WriteLine ("Start");
2054                         Assert.AreEqual (PreProcessControlState.MessageNeeded, c.PreProcessControlMessage (ref m), "A4");
2055                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2056
2057                         c.SetState (State.ProcessDialogKey);
2058                         if (testing_callstack) Console.WriteLine ("Start");
2059                         Assert.AreEqual (PreProcessControlState.MessageProcessed, c.PreProcessControlMessage (ref m), "A5");
2060                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2061
2062
2063                         m.Msg = WM_CHAR;
2064                         c.SetState (State.None);
2065                         if (testing_callstack) Console.WriteLine ("Start");
2066                         Assert.AreEqual (PreProcessControlState.MessageNotNeeded, c.PreProcessControlMessage (ref m), "A6");
2067                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2068
2069                         c.SetState (State.IsInputChar);
2070                         if (testing_callstack) Console.WriteLine ("Start");
2071                         Assert.AreEqual (PreProcessControlState.MessageNeeded, c.PreProcessControlMessage (ref m), "A7");
2072                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2073
2074                         c.SetState (State.ProcessDialogChar);
2075                         if (testing_callstack) Console.WriteLine ("Start");
2076                         Assert.AreEqual (PreProcessControlState.MessageProcessed, c.PreProcessControlMessage (ref m), "A8");
2077                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2078
2079
2080                         m.Msg = WM_SYSCHAR;
2081                         c.SetState (State.None);
2082                         if (testing_callstack) Console.WriteLine ("Start");
2083                         Assert.AreEqual (PreProcessControlState.MessageNotNeeded, c.PreProcessControlMessage (ref m), "A9");
2084                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2085
2086                         c.SetState (State.IsInputChar);
2087                         if (testing_callstack) Console.WriteLine ("Start");
2088                         Assert.AreEqual (PreProcessControlState.MessageNeeded, c.PreProcessControlMessage (ref m), "A10");
2089                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2090
2091                         c.SetState (State.ProcessDialogChar);
2092                         if (testing_callstack) Console.WriteLine ("Start");
2093                         Assert.AreEqual (PreProcessControlState.MessageProcessed, c.PreProcessControlMessage (ref m), "A11");
2094                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2095
2096
2097                         m.Msg = WM_KEYUP;
2098                         if (testing_callstack) Console.WriteLine ("Start");
2099                         Assert.AreEqual (PreProcessControlState.MessageNotNeeded, c.PreProcessControlMessage (ref m), "A12");
2100                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2101
2102                         c.SetState (State.OnPreviewKeyDown);
2103                         if (testing_callstack) Console.WriteLine ("Start");
2104                         Assert.AreEqual (PreProcessControlState.MessageNotNeeded, c.PreProcessControlMessage (ref m), "A13");
2105                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2106
2107                         c.SetState (State.ProcessCmdKey);
2108                         if (testing_callstack) Console.WriteLine ("Start");
2109                         Assert.AreEqual (PreProcessControlState.MessageNotNeeded, c.PreProcessControlMessage (ref m), "A14");
2110                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2111
2112                         c.SetState (State.IsInputKey);
2113                         if (testing_callstack) Console.WriteLine ("Start");
2114                         Assert.AreEqual (PreProcessControlState.MessageNotNeeded, c.PreProcessControlMessage (ref m), "A15");
2115                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2116
2117                         c.SetState (State.ProcessDialogKey);
2118                         if (testing_callstack) Console.WriteLine ("Start");
2119                         Assert.AreEqual (PreProcessControlState.MessageNotNeeded, c.PreProcessControlMessage (ref m), "A16");
2120                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2121                 }
2122 #endif
2123
2124                 [Test]
2125                 public void MethodPreProcessMessage ()
2126                 {
2127                         bool testing_callstack = false;
2128
2129                         MyControl c = new MyControl ();
2130                         Message m = new Message ();
2131                         m.HWnd = c.Handle;
2132                         m.Msg = WM_KEYDOWN;
2133                         m.WParam = (IntPtr)Keys.Down;
2134                         m.LParam = IntPtr.Zero;
2135
2136                         if (testing_callstack) Console.WriteLine ("Start");
2137                         Assert.AreEqual (false, c.PreProcessMessage (ref m), "A1");
2138                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2139
2140                         c.SetState (State.OnPreviewKeyDown);
2141                         if (testing_callstack) Console.WriteLine ("Start");
2142                         Assert.AreEqual (false, c.PreProcessMessage (ref m), "A2");
2143                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2144
2145                         c.SetState (State.ProcessCmdKey);
2146                         if (testing_callstack) Console.WriteLine ("Start");
2147                         Assert.AreEqual (true, c.PreProcessMessage (ref m), "A3");
2148                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2149
2150                         c.SetState (State.IsInputKey);
2151                         if (testing_callstack) Console.WriteLine ("Start");
2152                         Assert.AreEqual (false, c.PreProcessMessage (ref m), "A4");
2153                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2154
2155                         c.SetState (State.ProcessDialogKey);
2156                         if (testing_callstack) Console.WriteLine ("Start");
2157                         Assert.AreEqual (true, c.PreProcessMessage (ref m), "A5");
2158                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2159
2160
2161                         m.Msg = WM_CHAR;
2162                         c.SetState (State.None);
2163                         if (testing_callstack) Console.WriteLine ("Start");
2164                         Assert.AreEqual (false, c.PreProcessMessage (ref m), "A6");
2165                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2166
2167                         c.SetState (State.IsInputChar);
2168                         if (testing_callstack) Console.WriteLine ("Start");
2169                         Assert.AreEqual (false, c.PreProcessMessage (ref m), "A7");
2170                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2171
2172                         c.SetState (State.ProcessDialogChar);
2173                         if (testing_callstack) Console.WriteLine ("Start");
2174                         Assert.AreEqual (true, c.PreProcessMessage (ref m), "A8");
2175                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2176
2177
2178                         m.Msg = WM_SYSCHAR;
2179                         c.SetState (State.None);
2180                         if (testing_callstack) Console.WriteLine ("Start");
2181                         Assert.AreEqual (false, c.PreProcessMessage (ref m), "A9");
2182                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2183
2184                         c.SetState (State.IsInputChar);
2185                         if (testing_callstack) Console.WriteLine ("Start");
2186                         Assert.AreEqual (false, c.PreProcessMessage (ref m), "A10");
2187                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2188
2189                         c.SetState (State.ProcessDialogChar);
2190                         if (testing_callstack) Console.WriteLine ("Start");
2191                         Assert.AreEqual (true, c.PreProcessMessage (ref m), "A11");
2192                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2193
2194
2195                         m.Msg = WM_KEYUP;
2196                         if (testing_callstack) Console.WriteLine ("Start");
2197                         Assert.AreEqual (false, c.PreProcessMessage (ref m), "A12");
2198                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2199
2200                         c.SetState (State.OnPreviewKeyDown);
2201                         if (testing_callstack) Console.WriteLine ("Start");
2202                         Assert.AreEqual (false, c.PreProcessMessage (ref m), "A13");
2203                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2204
2205                         c.SetState (State.ProcessCmdKey);
2206                         if (testing_callstack) Console.WriteLine ("Start");
2207                         Assert.AreEqual (false, c.PreProcessMessage (ref m), "A14");
2208                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2209
2210                         c.SetState (State.IsInputKey);
2211                         if (testing_callstack) Console.WriteLine ("Start");
2212                         Assert.AreEqual (false, c.PreProcessMessage (ref m), "A15");
2213                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2214
2215                         c.SetState (State.ProcessDialogKey);
2216                         if (testing_callstack) Console.WriteLine ("Start");
2217                         Assert.AreEqual (false, c.PreProcessMessage (ref m), "A16");
2218                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2219                 }
2220                 private enum State
2221                 {
2222                         None,
2223                         ProcessCmdKey,
2224                         OnPreviewKeyDown,
2225                         IsInputChar,
2226                         IsInputKey,
2227                         PreProcessMessage,
2228                         ProcessDialogKey,
2229                         ProcessDialogChar
2230                 }
2231
2232                 private class MyControl : Control
2233                 {
2234
2235                         private State current_state;
2236                         bool testing_callstack = false;
2237
2238                         public void SetState (State state)
2239                         {
2240                                 current_state = state;
2241                         }
2242
2243                         protected override bool ProcessCmdKey (ref Message msg, Keys keyData)
2244                         {
2245                                 if (testing_callstack) Console.Write ("ProcessCmdKey[");
2246                                 if (current_state == State.ProcessCmdKey) {
2247                                         if (testing_callstack) Console.WriteLine ("]");
2248                                         return true;
2249                                 }
2250
2251                                 bool retval = base.ProcessCmdKey (ref msg, keyData);
2252                                 if (testing_callstack) Console.WriteLine ("]");
2253                                 return retval;
2254                         }
2255
2256 #if NET_2_0
2257                         protected override void OnPreviewKeyDown (PreviewKeyDownEventArgs e)
2258                         {
2259                                 if (testing_callstack) Console.Write ("OnPreviewKeyDown[");
2260                                 if (current_state == State.OnPreviewKeyDown) {
2261                                         e.IsInputKey = true;
2262                                         if (testing_callstack) Console.WriteLine ("]");
2263                                         return;
2264                                 }
2265
2266                                 base.OnPreviewKeyDown (e);
2267                                 if (testing_callstack) Console.WriteLine ("]");
2268                         }
2269 #endif
2270
2271                         protected override bool IsInputChar (char charCode)
2272                         {
2273                                 if (testing_callstack) Console.Write ("IsInputChar[");
2274                                 if (current_state == State.IsInputChar) {
2275                                         if (testing_callstack) Console.WriteLine ("true]");
2276                                         return true;
2277                                 }
2278
2279                                 bool retval = base.IsInputChar (charCode);
2280                                 if (testing_callstack) Console.WriteLine ("{0}]", retval.ToString ());
2281                                 return retval;
2282                         }
2283
2284                         protected override bool IsInputKey (Keys keyData)
2285                         {
2286                                 if (testing_callstack) Console.Write ("IsInputKey[");
2287                                 if (current_state == State.IsInputKey) {
2288                                         if (testing_callstack) Console.WriteLine ("]");
2289                                         return true;
2290                                 }
2291
2292                                 bool retval = base.IsInputKey (keyData);
2293                                 if (testing_callstack) Console.WriteLine ("]");
2294                                 return retval;
2295                         }
2296
2297                         public override bool PreProcessMessage (ref Message msg)
2298                         {
2299                                 if (testing_callstack) Console.Write ("PreProcessMessage[");
2300                                 if (current_state == State.PreProcessMessage) {
2301                                         if (testing_callstack) Console.WriteLine ("]");
2302                                         return true;
2303                                 }
2304
2305                                 bool retval = base.PreProcessMessage (ref msg);
2306                                 if (testing_callstack) Console.WriteLine ("]");
2307                                 return retval;
2308                         }
2309
2310                         protected override bool ProcessDialogKey (Keys keyData)
2311                         {
2312                                 if (testing_callstack) Console.Write ("ProcessDialogKey[");
2313                                 if (current_state == State.ProcessDialogKey) {
2314                                         if (testing_callstack) Console.WriteLine ("]");
2315                                         return true;
2316                                 }
2317
2318                                 bool retval = base.ProcessDialogKey (keyData);
2319                                 if (testing_callstack) Console.WriteLine ("]");
2320                                 return retval;
2321                         }
2322
2323                         protected override bool ProcessDialogChar (char charCode)
2324                         {
2325                                 if (testing_callstack) Console.Write ("ProcessDialogChar[");
2326                                 if (current_state == State.ProcessDialogChar) {
2327                                         if (testing_callstack) Console.WriteLine ("]");
2328                                         return true;
2329                                 }
2330
2331                                 bool retval = base.ProcessDialogChar (charCode);
2332                                 if (testing_callstack) Console.WriteLine ("]");
2333                                 return retval;
2334                         }
2335                 }
2336                 
2337                 [Test]
2338                 public void MethodIsInputChar ()
2339                 {
2340                         // Basically, show that this method always returns false
2341                         InputCharControl m = new InputCharControl ();
2342                         bool result = false;
2343                         
2344                         for (int i = 0; i < 256; i++)
2345                                 result |= m.PublicIsInputChar ((char)i);
2346                         
2347                         Assert.AreEqual (false, result, "I1");
2348                 }
2349
2350                 private class InputCharControl : Control
2351                 {
2352                         public bool PublicIsInputChar (char charCode)
2353                         {
2354                                 return base.IsInputChar (charCode);
2355                         }
2356
2357                 }
2358
2359                 [Test] // bug #81118, 81718
2360                 public void VisibleTriggersLayout ()
2361                 {
2362                         Form f = new Form ();
2363                         f.ShowInTaskbar = false;
2364                         
2365                         Control c = new Control ();
2366                         c.Visible = false;
2367                         
2368                         f.Controls.Add (c);
2369                         
2370                         c.Dock = DockStyle.Fill;
2371                         c.Visible = true;
2372                         
2373                         Assert.AreEqual (f.ClientSize.Width, c.Width, "L1");
2374                 }
2375                 
2376                 [Test]
2377                 public void ResumeLayoutEffects ()
2378                 {
2379                         Form f = new Form ();
2380                         f.ShowInTaskbar = false;
2381                         f.ClientSize = new Size (300, 300);
2382                         
2383                         Button button1 = new Button ();
2384                         f.Controls.Add (button1);
2385                         button1.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
2386                         button1.Location = new Point (f.ClientSize.Width - button1.Width, f.ClientSize.Height - button1.Height);
2387
2388                         f.Show ();
2389                         
2390                         Assert.AreEqual (new Point (225, 277), button1.Location, "A1");
2391                         
2392                         f.SuspendLayout ();
2393                         f.Height += 10;
2394                         f.ResumeLayout (false);
2395                         f.PerformLayout ();
2396
2397                         Assert.AreEqual (new Point (225, 277), button1.Location, "A2");
2398
2399                         f.SuspendLayout ();
2400                         f.Height += 10;
2401                         f.ResumeLayout ();
2402
2403                         Assert.AreEqual (new Point (225, 287), button1.Location, "A3");
2404                         f.Dispose ();
2405                 }
2406         }
2407
2408         [TestFixture]
2409         public class ControlSetTopLevelTest
2410         {
2411                 class ControlPoker : Control {
2412                         public void DoSetTopLevel ()
2413                         {
2414                                 SetTopLevel (true);
2415                         }
2416                         public bool DoGetTopLevel ()
2417                         {
2418                                 return GetTopLevel ();
2419                         }
2420                 }
2421
2422                 [Test]
2423                 public void TestControl ()
2424                 {
2425                         ControlPoker c = new ControlPoker ();
2426                         c.Visible = false;
2427                         c.DoSetTopLevel ();
2428                         Assert.IsTrue (c.DoGetTopLevel (), "1");
2429                         Assert.IsFalse (c.Visible, "2");
2430                 }
2431
2432                 [Test]
2433                 [ExpectedException (typeof (ArgumentException))]
2434                 public void TestChildControl ()
2435                 {
2436                         Control c1 = new Control();
2437                         ControlPoker c2 = new ControlPoker ();
2438
2439                         c1.Controls.Add (c2);
2440                         c2.DoSetTopLevel ();
2441                 }
2442
2443                 [Test]
2444                 [ExpectedException (typeof (ArgumentException))]
2445                 public void TestTopLevelAdd () {
2446                         Form f = new Form();
2447                         Form f1 = new Form();
2448                         f.Controls.Add(f1);
2449                 }
2450                 
2451                 [Category ("NotWorking")]
2452                 [Test]
2453                 public void TestForm ()
2454                 {
2455                         Form f = new Form ();
2456                         Assert.IsFalse (f.Visible, "3");
2457                         f.TopLevel = true;
2458                         Assert.IsFalse (f.Visible, "4");
2459                 }
2460         }
2461
2462         [TestFixture]
2463         public class ControlResizeLayoutTest
2464         {
2465                 class ControlPoker : Control {
2466                         public void DoOnResize ()
2467                         {
2468                                 OnResize (EventArgs.Empty);
2469                         }
2470                 }
2471
2472                 int child_event;
2473                 string child_affected_property;
2474                 void ChildLayoutEvent (object sender, LayoutEventArgs e)
2475                 {
2476                         child_event ++;
2477                         child_affected_property = e.AffectedProperty;
2478                 }
2479
2480                 int parent_event;
2481                 string parent_affected_property;
2482                 void ParentLayoutEvent (object sender, LayoutEventArgs e)
2483                 {
2484                         parent_event ++;
2485                         parent_affected_property = e.AffectedProperty;
2486
2487                         TestHelper.RemoveWarning (parent_affected_property);
2488                 }
2489
2490                 [Test]
2491                 public void Test ()
2492                 {
2493                         Panel p = new Panel ();
2494                         ControlPoker c = new ControlPoker();
2495
2496                         p.Controls.Add (c);
2497
2498                         p.Layout += new LayoutEventHandler (ParentLayoutEvent);
2499                         c.Layout += new LayoutEventHandler (ChildLayoutEvent);
2500
2501                         c.DoOnResize ();
2502
2503                         Assert.AreEqual (1, child_event, "1");
2504                         Assert.AreEqual ("Bounds", child_affected_property, "2");
2505
2506                         Assert.AreEqual (0, parent_event, "3");
2507                 }
2508                 
2509         }
2510
2511         [TestFixture]
2512         [Category ("NotWorking")]
2513         public class ControlInvokeTest {
2514                 public delegate void TestDelegate ();
2515
2516                 Form f;
2517                 Control c;
2518                 Thread control_t;
2519                 ApplicationContext control_context;
2520                 bool delegateCalled = false;
2521                 bool threadDied = false;
2522
2523                 object m;
2524
2525                 void CreateControl ()
2526                 {
2527                         try {
2528                         f = new Form ();
2529                         f.ShowInTaskbar = false;
2530                         
2531                         c = new Control ();
2532
2533                         f.Controls.Add (c);
2534
2535                         Console.WriteLine ("f.Handle = {0}", f.Handle);
2536                         Console.WriteLine ("c.Handle = {0}", c.Handle);
2537
2538                         control_context = new ApplicationContext (f);
2539
2540                         Monitor.Enter (m);
2541                         Console.WriteLine ("pulsing");
2542                         Monitor.Pulse (m);
2543                         Monitor.Exit (m);
2544                         Console.WriteLine ("control thread running");
2545                         Application.Run (control_context);
2546                         c.Dispose ();
2547                         Console.WriteLine ("dying");
2548                         threadDied = true;
2549                         Monitor.Enter (m);
2550                         Console.WriteLine ("pulsing again");
2551                         Monitor.Pulse (m);
2552                         Monitor.Exit (m);
2553                         } catch (Exception e) { Console.WriteLine (e); }
2554                 }
2555
2556                 [Test]
2557                 public void InvokeTest ()
2558                 {
2559                         m = new object ();
2560
2561                         control_t = new Thread(new ThreadStart(CreateControl));
2562
2563                         Monitor.Enter (m);
2564
2565                         control_t.Start ();
2566
2567                         Console.WriteLine ("waiting on monitor");
2568                         Monitor.Wait (m);
2569
2570                         Console.WriteLine ("making async call");
2571
2572                         IAsyncResult result;
2573                         result = c.BeginInvoke (new TestDelegate (delegate_call));
2574                         c.EndInvoke (result);
2575
2576                         Assert.IsTrue (delegateCalled, "Invoke1");
2577
2578                         Monitor.Wait (m);
2579                         Assert.IsTrue (threadDied, "Invoke2");
2580                 }
2581
2582                 public void delegate_call () {
2583                         try {
2584                         /* invoked on control_context's thread */
2585                         delegateCalled = true;
2586                         f.Dispose ();
2587                         Console.WriteLine ("calling Application.Exit");
2588                         control_context.ExitThread ();
2589                         } catch (Exception e) { Console.WriteLine (e); }
2590                 }
2591                 
2592         }
2593
2594         [TestFixture]
2595         public class ControlWMTest
2596         {
2597                 [Test]
2598                 public void WM_PARENTNOTIFY_Test ()
2599                 {
2600                         WMTester tester;
2601                         Control child;
2602                         int child_handle;
2603                         
2604                         tester = new WMTester ();
2605                         child = new Control ();
2606                         tester.Controls.Add (child);
2607                         
2608                         tester.Visible = true;
2609                         child.Visible = true;
2610
2611                         child_handle = child.Handle.ToInt32 ();
2612
2613                         ArrayList msgs;
2614                         Message m1;
2615                                 
2616                         msgs = tester.Find (WndMsg.WM_PARENTNOTIFY);
2617                         
2618                         Assert.AreEqual (1, msgs.Count, "#1");
2619                         
2620                         m1 = (Message) msgs [0];
2621                         Assert.AreEqual (WndMsg.WM_CREATE, ((WndMsg) LowOrder (m1.WParam)),  "#2");
2622                         //Assert.AreEqual (child.Identifier??, HighOrder (m1.WParam),  "#3");
2623                         Assert.AreEqual (child_handle, m1.LParam.ToInt32 (),  "#4");
2624
2625                         child.Dispose ();
2626
2627                         msgs = tester.Find (WndMsg.WM_PARENTNOTIFY);
2628                         Assert.AreEqual (2, msgs.Count, "#5");
2629                         m1 = (Message) msgs [1];
2630
2631                         Assert.AreEqual (WndMsg.WM_DESTROY, ((WndMsg) LowOrder (m1.WParam)),  "#6");
2632                         //Assert.AreEqual (child.Identifier??, HighOrder (m1.WParam),  "#7");
2633                         Assert.AreEqual (child_handle, m1.LParam.ToInt32 (),  "#8");
2634
2635                         tester.Dispose ();
2636                 }
2637
2638                 internal static int LowOrder (int param) 
2639                 {
2640                         return ((int)(short)(param & 0xffff));
2641                 }
2642
2643                 internal static int HighOrder (int param) 
2644                 {
2645                         return ((int)(short)(param >> 16));
2646                 }
2647
2648                 internal static int LowOrder (IntPtr param) 
2649                 {
2650                         return ((int)(short)(param.ToInt32 () & 0xffff));
2651                 }
2652
2653                 internal static int HighOrder (IntPtr param) 
2654                 {
2655                         return ((int)(short)(param.ToInt32 () >> 16));
2656                 }
2657
2658                 internal class WMTester : Form
2659                 {
2660                         internal ArrayList Messages = new ArrayList ();
2661                         
2662                         internal bool Contains (WndMsg msg)
2663                         {
2664                                 return Contains (msg, Messages);
2665                         }
2666
2667                         internal bool Contains (WndMsg msg, ArrayList list)
2668                         {
2669                                 foreach (Message m in Messages) 
2670                                 {
2671                                         if (m.Msg == (int) msg)
2672                                                 return true;
2673                                 }
2674                                 return false;
2675                         }
2676
2677                         internal ArrayList Find (WndMsg msg)
2678                         {
2679                                 ArrayList result = new ArrayList ();
2680
2681                                 foreach (Message m in Messages)
2682                                 {
2683                                         if (m.Msg == (int) msg)
2684                                                 result.Add (m);
2685                                 }
2686                                 return result;
2687                         }
2688
2689                         protected override void WndProc(ref Message m)
2690                         {
2691                                 Console.WriteLine ("WndProc: " + m.ToString ());
2692                                 Messages.Add (m);
2693                                 base.WndProc (ref m);
2694                         }
2695                 }
2696         }
2697
2698 #if NET_2_0
2699         [TestFixture]
2700         public class ControlLayoutTest
2701         {
2702                 [Test]
2703                 public void SetUp ()
2704                 {
2705                         _layoutCount = 0;
2706                 }
2707
2708                 [Test] // bug #80456
2709                 public void LayoutTest ()
2710                 {
2711                         MockLayoutEngine layoutEngine = new MockLayoutEngine ();
2712                         MockControl c = new MockControl (layoutEngine);
2713                         c.Layout += new LayoutEventHandler (LayoutEvent);
2714                         Assert.IsFalse (layoutEngine.LayoutInvoked, "#A1");
2715                         Assert.AreEqual (0, _layoutCount, "#A2");
2716                         c.PerformLayout ();
2717                         Assert.IsTrue (layoutEngine.LayoutInvoked, "#A3");
2718                         Assert.AreEqual (1, _layoutCount, "#A4");
2719
2720                         layoutEngine.Reset ();
2721                         c.OverrideOnLayout = true;
2722                         Assert.IsFalse (layoutEngine.LayoutInvoked, "#B1");
2723                         c.PerformLayout ();
2724                         Assert.IsFalse (layoutEngine.LayoutInvoked, "#B2");
2725                         Assert.AreEqual (1, _layoutCount, "#B3");
2726                 }
2727
2728                 void LayoutEvent (object sender, LayoutEventArgs e)
2729                 {
2730                         _layoutCount++;
2731                 }
2732
2733                 private int _layoutCount;
2734
2735                 class MockControl : Control
2736                 {
2737                         public MockControl (LayoutEngine layoutEngine)
2738                         {
2739                                 _layoutEngine = layoutEngine;
2740                         }
2741
2742                         public bool OverrideOnLayout
2743                         {
2744                                 get { return _overrideOnLayout; }
2745                                 set { _overrideOnLayout = value; }
2746                         }
2747
2748                         protected override void OnLayout (LayoutEventArgs levent)
2749                         {
2750                                 if (!OverrideOnLayout)
2751                                         base.OnLayout (levent);
2752                         }
2753
2754                         public override LayoutEngine LayoutEngine {
2755                                 get {
2756                                         if (_layoutEngine == null)
2757                                                 return base.LayoutEngine;
2758                                         return _layoutEngine;
2759                                 }
2760                         }
2761
2762                         private bool _overrideOnLayout;
2763                         private LayoutEngine _layoutEngine;
2764                 }
2765
2766                 class MockLayoutEngine : LayoutEngine
2767                 {
2768                         public bool LayoutInvoked {
2769                                 get { return _layoutInvoked; }
2770                         }
2771
2772                         public void Reset ()
2773                         {
2774                                 _layoutInvoked = false;
2775                         }
2776
2777                         public override bool Layout (object container, LayoutEventArgs args)
2778                         {
2779                                 _layoutInvoked = true;
2780                                 return true;
2781                         }
2782
2783                         private bool _layoutInvoked;
2784                 }
2785         }
2786 #endif
2787 }