New test.
[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 //
7
8 using System;
9 using System.Windows.Forms;
10 using System.Drawing;
11 using System.Threading;
12 using System.Reflection;
13 using System.Runtime.Remoting;
14
15 using NUnit.Framework;
16
17 namespace MWF.MonoTest
18 {
19         [TestFixture]
20         public class ControlTest
21         {
22                 internal static void TestAccessibility(Control c, string Default, string Description, string Name, AccessibleRole Role) {
23                         Assert.AreEqual(false, c.AccessibilityObject == null, "Acc1");
24                         Assert.AreEqual(Default, c.AccessibleDefaultActionDescription, "Acc2");
25                         Assert.AreEqual(Description, c.AccessibleDescription, "Acc3");
26                         Assert.AreEqual(Name, c.AccessibleName, "Acc4");
27                         Assert.AreEqual(Role, c.AccessibleRole, "Acc5");
28                 }
29
30                 [Test]
31                 public void PubPropTest()
32                 {
33                         Control c = new Control();
34
35                         TestAccessibility(c, null, null, null, AccessibleRole.Default);
36
37                         // A
38                         Assert.AreEqual(false, c.AllowDrop , "A1");
39                         Assert.AreEqual(AnchorStyles.Top | AnchorStyles.Left, c.Anchor, "A2");
40
41                         // B
42                         Assert.AreEqual("Control", c.BackColor.Name , "B1");
43                         Assert.AreEqual(null, c.BackgroundImage, "B2");
44                         Assert.AreEqual(null, c.BindingContext, "B3");
45                         Assert.AreEqual(0, c.Bottom, "B4");
46                         Assert.AreEqual (new Rectangle (0,0, 0, 0) , c.Bounds , "B5");
47
48                         // C
49                         Assert.AreEqual(false, c.CanFocus, "C1");
50                         Assert.AreEqual(true, c.CanSelect, "C2");
51                         Assert.AreEqual(false, c.Capture, "C3");
52                         Assert.AreEqual(true, c.CausesValidation, "C4");
53                         Assert.AreEqual (new Rectangle(0, 0, 0, 0) , c.ClientRectangle , "C5");
54                         Assert.AreEqual (new Size(0, 0), c.ClientSize , "C6");
55
56                         string name = c.CompanyName;
57                         if (!name.Equals("Mono Project, Novell, Inc.") && !name.Equals("Microsoft Corporation")) {
58                                 Assert.Fail("CompanyName property does not match any accepted value - C7");
59                         }
60                         Assert.AreEqual(null, c.Container, "C8");
61                         Assert.AreEqual(false, c.ContainsFocus, "C9");
62                         Assert.AreEqual(null, c.ContextMenu, "C10");
63                         Assert.AreEqual(0, c.Controls.Count, "C11");
64                         Assert.AreEqual(true, c.Created, "C12");
65                         Assert.AreEqual(Cursors.Default, c.Cursor, "C13");
66
67                         // D
68                         Assert.AreEqual(false, c.DataBindings == null, "D1");
69                         Assert.AreEqual("Control", Control.DefaultBackColor.Name, "D2");
70                         Assert.AreEqual("ControlText", Control.DefaultForeColor.Name, "D3");
71                         Assert.AreEqual(FontStyle.Regular, Control.DefaultFont.Style, "D4");
72                         Assert.AreEqual (new Rectangle(0, 0, 0, 0), c.DisplayRectangle , "D5");
73                         Assert.AreEqual(false, c.Disposing, "D6");
74                         Assert.AreEqual(DockStyle.None, c.Dock, "D7");
75
76                         // E
77                         Assert.AreEqual(true, c.Enabled, "E1");
78
79                         // F
80                         Assert.AreEqual(false, c.Focused, "F1");
81                         Assert.AreEqual(FontStyle.Regular, c.Font.Style, "F2");
82                         Assert.AreEqual(SystemColors.ControlText, c.ForeColor, "F3");
83
84                         // G
85
86                         // H
87                         Assert.AreEqual (((IWin32Window)c).Handle, c.Handle, "H1");
88                         Assert.AreEqual(false, c.HasChildren, "H2");
89                         Assert.AreEqual(0, c.Height, "H3");
90
91                         // I
92                         Assert.AreEqual (ImeMode.NoControl, c.ImeMode, "I1");
93                         Assert.AreEqual(false, c.InvokeRequired, "I2");
94                         Assert.AreEqual(false, c.IsAccessible, "I3");
95                         Assert.AreEqual(false, c.IsDisposed, "I4");
96                         Assert.AreEqual(true, c.IsHandleCreated, "I5");
97
98                         // J
99
100                         // K
101
102                         // L
103                         Assert.AreEqual(0, c.Left, "L1");
104                         Assert.AreEqual(Point.Empty, c.Location, "L2");
105
106                         // M
107 #if NET_2_0
108                         Assert.IsTrue(c.MaximumSize.IsEmpty);
109                         Assert.IsTrue(c.MinimumSize.IsEmpty);
110 #endif
111                         Assert.AreEqual(Keys.None, Control.ModifierKeys, "M1");
112                         Assert.AreEqual(false, Control.MousePosition.IsEmpty, "M2");
113                         Assert.AreEqual(MouseButtons.None, Control.MouseButtons, "M3");
114
115                         // N
116                         Assert.AreEqual("", c.Name, "N1");
117
118                         // O
119
120                         // P
121                         Assert.AreEqual(null, c.Parent, "P1");
122                         name = c.ProductName;
123                         if (!name.Equals("Novell Mono MWF") && !name.Equals("Microsoft (R) .NET Framework"))
124                                 Assert.Fail("ProductName property does not match any accepted value - P2");
125
126                         name = c.ProductVersion;
127                         if (!name.Equals("1.1.4322.2032")) {
128                                 Assert.Fail("This test is being run against the wrong framework version.\nExpected is Net 1.1sp1. - P3");
129                         }
130
131                         // R
132                         Assert.AreEqual(false, c.RecreatingHandle, "R1");
133                         Assert.AreEqual(null, c.Region, "R2");
134                         Assert.AreEqual(0, c.Right, "R3");
135                         Assert.AreEqual(RightToLeft.No, c.RightToLeft, "R4");
136
137                         // S
138                         Assert.AreEqual(null, c.Site, "S1");
139                         Assert.AreEqual (new Size(0, 0), c.Size, "S2");
140
141                         // T
142                         Assert.AreEqual(0, c.TabIndex , "T1");
143                         Assert.AreEqual(true, c.TabStop, "T2");
144                         Assert.AreEqual(null, c.Tag, "T3");
145                         Assert.AreEqual("", c.Text, "T4");
146                         Assert.AreEqual(0, c.Top, "T5");
147                         Assert.AreEqual(null, c.TopLevelControl, "T6");
148
149                         // U
150
151                         // V
152                         Assert.AreEqual(true, c.Visible, "V1");
153
154                         // W
155                         Assert.AreEqual(0, c.Width, "W1");
156
157                         // XYZ
158                 }
159
160                 [Test]
161                 public void RelationTest() {
162                         Control c1;
163                         Control c2;
164
165                         c1 = new Control();
166                         c2 = new Control();
167
168                         Assert.AreEqual(true , c1.Visible , "Rel1");
169                         Assert.AreEqual(false, c1.Contains(c2) , "Rel2");
170                         Assert.AreEqual("System.Windows.Forms.Control", c1.ToString() , "Rel3");
171
172                         c1.Controls.Add(c2);
173                         Assert.AreEqual(true , c2.Visible , "Rel4");
174                         Assert.AreEqual(true, c1.Contains(c2) , "Rel5");
175
176                         c1.Anchor = AnchorStyles.Top;
177                         c1.SuspendLayout ();
178                         c1.Anchor = AnchorStyles.Left ;
179                         c1.ResumeLayout ();
180                         Assert.AreEqual(AnchorStyles.Left , c1.Anchor, "Rel6");
181
182                         c1.SetBounds(10, 20, 30, 40) ;
183                         Assert.AreEqual(new Rectangle(10, 20, 30, 40), c1.Bounds, "Rel7");
184
185                         Assert.AreEqual(c1, c2.Parent, "Rel8");
186                 }
187
188                 private string TestControl(Control container, Control start, bool forward) {
189                         Control ctl;
190
191                         ctl = container.GetNextControl(start, forward);
192
193                         if (ctl == null) {
194                                 return null;
195                         }
196
197                         return ctl.Text;
198                 }
199
200                 [Test]
201                 public void TabOrder() {
202                         Form            form;
203                         Control         active;
204
205                         Label           label1 = new Label();           // To test non-tabstop items as well
206                         Label           label2 = new Label();
207
208                         GroupBox        group1 = new GroupBox();
209                         GroupBox        group2 = new GroupBox();
210                         GroupBox        group3 = new GroupBox();
211
212                         TextBox         text1 = new TextBox();
213
214                         RadioButton     radio11 = new RadioButton();
215                         RadioButton     radio12 = new RadioButton();
216                         RadioButton     radio13 = new RadioButton();
217                         RadioButton     radio14 = new RadioButton();
218                         RadioButton     radio21 = new RadioButton();
219                         RadioButton     radio22 = new RadioButton();
220                         RadioButton     radio23 = new RadioButton();
221                         RadioButton     radio24 = new RadioButton();
222                         RadioButton     radio31 = new RadioButton();
223                         RadioButton     radio32 = new RadioButton();
224                         RadioButton     radio33 = new RadioButton();
225                         RadioButton     radio34 = new RadioButton();
226
227                         form = new Form();
228
229                         form.ClientSize = new System.Drawing.Size (520, 520);
230                         Assert.AreEqual(new Size(520, 520), form.ClientSize, "Tab1");
231
232                         form.Text = "SWF Taborder Test App Form";
233                         Assert.AreEqual("SWF Taborder Test App Form", form.Text, "Tab2");
234
235                         label1.Location = new Point(10, 10);
236                         Assert.AreEqual(new Point(10, 10), label1.Location, "Tab3");
237                         label1.Text = "Label1";
238                         form.Controls.Add(label1);
239
240                         label2.Location = new Point(200, 10);
241                         label2.Text = "Label2";
242                         form.Controls.Add(label2);
243
244                         group1.Text = "Group1";
245                         group2.Text = "Group2";
246                         group3.Text = "Group3";
247
248                         group1.Size = new Size(200, 400);
249                         group2.Size = new Size(200, 400);
250                         group3.Size = new Size(180, 180);
251                         Assert.AreEqual(new Size(180, 180), group3.Size, "Tab4");
252
253                         group1.Location = new Point(10, 40);
254                         group2.Location = new Point(220, 40);
255                         group3.Location = new Point(10, 210);
256
257                         group1.TabIndex = 30;
258                         Assert.AreEqual(30, group1.TabIndex, "Tab5");
259                         group1.TabStop = true;
260
261                         // Don't assign, test automatic assignment
262                         //group2.TabIndex = 0;
263                         group2.TabStop = true;
264                         Assert.AreEqual(0, group2.TabIndex, "Tab6");
265
266                         group3.TabIndex = 35;
267                         group3.TabStop = true;
268
269                         // Test default tab index
270                         Assert.AreEqual(0, radio11.TabIndex, "Tab7");
271
272                         text1.Text = "Edit Control";
273
274                         radio11.Text = "Radio 1-1 [Tab1]";
275                         radio12.Text = "Radio 1-2 [Tab2]";
276                         radio13.Text = "Radio 1-3 [Tab3]";
277                         radio14.Text = "Radio 1-4 [Tab4]";
278
279                         radio21.Text = "Radio 2-1 [Tab4]";
280                         radio22.Text = "Radio 2-2 [Tab3]";
281                         radio23.Text = "Radio 2-3 [Tab2]";
282                         radio24.Text = "Radio 2-4 [Tab1]";
283
284                         radio31.Text = "Radio 3-1 [Tab1]";
285                         radio32.Text = "Radio 3-2 [Tab3]";
286                         radio33.Text = "Radio 3-3 [Tab2]";
287                         radio34.Text = "Radio 3-4 [Tab4]";
288
289                         // We don't assign TabIndex for radio1X; test automatic assignment
290                         text1.TabStop = true;
291                         radio11.TabStop = true;
292
293                         radio21.TabIndex = 4;
294                         radio22.TabIndex = 3;
295                         radio23.TabIndex = 2;
296                         radio24.TabIndex = 1;
297                         radio24.TabStop = true;
298
299                         radio31.TabIndex = 11;
300                         radio31.TabStop = true;
301                         radio32.TabIndex = 13;
302                         radio33.TabIndex = 12;
303                         radio34.TabIndex = 14;
304
305                         text1.Location = new Point(10, 100);
306
307                         radio11.Location = new Point(10, 20);
308                         radio12.Location = new Point(10, 40);
309                         radio13.Location = new Point(10, 60);
310                         radio14.Location = new Point(10, 80);
311
312                         radio21.Location = new Point(10, 20);
313                         radio22.Location = new Point(10, 40);
314                         radio23.Location = new Point(10, 60);
315                         radio24.Location = new Point(10, 80);
316
317                         radio31.Location = new Point(10, 20);
318                         radio32.Location = new Point(10, 40);
319                         radio33.Location = new Point(10, 60);
320                         radio34.Location = new Point(10, 80);
321
322                         text1.Size = new Size(150, text1.PreferredHeight);
323
324                         radio11.Size = new Size(150, 20);
325                         radio12.Size = new Size(150, 20);
326                         radio13.Size = new Size(150, 20);
327                         radio14.Size = new Size(150, 20);
328
329                         radio21.Size = new Size(150, 20);
330                         radio22.Size = new Size(150, 20);
331                         radio23.Size = new Size(150, 20);
332                         radio24.Size = new Size(150, 20);
333
334                         radio31.Size = new Size(150, 20);
335                         radio32.Size = new Size(150, 20);
336                         radio33.Size = new Size(150, 20);
337                         radio34.Size = new Size(150, 20);
338
339                         group1.Controls.Add(text1);
340
341                         group1.Controls.Add(radio11);
342                         group1.Controls.Add(radio12);
343                         group1.Controls.Add(radio13);
344                         group1.Controls.Add(radio14);
345
346                         group2.Controls.Add(radio21);
347                         group2.Controls.Add(radio22);
348                         group2.Controls.Add(radio23);
349                         group2.Controls.Add(radio24);
350
351                         group3.Controls.Add(radio31);
352                         group3.Controls.Add(radio32);
353                         group3.Controls.Add(radio33);
354                         group3.Controls.Add(radio34);
355
356                         form.Controls.Add(group1);
357                         form.Controls.Add(group2);
358                         group2.Controls.Add(group3);
359
360                         // Perform some tests, the TabIndex stuff below will alter the outcome
361                         Assert.AreEqual(null, TestControl(group2, radio34, true), "Tab8");
362                         Assert.AreEqual(31, group2.TabIndex, "Tab9");
363
364                         // Does the taborder of containers and non-selectable things change behaviour?
365                         label1.TabIndex = 5;
366                         label2.TabIndex = 4;
367                         group1.TabIndex = 3;
368                         group2.TabIndex = 1;
369
370                         // Start verification
371                         Assert.AreEqual(null, TestControl(group2, radio34, true), "Tab10");
372                         Assert.AreEqual(radio24.Text, TestControl(group2, group2, true), "Tab11");
373                         Assert.AreEqual(radio31.Text, TestControl(group2, group3, true), "Tab12");
374                         Assert.AreEqual(null, TestControl(group1, radio14, true), "Tab13");
375                         Assert.AreEqual(radio23.Text, TestControl(group2, radio24, true), "Tab14");
376                         Assert.AreEqual(group3.Text, TestControl(group2, radio21, true), "Tab15");
377                         Assert.AreEqual(radio13.Text, TestControl(form, radio12, true), "Tab16");
378                         Assert.AreEqual(label2.Text, TestControl(form, radio14, true), "Tab17");
379                         Assert.AreEqual(group1.Text, TestControl(form, radio34, true), "Tab18");
380                         Assert.AreEqual(radio23.Text, TestControl(group2, radio24, true), "Tab19");
381
382                         // Sanity checks
383                         Assert.AreEqual(null, TestControl(radio11, radio21, true), "Tab20");
384                         Assert.AreEqual(text1.Text, TestControl(group1, radio21, true), "Tab21");
385
386                         Assert.AreEqual(radio14.Text, TestControl(form, label2, false), "Tab22");
387                         Assert.AreEqual(radio21.Text, TestControl(group2, group3, false), "Tab23");
388
389                         Assert.AreEqual(4, radio21.TabIndex, "Tab24");
390                         Assert.AreEqual(1, radio11.TabIndex, "Tab25");
391                         Assert.AreEqual(3, radio13.TabIndex, "Tab26");
392                         Assert.AreEqual(35, group3.TabIndex, "Tab27");
393                         Assert.AreEqual(1, group2.TabIndex, "Tab28");
394
395                         Assert.AreEqual(label1.Text, TestControl(form, form, false), "Tab29");
396                         Assert.AreEqual(radio14.Text, TestControl(group1, group1, false), "Tab30");
397                         Assert.AreEqual(radio34.Text, TestControl(group3, group3, false), "Tab31");
398
399                         Assert.AreEqual(null, TestControl(label1, label1, false), "Tab31");
400                         Assert.AreEqual(null, TestControl(radio11, radio21, false), "Tab32");
401                 }
402
403                 [Test]
404                 public void ScaleTest()
405                 {
406                         Control r1 = new Control();
407
408                         r1.Width = 40;
409                         r1.Height = 20;
410                         r1.Scale(2);
411                         Assert.AreEqual(80, r1.Width, "Scale1");
412                         Assert.AreEqual(40, r1.Height, "Scale2");
413                 }
414
415                 [Test]
416                 public void TextTest()
417                 {
418                         Control r1 = new Control();
419                         r1.Text = "Hi" ;
420                         Assert.AreEqual("Hi" , r1.Text , "Text1");
421
422                         r1.ResetText();
423                         Assert.AreEqual("" , r1.Text , "Text2");
424                 }
425
426                 [Test]
427                 public void PubMethodTest7()
428                 {
429                         Control r1 = new Control();
430                         r1.RightToLeft = RightToLeft.Yes ;
431                         r1.ResetRightToLeft() ;
432                         Assert.AreEqual(RightToLeft.No , r1.RightToLeft , "#81");
433                         r1.ImeMode = ImeMode.Off ;
434                         r1.ResetImeMode () ;
435                         Assert.AreEqual(ImeMode.NoControl , r1.ImeMode , "#82");
436                         r1.ForeColor= SystemColors.GrayText ;
437                         r1.ResetForeColor() ;
438                         Assert.AreEqual(SystemColors.ControlText , r1.ForeColor , "#83");
439                         //r1.Font = Font.FromHdc();
440                         r1.ResetFont () ;
441                         //Assert.AreEqual(FontFamily.GenericSansSerif , r1.Font , "#83");
442                         r1.Cursor = Cursors.Hand ;
443                         r1.ResetCursor () ;
444                         Assert.AreEqual(Cursors.Default , r1.Cursor , "#83");
445                         //r1.DataBindings = System.Windows.Forms.Binding ;
446                         //r1.ResetBindings() ;
447                         //Assert.AreEqual(ControlBindingsCollection , r1.DataBindings  , "#83");
448                         r1.BackColor = System.Drawing.Color.Black ;
449                         r1.ResetBackColor() ;
450                         Assert.AreEqual( SystemColors.Control , r1.BackColor  , "#84");
451                         r1.BackColor = System.Drawing.Color.Black ;
452                         r1.Refresh() ;
453                         Assert.AreEqual( null , r1.Region , "#85");
454                         Rectangle M = new Rectangle(10, 20, 30 ,40);
455                         r1.RectangleToScreen(M) ;
456                         Assert.AreEqual( null , r1.Region , "#86");
457
458                 }
459
460                 [Test]
461                 public void ScreenClientCoords()
462                 {
463                         Label l;
464                         Point p1;
465                         Point p2;
466                         Point p3;
467
468                         l = new Label();
469                         l.Left = 10;
470                         l.Top  = 12;
471                         l.Visible = true;
472                         p1 = new Point (10,10);
473                         p2 = l.PointToScreen(p1);
474                         p3 = l.PointToClient(p2);
475
476                         Assert.AreEqual (p1, p3, "SC1");
477                 }
478
479                 [Test]
480                 public void ContainsTest ()
481                 {
482                         Control t = new Control ();
483                         Control s = new Control ();
484
485                         t.Controls.Add (s);
486
487                         Assert.AreEqual (true, t.Contains (s), "Con1");
488                         Assert.AreEqual (false, s.Contains (t), "Con2");
489                         Assert.AreEqual (false, s.Contains (null), "Con3");
490                         Assert.AreEqual (false, t.Contains (new Control ()), "Con4");
491                 }
492
493                 [Test]
494                 public void CreateHandleTest ()
495                 {
496                         Control parent;
497                         Control child;
498
499                         parent = null;
500                         child = null;
501
502                         try {
503                                 parent = new Control ();
504                                 child = new Control ();
505
506                                 parent.Visible = true;
507                                 parent.Controls.Add (child);
508
509                                 Assert.IsFalse (parent.IsHandleCreated, "CH1");
510                                 Assert.IsFalse (child.IsHandleCreated, "CH2");
511
512                                 parent.CreateControl ();
513                                 Assert.IsNotNull (parent.Handle, "CH3");
514                                 Assert.IsNotNull (child.Handle, "CH4");
515                                 Assert.IsTrue (parent.IsHandleCreated, "CH5");
516                                 Assert.IsTrue (child.IsHandleCreated, "CH6");
517                         } finally {
518                                 if (parent != null)
519                                         parent.Dispose ();
520                                 if (child != null)
521                                         child.Dispose ();
522                         }
523
524                         // Accessing Handle Property creates the handle
525                         try {
526                                 parent = new Control ();
527                                 parent.Visible = true;
528                                 child = new Control ();
529                                 parent.Controls.Add (child);
530                                 Assert.IsFalse (parent.IsHandleCreated, "CH7");
531                                 Assert.IsFalse (child.IsHandleCreated, "CH8");
532                                 Assert.IsNotNull (parent.Handle, "CH9");
533                                 Assert.IsTrue (parent.IsHandleCreated, "CH10");
534                                 Assert.IsTrue (child.IsHandleCreated, "CH11");
535                         } finally {
536                                 if (parent != null)
537                                         parent.Dispose ();
538                                 if (child != null)
539                                         child.Dispose ();
540                         }
541                 }
542
543                 [Test]
544                 public void CreateGraphicsTest ()
545                 {
546                         Graphics g = null;
547                         Pen p = null;
548
549                         try {
550                                 Control c = new Control ();
551                                 c.SetBounds (0,0, 20, 20);
552                                 g = c.CreateGraphics ();
553                                 Assert.IsNotNull (g, "Graph1");
554                         } finally {
555                                 if (p != null)
556                                         p.Dispose ();
557                                 if (g != null)
558                                         g.Dispose ();
559                         }
560                 }
561
562                 bool delegateCalled = false;
563                 public delegate void TestDelegate ();
564
565                 [Test]
566                 [ExpectedException(typeof(System.InvalidOperationException))]
567                 public void InvokeException1 () {
568                         Control c = new Control ();
569                         IAsyncResult result;
570
571                         result = c.BeginInvoke (new TestDelegate (delegate_call));
572                         c.EndInvoke (result);
573                 }
574
575                 [Test]
576                 [Ignore("Seems to hang")]
577                 public void InvokeTest () {
578                         Control c = null;
579
580                         try {
581                                 c = new Control ();
582                                 IAsyncResult result;
583
584                                 c.CreateControl ();
585                                 result = c.BeginInvoke (new TestDelegate (delegate_call));
586                                 c.EndInvoke (result);
587                                 Assert.AreEqual (true, delegateCalled, "Invoke1");
588                         } finally {
589                                 if (c != null)
590                                         c.Dispose ();
591                         }
592                 }
593
594                 public void delegate_call () {
595                         delegateCalled = true;
596                 }
597
598                 [Test]
599                 public void FindFormTest () {
600                         Form f = new Form ();
601
602                         f.Name = "form";
603                         Control c = null;
604
605                         try {
606                                 f.Controls.Add (c = new Control ());
607                                 Assert.AreEqual (f.Name, c.FindForm ().Name, "Find1");
608
609                                 f.Controls.Remove (c);
610
611                                 GroupBox g = new GroupBox ();
612                                 g.Name = "box";
613                                 f.Controls.Add (g);
614                                 g.Controls.Add (c);
615
616                                 Assert.AreEqual (f.Name, f.FindForm ().Name, "Find2");
617
618                                 g.Controls.Remove (c);
619                                 Assert.IsNull(c.FindForm (), "Find3");
620
621                         } finally {
622                                 if (c != null)
623                                         c.Dispose ();
624                                 if (f != null)
625                                         f.Dispose ();
626                         }
627                 }
628
629                 [Test]
630                 public void FocusTest ()
631                 {
632                         Form f = null;
633                         Button c = null, d = null;
634
635                         try {
636                                 f = new Form ();
637                                 f.Visible = true;
638                                 c = new Button ();
639                                 c.Visible = true;
640                                 f.Controls.Add (c);
641
642                                 d = new Button ();
643                                 d.Visible = false;
644                                 f.Controls.Add (d);
645
646                                 Assert.IsTrue (c.CanFocus, "Focus1");
647                                 Assert.IsFalse (c.Focused, "Focus2");
648                                 c.Focus ();
649                                 Assert.IsTrue (c.Focused, "Focus3");
650                                 d.Focus ();
651                                 Assert.IsFalse (d.Focused, "Focus4");
652
653                                 d.Visible = true;
654                                 d.Focus ();
655                                 Assert.IsTrue (d.Focused, "Focus5");
656                                 Assert.IsFalse (c.Focused, "Focus6");
657
658                                 c.Enabled = false;
659                                 Assert.IsFalse (c.Focused, "Focus7");
660                         } finally {
661                                 if (f != null)
662                                         f.Dispose ();
663                                 if (c != null)
664                                         c.Dispose ();
665                                 if (d != null)
666                                         d.Dispose ();
667                         }
668                 }
669
670                 [Test]
671                 public void FromHandleTest ()
672                 {
673                         Control c1 = null;
674                         Control c2 = null;
675
676                         try {
677                                 c1 = new Control ();
678                                 c2 = new Control ();
679
680                                 c1.Name = "parent";
681                                 c2.Name = "child";
682                                 c1.Controls.Add(c2);
683
684                                 // Handle
685                                 Assert.AreEqual (c1.Name, Control.FromHandle (c1.Handle).Name, "Handle1");
686                                 Assert.IsNull (Control.FromHandle (IntPtr.Zero), "Handle2");
687
688                                 // ChildHandle
689                                 Assert.AreEqual (c1.Name, Control.FromChildHandle (c1.Handle).Name, "Handle3");
690                                 Assert.IsNull (Control.FromChildHandle (IntPtr.Zero), "Handle4");
691
692
693                         } finally {
694                                 if (c1 != null)
695                                         c1.Dispose ();
696
697                                 if (c2 != null)
698                                         c2.Dispose ();
699                         }
700                 }
701
702                 [Test]
703                 public void GetChildAtPointTest ()
704                 {
705                         Control c = null, d = null, e = null;
706
707                         try {
708                                 c = new Control ();
709                                 c.Name = "c1";
710                                 c.SetBounds (0, 0, 100, 100);
711
712                                 d = new Control ();
713                                 d.Name = "d1";
714                                 d.SetBounds (10, 10, 40, 40);
715                                 c.Controls.Add (d);
716
717                                 e = new Control ();
718                                 e.Name = "e1";
719                                 e.SetBounds (55, 55, 10, 10);
720
721                                 Control l = c.GetChildAtPoint (new Point (15, 15));
722                                 Assert.AreEqual (d.Name, l.Name, "Child1");
723                                 Assert.IsFalse (e.Name == l.Name, "Child2");
724
725                                 l = c.GetChildAtPoint (new Point (57, 57));
726                                 Assert.IsNull (l, "Child3");
727
728                                 l = c.GetChildAtPoint (new Point (10, 10));
729                                 Assert.AreEqual (d.Name, l.Name, "Child4");
730
731                                 // GetChildAtPointSkip is not implemented and the following test is breaking for Net_2_0 profile
732 //                              #if NET_2_0
733 //                                      c.Controls.Add (e);
734 //                                      e.Visible = false;
735 //                                      l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Invisible);
736 //                                      Assert.IsNull (l, "Child5");
737
738 //                                      e.Visible = true;
739 //                                      l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Invisible);
740 //                                      Assert.AreSame (e.Name, l.Name, "Child6");
741 //                              #endif // NET_2_0                 
742                         } finally {
743                                 if (c != null)
744                                         c.Dispose ();
745                                 if (d != null)
746                                         d.Dispose ();
747                         }
748                 }
749
750                 
751                 public class LayoutTestControl : Control {
752                         public int LayoutCount;
753
754                         public LayoutTestControl () : base() {
755                                 LayoutCount = 0;
756                         }
757
758                         protected override void OnLayout(LayoutEventArgs levent) {
759                                 LayoutCount++;
760                                 base.OnLayout (levent);
761                         }
762                 }
763
764                 [Test]
765                 public void LayoutTest() {
766                         LayoutTestControl c;
767
768                         c = new LayoutTestControl();
769
770                         c.SuspendLayout();
771                         c.SuspendLayout();
772                         c.SuspendLayout();
773                         c.SuspendLayout();
774
775                         c.ResumeLayout(true);
776                         c.PerformLayout();
777                         c.ResumeLayout(true);
778                         c.PerformLayout();
779                         c.ResumeLayout(true);
780                         c.PerformLayout();
781                         c.ResumeLayout(true);
782                         c.PerformLayout();
783                         c.ResumeLayout(true);
784                         c.PerformLayout();
785                         c.ResumeLayout(true);
786                         c.PerformLayout();
787                         c.ResumeLayout(true);
788                         c.PerformLayout();
789                         c.SuspendLayout();
790                         c.PerformLayout();
791
792                         Assert.AreEqual(5, c.LayoutCount, "Layout Suspend/Resume locking does not bottom out at 0");
793                 }
794
795                 [Test]
796                 [ExpectedException(typeof(System.ArgumentException))]
797                 public void TransparentBackgroundTest1() {
798                         Control c;
799
800                         c = new Control();
801                         c.BackColor = Color.Transparent;
802                 }
803
804                 [Test]
805                 public void TransparentBackgroundTest2() {
806                         Panel   c;
807
808                         c = new Panel();
809                         c.BackColor = Color.Transparent;
810                         Assert.AreEqual(Color.Transparent, c.BackColor, "Transparent background not set");
811                 }
812
813                 [Test]
814                 public void TransparentBackgroundTest3() {
815                         Control c;
816
817                         c = new Control();
818                         c.BackColor = Color.Empty;
819                         Assert.AreEqual(Control.DefaultBackColor, c.BackColor, "Setting empty color failed");
820                 }
821
822                 [Test]
823                 public void EnabledTest1() {
824                         Control child;
825                         Control parent;
826                         Control grandma;
827
828                         grandma = new Control();
829                         parent = new Control();
830                         child = new Control();
831
832                         grandma.Controls.Add(parent);
833                         parent.Controls.Add(child);
834                         grandma.Enabled = false;
835                         Assert.AreEqual(grandma.Enabled, child.Enabled, "Child did not inherit disabled state");
836                 }
837
838                 int EnabledCalledCount = 0;
839                 private void EnabledTest2EnabledChanged(object sender, EventArgs e) {
840                         EnabledCalledCount++;
841                 }
842
843                 [Test]
844                 public void EnabledTest2() {
845                         // Check nesting of enabled calls
846                         // OnEnabled is not called for disabled child controls
847                         Control child;
848                         Control parent;
849                         Control grandma;
850
851                         EnabledCalledCount = 0;
852
853                         grandma = new Control();
854                         parent = new Control();
855                         child = new Control();
856                         child.EnabledChanged += new EventHandler(EnabledTest2EnabledChanged);
857
858                         grandma.Controls.Add(parent);
859                         parent.Controls.Add(child);
860                         grandma.Enabled = false;
861
862                         Assert.AreEqual(1, EnabledCalledCount, "Child Enabled Event not properly fired");
863                         grandma.Enabled = true;
864                         Assert.AreEqual(2, EnabledCalledCount, "Child Enabled Event not properly fired");
865                         child.Enabled = false;
866                         grandma.Enabled = false;
867                         Assert.AreEqual(3, EnabledCalledCount, "Child Enabled Event not properly fired");
868                 }
869         }
870 }