imported everything from my branch (which is slightly harmless).
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / ControlTest.cs
1 //\r
2 // Copyright (c) 2005 Novell, Inc.\r
3 //\r
4 // Authors:\r
5 //      Ritvik Mayank (mritvik@novell.com)\r
6 //\r
7 \r
8 using System;\r
9 using System.Windows.Forms;\r
10 using System.Drawing;\r
11 using System.Threading;\r
12 using System.Reflection;\r
13 using System.Runtime.Remoting;\r
14 \r
15 using NUnit.Framework;\r
16 \r
17 [TestFixture]\r
18 public class ControlTest\r
19 {\r
20         [Test]\r
21         public void PubPropTest()\r
22         {\r
23             Control c = new Control();\r
24             Assert.AreEqual(false, c.AccessibilityObject == null, "#1");\r
25             Assert.AreEqual(false, c.AllowDrop , "#2");\r
26             Assert.AreEqual(AnchorStyles.Top | AnchorStyles.Left, c.Anchor, "#4");\r
27             Assert.AreEqual("Control", c.BackColor.Name , "#6");\r
28             Assert.AreEqual(null, c.BindingContext, "#7");\r
29             Assert.AreEqual(null, c.BackgroundImage, "#8");\r
30             Assert.AreEqual(0, c.Bottom, "#10");\r
31             //Assert.AreEqual (Rectangle (0,0, 100, 23) , c.Bounds , "#11");\r
32             //Assert.AreEqual ( BoundsSpecified.X  , c.Bounds , "#11");\r
33             Assert.AreEqual(false, c.CanFocus, "#12");\r
34             Assert.AreEqual(true, c.CanSelect, "#13");\r
35             Assert.AreEqual(false, c.Capture, "#14");\r
36             Assert.AreEqual(true, c.CausesValidation, "#15");\r
37             //Assert.AreEqual (false , c.ClientRectangle , "#16");\r
38             //Assert.AreEqual (false , c.ClientSize , "#17");\r
39             // Assert.AreEqual("Mono Project, Novell, Inc.", c.CompanyName, "#18");\r
40             Assert.AreEqual(null, c.Container, "#19");\r
41             Assert.AreEqual(false, c.ContainsFocus, "#20");\r
42             Assert.AreEqual(null, c.ContextMenu, "#21");\r
43             //Assert.AreEqual (Control+ControlCollection , c.Controls, "#22");\r
44             Assert.AreEqual(true, c.Created, "#23");\r
45             Assert.AreEqual(Cursors.Default, c.Cursor, "#24");\r
46             Assert.AreEqual(false, c.DataBindings == null, "#25");\r
47             //Assert.AreEqual(false, c.DefaultBackColor, "#25a");\r
48             //Assert.AreEqual(false, c.DefaultForeColor == null, "#25b");\r
49             //Assert.AreEqual(false, c.DefaultFont == null, "#25c");\r
50             //Assert.AreEqual (false , c.DisplayRectangle , "#26");\r
51             //<{X=0,Y=0,Width=100,Height=23}>\r
52             Assert.AreEqual(false, c.Disposing, "#27");\r
53             Assert.AreEqual(DockStyle.None, c.Dock, "#28");\r
54             Assert.AreEqual(true, c.Enabled, "#29");\r
55             Assert.AreEqual(false, c.Focused, "#31");\r
56             //Assert.AreEqual(FontFamily.GenericSansSerif, c.Font, "#32");\r
57             Assert.AreEqual(SystemColors.ControlText, c.ForeColor, "#33");\r
58             //Assert.AreEqual (IWin32Window.Handle , c.Handle, "#34");\r
59             Assert.AreEqual(false, c.HasChildren, "#35");\r
60             Assert.AreEqual(0, c.Height, "#36");\r
61             //Assert.AreEqual (false , c.ImeMode, "#41");\r
62             Assert.AreEqual(false, c.InvokeRequired, "#42");\r
63             Assert.AreEqual(false, c.IsAccessible, "#43");\r
64             Assert.AreEqual(false, c.IsDisposed, "#44");\r
65             Assert.AreEqual(true, c.IsHandleCreated, "#45");\r
66             Assert.AreEqual(0, c.Left, "#46");\r
67             Assert.AreEqual(Point.Empty, c.Location, "#47");\r
68             //Assert.AreEqual(Point.Empty, c.ModifierKeys, "#47a");\r
69             //Assert.AreEqual(Point.Empty, c.MousePosition, "#47b");\r
70             //Assert.AreEqual(Point.Empty, c.MouseButtons, "#47c");\r
71             Assert.AreEqual("", c.Name, "#48");\r
72             Assert.AreEqual(null, c.Parent, "#49");\r
73             //Assert.AreEqual("Novell Mono MWF", c.ProductName, "#52");\r
74             Assert.AreEqual("1.1.4322.573", c.ProductVersion, "#53");\r
75             Assert.AreEqual(false, c.RecreatingHandle, "#54");\r
76             Assert.AreEqual(null, c.Region, "#55");\r
77             Assert.AreEqual(0, c.Right, "#56");\r
78             Assert.AreEqual(RightToLeft.No, c.RightToLeft, "#57");\r
79             Assert.AreEqual(null, c.Site, "#58");\r
80             //Assert.AreEqual (false , c.Size, "#59");\r
81             //Assert.AreEqual(true , c.TabIndex , "#60");\r
82             //true , 0\r
83             Assert.AreEqual(true, c.TabStop, "#60a");\r
84             Assert.AreEqual(null, c.Tag, "#61");\r
85             Assert.AreEqual("", c.Text, "#62");\r
86             Assert.AreEqual(0, c.Top, "#64");\r
87             Assert.AreEqual(null, c.TopLevelControl, "#65");\r
88             Assert.AreEqual(true, c.Visible, "#67");\r
89             Assert.AreEqual(0, c.Width, "#68");\r
90                 \r
91         }\r
92     \r
93         [Test]\r
94         public void PubMethodTest2()\r
95         {\r
96            Control C1 = new Control();\r
97            Control ctl = new Control();\r
98            C1.Show() ;\r
99            Assert.AreEqual(true , C1.Visible , "#69");\r
100            Assert.AreEqual(false, C1.Contains(ctl) , "#70");\r
101            Assert.AreEqual("System.Windows.Forms.Control", C1.ToString() , "#71");\r
102         \r
103           //C1.Update ();\r
104           //Assert.AreEqual(false, C1 , "#70");\r
105           //Causes the control to redraw the invalidated regions within its client area.\r
106         \r
107           C1.Anchor = AnchorStyles.Top;\r
108           C1.SuspendLayout ();\r
109           C1.Anchor = AnchorStyles.Left ;\r
110           C1.ResumeLayout ();\r
111           Assert.AreEqual(AnchorStyles.Left , C1.Anchor, "#73");\r
112         \r
113           C1.SetBounds(10, 20, 30, 40) ;\r
114           Assert.AreEqual( 20 ,C1.Bounds.Top , "#74a");\r
115           Assert.AreEqual( 10 ,C1.Bounds.Left , "#74b");\r
116           Assert.AreEqual( 30 ,C1.Bounds.Width , "#74c");\r
117           Assert.AreEqual( 40 ,C1.Bounds.Height , "#74d");\r
118         \r
119           //C1.SendToBack() ;\r
120           //Assert.AreEqual( false , C1.Bounds , "#75");\r
121         \r
122         }\r
123 \r
124         [Test]\r
125         public void PubMethodTest3()\r
126         {\r
127            Control B = new Control();\r
128            //Form frm = new Form.ControlNativeWindow() ;\r
129            //B.SelectNextControl(frm)  ;\r
130            //Assert.AreEqual(true, B.TabStop , "#75");\r
131            //LayoutEventArgs l = LayoutEventArgs ();\r
132            //Layout L1 = B.PerformLayout(l) ;\r
133            //Anchor = AnchorStyles.Bottom\r
134            B.SuspendLayout() ;\r
135            B.Size = Size.Empty ;\r
136            B.Location = Point.Empty;\r
137            B.Anchor = AnchorStyles.Bottom;\r
138            B.Dock = DockStyle.Left;\r
139            B.ResumeLayout() ;\r
140            //Assert.AreEqual(false, B.Size , "#75a");\r
141            //Assert.AreEqual(false, B.Location , "#75b");\r
142            Assert.AreEqual(AnchorStyles.Top | AnchorStyles.Left , B.Anchor , "#75c");\r
143            Assert.AreEqual(DockStyle.None , B.Dock , "#75d");\r
144         \r
145         }\r
146     \r
147         [Test]\r
148         public void PubMethodTest4()\r
149         {\r
150            Control s1 = new Control();\r
151            Control s2 = new Control();\r
152            s1.Text = "abc";\r
153            s2.Text = "abc";\r
154            Assert.AreEqual(false, s1.Equals(s2), "#76");\r
155            Assert.AreEqual(true, s1.Equals(s1), "#77");\r
156         }\r
157         [Test]\r
158         public void PubMethodTest5()\r
159         {\r
160            Control r1 = new Control();\r
161            r1.Width = 40;\r
162            r1.Height = 20;\r
163            r1.Scale(2);\r
164            Assert.AreEqual(80, r1.Width, "#78");\r
165            Assert.AreEqual(40, r1.Height, "#79");\r
166         }\r
167     \r
168         [Test]\r
169         public void PubMethodTest6()\r
170         {\r
171            Control r1 = new Control();\r
172            r1.Text = "Hi" ;\r
173            r1.ResetText();\r
174            Assert.AreEqual("" , r1.Text , "#80");\r
175         }\r
176     \r
177         [Test]\r
178         public void PubMethodTest7()\r
179         {\r
180            Control r1 = new Control();\r
181            r1.RightToLeft = RightToLeft.Yes ;\r
182            r1.ResetRightToLeft() ;\r
183            Assert.AreEqual(RightToLeft.No , r1.RightToLeft , "#81");\r
184            r1.ImeMode = ImeMode.Off ;\r
185            r1.ResetImeMode () ;\r
186            Assert.AreEqual(ImeMode.NoControl , r1.ImeMode , "#82");\r
187            r1.ForeColor= SystemColors.GrayText ;\r
188            r1.ResetForeColor() ;\r
189            Assert.AreEqual(SystemColors.ControlText , r1.ForeColor , "#83");\r
190            //r1.Font = Font.FromHdc();\r
191            r1.ResetFont () ;\r
192            //Assert.AreEqual(FontFamily.GenericSansSerif , r1.Font , "#83");\r
193            r1.Cursor = Cursors.Hand ;\r
194            r1.ResetCursor () ;\r
195            Assert.AreEqual(Cursors.Default , r1.Cursor , "#83");\r
196            //r1.DataBindings = System.Windows.Forms.Binding ;\r
197            //r1.ResetBindings() ;\r
198            //Assert.AreEqual(ControlBindingsCollection , r1.DataBindings  , "#83");\r
199            r1.BackColor = System.Drawing.Color.Black ;\r
200            r1.ResetBackColor() ;\r
201            Assert.AreEqual( SystemColors.Control , r1.BackColor  , "#84");\r
202            r1.BackColor = System.Drawing.Color.Black ;\r
203            r1.Refresh() ;\r
204            Assert.AreEqual( null , r1.Region , "#85");\r
205            Rectangle M = new Rectangle(10, 20, 30 ,40);\r
206            r1.RectangleToScreen(M) ;\r
207            Assert.AreEqual( null , r1.Region , "#86");\r
208     \r
209         }\r
210     \r
211         [Test]\r
212         public void PubMethodTest8()\r
213         {\r
214            Label N = new Label();\r
215            N.Left = 10;\r
216            N.Top  = 12;\r
217            N.Visible = true;\r
218            Point p = new Point (10,10);\r
219            Point p1 = N.PointToScreen(p) ;\r
220            Point p2 = N.PointToClient (p1);\r
221            Assert.AreEqual (p, p2, "#1 converting client->screen->client should not loose data");\r
222 \r
223         }\r
224     \r
225         [Test]\r
226         public void ContainsTest ()\r
227         {\r
228             Control t = new Control ();\r
229             Control s = new Control ();\r
230             t.Controls.Add (s);\r
231 \r
232             Assert.AreEqual (true, t.Contains (s), "#1 should contain");\r
233             Assert.AreEqual (false, s.Contains (t), "#2 should not contain");\r
234             Assert.AreEqual (false, s.Contains (null), "#3 should not contain");\r
235             Assert.AreEqual (false, t.Contains (new Control ()), "#4 should not contain");\r
236         }\r
237 \r
238         [Test]\r
239         public void CreateHandleTest ()\r
240         {\r
241             Control parent = null, child = null;\r
242             try {\r
243                 parent = new Control ();\r
244                 parent.Visible = true;\r
245                 child = new Control ();\r
246                 parent.Controls.Add (child);\r
247 \r
248                 Assert.IsFalse (parent.IsHandleCreated, "#1 handle should not be created while ctor");\r
249                 Assert.IsFalse (child.IsHandleCreated, "#2 handle should not be created while ctor");\r
250 \r
251                 parent.CreateControl ();\r
252                 Assert.IsNotNull (parent.Handle, "#3 should create a handle");\r
253                 Assert.IsNotNull (child.Handle, "#4 should create a handle");\r
254                 Assert.IsTrue (parent.IsHandleCreated, "#5 should have created handle");\r
255                 Assert.IsTrue (child.IsHandleCreated, "#6 should have created handle");\r
256             } finally {\r
257                 if (parent != null)\r
258                     parent.Dispose ();\r
259                 if (child != null)\r
260                     child.Dispose ();\r
261             }\r
262 \r
263             // Accessing Handle Property creates the handle\r
264             try {\r
265                 parent = new Control ();\r
266                 parent.Visible = true;\r
267                 child = new Control ();\r
268                 parent.Controls.Add (child);\r
269                 Assert.IsFalse (parent.IsHandleCreated, "#7 handle is not created while ctor");\r
270                 Assert.IsFalse (child.IsHandleCreated, "#8 handle is not created while ctor");\r
271                 Assert.IsNotNull (parent.Handle, "#9 should create a handle");\r
272                 Assert.IsTrue (parent.IsHandleCreated, "#10 should have created handle");\r
273                 Assert.IsTrue (child.IsHandleCreated, "#11 should have created handle");\r
274             } finally {\r
275                 if (parent != null)\r
276                     parent.Dispose ();\r
277                 if (child != null)\r
278                     child.Dispose ();\r
279             }\r
280         }\r
281 \r
282         [Test]\r
283         public void CreateGraphicsTest ()\r
284         {\r
285             Graphics g = null;\r
286             Pen p = null;\r
287             try {\r
288                 Control c = new Control ();\r
289                 c.SetBounds (0,0, 20, 20);\r
290                 g = c.CreateGraphics ();\r
291                 Assert.IsNotNull (g, "#1 should create a graphics object");\r
292                 g.DrawLine (p = new Pen (Color.Red), 10, 10, 20, 20);\r
293             } finally {\r
294                 if (p != null)\r
295                     p.Dispose ();\r
296                 if (g != null)\r
297                     g.Dispose ();\r
298             }\r
299         }\r
300 \r
301         bool delegateCalled = false;\r
302         public delegate void TestDelegate ();\r
303 \r
304         [Test]\r
305         public void InvokeTest ()\r
306         {\r
307             Control c = null;\r
308             try {\r
309                 c = new Control ();\r
310                 IAsyncResult result;\r
311                 try {\r
312                     result = c.BeginInvoke (new TestDelegate (delegate_call));\r
313                     c.EndInvoke (result);\r
314                     Assert.Fail ("#1 should not invoke without window handle");\r
315                 } catch (InvalidOperationException) { }\r
316                 c.CreateControl ();\r
317                 result = c.BeginInvoke (new TestDelegate (delegate_call));\r
318                 c.EndInvoke (result);\r
319 \r
320                 Assert.IsTrue (delegateCalled, "#1 value should have been set to true");\r
321             } finally {\r
322                 if (c != null)\r
323                     c.Dispose ();\r
324             }\r
325         }\r
326 \r
327         public void delegate_call ()\r
328         {\r
329             delegateCalled = true;\r
330         }\r
331 \r
332         [Test]\r
333         public void FindFormTest ()\r
334         {\r
335             Form f = new Form ();\r
336             f.Name = "form";\r
337             Control c = null;\r
338             try {\r
339                 f.Controls.Add (c = new Control ());\r
340                 Assert.AreEqual (f.Name, c.FindForm ().Name, "#1 should find the parent form");\r
341 \r
342                 f.Controls.Remove (c);\r
343 \r
344                 GroupBox g = new GroupBox ();\r
345                 g.Name = "box";\r
346                 f.Controls.Add (g);\r
347                 g.Controls.Add (c);\r
348 \r
349                 Assert.AreEqual (f.Name, f.FindForm ().Name, "#2 still should find the form");\r
350 \r
351                 g.Controls.Remove (c);\r
352                 Assert.IsNull(c.FindForm (), "#3 should be null");\r
353 \r
354             } finally {\r
355                 if (c != null)\r
356                     c.Dispose ();\r
357                 if (f != null)\r
358                     f.Dispose ();\r
359             }\r
360         }\r
361 \r
362         [Test]\r
363         public void FocusTest ()\r
364         {\r
365             Form f = null;\r
366             Button c = null, d = null;\r
367             try {\r
368                 f = new Form ();\r
369                 f.Visible = true;\r
370                 c = new Button ();\r
371                 c.Visible = true;\r
372                 f.Controls.Add (c);\r
373                 \r
374                 d = new Button ();\r
375                 d.Visible = false;\r
376                 f.Controls.Add (d);\r
377                 \r
378                 Assert.IsTrue (c.CanFocus, "#1 button should be able to be focused");\r
379                 Assert.IsFalse (c.Focused, "#2 button should not be focussed initially");\r
380                 c.Focus ();\r
381                 Assert.IsTrue (c.Focused, "#3 button should be focussed after Focus ()");\r
382                 d.Focus ();\r
383                 Assert.IsFalse (d.Focused, "#4 invisible button should not be focussed");\r
384 \r
385                 d.Visible = true;\r
386                 d.Focus ();\r
387                 Assert.IsTrue (d.Focused, "#5 button should be focussed after Focus () & visible");\r
388                 Assert.IsFalse (c.Focused, "#6 button should lost focus");\r
389 \r
390                 c.Enabled = false;\r
391                 Assert.IsFalse (c.Focused, "#7 disabled button should not be focused");\r
392             } finally {\r
393                 if (f != null)\r
394                     f.Dispose ();\r
395                 if (c != null)\r
396                     c.Dispose ();\r
397                 if (d != null)\r
398                     d.Dispose ();\r
399             }\r
400         }\r
401 \r
402         [Test]\r
403         public void FromChildHandleTest ()\r
404         {\r
405             // FIXME : how to make a control to have more than one handle?\r
406             Control c = null;\r
407             try {\r
408                 c = new Control ();\r
409                 c.Name = "hello";\r
410                 IntPtr handle = c.Handle;\r
411                 Assert.AreEqual (c.Name, Control.FromChildHandle (handle).Name, "#1 handle should be able to relate to control");\r
412 \r
413                 handle = IntPtr.Zero;\r
414                 Assert.IsNull (Control.FromChildHandle (handle), "#2 should return null");\r
415             } finally {\r
416                 if (c != null)\r
417                     c.Dispose ();\r
418             }\r
419         }\r
420 \r
421         [Test]\r
422         public void FromHandleTest ()\r
423         {\r
424             Control c = null;\r
425             try {\r
426                 c = new Control ();\r
427                 c.Name = "hello";\r
428                 IntPtr handle = c.Handle;\r
429                 Assert.AreEqual (c.Name, Control.FromHandle (handle).Name, "#1 handle should be able to relate to control");\r
430 \r
431                 handle = IntPtr.Zero;\r
432                 Assert.IsNull (Control.FromHandle (handle), "#2 should return null");\r
433             } finally {\r
434                 if (c != null)\r
435                     c.Dispose ();\r
436             }\r
437         }\r
438 \r
439         [Test]\r
440         public void GetChildAtPointTest ()\r
441         {\r
442             Control c = null, d = null, e = null;\r
443             try {\r
444                 c = new Control ();\r
445                 c.Name = "c1";\r
446                 c.SetBounds (0, 0, 100, 100);\r
447 \r
448                 d = new Control ();\r
449                 d.Name = "d1";\r
450                 d.SetBounds (10, 10, 40, 40);\r
451                 c.Controls.Add (d);\r
452 \r
453                 e = new Control ();\r
454                 e.Name = "e1";\r
455                 e.SetBounds (55, 55, 10, 10);\r
456 \r
457                 Control l = c.GetChildAtPoint (new Point (15, 15));\r
458                 Assert.AreEqual (d.Name, l.Name, "#1 should return the cild");\r
459                 Assert.IsFalse (e.Name == l.Name, "#2 e is not child of c");\r
460 \r
461                 l = c.GetChildAtPoint (new Point (57, 57));\r
462                 Assert.IsNull (l, "#3 no control at 55");\r
463 \r
464                 l = c.GetChildAtPoint (new Point (10, 10));\r
465                 Assert.AreEqual (d.Name, l.Name, "#4 should return the child even if it falls on border");\r
466 \r
467                 /* net 2.0 overload\r
468 #if NET_2_0\r
469                 c.Controls.Add (e);\r
470                 e.Visible = false;\r
471                 l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Invisible);\r
472                 Assert.IsNull (l, "#4 should ignore of type invisible");\r
473 \r
474                 e.Visible = true;\r
475                 l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Invisible);\r
476                 Assert.AreSame (e.Name, l.Name, "#4 should show visible controls");\r
477 #endif // NET_2_0                 \r
478                  */\r
479             } finally {\r
480                 if (c != null)\r
481                     c.Dispose ();\r
482                 if (d != null)\r
483                     d.Dispose ();\r
484             }\r
485         }\r
486 \r
487    }\r