2006-12-19 Daniel Nauck <dna@mono-project.de>
[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.Collections;
10 using InvalidEnumArgumentException = System.ComponentModel.InvalidEnumArgumentException;
11 using System.Drawing;
12 using System.Reflection;
13 using System.Runtime.Remoting;
14 using System.Threading;
15 using System.Windows.Forms;
16
17 using NUnit.Framework;
18
19 namespace MonoTests.System.Windows.Forms
20 {
21         [TestFixture]
22         public class ControlTest
23         {
24                 class Helper {
25                         public static void TestAccessibility(Control c, string Default, string Description, string Name, AccessibleRole Role)
26                         {
27                                 Assert.IsNotNull (c.AccessibilityObject, "Acc1");
28                                 Assert.AreEqual (Default, c.AccessibleDefaultActionDescription, "Acc2");
29                                 Assert.AreEqual (Description, c.AccessibleDescription, "Acc3");
30                                 Assert.AreEqual (Name, c.AccessibleName, "Acc4");
31                                 Assert.AreEqual (Role, c.AccessibleRole, "Acc5");
32                         }
33
34                         public static string TestControl(Control container, Control start, bool forward) {
35                                 Control ctl;
36
37                                 ctl = container.GetNextControl(start, forward);
38
39                                 if (ctl == null) {
40                                         return null;
41                                 }
42
43                                 return ctl.Text;
44                         }
45                 }
46
47                 [Test]
48                 public void CreatedTest ()
49                 {
50                         Control c = new Control ();
51                         Assert.IsFalse (c.Created, "A1");
52                 }
53
54                 [Test]
55                 [Category ("NotWorking")]
56                 public void CreatedAccessibilityTest ()
57                 {
58                         Control c = new Control ();
59                         Assert.IsFalse (c.Created, "A1");
60
61                         Helper.TestAccessibility(c, null, null, null, AccessibleRole.Default);
62
63                         Assert.IsTrue (c.Created, "A2");
64
65                         c.Dispose ();
66
67                         Assert.IsFalse (c.Created, "A3");
68                 }
69
70                 [Test]
71                 [Category ("NotWorking")]
72                 public void BoundsTest ()
73                 {
74                         Control c = new Control ();
75                         Assert.IsTrue (c.Bounds.IsEmpty, "A1");
76                         Assert.IsTrue (c.Size.IsEmpty, "A2");
77                         Assert.IsTrue (c.ClientSize.IsEmpty, "A3");
78                         Assert.IsTrue (c.ClientRectangle.IsEmpty, "A4");
79
80                         Assert.AreEqual (((IWin32Window)c).Handle, c.Handle, "A5");
81
82                         /* this part fails on linux because we can't allocate X windows which are 0x0,
83                            and the Control bounds directly reflect the size of the X window */
84
85                         Assert.IsTrue (c.Bounds.IsEmpty, "A6");
86                         Assert.IsTrue (c.Size.IsEmpty, "A7");
87                         Assert.IsTrue (c.ClientSize.IsEmpty, "A8");
88                         Assert.IsTrue (c.ClientRectangle.IsEmpty, "A9");
89                 }
90
91                 [Test]
92                 public void PubPropTest()
93                 {
94                         Control c = new Control();
95
96                         Assert.IsFalse (c.AllowDrop , "A1");
97                         Assert.AreEqual(AnchorStyles.Top | AnchorStyles.Left, c.Anchor, "A2");
98
99                         Assert.AreEqual ("Control", c.BackColor.Name , "B1");
100                         Assert.IsNull (c.BackgroundImage, "B2");
101                         Assert.IsNull (c.BindingContext, "B3");
102
103                         Assert.IsFalse (c.CanFocus, "C1");
104                         Assert.IsTrue (c.CanSelect, "C2");
105                         Assert.IsFalse (c.Capture, "C3");
106                         Assert.IsTrue (c.CausesValidation, "C4");
107
108                         Assert.IsNotNull (c.CompanyName, "C7");
109                         Assert.IsNull (c.Container, "C8");
110                         Assert.IsFalse (c.ContainsFocus, "C9");
111                         Assert.IsNull (c.ContextMenu, "C10");
112                         Assert.AreEqual (0, c.Controls.Count, "C11");
113                         Assert.IsFalse (c.Created, "C12");
114                         Assert.AreEqual (Cursors.Default, c.Cursor, "C13");
115
116                         Assert.IsNotNull(c.DataBindings, "D1");
117                         Assert.AreEqual("Control", Control.DefaultBackColor.Name, "D2");
118                         Assert.AreEqual("ControlText", Control.DefaultForeColor.Name, "D3");
119                         Assert.AreEqual(FontStyle.Regular, Control.DefaultFont.Style, "D4");
120                         Assert.AreEqual (new Rectangle(0, 0, 0, 0), c.DisplayRectangle , "D5");
121                         Assert.IsFalse (c.Disposing, "D6");
122                         Assert.AreEqual(DockStyle.None, c.Dock, "D7");
123
124                         Assert.IsTrue (c.Enabled, "E1");
125
126                         Assert.IsFalse  (c.Focused, "F1");
127                         Assert.AreEqual (FontStyle.Regular, c.Font.Style, "F2");
128                         Assert.AreEqual (SystemColors.ControlText, c.ForeColor, "F3");
129
130                         Assert.IsFalse  (c.HasChildren, "H2");
131
132                         Assert.AreEqual (ImeMode.NoControl, c.ImeMode, "I1");
133                         Assert.IsFalse (c.InvokeRequired, "I2");
134                         Assert.IsFalse (c.IsAccessible, "I3");
135                         Assert.IsFalse (c.IsDisposed, "I4");
136                         Assert.IsFalse (c.IsHandleCreated, "I5");
137
138                         Assert.AreEqual(Point.Empty, c.Location, "L2");
139
140 #if NET_2_0
141                         Assert.IsTrue(c.MaximumSize.IsEmpty);
142                         Assert.IsTrue(c.MinimumSize.IsEmpty);
143 #endif
144                         Assert.AreEqual (Keys.None, Control.ModifierKeys, "M1");
145                         Assert.IsFalse (Control.MousePosition.IsEmpty, "M2");
146                         Assert.AreEqual (MouseButtons.None, Control.MouseButtons, "M3");
147
148                         Assert.AreEqual("", c.Name, "N1");
149                         c.Name = "Control Name";
150                         Assert.AreEqual("Control Name", c.Name, "N2");
151
152                         Assert.IsNull (c.Parent, "P1");
153                         Assert.IsNotNull (c.ProductName, "P2");
154                         Assert.IsTrue (c.ProductName != "", "P3");
155                         Assert.IsNotNull (c.ProductVersion, "P4");
156                         Assert.IsTrue (c.ProductVersion != "", "P5");
157
158                         Assert.IsFalse (c.RecreatingHandle, "R1");
159                         Assert.IsNull (c.Region, "R2");
160                         Assert.AreEqual (RightToLeft.No, c.RightToLeft, "R4");
161
162                         Assert.IsNull (c.Site, "S1");
163
164                         Assert.AreEqual (0, c.TabIndex , "T1");
165                         Assert.IsTrue (c.TabStop, "T2");
166                         Assert.IsNull (c.Tag, "T3");
167                         Assert.AreEqual ("", c.Text, "T4");
168
169                         Assert.IsTrue (c.Visible, "V1");
170                 }
171
172                 [Test]
173                 public void SizeChangeTest ()
174                 {
175                         Form f = new Form ();
176                         Control c = new Control ();
177                         f.Controls.Add(c);
178                         f.Show();
179                         c.Resize += new EventHandler(SizeChangedTest_ResizeHandler);
180                         c.Tag = true;
181                         c.Size = c.Size;
182                         Assert.AreEqual (true, (bool) c.Tag, "#1");
183                         f.Close ();
184                 }
185
186                 private void SizeChangedTest_ResizeHandler (object sender, EventArgs e)
187                 {
188                         ((Control) sender).Tag = false;
189                 }
190
191                 [Test]
192                 public void NegativeHeightTest ()
193                 {
194                         Control c = new Control ();
195                         IntPtr handle = c.Handle;
196                         c.Resize += new EventHandler(NegativeHeightTest_ResizeHandler);
197                         c.Tag = -2;
198                         c.Height = 2;
199                         c.Height = -2;
200                         Assert.AreEqual (0, (int) c.Tag, "#1");
201                         c.Dispose ();
202                         Assert.AreEqual (handle, handle, "Removes warning.");
203                 }
204                 
205                 private void NegativeHeightTest_ResizeHandler (object sender, EventArgs e)
206                 {
207                         Control c = (Control) sender;
208                         c.Tag = c.Height;
209                 }
210                 
211                 [Test]
212                 public void TopLevelControlTest () {
213                         Control c = new Control ();
214
215                         Assert.AreEqual(null, c.TopLevelControl, "T1");
216
217                         Panel p = new Panel ();
218
219                         p.Controls.Add (c);
220
221                         Assert.AreEqual(null, c.TopLevelControl, "T2");
222
223                         Form f = new Form ();
224                         f.ShowInTaskbar = false;
225
226                         f.Controls.Add (p);
227
228                         Assert.AreEqual (f, c.TopLevelControl, "T3");
229                         Assert.AreEqual (f, f.TopLevelControl, "T4");
230                 }
231
232                 [Test]
233                 public void RelationTest() {
234                         Control c1;
235                         Control c2;
236
237                         c1 = new Control();
238                         c2 = new Control();
239
240                         Assert.AreEqual(true , c1.Visible , "Rel1");
241                         Assert.AreEqual(false, c1.Contains(c2) , "Rel2");
242                         Assert.AreEqual("System.Windows.Forms.Control", c1.ToString() , "Rel3");
243
244                         c1.Controls.Add(c2);
245                         Assert.AreEqual(true , c2.Visible , "Rel4");
246                         Assert.AreEqual(true, c1.Contains(c2) , "Rel5");
247
248                         c1.Anchor = AnchorStyles.Top;
249                         c1.SuspendLayout ();
250                         c1.Anchor = AnchorStyles.Left ;
251                         c1.ResumeLayout ();
252                         Assert.AreEqual(AnchorStyles.Left , c1.Anchor, "Rel6");
253
254                         c1.SetBounds(10, 20, 30, 40) ;
255                         Assert.AreEqual(new Rectangle(10, 20, 30, 40), c1.Bounds, "Rel7");
256
257                         Assert.AreEqual(c1, c2.Parent, "Rel8");
258                 }
259
260                 [Test]
261                 [Category ("NotWorking")]
262                 public void TabOrder() {
263                         Form            form;
264                         Control         active;
265
266                         Label           label1 = new Label();           // To test non-tabstop items as well
267                         Label           label2 = new Label();
268
269                         GroupBox        group1 = new GroupBox();
270                         GroupBox        group2 = new GroupBox();
271                         GroupBox        group3 = new GroupBox();
272
273                         TextBox         text1 = new TextBox();
274
275                         RadioButton     radio11 = new RadioButton();
276                         RadioButton     radio12 = new RadioButton();
277                         RadioButton     radio13 = new RadioButton();
278                         RadioButton     radio14 = new RadioButton();
279                         RadioButton     radio21 = new RadioButton();
280                         RadioButton     radio22 = new RadioButton();
281                         RadioButton     radio23 = new RadioButton();
282                         RadioButton     radio24 = new RadioButton();
283                         RadioButton     radio31 = new RadioButton();
284                         RadioButton     radio32 = new RadioButton();
285                         RadioButton     radio33 = new RadioButton();
286                         RadioButton     radio34 = new RadioButton();
287
288                         form = new Form();
289                         form.ShowInTaskbar = false;
290
291                         form.ClientSize = new Size (520, 520);
292                         Assert.AreEqual(new Size(520, 520), form.ClientSize, "Tab1");
293
294                         form.Text = "SWF Taborder Test App Form";
295                         Assert.AreEqual("SWF Taborder Test App Form", form.Text, "Tab2");
296
297                         label1.Location = new Point(10, 10);
298                         Assert.AreEqual(new Point(10, 10), label1.Location, "Tab3");
299                         label1.Text = "Label1";
300                         form.Controls.Add(label1);
301
302                         label2.Location = new Point(200, 10);
303                         label2.Text = "Label2";
304                         form.Controls.Add(label2);
305
306                         group1.Text = "Group1";
307                         group2.Text = "Group2";
308                         group3.Text = "Group3";
309
310                         group1.Size = new Size(200, 400);
311                         group2.Size = new Size(200, 400);
312                         group3.Size = new Size(180, 180);
313                         Assert.AreEqual(new Size(180, 180), group3.Size, "Tab4");
314
315                         group1.Location = new Point(10, 40);
316                         group2.Location = new Point(220, 40);
317                         group3.Location = new Point(10, 210);
318
319                         group1.TabIndex = 30;
320                         Assert.AreEqual(30, group1.TabIndex, "Tab5");
321                         group1.TabStop = true;
322
323                         // Don't assign, test automatic assignment
324                         //group2.TabIndex = 0;
325                         group2.TabStop = true;
326                         Assert.AreEqual(0, group2.TabIndex, "Tab6");
327
328                         group3.TabIndex = 35;
329                         group3.TabStop = true;
330
331                         // Test default tab index
332                         Assert.AreEqual(0, radio11.TabIndex, "Tab7");
333
334                         text1.Text = "Edit Control";
335
336                         radio11.Text = "Radio 1-1 [Tab1]";
337                         radio12.Text = "Radio 1-2 [Tab2]";
338                         radio13.Text = "Radio 1-3 [Tab3]";
339                         radio14.Text = "Radio 1-4 [Tab4]";
340
341                         radio21.Text = "Radio 2-1 [Tab4]";
342                         radio22.Text = "Radio 2-2 [Tab3]";
343                         radio23.Text = "Radio 2-3 [Tab2]";
344                         radio24.Text = "Radio 2-4 [Tab1]";
345
346                         radio31.Text = "Radio 3-1 [Tab1]";
347                         radio32.Text = "Radio 3-2 [Tab3]";
348                         radio33.Text = "Radio 3-3 [Tab2]";
349                         radio34.Text = "Radio 3-4 [Tab4]";
350
351                         // We don't assign TabIndex for radio1X; test automatic assignment
352                         text1.TabStop = true;
353                         radio11.TabStop = true;
354
355                         radio21.TabIndex = 4;
356                         radio22.TabIndex = 3;
357                         radio23.TabIndex = 2;
358                         radio24.TabIndex = 1;
359                         radio24.TabStop = true;
360
361                         radio31.TabIndex = 11;
362                         radio31.TabStop = true;
363                         radio32.TabIndex = 13;
364                         radio33.TabIndex = 12;
365                         radio34.TabIndex = 14;
366
367                         text1.Location = new Point(10, 100);
368
369                         radio11.Location = new Point(10, 20);
370                         radio12.Location = new Point(10, 40);
371                         radio13.Location = new Point(10, 60);
372                         radio14.Location = new Point(10, 80);
373
374                         radio21.Location = new Point(10, 20);
375                         radio22.Location = new Point(10, 40);
376                         radio23.Location = new Point(10, 60);
377                         radio24.Location = new Point(10, 80);
378
379                         radio31.Location = new Point(10, 20);
380                         radio32.Location = new Point(10, 40);
381                         radio33.Location = new Point(10, 60);
382                         radio34.Location = new Point(10, 80);
383
384                         text1.Size = new Size(150, text1.PreferredHeight);
385
386                         radio11.Size = new Size(150, 20);
387                         radio12.Size = new Size(150, 20);
388                         radio13.Size = new Size(150, 20);
389                         radio14.Size = new Size(150, 20);
390
391                         radio21.Size = new Size(150, 20);
392                         radio22.Size = new Size(150, 20);
393                         radio23.Size = new Size(150, 20);
394                         radio24.Size = new Size(150, 20);
395
396                         radio31.Size = new Size(150, 20);
397                         radio32.Size = new Size(150, 20);
398                         radio33.Size = new Size(150, 20);
399                         radio34.Size = new Size(150, 20);
400
401                         group1.Controls.Add(text1);
402
403                         group1.Controls.Add(radio11);
404                         group1.Controls.Add(radio12);
405                         group1.Controls.Add(radio13);
406                         group1.Controls.Add(radio14);
407
408                         group2.Controls.Add(radio21);
409                         group2.Controls.Add(radio22);
410                         group2.Controls.Add(radio23);
411                         group2.Controls.Add(radio24);
412
413                         group3.Controls.Add(radio31);
414                         group3.Controls.Add(radio32);
415                         group3.Controls.Add(radio33);
416                         group3.Controls.Add(radio34);
417
418                         form.Controls.Add(group1);
419                         form.Controls.Add(group2);
420                         group2.Controls.Add(group3);
421
422                         // Perform some tests, the TabIndex stuff below will alter the outcome
423                         Assert.AreEqual(null, Helper.TestControl(group2, radio34, true), "Tab8");
424                         Assert.AreEqual(31, group2.TabIndex, "Tab9");
425
426                         // Does the taborder of containers and non-selectable things change behaviour?
427                         label1.TabIndex = 5;
428                         label2.TabIndex = 4;
429                         group1.TabIndex = 3;
430                         group2.TabIndex = 1;
431
432                         // Start verification
433                         Assert.AreEqual(null, Helper.TestControl(group2, radio34, true), "Tab10");
434                         Assert.AreEqual(radio24.Text, Helper.TestControl(group2, group2, true), "Tab11");
435                         Assert.AreEqual(radio31.Text, Helper.TestControl(group2, group3, true), "Tab12");
436                         Assert.AreEqual(null, Helper.TestControl(group1, radio14, true), "Tab13");
437                         Assert.AreEqual(radio23.Text, Helper.TestControl(group2, radio24, true), "Tab14");
438                         Assert.AreEqual(group3.Text, Helper.TestControl(group2, radio21, true), "Tab15");
439                         Assert.AreEqual(radio13.Text, Helper.TestControl(form, radio12, true), "Tab16");
440                         Assert.AreEqual(label2.Text, Helper.TestControl(form, radio14, true), "Tab17");
441                         Assert.AreEqual(group1.Text, Helper.TestControl(form, radio34, true), "Tab18");
442                         Assert.AreEqual(radio23.Text, Helper.TestControl(group2, radio24, true), "Tab19");
443
444                         // Sanity checks
445                         Assert.AreEqual(null, Helper.TestControl(radio11, radio21, true), "Tab20");
446                         Assert.AreEqual(text1.Text, Helper.TestControl(group1, radio21, true), "Tab21");
447
448                         Assert.AreEqual(radio14.Text, Helper.TestControl(form, label2, false), "Tab22");
449                         Assert.AreEqual(radio21.Text, Helper.TestControl(group2, group3, false), "Tab23");
450
451                         Assert.AreEqual(4, radio21.TabIndex, "Tab24");
452                         Assert.AreEqual(1, radio11.TabIndex, "Tab25");
453                         Assert.AreEqual(3, radio13.TabIndex, "Tab26");
454                         Assert.AreEqual(35, group3.TabIndex, "Tab27");
455                         Assert.AreEqual(1, group2.TabIndex, "Tab28");
456
457                         Assert.AreEqual(label1.Text, Helper.TestControl(form, form, false), "Tab29");
458                         Assert.AreEqual(radio14.Text, Helper.TestControl(group1, group1, false), "Tab30");
459                         Assert.AreEqual(radio34.Text, Helper.TestControl(group3, group3, false), "Tab31");
460
461                         Assert.AreEqual(null, Helper.TestControl(label1, label1, false), "Tab31");
462                         Assert.AreEqual(null, Helper.TestControl(radio11, radio21, false), "Tab32");
463                         form.Dispose ();
464                 }
465
466                 [Test]
467                 public void ScaleTest()
468                 {
469                         Control r1 = new Control();
470
471                         r1.Width = 40;
472                         r1.Height = 20;
473                         r1.Scale(2);
474                         Assert.AreEqual(80, r1.Width, "Scale1");
475                         Assert.AreEqual(40, r1.Height, "Scale2");
476                 }
477
478                 [Test]
479                 public void TextTest()
480                 {
481                         Control r1 = new Control();
482                         r1.Text = "Hi" ;
483                         Assert.AreEqual("Hi" , r1.Text , "Text1");
484
485                         r1.ResetText();
486                         Assert.AreEqual("" , r1.Text , "Text2");
487                 }
488
489                 [Test]
490                 public void PubMethodTest7()
491                 {
492                         Control r1 = new Control();
493                         r1.RightToLeft = RightToLeft.Yes ;
494                         r1.ResetRightToLeft() ;
495                         Assert.AreEqual(RightToLeft.No , r1.RightToLeft , "#81");
496                         r1.ImeMode = ImeMode.Off ;
497                         r1.ResetImeMode () ;
498                         Assert.AreEqual(ImeMode.NoControl , r1.ImeMode , "#82");
499                         r1.ForeColor= SystemColors.GrayText ;
500                         r1.ResetForeColor() ;
501                         Assert.AreEqual(SystemColors.ControlText , r1.ForeColor , "#83");
502                         //r1.Font = Font.FromHdc();
503                         r1.ResetFont () ;
504                         //Assert.AreEqual(FontFamily.GenericSansSerif , r1.Font , "#83");
505                         r1.Cursor = Cursors.Hand ;
506                         r1.ResetCursor () ;
507                         Assert.AreEqual(Cursors.Default , r1.Cursor , "#83");
508                         //r1.DataBindings = System.Windows.Forms.Binding ;
509                         //r1.ResetBindings() ;
510                         //Assert.AreEqual(ControlBindingsCollection , r1.DataBindings  , "#83");
511                         r1.BackColor = Color.Black ;
512                         r1.ResetBackColor() ;
513                         Assert.AreEqual( SystemColors.Control , r1.BackColor  , "#84");
514                         r1.BackColor = Color.Black ;
515                         r1.Refresh() ;
516                         Assert.AreEqual( null , r1.Region , "#85");
517                         Rectangle M = new Rectangle(10, 20, 30 ,40);
518                         r1.RectangleToScreen(M) ;
519                         Assert.AreEqual( null , r1.Region , "#86");
520
521                 }
522
523                 [Test]
524                 public void ScreenClientCoords()
525                 {
526                         Label l;
527                         Point p1;
528                         Point p2;
529                         Point p3;
530
531                         l = new Label();
532                         l.Left = 10;
533                         l.Top  = 12;
534                         l.Visible = true;
535                         p1 = new Point (10,10);
536                         p2 = l.PointToScreen(p1);
537                         p3 = l.PointToClient(p2);
538
539                         Assert.AreEqual (p1, p3, "SC1");
540                 }
541
542                 [Test]
543                 public void ContainsTest ()
544                 {
545                         Control t = new Control ();
546                         Control s = new Control ();
547
548                         t.Controls.Add (s);
549
550                         Assert.AreEqual (true, t.Contains (s), "Con1");
551                         Assert.AreEqual (false, s.Contains (t), "Con2");
552                         Assert.AreEqual (false, s.Contains (null), "Con3");
553                         Assert.AreEqual (false, t.Contains (new Control ()), "Con4");
554                 }
555
556                 [Test]
557                 public void CreateHandleTest ()
558                 {
559                         Control parent;
560                         Control child;
561
562                         parent = null;
563                         child = null;
564
565                         try {
566                                 parent = new Control ();
567                                 child = new Control ();
568
569                                 parent.Visible = true;
570                                 parent.Controls.Add (child);
571
572                                 Assert.IsFalse (parent.IsHandleCreated, "CH1");
573                                 Assert.IsFalse (child.IsHandleCreated, "CH2");
574
575                                 parent.CreateControl ();
576                                 Assert.IsNotNull (parent.Handle, "CH3");
577                                 Assert.IsNotNull (child.Handle, "CH4");
578                                 Assert.IsTrue (parent.IsHandleCreated, "CH5");
579                                 Assert.IsTrue (child.IsHandleCreated, "CH6");
580                         } finally {
581                                 if (parent != null)
582                                         parent.Dispose ();
583                                 if (child != null)
584                                         child.Dispose ();
585                         }
586
587                         // Accessing Handle Property creates the handle
588                         try {
589                                 parent = new Control ();
590                                 parent.Visible = true;
591                                 child = new Control ();
592                                 parent.Controls.Add (child);
593                                 Assert.IsFalse (parent.IsHandleCreated, "CH7");
594                                 Assert.IsFalse (child.IsHandleCreated, "CH8");
595                                 Assert.IsNotNull (parent.Handle, "CH9");
596                                 Assert.IsTrue (parent.IsHandleCreated, "CH10");
597                                 Assert.IsTrue (child.IsHandleCreated, "CH11");
598                         } finally {
599                                 if (parent != null)
600                                         parent.Dispose ();
601                                 if (child != null)
602                                         child.Dispose ();
603                         }
604                 }
605
606                 [Test]
607                 [Category ("NotWorking")]
608                 public void CreateHandleTest2 ()
609                 {
610                         // This should eventually test all operations
611                         // that can be performed on a control (within
612                         // reason)
613                         Control c = new Control ();
614
615                         Assert.IsFalse (c.IsHandleCreated, "0");
616
617                         c.Width = 100;
618                         Assert.IsFalse (c.IsHandleCreated, "1");
619
620                         c.Height = 100;
621                         Assert.IsFalse (c.IsHandleCreated, "2");
622
623                         c.Name = "hi";
624                         Assert.IsFalse (c.IsHandleCreated, "3");
625
626                         c.Left = 5;
627                         Assert.IsFalse (c.IsHandleCreated, "5");
628
629                         c.Top = 5;
630                         Assert.IsFalse (c.IsHandleCreated, "6");
631
632                         c.Location = new Point (1,1);
633                         Assert.IsFalse (c.IsHandleCreated, "7");
634
635                         c.Region = new Region ();
636                         Assert.IsFalse (c.IsHandleCreated, "8");
637
638                         c.Size = new Size (100, 100);
639                         Assert.IsFalse (c.IsHandleCreated, "9");
640
641                         c.Text = "bye";
642                         Assert.IsFalse (c.IsHandleCreated, "10");
643
644                         c.Visible = !c.Visible;
645                         Assert.IsFalse (c.IsHandleCreated, "11");
646                 }
647
648                 [Test]
649                 public void CreateGraphicsTest ()
650                 {
651                         Graphics g = null;
652                         Pen p = null;
653
654                         try {
655                                 Control c = new Control ();
656                                 c.SetBounds (0,0, 20, 20);
657                                 g = c.CreateGraphics ();
658                                 Assert.IsNotNull (g, "Graph1");
659                         } finally {
660                                 if (p != null)
661                                         p.Dispose ();
662                                 if (g != null)
663                                         g.Dispose ();
664                         }
665                 }
666
667                 bool delegateCalled = false;
668                 public delegate void TestDelegate ();
669
670                 public void delegate_call () {
671                         delegateCalled = true;
672                 }
673
674                 [Test]
675                 [ExpectedException(typeof(InvalidOperationException))]
676                 public void InvokeException1 () {
677                         Control c = new Control ();
678                         IAsyncResult result;
679
680                         result = c.BeginInvoke (new TestDelegate (delegate_call));
681                         c.EndInvoke (result);
682                 }
683
684                 [Test]
685                 public void FindFormTest () {
686                         Form f = new Form ();
687
688                         f.ShowInTaskbar = false;
689                         f.Name = "form";
690                         Control c = null;
691
692                         try {
693                                 f.Controls.Add (c = new Control ());
694                                 Assert.AreEqual (f.Name, c.FindForm ().Name, "Find1");
695
696                                 f.Controls.Remove (c);
697
698                                 GroupBox g = new GroupBox ();
699                                 g.Name = "box";
700                                 f.Controls.Add (g);
701                                 g.Controls.Add (c);
702
703                                 Assert.AreEqual (f.Name, f.FindForm ().Name, "Find2");
704
705                                 g.Controls.Remove (c);
706                                 Assert.IsNull(c.FindForm (), "Find3");
707
708                         } finally {
709                                 if (c != null)
710                                         c.Dispose ();
711                                 if (f != null)
712                                         f.Dispose ();
713                         }
714                 }
715
716                 [Test]
717                 public void FocusTest ()
718                 {
719                         Form f = null;
720                         Button c = null, d = null;
721
722                         try {
723                                 f = new Form ();
724                                 f.ShowInTaskbar = false;
725                                 f.Visible = true;
726                                 c = new Button ();
727                                 c.Visible = true;
728                                 f.Controls.Add (c);
729
730                                 d = new Button ();
731                                 d.Visible = false;
732                                 f.Controls.Add (d);
733
734                                 Assert.IsTrue (c.CanFocus, "Focus1");
735                                 Assert.IsFalse (c.Focused, "Focus2");
736                                 c.Focus ();
737                                 Assert.IsTrue (c.Focused, "Focus3");
738                                 d.Focus ();
739                                 Assert.IsFalse (d.Focused, "Focus4");
740
741                                 d.Visible = true;
742                                 d.Focus ();
743                                 Assert.IsTrue (d.Focused, "Focus5");
744                                 Assert.IsFalse (c.Focused, "Focus6");
745
746                                 c.Enabled = false;
747                                 Assert.IsFalse (c.Focused, "Focus7");
748                         } finally {
749                                 if (f != null)
750                                         f.Dispose ();
751                                 if (c != null)
752                                         c.Dispose ();
753                                 if (d != null)
754                                         d.Dispose ();
755                         }
756                 }
757
758                 [Test]
759                 public void FromHandleTest ()
760                 {
761                         Control c1 = null;
762                         Control c2 = null;
763
764                         try {
765                                 c1 = new Control ();
766                                 c2 = new Control ();
767
768                                 c1.Name = "parent";
769                                 c2.Name = "child";
770                                 c1.Controls.Add(c2);
771
772                                 // Handle
773                                 Assert.AreEqual (c1.Name, Control.FromHandle (c1.Handle).Name, "Handle1");
774                                 Assert.IsNull (Control.FromHandle (IntPtr.Zero), "Handle2");
775
776                                 // ChildHandle
777                                 Assert.AreEqual (c1.Name, Control.FromChildHandle (c1.Handle).Name, "Handle3");
778                                 Assert.IsNull (Control.FromChildHandle (IntPtr.Zero), "Handle4");
779
780
781                         } finally {
782                                 if (c1 != null)
783                                         c1.Dispose ();
784
785                                 if (c2 != null)
786                                         c2.Dispose ();
787                         }
788                 }
789
790                 [Test]
791                 public void GetChildAtPointTest ()
792                 {
793                         Control c = null, d = null, e = null;
794
795                         try {
796                                 c = new Control ();
797                                 c.Name = "c1";
798                                 c.SetBounds (0, 0, 100, 100);
799
800                                 d = new Control ();
801                                 d.Name = "d1";
802                                 d.SetBounds (10, 10, 40, 40);
803                                 c.Controls.Add (d);
804
805                                 e = new Control ();
806                                 e.Name = "e1";
807                                 e.SetBounds (55, 55, 10, 10);
808
809                                 Control l = c.GetChildAtPoint (new Point (15, 15));
810                                 Assert.AreEqual (d.Name, l.Name, "Child1");
811                                 Assert.IsFalse (e.Name == l.Name, "Child2");
812
813                                 l = c.GetChildAtPoint (new Point (57, 57));
814                                 Assert.IsNull (l, "Child3");
815
816                                 l = c.GetChildAtPoint (new Point (10, 10));
817                                 Assert.AreEqual (d.Name, l.Name, "Child4");
818
819                                 // GetChildAtPointSkip is not implemented and the following test is breaking for Net_2_0 profile
820 //                              #if NET_2_0
821 //                                      c.Controls.Add (e);
822 //                                      e.Visible = false;
823 //                                      l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Invisible);
824 //                                      Assert.IsNull (l, "Child5");
825
826 //                                      e.Visible = true;
827 //                                      l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Invisible);
828 //                                      Assert.AreSame (e.Name, l.Name, "Child6");
829 //                              #endif // NET_2_0
830                         } finally {
831                                 if (c != null)
832                                         c.Dispose ();
833                                 if (d != null)
834                                         d.Dispose ();
835                         }
836                 }
837
838                 
839                 public class LayoutTestControl : Control {
840                         public int LayoutCount;
841
842                         public LayoutTestControl () : base() {
843                                 LayoutCount = 0;
844                         }
845
846                         protected override void OnLayout(LayoutEventArgs levent) {
847                                 LayoutCount++;
848                                 base.OnLayout (levent);
849                         }
850                 }
851
852                 [Test]
853                 public void LayoutTest() {
854                         LayoutTestControl c;
855
856                         c = new LayoutTestControl();
857
858                         c.SuspendLayout();
859                         c.SuspendLayout();
860                         c.SuspendLayout();
861                         c.SuspendLayout();
862
863                         c.ResumeLayout(true);
864                         c.PerformLayout();
865                         c.ResumeLayout(true);
866                         c.PerformLayout();
867                         c.ResumeLayout(true);
868                         c.PerformLayout();
869                         c.ResumeLayout(true);
870                         c.PerformLayout();
871                         c.ResumeLayout(true);
872                         c.PerformLayout();
873                         c.ResumeLayout(true);
874                         c.PerformLayout();
875                         c.ResumeLayout(true);
876                         c.PerformLayout();
877                         c.SuspendLayout();
878                         c.PerformLayout();
879
880                         Assert.AreEqual(5, c.LayoutCount, "Layout Suspend/Resume locking does not bottom out at 0");
881                 }
882
883                 [Test]
884                 [ExpectedException(typeof(ArgumentException))]
885                 public void TransparentBackgroundTest1() {
886                         Control c;
887
888                         c = new Control();
889                         c.BackColor = Color.Transparent;
890                 }
891
892                 [Test]
893                 public void TransparentBackgroundTest2() {
894                         Panel   c;
895
896                         c = new Panel();
897                         c.BackColor = Color.Transparent;
898                         Assert.AreEqual(Color.Transparent, c.BackColor, "Transparent background not set");
899                 }
900
901                 [Test]
902                 public void TransparentBackgroundTest3() {
903                         Control c;
904
905                         c = new Control();
906                         c.BackColor = Color.Empty;
907                         Assert.AreEqual(Control.DefaultBackColor, c.BackColor, "Setting empty color failed");
908                 }
909
910                 [Test]
911                 public void Dock_Value_Invalid ()
912                 {
913                         Control c = new Control ();
914                         try {
915                                 c.Dock = (DockStyle) 666;
916                                 Assert.Fail ("#1");
917                         } catch (InvalidEnumArgumentException ex) {
918                                 Assert.AreEqual (typeof (InvalidEnumArgumentException), ex.GetType (), "#2");
919                                 Assert.IsNotNull (ex.Message, "#3");
920                                 Assert.IsNotNull (ex.ParamName, "#4");
921                                 Assert.AreEqual ("value", ex.ParamName, "#5");
922                                 Assert.IsNull (ex.InnerException, "#6");
923                         }
924                 }
925
926                 [Test]
927                 public void EnabledTest1() {
928                         Control child;
929                         Control parent;
930                         Control grandma;
931
932                         grandma = new Control();
933                         parent = new Control();
934                         child = new Control();
935
936                         grandma.Controls.Add(parent);
937                         parent.Controls.Add(child);
938                         grandma.Enabled = false;
939                         Assert.AreEqual(grandma.Enabled, child.Enabled, "Child did not inherit disabled state");
940                 }
941
942                 int EnabledCalledCount = 0;
943                 private void EnabledTest2EnabledChanged(object sender, EventArgs e) {
944                         EnabledCalledCount++;
945                 }
946
947                 [Test]
948                 public void EnabledTest2() {
949                         // Check nesting of enabled calls
950                         // OnEnabled is not called for disabled child controls
951                         Control child;
952                         Control parent;
953                         Control grandma;
954
955                         EnabledCalledCount = 0;
956
957                         grandma = new Control();
958                         parent = new Control();
959                         child = new Control();
960                         child.EnabledChanged += new EventHandler(EnabledTest2EnabledChanged);
961
962                         grandma.Controls.Add(parent);
963                         parent.Controls.Add(child);
964                         grandma.Enabled = false;
965
966                         Assert.AreEqual(1, EnabledCalledCount, "Child Enabled Event not properly fired");
967                         grandma.Enabled = true;
968                         Assert.AreEqual(2, EnabledCalledCount, "Child Enabled Event not properly fired");
969                         child.Enabled = false;
970                         grandma.Enabled = false;
971                         Assert.AreEqual(3, EnabledCalledCount, "Child Enabled Event not properly fired");
972                 }
973
974                 [Test]
975                 public void ControlsRemoveNullTest ()
976                 {
977                         Control c = new Control ();
978                         c.Controls.Remove (null);
979                 }
980
981                 [Test]
982                 public void ControlsAddNullTest ()
983                 {
984                         Control c = new Control ();
985                         c.Controls.Add (null);
986                 }
987
988                 [Test]
989                 [ExpectedException (typeof (ArgumentNullException))]
990                 public void ControlsSetChildIndexNullTest ()
991                 {
992                         Control c = new Control ();
993                         c.Controls.SetChildIndex (null, 1);
994                 }
995
996                 [Test]
997                 [ExpectedException (typeof (ArgumentNullException))]
998                 public void ControlsAddRangeNullTest ()
999                 {
1000                         Control c = new Control ();
1001                         c.Controls.AddRange (null);
1002                 }
1003
1004                 [Test]
1005                 public void ControlsAddRangeNullElementTest ()
1006                 {
1007                         Control c = new Control ();
1008                         Control[] subcontrols = new Control[2];
1009                         subcontrols[0] = new Control ();
1010                         subcontrols[1] = null;
1011
1012                         c.Controls.AddRange (subcontrols);
1013                 }
1014
1015                 [Test]
1016                 public void RegionTest () {
1017                         Form f = new Form ();
1018                         f.ShowInTaskbar = false;
1019                         Control c = new Control ();
1020                         f.Controls.Add (c);
1021                         Assert.IsNull (c.Region, "#A1");
1022                         f.Show ();
1023                         Assert.IsNull (c.Region, "#A2");
1024                         c.Region = null;
1025                         Assert.IsNull (c.Region, "#A3");
1026                         f.Dispose ();
1027
1028                         Region region = new Region ();
1029                         f = new Form ();
1030                         f.ShowInTaskbar = false;
1031                         c = new Control ();
1032                         f.Controls.Add (c);
1033                         c.Region = region;
1034                         Assert.IsNotNull (c.Region, "#B1");
1035                         Assert.AreSame (region, c.Region, "#B2");
1036                         f.Show ();
1037                         c.Region = null;
1038                         Assert.IsNull (c.Region, "#B3");
1039
1040                         f.Dispose ();
1041                 }
1042
1043                 [Test] // bug #80280
1044                 [Category ("NotWorking")]
1045                 public void Validated_Multiple_Containers ()
1046                 {
1047                         Form form = new Form ();
1048                         form.ShowInTaskbar = false;
1049
1050                         UserControl control1 = new UserControl();
1051                         UserControl container1 = new UserControl();
1052                         control1.Tag = true;
1053                         control1.Validated += new EventHandler (Control_ValidatedHandler);
1054                         container1.Controls.Add(control1);
1055                         form.Controls.Add (container1);
1056
1057                         UserControl container2 = new UserControl();
1058                         UserControl control2 = new UserControl();
1059                         container2.Controls.Add(control2);
1060                         form.Controls.Add (container2);
1061
1062                         Assert.IsTrue ((bool) control1.Tag, "#1");
1063                         control1.Select();
1064                         Assert.IsTrue ((bool) control1.Tag, "#2");
1065                         control2.Select();
1066                         Assert.IsFalse ((bool) control1.Tag, "#3");
1067
1068                         form.Dispose ();
1069                 }
1070
1071                 private void Control_ValidatedHandler (object sender, EventArgs e)
1072                 {
1073                         ((Control) sender).Tag = false;
1074                 }
1075         }
1076
1077         [TestFixture]
1078         public class ControlInvokeTest {
1079                 public delegate void TestDelegate ();
1080
1081                 Form f;
1082                 Control c;
1083                 Thread control_t;
1084                 ApplicationContext control_context;
1085                 bool delegateCalled = false;
1086
1087                 object m;
1088
1089                 void CreateControl ()
1090                 {
1091                         f = new Form ();
1092                         f.ShowInTaskbar = false;
1093                         
1094                         c = new Control ();
1095
1096                         f.Controls.Add (c);
1097
1098                         Console.WriteLine ("f.Handle = {0}", f.Handle);
1099                         Console.WriteLine ("c.Handle = {0}", c.Handle);
1100
1101                         control_context = new ApplicationContext (f);
1102
1103                         Monitor.Enter (m);
1104                         Console.WriteLine ("pulsing");
1105                         Monitor.Pulse (m);
1106                         Monitor.Exit (m);
1107                         Console.WriteLine ("control thread running");
1108                         Application.Run (control_context);
1109                         c.Dispose ();
1110                 }
1111
1112                 [Test]
1113                 public void InvokeTest ()
1114                 {
1115                         m = new object ();
1116
1117                         control_t = new Thread(new ThreadStart(CreateControl));
1118
1119                         Monitor.Enter (m);
1120
1121                         control_t.Start ();
1122
1123                         Console.WriteLine ("waiting on monitor");
1124                         Monitor.Wait (m);
1125
1126                         Console.WriteLine ("making async call");
1127
1128                         IAsyncResult result;
1129                         result = c.BeginInvoke (new TestDelegate (delegate_call));
1130                         c.EndInvoke (result);
1131
1132                         Assert.AreEqual (true, delegateCalled, "Invoke1");
1133                 }
1134
1135                 public void delegate_call () {
1136                         /* invoked on control_context's thread */
1137                         delegateCalled = true;
1138                         f.Dispose ();
1139                         Application.Exit ();
1140                 }
1141                 
1142         }
1143
1144         [TestFixture]
1145         public class ControlWMTest
1146         {
1147                 [Test]
1148                 public void WM_PARENTNOTIFY_Test ()
1149                 {
1150                         WMTester tester;
1151                         Control child;
1152                         int child_handle;
1153                         
1154                         tester = new WMTester ();
1155                         child = new Control ();
1156                         tester.Controls.Add (child);
1157                         
1158                         tester.Visible = true;
1159                         child.Visible = true;
1160
1161                         child_handle = child.Handle.ToInt32 ();
1162
1163                         ArrayList msgs;
1164                         Message m1;
1165                                 
1166                         msgs = tester.Find (WndMsg.WM_PARENTNOTIFY);
1167                         
1168                         Assert.AreEqual (1, msgs.Count, "#1");
1169                         
1170                         m1 = (Message) msgs [0];
1171                         Assert.AreEqual (WndMsg.WM_CREATE, ((WndMsg) LowOrder (m1.WParam)),  "#2");
1172                         //Assert.AreEqual (child.Identifier??, HighOrder (m1.WParam),  "#3");
1173                         Assert.AreEqual (child_handle, m1.LParam.ToInt32 (),  "#4");
1174
1175                         child.Dispose ();
1176
1177                         msgs = tester.Find (WndMsg.WM_PARENTNOTIFY);
1178                         Assert.AreEqual (2, msgs.Count, "#5");
1179                         m1 = (Message) msgs [1];
1180
1181                         Assert.AreEqual (WndMsg.WM_DESTROY, ((WndMsg) LowOrder (m1.WParam)),  "#6");
1182                         //Assert.AreEqual (child.Identifier??, HighOrder (m1.WParam),  "#7");
1183                         Assert.AreEqual (child_handle, m1.LParam.ToInt32 (),  "#8");
1184
1185                         tester.Dispose ();
1186                 }
1187
1188                 internal static int LowOrder (int param) 
1189                 {
1190                         return ((int)(short)(param & 0xffff));
1191                 }
1192
1193                 internal static int HighOrder (int param) 
1194                 {
1195                         return ((int)(short)(param >> 16));
1196                 }
1197
1198                 internal static int LowOrder (IntPtr param) 
1199                 {
1200                         return ((int)(short)(param.ToInt32 () & 0xffff));
1201                 }
1202
1203                 internal static int HighOrder (IntPtr param) 
1204                 {
1205                         return ((int)(short)(param.ToInt32 () >> 16));
1206                 }
1207
1208                 internal class WMTester : Form
1209                 {
1210                         internal ArrayList Messages = new ArrayList ();
1211                         
1212                         internal bool Contains (WndMsg msg)
1213                         {
1214                                 return Contains (msg, Messages);
1215                         }
1216
1217                         internal bool Contains (WndMsg msg, ArrayList list)
1218                         {
1219                                 foreach (Message m in Messages) 
1220                                 {
1221                                         if (m.Msg == (int) msg)
1222                                                 return true;
1223                                 }
1224                                 return false;
1225                         }
1226
1227                         internal ArrayList Find (WndMsg msg)
1228                         {
1229                                 ArrayList result = new ArrayList ();
1230
1231                                 foreach (Message m in Messages)
1232                                 {
1233                                         if (m.Msg == (int) msg)
1234                                                 result.Add (m);
1235                                 }
1236                                 return result;
1237                         }
1238
1239                         protected override void WndProc(ref Message m)
1240                         {
1241                                 Console.WriteLine ("WndProc: " + m.ToString ());
1242                                 Messages.Add (m);
1243                                 base.WndProc (ref m);
1244                         }
1245                 }
1246         }
1247 }