2 // Copyright (c) 2005 Novell, Inc.
5 // Ritvik Mayank (mritvik@novell.com)
6 // Stefan Noack (noackstefan@googlemail.com)
10 using System.Collections;
11 using System.ComponentModel;
13 using System.Reflection;
14 using System.Runtime.Remoting;
15 using System.Threading;
16 using System.Windows.Forms;
18 using System.Windows.Forms.Layout;
21 using NUnit.Framework;
22 using CategoryAttribute = NUnit.Framework.CategoryAttribute;
24 namespace MonoTests.System.Windows.Forms
27 public class ControlTest
32 public void AutoSizeTest ()
34 ControlAutoSizeTester c = new ControlAutoSizeTester (new Size (23, 17), AutoSizeMode.GrowAndShrink);
37 f.Size = new Size (200, 200);
41 Size s = new Size (42, 42);
44 Point l = new Point (10, 10);
47 //Check wether normal size setting is OK
48 Assert.AreEqual (s, c.Size, "#S1");
50 //Check wether size remains without GetPreferredSize implemented even when AutoSize turned on.
53 Assert.AreEqual (s, c.Size, "#S2");
55 //Simulate a Control implementing GetPreferredSize
56 c.UseCustomPrefSize = true;
59 //Check wether size shrinks to preferred size
60 Assert.AreEqual (c.CustomPrefSize, c.Size, "#S3");
61 //Check wether Location stays constant
62 Assert.AreEqual (l, c.Location, "#L1");
64 //Check wether Dock is respected
65 c.Dock = DockStyle.Bottom;
66 Assert.AreEqual (f.ClientSize.Width, c.Width, "#D1");
68 //Check wether size shrinks to preferred size again
69 c.Dock = DockStyle.None;
70 Assert.AreEqual (c.CustomPrefSize, c.Size, "#S4");
72 //Check wether Anchor is respected for adjusting Locatioon
73 c.Anchor = AnchorStyles.Bottom;
75 Assert.AreEqual (l.Y + 50, c.Top, "#A1");
76 //Check wether size is still OK
77 Assert.AreEqual (c.CustomPrefSize, c.Size, "#S5");
81 c.Anchor = AnchorStyles.Top | AnchorStyles.Left;
84 //Check wether shrinking to zero is possible
85 c.CustomPrefSize = new Size (0, 0);
87 Assert.AreEqual (c.CustomPrefSize, c.Size, "#S6");
89 //Check wether MinimumSize is honored
90 c.MinimumSize = new Size (10, 12);
91 c.CustomPrefSize = new Size (5, 5);
93 Assert.AreEqual (c.MinimumSize, c.Size, "#S7");
94 c.MinimumSize = new Size (0, 0);
96 //Check wether MaximumSize is honored
97 c.MaximumSize = new Size (100, 120);
98 c.CustomPrefSize = new Size (500, 500);
100 Assert.AreEqual (c.MaximumSize, c.Size, "#S8");
102 //Check wether shrinking does not happen when GrowOnly
104 s = new Size (23, 23);
106 c.CustomPrefSize = new Size (5, 5);
107 c.AutoSizeMode = AutoSizeMode.GrowOnly;
110 Assert.AreEqual (s, c.Size, "#S9");
114 public class ControlAutoSizeTester : Control {
117 public ControlAutoSizeTester (Size customPrefSize, AutoSizeMode autoSizeMode)
119 custom_prefsize = customPrefSize;
120 AutoSizeMode = autoSizeMode;
123 public AutoSizeMode AutoSizeMode {
125 base.SetAutoSizeMode (value);
129 private bool use_custom_prefsize = false;
130 public bool UseCustomPrefSize {
132 use_custom_prefsize = value;
136 private Size custom_prefsize;
138 public Size CustomPrefSize {
140 return custom_prefsize;
143 custom_prefsize = value;
148 public override Size GetPreferredSize (Size proposedSize)
150 if (use_custom_prefsize) {
151 return custom_prefsize;
153 return base.GetPreferredSize(proposedSize);
161 public void Bug82748 ()
163 Form f = new Form ();
164 f.ShowInTaskbar = false;
166 Control c = new Control ();
167 c.Size = new Size (100, 100);
169 Control c2 = new Control ();
173 c.Anchor = AnchorStyles.Right;
179 Assert.AreEqual (0, c.Left, "A1");
186 public void InvokeTestParentHandle ()
188 Control child, parent;
190 parent = new Control ();
191 child = new Control ();
192 parent.Controls.Add (child);
193 parent.Visible = true;
194 parent.CreateControl ();
195 child.Invoke (new CrossAppDomainDelegate (dummy)) ;
202 public class ControlStylesTester : Control {
203 private WindowStyles style;
204 private WindowStyles ex_style;
205 private bool or_styles;
207 public ControlStylesTester (WindowStyles Style, WindowStyles ExStyle, bool OrStyles)
211 or_styles = OrStyles;
214 protected override CreateParams CreateParams {
216 CreateParams result = base.CreateParams;
218 result.Style |= (int) style;
219 result.ExStyle |= (int) ex_style;
221 result.Style = (int) style;
222 result.ExStyle = (int) ex_style;
230 public void ControlSizeTest ()
232 ControlStylesTester c = new ControlStylesTester (WindowStyles.WS_CHILD, 0, true);
233 Assert.AreEqual ("{X=0,Y=0}", c.Location.ToString (), "#L1");
234 Assert.AreEqual ("{Width=0, Height=0}", c.Size.ToString (), "#S1");
239 public void CaptureTest ()
241 Form frm = new Form ();
242 ControlOverrideLogger log = new ControlOverrideLogger ();
243 frm.Controls.Add (log);
247 log.BackColor = Color.Blue;
248 log.Size = new Size (100, 100);
251 Application.DoEvents ();
254 Assert.IsTrue (log.Log.ToString ().IndexOf ("OnMouseCaptureChanged") > -1, "#01");
256 log = new ControlOverrideLogger ();
259 Assert.IsTrue (log.Log.ToString ().IndexOf ("OnMouseCaptureChanged") > -1, "#02");
261 log = new ControlOverrideLogger ();
263 Assert.IsTrue (log.IsHandleCreated, "#03");
264 Assert.IsTrue (log.Log.ToString ().IndexOf ("OnMouseCaptureChanged") == -1, "#04");
266 log = new ControlOverrideLogger ();
268 Assert.IsFalse (log.IsHandleCreated, "#05");
269 Assert.IsTrue (log.Log.ToString ().IndexOf ("OnMouseCaptureChanged") == -1, "#06");
273 public class OnPaintTester : Form
277 ArrayList list = new ArrayList ();
278 public bool Recursive;
279 public bool TestRefresh;
280 public bool TestInvalidate;
281 public bool TestUpdate;
283 public new bool DoubleBuffered {
285 return base.DoubleBuffered;
288 base.DoubleBuffered = value;
292 protected override void OnPaint (PaintEventArgs pevent)
294 Assert.IsFalse (list.Contains (pevent.Graphics), "OnPaintTester.OnPaint: Got the same Graphics twice");
295 list.Add (pevent.Graphics);
299 Recursive = counter > 0 || Recursive;
304 else if (TestInvalidate)
310 base.OnPaint (pevent);
314 public new void Show ()
317 Application.DoEvents ();
322 public void ControlCollectionTest ()
324 Form frm = new Form ();
325 frm.IsMdiContainer = true;
326 Form child = new Form ();
327 Control.ControlCollection c = new Control.ControlCollection (frm);
328 child.MdiParent = frm;
333 [ExpectedException (typeof (ArgumentException))]
334 public void ControlCollectionExceptionTest ()
336 Form frm = new Form ();
337 frm.IsMdiContainer = true;
338 Form child = new Form ();
339 Control.ControlCollection c = new Control.ControlCollection (frm);
340 //child.MdiParent = frm;
345 [Category ("NotWorking")]
346 public void OnPaintTest ()
348 using (OnPaintTester t = new OnPaintTester ()) {
349 t.TestRefresh = true;
351 Assert.IsTrue (t.Recursive, "#1");
354 using (OnPaintTester t = new OnPaintTester ()) {
357 Assert.IsFalse (t.Recursive, "#2");
360 using (OnPaintTester t = new OnPaintTester ()) {
361 t.TestInvalidate = true;
363 Assert.IsFalse (t.Recursive, "#3");
368 [Category ("Interactive")]
369 public void OnPaintDoubleBufferedTest ()
371 using (OnPaintTester t = new OnPaintTester ()) {
372 t.DoubleBuffered = true;
373 t.TestRefresh = true;
375 Assert.IsTrue (t.Recursive, "#1");
378 using (OnPaintTester t = new OnPaintTester ()) {
379 t.DoubleBuffered = true;
382 Assert.IsFalse (t.Recursive, "#2");
385 using (OnPaintTester t = new OnPaintTester ()) {
386 t.DoubleBuffered = true;
387 t.TestInvalidate = true;
389 Assert.IsFalse (t.Recursive, "#3");
393 public class PaintEventForm : Form
395 public ArrayList overrides = new ArrayList ();
396 public ArrayList events = new ArrayList ();
398 public PaintEventForm ()
400 Paint += new PaintEventHandler (DoubleBufferEventForm_Paint);
402 public bool GetControlStyle (ControlStyles style)
404 return base.GetStyle (style);
407 public void SetControlStyle (ControlStyles style, bool value)
409 base.SetStyle (style, value);
412 void DoubleBufferEventForm_Paint (object sender, PaintEventArgs e)
417 protected override void OnPaintBackground (PaintEventArgs e)
419 base.OnPaintBackground (e);
420 overrides.Add("OnPaintBackground");
423 protected override void OnPaint (PaintEventArgs pevent)
425 base.OnPaint (pevent);
426 overrides.Add("OnPaint");
431 [Ignore ("Can't find a reliable way to generate a paint message on Windows.")]
432 public void EventStyleTest ()
435 using (PaintEventForm f = new PaintEventForm ()) {
437 f.SetControlStyle (ControlStyles.OptimizedDoubleBuffer, true);
439 Assert.IsTrue (f.overrides.Contains ("OnPaintBackground"), "#A1");
440 Assert.IsTrue (f.overrides.Contains ("OnPaint"), "#A2");
441 Assert.IsTrue (f.events.Contains ("Paint"), "#A3");
444 using (PaintEventForm f = new PaintEventForm ()) {
446 f.SetControlStyle (ControlStyles.DoubleBuffer, true);
448 Assert.IsTrue (f.overrides.Contains ("OnPaintBackground"), "#B1");
449 Assert.IsTrue (f.overrides.Contains ("OnPaint"), "#B2");
450 Assert.IsTrue (f.events.Contains ("Paint"), "#B3");
453 using (PaintEventForm f = new PaintEventForm ()) {
455 f.SetControlStyle (ControlStyles.AllPaintingInWmPaint, true);
457 Assert.IsTrue (f.overrides.Contains ("OnPaintBackground"), "#C1");
458 Assert.IsTrue (f.overrides.Contains ("OnPaint"), "#C2");
459 Assert.IsTrue (f.events.Contains ("Paint"), "#C3");
462 using (PaintEventForm f = new PaintEventForm ()) {
464 f.SetControlStyle (ControlStyles.UserPaint, true);
466 Assert.IsTrue (f.overrides.Contains ("OnPaintBackground"), "#D1");
467 Assert.IsTrue (f.overrides.Contains ("OnPaint"), "#D2");
468 Assert.IsTrue (f.events.Contains ("Paint"), "#D3");
471 using (PaintEventForm f = new PaintEventForm ()) {
473 f.SetControlStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
475 Assert.IsTrue (f.overrides.Contains ("OnPaintBackground"), "#E1");
476 Assert.IsTrue (f.overrides.Contains ("OnPaint"), "#E2");
477 Assert.IsTrue (f.events.Contains ("Paint"), "#E3");
480 using (PaintEventForm f = new PaintEventForm ()) {
482 f.SetControlStyle (ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);
484 Assert.IsTrue (f.overrides.Contains ("OnPaintBackground"), "#F1");
485 Assert.IsTrue (f.overrides.Contains ("OnPaint"), "#F2");
486 Assert.IsTrue (f.events.Contains ("Paint"), "#F3");
489 using (PaintEventForm f = new PaintEventForm ()) {
491 f.SetControlStyle (ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
493 Assert.IsTrue (f.overrides.Contains ("OnPaintBackground"), "#G1");
494 Assert.IsTrue (f.overrides.Contains ("OnPaint"), "#G2");
495 Assert.IsTrue (f.events.Contains ("Paint"), "#G3");
498 using (PaintEventForm f = new PaintEventForm ()) {
500 f.SetControlStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer, true);
502 Assert.IsTrue (f.overrides.Contains ("OnPaintBackground"), "#H1");
503 Assert.IsTrue (f.overrides.Contains ("OnPaint"), "#H2");
504 Assert.IsTrue (f.events.Contains ("Paint"), "#H3");
508 public class DoubleBufferedForm : Form
512 public DoubleBufferedForm ()
514 this.DoubleBuffered = true;
517 protected override void OnPaint (PaintEventArgs e)
519 if (failed || painted)
524 e.Graphics.DrawString (Size.ToString (), Font, Brushes.AliceBlue, new Point (2, 2));
525 } catch (Exception exception) {
526 Console.WriteLine (exception.StackTrace);
532 public class DoubleBufferControl : Control
534 public bool IsDoubleBuffered
536 get { return base.DoubleBuffered; }
537 set { base.DoubleBuffered = value; }
540 public bool GetControlStyle (ControlStyles style)
542 return base.GetStyle (style);
545 public void SetControlStyle (ControlStyles style, bool value)
547 base.SetStyle (style, value);
552 [Ignore ("Can't find a reliable way to generate a paint message on Windows.")]
553 public void DoubleBufferTest ()
555 DoubleBufferedForm f = new DoubleBufferedForm ();
556 f.ShowInTaskbar = false;
560 Assert.IsFalse (f.failed, "#01");
561 Assert.IsTrue (f.painted, "The control was never painted, so please check the test");
566 public void DoubleBufferedTest ()
568 DoubleBufferControl c = new DoubleBufferControl ();
569 Assert.IsFalse (c.IsDoubleBuffered, "#A1");
570 Assert.IsTrue (c.GetControlStyle (ControlStyles.AllPaintingInWmPaint), "#A2");
571 Assert.IsFalse (c.GetControlStyle (ControlStyles.DoubleBuffer), "#A3");
572 Assert.IsFalse (c.GetControlStyle (ControlStyles.OptimizedDoubleBuffer), "#A4");
573 Assert.IsTrue (c.GetControlStyle (ControlStyles.UserPaint), "#A5");
575 c.SetControlStyle (ControlStyles.OptimizedDoubleBuffer, true);
576 Assert.IsTrue (c.IsDoubleBuffered, "#B1");
577 Assert.IsTrue (c.GetControlStyle (ControlStyles.AllPaintingInWmPaint), "#B2");
578 Assert.IsFalse (c.GetControlStyle (ControlStyles.DoubleBuffer), "#B3");
579 Assert.IsTrue (c.GetControlStyle (ControlStyles.OptimizedDoubleBuffer), "#B4");
580 Assert.IsTrue (c.GetControlStyle (ControlStyles.UserPaint), "#A5");
582 c.SetControlStyle (ControlStyles.AllPaintingInWmPaint, false);
583 c.SetControlStyle (ControlStyles.UserPaint, false);
584 Assert.IsTrue (c.IsDoubleBuffered, "#C1");
585 Assert.IsFalse (c.GetControlStyle (ControlStyles.AllPaintingInWmPaint), "#C2");
586 Assert.IsFalse (c.GetControlStyle (ControlStyles.DoubleBuffer), "#C3");
587 Assert.IsTrue (c.GetControlStyle (ControlStyles.OptimizedDoubleBuffer), "#C4");
588 Assert.IsFalse (c.GetControlStyle (ControlStyles.UserPaint), "#C5");
590 c.SetControlStyle (ControlStyles.OptimizedDoubleBuffer, false);
591 Assert.IsFalse (c.IsDoubleBuffered, "#D1");
592 Assert.IsFalse (c.GetControlStyle (ControlStyles.AllPaintingInWmPaint), "#D2");
593 Assert.IsFalse (c.GetControlStyle (ControlStyles.DoubleBuffer), "#D3");
594 Assert.IsFalse (c.GetControlStyle (ControlStyles.OptimizedDoubleBuffer), "#D4");
595 Assert.IsFalse (c.GetControlStyle (ControlStyles.UserPaint), "#D5");
597 c.SetControlStyle (ControlStyles.DoubleBuffer, true);
598 Assert.IsFalse (c.IsDoubleBuffered, "#E1");
599 Assert.IsFalse (c.GetControlStyle (ControlStyles.AllPaintingInWmPaint), "#E2");
600 Assert.IsTrue (c.GetControlStyle (ControlStyles.DoubleBuffer), "#E3");
601 Assert.IsFalse (c.GetControlStyle (ControlStyles.OptimizedDoubleBuffer), "#E4");
602 Assert.IsFalse (c.GetControlStyle (ControlStyles.UserPaint), "#E5");
604 c.SetControlStyle (ControlStyles.DoubleBuffer, false);
605 Assert.IsFalse (c.IsDoubleBuffered, "#F1");
606 Assert.IsFalse (c.GetControlStyle (ControlStyles.AllPaintingInWmPaint), "#F2");
607 Assert.IsFalse (c.GetControlStyle (ControlStyles.DoubleBuffer), "#F3");
608 Assert.IsFalse (c.GetControlStyle (ControlStyles.OptimizedDoubleBuffer), "#F4");
609 Assert.IsFalse (c.GetControlStyle (ControlStyles.UserPaint), "#F5");
611 c.IsDoubleBuffered = true;
612 Assert.IsTrue (c.IsDoubleBuffered, "#G1");
613 Assert.IsTrue (c.GetControlStyle (ControlStyles.AllPaintingInWmPaint), "#G2");
614 Assert.IsFalse (c.GetControlStyle (ControlStyles.DoubleBuffer), "#G3");
615 Assert.IsTrue (c.GetControlStyle (ControlStyles.OptimizedDoubleBuffer), "#G4");
616 Assert.IsFalse (c.GetControlStyle (ControlStyles.UserPaint), "#G5");
618 c.SetControlStyle (ControlStyles.AllPaintingInWmPaint, true);
619 c.SetControlStyle (ControlStyles.OptimizedDoubleBuffer, true);
620 c.SetControlStyle (ControlStyles.DoubleBuffer, true);
621 c.SetControlStyle (ControlStyles.UserPaint, true);
622 c.IsDoubleBuffered = false;
623 Assert.IsFalse (c.IsDoubleBuffered, "#H1");
624 Assert.IsTrue (c.GetControlStyle (ControlStyles.AllPaintingInWmPaint), "#H2");
625 Assert.IsTrue (c.GetControlStyle (ControlStyles.DoubleBuffer), "#H3");
626 Assert.IsFalse (c.GetControlStyle (ControlStyles.OptimizedDoubleBuffer), "#H4");
627 Assert.IsTrue (c.GetControlStyle (ControlStyles.UserPaint), "#H5");
631 public void DoubleBufferedStyleTest ()
633 DoubleBufferControl c = new DoubleBufferControl ();
634 TestControlStyle.CheckStyles (c, "#A1", ControlStyles.UserPaint, ControlStyles.StandardClick, ControlStyles.Selectable, ControlStyles.StandardDoubleClick, ControlStyles.AllPaintingInWmPaint, ControlStyles.UseTextForAccessibility);
636 c.IsDoubleBuffered = true;
637 TestControlStyle.CheckStyles (c, "#A2", ControlStyles.UserPaint, ControlStyles.StandardClick, ControlStyles.Selectable, ControlStyles.StandardDoubleClick, ControlStyles.AllPaintingInWmPaint, ControlStyles.UseTextForAccessibility, ControlStyles.OptimizedDoubleBuffer);
639 c.IsDoubleBuffered = false;
640 TestControlStyle.CheckStyles (c, "#A3", ControlStyles.UserPaint, ControlStyles.StandardClick, ControlStyles.Selectable, ControlStyles.StandardDoubleClick, ControlStyles.AllPaintingInWmPaint, ControlStyles.UseTextForAccessibility);
642 c = new DoubleBufferControl ();
643 foreach (ControlStyles style in Enum.GetValues (typeof(ControlStyles))) {
644 c.SetControlStyle (style, false);
647 TestControlStyle.CheckStyles (c, "#B1");
649 c.IsDoubleBuffered = true;
650 TestControlStyle.CheckStyles (c, "#B2", ControlStyles.OptimizedDoubleBuffer, ControlStyles.AllPaintingInWmPaint);
652 c.IsDoubleBuffered = false;
653 TestControlStyle.CheckStyles (c, "#B3", ControlStyles.AllPaintingInWmPaint);
659 public static void TestAccessibility(Control c, string Default, string Description, string Name, AccessibleRole Role)
661 Assert.IsNotNull (c.AccessibilityObject, "Acc1");
662 Assert.AreEqual (Default, c.AccessibleDefaultActionDescription, "Acc2");
663 Assert.AreEqual (Description, c.AccessibleDescription, "Acc3");
664 Assert.AreEqual (Name, c.AccessibleName, "Acc4");
665 Assert.AreEqual (Role, c.AccessibleRole, "Acc5");
668 public static string TestControl(Control container, Control start, bool forward) {
671 ctl = container.GetNextControl(start, forward);
682 public void CreatedTest ()
684 Control c = new Control ();
685 Assert.IsFalse (c.Created, "A1");
689 [Category ("NotWorking")]
690 public void CreatedAccessibilityTest ()
692 Control c = new Control ();
693 Assert.IsFalse (c.Created, "A1");
695 Helper.TestAccessibility(c, null, null, null, AccessibleRole.Default);
697 Assert.IsTrue (c.Created, "A2");
701 Assert.IsFalse (c.Created, "A3");
705 [Category ("NotWorking")]
706 public void BoundsTest ()
708 Control c = new Control ();
709 Assert.IsTrue (c.Bounds.IsEmpty, "A1");
710 Assert.IsTrue (c.Size.IsEmpty, "A2");
711 Assert.IsTrue (c.ClientSize.IsEmpty, "A3");
712 Assert.IsTrue (c.ClientRectangle.IsEmpty, "A4");
714 Assert.AreEqual (((IWin32Window)c).Handle, c.Handle, "A5");
716 /* this part fails on linux because we can't allocate X windows which are 0x0,
717 and the Control bounds directly reflect the size of the X window */
719 Assert.IsTrue (c.Bounds.IsEmpty, "A6");
720 Assert.IsTrue (c.Size.IsEmpty, "A7");
721 Assert.IsTrue (c.ClientSize.IsEmpty, "A8");
722 Assert.IsTrue (c.ClientRectangle.IsEmpty, "A9");
726 [Ignore ("Depends on specific DPI")]
727 public void FontHeightTest ()
729 MockControl c = new MockControl ();
730 Assert.AreEqual (13, c.font_height);
734 public void FontTest ()
736 Control c = new Control ();
737 Assert.IsFalse (c.Font.Bold, "#A1");
738 //Assert.AreEqual ("Microsoft Sans Serif", c.Font.FontFamily.Name, "#A2");
739 Assert.IsFalse (c.Font.Italic, "#A3");
740 //Assert.AreEqual ("Microsoft Sans Serif", c.Font.Name, "#A4");
741 Assert.AreEqual (8.25, c.Font.Size, "#A5");
742 Assert.AreEqual (8.25, c.Font.SizeInPoints, "#A6");
743 Assert.IsFalse (c.Font.Strikeout, "#A7");
744 Assert.IsFalse (c.Font.Underline, "#A8");
745 Assert.AreEqual (GraphicsUnit.Point, c.Font.Unit, "#A9");
747 Assert.IsTrue (c.Font.IsSystemFont, "#A10");
750 c.Font = new Font (c.Font.FontFamily, 3, FontStyle.Italic);
751 Assert.IsFalse (c.Font.Bold, "#B1");
752 //Assert.AreEqual ("Microsoft Sans Serif", c.Font.FontFamily.Name, "#B2");
753 Assert.IsTrue (c.Font.Italic, "#B3");
754 //Assert.AreEqual ("Microsoft Sans Serif", c.Font.Name, "#B4");
755 Assert.AreEqual (3, c.Font.Size, "#B5");
756 Assert.AreEqual (3, c.Font.SizeInPoints, "#B6");
757 Assert.IsFalse (c.Font.Strikeout, "#B7");
758 Assert.IsFalse (c.Font.Underline, "#B8");
759 Assert.AreEqual (GraphicsUnit.Point, c.Font.Unit, "#B9");
761 Assert.AreEqual (false, c.Font.IsSystemFont, "#B10");
766 [Category ("NotWorking")] // on Unix mapping is done to Bitstream Vera Sans
767 public void FontTest_Names ()
769 Control c = new Control ();
770 Assert.AreEqual ("Microsoft Sans Serif", c.Font.FontFamily.Name, "#1");
771 Assert.AreEqual ("Microsoft Sans Serif", c.Font.Name, "#2");
775 public void PubPropTest()
777 Control c = new Control();
779 Assert.IsFalse (c.AllowDrop , "A1");
780 Assert.AreEqual(AnchorStyles.Top | AnchorStyles.Left, c.Anchor, "A2");
782 Assert.AreEqual ("Control", c.BackColor.Name , "B1");
783 Assert.IsNull (c.BackgroundImage, "B2");
784 Assert.IsNull (c.BindingContext, "B3");
786 Assert.AreEqual (ImageLayout.Tile, c.BackgroundImageLayout, "B4");
789 Assert.IsFalse (c.CanFocus, "C1");
790 Assert.IsTrue (c.CanSelect, "C2");
791 Assert.IsFalse (c.Capture, "C3");
792 Assert.IsTrue (c.CausesValidation, "C4");
794 Assert.IsNotNull (c.CompanyName, "C7");
795 Assert.IsNull (c.Container, "C8");
796 Assert.IsFalse (c.ContainsFocus, "C9");
797 Assert.IsNull (c.ContextMenu, "C10");
798 Assert.AreEqual (0, c.Controls.Count, "C11");
799 Assert.IsFalse (c.Created, "C12");
800 Assert.AreEqual (Cursors.Default, c.Cursor, "C13");
802 Assert.IsNotNull(c.DataBindings, "D1");
803 Assert.AreEqual("Control", Control.DefaultBackColor.Name, "D2");
804 Assert.AreEqual("ControlText", Control.DefaultForeColor.Name, "D3");
805 Assert.AreEqual(FontStyle.Regular, Control.DefaultFont.Style, "D4");
806 Assert.AreEqual (new Rectangle(0, 0, 0, 0), c.DisplayRectangle , "D5");
807 Assert.IsFalse (c.Disposing, "D6");
808 Assert.AreEqual(DockStyle.None, c.Dock, "D7");
810 Assert.IsTrue (c.Enabled, "E1");
812 Assert.IsFalse (c.Focused, "F1");
813 Assert.AreEqual (FontStyle.Regular, c.Font.Style, "F2");
814 Assert.AreEqual (SystemColors.ControlText, c.ForeColor, "F3");
816 Assert.IsFalse (c.HasChildren, "H2");
818 Assert.AreEqual (ImeMode.NoControl, c.ImeMode, "I1");
819 Assert.IsFalse (c.InvokeRequired, "I2");
820 Assert.IsFalse (c.IsAccessible, "I3");
821 Assert.IsFalse (c.IsDisposed, "I4");
822 Assert.IsFalse (c.IsHandleCreated, "I5");
824 Assert.AreEqual(Point.Empty, c.Location, "L2");
827 Assert.IsTrue(c.MaximumSize.IsEmpty);
828 Assert.IsTrue(c.MinimumSize.IsEmpty);
830 Assert.AreEqual (Keys.None, Control.ModifierKeys, "M1");
831 Assert.IsTrue (Control.MousePosition.X >= 0 && Control.MousePosition.Y >= 0, "M2");
832 Assert.AreEqual (MouseButtons.None, Control.MouseButtons, "M3");
834 Assert.AreEqual("", c.Name, "N1");
835 c.Name = "Control Name";
836 Assert.AreEqual("Control Name", c.Name, "N2");
838 Assert.IsNull (c.Parent, "P1");
839 Assert.IsNotNull (c.ProductName, "P2");
840 Assert.IsTrue (c.ProductName != "", "P3");
841 Assert.IsNotNull (c.ProductVersion, "P4");
842 Assert.IsTrue (c.ProductVersion != "", "P5");
844 Assert.IsFalse (c.RecreatingHandle, "R1");
845 Assert.IsNull (c.Region, "R2");
846 Assert.AreEqual (RightToLeft.No, c.RightToLeft, "R4");
848 Assert.IsNull (c.Site, "S1");
850 Assert.AreEqual (0, c.TabIndex , "T1");
851 Assert.IsTrue (c.TabStop, "T2");
852 Assert.IsNull (c.Tag, "T3");
853 Assert.AreEqual ("", c.Text, "T4");
855 Assert.IsTrue (c.Visible, "V1");
859 public void SizeChangeTest ()
861 Form f = new Form ();
862 Control c = new Control ();
865 c.Resize += new EventHandler(SizeChangedTest_ResizeHandler);
868 Assert.AreEqual (true, (bool) c.Tag, "#1");
872 private void SizeChangedTest_ResizeHandler (object sender, EventArgs e)
874 ((Control) sender).Tag = false;
878 public void NegativeHeightTest ()
880 Control c = new Control ();
881 IntPtr handle = c.Handle;
882 c.Resize += new EventHandler(NegativeHeightTest_ResizeHandler);
886 Assert.AreEqual (0, (int) c.Tag, "#1");
888 Assert.AreEqual (handle, handle, "Removes warning.");
891 private void NegativeHeightTest_ResizeHandler (object sender, EventArgs e)
893 Control c = (Control) sender;
898 public void TopLevelControlTest () {
899 Control c = new Control ();
901 Assert.AreEqual(null, c.TopLevelControl, "T1");
903 Panel p = new Panel ();
907 Assert.AreEqual(null, c.TopLevelControl, "T2");
909 Form f = new Form ();
910 f.ShowInTaskbar = false;
914 Assert.AreEqual (f, c.TopLevelControl, "T3");
915 Assert.AreEqual (f, f.TopLevelControl, "T4");
919 public void RelationTest() {
926 Assert.AreEqual(true , c1.Visible , "Rel1");
927 Assert.AreEqual(false, c1.Contains(c2) , "Rel2");
928 Assert.AreEqual("System.Windows.Forms.Control", c1.ToString() , "Rel3");
931 Assert.AreEqual(true , c2.Visible , "Rel4");
932 Assert.AreEqual(true, c1.Contains(c2) , "Rel5");
934 c1.Anchor = AnchorStyles.Top;
936 c1.Anchor = AnchorStyles.Left ;
938 Assert.AreEqual(AnchorStyles.Left , c1.Anchor, "Rel6");
940 c1.SetBounds(10, 20, 30, 40) ;
941 Assert.AreEqual(new Rectangle(10, 20, 30, 40), c1.Bounds, "Rel7");
943 Assert.AreEqual(c1, c2.Parent, "Rel8");
947 public void AnchorDockTest ()
949 Control c = new Control ();
951 Assert.AreEqual (DockStyle.None, c.Dock, "1");
952 Assert.AreEqual (AnchorStyles.Top | AnchorStyles.Left, c.Anchor, "2");
954 c.Dock = DockStyle.Top;
955 Assert.AreEqual (DockStyle.Top, c.Dock, "3");
956 Assert.AreEqual (AnchorStyles.Top | AnchorStyles.Left, c.Anchor, "4");
958 c.Anchor = AnchorStyles.Top;
959 Assert.AreEqual (DockStyle.None, c.Dock, "5");
960 Assert.AreEqual (AnchorStyles.Top, c.Anchor, "6");
964 [Category ("NotWorking")]
965 public void TabOrder()
970 Label label1 = new Label(); // To test non-tabstop items as well
971 Label label2 = new Label();
973 GroupBox group1 = new GroupBox();
974 GroupBox group2 = new GroupBox();
975 GroupBox group3 = new GroupBox();
977 TextBox text1 = new TextBox();
979 RadioButton radio11 = new RadioButton();
980 RadioButton radio12 = new RadioButton();
981 RadioButton radio13 = new RadioButton();
982 RadioButton radio14 = new RadioButton();
983 RadioButton radio21 = new RadioButton();
984 RadioButton radio22 = new RadioButton();
985 RadioButton radio23 = new RadioButton();
986 RadioButton radio24 = new RadioButton();
987 RadioButton radio31 = new RadioButton();
988 RadioButton radio32 = new RadioButton();
989 RadioButton radio33 = new RadioButton();
990 RadioButton radio34 = new RadioButton();
993 form.ShowInTaskbar = false;
995 form.ClientSize = new Size (520, 520);
996 Assert.AreEqual(new Size(520, 520), form.ClientSize, "Tab1");
998 form.Text = "SWF Taborder Test App Form";
999 Assert.AreEqual("SWF Taborder Test App Form", form.Text, "Tab2");
1001 label1.Location = new Point(10, 10);
1002 Assert.AreEqual(new Point(10, 10), label1.Location, "Tab3");
1003 label1.Text = "Label1";
1004 form.Controls.Add(label1);
1006 label2.Location = new Point(200, 10);
1007 label2.Text = "Label2";
1008 form.Controls.Add(label2);
1010 group1.Text = "Group1";
1011 group2.Text = "Group2";
1012 group3.Text = "Group3";
1014 group1.Size = new Size(200, 400);
1015 group2.Size = new Size(200, 400);
1016 group3.Size = new Size(180, 180);
1017 Assert.AreEqual(new Size(180, 180), group3.Size, "Tab4");
1019 group1.Location = new Point(10, 40);
1020 group2.Location = new Point(220, 40);
1021 group3.Location = new Point(10, 210);
1023 group1.TabIndex = 30;
1024 Assert.AreEqual(30, group1.TabIndex, "Tab5");
1025 group1.TabStop = true;
1027 // Don't assign, test automatic assignment
1028 //group2.TabIndex = 0;
1029 group2.TabStop = true;
1030 Assert.AreEqual(0, group2.TabIndex, "Tab6");
1032 group3.TabIndex = 35;
1033 group3.TabStop = true;
1035 // Test default tab index
1036 Assert.AreEqual(0, radio11.TabIndex, "Tab7");
1038 text1.Text = "Edit Control";
1040 radio11.Text = "Radio 1-1 [Tab1]";
1041 radio12.Text = "Radio 1-2 [Tab2]";
1042 radio13.Text = "Radio 1-3 [Tab3]";
1043 radio14.Text = "Radio 1-4 [Tab4]";
1045 radio21.Text = "Radio 2-1 [Tab4]";
1046 radio22.Text = "Radio 2-2 [Tab3]";
1047 radio23.Text = "Radio 2-3 [Tab2]";
1048 radio24.Text = "Radio 2-4 [Tab1]";
1050 radio31.Text = "Radio 3-1 [Tab1]";
1051 radio32.Text = "Radio 3-2 [Tab3]";
1052 radio33.Text = "Radio 3-3 [Tab2]";
1053 radio34.Text = "Radio 3-4 [Tab4]";
1055 // We don't assign TabIndex for radio1X; test automatic assignment
1056 text1.TabStop = true;
1057 radio11.TabStop = true;
1059 radio21.TabIndex = 4;
1060 radio22.TabIndex = 3;
1061 radio23.TabIndex = 2;
1062 radio24.TabIndex = 1;
1063 radio24.TabStop = true;
1065 radio31.TabIndex = 11;
1066 radio31.TabStop = true;
1067 radio32.TabIndex = 13;
1068 radio33.TabIndex = 12;
1069 radio34.TabIndex = 14;
1071 text1.Location = new Point(10, 100);
1073 radio11.Location = new Point(10, 20);
1074 radio12.Location = new Point(10, 40);
1075 radio13.Location = new Point(10, 60);
1076 radio14.Location = new Point(10, 80);
1078 radio21.Location = new Point(10, 20);
1079 radio22.Location = new Point(10, 40);
1080 radio23.Location = new Point(10, 60);
1081 radio24.Location = new Point(10, 80);
1083 radio31.Location = new Point(10, 20);
1084 radio32.Location = new Point(10, 40);
1085 radio33.Location = new Point(10, 60);
1086 radio34.Location = new Point(10, 80);
1088 text1.Size = new Size(150, text1.PreferredHeight);
1090 radio11.Size = new Size(150, 20);
1091 radio12.Size = new Size(150, 20);
1092 radio13.Size = new Size(150, 20);
1093 radio14.Size = new Size(150, 20);
1095 radio21.Size = new Size(150, 20);
1096 radio22.Size = new Size(150, 20);
1097 radio23.Size = new Size(150, 20);
1098 radio24.Size = new Size(150, 20);
1100 radio31.Size = new Size(150, 20);
1101 radio32.Size = new Size(150, 20);
1102 radio33.Size = new Size(150, 20);
1103 radio34.Size = new Size(150, 20);
1105 group1.Controls.Add(text1);
1107 group1.Controls.Add(radio11);
1108 group1.Controls.Add(radio12);
1109 group1.Controls.Add(radio13);
1110 group1.Controls.Add(radio14);
1112 group2.Controls.Add(radio21);
1113 group2.Controls.Add(radio22);
1114 group2.Controls.Add(radio23);
1115 group2.Controls.Add(radio24);
1117 group3.Controls.Add(radio31);
1118 group3.Controls.Add(radio32);
1119 group3.Controls.Add(radio33);
1120 group3.Controls.Add(radio34);
1122 form.Controls.Add(group1);
1123 form.Controls.Add(group2);
1124 group2.Controls.Add(group3);
1126 // Perform some tests, the TabIndex stuff below will alter the outcome
1127 Assert.AreEqual(null, Helper.TestControl(group2, radio34, true), "Tab8");
1128 Assert.AreEqual(31, group2.TabIndex, "Tab9");
1130 // Does the taborder of containers and non-selectable things change behaviour?
1131 label1.TabIndex = 5;
1132 label2.TabIndex = 4;
1133 group1.TabIndex = 3;
1134 group2.TabIndex = 1;
1136 // Start verification
1137 Assert.AreEqual(null, Helper.TestControl(group2, radio34, true), "Tab10");
1138 Assert.AreEqual(radio24.Text, Helper.TestControl(group2, group2, true), "Tab11");
1139 Assert.AreEqual(radio31.Text, Helper.TestControl(group2, group3, true), "Tab12");
1140 Assert.AreEqual(null, Helper.TestControl(group1, radio14, true), "Tab13");
1141 Assert.AreEqual(radio23.Text, Helper.TestControl(group2, radio24, true), "Tab14");
1142 Assert.AreEqual(group3.Text, Helper.TestControl(group2, radio21, true), "Tab15");
1143 Assert.AreEqual(radio13.Text, Helper.TestControl(form, radio12, true), "Tab16");
1144 Assert.AreEqual(label2.Text, Helper.TestControl(form, radio14, true), "Tab17");
1145 Assert.AreEqual(group1.Text, Helper.TestControl(form, radio34, true), "Tab18");
1146 Assert.AreEqual(radio23.Text, Helper.TestControl(group2, radio24, true), "Tab19");
1149 Assert.AreEqual(null, Helper.TestControl(radio11, radio21, true), "Tab20");
1150 Assert.AreEqual(text1.Text, Helper.TestControl(group1, radio21, true), "Tab21");
1152 Assert.AreEqual(radio14.Text, Helper.TestControl(form, label2, false), "Tab22");
1153 Assert.AreEqual(radio21.Text, Helper.TestControl(group2, group3, false), "Tab23");
1155 Assert.AreEqual(4, radio21.TabIndex, "Tab24");
1156 Assert.AreEqual(1, radio11.TabIndex, "Tab25");
1157 Assert.AreEqual(3, radio13.TabIndex, "Tab26");
1158 Assert.AreEqual(35, group3.TabIndex, "Tab27");
1159 Assert.AreEqual(1, group2.TabIndex, "Tab28");
1161 Assert.AreEqual(label1.Text, Helper.TestControl(form, form, false), "Tab29");
1162 Assert.AreEqual(radio14.Text, Helper.TestControl(group1, group1, false), "Tab30");
1163 Assert.AreEqual(radio34.Text, Helper.TestControl(group3, group3, false), "Tab31");
1165 Assert.AreEqual(null, Helper.TestControl(label1, label1, false), "Tab31");
1166 Assert.AreEqual(null, Helper.TestControl(radio11, radio21, false), "Tab32");
1171 public void ScaleTest()
1173 Control r1 = new Control();
1178 Assert.AreEqual(80, r1.Width, "Scale1");
1179 Assert.AreEqual(40, r1.Height, "Scale2");
1184 public void ScaleChildrenTest ()
1186 ScaleChildrenControl c = new ScaleChildrenControl ();
1187 Assert.AreEqual (true, c.PublicScaleChildren, "A1");
1190 private class ScaleChildrenControl : Control
1192 public bool PublicScaleChildren {
1193 get { return base.ScaleChildren; }
1198 public void ScaleControlTest ()
1200 ScaleControl c = new ScaleControl ();
1202 c.Location = new Point (5, 10);
1203 c.Size = new Size (15, 20);
1205 Assert.AreEqual (new Rectangle (5, 10, 15, 20), c.Bounds, "A1");
1207 c.PublicScaleControl (new SizeF (1.5f, 1.3f), BoundsSpecified.All);
1208 Assert.AreEqual (new Rectangle (8, 13, 22, 26), c.Bounds, "A2");
1210 c.PublicScaleControl (new SizeF (2f, 1.5f), BoundsSpecified.Location);
1211 Assert.AreEqual (new Rectangle (16, 20, 22, 26), c.Bounds, "A3");
1213 c.PublicScaleControl (new SizeF (1.5f, 2f), BoundsSpecified.Size);
1214 Assert.AreEqual (new Rectangle (16, 20, 33, 52), c.Bounds, "A4");
1216 c.PublicScaleControl (new SizeF (1.5f, 1.5f), BoundsSpecified.Width);
1217 Assert.AreEqual (new Rectangle (16, 20, 50, 52), c.Bounds, "A5");
1219 c.PublicScaleControl (new SizeF (1.5f, 1.3f), BoundsSpecified.None);
1220 Assert.AreEqual (new Rectangle (16, 20, 50, 52), c.Bounds, "A6");
1222 // Test with ScaleChildren
1223 c = new ScaleControl ();
1225 c.Location = new Point (5, 10);
1226 c.Size = new Size (50, 50);
1228 Control c2 = new Control ();
1229 c2.Location = new Point (15, 15);
1230 c2.Size = new Size (25, 25);
1231 c.Controls.Add (c2);
1233 Assert.AreEqual (new Rectangle (5, 10, 50, 50), c.Bounds, "B1");
1234 Assert.AreEqual (new Rectangle (15, 15, 25, 25), c2.Bounds, "B2");
1236 c.scale_children = false;
1238 c.PublicScaleControl (new SizeF (2f, 2f), BoundsSpecified.All);
1239 Assert.AreEqual (new Rectangle (10, 20, 100, 100), c.Bounds, "B3");
1240 Assert.AreEqual (new Rectangle (15, 15, 25, 25), c2.Bounds, "B4");
1242 c.scale_children = true;
1244 // Will not scale children in ScaleControl
1245 c.PublicScaleControl (new SizeF (2f, 2f), BoundsSpecified.All);
1246 Assert.AreEqual (new Rectangle (20, 40, 200, 200), c.Bounds, "B5");
1247 Assert.AreEqual (new Rectangle (15, 15, 25, 25), c2.Bounds, "B6");
1249 // Does scale children in Scale
1250 c.Scale (new SizeF (2f, 2f));
1251 Assert.AreEqual (new Rectangle (40, 80, 400, 400), c.Bounds, "B7");
1252 Assert.AreEqual (new Rectangle (30, 30, 50, 50), c2.Bounds, "B8");
1256 public void GetScaledBoundsTest ()
1258 ScaleControl c = new ScaleControl ();
1260 Rectangle r = new Rectangle (10, 20, 30, 40);
1262 Assert.AreEqual (new Rectangle (20, 10, 60, 20), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.All), "A1");
1263 Assert.AreEqual (new Rectangle (20, 10, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Location), "A2");
1264 Assert.AreEqual (new Rectangle (10, 20, 60, 20), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Size), "A3");
1265 Assert.AreEqual (new Rectangle (10, 20, 30, 20), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Height), "A4");
1266 Assert.AreEqual (new Rectangle (20, 20, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.X), "A5");
1267 Assert.AreEqual (new Rectangle (10, 20, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.None), "A6");
1269 c.PublicSetTopLevel (true);
1271 Assert.AreEqual (new Rectangle (10, 20, 60, 20), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.All), "A7");
1272 Assert.AreEqual (new Rectangle (10, 20, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Location), "A8");
1273 Assert.AreEqual (new Rectangle (10, 20, 60, 20), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Size), "A9");
1274 Assert.AreEqual (new Rectangle (10, 20, 30, 20), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Height), "A10");
1275 Assert.AreEqual (new Rectangle (10, 20, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.X), "A11");
1276 Assert.AreEqual (new Rectangle (10, 20, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.None), "A12");
1278 c = new ScaleControl ();
1279 c.PublicSetStyle (ControlStyles.FixedHeight, true);
1280 c.PublicSetStyle (ControlStyles.FixedWidth, true);
1282 Assert.AreEqual (new Rectangle (20, 10, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.All), "A13");
1283 Assert.AreEqual (new Rectangle (20, 10, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Location), "A14");
1284 Assert.AreEqual (new Rectangle (10, 20, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Size), "A15");
1285 Assert.AreEqual (new Rectangle (10, 20, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Height), "A16");
1286 Assert.AreEqual (new Rectangle (20, 20, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.X), "A17");
1287 Assert.AreEqual (new Rectangle (10, 20, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.None), "A18");
1290 private class ScaleControl : Control
1292 public bool scale_children = true;
1294 public void PublicScaleControl (SizeF factor, BoundsSpecified specified)
1296 base.ScaleControl (factor, specified);
1299 public Rectangle PublicGetScaledBounds (Rectangle bounds, SizeF factor, BoundsSpecified specified)
1301 return base.GetScaledBounds (bounds, factor, specified);
1304 public void PublicSetStyle (ControlStyles flag, bool value)
1306 base.SetStyle (flag, value);
1309 public void PublicSetTopLevel (bool value)
1311 base.SetTopLevel (value);
1314 protected override bool ScaleChildren {
1315 get { return scale_children; }
1320 [Test] // Bug #347282
1321 public void ScaleHierarchy ()
1323 Form f = new Form ();
1324 Panel p = new Panel ();
1325 Button b = new Button ();
1327 f.ClientSize = new Size (300, 300);
1332 f.AutoScaleBaseSize = new Size (3, 11);
1335 // Due to font differences, all we can guarantee is that
1336 // the button is larger that the default.
1337 Assert.IsTrue (b.Width > 75, "A1");
1338 Assert.IsTrue (b.Height > 23, "A2");
1343 class TestWindowTarget : IWindowTarget
1345 public void OnHandleChange (IntPtr newHandle) {
1348 public void OnMessage (ref Message m) {
1353 public void WindowTargetTest()
1355 Control c = new Control ();
1356 Assert.IsNotNull (c.WindowTarget, "WindowTarget1");
1357 c.WindowTarget = null;
1358 Assert.IsNull (c.WindowTarget, "WindowTarget2");
1360 IWindowTarget existing_target = c.WindowTarget;
1361 IWindowTarget new_target = new TestWindowTarget ();
1362 c.WindowTarget = new_target;
1363 Assert.AreSame (new_target, c.WindowTarget, "WindowTarget3");
1365 TestHelper.RemoveWarning (existing_target);
1369 public void TextTest()
1371 Control r1 = new Control();
1373 Assert.AreEqual("Hi" , r1.Text , "Text1");
1376 Assert.AreEqual("" , r1.Text , "Text2");
1380 public void PubMethodTest7()
1382 Control r1 = new Control();
1383 r1.RightToLeft = RightToLeft.Yes ;
1384 r1.ResetRightToLeft() ;
1385 Assert.AreEqual(RightToLeft.No , r1.RightToLeft , "#81");
1386 r1.ImeMode = ImeMode.Off ;
1387 r1.ResetImeMode () ;
1388 Assert.AreEqual(ImeMode.NoControl , r1.ImeMode , "#82");
1389 r1.ForeColor= SystemColors.GrayText ;
1390 r1.ResetForeColor() ;
1391 Assert.AreEqual(SystemColors.ControlText , r1.ForeColor , "#83");
1392 //r1.Font = Font.FromHdc();
1394 //Assert.AreEqual(FontFamily.GenericSansSerif , r1.Font , "#83");
1395 r1.Cursor = Cursors.Hand ;
1397 Assert.AreEqual(Cursors.Default , r1.Cursor , "#83");
1398 //r1.DataBindings = System.Windows.Forms.Binding ;
1399 //r1.ResetBindings() ;
1400 //Assert.AreEqual(ControlBindingsCollection , r1.DataBindings , "#83");
1401 r1.BackColor = Color.Black ;
1402 r1.ResetBackColor() ;
1403 Assert.AreEqual( SystemColors.Control , r1.BackColor , "#84");
1404 r1.BackColor = Color.Black ;
1406 Assert.AreEqual( null , r1.Region , "#85");
1407 Rectangle M = new Rectangle(10, 20, 30 ,40);
1408 r1.RectangleToScreen(M) ;
1409 Assert.AreEqual( null , r1.Region , "#86");
1413 public void ScreenClientCoords()
1424 p1 = new Point (10,10);
1425 p2 = l.PointToScreen(p1);
1426 p3 = l.PointToClient(p2);
1428 Assert.AreEqual (p1, p3, "SC1");
1432 public void ContainsTest ()
1434 Control t = new Control ();
1435 Control s = new Control ();
1439 Assert.AreEqual (true, t.Contains (s), "Con1");
1440 Assert.AreEqual (false, s.Contains (t), "Con2");
1441 Assert.AreEqual (false, s.Contains (null), "Con3");
1442 Assert.AreEqual (false, t.Contains (new Control ()), "Con4");
1446 public void IsHandleCreated_NotVisible ()
1448 Control c = new Control ();
1451 Form form = new Form ();
1452 form.ShowInTaskbar = false;
1453 form.Controls.Add (c);
1456 Assert.IsFalse (c.IsHandleCreated, "#1");
1458 Assert.IsTrue (c.IsHandleCreated, "#2");
1460 Assert.IsTrue (c.IsHandleCreated, "#3");
1464 class OnCreateControlTest : Control {
1465 public bool reached = false;
1466 protected override void OnCreateControl () {
1472 public void CreateControlVisibleTest ()
1474 OnCreateControlTest test = new OnCreateControlTest ();
1475 test.Visible = false;
1476 Assert.IsFalse (test.IsHandleCreated, "0");
1477 Assert.IsFalse (test.Visible, "1");
1478 test.Visible = true;
1479 Assert.IsTrue (test.Visible, "2");
1480 Assert.IsTrue (test.reached, "3");
1485 public void CreateGraphicsTest ()
1491 Control c = new Control ();
1492 c.SetBounds (0,0, 20, 20);
1493 g = c.CreateGraphics ();
1494 Assert.IsNotNull (g, "Graph1");
1503 bool delegateCalled = false;
1504 public delegate void TestDelegate ();
1506 public void delegate_call () {
1507 delegateCalled = true;
1509 TestHelper.RemoveWarning (delegateCalled);
1513 [ExpectedException(typeof(InvalidOperationException))]
1514 public void InvokeException1 () {
1515 Control c = new Control ();
1516 IAsyncResult result;
1518 result = c.BeginInvoke (new TestDelegate (delegate_call));
1519 c.EndInvoke (result);
1523 public void FindFormTest () {
1524 Form f = new Form ();
1526 f.ShowInTaskbar = false;
1531 f.Controls.Add (c = new Control ());
1532 Assert.AreEqual (f.Name, c.FindForm ().Name, "Find1");
1534 f.Controls.Remove (c);
1536 GroupBox g = new GroupBox ();
1541 Assert.AreEqual (f.Name, f.FindForm ().Name, "Find2");
1543 g.Controls.Remove (c);
1544 Assert.IsNull(c.FindForm (), "Find3");
1555 public void FocusTest ()
1558 Button c = null, d = null;
1562 f.ShowInTaskbar = false;
1572 Assert.IsTrue (c.CanFocus, "Focus1");
1573 Assert.IsFalse (c.Focused, "Focus2");
1575 Assert.IsTrue (c.Focused, "Focus3");
1577 Assert.IsFalse (d.Focused, "Focus4");
1581 Assert.IsTrue (d.Focused, "Focus5");
1582 Assert.IsFalse (c.Focused, "Focus6");
1585 Assert.IsFalse (c.Focused, "Focus7");
1597 public void FromHandleTest ()
1603 c1 = new Control ();
1604 c2 = new Control ();
1608 c1.Controls.Add(c2);
1611 Assert.AreEqual (c1.Name, Control.FromHandle (c1.Handle).Name, "Handle1");
1612 Assert.IsNull (Control.FromHandle (IntPtr.Zero), "Handle2");
1615 Assert.AreEqual (c1.Name, Control.FromChildHandle (c1.Handle).Name, "Handle3");
1616 Assert.IsNull (Control.FromChildHandle (IntPtr.Zero), "Handle4");
1629 public void GetChildAtPointTest ()
1631 Control c = null, d = null;
1632 TransparentControl e = null;
1637 c.SetBounds (0, 0, 100, 100);
1641 d.SetBounds (10, 10, 40, 40);
1644 e = new TransparentControl ();
1646 e.SetBounds (55, 55, 10, 10);
1648 Control l = c.GetChildAtPoint (new Point (15, 15));
1649 Assert.AreEqual (d.Name, l.Name, "Child1");
1650 Assert.IsFalse (e.Name == l.Name, "Child2");
1652 l = c.GetChildAtPoint (new Point (57, 57));
1653 Assert.AreEqual (null, l, "Child3");
1655 l = c.GetChildAtPoint (new Point (10, 10));
1656 Assert.AreEqual (d.Name, l.Name, "Child4");
1658 // GetChildAtPointSkip is not implemented and the following test is breaking for Net_2_0 profile
1662 l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Invisible);
1663 Assert.IsNull (l, "Child5");
1666 l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Invisible);
1667 Assert.AreSame (e.Name, l.Name, "Child6");
1670 l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Disabled);
1671 Assert.IsNull (l, "Child7");
1674 l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Disabled);
1675 Assert.AreSame (e.Name, l.Name, "Child8");
1678 e.BackColor = Color.Transparent;
1679 l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Transparent);
1680 Assert.IsNull (l, "Child9");
1682 e.BackColor = Color.Green;
1683 l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Transparent);
1684 Assert.AreSame (e.Name, l.Name, "Child10");
1695 private class TransparentControl : Control
1697 public TransparentControl ()
1699 SetStyle (ControlStyles.SupportsTransparentBackColor, true);
1702 protected override CreateParams CreateParams
1706 CreateParams cp = base.CreateParams;
1707 cp.ExStyle |= 0x00000020;
1714 public void ResetFontTest ()
1716 Control c = new Control ();
1717 c.Font = new Font (c.Font.FontFamily, 3, FontStyle.Italic);
1720 Assert.IsFalse (c.Font.Bold, "#1");
1721 //Assert.AreEqual ("Microsoft Sans Serif", c.Font.FontFamily.Name, "#2");
1722 Assert.IsFalse (c.Font.Italic, "#3");
1723 //Assert.AreEqual ("Microsoft Sans Serif", c.Font.Name, "#4");
1724 Assert.AreEqual (8.25, c.Font.Size, "#5");
1725 Assert.AreEqual (8.25, c.Font.SizeInPoints, "#6");
1726 Assert.IsFalse (c.Font.Strikeout, "#7");
1727 Assert.IsFalse (c.Font.Underline, "#8");
1728 Assert.AreEqual (GraphicsUnit.Point, c.Font.Unit, "#9");
1730 Assert.AreEqual (true, c.Font.IsSystemFont, "#10");
1734 public class LayoutTestControl : Control {
1735 public int LayoutCount;
1737 public LayoutTestControl () : base() {
1741 protected override void OnLayout(LayoutEventArgs levent) {
1743 base.OnLayout (levent);
1748 public void LayoutTest() {
1749 LayoutTestControl c;
1751 c = new LayoutTestControl();
1758 c.ResumeLayout(true);
1760 c.ResumeLayout(true);
1762 c.ResumeLayout(true);
1764 c.ResumeLayout(true);
1766 c.ResumeLayout(true);
1768 c.ResumeLayout(true);
1770 c.ResumeLayout(true);
1775 Assert.AreEqual(5, c.LayoutCount, "Layout Suspend/Resume locking does not bottom out at 0");
1779 [ExpectedException(typeof(ArgumentException))]
1780 public void TransparentBackgroundTest1() {
1784 c.BackColor = Color.Transparent;
1788 public void TransparentBackgroundTest2() {
1792 c.BackColor = Color.Transparent;
1793 Assert.AreEqual(Color.Transparent, c.BackColor, "Transparent background not set");
1797 public void TransparentBackgroundTest3() {
1801 c.BackColor = Color.Empty;
1802 Assert.AreEqual(Control.DefaultBackColor, c.BackColor, "Setting empty color failed");
1806 public void Dock_Value_Invalid ()
1808 Control c = new Control ();
1810 c.Dock = (DockStyle) 666;
1812 } catch (InvalidEnumArgumentException ex) {
1813 Assert.AreEqual (typeof (InvalidEnumArgumentException), ex.GetType (), "#2");
1814 Assert.IsNotNull (ex.Message, "#3");
1815 Assert.IsNotNull (ex.ParamName, "#4");
1816 Assert.AreEqual ("value", ex.ParamName, "#5");
1817 Assert.IsNull (ex.InnerException, "#6");
1822 public void EnabledTest1() {
1827 grandma = new Control();
1828 parent = new Control();
1829 child = new Control();
1831 grandma.Controls.Add(parent);
1832 parent.Controls.Add(child);
1833 grandma.Enabled = false;
1834 Assert.AreEqual(grandma.Enabled, child.Enabled, "Child did not inherit disabled state");
1837 int EnabledCalledCount = 0;
1838 private void EnabledTest2EnabledChanged(object sender, EventArgs e) {
1839 EnabledCalledCount++;
1843 public void EnabledTest2() {
1844 // Check nesting of enabled calls
1845 // OnEnabled is not called for disabled child controls
1850 EnabledCalledCount = 0;
1852 grandma = new Control();
1853 parent = new Control();
1854 child = new Control();
1855 child.EnabledChanged += new EventHandler(EnabledTest2EnabledChanged);
1857 grandma.Controls.Add(parent);
1858 parent.Controls.Add(child);
1859 grandma.Enabled = false;
1861 Assert.AreEqual(1, EnabledCalledCount, "Child Enabled Event not properly fired");
1862 grandma.Enabled = true;
1863 Assert.AreEqual(2, EnabledCalledCount, "Child Enabled Event not properly fired");
1864 child.Enabled = false;
1865 grandma.Enabled = false;
1866 Assert.AreEqual(3, EnabledCalledCount, "Child Enabled Event not properly fired");
1870 public void ControlsRemoveNullTest ()
1872 Control c = new Control ();
1873 c.Controls.Remove (null);
1877 public void ControlsAddNullTest ()
1879 Control c = new Control ();
1880 c.Controls.Add (null);
1884 [ExpectedException (typeof (ArgumentNullException))]
1885 public void ControlsSetChildIndexNullTest ()
1887 Control c = new Control ();
1888 c.Controls.SetChildIndex (null, 1);
1892 [ExpectedException (typeof (ArgumentNullException))]
1893 public void ControlsAddRangeNullTest ()
1895 Control c = new Control ();
1896 c.Controls.AddRange (null);
1900 public void ControlsAddRangeNullElementTest ()
1902 Control c = new Control ();
1903 Control[] subcontrols = new Control[2];
1904 subcontrols[0] = new Control ();
1905 subcontrols[1] = null;
1907 c.Controls.AddRange (subcontrols);
1911 public void RegionTest () {
1912 Form f = new Form ();
1913 f.ShowInTaskbar = false;
1914 Control c = new Control ();
1916 Assert.IsNull (c.Region, "#A1");
1918 Assert.IsNull (c.Region, "#A2");
1920 Assert.IsNull (c.Region, "#A3");
1923 Region region = new Region ();
1925 f.ShowInTaskbar = false;
1929 Assert.IsNotNull (c.Region, "#B1");
1930 Assert.AreSame (region, c.Region, "#B2");
1933 Assert.IsNull (c.Region, "#B3");
1938 [Test] // bug #330501
1939 [Category ("NotWorking")]
1940 public void OnValidating_Parent_Close ()
1942 MockControl control = new MockControl ();
1944 Form f = new Form ();
1945 f.Controls.Add (control);
1946 f.ShowInTaskbar = false;
1949 Assert.AreEqual (0, control.ValidatingCount, "#A1");
1950 Assert.IsTrue (control.Focused, "#A2");
1952 Assert.AreEqual (1, control.ValidatingCount, "#B1");
1953 Assert.IsFalse (control.Focused, "#B2");
1957 [Test] // bug #80280
1958 public void Validated_Multiple_Containers ()
1960 Form form = new Form ();
1961 form.ShowInTaskbar = false;
1963 UserControl control1 = new UserControl();
1964 UserControl container1 = new UserControl();
1965 control1.Tag = true;
1966 control1.Validated += new EventHandler (Control_ValidatedHandler);
1967 container1.Controls.Add(control1);
1968 form.Controls.Add (container1);
1970 UserControl container2 = new UserControl();
1971 UserControl control2 = new UserControl();
1972 container2.Controls.Add(control2);
1973 form.Controls.Add (container2);
1975 Assert.IsTrue ((bool) control1.Tag, "#1");
1977 Assert.IsTrue ((bool) control1.Tag, "#2");
1979 Assert.IsFalse ((bool) control1.Tag, "#3");
1984 private void Control_ValidatedHandler (object sender, EventArgs e)
1986 ((Control) sender).Tag = false;
1990 public void ControlReparentLocationTest ()
1992 Form form = new Form ();
1993 Label l = new Label ();
1994 l.Location = new Point (0, 0);
1995 form.Controls.Add (l);
1997 Assert.AreEqual (0, l.Left, "#A1");
1998 Assert.AreEqual (0, l.Top, "#A2");
2000 form.Controls.Remove (l);
2002 form.Controls.Add (l);
2003 Assert.AreEqual (0, l.Left, "#A3");
2004 Assert.AreEqual (0, l.Top, "#A4");
2009 public void UseWaitCursorTest ()
2011 Control c = new Control ();
2012 Assert.IsFalse (c.UseWaitCursor, "#1");
2013 c.Cursor = Cursors.Hand;
2014 c.UseWaitCursor = true;
2015 Assert.AreEqual (Cursors.WaitCursor, c.Cursor, "#2");
2016 c.UseWaitCursor = false;
2017 Assert.AreEqual (Cursors.Hand, c.Cursor, "#3");
2019 c.UseWaitCursor = true;
2020 c.Cursor = Cursors.Help;
2021 Assert.AreEqual (Cursors.WaitCursor, c.Cursor, "#4");
2022 Assert.AreEqual (true, c.UseWaitCursor, "#5");
2024 c.UseWaitCursor = false;
2025 Assert.AreEqual (Cursors.Help, c.Cursor, "#6");
2028 [Test] // bug #80621, #81125
2029 public void DontCallSizeFromClientSize ()
2031 SizeControl sc = new SizeControl ();
2033 Assert.AreEqual (0, sc.size_from_client_size_count, "A1");
2035 sc.ClientSize = new Size (300, 300);
2036 Assert.AreEqual (0, sc.size_from_client_size_count, "A2");
2038 SizeForm sf = new SizeForm ();
2039 sf.ShowInTaskbar = false;
2042 Assert.AreEqual (0, sc.size_from_client_size_count, "A3");
2044 sc.ClientSize = new Size (300, 300);
2045 Assert.AreEqual (0, sc.size_from_client_size_count, "A4");
2050 private class SizeControl : Control
2052 public int size_from_client_size_count = 0;
2054 protected override Size SizeFromClientSize (Size clientSize)
2056 size_from_client_size_count++;
2057 return base.SizeFromClientSize (clientSize);
2061 private class SizeForm : Form
2063 public int size_from_client_size_count = 0;
2065 protected override Size SizeFromClientSize (Size clientSize)
2067 size_from_client_size_count++;
2068 return base.SizeFromClientSize (clientSize);
2073 public class MockControl : Control
2075 public int font_height {
2076 get { return base.FontHeight; }
2077 set { base.FontHeight = value; }
2080 public int ValidatingCount;
2082 protected override void OnValidating (CancelEventArgs e)
2088 const int WM_KEYDOWN = 0x0100;
2089 const int WM_CHAR = 0x0102;
2090 const int WM_SYSCHAR = 0x0106;
2091 const int WM_KEYUP = 0x0101;
2095 public void MethodPreProcessControlMessage ()
2097 bool testing_callstack = false;
2099 MyControl c = new MyControl ();
2100 Message m = new Message ();
2103 m.WParam = (IntPtr)Keys.Down;
2104 m.LParam = IntPtr.Zero;
2106 if (testing_callstack) Console.WriteLine ("Start");
2107 Assert.AreEqual (PreProcessControlState.MessageNotNeeded, c.PreProcessControlMessage (ref m), "A1");
2108 if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2110 c.SetState (State.OnPreviewKeyDown);
2111 if (testing_callstack) Console.WriteLine ("Start");
2112 Assert.AreEqual (PreProcessControlState.MessageNeeded, c.PreProcessControlMessage (ref m), "A2");
2113 if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2115 c.SetState (State.ProcessCmdKey);
2116 if (testing_callstack) Console.WriteLine ("Start");
2117 Assert.AreEqual (PreProcessControlState.MessageProcessed, c.PreProcessControlMessage (ref m), "A3");
2118 if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2120 c.SetState (State.IsInputKey);
2121 if (testing_callstack) Console.WriteLine ("Start");
2122 Assert.AreEqual (PreProcessControlState.MessageNeeded, c.PreProcessControlMessage (ref m), "A4");
2123 if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2125 c.SetState (State.ProcessDialogKey);
2126 if (testing_callstack) Console.WriteLine ("Start");
2127 Assert.AreEqual (PreProcessControlState.MessageProcessed, c.PreProcessControlMessage (ref m), "A5");
2128 if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2132 c.SetState (State.None);
2133 if (testing_callstack) Console.WriteLine ("Start");
2134 Assert.AreEqual (PreProcessControlState.MessageNotNeeded, c.PreProcessControlMessage (ref m), "A6");
2135 if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2137 c.SetState (State.IsInputChar);
2138 if (testing_callstack) Console.WriteLine ("Start");
2139 Assert.AreEqual (PreProcessControlState.MessageNeeded, c.PreProcessControlMessage (ref m), "A7");
2140 if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2142 c.SetState (State.ProcessDialogChar);
2143 if (testing_callstack) Console.WriteLine ("Start");
2144 Assert.AreEqual (PreProcessControlState.MessageProcessed, c.PreProcessControlMessage (ref m), "A8");
2145 if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2149 c.SetState (State.None);
2150 if (testing_callstack) Console.WriteLine ("Start");
2151 Assert.AreEqual (PreProcessControlState.MessageNotNeeded, c.PreProcessControlMessage (ref m), "A9");
2152 if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2154 c.SetState (State.IsInputChar);
2155 if (testing_callstack) Console.WriteLine ("Start");
2156 Assert.AreEqual (PreProcessControlState.MessageNeeded, c.PreProcessControlMessage (ref m), "A10");
2157 if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2159 c.SetState (State.ProcessDialogChar);
2160 if (testing_callstack) Console.WriteLine ("Start");
2161 Assert.AreEqual (PreProcessControlState.MessageProcessed, c.PreProcessControlMessage (ref m), "A11");
2162 if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2166 if (testing_callstack) Console.WriteLine ("Start");
2167 Assert.AreEqual (PreProcessControlState.MessageNotNeeded, c.PreProcessControlMessage (ref m), "A12");
2168 if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2170 c.SetState (State.OnPreviewKeyDown);
2171 if (testing_callstack) Console.WriteLine ("Start");
2172 Assert.AreEqual (PreProcessControlState.MessageNotNeeded, c.PreProcessControlMessage (ref m), "A13");
2173 if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2175 c.SetState (State.ProcessCmdKey);
2176 if (testing_callstack) Console.WriteLine ("Start");
2177 Assert.AreEqual (PreProcessControlState.MessageNotNeeded, c.PreProcessControlMessage (ref m), "A14");
2178 if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2180 c.SetState (State.IsInputKey);
2181 if (testing_callstack) Console.WriteLine ("Start");
2182 Assert.AreEqual (PreProcessControlState.MessageNotNeeded, c.PreProcessControlMessage (ref m), "A15");
2183 if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2185 c.SetState (State.ProcessDialogKey);
2186 if (testing_callstack) Console.WriteLine ("Start");
2187 Assert.AreEqual (PreProcessControlState.MessageNotNeeded, c.PreProcessControlMessage (ref m), "A16");
2188 if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2193 public void MethodPreProcessMessage ()
2195 bool testing_callstack = false;
2197 MyControl c = new MyControl ();
2198 Message m = new Message ();
2201 m.WParam = (IntPtr)Keys.Down;
2202 m.LParam = IntPtr.Zero;
2204 if (testing_callstack) Console.WriteLine ("Start");
2205 Assert.AreEqual (false, c.PreProcessMessage (ref m), "A1");
2206 if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2208 c.SetState (State.OnPreviewKeyDown);
2209 if (testing_callstack) Console.WriteLine ("Start");
2210 Assert.AreEqual (false, c.PreProcessMessage (ref m), "A2");
2211 if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2213 c.SetState (State.ProcessCmdKey);
2214 if (testing_callstack) Console.WriteLine ("Start");
2215 Assert.AreEqual (true, c.PreProcessMessage (ref m), "A3");
2216 if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2218 c.SetState (State.IsInputKey);
2219 if (testing_callstack) Console.WriteLine ("Start");
2220 Assert.AreEqual (false, c.PreProcessMessage (ref m), "A4");
2221 if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2223 c.SetState (State.ProcessDialogKey);
2224 if (testing_callstack) Console.WriteLine ("Start");
2225 Assert.AreEqual (true, c.PreProcessMessage (ref m), "A5");
2226 if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2230 c.SetState (State.None);
2231 if (testing_callstack) Console.WriteLine ("Start");
2232 Assert.AreEqual (false, c.PreProcessMessage (ref m), "A6");
2233 if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2235 c.SetState (State.IsInputChar);
2236 if (testing_callstack) Console.WriteLine ("Start");
2237 Assert.AreEqual (false, c.PreProcessMessage (ref m), "A7");
2238 if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2240 c.SetState (State.ProcessDialogChar);
2241 if (testing_callstack) Console.WriteLine ("Start");
2242 Assert.AreEqual (true, c.PreProcessMessage (ref m), "A8");
2243 if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2247 c.SetState (State.None);
2248 if (testing_callstack) Console.WriteLine ("Start");
2249 Assert.AreEqual (false, c.PreProcessMessage (ref m), "A9");
2250 if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2252 c.SetState (State.IsInputChar);
2253 if (testing_callstack) Console.WriteLine ("Start");
2254 Assert.AreEqual (false, c.PreProcessMessage (ref m), "A10");
2255 if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2257 c.SetState (State.ProcessDialogChar);
2258 if (testing_callstack) Console.WriteLine ("Start");
2259 Assert.AreEqual (true, c.PreProcessMessage (ref m), "A11");
2260 if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2264 if (testing_callstack) Console.WriteLine ("Start");
2265 Assert.AreEqual (false, c.PreProcessMessage (ref m), "A12");
2266 if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2268 c.SetState (State.OnPreviewKeyDown);
2269 if (testing_callstack) Console.WriteLine ("Start");
2270 Assert.AreEqual (false, c.PreProcessMessage (ref m), "A13");
2271 if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2273 c.SetState (State.ProcessCmdKey);
2274 if (testing_callstack) Console.WriteLine ("Start");
2275 Assert.AreEqual (false, c.PreProcessMessage (ref m), "A14");
2276 if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2278 c.SetState (State.IsInputKey);
2279 if (testing_callstack) Console.WriteLine ("Start");
2280 Assert.AreEqual (false, c.PreProcessMessage (ref m), "A15");
2281 if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2283 c.SetState (State.ProcessDialogKey);
2284 if (testing_callstack) Console.WriteLine ("Start");
2285 Assert.AreEqual (false, c.PreProcessMessage (ref m), "A16");
2286 if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2300 private class MyControl : Control
2303 private State current_state;
2304 bool testing_callstack = false;
2306 public void SetState (State state)
2308 current_state = state;
2311 protected override bool ProcessCmdKey (ref Message msg, Keys keyData)
2313 if (testing_callstack) Console.Write ("ProcessCmdKey[");
2314 if (current_state == State.ProcessCmdKey) {
2315 if (testing_callstack) Console.WriteLine ("]");
2319 bool retval = base.ProcessCmdKey (ref msg, keyData);
2320 if (testing_callstack) Console.WriteLine ("]");
2325 protected override void OnPreviewKeyDown (PreviewKeyDownEventArgs e)
2327 if (testing_callstack) Console.Write ("OnPreviewKeyDown[");
2328 if (current_state == State.OnPreviewKeyDown) {
2329 e.IsInputKey = true;
2330 if (testing_callstack) Console.WriteLine ("]");
2334 base.OnPreviewKeyDown (e);
2335 if (testing_callstack) Console.WriteLine ("]");
2339 protected override bool IsInputChar (char charCode)
2341 if (testing_callstack) Console.Write ("IsInputChar[");
2342 if (current_state == State.IsInputChar) {
2343 if (testing_callstack) Console.WriteLine ("true]");
2347 bool retval = base.IsInputChar (charCode);
2348 if (testing_callstack) Console.WriteLine ("{0}]", retval.ToString ());
2352 protected override bool IsInputKey (Keys keyData)
2354 if (testing_callstack) Console.Write ("IsInputKey[");
2355 if (current_state == State.IsInputKey) {
2356 if (testing_callstack) Console.WriteLine ("]");
2360 bool retval = base.IsInputKey (keyData);
2361 if (testing_callstack) Console.WriteLine ("]");
2365 public override bool PreProcessMessage (ref Message msg)
2367 if (testing_callstack) Console.Write ("PreProcessMessage[");
2368 if (current_state == State.PreProcessMessage) {
2369 if (testing_callstack) Console.WriteLine ("]");
2373 bool retval = base.PreProcessMessage (ref msg);
2374 if (testing_callstack) Console.WriteLine ("]");
2378 protected override bool ProcessDialogKey (Keys keyData)
2380 if (testing_callstack) Console.Write ("ProcessDialogKey[");
2381 if (current_state == State.ProcessDialogKey) {
2382 if (testing_callstack) Console.WriteLine ("]");
2386 bool retval = base.ProcessDialogKey (keyData);
2387 if (testing_callstack) Console.WriteLine ("]");
2391 protected override bool ProcessDialogChar (char charCode)
2393 if (testing_callstack) Console.Write ("ProcessDialogChar[");
2394 if (current_state == State.ProcessDialogChar) {
2395 if (testing_callstack) Console.WriteLine ("]");
2399 bool retval = base.ProcessDialogChar (charCode);
2400 if (testing_callstack) Console.WriteLine ("]");
2406 public void MethodIsInputChar ()
2408 // Basically, show that this method always returns false
2409 InputCharControl m = new InputCharControl ();
2410 bool result = false;
2412 for (int i = 0; i < 256; i++)
2413 result |= m.PublicIsInputChar ((char)i);
2415 Assert.AreEqual (false, result, "I1");
2418 private class InputCharControl : Control
2420 public bool PublicIsInputChar (char charCode)
2422 return base.IsInputChar (charCode);
2427 [Test] // bug #81118, 81718
2428 public void VisibleTriggersLayout ()
2430 Form f = new Form ();
2431 f.ShowInTaskbar = false;
2433 Control c = new Control ();
2438 c.Dock = DockStyle.Fill;
2441 Assert.AreEqual (f.ClientSize.Width, c.Width, "L1");
2445 public void ResumeLayoutEffects ()
2447 Form f = new Form ();
2448 f.ShowInTaskbar = false;
2449 f.ClientSize = new Size (300, 300);
2451 Button button1 = new Button ();
2452 f.Controls.Add (button1);
2453 button1.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
2454 button1.Location = new Point (f.ClientSize.Width - button1.Width, f.ClientSize.Height - button1.Height);
2458 Assert.AreEqual (new Point (225, 277), button1.Location, "A1");
2462 f.ResumeLayout (false);
2465 Assert.AreEqual (new Point (225, 277), button1.Location, "A2");
2471 Assert.AreEqual (new Point (225, 287), button1.Location, "A3");
2476 public void DisposeEnumerator ()
2478 // We can modify the collection while looping through it without crashing
2479 Control c = new Control ();
2481 c.Controls.Add (new Control ());
2482 c.Controls.Add (new Control ());
2484 foreach (Control c1 in c.Controls)
2485 c.Controls.Remove (c1);
2490 public class ControlSetTopLevelTest
2492 class ControlPoker : Control {
2493 public void DoSetTopLevel ()
2497 public bool DoGetTopLevel ()
2499 return GetTopLevel ();
2504 public void TestControl ()
2506 ControlPoker c = new ControlPoker ();
2509 Assert.IsTrue (c.DoGetTopLevel (), "1");
2510 Assert.IsFalse (c.Visible, "2");
2514 [ExpectedException (typeof (ArgumentException))]
2515 public void TestChildControl ()
2517 Control c1 = new Control();
2518 ControlPoker c2 = new ControlPoker ();
2520 c1.Controls.Add (c2);
2521 c2.DoSetTopLevel ();
2525 [ExpectedException (typeof (ArgumentException))]
2526 public void TestTopLevelAdd () {
2527 Form f = new Form();
2528 Form f1 = new Form();
2532 [Category ("NotWorking")]
2534 public void TestForm ()
2536 Form f = new Form ();
2537 Assert.IsFalse (f.Visible, "3");
2539 Assert.IsFalse (f.Visible, "4");
2544 public class ControlResizeLayoutTest
2546 class ControlPoker : Control {
2547 public void DoOnResize ()
2549 OnResize (EventArgs.Empty);
2554 string child_affected_property;
2555 void ChildLayoutEvent (object sender, LayoutEventArgs e)
2558 child_affected_property = e.AffectedProperty;
2562 string parent_affected_property;
2563 void ParentLayoutEvent (object sender, LayoutEventArgs e)
2566 parent_affected_property = e.AffectedProperty;
2568 TestHelper.RemoveWarning (parent_affected_property);
2574 Panel p = new Panel ();
2575 ControlPoker c = new ControlPoker();
2579 p.Layout += new LayoutEventHandler (ParentLayoutEvent);
2580 c.Layout += new LayoutEventHandler (ChildLayoutEvent);
2584 Assert.AreEqual (1, child_event, "1");
2585 Assert.AreEqual ("Bounds", child_affected_property, "2");
2587 Assert.AreEqual (0, parent_event, "3");
2593 [Category ("NotWorking")]
2594 public class ControlInvokeTest {
2595 public delegate void TestDelegate ();
2600 ApplicationContext control_context;
2601 bool delegateCalled = false;
2602 bool threadDied = false;
2606 void CreateControl ()
2610 f.ShowInTaskbar = false;
2616 Console.WriteLine ("f.Handle = {0}", f.Handle);
2617 Console.WriteLine ("c.Handle = {0}", c.Handle);
2619 control_context = new ApplicationContext (f);
2622 Console.WriteLine ("pulsing");
2625 Console.WriteLine ("control thread running");
2626 Application.Run (control_context);
2628 Console.WriteLine ("dying");
2631 Console.WriteLine ("pulsing again");
2634 } catch (Exception e) { Console.WriteLine (e); }
2638 public void InvokeTest ()
2642 control_t = new Thread(new ThreadStart(CreateControl));
2648 Console.WriteLine ("waiting on monitor");
2651 Console.WriteLine ("making async call");
2653 IAsyncResult result;
2654 result = c.BeginInvoke (new TestDelegate (delegate_call));
2655 c.EndInvoke (result);
2657 Assert.IsTrue (delegateCalled, "Invoke1");
2660 Assert.IsTrue (threadDied, "Invoke2");
2663 public void delegate_call () {
2665 /* invoked on control_context's thread */
2666 delegateCalled = true;
2668 Console.WriteLine ("calling Application.Exit");
2669 control_context.ExitThread ();
2670 } catch (Exception e) { Console.WriteLine (e); }
2676 public class ControlWMTest
2679 public void WM_PARENTNOTIFY_Test ()
2685 tester = new WMTester ();
2686 child = new Control ();
2687 tester.Controls.Add (child);
2689 tester.Visible = true;
2690 child.Visible = true;
2692 child_handle = child.Handle.ToInt32 ();
2697 msgs = tester.Find (WndMsg.WM_PARENTNOTIFY);
2699 Assert.AreEqual (1, msgs.Count, "#1");
2701 m1 = (Message) msgs [0];
2702 Assert.AreEqual (WndMsg.WM_CREATE, ((WndMsg) LowOrder (m1.WParam)), "#2");
2703 //Assert.AreEqual (child.Identifier??, HighOrder (m1.WParam), "#3");
2704 Assert.AreEqual (child_handle, m1.LParam.ToInt32 (), "#4");
2708 msgs = tester.Find (WndMsg.WM_PARENTNOTIFY);
2709 Assert.AreEqual (2, msgs.Count, "#5");
2710 m1 = (Message) msgs [1];
2712 Assert.AreEqual (WndMsg.WM_DESTROY, ((WndMsg) LowOrder (m1.WParam)), "#6");
2713 //Assert.AreEqual (child.Identifier??, HighOrder (m1.WParam), "#7");
2714 Assert.AreEqual (child_handle, m1.LParam.ToInt32 (), "#8");
2719 internal static int LowOrder (int param)
2721 return ((int)(short)(param & 0xffff));
2724 internal static int HighOrder (int param)
2726 return ((int)(short)(param >> 16));
2729 internal static int LowOrder (IntPtr param)
2731 return ((int)(short)(param.ToInt32 () & 0xffff));
2734 internal static int HighOrder (IntPtr param)
2736 return ((int)(short)(param.ToInt32 () >> 16));
2739 internal class WMTester : Form
2741 internal ArrayList Messages = new ArrayList ();
2743 internal bool Contains (WndMsg msg)
2745 return Contains (msg, Messages);
2748 internal bool Contains (WndMsg msg, ArrayList list)
2750 foreach (Message m in Messages)
2752 if (m.Msg == (int) msg)
2758 internal ArrayList Find (WndMsg msg)
2760 ArrayList result = new ArrayList ();
2762 foreach (Message m in Messages)
2764 if (m.Msg == (int) msg)
2770 protected override void WndProc(ref Message m)
2772 Console.WriteLine ("WndProc: " + m.ToString ());
2774 base.WndProc (ref m);
2781 public class ControlLayoutTest
2784 public void SetUp ()
2789 [Test] // bug #80456
2790 public void LayoutTest ()
2792 MockLayoutEngine layoutEngine = new MockLayoutEngine ();
2793 MockControl c = new MockControl (layoutEngine);
2794 c.Layout += new LayoutEventHandler (LayoutEvent);
2795 Assert.IsFalse (layoutEngine.LayoutInvoked, "#A1");
2796 Assert.AreEqual (0, _layoutCount, "#A2");
2798 Assert.IsTrue (layoutEngine.LayoutInvoked, "#A3");
2799 Assert.AreEqual (1, _layoutCount, "#A4");
2801 layoutEngine.Reset ();
2802 c.OverrideOnLayout = true;
2803 Assert.IsFalse (layoutEngine.LayoutInvoked, "#B1");
2805 Assert.IsFalse (layoutEngine.LayoutInvoked, "#B2");
2806 Assert.AreEqual (1, _layoutCount, "#B3");
2809 void LayoutEvent (object sender, LayoutEventArgs e)
2814 private int _layoutCount;
2816 class MockControl : Control
2818 public MockControl (LayoutEngine layoutEngine)
2820 _layoutEngine = layoutEngine;
2823 public bool OverrideOnLayout
2825 get { return _overrideOnLayout; }
2826 set { _overrideOnLayout = value; }
2829 protected override void OnLayout (LayoutEventArgs levent)
2831 if (!OverrideOnLayout)
2832 base.OnLayout (levent);
2835 public override LayoutEngine LayoutEngine {
2837 if (_layoutEngine == null)
2838 return base.LayoutEngine;
2839 return _layoutEngine;
2843 private bool _overrideOnLayout;
2844 private LayoutEngine _layoutEngine;
2847 class MockLayoutEngine : LayoutEngine
2849 public bool LayoutInvoked {
2850 get { return _layoutInvoked; }
2853 public void Reset ()
2855 _layoutInvoked = false;
2858 public override bool Layout (object container, LayoutEventArgs args)
2860 _layoutInvoked = true;
2864 private bool _layoutInvoked;