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