In System.Windows.Forms:
[mono-project.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / ControlTest.cs
blob1b9584c078b1dfcab9825e8dcb5b2a97c7241d01
1 //
2 // Copyright (c) 2005 Novell, Inc.
3 //
4 // Authors:
5 // Ritvik Mayank (mritvik@novell.com)
6 //
8 using System;
9 using InvalidEnumArgumentException = System.ComponentModel.InvalidEnumArgumentException;
10 using System.Drawing;
11 using System.Reflection;
12 using System.Runtime.Remoting;
13 using System.Threading;
14 using System.Windows.Forms;
16 using NUnit.Framework;
18 namespace MonoTests.System.Windows.Forms
20 [TestFixture]
21 public class ControlTest
23 class Helper {
24 public static void TestAccessibility(Control c, string Default, string Description, string Name, AccessibleRole Role)
26 Assert.IsNotNull (c.AccessibilityObject, "Acc1");
27 Assert.AreEqual (Default, c.AccessibleDefaultActionDescription, "Acc2");
28 Assert.AreEqual (Description, c.AccessibleDescription, "Acc3");
29 Assert.AreEqual (Name, c.AccessibleName, "Acc4");
30 Assert.AreEqual (Role, c.AccessibleRole, "Acc5");
33 public static string TestControl(Control container, Control start, bool forward) {
34 Control ctl;
36 ctl = container.GetNextControl(start, forward);
38 if (ctl == null) {
39 return null;
42 return ctl.Text;
46 [Test]
47 public void CreatedTest ()
49 Control c = new Control ();
50 Assert.IsFalse (c.Created, "A1");
53 [Test]
54 [Category ("NotWorking")]
55 public void CreatedAccessibilityTest ()
57 Control c = new Control ();
58 Assert.IsFalse (c.Created, "A1");
60 Helper.TestAccessibility(c, null, null, null, AccessibleRole.Default);
62 Assert.IsTrue (c.Created, "A2");
64 c.Dispose ();
66 Assert.IsFalse (c.Created, "A3");
69 [Test]
70 [Category ("NotWorking")]
71 public void BoundsTest ()
73 Control c = new Control ();
74 Assert.IsTrue (c.Bounds.IsEmpty, "A1");
75 Assert.IsTrue (c.Size.IsEmpty, "A2");
76 Assert.IsTrue (c.ClientSize.IsEmpty, "A3");
77 Assert.IsTrue (c.ClientRectangle.IsEmpty, "A4");
79 Assert.AreEqual (((IWin32Window)c).Handle, c.Handle, "A5");
81 /* this part fails on linux because we can't allocate X windows which are 0x0,
82 and the Control bounds directly reflect the size of the X window */
84 Assert.IsTrue (c.Bounds.IsEmpty, "A6");
85 Assert.IsTrue (c.Size.IsEmpty, "A7");
86 Assert.IsTrue (c.ClientSize.IsEmpty, "A8");
87 Assert.IsTrue (c.ClientRectangle.IsEmpty, "A9");
90 [Test]
91 public void PubPropTest()
93 Control c = new Control();
95 Assert.IsFalse (c.AllowDrop , "A1");
96 Assert.AreEqual(AnchorStyles.Top | AnchorStyles.Left, c.Anchor, "A2");
98 Assert.AreEqual ("Control", c.BackColor.Name , "B1");
99 Assert.IsNull (c.BackgroundImage, "B2");
100 Assert.IsNull (c.BindingContext, "B3");
102 Assert.IsFalse (c.CanFocus, "C1");
103 Assert.IsTrue (c.CanSelect, "C2");
104 Assert.IsFalse (c.Capture, "C3");
105 Assert.IsTrue (c.CausesValidation, "C4");
107 Assert.IsNotNull (c.CompanyName, "C7");
108 Assert.IsNull (c.Container, "C8");
109 Assert.IsFalse (c.ContainsFocus, "C9");
110 Assert.IsNull (c.ContextMenu, "C10");
111 Assert.AreEqual (0, c.Controls.Count, "C11");
112 Assert.IsFalse (c.Created, "C12");
113 Assert.AreEqual (Cursors.Default, c.Cursor, "C13");
115 Assert.IsNotNull(c.DataBindings, "D1");
116 Assert.AreEqual("Control", Control.DefaultBackColor.Name, "D2");
117 Assert.AreEqual("ControlText", Control.DefaultForeColor.Name, "D3");
118 Assert.AreEqual(FontStyle.Regular, Control.DefaultFont.Style, "D4");
119 Assert.AreEqual (new Rectangle(0, 0, 0, 0), c.DisplayRectangle , "D5");
120 Assert.IsFalse (c.Disposing, "D6");
121 Assert.AreEqual(DockStyle.None, c.Dock, "D7");
123 Assert.IsTrue (c.Enabled, "E1");
125 Assert.IsFalse (c.Focused, "F1");
126 Assert.AreEqual (FontStyle.Regular, c.Font.Style, "F2");
127 Assert.AreEqual (SystemColors.ControlText, c.ForeColor, "F3");
129 Assert.IsFalse (c.HasChildren, "H2");
131 Assert.AreEqual (ImeMode.NoControl, c.ImeMode, "I1");
132 Assert.IsFalse (c.InvokeRequired, "I2");
133 Assert.IsFalse (c.IsAccessible, "I3");
134 Assert.IsFalse (c.IsDisposed, "I4");
135 Assert.IsFalse (c.IsHandleCreated, "I5");
137 Assert.AreEqual(Point.Empty, c.Location, "L2");
139 #if NET_2_0
140 Assert.IsTrue(c.MaximumSize.IsEmpty);
141 Assert.IsTrue(c.MinimumSize.IsEmpty);
142 #endif
143 Assert.AreEqual (Keys.None, Control.ModifierKeys, "M1");
144 Assert.IsFalse (Control.MousePosition.IsEmpty, "M2");
145 Assert.AreEqual (MouseButtons.None, Control.MouseButtons, "M3");
147 Assert.AreEqual("", c.Name, "N1");
148 c.Name = "Control Name";
149 Assert.AreEqual("Control Name", c.Name, "N2");
151 Assert.IsNull (c.Parent, "P1");
152 Assert.IsNotNull (c.ProductName, "P2");
153 Assert.IsTrue (c.ProductName != "", "P3");
154 Assert.IsNotNull (c.ProductVersion, "P4");
155 Assert.IsTrue (c.ProductVersion != "", "P5");
157 Assert.IsFalse (c.RecreatingHandle, "R1");
158 Assert.IsNull (c.Region, "R2");
159 Assert.AreEqual (RightToLeft.No, c.RightToLeft, "R4");
161 Assert.IsNull (c.Site, "S1");
163 Assert.AreEqual (0, c.TabIndex , "T1");
164 Assert.IsTrue (c.TabStop, "T2");
165 Assert.IsNull (c.Tag, "T3");
166 Assert.AreEqual ("", c.Text, "T4");
168 Assert.IsTrue (c.Visible, "V1");
171 [Test]
172 public void SizeChangeTest ()
174 Form f = new Form ();
175 Control c = new Control ();
176 f.Controls.Add(c);
177 f.Show();
178 c.Resize += new EventHandler(SizeChangedTest_ResizeHandler);
179 c.Tag = true;
180 c.Size = c.Size;
181 Assert.AreEqual (true, (bool) c.Tag, "#1");
182 f.Close ();
185 private void SizeChangedTest_ResizeHandler (object sender, EventArgs e)
187 Console.WriteLine("Resized");
188 ((Control) sender).Tag = false;
191 [Test]
192 public void NegativeHeightTest ()
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 ();
204 private void NegativeHeightTest_ResizeHandler (object sender, EventArgs e)
206 Control c = (Control) sender;
207 c.Tag = c.Height;
210 [Test]
211 public void TopLevelControlTest () {
212 Control c = new Control ();
214 Assert.AreEqual(null, c.TopLevelControl, "T1");
216 Panel p = new Panel ();
218 p.Controls.Add (c);
220 Assert.AreEqual(null, c.TopLevelControl, "T2");
222 Form f = new Form ();
223 f.ShowInTaskbar = false;
225 f.Controls.Add (p);
227 Assert.AreEqual (f, c.TopLevelControl, "T3");
228 Assert.AreEqual (f, f.TopLevelControl, "T4");
231 [Test]
232 public void RelationTest() {
233 Control c1;
234 Control c2;
236 c1 = new Control();
237 c2 = new Control();
239 Assert.AreEqual(true , c1.Visible , "Rel1");
240 Assert.AreEqual(false, c1.Contains(c2) , "Rel2");
241 Assert.AreEqual("System.Windows.Forms.Control", c1.ToString() , "Rel3");
243 c1.Controls.Add(c2);
244 Assert.AreEqual(true , c2.Visible , "Rel4");
245 Assert.AreEqual(true, c1.Contains(c2) , "Rel5");
247 c1.Anchor = AnchorStyles.Top;
248 c1.SuspendLayout ();
249 c1.Anchor = AnchorStyles.Left ;
250 c1.ResumeLayout ();
251 Assert.AreEqual(AnchorStyles.Left , c1.Anchor, "Rel6");
253 c1.SetBounds(10, 20, 30, 40) ;
254 Assert.AreEqual(new Rectangle(10, 20, 30, 40), c1.Bounds, "Rel7");
256 Assert.AreEqual(c1, c2.Parent, "Rel8");
259 [Test]
260 [Category ("NotWorking")]
261 public void TabOrder() {
262 Form form;
263 Control active;
265 Label label1 = new Label(); // To test non-tabstop items as well
266 Label label2 = new Label();
268 GroupBox group1 = new GroupBox();
269 GroupBox group2 = new GroupBox();
270 GroupBox group3 = new GroupBox();
272 TextBox text1 = new TextBox();
274 RadioButton radio11 = new RadioButton();
275 RadioButton radio12 = new RadioButton();
276 RadioButton radio13 = new RadioButton();
277 RadioButton radio14 = new RadioButton();
278 RadioButton radio21 = new RadioButton();
279 RadioButton radio22 = new RadioButton();
280 RadioButton radio23 = new RadioButton();
281 RadioButton radio24 = new RadioButton();
282 RadioButton radio31 = new RadioButton();
283 RadioButton radio32 = new RadioButton();
284 RadioButton radio33 = new RadioButton();
285 RadioButton radio34 = new RadioButton();
287 form = new Form();
288 form.ShowInTaskbar = false;
290 form.ClientSize = new Size (520, 520);
291 Assert.AreEqual(new Size(520, 520), form.ClientSize, "Tab1");
293 form.Text = "SWF Taborder Test App Form";
294 Assert.AreEqual("SWF Taborder Test App Form", form.Text, "Tab2");
296 label1.Location = new Point(10, 10);
297 Assert.AreEqual(new Point(10, 10), label1.Location, "Tab3");
298 label1.Text = "Label1";
299 form.Controls.Add(label1);
301 label2.Location = new Point(200, 10);
302 label2.Text = "Label2";
303 form.Controls.Add(label2);
305 group1.Text = "Group1";
306 group2.Text = "Group2";
307 group3.Text = "Group3";
309 group1.Size = new Size(200, 400);
310 group2.Size = new Size(200, 400);
311 group3.Size = new Size(180, 180);
312 Assert.AreEqual(new Size(180, 180), group3.Size, "Tab4");
314 group1.Location = new Point(10, 40);
315 group2.Location = new Point(220, 40);
316 group3.Location = new Point(10, 210);
318 group1.TabIndex = 30;
319 Assert.AreEqual(30, group1.TabIndex, "Tab5");
320 group1.TabStop = true;
322 // Don't assign, test automatic assignment
323 //group2.TabIndex = 0;
324 group2.TabStop = true;
325 Assert.AreEqual(0, group2.TabIndex, "Tab6");
327 group3.TabIndex = 35;
328 group3.TabStop = true;
330 // Test default tab index
331 Assert.AreEqual(0, radio11.TabIndex, "Tab7");
333 text1.Text = "Edit Control";
335 radio11.Text = "Radio 1-1 [Tab1]";
336 radio12.Text = "Radio 1-2 [Tab2]";
337 radio13.Text = "Radio 1-3 [Tab3]";
338 radio14.Text = "Radio 1-4 [Tab4]";
340 radio21.Text = "Radio 2-1 [Tab4]";
341 radio22.Text = "Radio 2-2 [Tab3]";
342 radio23.Text = "Radio 2-3 [Tab2]";
343 radio24.Text = "Radio 2-4 [Tab1]";
345 radio31.Text = "Radio 3-1 [Tab1]";
346 radio32.Text = "Radio 3-2 [Tab3]";
347 radio33.Text = "Radio 3-3 [Tab2]";
348 radio34.Text = "Radio 3-4 [Tab4]";
350 // We don't assign TabIndex for radio1X; test automatic assignment
351 text1.TabStop = true;
352 radio11.TabStop = true;
354 radio21.TabIndex = 4;
355 radio22.TabIndex = 3;
356 radio23.TabIndex = 2;
357 radio24.TabIndex = 1;
358 radio24.TabStop = true;
360 radio31.TabIndex = 11;
361 radio31.TabStop = true;
362 radio32.TabIndex = 13;
363 radio33.TabIndex = 12;
364 radio34.TabIndex = 14;
366 text1.Location = new Point(10, 100);
368 radio11.Location = new Point(10, 20);
369 radio12.Location = new Point(10, 40);
370 radio13.Location = new Point(10, 60);
371 radio14.Location = new Point(10, 80);
373 radio21.Location = new Point(10, 20);
374 radio22.Location = new Point(10, 40);
375 radio23.Location = new Point(10, 60);
376 radio24.Location = new Point(10, 80);
378 radio31.Location = new Point(10, 20);
379 radio32.Location = new Point(10, 40);
380 radio33.Location = new Point(10, 60);
381 radio34.Location = new Point(10, 80);
383 text1.Size = new Size(150, text1.PreferredHeight);
385 radio11.Size = new Size(150, 20);
386 radio12.Size = new Size(150, 20);
387 radio13.Size = new Size(150, 20);
388 radio14.Size = new Size(150, 20);
390 radio21.Size = new Size(150, 20);
391 radio22.Size = new Size(150, 20);
392 radio23.Size = new Size(150, 20);
393 radio24.Size = new Size(150, 20);
395 radio31.Size = new Size(150, 20);
396 radio32.Size = new Size(150, 20);
397 radio33.Size = new Size(150, 20);
398 radio34.Size = new Size(150, 20);
400 group1.Controls.Add(text1);
402 group1.Controls.Add(radio11);
403 group1.Controls.Add(radio12);
404 group1.Controls.Add(radio13);
405 group1.Controls.Add(radio14);
407 group2.Controls.Add(radio21);
408 group2.Controls.Add(radio22);
409 group2.Controls.Add(radio23);
410 group2.Controls.Add(radio24);
412 group3.Controls.Add(radio31);
413 group3.Controls.Add(radio32);
414 group3.Controls.Add(radio33);
415 group3.Controls.Add(radio34);
417 form.Controls.Add(group1);
418 form.Controls.Add(group2);
419 group2.Controls.Add(group3);
421 // Perform some tests, the TabIndex stuff below will alter the outcome
422 Assert.AreEqual(null, Helper.TestControl(group2, radio34, true), "Tab8");
423 Assert.AreEqual(31, group2.TabIndex, "Tab9");
425 // Does the taborder of containers and non-selectable things change behaviour?
426 label1.TabIndex = 5;
427 label2.TabIndex = 4;
428 group1.TabIndex = 3;
429 group2.TabIndex = 1;
431 // Start verification
432 Assert.AreEqual(null, Helper.TestControl(group2, radio34, true), "Tab10");
433 Assert.AreEqual(radio24.Text, Helper.TestControl(group2, group2, true), "Tab11");
434 Assert.AreEqual(radio31.Text, Helper.TestControl(group2, group3, true), "Tab12");
435 Assert.AreEqual(null, Helper.TestControl(group1, radio14, true), "Tab13");
436 Assert.AreEqual(radio23.Text, Helper.TestControl(group2, radio24, true), "Tab14");
437 Assert.AreEqual(group3.Text, Helper.TestControl(group2, radio21, true), "Tab15");
438 Assert.AreEqual(radio13.Text, Helper.TestControl(form, radio12, true), "Tab16");
439 Assert.AreEqual(label2.Text, Helper.TestControl(form, radio14, true), "Tab17");
440 Assert.AreEqual(group1.Text, Helper.TestControl(form, radio34, true), "Tab18");
441 Assert.AreEqual(radio23.Text, Helper.TestControl(group2, radio24, true), "Tab19");
443 // Sanity checks
444 Assert.AreEqual(null, Helper.TestControl(radio11, radio21, true), "Tab20");
445 Assert.AreEqual(text1.Text, Helper.TestControl(group1, radio21, true), "Tab21");
447 Assert.AreEqual(radio14.Text, Helper.TestControl(form, label2, false), "Tab22");
448 Assert.AreEqual(radio21.Text, Helper.TestControl(group2, group3, false), "Tab23");
450 Assert.AreEqual(4, radio21.TabIndex, "Tab24");
451 Assert.AreEqual(1, radio11.TabIndex, "Tab25");
452 Assert.AreEqual(3, radio13.TabIndex, "Tab26");
453 Assert.AreEqual(35, group3.TabIndex, "Tab27");
454 Assert.AreEqual(1, group2.TabIndex, "Tab28");
456 Assert.AreEqual(label1.Text, Helper.TestControl(form, form, false), "Tab29");
457 Assert.AreEqual(radio14.Text, Helper.TestControl(group1, group1, false), "Tab30");
458 Assert.AreEqual(radio34.Text, Helper.TestControl(group3, group3, false), "Tab31");
460 Assert.AreEqual(null, Helper.TestControl(label1, label1, false), "Tab31");
461 Assert.AreEqual(null, Helper.TestControl(radio11, radio21, false), "Tab32");
462 form.Dispose ();
465 [Test]
466 public void ScaleTest()
468 Control r1 = new Control();
470 r1.Width = 40;
471 r1.Height = 20;
472 r1.Scale(2);
473 Assert.AreEqual(80, r1.Width, "Scale1");
474 Assert.AreEqual(40, r1.Height, "Scale2");
477 [Test]
478 public void TextTest()
480 Control r1 = new Control();
481 r1.Text = "Hi" ;
482 Assert.AreEqual("Hi" , r1.Text , "Text1");
484 r1.ResetText();
485 Assert.AreEqual("" , r1.Text , "Text2");
488 [Test]
489 public void PubMethodTest7()
491 Control r1 = new Control();
492 r1.RightToLeft = RightToLeft.Yes ;
493 r1.ResetRightToLeft() ;
494 Assert.AreEqual(RightToLeft.No , r1.RightToLeft , "#81");
495 r1.ImeMode = ImeMode.Off ;
496 r1.ResetImeMode () ;
497 Assert.AreEqual(ImeMode.NoControl , r1.ImeMode , "#82");
498 r1.ForeColor= SystemColors.GrayText ;
499 r1.ResetForeColor() ;
500 Assert.AreEqual(SystemColors.ControlText , r1.ForeColor , "#83");
501 //r1.Font = Font.FromHdc();
502 r1.ResetFont () ;
503 //Assert.AreEqual(FontFamily.GenericSansSerif , r1.Font , "#83");
504 r1.Cursor = Cursors.Hand ;
505 r1.ResetCursor () ;
506 Assert.AreEqual(Cursors.Default , r1.Cursor , "#83");
507 //r1.DataBindings = System.Windows.Forms.Binding ;
508 //r1.ResetBindings() ;
509 //Assert.AreEqual(ControlBindingsCollection , r1.DataBindings , "#83");
510 r1.BackColor = Color.Black ;
511 r1.ResetBackColor() ;
512 Assert.AreEqual( SystemColors.Control , r1.BackColor , "#84");
513 r1.BackColor = Color.Black ;
514 r1.Refresh() ;
515 Assert.AreEqual( null , r1.Region , "#85");
516 Rectangle M = new Rectangle(10, 20, 30 ,40);
517 r1.RectangleToScreen(M) ;
518 Assert.AreEqual( null , r1.Region , "#86");
522 [Test]
523 public void ScreenClientCoords()
525 Label l;
526 Point p1;
527 Point p2;
528 Point p3;
530 l = new Label();
531 l.Left = 10;
532 l.Top = 12;
533 l.Visible = true;
534 p1 = new Point (10,10);
535 p2 = l.PointToScreen(p1);
536 p3 = l.PointToClient(p2);
538 Assert.AreEqual (p1, p3, "SC1");
541 [Test]
542 public void ContainsTest ()
544 Control t = new Control ();
545 Control s = new Control ();
547 t.Controls.Add (s);
549 Assert.AreEqual (true, t.Contains (s), "Con1");
550 Assert.AreEqual (false, s.Contains (t), "Con2");
551 Assert.AreEqual (false, s.Contains (null), "Con3");
552 Assert.AreEqual (false, t.Contains (new Control ()), "Con4");
555 [Test]
556 public void CreateHandleTest ()
558 Control parent;
559 Control child;
561 parent = null;
562 child = null;
564 try {
565 parent = new Control ();
566 child = new Control ();
568 parent.Visible = true;
569 parent.Controls.Add (child);
571 Assert.IsFalse (parent.IsHandleCreated, "CH1");
572 Assert.IsFalse (child.IsHandleCreated, "CH2");
574 parent.CreateControl ();
575 Assert.IsNotNull (parent.Handle, "CH3");
576 Assert.IsNotNull (child.Handle, "CH4");
577 Assert.IsTrue (parent.IsHandleCreated, "CH5");
578 Assert.IsTrue (child.IsHandleCreated, "CH6");
579 } finally {
580 if (parent != null)
581 parent.Dispose ();
582 if (child != null)
583 child.Dispose ();
586 // Accessing Handle Property creates the handle
587 try {
588 parent = new Control ();
589 parent.Visible = true;
590 child = new Control ();
591 parent.Controls.Add (child);
592 Assert.IsFalse (parent.IsHandleCreated, "CH7");
593 Assert.IsFalse (child.IsHandleCreated, "CH8");
594 Assert.IsNotNull (parent.Handle, "CH9");
595 Assert.IsTrue (parent.IsHandleCreated, "CH10");
596 Assert.IsTrue (child.IsHandleCreated, "CH11");
597 } finally {
598 if (parent != null)
599 parent.Dispose ();
600 if (child != null)
601 child.Dispose ();
605 [Test]
606 [Category ("NotWorking")]
607 public void CreateHandleTest2 ()
609 // This should eventually test all operations
610 // that can be performed on a control (within
611 // reason)
612 Control c = new Control ();
614 Assert.IsFalse (c.IsHandleCreated, "0");
616 c.Width = 100;
617 Assert.IsFalse (c.IsHandleCreated, "1");
619 c.Height = 100;
620 Assert.IsFalse (c.IsHandleCreated, "2");
622 c.Name = "hi";
623 Assert.IsFalse (c.IsHandleCreated, "3");
625 c.Left = 5;
626 Assert.IsFalse (c.IsHandleCreated, "5");
628 c.Top = 5;
629 Assert.IsFalse (c.IsHandleCreated, "6");
631 c.Location = new Point (1,1);
632 Assert.IsFalse (c.IsHandleCreated, "7");
634 c.Region = new Region ();
635 Assert.IsFalse (c.IsHandleCreated, "8");
637 c.Size = new Size (100, 100);
638 Assert.IsFalse (c.IsHandleCreated, "9");
640 c.Text = "bye";
641 Assert.IsFalse (c.IsHandleCreated, "10");
643 c.Visible = !c.Visible;
644 Assert.IsFalse (c.IsHandleCreated, "11");
647 [Test]
648 public void CreateGraphicsTest ()
650 Graphics g = null;
651 Pen p = null;
653 try {
654 Control c = new Control ();
655 c.SetBounds (0,0, 20, 20);
656 g = c.CreateGraphics ();
657 Assert.IsNotNull (g, "Graph1");
658 } finally {
659 if (p != null)
660 p.Dispose ();
661 if (g != null)
662 g.Dispose ();
666 bool delegateCalled = false;
667 public delegate void TestDelegate ();
669 public void delegate_call () {
670 delegateCalled = true;
673 [Test]
674 [ExpectedException(typeof(InvalidOperationException))]
675 public void InvokeException1 () {
676 Control c = new Control ();
677 IAsyncResult result;
679 result = c.BeginInvoke (new TestDelegate (delegate_call));
680 c.EndInvoke (result);
683 [Test]
684 public void FindFormTest () {
685 Form f = new Form ();
687 f.ShowInTaskbar = false;
688 f.Name = "form";
689 Control c = null;
691 try {
692 f.Controls.Add (c = new Control ());
693 Assert.AreEqual (f.Name, c.FindForm ().Name, "Find1");
695 f.Controls.Remove (c);
697 GroupBox g = new GroupBox ();
698 g.Name = "box";
699 f.Controls.Add (g);
700 g.Controls.Add (c);
702 Assert.AreEqual (f.Name, f.FindForm ().Name, "Find2");
704 g.Controls.Remove (c);
705 Assert.IsNull(c.FindForm (), "Find3");
707 } finally {
708 if (c != null)
709 c.Dispose ();
710 if (f != null)
711 f.Dispose ();
715 [Test]
716 public void FocusTest ()
718 Form f = null;
719 Button c = null, d = null;
721 try {
722 f = new Form ();
723 f.ShowInTaskbar = false;
724 f.Visible = true;
725 c = new Button ();
726 c.Visible = true;
727 f.Controls.Add (c);
729 d = new Button ();
730 d.Visible = false;
731 f.Controls.Add (d);
733 Assert.IsTrue (c.CanFocus, "Focus1");
734 Assert.IsFalse (c.Focused, "Focus2");
735 c.Focus ();
736 Assert.IsTrue (c.Focused, "Focus3");
737 d.Focus ();
738 Assert.IsFalse (d.Focused, "Focus4");
740 d.Visible = true;
741 d.Focus ();
742 Assert.IsTrue (d.Focused, "Focus5");
743 Assert.IsFalse (c.Focused, "Focus6");
745 c.Enabled = false;
746 Assert.IsFalse (c.Focused, "Focus7");
747 } finally {
748 if (f != null)
749 f.Dispose ();
750 if (c != null)
751 c.Dispose ();
752 if (d != null)
753 d.Dispose ();
757 [Test]
758 public void FromHandleTest ()
760 Control c1 = null;
761 Control c2 = null;
763 try {
764 c1 = new Control ();
765 c2 = new Control ();
767 c1.Name = "parent";
768 c2.Name = "child";
769 c1.Controls.Add(c2);
771 // Handle
772 Assert.AreEqual (c1.Name, Control.FromHandle (c1.Handle).Name, "Handle1");
773 Assert.IsNull (Control.FromHandle (IntPtr.Zero), "Handle2");
775 // ChildHandle
776 Assert.AreEqual (c1.Name, Control.FromChildHandle (c1.Handle).Name, "Handle3");
777 Assert.IsNull (Control.FromChildHandle (IntPtr.Zero), "Handle4");
780 } finally {
781 if (c1 != null)
782 c1.Dispose ();
784 if (c2 != null)
785 c2.Dispose ();
789 [Test]
790 public void GetChildAtPointTest ()
792 Control c = null, d = null, e = null;
794 try {
795 c = new Control ();
796 c.Name = "c1";
797 c.SetBounds (0, 0, 100, 100);
799 d = new Control ();
800 d.Name = "d1";
801 d.SetBounds (10, 10, 40, 40);
802 c.Controls.Add (d);
804 e = new Control ();
805 e.Name = "e1";
806 e.SetBounds (55, 55, 10, 10);
808 Control l = c.GetChildAtPoint (new Point (15, 15));
809 Assert.AreEqual (d.Name, l.Name, "Child1");
810 Assert.IsFalse (e.Name == l.Name, "Child2");
812 l = c.GetChildAtPoint (new Point (57, 57));
813 Assert.IsNull (l, "Child3");
815 l = c.GetChildAtPoint (new Point (10, 10));
816 Assert.AreEqual (d.Name, l.Name, "Child4");
818 // GetChildAtPointSkip is not implemented and the following test is breaking for Net_2_0 profile
819 // #if NET_2_0
820 // c.Controls.Add (e);
821 // e.Visible = false;
822 // l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Invisible);
823 // Assert.IsNull (l, "Child5");
825 // e.Visible = true;
826 // l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Invisible);
827 // Assert.AreSame (e.Name, l.Name, "Child6");
828 // #endif // NET_2_0
829 } finally {
830 if (c != null)
831 c.Dispose ();
832 if (d != null)
833 d.Dispose ();
838 public class LayoutTestControl : Control {
839 public int LayoutCount;
841 public LayoutTestControl () : base() {
842 LayoutCount = 0;
845 protected override void OnLayout(LayoutEventArgs levent) {
846 LayoutCount++;
847 base.OnLayout (levent);
851 [Test]
852 public void LayoutTest() {
853 LayoutTestControl c;
855 c = new LayoutTestControl();
857 c.SuspendLayout();
858 c.SuspendLayout();
859 c.SuspendLayout();
860 c.SuspendLayout();
862 c.ResumeLayout(true);
863 c.PerformLayout();
864 c.ResumeLayout(true);
865 c.PerformLayout();
866 c.ResumeLayout(true);
867 c.PerformLayout();
868 c.ResumeLayout(true);
869 c.PerformLayout();
870 c.ResumeLayout(true);
871 c.PerformLayout();
872 c.ResumeLayout(true);
873 c.PerformLayout();
874 c.ResumeLayout(true);
875 c.PerformLayout();
876 c.SuspendLayout();
877 c.PerformLayout();
879 Assert.AreEqual(5, c.LayoutCount, "Layout Suspend/Resume locking does not bottom out at 0");
882 [Test]
883 [ExpectedException(typeof(ArgumentException))]
884 public void TransparentBackgroundTest1() {
885 Control c;
887 c = new Control();
888 c.BackColor = Color.Transparent;
891 [Test]
892 public void TransparentBackgroundTest2() {
893 Panel c;
895 c = new Panel();
896 c.BackColor = Color.Transparent;
897 Assert.AreEqual(Color.Transparent, c.BackColor, "Transparent background not set");
900 [Test]
901 public void TransparentBackgroundTest3() {
902 Control c;
904 c = new Control();
905 c.BackColor = Color.Empty;
906 Assert.AreEqual(Control.DefaultBackColor, c.BackColor, "Setting empty color failed");
909 [Test]
910 public void Dock_Value_Invalid ()
912 Control c = new Control ();
913 try {
914 c.Dock = (DockStyle) 666;
915 Assert.Fail ("#1");
916 } catch (InvalidEnumArgumentException ex) {
917 Assert.AreEqual (typeof (InvalidEnumArgumentException), ex.GetType (), "#2");
918 Assert.IsNotNull (ex.Message, "#3");
919 Assert.IsNotNull (ex.ParamName, "#4");
920 Assert.AreEqual ("value", ex.ParamName, "#5");
921 Assert.IsNull (ex.InnerException, "#6");
925 [Test]
926 public void EnabledTest1() {
927 Control child;
928 Control parent;
929 Control grandma;
931 grandma = new Control();
932 parent = new Control();
933 child = new Control();
935 grandma.Controls.Add(parent);
936 parent.Controls.Add(child);
937 grandma.Enabled = false;
938 Assert.AreEqual(grandma.Enabled, child.Enabled, "Child did not inherit disabled state");
941 int EnabledCalledCount = 0;
942 private void EnabledTest2EnabledChanged(object sender, EventArgs e) {
943 EnabledCalledCount++;
946 [Test]
947 public void EnabledTest2() {
948 // Check nesting of enabled calls
949 // OnEnabled is not called for disabled child controls
950 Control child;
951 Control parent;
952 Control grandma;
954 EnabledCalledCount = 0;
956 grandma = new Control();
957 parent = new Control();
958 child = new Control();
959 child.EnabledChanged += new EventHandler(EnabledTest2EnabledChanged);
961 grandma.Controls.Add(parent);
962 parent.Controls.Add(child);
963 grandma.Enabled = false;
965 Assert.AreEqual(1, EnabledCalledCount, "Child Enabled Event not properly fired");
966 grandma.Enabled = true;
967 Assert.AreEqual(2, EnabledCalledCount, "Child Enabled Event not properly fired");
968 child.Enabled = false;
969 grandma.Enabled = false;
970 Assert.AreEqual(3, EnabledCalledCount, "Child Enabled Event not properly fired");
973 [Test]
974 public void ControlsRemoveNullTest ()
976 Control c = new Control ();
977 c.Controls.Remove (null);
980 [Test]
981 public void ControlsAddNullTest ()
983 Control c = new Control ();
984 c.Controls.Add (null);
987 [Test]
988 [ExpectedException (typeof (ArgumentNullException))]
989 public void ControlsSetChildIndexNullTest ()
991 Control c = new Control ();
992 c.Controls.SetChildIndex (null, 1);
995 [Test]
996 [ExpectedException (typeof (ArgumentNullException))]
997 public void ControlsAddRangeNullTest ()
999 Control c = new Control ();
1000 c.Controls.AddRange (null);
1003 [Test]
1004 public void ControlsAddRangeNullElementTest ()
1006 Control c = new Control ();
1007 Control[] subcontrols = new Control[2];
1008 subcontrols[0] = new Control ();
1009 subcontrols[1] = null;
1011 c.Controls.AddRange (subcontrols);
1015 [TestFixture]
1016 public class ControlInvokeTest {
1017 public delegate void TestDelegate ();
1019 Form f;
1020 Control c;
1021 Thread control_t;
1022 ApplicationContext control_context;
1023 bool delegateCalled = false;
1025 object m;
1027 void CreateControl ()
1029 f = new Form ();
1030 f.ShowInTaskbar = false;
1032 c = new Control ();
1034 f.Controls.Add (c);
1036 Console.WriteLine ("f.Handle = {0}", f.Handle);
1037 Console.WriteLine ("c.Handle = {0}", c.Handle);
1039 control_context = new ApplicationContext (f);
1041 Monitor.Enter (m);
1042 Console.WriteLine ("pulsing");
1043 Monitor.Pulse (m);
1044 Monitor.Exit (m);
1045 Console.WriteLine ("control thread running");
1046 Application.Run (control_context);
1047 c.Dispose ();
1050 [Test]
1051 public void InvokeTest ()
1053 m = new object ();
1055 control_t = new Thread(new ThreadStart(CreateControl));
1057 Monitor.Enter (m);
1059 control_t.Start ();
1061 Console.WriteLine ("waiting on monitor");
1062 Monitor.Wait (m);
1064 Console.WriteLine ("making async call");
1066 IAsyncResult result;
1067 result = c.BeginInvoke (new TestDelegate (delegate_call));
1068 c.EndInvoke (result);
1070 Assert.AreEqual (true, delegateCalled, "Invoke1");
1073 public void delegate_call () {
1074 /* invoked on control_context's thread */
1075 delegateCalled = true;
1076 f.Dispose ();
1077 Application.Exit ();