2 // Copyright (c) 2005 Novell, Inc.
5 // Ritvik Mayank (mritvik@novell.com)
9 using System.Windows.Forms;
11 using System.Threading;
12 using System.Reflection;
13 using System.Runtime.Remoting;
15 using NUnit.Framework;
17 namespace MWF.MonoTest
20 public class ControlTest
22 internal static void TestAccessibility(Control c, string Default, string Description, string Name, AccessibleRole Role) {
23 Assert.AreEqual(false, c.AccessibilityObject == null, "Acc1");
24 Assert.AreEqual(Default, c.AccessibleDefaultActionDescription, "Acc2");
25 Assert.AreEqual(Description, c.AccessibleDescription, "Acc3");
26 Assert.AreEqual(Name, c.AccessibleName, "Acc4");
27 Assert.AreEqual(Role, c.AccessibleRole, "Acc5");
31 public void PubPropTest()
33 Control c = new Control();
35 TestAccessibility(c, null, null, null, AccessibleRole.Default);
38 Assert.AreEqual(false, c.AllowDrop , "A1");
39 Assert.AreEqual(AnchorStyles.Top | AnchorStyles.Left, c.Anchor, "A2");
42 Assert.AreEqual("Control", c.BackColor.Name , "B1");
43 Assert.AreEqual(null, c.BackgroundImage, "B2");
44 Assert.AreEqual(null, c.BindingContext, "B3");
45 Assert.AreEqual(0, c.Bottom, "B4");
46 Assert.AreEqual (new Rectangle (0,0, 0, 0) , c.Bounds , "B5");
49 Assert.AreEqual(false, c.CanFocus, "C1");
50 Assert.AreEqual(true, c.CanSelect, "C2");
51 Assert.AreEqual(false, c.Capture, "C3");
52 Assert.AreEqual(true, c.CausesValidation, "C4");
53 Assert.AreEqual (new Rectangle(0, 0, 0, 0) , c.ClientRectangle , "C5");
54 Assert.AreEqual (new Size(0, 0), c.ClientSize , "C6");
56 string name = c.CompanyName;
57 if (!name.Equals("Mono Project, Novell, Inc.") && !name.Equals("Microsoft Corporation")) {
58 Assert.Fail("CompanyName property does not match any accepted value - C7");
60 Assert.AreEqual(null, c.Container, "C8");
61 Assert.AreEqual(false, c.ContainsFocus, "C9");
62 Assert.AreEqual(null, c.ContextMenu, "C10");
63 Assert.AreEqual(0, c.Controls.Count, "C11");
64 Assert.AreEqual(true, c.Created, "C12");
65 Assert.AreEqual(Cursors.Default, c.Cursor, "C13");
68 Assert.AreEqual(false, c.DataBindings == null, "D1");
69 Assert.AreEqual("Control", Control.DefaultBackColor.Name, "D2");
70 Assert.AreEqual("ControlText", Control.DefaultForeColor.Name, "D3");
71 Assert.AreEqual(FontStyle.Regular, Control.DefaultFont.Style, "D4");
72 Assert.AreEqual (new Rectangle(0, 0, 0, 0), c.DisplayRectangle , "D5");
73 Assert.AreEqual(false, c.Disposing, "D6");
74 Assert.AreEqual(DockStyle.None, c.Dock, "D7");
77 Assert.AreEqual(true, c.Enabled, "E1");
80 Assert.AreEqual(false, c.Focused, "F1");
81 Assert.AreEqual(FontStyle.Regular, c.Font.Style, "F2");
82 Assert.AreEqual(SystemColors.ControlText, c.ForeColor, "F3");
87 Assert.AreEqual (((IWin32Window)c).Handle, c.Handle, "H1");
88 Assert.AreEqual(false, c.HasChildren, "H2");
89 Assert.AreEqual(0, c.Height, "H3");
92 Assert.AreEqual (ImeMode.NoControl, c.ImeMode, "I1");
93 Assert.AreEqual(false, c.InvokeRequired, "I2");
94 Assert.AreEqual(false, c.IsAccessible, "I3");
95 Assert.AreEqual(false, c.IsDisposed, "I4");
96 Assert.AreEqual(true, c.IsHandleCreated, "I5");
103 Assert.AreEqual(0, c.Left, "L1");
104 Assert.AreEqual(Point.Empty, c.Location, "L2");
108 Assert.IsTrue(c.MaximumSize.IsEmpty);
109 Assert.IsTrue(c.MinimumSize.IsEmpty);
111 Assert.AreEqual(Keys.None, Control.ModifierKeys, "M1");
112 Assert.AreEqual(false, Control.MousePosition.IsEmpty, "M2");
113 Assert.AreEqual(MouseButtons.None, Control.MouseButtons, "M3");
116 Assert.AreEqual("", c.Name, "N1");
121 Assert.AreEqual(null, c.Parent, "P1");
122 name = c.ProductName;
123 if (!name.Equals("Novell Mono MWF") && !name.Equals("Microsoft (R) .NET Framework"))
124 Assert.Fail("ProductName property does not match any accepted value - P2");
126 name = c.ProductVersion;
127 if (!name.Equals("1.1.4322.2032")) {
128 Assert.Fail("This test is being run against the wrong framework version.\nExpected is Net 1.1sp1. - P3");
132 Assert.AreEqual(false, c.RecreatingHandle, "R1");
133 Assert.AreEqual(null, c.Region, "R2");
134 Assert.AreEqual(0, c.Right, "R3");
135 Assert.AreEqual(RightToLeft.No, c.RightToLeft, "R4");
138 Assert.AreEqual(null, c.Site, "S1");
139 Assert.AreEqual (new Size(0, 0), c.Size, "S2");
142 Assert.AreEqual(0, c.TabIndex , "T1");
143 Assert.AreEqual(true, c.TabStop, "T2");
144 Assert.AreEqual(null, c.Tag, "T3");
145 Assert.AreEqual("", c.Text, "T4");
146 Assert.AreEqual(0, c.Top, "T5");
147 Assert.AreEqual(null, c.TopLevelControl, "T6");
152 Assert.AreEqual(true, c.Visible, "V1");
155 Assert.AreEqual(0, c.Width, "W1");
161 public void RelationTest() {
168 Assert.AreEqual(true , c1.Visible , "Rel1");
169 Assert.AreEqual(false, c1.Contains(c2) , "Rel2");
170 Assert.AreEqual("System.Windows.Forms.Control", c1.ToString() , "Rel3");
173 Assert.AreEqual(true , c2.Visible , "Rel4");
174 Assert.AreEqual(true, c1.Contains(c2) , "Rel5");
176 c1.Anchor = AnchorStyles.Top;
178 c1.Anchor = AnchorStyles.Left ;
180 Assert.AreEqual(AnchorStyles.Left , c1.Anchor, "Rel6");
182 c1.SetBounds(10, 20, 30, 40) ;
183 Assert.AreEqual(new Rectangle(10, 20, 30, 40), c1.Bounds, "Rel7");
185 Assert.AreEqual(c1, c2.Parent, "Rel8");
188 private string TestControl(Control container, Control start, bool forward) {
191 ctl = container.GetNextControl(start, forward);
201 public void TabOrder() {
205 Label label1 = new Label(); // To test non-tabstop items as well
206 Label label2 = new Label();
208 GroupBox group1 = new GroupBox();
209 GroupBox group2 = new GroupBox();
210 GroupBox group3 = new GroupBox();
212 TextBox text1 = new TextBox();
214 RadioButton radio11 = new RadioButton();
215 RadioButton radio12 = new RadioButton();
216 RadioButton radio13 = new RadioButton();
217 RadioButton radio14 = new RadioButton();
218 RadioButton radio21 = new RadioButton();
219 RadioButton radio22 = new RadioButton();
220 RadioButton radio23 = new RadioButton();
221 RadioButton radio24 = new RadioButton();
222 RadioButton radio31 = new RadioButton();
223 RadioButton radio32 = new RadioButton();
224 RadioButton radio33 = new RadioButton();
225 RadioButton radio34 = new RadioButton();
229 form.ClientSize = new System.Drawing.Size (520, 520);
230 Assert.AreEqual(new Size(520, 520), form.ClientSize, "Tab1");
232 form.Text = "SWF Taborder Test App Form";
233 Assert.AreEqual("SWF Taborder Test App Form", form.Text, "Tab2");
235 label1.Location = new Point(10, 10);
236 Assert.AreEqual(new Point(10, 10), label1.Location, "Tab3");
237 label1.Text = "Label1";
238 form.Controls.Add(label1);
240 label2.Location = new Point(200, 10);
241 label2.Text = "Label2";
242 form.Controls.Add(label2);
244 group1.Text = "Group1";
245 group2.Text = "Group2";
246 group3.Text = "Group3";
248 group1.Size = new Size(200, 400);
249 group2.Size = new Size(200, 400);
250 group3.Size = new Size(180, 180);
251 Assert.AreEqual(new Size(180, 180), group3.Size, "Tab4");
253 group1.Location = new Point(10, 40);
254 group2.Location = new Point(220, 40);
255 group3.Location = new Point(10, 210);
257 group1.TabIndex = 30;
258 Assert.AreEqual(30, group1.TabIndex, "Tab5");
259 group1.TabStop = true;
261 // Don't assign, test automatic assignment
262 //group2.TabIndex = 0;
263 group2.TabStop = true;
264 Assert.AreEqual(0, group2.TabIndex, "Tab6");
266 group3.TabIndex = 35;
267 group3.TabStop = true;
269 // Test default tab index
270 Assert.AreEqual(0, radio11.TabIndex, "Tab7");
272 text1.Text = "Edit Control";
274 radio11.Text = "Radio 1-1 [Tab1]";
275 radio12.Text = "Radio 1-2 [Tab2]";
276 radio13.Text = "Radio 1-3 [Tab3]";
277 radio14.Text = "Radio 1-4 [Tab4]";
279 radio21.Text = "Radio 2-1 [Tab4]";
280 radio22.Text = "Radio 2-2 [Tab3]";
281 radio23.Text = "Radio 2-3 [Tab2]";
282 radio24.Text = "Radio 2-4 [Tab1]";
284 radio31.Text = "Radio 3-1 [Tab1]";
285 radio32.Text = "Radio 3-2 [Tab3]";
286 radio33.Text = "Radio 3-3 [Tab2]";
287 radio34.Text = "Radio 3-4 [Tab4]";
289 // We don't assign TabIndex for radio1X; test automatic assignment
290 text1.TabStop = true;
291 radio11.TabStop = true;
293 radio21.TabIndex = 4;
294 radio22.TabIndex = 3;
295 radio23.TabIndex = 2;
296 radio24.TabIndex = 1;
297 radio24.TabStop = true;
299 radio31.TabIndex = 11;
300 radio31.TabStop = true;
301 radio32.TabIndex = 13;
302 radio33.TabIndex = 12;
303 radio34.TabIndex = 14;
305 text1.Location = new Point(10, 100);
307 radio11.Location = new Point(10, 20);
308 radio12.Location = new Point(10, 40);
309 radio13.Location = new Point(10, 60);
310 radio14.Location = new Point(10, 80);
312 radio21.Location = new Point(10, 20);
313 radio22.Location = new Point(10, 40);
314 radio23.Location = new Point(10, 60);
315 radio24.Location = new Point(10, 80);
317 radio31.Location = new Point(10, 20);
318 radio32.Location = new Point(10, 40);
319 radio33.Location = new Point(10, 60);
320 radio34.Location = new Point(10, 80);
322 text1.Size = new Size(150, text1.PreferredHeight);
324 radio11.Size = new Size(150, 20);
325 radio12.Size = new Size(150, 20);
326 radio13.Size = new Size(150, 20);
327 radio14.Size = new Size(150, 20);
329 radio21.Size = new Size(150, 20);
330 radio22.Size = new Size(150, 20);
331 radio23.Size = new Size(150, 20);
332 radio24.Size = new Size(150, 20);
334 radio31.Size = new Size(150, 20);
335 radio32.Size = new Size(150, 20);
336 radio33.Size = new Size(150, 20);
337 radio34.Size = new Size(150, 20);
339 group1.Controls.Add(text1);
341 group1.Controls.Add(radio11);
342 group1.Controls.Add(radio12);
343 group1.Controls.Add(radio13);
344 group1.Controls.Add(radio14);
346 group2.Controls.Add(radio21);
347 group2.Controls.Add(radio22);
348 group2.Controls.Add(radio23);
349 group2.Controls.Add(radio24);
351 group3.Controls.Add(radio31);
352 group3.Controls.Add(radio32);
353 group3.Controls.Add(radio33);
354 group3.Controls.Add(radio34);
356 form.Controls.Add(group1);
357 form.Controls.Add(group2);
358 group2.Controls.Add(group3);
360 // Perform some tests, the TabIndex stuff below will alter the outcome
361 Assert.AreEqual(null, TestControl(group2, radio34, true), "Tab8");
362 Assert.AreEqual(31, group2.TabIndex, "Tab9");
364 // Does the taborder of containers and non-selectable things change behaviour?
370 // Start verification
371 Assert.AreEqual(null, TestControl(group2, radio34, true), "Tab10");
372 Assert.AreEqual(radio24.Text, TestControl(group2, group2, true), "Tab11");
373 Assert.AreEqual(radio31.Text, TestControl(group2, group3, true), "Tab12");
374 Assert.AreEqual(null, TestControl(group1, radio14, true), "Tab13");
375 Assert.AreEqual(radio23.Text, TestControl(group2, radio24, true), "Tab14");
376 Assert.AreEqual(group3.Text, TestControl(group2, radio21, true), "Tab15");
377 Assert.AreEqual(radio13.Text, TestControl(form, radio12, true), "Tab16");
378 Assert.AreEqual(label2.Text, TestControl(form, radio14, true), "Tab17");
379 Assert.AreEqual(group1.Text, TestControl(form, radio34, true), "Tab18");
380 Assert.AreEqual(radio23.Text, TestControl(group2, radio24, true), "Tab19");
383 Assert.AreEqual(null, TestControl(radio11, radio21, true), "Tab20");
384 Assert.AreEqual(text1.Text, TestControl(group1, radio21, true), "Tab21");
386 Assert.AreEqual(radio14.Text, TestControl(form, label2, false), "Tab22");
387 Assert.AreEqual(radio21.Text, TestControl(group2, group3, false), "Tab23");
389 Assert.AreEqual(4, radio21.TabIndex, "Tab24");
390 Assert.AreEqual(1, radio11.TabIndex, "Tab25");
391 Assert.AreEqual(3, radio13.TabIndex, "Tab26");
392 Assert.AreEqual(35, group3.TabIndex, "Tab27");
393 Assert.AreEqual(1, group2.TabIndex, "Tab28");
395 Assert.AreEqual(label1.Text, TestControl(form, form, false), "Tab29");
396 Assert.AreEqual(radio14.Text, TestControl(group1, group1, false), "Tab30");
397 Assert.AreEqual(radio34.Text, TestControl(group3, group3, false), "Tab31");
399 Assert.AreEqual(null, TestControl(label1, label1, false), "Tab31");
400 Assert.AreEqual(null, TestControl(radio11, radio21, false), "Tab32");
404 public void ScaleTest()
406 Control r1 = new Control();
411 Assert.AreEqual(80, r1.Width, "Scale1");
412 Assert.AreEqual(40, r1.Height, "Scale2");
416 public void TextTest()
418 Control r1 = new Control();
420 Assert.AreEqual("Hi" , r1.Text , "Text1");
423 Assert.AreEqual("" , r1.Text , "Text2");
427 public void PubMethodTest7()
429 Control r1 = new Control();
430 r1.RightToLeft = RightToLeft.Yes ;
431 r1.ResetRightToLeft() ;
432 Assert.AreEqual(RightToLeft.No , r1.RightToLeft , "#81");
433 r1.ImeMode = ImeMode.Off ;
435 Assert.AreEqual(ImeMode.NoControl , r1.ImeMode , "#82");
436 r1.ForeColor= SystemColors.GrayText ;
437 r1.ResetForeColor() ;
438 Assert.AreEqual(SystemColors.ControlText , r1.ForeColor , "#83");
439 //r1.Font = Font.FromHdc();
441 //Assert.AreEqual(FontFamily.GenericSansSerif , r1.Font , "#83");
442 r1.Cursor = Cursors.Hand ;
444 Assert.AreEqual(Cursors.Default , r1.Cursor , "#83");
445 //r1.DataBindings = System.Windows.Forms.Binding ;
446 //r1.ResetBindings() ;
447 //Assert.AreEqual(ControlBindingsCollection , r1.DataBindings , "#83");
448 r1.BackColor = System.Drawing.Color.Black ;
449 r1.ResetBackColor() ;
450 Assert.AreEqual( SystemColors.Control , r1.BackColor , "#84");
451 r1.BackColor = System.Drawing.Color.Black ;
453 Assert.AreEqual( null , r1.Region , "#85");
454 Rectangle M = new Rectangle(10, 20, 30 ,40);
455 r1.RectangleToScreen(M) ;
456 Assert.AreEqual( null , r1.Region , "#86");
461 public void ScreenClientCoords()
472 p1 = new Point (10,10);
473 p2 = l.PointToScreen(p1);
474 p3 = l.PointToClient(p2);
476 Assert.AreEqual (p1, p3, "SC1");
480 public void ContainsTest ()
482 Control t = new Control ();
483 Control s = new Control ();
487 Assert.AreEqual (true, t.Contains (s), "Con1");
488 Assert.AreEqual (false, s.Contains (t), "Con2");
489 Assert.AreEqual (false, s.Contains (null), "Con3");
490 Assert.AreEqual (false, t.Contains (new Control ()), "Con4");
494 public void CreateHandleTest ()
503 parent = new Control ();
504 child = new Control ();
506 parent.Visible = true;
507 parent.Controls.Add (child);
509 Assert.IsFalse (parent.IsHandleCreated, "CH1");
510 Assert.IsFalse (child.IsHandleCreated, "CH2");
512 parent.CreateControl ();
513 Assert.IsNotNull (parent.Handle, "CH3");
514 Assert.IsNotNull (child.Handle, "CH4");
515 Assert.IsTrue (parent.IsHandleCreated, "CH5");
516 Assert.IsTrue (child.IsHandleCreated, "CH6");
524 // Accessing Handle Property creates the handle
526 parent = new Control ();
527 parent.Visible = true;
528 child = new Control ();
529 parent.Controls.Add (child);
530 Assert.IsFalse (parent.IsHandleCreated, "CH7");
531 Assert.IsFalse (child.IsHandleCreated, "CH8");
532 Assert.IsNotNull (parent.Handle, "CH9");
533 Assert.IsTrue (parent.IsHandleCreated, "CH10");
534 Assert.IsTrue (child.IsHandleCreated, "CH11");
544 public void CreateGraphicsTest ()
550 Control c = new Control ();
551 c.SetBounds (0,0, 20, 20);
552 g = c.CreateGraphics ();
553 Assert.IsNotNull (g, "Graph1");
562 bool delegateCalled = false;
563 public delegate void TestDelegate ();
566 [ExpectedException(typeof(System.InvalidOperationException))]
567 public void InvokeException1 () {
568 Control c = new Control ();
571 result = c.BeginInvoke (new TestDelegate (delegate_call));
572 c.EndInvoke (result);
576 [Ignore("Seems to hang")]
577 public void InvokeTest () {
585 result = c.BeginInvoke (new TestDelegate (delegate_call));
586 c.EndInvoke (result);
587 Assert.AreEqual (true, delegateCalled, "Invoke1");
594 public void delegate_call () {
595 delegateCalled = true;
599 public void FindFormTest () {
600 Form f = new Form ();
606 f.Controls.Add (c = new Control ());
607 Assert.AreEqual (f.Name, c.FindForm ().Name, "Find1");
609 f.Controls.Remove (c);
611 GroupBox g = new GroupBox ();
616 Assert.AreEqual (f.Name, f.FindForm ().Name, "Find2");
618 g.Controls.Remove (c);
619 Assert.IsNull(c.FindForm (), "Find3");
630 public void FocusTest ()
633 Button c = null, d = null;
646 Assert.IsTrue (c.CanFocus, "Focus1");
647 Assert.IsFalse (c.Focused, "Focus2");
649 Assert.IsTrue (c.Focused, "Focus3");
651 Assert.IsFalse (d.Focused, "Focus4");
655 Assert.IsTrue (d.Focused, "Focus5");
656 Assert.IsFalse (c.Focused, "Focus6");
659 Assert.IsFalse (c.Focused, "Focus7");
671 public void FromHandleTest ()
685 Assert.AreEqual (c1.Name, Control.FromHandle (c1.Handle).Name, "Handle1");
686 Assert.IsNull (Control.FromHandle (IntPtr.Zero), "Handle2");
689 Assert.AreEqual (c1.Name, Control.FromChildHandle (c1.Handle).Name, "Handle3");
690 Assert.IsNull (Control.FromChildHandle (IntPtr.Zero), "Handle4");
703 public void GetChildAtPointTest ()
705 Control c = null, d = null, e = null;
710 c.SetBounds (0, 0, 100, 100);
714 d.SetBounds (10, 10, 40, 40);
719 e.SetBounds (55, 55, 10, 10);
721 Control l = c.GetChildAtPoint (new Point (15, 15));
722 Assert.AreEqual (d.Name, l.Name, "Child1");
723 Assert.IsFalse (e.Name == l.Name, "Child2");
725 l = c.GetChildAtPoint (new Point (57, 57));
726 Assert.IsNull (l, "Child3");
728 l = c.GetChildAtPoint (new Point (10, 10));
729 Assert.AreEqual (d.Name, l.Name, "Child4");
731 // GetChildAtPointSkip is not implemented and the following test is breaking for Net_2_0 profile
733 // c.Controls.Add (e);
734 // e.Visible = false;
735 // l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Invisible);
736 // Assert.IsNull (l, "Child5");
739 // l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Invisible);
740 // Assert.AreSame (e.Name, l.Name, "Child6");
751 public class LayoutTestControl : Control {
752 public int LayoutCount;
754 public LayoutTestControl () : base() {
758 protected override void OnLayout(LayoutEventArgs levent) {
760 base.OnLayout (levent);
765 public void LayoutTest() {
768 c = new LayoutTestControl();
775 c.ResumeLayout(true);
777 c.ResumeLayout(true);
779 c.ResumeLayout(true);
781 c.ResumeLayout(true);
783 c.ResumeLayout(true);
785 c.ResumeLayout(true);
787 c.ResumeLayout(true);
792 Assert.AreEqual(5, c.LayoutCount, "Layout Suspend/Resume locking does not bottom out at 0");
796 [ExpectedException(typeof(System.ArgumentException))]
797 public void TransparentBackgroundTest1() {
801 c.BackColor = Color.Transparent;
805 public void TransparentBackgroundTest2() {
809 c.BackColor = Color.Transparent;
810 Assert.AreEqual(Color.Transparent, c.BackColor, "Transparent background not set");
814 public void TransparentBackgroundTest3() {
818 c.BackColor = Color.Empty;
819 Assert.AreEqual(Control.DefaultBackColor, c.BackColor, "Setting empty color failed");
823 public void EnabledTest1() {
828 grandma = new Control();
829 parent = new Control();
830 child = new Control();
832 grandma.Controls.Add(parent);
833 parent.Controls.Add(child);
834 grandma.Enabled = false;
835 Assert.AreEqual(grandma.Enabled, child.Enabled, "Child did not inherit disabled state");
838 int EnabledCalledCount = 0;
839 private void EnabledTest2EnabledChanged(object sender, EventArgs e) {
840 EnabledCalledCount++;
844 public void EnabledTest2() {
845 // Check nesting of enabled calls
846 // OnEnabled is not called for disabled child controls
851 EnabledCalledCount = 0;
853 grandma = new Control();
854 parent = new Control();
855 child = new Control();
856 child.EnabledChanged += new EventHandler(EnabledTest2EnabledChanged);
858 grandma.Controls.Add(parent);
859 parent.Controls.Add(child);
860 grandma.Enabled = false;
862 Assert.AreEqual(1, EnabledCalledCount, "Child Enabled Event not properly fired");
863 grandma.Enabled = true;
864 Assert.AreEqual(2, EnabledCalledCount, "Child Enabled Event not properly fired");
865 child.Enabled = false;
866 grandma.Enabled = false;
867 Assert.AreEqual(3, EnabledCalledCount, "Child Enabled Event not properly fired");