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