BindingFlags.Public needed here as Exception.HResult is now public in .NET 4.5. This...
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / ControlTest.cs
1 //
2 // Copyright (c) 2005 Novell, Inc.
3 //
4 // Authors:
5 //      Ritvik Mayank (mritvik@novell.com)
6 //              Stefan Noack (noackstefan@googlemail.com)
7 //
8
9 using System;
10 using System.Collections;
11 using System.ComponentModel;
12 using System.Drawing;
13 using System.Reflection;
14 using System.Runtime.Remoting;
15 using System.Threading;
16 using System.Windows.Forms;
17 #if NET_2_0
18 using System.Windows.Forms.Layout;
19 #endif
20
21 using NUnit.Framework;
22 using CategoryAttribute = NUnit.Framework.CategoryAttribute;
23
24 namespace MonoTests.System.Windows.Forms
25 {
26         [TestFixture]
27         public class ControlTest : TestHelper
28         {
29                 [Test] // .ctor ()
30                 public void Constructor1 ()
31                 {
32                         MockControl c = new MockControl ();
33
34                         Assert.AreEqual (0, c.OnLocationChangedCount, "OnLocationChangedCount");
35                         Assert.AreEqual (0, c.OnResizeCount, "OnResizeCount");
36                         Assert.AreEqual (0, c.OnSizeChangedCount, "OnSizeChangedCount");
37                         Assert.AreEqual (0, c.Height, "Height");
38                         Assert.AreEqual (0, c.Left, "Left");
39                         Assert.AreEqual (string.Empty, c.Name, "Name");
40                         Assert.IsNull (c.Parent, "Parent");
41                         Assert.AreEqual (string.Empty, c.Text, "#A:Text");
42                         Assert.AreEqual (0, c.Top, "Top");
43                         Assert.AreEqual (0, c.Width, "Width");
44                 }
45
46                 [Test] // .ctor (String)
47                 public void Constructor2 ()
48                 {
49                         MockControl c = new MockControl ((string) null);
50
51                         Assert.AreEqual (0, c.OnLocationChangedCount, "#A:OnLocationChangedCount");
52                         Assert.AreEqual (0, c.OnResizeCount, "#A:OnResizeCount");
53                         Assert.AreEqual (0, c.OnSizeChangedCount, "#A:OnSizeChangedCount");
54                         Assert.AreEqual (0, c.Height, "#A:Height");
55                         Assert.AreEqual (0, c.Left, "#A:Left");
56                         Assert.AreEqual (string.Empty, c.Name, "#A:Name");
57                         Assert.IsNull (c.Parent, "#A:Parent");
58                         Assert.AreEqual (string.Empty, c.Text, "#A:Text");
59                         Assert.AreEqual (0, c.Top, "#A:Top");
60                         Assert.AreEqual (0, c.Width, "#A:Width");
61
62                         c = new MockControl ("child");
63
64                         Assert.AreEqual (0, c.OnLocationChangedCount, "#B:OnLocationChangedCount");
65                         Assert.AreEqual (0, c.OnResizeCount, "#B:OnResizeCount");
66                         Assert.AreEqual (0, c.OnSizeChangedCount, "#B:OnSizeChangedCount");
67                         Assert.AreEqual (0, c.Height, "#B:Height");
68                         Assert.AreEqual (0, c.Left, "#B:Left");
69                         Assert.AreEqual (string.Empty, c.Name, "#B:Name");
70                         Assert.IsNull (c.Parent, "#B:Parent");
71                         Assert.AreEqual ("child", c.Text, "#B:Text");
72                         Assert.AreEqual (0, c.Top, "#B:Top");
73                         Assert.AreEqual (0, c.Width, "#B:Width");
74                 }
75
76                 [Test] // .ctor (Control, String)
77                 public void Constructor3 ()
78                 {
79                         Control parent = new Control ("parent");
80                         MockControl c = new MockControl ((Control) null, (string) null);
81
82                         Assert.AreEqual (0, c.OnLocationChangedCount, "#A:OnLocationChangedCount");
83                         Assert.AreEqual (0, c.OnResizeCount, "#A:OnResizeCount");
84                         Assert.AreEqual (0, c.OnSizeChangedCount, "#A:OnSizeChangedCount");
85                         Assert.AreEqual (0, c.Height, "#A:Height");
86                         Assert.AreEqual (0, c.Left, "#A:Left");
87                         Assert.AreEqual (string.Empty, c.Name, "#A:Name");
88                         Assert.IsNull (c.Parent, "#A:Parent");
89                         Assert.AreEqual (string.Empty, c.Text, "#A:Text");
90                         Assert.AreEqual (0, c.Top, "#A:Top");
91                         Assert.AreEqual (0, c.Width, "#A:Width");
92
93                         c = new MockControl ((Control) null, "child");
94
95                         Assert.AreEqual (0, c.OnLocationChangedCount, "#B:OnLocationChangedCount");
96                         Assert.AreEqual (0, c.OnResizeCount, "#B:OnResizeCount");
97                         Assert.AreEqual (0, c.OnSizeChangedCount, "#B:OnSizeChangedCount");
98                         Assert.AreEqual (0, c.Height, "#B:Height");
99                         Assert.AreEqual (0, c.Left, "#B:Left");
100                         Assert.AreEqual (string.Empty, c.Name, "#B:Name");
101                         Assert.IsNull (c.Parent, "#B:Parent");
102                         Assert.AreEqual ("child", c.Text, "#B:Text");
103                         Assert.AreEqual (0, c.Top, "#B:Top");
104                         Assert.AreEqual (0, c.Width, "#B:Width");
105
106                         c = new MockControl (parent, (string) null);
107
108                         Assert.AreEqual (0, c.OnLocationChangedCount, "#C:OnLocationChangedCount");
109                         Assert.AreEqual (0, c.OnResizeCount, "#C:OnResizeCount");
110                         Assert.AreEqual (0, c.OnSizeChangedCount, "#C:OnSizeChangedCount");
111                         Assert.AreEqual (0, c.Height, "#C:Height");
112                         Assert.AreEqual (0, c.Left, "#C:Left");
113                         Assert.AreEqual (string.Empty, c.Name, "#C:Name");
114                         Assert.AreSame (parent, c.Parent, "#C:Parent");
115                         Assert.AreEqual (string.Empty, c.Text, "#C:Text");
116                         Assert.AreEqual (0, c.Top, "#C:Top");
117                         Assert.AreEqual (0, c.Width, "#C:Width");
118
119                         c = new MockControl (parent, "child");
120
121                         Assert.AreEqual (0, c.OnLocationChangedCount, "#D:OnLocationChangedCount");
122                         Assert.AreEqual (0, c.OnResizeCount, "#D:OnResizeCount");
123                         Assert.AreEqual (0, c.OnSizeChangedCount, "#D:OnSizeChangedCount");
124                         Assert.AreEqual (0, c.Height, "#D:Height");
125                         Assert.AreEqual (0, c.Left, "#D:Left");
126                         Assert.AreEqual (string.Empty, c.Name, "#D:Name");
127                         Assert.AreSame (parent, c.Parent, "#D:Parent");
128                         Assert.AreEqual ("child", c.Text, "#D:Text");
129                         Assert.AreEqual (0, c.Top, "#D:Top");
130                         Assert.AreEqual (0, c.Width, "#D:Width");
131                 }
132
133                 [Test] // .ctor (String, Int32, Int32, Int32, Int32)
134                 public void Constructor4 ()
135                 {
136                         MockControl c = new MockControl ((string) null, 0, 0, 0, 0);
137
138                         Assert.AreEqual (0, c.OnLocationChangedCount, "#A:OnLocationChangedCount");
139                         Assert.AreEqual (0, c.OnResizeCount, "#A:OnResizeCount");
140                         Assert.AreEqual (0, c.OnSizeChangedCount, "#A:OnSizeChangedCount");
141                         Assert.AreEqual (0, c.Height, "#A:Height");
142                         Assert.AreEqual (0, c.Left, "#A:Left");
143                         Assert.AreEqual (string.Empty, c.Name, "#A:Name");
144                         Assert.IsNull (c.Parent, "#A:Parent");
145                         Assert.AreEqual (string.Empty, c.Text, "#A:Text");
146                         Assert.AreEqual (0, c.Top, "#A:Top");
147                         Assert.AreEqual (0, c.Width, "#A:Width");
148
149                         c = new MockControl ((string) null, 1, 0, 0, 0);
150
151                         Assert.AreEqual (1, c.OnLocationChangedCount, "#B:OnLocationChangedCount");
152                         Assert.AreEqual (0, c.OnResizeCount, "#B:OnResizeCount");
153                         Assert.AreEqual (0, c.OnSizeChangedCount, "#B:OnSizeChangedCount");
154                         Assert.AreEqual (0, c.Height, "#B:Height");
155                         Assert.AreEqual (1, c.Left, "#B:Left");
156                         Assert.AreEqual (string.Empty, c.Name, "#B:Name");
157                         Assert.IsNull (c.Parent, "#B:Parent");
158                         Assert.AreEqual (string.Empty, c.Text, "#B:Text");
159                         Assert.AreEqual (0, c.Top, "#B:Top");
160                         Assert.AreEqual (0, c.Width, "#B:Width");
161
162                         c = new MockControl ("child", 0, 1, 0, 0);
163
164                         Assert.AreEqual (1, c.OnLocationChangedCount, "#C:OnLocationChangedCount");
165                         Assert.AreEqual (0, c.OnResizeCount, "#C:OnResizeCount");
166                         Assert.AreEqual (0, c.OnSizeChangedCount, "#C:OnSizeChangedCount");
167                         Assert.AreEqual (0, c.Height, "#C:Height");
168                         Assert.AreEqual (0, c.Left, "#C:Left");
169                         Assert.AreEqual (string.Empty, c.Name, "#C:Name");
170                         Assert.IsNull (c.Parent, "#C:Parent");
171                         Assert.AreEqual ("child", c.Text, "#C:Text");
172                         Assert.AreEqual (1, c.Top, "#C:Top");
173                         Assert.AreEqual (0, c.Width, "#C:Width");
174
175                         c = new MockControl ("child", 0, 0, 1, 0);
176
177                         Assert.AreEqual (0, c.OnLocationChangedCount, "#D:OnLocationChangedCount");
178                         Assert.AreEqual (1, c.OnResizeCount, "#D:OnResizeCount");
179                         Assert.AreEqual (1, c.OnSizeChangedCount, "#D:OnSizeChangedCount");
180                         Assert.AreEqual (0, c.Height, "#D:Height");
181                         Assert.AreEqual (0, c.Left, "#D:Left");
182                         Assert.AreEqual (string.Empty, c.Name, "#D:Name");
183                         Assert.IsNull (c.Parent, "#D:Parent");
184                         Assert.AreEqual ("child", c.Text, "#D:Text");
185                         Assert.AreEqual (0, c.Top, "#D:Top");
186                         Assert.AreEqual (1, c.Width, "#D:Width");
187
188                         c = new MockControl ("child", 0, 0, 0, 1);
189
190                         Assert.AreEqual (0, c.OnLocationChangedCount, "#E:OnLocationChangedCount");
191                         Assert.AreEqual (1, c.OnResizeCount, "#E:OnResizeCount");
192                         Assert.AreEqual (1, c.OnSizeChangedCount, "#E:OnSizeChangedCount");
193                         Assert.AreEqual (1, c.Height, "#E:Height");
194                         Assert.AreEqual (0, c.Left, "#E:Left");
195                         Assert.AreEqual (string.Empty, c.Name, "#E:Name");
196                         Assert.IsNull (c.Parent, "#E:Parent");
197                         Assert.AreEqual ("child", c.Text, "#E:Text");
198                         Assert.AreEqual (0, c.Top, "#E:Top");
199                         Assert.AreEqual (0, c.Width, "#E:Width");
200
201                         c = new MockControl ("child", 1, 0, 1, 0);
202
203                         Assert.AreEqual (1, c.OnLocationChangedCount, "#F:OnLocationChangedCount");
204                         Assert.AreEqual (1, c.OnResizeCount, "#F:OnResizeCount");
205                         Assert.AreEqual (1, c.OnSizeChangedCount, "#F:OnSizeChangedCount");
206                         Assert.AreEqual (0, c.Height, "#F:Height");
207                         Assert.AreEqual (1, c.Left, "#F:Left");
208                         Assert.AreEqual (string.Empty, c.Name, "#F:Name");
209                         Assert.IsNull (c.Parent, "#F:Parent");
210                         Assert.AreEqual ("child", c.Text, "#F:Text");
211                         Assert.AreEqual (0, c.Top, "#F:Top");
212                         Assert.AreEqual (1, c.Width, "#F:Width");
213
214                         c = new MockControl ("child", 0, 1, 0, 1);
215
216                         Assert.AreEqual (1, c.OnLocationChangedCount, "#G:OnLocationChangedCount");
217                         Assert.AreEqual (1, c.OnResizeCount, "#G:OnResizeCount");
218                         Assert.AreEqual (1, c.OnSizeChangedCount, "#G:OnSizeChangedCount");
219                         Assert.AreEqual (1, c.Height, "#G:Height");
220                         Assert.AreEqual (0, c.Left, "#G:Left");
221                         Assert.AreEqual (string.Empty, c.Name, "#G:Name");
222                         Assert.IsNull (c.Parent, "#G:Parent");
223                         Assert.AreEqual ("child", c.Text, "#G:Text");
224                         Assert.AreEqual (1, c.Top, "#G:Top");
225                         Assert.AreEqual (0, c.Width, "#G:Width");
226                 }
227
228                 [Test] // .ctor (Control, String, Int32, Int32, Int32, Int32)
229                 public void Constructor5 ()
230                 {
231                         Control parent = new Control ("parent");
232                         MockControl c = new MockControl ((Control) null,
233                                 (string) null, 0, 0, 0, 0);
234
235                         Assert.AreEqual (0, c.OnLocationChangedCount, "#A:OnLocationChangedCount");
236                         Assert.AreEqual (0, c.OnResizeCount, "#A:OnResizeCount");
237                         Assert.AreEqual (0, c.OnSizeChangedCount, "#A:OnSizeChangedCount");
238                         Assert.AreEqual (0, c.Height, "#A:Height");
239                         Assert.AreEqual (0, c.Left, "#A:Left");
240                         Assert.AreEqual (string.Empty, c.Name, "#A:Name");
241                         Assert.IsNull (c.Parent, "#A:Parent");
242                         Assert.AreEqual (string.Empty, c.Text, "#A:Text");
243                         Assert.AreEqual (0, c.Top, "#A:Top");
244                         Assert.AreEqual (0, c.Width, "#A:Width");
245
246                         c = new MockControl (parent, (string) null, 1, 0, 0, 0);
247
248                         Assert.AreEqual (1, c.OnLocationChangedCount, "#B:OnLocationChangedCount");
249                         Assert.AreEqual (0, c.OnResizeCount, "#B:OnResizeCount");
250                         Assert.AreEqual (0, c.OnSizeChangedCount, "#B:OnSizeChangedCount");
251                         Assert.AreEqual (0, c.Height, "#B:Height");
252                         Assert.AreEqual (1, c.Left, "#B:Left");
253                         Assert.AreEqual (string.Empty, c.Name, "#B:Name");
254                         Assert.AreSame (parent, c.Parent, "#B:Parent");
255                         Assert.AreEqual (string.Empty, c.Text, "#B:Text");
256                         Assert.AreEqual (0, c.Top, "#B:Top");
257                         Assert.AreEqual (0, c.Width, "#B:Width");
258
259                         c = new MockControl ((Control) null, "child", 0, 1, 0, 0);
260
261                         Assert.AreEqual (1, c.OnLocationChangedCount, "#C:OnLocationChangedCount");
262                         Assert.AreEqual (0, c.OnResizeCount, "#C:OnResizeCount");
263                         Assert.AreEqual (0, c.OnSizeChangedCount, "#C:OnSizeChangedCount");
264                         Assert.AreEqual (0, c.Height, "#C:Height");
265                         Assert.AreEqual (0, c.Left, "#C:Left");
266                         Assert.AreEqual (string.Empty, c.Name, "#C:Name");
267                         Assert.IsNull (c.Parent, "#C:Parent");
268                         Assert.AreEqual ("child", c.Text, "#C:Text");
269                         Assert.AreEqual (1, c.Top, "#C:Top");
270                         Assert.AreEqual (0, c.Width, "#C:Width");
271
272                         c = new MockControl (parent, "child", 0, 0, 1, 0);
273
274                         Assert.AreEqual (0, c.OnLocationChangedCount, "#D:OnLocationChangedCount");
275                         Assert.AreEqual (1, c.OnResizeCount, "#D:OnResizeCount");
276                         Assert.AreEqual (1, c.OnSizeChangedCount, "#D:OnSizeChangedCount");
277                         Assert.AreEqual (0, c.Height, "#D:Height");
278                         Assert.AreEqual (0, c.Left, "#D:Left");
279                         Assert.AreEqual (string.Empty, c.Name, "#D:Name");
280                         Assert.AreSame (parent, c.Parent, "#D:Parent");
281                         Assert.AreEqual ("child", c.Text, "#D:Text");
282                         Assert.AreEqual (0, c.Top, "#D:Top");
283                         Assert.AreEqual (1, c.Width, "#D:Width");
284
285                         c = new MockControl (parent, "child", 0, 0, 0, 1);
286
287                         Assert.AreEqual (0, c.OnLocationChangedCount, "#E:OnLocationChangedCount");
288                         Assert.AreEqual (1, c.OnResizeCount, "#E:OnResizeCount");
289                         Assert.AreEqual (1, c.OnSizeChangedCount, "#E:OnSizeChangedCount");
290                         Assert.AreEqual (1, c.Height, "#E:Height");
291                         Assert.AreEqual (0, c.Left, "#E:Left");
292                         Assert.AreEqual (string.Empty, c.Name, "#E:Name");
293                         Assert.AreSame (parent, c.Parent, "#E:Parent");
294                         Assert.AreEqual ("child", c.Text, "#E:Text");
295                         Assert.AreEqual (0, c.Top, "#E:Top");
296                         Assert.AreEqual (0, c.Width, "#E:Width");
297
298                         c = new MockControl (parent, "child", 1, 0, 1, 0);
299
300                         Assert.AreEqual (1, c.OnLocationChangedCount, "#F:OnLocationChangedCount");
301                         Assert.AreEqual (1, c.OnResizeCount, "#F:OnResizeCount");
302                         Assert.AreEqual (1, c.OnSizeChangedCount, "#F:OnSizeChangedCount");
303                         Assert.AreEqual (0, c.Height, "#F:Height");
304                         Assert.AreEqual (1, c.Left, "#F:Left");
305                         Assert.AreEqual (string.Empty, c.Name, "#F:Name");
306                         Assert.AreSame (parent, c.Parent, "#F:Parent");
307                         Assert.AreEqual ("child", c.Text, "#F:Text");
308                         Assert.AreEqual (0, c.Top, "#F:Top");
309                         Assert.AreEqual (1, c.Width, "#F:Width");
310
311                         c = new MockControl (parent, "child", 0, 1, 0, 1);
312
313                         Assert.AreEqual (1, c.OnLocationChangedCount, "#G:OnLocationChangedCount");
314                         Assert.AreEqual (1, c.OnResizeCount, "#G:OnResizeCount");
315                         Assert.AreEqual (1, c.OnSizeChangedCount, "#G:OnSizeChangedCount");
316                         Assert.AreEqual (1, c.Height, "#G:Height");
317                         Assert.AreEqual (0, c.Left, "#G:Left");
318                         Assert.AreEqual (string.Empty, c.Name, "#G:Name");
319                         Assert.AreSame (parent, c.Parent, "#G:Parent");
320                         Assert.AreEqual ("child", c.Text, "#G:Text");
321                         Assert.AreEqual (1, c.Top, "#G:Top");
322                         Assert.AreEqual (0, c.Width, "#G:Width");
323                 }
324
325                 [Test]
326                 public void DisposeTest ()
327                 {
328                         ControlDisposeTester control = new ControlDisposeTester ();
329                         control.Visible = true;
330                         control.Dispose ();
331
332                         try {
333                                 control.CreateControl ();
334                         } catch (ObjectDisposedException ex) {
335                                 Console.WriteLine (ex);
336                                 Assert.Fail ("#1");
337                         }
338                         Assert.IsFalse (control.IsHandleCreated, "#2");
339
340                         // The control remains Visible until WM_DESTROY is received.
341                         Assert.IsTrue (control.Visible, "#3");
342
343                         try {
344                                 control.InvokeCreateHandle ();
345                                 Assert.Fail ("#4");
346                         } catch (ObjectDisposedException ex) {
347                                 //Console.WriteLine (ex);
348                         }
349                 }
350
351                 private class ControlDisposeTester : Control
352                 {
353                         public void InvokeCreateHandle ()
354                         {
355                                 CreateHandle ();
356                         }
357                 }
358 #if NET_2_0
359                 [Test]
360                 public void AutoSizeTest ()
361                 {
362                         ControlAutoSizeTester c = new ControlAutoSizeTester (new Size (23, 17), AutoSizeMode.GrowAndShrink);
363                         
364                         Form f = new Form();
365                         f.Size = new Size (200, 200);
366                         c.Parent = f;
367                         f.Show();
368                         
369                         Size s = new Size (42, 42);
370                         c.Size = s;
371                         
372                         Point l = new Point (10, 10);
373                         c.Location = l;
374                         
375                         //Check wether normal size setting is OK
376                         Assert.AreEqual (s, c.Size, "#S1");
377                         
378                         //Check wether size remains without GetPreferredSize implemented even when AutoSize turned on.
379                         c.AutoSize = true;
380                         f.PerformLayout();
381                         Assert.AreEqual (s, c.Size, "#S2");
382                         
383                         //Simulate a Control implementing GetPreferredSize
384                         c.UseCustomPrefSize = true;
385                         f.PerformLayout();
386                         
387                         //Check wether size shrinks to preferred size
388                         Assert.AreEqual (c.CustomPrefSize, c.Size, "#S3");
389                         //Check wether Location stays constant
390                         Assert.AreEqual (l, c.Location, "#L1");
391                         
392                         //Check wether Dock is respected
393                         c.Dock = DockStyle.Bottom;
394                         Assert.AreEqual (f.ClientSize.Width, c.Width, "#D1");
395                         
396                         //Check wether size shrinks to preferred size again
397                         c.Dock = DockStyle.None;
398                         Assert.AreEqual (c.CustomPrefSize, c.Size, "#S4");
399                         
400                         //Check wether Anchor is respected for adjusting Locatioon
401                         c.Anchor = AnchorStyles.Bottom;
402                         f.Height += 50;
403                         Assert.AreEqual (l.Y + 50, c.Top, "#A1");
404                         //Check wether size is still OK
405                         Assert.AreEqual (c.CustomPrefSize, c.Size, "#S5");
406                         
407                         
408                         //just tidy up
409                         c.Anchor = AnchorStyles.Top | AnchorStyles.Left;
410                         c.Location = l;
411                         
412                         //Check wether shrinking to zero is possible 
413                         c.CustomPrefSize = new Size (0, 0);
414                         f.PerformLayout();
415                         Assert.AreEqual (c.CustomPrefSize, c.Size, "#S6");
416                         
417                         //Check wether MinimumSize is honored
418                         c.MinimumSize = new Size (10, 12);
419                         c.CustomPrefSize = new Size (5, 5);
420                         f.PerformLayout();
421                         Assert.AreEqual (c.MinimumSize, c.Size, "#S7");
422                         c.MinimumSize = new Size (0, 0);
423                         
424                         //Check wether MaximumSize is honored
425                         c.MaximumSize = new Size (100, 120); 
426                         c.CustomPrefSize = new Size (500, 500);
427                         f.PerformLayout();
428                         Assert.AreEqual (c.MaximumSize, c.Size, "#S8");
429                         
430                         //Check wether shrinking does not happen when GrowOnly
431                         c.AutoSize = false;
432                         s = new Size (23, 23);
433                         c.Size = s;
434                         c.CustomPrefSize = new Size (5, 5);
435                         c.AutoSizeMode = AutoSizeMode.GrowOnly;
436                         c.AutoSize = true;
437                         f.PerformLayout();
438                         Assert.AreEqual (s, c.Size, "#S9");
439                         f.Close ();
440                 }
441                 
442                 public class ControlAutoSizeTester : Control {
443                         
444
445                         public ControlAutoSizeTester (Size customPrefSize, AutoSizeMode autoSizeMode)
446                         {
447                                 custom_prefsize = customPrefSize;
448                                 AutoSizeMode = autoSizeMode;
449                         }
450                         
451                         public AutoSizeMode AutoSizeMode {
452                                 set {
453                                         base.SetAutoSizeMode (value);
454                                 }
455                         }
456
457                         private bool use_custom_prefsize = false;
458                         public bool UseCustomPrefSize {
459                                 set {
460                                         use_custom_prefsize = value;
461                                 }
462                         }
463                         
464                         private Size custom_prefsize;
465                         
466                         public Size CustomPrefSize {
467                                 get {
468                                         return custom_prefsize;
469                                 }
470                                 set {
471                                         custom_prefsize = value;
472                                 }
473                         }
474                         
475                         
476                         public override Size GetPreferredSize (Size proposedSize)
477                         {
478                                 if (use_custom_prefsize) {
479                                         return custom_prefsize;
480                                 } else {        
481                                         return base.GetPreferredSize(proposedSize);
482                                 }
483                         }
484                         
485                 }
486 #endif
487                 
488                 [Test]
489                 public void Bug82748 ()
490                 {
491                         Form f = new Form ();
492                         f.ShowInTaskbar = false;
493                         
494                         Control c = new Control ();
495                         c.Size = new Size (100, 100);
496                         
497                         Control c2 = new Control ();
498                         c2.Size = c.Size;
499                         c2.Controls.Add (c);
500                         
501                         c.Anchor = AnchorStyles.Right;
502                         
503                         f.Controls.Add (c);
504                         
505                         f.Show ();
506                         
507                         Assert.AreEqual (0, c.Left, "A1");
508                         
509                         f.Close ();
510                         f.Dispose ();
511                 }
512                 
513                 [Test]
514                 public void InvokeTestParentHandle ()
515                 {
516                         Control child, parent;
517                         
518                         parent = new Control ();
519                         child = new Control ();
520                         parent.Controls.Add (child);
521                         parent.Visible = true;
522                         parent.CreateControl ();
523                         child.Invoke (new CrossAppDomainDelegate (dummy)) ;
524                 }
525                 
526                 public void dummy ()
527                 {
528                 }
529                 
530                 public class ControlStylesTester : Control {
531                         private WindowStyles style;
532                         private WindowStyles ex_style;
533                         private bool or_styles;
534
535                         public ControlStylesTester (WindowStyles Style, WindowStyles ExStyle, bool OrStyles)
536                         {
537                                 style = Style;
538                                 ex_style = ExStyle;
539                                 or_styles = OrStyles;
540                         }
541
542                         protected override CreateParams CreateParams {
543                                 get {
544                                         CreateParams result = base.CreateParams;
545                                         if (or_styles) {
546                                                 result.Style |= (int) style;
547                                                 result.ExStyle |= (int) ex_style;
548                                         } else {
549                                                 result.Style = (int) style;
550                                                 result.ExStyle = (int) ex_style;
551                                         }
552                                         return result;
553                                 }
554                         }
555                 }
556                 
557                 [Test]
558                 public void ControlSizeTest ()
559                 {
560                         ControlStylesTester c = new ControlStylesTester (WindowStyles.WS_CHILD, 0, true);
561                         Assert.AreEqual ("{X=0,Y=0}", c.Location.ToString (), "#L1");
562                         Assert.AreEqual ("{Width=0, Height=0}", c.Size.ToString (), "#S1");
563                 }
564
565 #if NET_2_0
566                 [Test]
567                 public void CaptureTest ()
568                 {
569                         Form frm = new Form ();
570                         ControlOverrideLogger log = new ControlOverrideLogger ();
571                         frm.Controls.Add (log);
572                         log.Visible = true;
573                         log.Capture = true;
574                         log.Capture = false;
575                         log.BackColor = Color.Blue;
576                         log.Size = new Size (100, 100);
577                         
578                         frm.Show ();
579                         Application.DoEvents ();
580                         
581                         frm.Dispose ();
582                         Assert.IsTrue (log.Log.ToString ().IndexOf ("OnMouseCaptureChanged") > -1, "#01");
583                         
584                         log = new ControlOverrideLogger ();
585                         log.Capture = true;
586                         log.Capture = false;
587                         Assert.IsTrue (log.Log.ToString ().IndexOf ("OnMouseCaptureChanged") > -1, "#02");
588
589                         log = new ControlOverrideLogger ();
590                         log.Capture = true;
591                         Assert.IsTrue (log.IsHandleCreated, "#03");
592                         Assert.IsTrue (log.Log.ToString ().IndexOf ("OnMouseCaptureChanged") == -1, "#04");
593                         
594                         log = new ControlOverrideLogger ();
595                         log.Capture = false;
596                         Assert.IsFalse (log.IsHandleCreated, "#05");
597                         Assert.IsTrue (log.Log.ToString ().IndexOf ("OnMouseCaptureChanged") == -1, "#06");
598                 }
599 #endif
600         
601                 public class OnPaintTester : Form
602                 {
603                         int counter;
604                         int total;
605                         ArrayList list = new ArrayList ();
606                         public bool Recursive;
607                         public bool TestRefresh;
608                         public bool TestInvalidate;
609                         public bool TestUpdate;
610 #if NET_2_0
611                         public new bool DoubleBuffered {
612                                 get {
613                                         return base.DoubleBuffered;
614                                 }
615                                 set {
616                                         base.DoubleBuffered = value;
617                                 }
618                         }
619 #endif
620                         protected override void OnPaint (PaintEventArgs pevent)
621                         {
622                                 Assert.IsFalse (list.Contains (pevent.Graphics), "OnPaintTester.OnPaint: Got the same Graphics twice");
623                                 list.Add (pevent.Graphics);
624                                 
625                                 if (total > 10)
626                                         return;
627                                 Recursive = counter > 0 || Recursive;
628                                 counter++;
629                                 if (counter < 2) {
630                                         if (TestRefresh)
631                                                 Refresh ();
632                                         else if (TestInvalidate)
633                                                 Invalidate ();
634                                         else {
635                                                 Update ();
636                                         }
637                                 }
638                                 base.OnPaint (pevent);
639                                 counter--;
640                                 total++;
641                         }
642                         public new void Show ()
643                         {
644                                 base.Show ();
645                                 Application.DoEvents ();
646                         }
647                 }
648
649                 [Test]
650                 public void ControlCollectionTest ()
651                 {
652                         Form frm = new Form ();
653                         frm.IsMdiContainer = true;
654                         Form child = new Form ();
655                         Control.ControlCollection c = new Control.ControlCollection (frm);
656                         child.MdiParent = frm;
657                         c.Add (child);
658                         
659                         child.Dispose ();
660                         frm.Dispose ();
661                 }
662
663                 [Test]
664                 [ExpectedException (typeof (ArgumentException))]
665                 public void ControlCollectionExceptionTest ()
666                 {
667                         Form frm = new Form ();
668                         frm.IsMdiContainer = true;
669                         Form child = new Form ();
670                         Control.ControlCollection c = new Control.ControlCollection (frm);
671                         //child.MdiParent = frm;
672                         c.Add (child);
673                         
674                         child.Dispose ();
675                         frm.Dispose ();                 
676                 }
677                 
678                 [Test]
679                 [Category ("NotWorking")]
680                 public void OnPaintTest ()
681                 {
682                         using (OnPaintTester t = new OnPaintTester ()) {
683                                 t.TestRefresh = true;
684                                 t.Show ();
685                                 Assert.IsTrue (t.Recursive, "#1");
686                         }
687
688                         using (OnPaintTester t = new OnPaintTester ()) {
689                                 t.TestUpdate = true;
690                                 t.Show ();
691                                 Assert.IsFalse (t.Recursive, "#2");
692                         }
693
694                         using (OnPaintTester t = new OnPaintTester ()) {
695                                 t.TestInvalidate = true;
696                                 t.Show ();
697                                 Assert.IsFalse (t.Recursive, "#3");
698                         }
699                 }
700 #if NET_2_0
701                 [Test]
702                 [Category ("Interactive")]
703                 public void OnPaintDoubleBufferedTest ()
704                 {
705                         using (OnPaintTester t = new OnPaintTester ()) {
706                                 t.DoubleBuffered = true;
707                                 t.TestRefresh = true;
708                                 t.Show ();
709                                 Assert.IsTrue (t.Recursive, "#1");
710                         }
711
712                         using (OnPaintTester t = new OnPaintTester ()) {
713                                 t.DoubleBuffered = true;
714                                 t.TestUpdate = true;
715                                 t.Show ();
716                                 Assert.IsFalse (t.Recursive, "#2");
717                         }
718
719                         using (OnPaintTester t = new OnPaintTester ()) {
720                                 t.DoubleBuffered = true;
721                                 t.TestInvalidate = true;
722                                 t.Show ();
723                                 Assert.IsFalse (t.Recursive, "#3");
724                         }
725                 }
726 #endif
727                 public class PaintEventForm : Form
728                 {
729                         public ArrayList overrides = new ArrayList ();
730                         public ArrayList events = new ArrayList ();
731
732                         public PaintEventForm ()
733                         {
734                                 Paint += new PaintEventHandler (DoubleBufferEventForm_Paint);
735                         }
736                         public bool GetControlStyle (ControlStyles style)
737                         {
738                                 return base.GetStyle (style);
739                         }
740
741                         public void SetControlStyle (ControlStyles style, bool value)
742                         {
743                                 base.SetStyle (style, value);
744                         }
745                         
746                         void DoubleBufferEventForm_Paint (object sender, PaintEventArgs e)
747                         {
748                                 events.Add("Paint");
749                         }
750                         
751                         protected override void OnPaintBackground (PaintEventArgs e)
752                         {
753                                 base.OnPaintBackground (e);
754                                 overrides.Add("OnPaintBackground");
755                         }
756
757                         protected override void OnPaint (PaintEventArgs pevent)
758                         {
759                                 base.OnPaint (pevent);
760                                 overrides.Add("OnPaint");
761                         }
762                 }
763
764                 [Test]
765                 [Ignore ("Can't find a reliable way to generate a paint message on Windows.")]
766                 public void EventStyleTest ()
767                 {
768 #if NET_2_0
769                         using (PaintEventForm f = new PaintEventForm ()) {
770                                 f.Show ();
771                                 f.SetControlStyle (ControlStyles.OptimizedDoubleBuffer, true);
772                                 f.Refresh ();
773                                 Assert.IsTrue (f.overrides.Contains ("OnPaintBackground"), "#A1");
774                                 Assert.IsTrue (f.overrides.Contains ("OnPaint"), "#A2");
775                                 Assert.IsTrue (f.events.Contains ("Paint"), "#A3");
776                         }
777 #endif
778                         using (PaintEventForm f = new PaintEventForm ()) {
779                                 f.Show ();
780                                 f.SetControlStyle (ControlStyles.DoubleBuffer, true);
781                                 f.Refresh ();
782                                 Assert.IsTrue (f.overrides.Contains ("OnPaintBackground"), "#B1");
783                                 Assert.IsTrue (f.overrides.Contains ("OnPaint"), "#B2");
784                                 Assert.IsTrue (f.events.Contains ("Paint"), "#B3");
785                         }
786
787                         using (PaintEventForm f = new PaintEventForm ()) {
788                                 f.Show ();
789                                 f.SetControlStyle (ControlStyles.AllPaintingInWmPaint, true);
790                                 f.Refresh ();
791                                 Assert.IsTrue (f.overrides.Contains ("OnPaintBackground"), "#C1");
792                                 Assert.IsTrue (f.overrides.Contains ("OnPaint"), "#C2");
793                                 Assert.IsTrue (f.events.Contains ("Paint"), "#C3");
794                         }
795
796                         using (PaintEventForm f = new PaintEventForm ()) {
797                                 f.Show ();
798                                 f.SetControlStyle (ControlStyles.UserPaint, true);
799                                 f.Refresh ();
800                                 Assert.IsTrue (f.overrides.Contains ("OnPaintBackground"), "#D1");
801                                 Assert.IsTrue (f.overrides.Contains ("OnPaint"), "#D2");
802                                 Assert.IsTrue (f.events.Contains ("Paint"), "#D3");
803                         }
804
805                         using (PaintEventForm f = new PaintEventForm ()) {
806                                 f.Show ();
807                                 f.SetControlStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
808                                 f.Refresh ();
809                                 Assert.IsTrue (f.overrides.Contains ("OnPaintBackground"), "#E1");
810                                 Assert.IsTrue (f.overrides.Contains ("OnPaint"), "#E2");
811                                 Assert.IsTrue (f.events.Contains ("Paint"), "#E3");
812                         }
813
814                         using (PaintEventForm f = new PaintEventForm ()) {
815                                 f.Show ();
816                                 f.SetControlStyle (ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);
817                                 f.Refresh ();
818                                 Assert.IsTrue (f.overrides.Contains ("OnPaintBackground"), "#F1");
819                                 Assert.IsTrue (f.overrides.Contains ("OnPaint"), "#F2");
820                                 Assert.IsTrue (f.events.Contains ("Paint"), "#F3");
821                         }
822
823                         using (PaintEventForm f = new PaintEventForm ()) {
824                                 f.Show ();
825                                 f.SetControlStyle (ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
826                                 f.Refresh ();
827                                 Assert.IsTrue (f.overrides.Contains ("OnPaintBackground"), "#G1");
828                                 Assert.IsTrue (f.overrides.Contains ("OnPaint"), "#G2");
829                                 Assert.IsTrue (f.events.Contains ("Paint"), "#G3");
830                         }
831
832                         using (PaintEventForm f = new PaintEventForm ()) {
833                                 f.Show ();
834                                 f.SetControlStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer, true);
835                                 f.Refresh ();
836                                 Assert.IsTrue (f.overrides.Contains ("OnPaintBackground"), "#H1");
837                                 Assert.IsTrue (f.overrides.Contains ("OnPaint"), "#H2");
838                                 Assert.IsTrue (f.events.Contains ("Paint"), "#H3");
839                         }
840                 }
841 #if NET_2_0
842                 public class DoubleBufferedForm : Form
843                 {
844                         public bool painted;
845                         public bool failed;
846                         public DoubleBufferedForm ()
847                         {
848                                 this.DoubleBuffered = true;
849                         }
850
851                         protected override void OnPaint (PaintEventArgs e)
852                         {
853                                 if (failed || painted)
854                                         return;
855                                 painted = true;
856                                 Height = Height + 1;
857                                 try {
858                                         e.Graphics.DrawString (Size.ToString (), Font, Brushes.AliceBlue, new Point (2, 2));
859                                 } catch (Exception exception) {
860                                         Console.WriteLine (exception.StackTrace);
861                                         failed = true;
862                                 }
863                         }
864                 }
865
866                 public class DoubleBufferControl : Control
867                 {
868                         public bool IsDoubleBuffered
869                         {
870                                 get { return base.DoubleBuffered; }
871                                 set { base.DoubleBuffered = value; }
872                         }
873
874                         public bool GetControlStyle (ControlStyles style)
875                         {
876                                 return base.GetStyle (style);
877                         }
878
879                         public void SetControlStyle (ControlStyles style, bool value)
880                         {
881                                 base.SetStyle (style, value);
882                         }
883                 }
884
885                 [Test]
886                 [Ignore ("Can't find a reliable way to generate a paint message on Windows.")]
887                 public void DoubleBufferTest ()
888                 {
889                         DoubleBufferedForm f = new DoubleBufferedForm ();
890                         f.ShowInTaskbar = false;
891                         f.Show ();
892                         f.Refresh ();
893                         
894                         Assert.IsFalse (f.failed, "#01");
895                         Assert.IsTrue (f.painted, "The control was never painted, so please check the test");
896                         f.Close ();
897                 }
898
899                 [Test]
900                 public void DoubleBufferedTest ()
901                 {
902                         DoubleBufferControl c = new DoubleBufferControl ();
903                         Assert.IsFalse (c.IsDoubleBuffered, "#A1");
904                         Assert.IsTrue (c.GetControlStyle (ControlStyles.AllPaintingInWmPaint), "#A2");
905                         Assert.IsFalse (c.GetControlStyle (ControlStyles.DoubleBuffer), "#A3");
906                         Assert.IsFalse (c.GetControlStyle (ControlStyles.OptimizedDoubleBuffer), "#A4");
907                         Assert.IsTrue (c.GetControlStyle (ControlStyles.UserPaint), "#A5");
908
909                         c.SetControlStyle (ControlStyles.OptimizedDoubleBuffer, true);
910                         Assert.IsTrue (c.IsDoubleBuffered, "#B1");
911                         Assert.IsTrue (c.GetControlStyle (ControlStyles.AllPaintingInWmPaint), "#B2");
912                         Assert.IsFalse (c.GetControlStyle (ControlStyles.DoubleBuffer), "#B3");
913                         Assert.IsTrue (c.GetControlStyle (ControlStyles.OptimizedDoubleBuffer), "#B4");
914                         Assert.IsTrue (c.GetControlStyle (ControlStyles.UserPaint), "#A5");
915
916                         c.SetControlStyle (ControlStyles.AllPaintingInWmPaint, false);
917                         c.SetControlStyle (ControlStyles.UserPaint, false);
918                         Assert.IsTrue (c.IsDoubleBuffered, "#C1");
919                         Assert.IsFalse (c.GetControlStyle (ControlStyles.AllPaintingInWmPaint), "#C2");
920                         Assert.IsFalse (c.GetControlStyle (ControlStyles.DoubleBuffer), "#C3");
921                         Assert.IsTrue (c.GetControlStyle (ControlStyles.OptimizedDoubleBuffer), "#C4");
922                         Assert.IsFalse (c.GetControlStyle (ControlStyles.UserPaint), "#C5");
923
924                         c.SetControlStyle (ControlStyles.OptimizedDoubleBuffer, false);
925                         Assert.IsFalse (c.IsDoubleBuffered, "#D1");
926                         Assert.IsFalse (c.GetControlStyle (ControlStyles.AllPaintingInWmPaint), "#D2");
927                         Assert.IsFalse (c.GetControlStyle (ControlStyles.DoubleBuffer), "#D3");
928                         Assert.IsFalse (c.GetControlStyle (ControlStyles.OptimizedDoubleBuffer), "#D4");
929                         Assert.IsFalse (c.GetControlStyle (ControlStyles.UserPaint), "#D5");
930
931                         c.SetControlStyle (ControlStyles.DoubleBuffer, true);
932                         Assert.IsFalse (c.IsDoubleBuffered, "#E1");
933                         Assert.IsFalse (c.GetControlStyle (ControlStyles.AllPaintingInWmPaint), "#E2");
934                         Assert.IsTrue (c.GetControlStyle (ControlStyles.DoubleBuffer), "#E3");
935                         Assert.IsFalse (c.GetControlStyle (ControlStyles.OptimizedDoubleBuffer), "#E4");
936                         Assert.IsFalse (c.GetControlStyle (ControlStyles.UserPaint), "#E5");
937
938                         c.SetControlStyle (ControlStyles.DoubleBuffer, false);
939                         Assert.IsFalse (c.IsDoubleBuffered, "#F1");
940                         Assert.IsFalse (c.GetControlStyle (ControlStyles.AllPaintingInWmPaint), "#F2");
941                         Assert.IsFalse (c.GetControlStyle (ControlStyles.DoubleBuffer), "#F3");
942                         Assert.IsFalse (c.GetControlStyle (ControlStyles.OptimizedDoubleBuffer), "#F4");
943                         Assert.IsFalse (c.GetControlStyle (ControlStyles.UserPaint), "#F5");
944
945                         c.IsDoubleBuffered = true;
946                         Assert.IsTrue (c.IsDoubleBuffered, "#G1");
947                         Assert.IsTrue (c.GetControlStyle (ControlStyles.AllPaintingInWmPaint), "#G2");
948                         Assert.IsFalse (c.GetControlStyle (ControlStyles.DoubleBuffer), "#G3");
949                         Assert.IsTrue (c.GetControlStyle (ControlStyles.OptimizedDoubleBuffer), "#G4");
950                         Assert.IsFalse (c.GetControlStyle (ControlStyles.UserPaint), "#G5");
951
952                         c.SetControlStyle (ControlStyles.AllPaintingInWmPaint, true);
953                         c.SetControlStyle (ControlStyles.OptimizedDoubleBuffer, true);
954                         c.SetControlStyle (ControlStyles.DoubleBuffer, true);
955                         c.SetControlStyle (ControlStyles.UserPaint, true);
956                         c.IsDoubleBuffered = false;
957                         Assert.IsFalse (c.IsDoubleBuffered, "#H1");
958                         Assert.IsTrue (c.GetControlStyle (ControlStyles.AllPaintingInWmPaint), "#H2");
959                         Assert.IsTrue (c.GetControlStyle (ControlStyles.DoubleBuffer), "#H3");
960                         Assert.IsFalse (c.GetControlStyle (ControlStyles.OptimizedDoubleBuffer), "#H4");
961                         Assert.IsTrue (c.GetControlStyle (ControlStyles.UserPaint), "#H5");
962                 }
963
964                 [Test]
965                 public void DoubleBufferedStyleTest ()
966                 {
967                         DoubleBufferControl c = new DoubleBufferControl ();
968                         TestControlStyle.CheckStyles (c, "#A1", ControlStyles.UserPaint, ControlStyles.StandardClick, ControlStyles.Selectable, ControlStyles.StandardDoubleClick, ControlStyles.AllPaintingInWmPaint, ControlStyles.UseTextForAccessibility);
969
970                         c.IsDoubleBuffered = true;
971                         TestControlStyle.CheckStyles (c, "#A2", ControlStyles.UserPaint, ControlStyles.StandardClick, ControlStyles.Selectable, ControlStyles.StandardDoubleClick, ControlStyles.AllPaintingInWmPaint, ControlStyles.UseTextForAccessibility, ControlStyles.OptimizedDoubleBuffer);
972
973                         c.IsDoubleBuffered = false;
974                         TestControlStyle.CheckStyles (c, "#A3", ControlStyles.UserPaint, ControlStyles.StandardClick, ControlStyles.Selectable, ControlStyles.StandardDoubleClick, ControlStyles.AllPaintingInWmPaint, ControlStyles.UseTextForAccessibility);
975
976                         c = new DoubleBufferControl ();
977                         foreach (ControlStyles style in Enum.GetValues (typeof(ControlStyles))) {
978                                 c.SetControlStyle (style, false);
979                         }
980
981                         TestControlStyle.CheckStyles (c, "#B1");
982
983                         c.IsDoubleBuffered = true;
984                         TestControlStyle.CheckStyles (c, "#B2", ControlStyles.OptimizedDoubleBuffer, ControlStyles.AllPaintingInWmPaint);
985
986                         c.IsDoubleBuffered = false;
987                         TestControlStyle.CheckStyles (c, "#B3", ControlStyles.AllPaintingInWmPaint);
988
989                 }
990 #endif
991
992                 class Helper {
993                         public static void TestAccessibility(Control c, string Default, string Description, string Name, AccessibleRole Role)
994                         {
995                                 Assert.IsNotNull (c.AccessibilityObject, "Acc1");
996                                 Assert.AreEqual (Default, c.AccessibleDefaultActionDescription, "Acc2");
997                                 Assert.AreEqual (Description, c.AccessibleDescription, "Acc3");
998                                 Assert.AreEqual (Name, c.AccessibleName, "Acc4");
999                                 Assert.AreEqual (Role, c.AccessibleRole, "Acc5");
1000                         }
1001
1002                         public static string TestControl(Control container, Control start, bool forward) {
1003                                 Control ctl;
1004
1005                                 ctl = container.GetNextControl(start, forward);
1006
1007                                 if (ctl == null) {
1008                                         return null;
1009                                 }
1010
1011                                 return ctl.Text;
1012                         }
1013                 }
1014
1015                 [Test]
1016                 public void CreatedTest ()
1017                 {
1018                         Control c = new Control ();
1019                         Assert.IsFalse (c.Created, "A1");
1020                 }
1021
1022                 class CustomA11yEnabledControl : Control {
1023                         public CustomA11yEnabledControl () : base() { }
1024                         protected override AccessibleObject CreateAccessibilityInstance () { return new CustomAccessibleObject (this); }
1025
1026                         class CustomAccessibleObject : ControlAccessibleObject {
1027                                 public CustomAccessibleObject (CustomA11yEnabledControl control) : base(control) { }
1028                                 public override string Name { get { return "custom name"; } }
1029                                 public override string DefaultAction { get { return "custom default action"; } }
1030                                 public override string Description { get { return "custom description"; } }
1031                                 public override AccessibleRole Role { get { return AccessibleRole.Alert; } }
1032                         }
1033                 }
1034
1035                 [Test]
1036                 public void CreatedAccessibilityTest ()
1037                 {
1038                         CustomA11yEnabledControl c = new CustomA11yEnabledControl ();
1039                         Assert.IsFalse(c.Created, "A1");
1040
1041                         // Tests default values
1042
1043                         Assert.AreEqual (null, c.AccessibleDefaultActionDescription, "A2.0");
1044                         Assert.IsFalse(c.IsHandleCreated, "A2.1");
1045
1046                         Assert.AreEqual (null, c.AccessibleDescription, "A3.0");
1047                         Assert.IsFalse(c.IsHandleCreated, "A3.1");
1048
1049                         Assert.AreEqual (null, c.AccessibleName, "A4.0");
1050                         Assert.IsFalse(c.IsHandleCreated, "A4.1");
1051
1052                         Assert.AreEqual (AccessibleRole.Default, c.AccessibleRole, "A5.0");
1053                         Assert.IsFalse(c.IsHandleCreated, "A5.1");
1054
1055                         object o = c.AccessibilityObject;
1056                         Assert.IsTrue(c.IsHandleCreated, "A6");
1057
1058                         // Tests to confirm that:
1059                         // - calling Control.AccessibleXXXXX is not returning AccessibleObject.XXXXX
1060                         // - Handle is not Created when calling Control.AccessibleXXXXX
1061                         c = new CustomA11yEnabledControl ();
1062
1063                         string accessibleDefaultActionDescription = "default action description";
1064                         c.AccessibleDefaultActionDescription = accessibleDefaultActionDescription;
1065                         Assert.IsFalse (c.IsHandleCreated, "A7.0");
1066                         Assert.AreEqual (accessibleDefaultActionDescription, c.AccessibleDefaultActionDescription, "A7.1");
1067
1068                         string accessibleDescription = "accessible description";
1069                         c.AccessibleDescription = accessibleDescription;
1070                         Assert.IsFalse (c.IsHandleCreated, "A8.0");
1071                         Assert.AreEqual (accessibleDescription, c.AccessibleDescription, "A8.1");
1072
1073                         string accessibleName = "accessible name";
1074                         c.AccessibleName = accessibleName;
1075                         Assert.IsFalse (c.IsHandleCreated, "A9.0");
1076                         Assert.AreEqual (accessibleName, c.AccessibleName, "A9.1");
1077
1078                         AccessibleRole accessibleRole = AccessibleRole.Diagram;
1079                         c.AccessibleRole = accessibleRole;
1080                         Assert.AreEqual (accessibleRole, c.AccessibleRole, "A10.0");
1081                         Assert.IsFalse (c.IsHandleCreated, "A10.1");
1082
1083                         c.Dispose ();
1084                         Assert.IsFalse (c.Created, "A11");
1085                 }
1086
1087                 [Test]
1088                 [Category ("NotWorking")]
1089                 public void BoundsTest ()
1090                 {
1091                         Control c = new Control ();
1092                         Assert.IsTrue (c.Bounds.IsEmpty, "A1");
1093                         Assert.IsTrue (c.Size.IsEmpty, "A2");
1094                         Assert.IsTrue (c.ClientSize.IsEmpty, "A3");
1095                         Assert.IsTrue (c.ClientRectangle.IsEmpty, "A4");
1096
1097                         Assert.AreEqual (((IWin32Window)c).Handle, c.Handle, "A5");
1098
1099                         /* this part fails on linux because we can't allocate X windows which are 0x0,
1100                            and the Control bounds directly reflect the size of the X window */
1101
1102                         Assert.IsTrue (c.Bounds.IsEmpty, "A6");
1103                         Assert.IsTrue (c.Size.IsEmpty, "A7");
1104                         Assert.IsTrue (c.ClientSize.IsEmpty, "A8");
1105                         Assert.IsTrue (c.ClientRectangle.IsEmpty, "A9");
1106                 }
1107
1108                 [Test]
1109                 [Ignore ("Depends on specific DPI")]
1110                 public void FontHeightTest ()
1111                 {
1112                         MockControl c = new MockControl ();
1113                         Assert.AreEqual (13, c.font_height);
1114                 }
1115
1116                 [Test]
1117                 public void FontTest ()
1118                 {
1119                         Control c = new Control ();
1120                         Assert.IsFalse (c.Font.Bold, "#A1");
1121                         //Assert.AreEqual ("Microsoft Sans Serif", c.Font.FontFamily.Name, "#A2");
1122                         Assert.IsFalse (c.Font.Italic, "#A3");
1123                         //Assert.AreEqual ("Microsoft Sans Serif", c.Font.Name, "#A4");
1124                         Assert.AreEqual (8.25, c.Font.Size, "#A5");
1125                         Assert.AreEqual (8.25, c.Font.SizeInPoints, "#A6");
1126                         Assert.IsFalse (c.Font.Strikeout, "#A7");
1127                         Assert.IsFalse (c.Font.Underline, "#A8");
1128                         Assert.AreEqual (GraphicsUnit.Point, c.Font.Unit, "#A9");
1129 #if NET_2_0
1130                         Assert.IsTrue (c.Font.IsSystemFont, "#A10");
1131 #endif
1132
1133                         c.Font = new Font (c.Font.FontFamily, 3, FontStyle.Italic);
1134                         Assert.IsFalse (c.Font.Bold, "#B1");
1135                         //Assert.AreEqual ("Microsoft Sans Serif", c.Font.FontFamily.Name, "#B2");
1136                         Assert.IsTrue (c.Font.Italic, "#B3");
1137                         //Assert.AreEqual ("Microsoft Sans Serif", c.Font.Name, "#B4");
1138                         Assert.AreEqual (3, c.Font.Size, "#B5");
1139                         Assert.AreEqual (3, c.Font.SizeInPoints, "#B6");
1140                         Assert.IsFalse (c.Font.Strikeout, "#B7");
1141                         Assert.IsFalse (c.Font.Underline, "#B8");
1142                         Assert.AreEqual (GraphicsUnit.Point, c.Font.Unit, "#B9");
1143 #if NET_2_0
1144                         Assert.AreEqual (false, c.Font.IsSystemFont, "#B10");
1145 #endif
1146                 }
1147
1148                 [Test]
1149                 [Category ("NotWorking")] // on Unix mapping is done to Bitstream Vera Sans
1150                 public void FontTest_Names ()
1151                 {
1152                         Control c = new Control ();
1153                         Assert.AreEqual ("Microsoft Sans Serif", c.Font.FontFamily.Name, "#1");
1154                         Assert.AreEqual ("Microsoft Sans Serif", c.Font.Name, "#2");
1155                 }
1156
1157                 [Test]
1158                 public void PubPropTest()
1159                 {
1160                         Control c = new Control();
1161
1162                         Assert.IsFalse (c.AllowDrop , "A1");
1163                         Assert.AreEqual(AnchorStyles.Top | AnchorStyles.Left, c.Anchor, "A2");
1164
1165                         Assert.AreEqual ("Control", c.BackColor.Name , "B1");
1166                         Assert.IsNull (c.BackgroundImage, "B2");
1167                         Assert.IsNull (c.BindingContext, "B3");
1168 #if NET_2_0
1169                         Assert.AreEqual (ImageLayout.Tile, c.BackgroundImageLayout, "B4");
1170 #endif
1171
1172                         Assert.IsFalse (c.CanFocus, "C1");
1173                         Assert.IsTrue (c.CanSelect, "C2");
1174                         Assert.IsFalse (c.Capture, "C3");
1175                         Assert.IsTrue (c.CausesValidation, "C4");
1176
1177                         Assert.IsNotNull (c.CompanyName, "C7");
1178                         Assert.IsNull (c.Container, "C8");
1179                         Assert.IsFalse (c.ContainsFocus, "C9");
1180                         Assert.IsNull (c.ContextMenu, "C10");
1181                         Assert.AreEqual (0, c.Controls.Count, "C11");
1182                         Assert.IsFalse (c.Created, "C12");
1183                         Assert.AreEqual (Cursors.Default, c.Cursor, "C13");
1184
1185                         Assert.IsNotNull(c.DataBindings, "D1");
1186                         Assert.AreEqual("Control", Control.DefaultBackColor.Name, "D2");
1187                         Assert.AreEqual("ControlText", Control.DefaultForeColor.Name, "D3");
1188                         Assert.AreEqual(FontStyle.Regular, Control.DefaultFont.Style, "D4");
1189                         Assert.AreEqual (new Rectangle(0, 0, 0, 0), c.DisplayRectangle , "D5");
1190                         Assert.IsFalse (c.Disposing, "D6");
1191                         Assert.AreEqual(DockStyle.None, c.Dock, "D7");
1192
1193                         Assert.IsTrue (c.Enabled, "E1");
1194
1195                         Assert.IsFalse  (c.Focused, "F1");
1196                         Assert.AreEqual (FontStyle.Regular, c.Font.Style, "F2");
1197                         Assert.AreEqual (SystemColors.ControlText, c.ForeColor, "F3");
1198
1199                         Assert.IsFalse  (c.HasChildren, "H2");
1200
1201                         Assert.AreEqual (ImeMode.NoControl, c.ImeMode, "I1");
1202                         Assert.IsFalse (c.InvokeRequired, "I2");
1203                         Assert.IsFalse (c.IsAccessible, "I3");
1204                         Assert.IsFalse (c.IsDisposed, "I4");
1205                         Assert.IsFalse (c.IsHandleCreated, "I5");
1206
1207                         Assert.AreEqual(Point.Empty, c.Location, "L2");
1208
1209 #if NET_2_0
1210                         Assert.IsTrue(c.MaximumSize.IsEmpty);
1211                         Assert.IsTrue(c.MinimumSize.IsEmpty);
1212 #endif
1213                         Assert.AreEqual (Keys.None, Control.ModifierKeys, "M1");
1214                         Assert.IsTrue (Control.MousePosition.X >= 0 && Control.MousePosition.Y >= 0, "M2");
1215                         Assert.AreEqual (MouseButtons.None, Control.MouseButtons, "M3");
1216
1217                         Assert.AreEqual("", c.Name, "N1");
1218                         c.Name = "Control Name";
1219                         Assert.AreEqual("Control Name", c.Name, "N2");
1220
1221                         Assert.IsNull (c.Parent, "P1");
1222                         Assert.IsNotNull (c.ProductName, "P2");
1223                         Assert.IsTrue (c.ProductName != "", "P3");
1224                         Assert.IsNotNull (c.ProductVersion, "P4");
1225                         Assert.IsTrue (c.ProductVersion != "", "P5");
1226
1227                         Assert.IsFalse (c.RecreatingHandle, "R1");
1228                         Assert.IsNull (c.Region, "R2");
1229                         Assert.AreEqual (RightToLeft.No, c.RightToLeft, "R4");
1230
1231                         Assert.IsNull (c.Site, "S1");
1232
1233                         Assert.AreEqual (0, c.TabIndex , "T1");
1234                         Assert.IsTrue (c.TabStop, "T2");
1235                         Assert.IsNull (c.Tag, "T3");
1236                         Assert.AreEqual ("", c.Text, "T4");
1237
1238                         Assert.IsTrue (c.Visible, "V1");
1239                 }
1240
1241                 [Test]
1242                 public void SizeChangeTest ()
1243                 {
1244                         Form f = new Form ();
1245                         Control c = new Control ();
1246                         f.Controls.Add(c);
1247                         f.Show();
1248                         c.Resize += new EventHandler(SizeChangedTest_ResizeHandler);
1249                         c.Tag = true;
1250                         c.Size = c.Size;
1251                         Assert.AreEqual (true, (bool) c.Tag, "#1");
1252                         f.Close ();
1253                 }
1254
1255                 private void SizeChangedTest_ResizeHandler (object sender, EventArgs e)
1256                 {
1257                         ((Control) sender).Tag = false;
1258                 }
1259
1260                 [Test]
1261                 public void NegativeHeightTest ()
1262                 {
1263                         Control c = new Control ();
1264                         IntPtr handle = c.Handle;
1265                         c.Resize += new EventHandler(NegativeHeightTest_ResizeHandler);
1266                         c.Tag = -2;
1267                         c.Height = 2;
1268                         c.Height = -2;
1269                         Assert.AreEqual (0, (int) c.Tag, "#1");
1270                         c.Dispose ();
1271                         Assert.AreEqual (handle, handle, "Removes warning.");
1272                 }
1273                 
1274                 private void NegativeHeightTest_ResizeHandler (object sender, EventArgs e)
1275                 {
1276                         Control c = (Control) sender;
1277                         c.Tag = c.Height;
1278                 }
1279                 
1280                 [Test]
1281                 public void TopLevelControlTest () {
1282                         Control c = new Control ();
1283
1284                         Assert.AreEqual(null, c.TopLevelControl, "T1");
1285
1286                         Panel p = new Panel ();
1287
1288                         p.Controls.Add (c);
1289
1290                         Assert.AreEqual(null, c.TopLevelControl, "T2");
1291
1292                         Form f = new Form ();
1293                         f.ShowInTaskbar = false;
1294
1295                         f.Controls.Add (p);
1296
1297                         Assert.AreEqual (f, c.TopLevelControl, "T3");
1298                         Assert.AreEqual (f, f.TopLevelControl, "T4");
1299                         
1300                         f.Dispose ();
1301                 }
1302
1303                 [Test]
1304                 public void RelationTest() {
1305                         Control c1;
1306                         Control c2;
1307
1308                         c1 = new Control();
1309                         c2 = new Control();
1310
1311                         Assert.AreEqual(true , c1.Visible , "Rel1");
1312                         Assert.AreEqual(false, c1.Contains(c2) , "Rel2");
1313                         Assert.AreEqual("System.Windows.Forms.Control", c1.ToString() , "Rel3");
1314
1315                         c1.Controls.Add(c2);
1316                         Assert.AreEqual(true , c2.Visible , "Rel4");
1317                         Assert.AreEqual(true, c1.Contains(c2) , "Rel5");
1318
1319                         c1.Anchor = AnchorStyles.Top;
1320                         c1.SuspendLayout ();
1321                         c1.Anchor = AnchorStyles.Left ;
1322                         c1.ResumeLayout ();
1323                         Assert.AreEqual(AnchorStyles.Left , c1.Anchor, "Rel6");
1324
1325                         c1.SetBounds(10, 20, 30, 40) ;
1326                         Assert.AreEqual(new Rectangle(10, 20, 30, 40), c1.Bounds, "Rel7");
1327
1328                         Assert.AreEqual(c1, c2.Parent, "Rel8");
1329                 }
1330
1331                 [Test]
1332                 public void AnchorDockTest ()
1333                 {
1334                         Control c = new Control ();
1335
1336                         Assert.AreEqual (DockStyle.None, c.Dock, "1");
1337                         Assert.AreEqual (AnchorStyles.Top | AnchorStyles.Left, c.Anchor, "2");
1338
1339                         c.Dock = DockStyle.Top;
1340                         Assert.AreEqual (DockStyle.Top, c.Dock, "3");
1341                         Assert.AreEqual (AnchorStyles.Top | AnchorStyles.Left, c.Anchor, "4");
1342
1343                         c.Anchor = AnchorStyles.Top;
1344                         Assert.AreEqual (DockStyle.None, c.Dock, "5");
1345                         Assert.AreEqual (AnchorStyles.Top, c.Anchor, "6");
1346                 }
1347
1348                 [Test]
1349                 [Category ("NotWorking")]
1350                 public void TabOrder()
1351                 {
1352                         Form            form;
1353                         //Control               active;
1354
1355                         Label           label1 = new Label();           // To test non-tabstop items as well
1356                         Label           label2 = new Label();
1357
1358                         GroupBox        group1 = new GroupBox();
1359                         GroupBox        group2 = new GroupBox();
1360                         GroupBox        group3 = new GroupBox();
1361
1362                         TextBox         text1 = new TextBox();
1363
1364                         RadioButton     radio11 = new RadioButton();
1365                         RadioButton     radio12 = new RadioButton();
1366                         RadioButton     radio13 = new RadioButton();
1367                         RadioButton     radio14 = new RadioButton();
1368                         RadioButton     radio21 = new RadioButton();
1369                         RadioButton     radio22 = new RadioButton();
1370                         RadioButton     radio23 = new RadioButton();
1371                         RadioButton     radio24 = new RadioButton();
1372                         RadioButton     radio31 = new RadioButton();
1373                         RadioButton     radio32 = new RadioButton();
1374                         RadioButton     radio33 = new RadioButton();
1375                         RadioButton     radio34 = new RadioButton();
1376
1377                         form = new Form();
1378                         form.ShowInTaskbar = false;
1379
1380                         form.ClientSize = new Size (520, 520);
1381                         Assert.AreEqual(new Size(520, 520), form.ClientSize, "Tab1");
1382
1383                         form.Text = "SWF Taborder Test App Form";
1384                         Assert.AreEqual("SWF Taborder Test App Form", form.Text, "Tab2");
1385
1386                         label1.Location = new Point(10, 10);
1387                         Assert.AreEqual(new Point(10, 10), label1.Location, "Tab3");
1388                         label1.Text = "Label1";
1389                         form.Controls.Add(label1);
1390
1391                         label2.Location = new Point(200, 10);
1392                         label2.Text = "Label2";
1393                         form.Controls.Add(label2);
1394
1395                         group1.Text = "Group1";
1396                         group2.Text = "Group2";
1397                         group3.Text = "Group3";
1398
1399                         group1.Size = new Size(200, 400);
1400                         group2.Size = new Size(200, 400);
1401                         group3.Size = new Size(180, 180);
1402                         Assert.AreEqual(new Size(180, 180), group3.Size, "Tab4");
1403
1404                         group1.Location = new Point(10, 40);
1405                         group2.Location = new Point(220, 40);
1406                         group3.Location = new Point(10, 210);
1407
1408                         group1.TabIndex = 30;
1409                         Assert.AreEqual(30, group1.TabIndex, "Tab5");
1410                         group1.TabStop = true;
1411
1412                         // Don't assign, test automatic assignment
1413                         //group2.TabIndex = 0;
1414                         group2.TabStop = true;
1415                         Assert.AreEqual(0, group2.TabIndex, "Tab6");
1416
1417                         group3.TabIndex = 35;
1418                         group3.TabStop = true;
1419
1420                         // Test default tab index
1421                         Assert.AreEqual(0, radio11.TabIndex, "Tab7");
1422
1423                         text1.Text = "Edit Control";
1424
1425                         radio11.Text = "Radio 1-1 [Tab1]";
1426                         radio12.Text = "Radio 1-2 [Tab2]";
1427                         radio13.Text = "Radio 1-3 [Tab3]";
1428                         radio14.Text = "Radio 1-4 [Tab4]";
1429
1430                         radio21.Text = "Radio 2-1 [Tab4]";
1431                         radio22.Text = "Radio 2-2 [Tab3]";
1432                         radio23.Text = "Radio 2-3 [Tab2]";
1433                         radio24.Text = "Radio 2-4 [Tab1]";
1434
1435                         radio31.Text = "Radio 3-1 [Tab1]";
1436                         radio32.Text = "Radio 3-2 [Tab3]";
1437                         radio33.Text = "Radio 3-3 [Tab2]";
1438                         radio34.Text = "Radio 3-4 [Tab4]";
1439
1440                         // We don't assign TabIndex for radio1X; test automatic assignment
1441                         text1.TabStop = true;
1442                         radio11.TabStop = true;
1443
1444                         radio21.TabIndex = 4;
1445                         radio22.TabIndex = 3;
1446                         radio23.TabIndex = 2;
1447                         radio24.TabIndex = 1;
1448                         radio24.TabStop = true;
1449
1450                         radio31.TabIndex = 11;
1451                         radio31.TabStop = true;
1452                         radio32.TabIndex = 13;
1453                         radio33.TabIndex = 12;
1454                         radio34.TabIndex = 14;
1455
1456                         text1.Location = new Point(10, 100);
1457
1458                         radio11.Location = new Point(10, 20);
1459                         radio12.Location = new Point(10, 40);
1460                         radio13.Location = new Point(10, 60);
1461                         radio14.Location = new Point(10, 80);
1462
1463                         radio21.Location = new Point(10, 20);
1464                         radio22.Location = new Point(10, 40);
1465                         radio23.Location = new Point(10, 60);
1466                         radio24.Location = new Point(10, 80);
1467
1468                         radio31.Location = new Point(10, 20);
1469                         radio32.Location = new Point(10, 40);
1470                         radio33.Location = new Point(10, 60);
1471                         radio34.Location = new Point(10, 80);
1472
1473                         text1.Size = new Size(150, text1.PreferredHeight);
1474
1475                         radio11.Size = new Size(150, 20);
1476                         radio12.Size = new Size(150, 20);
1477                         radio13.Size = new Size(150, 20);
1478                         radio14.Size = new Size(150, 20);
1479
1480                         radio21.Size = new Size(150, 20);
1481                         radio22.Size = new Size(150, 20);
1482                         radio23.Size = new Size(150, 20);
1483                         radio24.Size = new Size(150, 20);
1484
1485                         radio31.Size = new Size(150, 20);
1486                         radio32.Size = new Size(150, 20);
1487                         radio33.Size = new Size(150, 20);
1488                         radio34.Size = new Size(150, 20);
1489
1490                         group1.Controls.Add(text1);
1491
1492                         group1.Controls.Add(radio11);
1493                         group1.Controls.Add(radio12);
1494                         group1.Controls.Add(radio13);
1495                         group1.Controls.Add(radio14);
1496
1497                         group2.Controls.Add(radio21);
1498                         group2.Controls.Add(radio22);
1499                         group2.Controls.Add(radio23);
1500                         group2.Controls.Add(radio24);
1501
1502                         group3.Controls.Add(radio31);
1503                         group3.Controls.Add(radio32);
1504                         group3.Controls.Add(radio33);
1505                         group3.Controls.Add(radio34);
1506
1507                         form.Controls.Add(group1);
1508                         form.Controls.Add(group2);
1509                         group2.Controls.Add(group3);
1510
1511                         // Perform some tests, the TabIndex stuff below will alter the outcome
1512                         Assert.AreEqual(null, Helper.TestControl(group2, radio34, true), "Tab8");
1513                         Assert.AreEqual(31, group2.TabIndex, "Tab9");
1514
1515                         // Does the taborder of containers and non-selectable things change behaviour?
1516                         label1.TabIndex = 5;
1517                         label2.TabIndex = 4;
1518                         group1.TabIndex = 3;
1519                         group2.TabIndex = 1;
1520
1521                         // Start verification
1522                         Assert.AreEqual(null, Helper.TestControl(group2, radio34, true), "Tab10");
1523                         Assert.AreEqual(radio24.Text, Helper.TestControl(group2, group2, true), "Tab11");
1524                         Assert.AreEqual(radio31.Text, Helper.TestControl(group2, group3, true), "Tab12");
1525                         Assert.AreEqual(null, Helper.TestControl(group1, radio14, true), "Tab13");
1526                         Assert.AreEqual(radio23.Text, Helper.TestControl(group2, radio24, true), "Tab14");
1527                         Assert.AreEqual(group3.Text, Helper.TestControl(group2, radio21, true), "Tab15");
1528                         Assert.AreEqual(radio13.Text, Helper.TestControl(form, radio12, true), "Tab16");
1529                         Assert.AreEqual(label2.Text, Helper.TestControl(form, radio14, true), "Tab17");
1530                         Assert.AreEqual(group1.Text, Helper.TestControl(form, radio34, true), "Tab18");
1531                         Assert.AreEqual(radio23.Text, Helper.TestControl(group2, radio24, true), "Tab19");
1532
1533                         // Sanity checks
1534                         Assert.AreEqual(null, Helper.TestControl(radio11, radio21, true), "Tab20");
1535                         Assert.AreEqual(text1.Text, Helper.TestControl(group1, radio21, true), "Tab21");
1536
1537                         Assert.AreEqual(radio14.Text, Helper.TestControl(form, label2, false), "Tab22");
1538                         Assert.AreEqual(radio21.Text, Helper.TestControl(group2, group3, false), "Tab23");
1539
1540                         Assert.AreEqual(4, radio21.TabIndex, "Tab24");
1541                         Assert.AreEqual(1, radio11.TabIndex, "Tab25");
1542                         Assert.AreEqual(3, radio13.TabIndex, "Tab26");
1543                         Assert.AreEqual(35, group3.TabIndex, "Tab27");
1544                         Assert.AreEqual(1, group2.TabIndex, "Tab28");
1545
1546                         Assert.AreEqual(label1.Text, Helper.TestControl(form, form, false), "Tab29");
1547                         Assert.AreEqual(radio14.Text, Helper.TestControl(group1, group1, false), "Tab30");
1548                         Assert.AreEqual(radio34.Text, Helper.TestControl(group3, group3, false), "Tab31");
1549
1550                         Assert.AreEqual(null, Helper.TestControl(label1, label1, false), "Tab31");
1551                         Assert.AreEqual(null, Helper.TestControl(radio11, radio21, false), "Tab32");
1552                         form.Dispose ();
1553                 }
1554
1555                 [Test]
1556                 public void ScaleTest()
1557                 {
1558                         Control r1 = new Control();
1559
1560                         r1.Width = 40;
1561                         r1.Height = 20;
1562                         r1.Scale(2);
1563                         Assert.AreEqual(80, r1.Width, "Scale1");
1564                         Assert.AreEqual(40, r1.Height, "Scale2");
1565                 }
1566
1567 #if NET_2_0
1568                 [Test]
1569                 public void ScaleChildrenTest ()
1570                 {
1571                         ScaleChildrenControl c = new ScaleChildrenControl ();
1572                         Assert.AreEqual (true, c.PublicScaleChildren, "A1");
1573                 }
1574                 
1575                 private class ScaleChildrenControl : Control
1576                 {
1577                         public bool PublicScaleChildren {
1578                                 get { return base.ScaleChildren; }
1579                         }
1580                 }
1581                 
1582                 [Test]
1583                 public void ScaleControlTest ()
1584                 {
1585                         ScaleControl c = new ScaleControl ();
1586                         
1587                         c.Location = new Point (5, 10);
1588                         c.Size = new Size (15, 20);
1589                         
1590                         Assert.AreEqual (new Rectangle (5, 10, 15, 20), c.Bounds, "A1");
1591
1592                         c.PublicScaleControl (new SizeF (1.5f, 1.3f), BoundsSpecified.All);
1593                         Assert.AreEqual (new Rectangle (8, 13, 22, 26), c.Bounds, "A2");
1594
1595                         c.PublicScaleControl (new SizeF (2f, 1.5f), BoundsSpecified.Location);
1596                         Assert.AreEqual (new Rectangle (16, 20, 22, 26), c.Bounds, "A3");
1597
1598                         c.PublicScaleControl (new SizeF (1.5f, 2f), BoundsSpecified.Size);
1599                         Assert.AreEqual (new Rectangle (16, 20, 33, 52), c.Bounds, "A4");
1600
1601                         c.PublicScaleControl (new SizeF (1.5f, 1.5f), BoundsSpecified.Width);
1602                         Assert.AreEqual (new Rectangle (16, 20, 50, 52), c.Bounds, "A5");
1603
1604                         c.PublicScaleControl (new SizeF (1.5f, 1.3f), BoundsSpecified.None);
1605                         Assert.AreEqual (new Rectangle (16, 20, 50, 52), c.Bounds, "A6");
1606                         
1607                         // Test with ScaleChildren
1608                         c = new ScaleControl ();
1609
1610                         c.Location = new Point (5, 10);
1611                         c.Size = new Size (50, 50);
1612                         
1613                         Control c2 = new Control ();
1614                         c2.Location = new Point (15, 15);
1615                         c2.Size = new Size (25, 25);
1616                         c.Controls.Add (c2);
1617
1618                         Assert.AreEqual (new Rectangle (5, 10, 50, 50), c.Bounds, "B1");
1619                         Assert.AreEqual (new Rectangle (15, 15, 25, 25), c2.Bounds, "B2");
1620
1621                         c.scale_children = false;
1622
1623                         c.PublicScaleControl (new SizeF (2f, 2f), BoundsSpecified.All);
1624                         Assert.AreEqual (new Rectangle (10, 20, 100, 100), c.Bounds, "B3");
1625                         Assert.AreEqual (new Rectangle (15, 15, 25, 25), c2.Bounds, "B4");
1626
1627                         c.scale_children = true;
1628
1629                         // Will not scale children in ScaleControl
1630                         c.PublicScaleControl (new SizeF (2f, 2f), BoundsSpecified.All);
1631                         Assert.AreEqual (new Rectangle (20, 40, 200, 200), c.Bounds, "B5");
1632                         Assert.AreEqual (new Rectangle (15, 15, 25, 25), c2.Bounds, "B6");
1633                         
1634                         // Does scale children in Scale
1635                         c.Scale (new SizeF (2f, 2f));
1636                         Assert.AreEqual (new Rectangle (40, 80, 400, 400), c.Bounds, "B7");
1637                         Assert.AreEqual (new Rectangle (30, 30, 50, 50), c2.Bounds, "B8");
1638                 }
1639                 
1640                 [Test]
1641                 public void GetScaledBoundsTest ()
1642                 {
1643                         ScaleControl c = new ScaleControl ();
1644                         
1645                         Rectangle r = new Rectangle (10, 20, 30, 40);
1646
1647                         Assert.AreEqual (new Rectangle (20, 10, 60, 20), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.All), "A1");
1648                         Assert.AreEqual (new Rectangle (20, 10, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Location), "A2");
1649                         Assert.AreEqual (new Rectangle (10, 20, 60, 20), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Size), "A3");
1650                         Assert.AreEqual (new Rectangle (10, 20, 30, 20), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Height), "A4");
1651                         Assert.AreEqual (new Rectangle (20, 20, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.X), "A5");
1652                         Assert.AreEqual (new Rectangle (10, 20, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.None), "A6");
1653                         
1654                         c.PublicSetTopLevel (true);
1655
1656                         Assert.AreEqual (new Rectangle (10, 20, 60, 20), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.All), "A7");
1657                         Assert.AreEqual (new Rectangle (10, 20, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Location), "A8");
1658                         Assert.AreEqual (new Rectangle (10, 20, 60, 20), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Size), "A9");
1659                         Assert.AreEqual (new Rectangle (10, 20, 30, 20), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Height), "A10");
1660                         Assert.AreEqual (new Rectangle (10, 20, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.X), "A11");
1661                         Assert.AreEqual (new Rectangle (10, 20, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.None), "A12");
1662
1663                         c = new ScaleControl ();
1664                         c.PublicSetStyle (ControlStyles.FixedHeight, true);
1665                         c.PublicSetStyle (ControlStyles.FixedWidth, true);
1666
1667                         Assert.AreEqual (new Rectangle (20, 10, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.All), "A13");
1668                         Assert.AreEqual (new Rectangle (20, 10, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Location), "A14");
1669                         Assert.AreEqual (new Rectangle (10, 20, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Size), "A15");
1670                         Assert.AreEqual (new Rectangle (10, 20, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Height), "A16");
1671                         Assert.AreEqual (new Rectangle (20, 20, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.X), "A17");
1672                         Assert.AreEqual (new Rectangle (10, 20, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.None), "A18");
1673                 }
1674                 
1675                 private class ScaleControl : Control
1676                 {
1677                         public bool scale_children = true;
1678                         
1679                         public void PublicScaleControl (SizeF factor, BoundsSpecified specified)
1680                         {
1681                                 base.ScaleControl (factor, specified);
1682                         }
1683
1684                         public Rectangle PublicGetScaledBounds (Rectangle bounds, SizeF factor, BoundsSpecified specified)
1685                         {
1686                                 return base.GetScaledBounds (bounds, factor, specified);
1687                         }
1688                         
1689                         public void PublicSetStyle (ControlStyles flag, bool value)
1690                         {
1691                                 base.SetStyle (flag, value);
1692                         }
1693                         
1694                         public void PublicSetTopLevel (bool value)
1695                         {
1696                                 base.SetTopLevel (value);
1697                         }
1698                         
1699                         protected override bool ScaleChildren {
1700                                 get { return scale_children; }
1701                         }
1702                 }
1703 #endif
1704
1705                 [Test]  // Bug #347282
1706                 public void ScaleHierarchy ()
1707                 {
1708                         Form f = new Form ();
1709                         Panel p = new Panel ();
1710                         Button b = new Button ();
1711                         
1712                         f.ClientSize = new Size (300, 300);
1713
1714                         f.Controls.Add (p);
1715                         p.Controls.Add (b);
1716                         
1717                         f.AutoScaleBaseSize = new Size (3, 11);
1718                         f.Show ();
1719                         
1720                         // Due to font differences, all we can guarantee is that
1721                         // the button is larger that the default.
1722                         Assert.IsTrue (b.Width > 75, "A1");
1723                         Assert.IsTrue (b.Height > 23, "A2");
1724                         
1725                         f.Dispose ();
1726                 }
1727                 
1728                 class TestWindowTarget : IWindowTarget
1729                 {
1730                         public void OnHandleChange (IntPtr newHandle) {
1731                         }
1732
1733                         public void OnMessage (ref Message m) {
1734                         }
1735                 }
1736
1737                 [Test]
1738                 public void WindowTargetTest()
1739                 {
1740                         Control c = new Control ();
1741                         Assert.IsNotNull (c.WindowTarget, "WindowTarget1");
1742                         c.WindowTarget = null;
1743                         Assert.IsNull (c.WindowTarget, "WindowTarget2");
1744
1745                         IWindowTarget existing_target = c.WindowTarget;
1746                         IWindowTarget new_target = new TestWindowTarget ();
1747                         c.WindowTarget = new_target;
1748                         Assert.AreSame (new_target, c.WindowTarget, "WindowTarget3");
1749                         
1750                         TestHelper.RemoveWarning (existing_target);
1751                 }
1752
1753                 [Test]
1754                 public void TextTest()
1755                 {
1756                         Control r1 = new Control();
1757                         r1.Text = "Hi" ;
1758                         Assert.AreEqual("Hi" , r1.Text , "Text1");
1759
1760                         r1.ResetText();
1761                         Assert.AreEqual("" , r1.Text , "Text2");
1762                 }
1763
1764                 [Test]
1765                 public void PubMethodTest7()
1766                 {
1767                         Control r1 = new Control();
1768                         r1.RightToLeft = RightToLeft.Yes ;
1769                         r1.ResetRightToLeft() ;
1770                         Assert.AreEqual(RightToLeft.No , r1.RightToLeft , "#81");
1771                         r1.ImeMode = ImeMode.Off ;
1772                         r1.ResetImeMode () ;
1773                         Assert.AreEqual(ImeMode.NoControl , r1.ImeMode , "#82");
1774                         r1.ForeColor= SystemColors.GrayText ;
1775                         r1.ResetForeColor() ;
1776                         Assert.AreEqual(SystemColors.ControlText , r1.ForeColor , "#83");
1777                         //r1.Font = Font.FromHdc();
1778                         r1.ResetFont () ;
1779                         //Assert.AreEqual(FontFamily.GenericSansSerif , r1.Font , "#83");
1780                         r1.Cursor = Cursors.Hand ;
1781                         r1.ResetCursor () ;
1782                         Assert.AreEqual(Cursors.Default , r1.Cursor , "#83");
1783                         //r1.DataBindings = System.Windows.Forms.Binding ;
1784                         //r1.ResetBindings() ;
1785                         //Assert.AreEqual(ControlBindingsCollection , r1.DataBindings  , "#83");
1786                         r1.BackColor = Color.Black ;
1787                         r1.ResetBackColor() ;
1788                         Assert.AreEqual( SystemColors.Control , r1.BackColor  , "#84");
1789                         r1.BackColor = Color.Black ;
1790                         r1.Refresh() ;
1791                         Assert.AreEqual( null , r1.Region , "#85");
1792                         Rectangle M = new Rectangle(10, 20, 30 ,40);
1793                         r1.RectangleToScreen(M) ;
1794                         Assert.AreEqual( null , r1.Region , "#86");
1795                 }
1796
1797                 [Test]
1798                 public void ScreenClientCoords()
1799                 {
1800                         Label l;
1801                         Point p1;
1802                         Point p2;
1803                         Point p3;
1804
1805                         l = new Label();
1806                         l.Left = 10;
1807                         l.Top  = 12;
1808                         l.Visible = true;
1809                         p1 = new Point (10,10);
1810                         p2 = l.PointToScreen(p1);
1811                         p3 = l.PointToClient(p2);
1812
1813                         Assert.AreEqual (p1, p3, "SC1");
1814                 }
1815
1816                 [Test]
1817                 public void ContainsTest ()
1818                 {
1819                         Control t = new Control ();
1820                         Control s = new Control ();
1821
1822                         t.Controls.Add (s);
1823
1824                         Assert.AreEqual (true, t.Contains (s), "Con1");
1825                         Assert.AreEqual (false, s.Contains (t), "Con2");
1826                         Assert.AreEqual (false, s.Contains (null), "Con3");
1827                         Assert.AreEqual (false, t.Contains (new Control ()), "Con4");
1828                 }
1829
1830                 [Test]
1831                 public void IsHandleCreated_NotVisible ()
1832                 {
1833                         Control c = new Control ();
1834                         c.Visible = false;
1835
1836                         Form form = new Form ();
1837                         form.ShowInTaskbar = false;
1838                         form.Controls.Add (c);
1839                         form.Show ();
1840
1841                         Assert.IsFalse (c.IsHandleCreated, "#1");
1842                         c.Visible = true;
1843                         Assert.IsTrue (c.IsHandleCreated, "#2");
1844                         c.Visible = false;
1845                         Assert.IsTrue (c.IsHandleCreated, "#3");
1846                         form.Close ();
1847                 }
1848
1849                 class OnCreateControlTest : Control {
1850                         public bool reached = false;
1851                         protected override void OnCreateControl () {
1852                                 reached = true;
1853                         }
1854                 }
1855
1856                 [Test]
1857                 public void CreateControlVisibleTest ()
1858                 {
1859                         OnCreateControlTest test = new OnCreateControlTest ();
1860                         test.Visible = false;
1861                         Assert.IsFalse (test.IsHandleCreated, "0");
1862                         Assert.IsFalse (test.Visible, "1");
1863                         test.Visible = true;
1864                         Assert.IsTrue (test.Visible, "2");
1865                         Assert.IsTrue (test.reached, "3");
1866                 }
1867
1868
1869                 [Test]
1870                 public void CreateGraphicsTest ()
1871                 {
1872                         Graphics g = null;
1873                         Pen p = null;
1874
1875                         try {
1876                                 Control c = new Control ();
1877                                 c.SetBounds (0,0, 20, 20);
1878                                 g = c.CreateGraphics ();
1879                                 Assert.IsNotNull (g, "Graph1");
1880                         } finally {
1881                                 if (p != null)
1882                                         p.Dispose ();
1883                                 if (g != null)
1884                                         g.Dispose ();
1885                         }
1886                 }
1887
1888                 bool delegateCalled = false;
1889                 public delegate void TestDelegate ();
1890
1891                 public void delegate_call () {
1892                         delegateCalled = true;
1893
1894                         TestHelper.RemoveWarning (delegateCalled);
1895                 }
1896
1897                 [Test]
1898                 [ExpectedException(typeof(InvalidOperationException))]
1899                 public void InvokeException1 () {
1900                         Control c = new Control ();
1901                         IAsyncResult result;
1902
1903                         result = c.BeginInvoke (new TestDelegate (delegate_call));
1904                         c.EndInvoke (result);
1905                 }
1906
1907                 [Test]
1908                 public void FindFormTest () {
1909                         Form f = new Form ();
1910
1911                         f.ShowInTaskbar = false;
1912                         f.Name = "form";
1913                         Control c = null;
1914
1915                         try {
1916                                 f.Controls.Add (c = new Control ());
1917                                 Assert.AreEqual (f.Name, c.FindForm ().Name, "Find1");
1918
1919                                 f.Controls.Remove (c);
1920
1921                                 GroupBox g = new GroupBox ();
1922                                 g.Name = "box";
1923                                 f.Controls.Add (g);
1924                                 g.Controls.Add (c);
1925
1926                                 Assert.AreEqual (f.Name, f.FindForm ().Name, "Find2");
1927
1928                                 g.Controls.Remove (c);
1929                                 Assert.IsNull(c.FindForm (), "Find3");
1930
1931                         } finally {
1932                                 if (c != null)
1933                                         c.Dispose ();
1934                                 if (f != null)
1935                                         f.Dispose ();
1936                         }
1937                 }
1938
1939                 [Test]
1940                 public void FocusTest ()
1941                 {
1942                         Form f = null;
1943                         Button c = null, d = null;
1944
1945                         try {
1946                                 f = new Form ();
1947                                 f.ShowInTaskbar = false;
1948                                 f.Visible = true;
1949                                 c = new Button ();
1950                                 c.Visible = true;
1951                                 f.Controls.Add (c);
1952
1953                                 d = new Button ();
1954                                 d.Visible = false;
1955                                 f.Controls.Add (d);
1956
1957                                 Assert.IsTrue (c.CanFocus, "Focus1");
1958                                 Assert.IsFalse (c.Focused, "Focus2");
1959                                 c.Focus ();
1960                                 Assert.IsTrue (c.Focused, "Focus3");
1961                                 d.Focus ();
1962                                 Assert.IsFalse (d.Focused, "Focus4");
1963
1964                                 d.Visible = true;
1965                                 d.Focus ();
1966                                 Assert.IsTrue (d.Focused, "Focus5");
1967                                 Assert.IsFalse (c.Focused, "Focus6");
1968
1969                                 c.Enabled = false;
1970                                 Assert.IsFalse (c.Focused, "Focus7");
1971                         } finally {
1972                                 if (f != null)
1973                                         f.Dispose ();
1974                                 if (c != null)
1975                                         c.Dispose ();
1976                                 if (d != null)
1977                                         d.Dispose ();
1978                         }
1979                 }
1980
1981                 [Test]
1982                 public void FromHandleTest ()
1983                 {
1984                         Control c1 = null;
1985                         Control c2 = null;
1986
1987                         try {
1988                                 c1 = new Control ();
1989                                 c2 = new Control ();
1990
1991                                 c1.Name = "parent";
1992                                 c2.Name = "child";
1993                                 c1.Controls.Add(c2);
1994
1995                                 // Handle
1996                                 Assert.AreEqual (c1.Name, Control.FromHandle (c1.Handle).Name, "Handle1");
1997                                 Assert.IsNull (Control.FromHandle (IntPtr.Zero), "Handle2");
1998
1999                                 // ChildHandle
2000                                 Assert.AreEqual (c1.Name, Control.FromChildHandle (c1.Handle).Name, "Handle3");
2001                                 Assert.IsNull (Control.FromChildHandle (IntPtr.Zero), "Handle4");
2002
2003
2004                         } finally {
2005                                 if (c1 != null)
2006                                         c1.Dispose ();
2007
2008                                 if (c2 != null)
2009                                         c2.Dispose ();
2010                         }
2011                 }
2012
2013                 [Test]
2014                 public void GetChildAtPointTest ()
2015                 {
2016                         Control c = null, d = null;
2017                         TransparentControl e = null;
2018
2019                         try {
2020                                 c = new Control ();
2021                                 c.Name = "c1";
2022                                 c.SetBounds (0, 0, 100, 100);
2023
2024                                 d = new Control ();
2025                                 d.Name = "d1";
2026                                 d.SetBounds (10, 10, 40, 40);
2027                                 c.Controls.Add (d);
2028
2029                                 e = new TransparentControl ();
2030                                 e.Name = "e1";
2031                                 e.SetBounds (55, 55, 10, 10);
2032
2033                                 Control l = c.GetChildAtPoint (new Point (15, 15));
2034                                 Assert.AreEqual (d.Name, l.Name, "Child1");
2035                                 Assert.IsFalse (e.Name == l.Name, "Child2");
2036
2037                                 l = c.GetChildAtPoint (new Point (57, 57));
2038                                 Assert.AreEqual (null, l, "Child3");
2039
2040                                 l = c.GetChildAtPoint (new Point (10, 10));
2041                                 Assert.AreEqual (d.Name, l.Name, "Child4");
2042
2043                                 // GetChildAtPointSkip is not implemented and the following test is breaking for Net_2_0 profile
2044 #if NET_2_0
2045                                 c.Controls.Add (e);
2046                                 e.Visible = false;
2047                                 l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Invisible);
2048                                 Assert.IsNull (l, "Child5");
2049
2050                                 e.Visible = true;
2051                                 l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Invisible);
2052                                 Assert.AreSame (e.Name, l.Name, "Child6");
2053
2054                                 e.Enabled = false;
2055                                 l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Disabled);
2056                                 Assert.IsNull (l, "Child7");
2057
2058                                 e.Enabled = true;
2059                                 l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Disabled);
2060                                 Assert.AreSame (e.Name, l.Name, "Child8");
2061
2062                                 
2063                                 e.BackColor = Color.Transparent;
2064                                 l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Transparent);
2065                                 Assert.IsNull (l, "Child9");
2066
2067                                 e.BackColor = Color.Green;
2068                                 l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Transparent);
2069                                 Assert.AreSame (e.Name, l.Name, "Child10");
2070
2071 #endif // NET_2_0
2072                         } finally {
2073                                 if (c != null)
2074                                         c.Dispose ();
2075                                 if (d != null)
2076                                         d.Dispose ();
2077                         }
2078                 }
2079
2080                 private class TransparentControl : Control
2081                 {
2082                         public TransparentControl ()
2083                         {
2084                                 SetStyle (ControlStyles.SupportsTransparentBackColor, true);
2085                         }
2086
2087                         protected override CreateParams CreateParams
2088                         {
2089                                 get
2090                                 {
2091                                         CreateParams cp = base.CreateParams;
2092                                         cp.ExStyle |= 0x00000020;
2093                                         return cp;
2094                                 }
2095                         }
2096                 }
2097                 
2098                 [Test]
2099                 public void ResetFontTest ()
2100                 {
2101                         Control c = new Control ();
2102                         c.Font = new Font (c.Font.FontFamily, 3, FontStyle.Italic);
2103                         c.ResetFont ();
2104
2105                         Assert.IsFalse (c.Font.Bold, "#1");
2106                         //Assert.AreEqual ("Microsoft Sans Serif", c.Font.FontFamily.Name, "#2");
2107                         Assert.IsFalse (c.Font.Italic, "#3");
2108                         //Assert.AreEqual ("Microsoft Sans Serif", c.Font.Name, "#4");
2109                         Assert.AreEqual (8.25, c.Font.Size, "#5");
2110                         Assert.AreEqual (8.25, c.Font.SizeInPoints, "#6");
2111                         Assert.IsFalse (c.Font.Strikeout, "#7");
2112                         Assert.IsFalse (c.Font.Underline, "#8");
2113                         Assert.AreEqual (GraphicsUnit.Point, c.Font.Unit, "#9");
2114 #if NET_2_0
2115                         Assert.AreEqual (true, c.Font.IsSystemFont, "#10");
2116 #endif
2117                 }
2118
2119                 public class LayoutTestControl : Control {
2120                         public int LayoutCount;
2121
2122                         public LayoutTestControl () : base() {
2123                                 LayoutCount = 0;
2124                         }
2125
2126                         protected override void OnLayout(LayoutEventArgs levent) {
2127                                 LayoutCount++;
2128                                 base.OnLayout (levent);
2129                         }
2130                 }
2131
2132                 [Test]
2133                 public void LayoutTest() {
2134                         LayoutTestControl c;
2135
2136                         c = new LayoutTestControl();
2137
2138                         c.SuspendLayout();
2139                         c.SuspendLayout();
2140                         c.SuspendLayout();
2141                         c.SuspendLayout();
2142
2143                         c.ResumeLayout(true);
2144                         c.PerformLayout();
2145                         c.ResumeLayout(true);
2146                         c.PerformLayout();
2147                         c.ResumeLayout(true);
2148                         c.PerformLayout();
2149                         c.ResumeLayout(true);
2150                         c.PerformLayout();
2151                         c.ResumeLayout(true);
2152                         c.PerformLayout();
2153                         c.ResumeLayout(true);
2154                         c.PerformLayout();
2155                         c.ResumeLayout(true);
2156                         c.PerformLayout();
2157                         c.SuspendLayout();
2158                         c.PerformLayout();
2159
2160                         Assert.AreEqual(5, c.LayoutCount, "Layout Suspend/Resume locking does not bottom out at 0");
2161                 }
2162
2163                 [Test]
2164                 [ExpectedException(typeof(ArgumentException))]
2165                 public void TransparentBackgroundTest1() {
2166                         Control c;
2167
2168                         c = new Control();
2169                         c.BackColor = Color.Transparent;
2170                 }
2171
2172                 [Test]
2173                 public void TransparentBackgroundTest2() {
2174                         Panel   c;
2175
2176                         c = new Panel();
2177                         c.BackColor = Color.Transparent;
2178                         Assert.AreEqual(Color.Transparent, c.BackColor, "Transparent background not set");
2179                 }
2180
2181                 [Test]
2182                 public void TransparentBackgroundTest3() {
2183                         Control c;
2184
2185                         c = new Control();
2186                         c.BackColor = Color.Empty;
2187                         Assert.AreEqual(Control.DefaultBackColor, c.BackColor, "Setting empty color failed");
2188                 }
2189
2190                 [Test]
2191                 public void Dock_Value_Invalid ()
2192                 {
2193                         Control c = new Control ();
2194                         try {
2195                                 c.Dock = (DockStyle) 666;
2196                                 Assert.Fail ("#1");
2197                         } catch (InvalidEnumArgumentException ex) {
2198                                 Assert.AreEqual (typeof (InvalidEnumArgumentException), ex.GetType (), "#2");
2199                                 Assert.IsNotNull (ex.Message, "#3");
2200                                 Assert.IsNotNull (ex.ParamName, "#4");
2201                                 Assert.AreEqual ("value", ex.ParamName, "#5");
2202                                 Assert.IsNull (ex.InnerException, "#6");
2203                         }
2204                 }
2205
2206                 [Test]
2207                 public void EnabledTest1() {
2208                         Control child;
2209                         Control parent;
2210                         Control grandma;
2211
2212                         grandma = new Control();
2213                         parent = new Control();
2214                         child = new Control();
2215
2216                         grandma.Controls.Add(parent);
2217                         parent.Controls.Add(child);
2218                         grandma.Enabled = false;
2219                         Assert.AreEqual(grandma.Enabled, child.Enabled, "Child did not inherit disabled state");
2220                 }
2221
2222                 int EnabledCalledCount = 0;
2223                 private void EnabledTest2EnabledChanged(object sender, EventArgs e) {
2224                         EnabledCalledCount++;
2225                 }
2226
2227                 [Test]
2228                 public void EnabledTest2() {
2229                         // Check nesting of enabled calls
2230                         // OnEnabled is not called for disabled child controls
2231                         Control child;
2232                         Control parent;
2233                         Control grandma;
2234
2235                         EnabledCalledCount = 0;
2236
2237                         grandma = new Control();
2238                         parent = new Control();
2239                         child = new Control();
2240                         child.EnabledChanged += new EventHandler(EnabledTest2EnabledChanged);
2241
2242                         grandma.Controls.Add(parent);
2243                         parent.Controls.Add(child);
2244                         grandma.Enabled = false;
2245
2246                         Assert.AreEqual(1, EnabledCalledCount, "Child Enabled Event not properly fired");
2247                         grandma.Enabled = true;
2248                         Assert.AreEqual(2, EnabledCalledCount, "Child Enabled Event not properly fired");
2249                         child.Enabled = false;
2250                         grandma.Enabled = false;
2251                         Assert.AreEqual(3, EnabledCalledCount, "Child Enabled Event not properly fired");
2252                 }
2253
2254                 [Test]
2255                 public void ControlsRemoveNullTest ()
2256                 {
2257                         Control c = new Control ();
2258                         c.Controls.Remove (null);
2259                 }
2260
2261                 [Test]
2262                 public void ControlsAddNullTest ()
2263                 {
2264                         Control c = new Control ();
2265                         c.Controls.Add (null);
2266                 }
2267
2268                 [Test]
2269                 [ExpectedException (typeof (ArgumentNullException))]
2270                 public void ControlsSetChildIndexNullTest ()
2271                 {
2272                         Control c = new Control ();
2273                         c.Controls.SetChildIndex (null, 1);
2274                 }
2275
2276                 [Test]
2277                 [ExpectedException (typeof (ArgumentNullException))]
2278                 public void ControlsAddRangeNullTest ()
2279                 {
2280                         Control c = new Control ();
2281                         c.Controls.AddRange (null);
2282                 }
2283
2284                 [Test]
2285                 public void ControlsAddRangeNullElementTest ()
2286                 {
2287                         Control c = new Control ();
2288                         Control[] subcontrols = new Control[2];
2289                         subcontrols[0] = new Control ();
2290                         subcontrols[1] = null;
2291
2292                         c.Controls.AddRange (subcontrols);
2293                 }
2294
2295                 [Test]
2296                 public void RegionTest () {
2297                         Form f = new Form ();
2298                         f.ShowInTaskbar = false;
2299                         Control c = new Control ();
2300                         f.Controls.Add (c);
2301                         Assert.IsNull (c.Region, "#A1");
2302                         f.Show ();
2303                         Assert.IsNull (c.Region, "#A2");
2304                         c.Region = null;
2305                         Assert.IsNull (c.Region, "#A3");
2306                         f.Dispose ();
2307
2308                         Region region = new Region ();
2309                         f = new Form ();
2310                         f.ShowInTaskbar = false;
2311                         c = new Control ();
2312                         f.Controls.Add (c);
2313                         c.Region = region;
2314                         Assert.IsNotNull (c.Region, "#B1");
2315                         Assert.AreSame (region, c.Region, "#B2");
2316                         f.Show ();
2317                         c.Region = null;
2318                         Assert.IsNull (c.Region, "#B3");
2319
2320                         f.Dispose ();
2321                 }
2322
2323                 [Test] // bug #330501
2324                 public void OnValidating_Parent_Close ()
2325                 {
2326                         MockControl control = new MockControl ();
2327
2328                         Form f = new Form ();
2329                         f.Controls.Add (control);
2330                         f.ShowInTaskbar = false;
2331
2332                         f.Show ();
2333                         Assert.AreEqual (0, control.OnValidatingCount, "#A1");
2334                         f.Close ();
2335                         Assert.AreEqual (1, control.OnValidatingCount, "#A2");
2336                         f.Dispose ();
2337                 }
2338
2339                 [Test] // bug #80280
2340                 public void Validated_Multiple_Containers ()
2341                 {
2342                         Form form = new Form ();
2343                         form.ShowInTaskbar = false;
2344
2345                         UserControl control1 = new UserControl();
2346                         UserControl container1 = new UserControl();
2347                         control1.Tag = true;
2348                         control1.Validated += new EventHandler (Control_ValidatedHandler);
2349                         container1.Controls.Add(control1);
2350                         form.Controls.Add (container1);
2351
2352                         UserControl container2 = new UserControl();
2353                         UserControl control2 = new UserControl();
2354                         container2.Controls.Add(control2);
2355                         form.Controls.Add (container2);
2356
2357                         Assert.IsTrue ((bool) control1.Tag, "#1");
2358                         control1.Select();
2359                         Assert.IsTrue ((bool) control1.Tag, "#2");
2360                         control2.Select();
2361                         Assert.IsFalse ((bool) control1.Tag, "#3");
2362
2363                         form.Dispose ();
2364                 }
2365
2366                 private void Control_ValidatedHandler (object sender, EventArgs e)
2367                 {
2368                         ((Control) sender).Tag = false;
2369                 }
2370                 
2371                 [Test]
2372                 public void ControlReparentLocationTest ()
2373                 {
2374                         Form form = new Form ();
2375                         Label l = new Label ();
2376                         l.Location = new Point (0, 0);
2377                         form.Controls.Add (l);
2378                         form.Show ();
2379                         Assert.AreEqual (0, l.Left, "#A1");
2380                         Assert.AreEqual (0, l.Top, "#A2");
2381                         form.Hide ();
2382                         form.Controls.Remove (l);
2383                         form.Show ();
2384                         form.Controls.Add (l);
2385                         Assert.AreEqual (0, l.Left, "#A3");
2386                         Assert.AreEqual (0, l.Top, "#A4");
2387                         
2388                         form.Dispose ();
2389                 }
2390
2391 #if NET_2_0
2392                 [Test]
2393                 public void UseWaitCursorTest ()
2394                 {
2395                         Control c = new Control ();
2396                         Assert.IsFalse (c.UseWaitCursor, "#1");
2397                         c.Cursor = Cursors.Hand;
2398                         c.UseWaitCursor = true;
2399                         Assert.AreEqual (Cursors.WaitCursor, c.Cursor, "#2");
2400                         c.UseWaitCursor = false;
2401                         Assert.AreEqual (Cursors.Hand, c.Cursor, "#3");
2402                         
2403                         c.UseWaitCursor = true;
2404                         c.Cursor = Cursors.Help;
2405                         Assert.AreEqual (Cursors.WaitCursor, c.Cursor, "#4");
2406                         Assert.AreEqual (true, c.UseWaitCursor, "#5");
2407                         
2408                         c.UseWaitCursor = false;
2409                         Assert.AreEqual (Cursors.Help, c.Cursor, "#6");
2410                 }
2411
2412                 [Test] // bug #80621, #81125
2413                 public void DontCallSizeFromClientSize ()
2414                 {
2415                         SizeControl sc = new SizeControl ();
2416                         
2417                         Assert.AreEqual (0, sc.size_from_client_size_count, "A1");
2418                         
2419                         sc.ClientSize = new Size (300, 300);
2420                         Assert.AreEqual (0, sc.size_from_client_size_count, "A2");
2421                         
2422                         SizeForm sf = new SizeForm ();
2423                         sf.ShowInTaskbar = false;
2424                         sf.Show ();
2425                         
2426                         Assert.AreEqual (0, sc.size_from_client_size_count, "A3");
2427
2428                         sc.ClientSize = new Size (300, 300);
2429                         Assert.AreEqual (0, sc.size_from_client_size_count, "A4");      
2430                         
2431                         sf.Dispose ();  
2432                 }
2433                 
2434                 private class SizeControl : Control
2435                 {
2436                         public int size_from_client_size_count = 0;
2437                         
2438                         protected override Size SizeFromClientSize (Size clientSize)
2439                         {
2440                                 size_from_client_size_count++;
2441                                 return base.SizeFromClientSize (clientSize);
2442                         }
2443                 }
2444
2445                 private class SizeForm : Form
2446                 {
2447                         public int size_from_client_size_count = 0;
2448
2449                         protected override Size SizeFromClientSize (Size clientSize)
2450                         {
2451                                 size_from_client_size_count++;
2452                                 return base.SizeFromClientSize (clientSize);
2453                         }
2454                 }
2455 #endif
2456
2457                 public class MockControl : Control
2458                 {
2459                         public int OnValidatingCount;
2460                         public int OnSizeChangedCount;
2461                         public int OnResizeCount;
2462                         public int OnLocationChangedCount;
2463
2464                         public MockControl ()
2465                         {
2466                         }
2467
2468                         public MockControl (string text)
2469                                 : base (text)
2470                         {
2471                         }
2472
2473                         public MockControl (Control parent, string text)
2474                                 : base (parent, text)
2475                         {
2476                         }
2477
2478                         public MockControl (string text, int left, int top, int width, int height)
2479                                 : base (text, left, top, width, height)
2480                         {
2481                         }
2482
2483                         public MockControl (Control parent, string text, int left, int top, int width, int height)
2484                                 : base (parent, text, left, top, width, height)
2485                         {
2486                         }
2487
2488                         public int font_height
2489                         {
2490                                 get { return base.FontHeight; }
2491                                 set { base.FontHeight = value; }
2492                         }
2493
2494                         protected override void OnLocationChanged (EventArgs e)
2495                         {
2496                                 OnLocationChangedCount++;
2497                                 base.OnLocationChanged (e);
2498                         }
2499
2500                         protected override void OnSizeChanged (EventArgs e)
2501                         {
2502                                 OnSizeChangedCount++;
2503                                 base.OnSizeChanged (e);
2504                         }
2505
2506                         protected override void OnResize (EventArgs e)
2507                         {
2508                                 OnResizeCount++;
2509                                 base.OnResize (e);
2510                         }
2511
2512                         protected override void OnValidating (CancelEventArgs e)
2513                         {
2514                                 OnValidatingCount++;
2515                         }
2516                 }
2517
2518                 const int WM_KEYDOWN = 0x0100;
2519                 const int WM_CHAR = 0x0102;
2520                 const int WM_SYSCHAR = 0x0106;
2521                 const int WM_KEYUP = 0x0101;
2522
2523 #if NET_2_0
2524                 [Test]
2525                 public void MethodPreProcessControlMessage ()
2526                 {
2527                         bool testing_callstack = false;
2528
2529                         MyControl c = new MyControl ();
2530                         Message m = new Message ();
2531                         m.HWnd = c.Handle;
2532                         m.Msg = WM_KEYDOWN;
2533                         m.WParam = (IntPtr)Keys.Down;
2534                         m.LParam = IntPtr.Zero;
2535
2536                         if (testing_callstack) Console.WriteLine ("Start");
2537                         Assert.AreEqual (PreProcessControlState.MessageNotNeeded, c.PreProcessControlMessage (ref m), "A1");
2538                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2539
2540                         c.SetState (State.OnPreviewKeyDown);
2541                         if (testing_callstack) Console.WriteLine ("Start");
2542                         Assert.AreEqual (PreProcessControlState.MessageNeeded, c.PreProcessControlMessage (ref m), "A2");
2543                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2544
2545                         c.SetState (State.ProcessCmdKey);
2546                         if (testing_callstack) Console.WriteLine ("Start");
2547                         Assert.AreEqual (PreProcessControlState.MessageProcessed, c.PreProcessControlMessage (ref m), "A3");
2548                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2549
2550                         c.SetState (State.IsInputKey);
2551                         if (testing_callstack) Console.WriteLine ("Start");
2552                         Assert.AreEqual (PreProcessControlState.MessageNeeded, c.PreProcessControlMessage (ref m), "A4");
2553                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2554
2555                         c.SetState (State.ProcessDialogKey);
2556                         if (testing_callstack) Console.WriteLine ("Start");
2557                         Assert.AreEqual (PreProcessControlState.MessageProcessed, c.PreProcessControlMessage (ref m), "A5");
2558                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2559
2560
2561                         m.Msg = WM_CHAR;
2562                         c.SetState (State.None);
2563                         if (testing_callstack) Console.WriteLine ("Start");
2564                         Assert.AreEqual (PreProcessControlState.MessageNotNeeded, c.PreProcessControlMessage (ref m), "A6");
2565                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2566
2567                         c.SetState (State.IsInputChar);
2568                         if (testing_callstack) Console.WriteLine ("Start");
2569                         Assert.AreEqual (PreProcessControlState.MessageNeeded, c.PreProcessControlMessage (ref m), "A7");
2570                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2571
2572                         c.SetState (State.ProcessDialogChar);
2573                         if (testing_callstack) Console.WriteLine ("Start");
2574                         Assert.AreEqual (PreProcessControlState.MessageProcessed, c.PreProcessControlMessage (ref m), "A8");
2575                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2576
2577
2578                         m.Msg = WM_SYSCHAR;
2579                         c.SetState (State.None);
2580                         if (testing_callstack) Console.WriteLine ("Start");
2581                         Assert.AreEqual (PreProcessControlState.MessageNotNeeded, c.PreProcessControlMessage (ref m), "A9");
2582                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2583
2584                         c.SetState (State.IsInputChar);
2585                         if (testing_callstack) Console.WriteLine ("Start");
2586                         Assert.AreEqual (PreProcessControlState.MessageNeeded, c.PreProcessControlMessage (ref m), "A10");
2587                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2588
2589                         c.SetState (State.ProcessDialogChar);
2590                         if (testing_callstack) Console.WriteLine ("Start");
2591                         Assert.AreEqual (PreProcessControlState.MessageProcessed, c.PreProcessControlMessage (ref m), "A11");
2592                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2593
2594
2595                         m.Msg = WM_KEYUP;
2596                         if (testing_callstack) Console.WriteLine ("Start");
2597                         Assert.AreEqual (PreProcessControlState.MessageNotNeeded, c.PreProcessControlMessage (ref m), "A12");
2598                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2599
2600                         c.SetState (State.OnPreviewKeyDown);
2601                         if (testing_callstack) Console.WriteLine ("Start");
2602                         Assert.AreEqual (PreProcessControlState.MessageNotNeeded, c.PreProcessControlMessage (ref m), "A13");
2603                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2604
2605                         c.SetState (State.ProcessCmdKey);
2606                         if (testing_callstack) Console.WriteLine ("Start");
2607                         Assert.AreEqual (PreProcessControlState.MessageNotNeeded, c.PreProcessControlMessage (ref m), "A14");
2608                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2609
2610                         c.SetState (State.IsInputKey);
2611                         if (testing_callstack) Console.WriteLine ("Start");
2612                         Assert.AreEqual (PreProcessControlState.MessageNotNeeded, c.PreProcessControlMessage (ref m), "A15");
2613                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2614
2615                         c.SetState (State.ProcessDialogKey);
2616                         if (testing_callstack) Console.WriteLine ("Start");
2617                         Assert.AreEqual (PreProcessControlState.MessageNotNeeded, c.PreProcessControlMessage (ref m), "A16");
2618                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2619                 }
2620 #endif
2621
2622                 [Test]
2623                 public void MethodPreProcessMessage ()
2624                 {
2625                         bool testing_callstack = false;
2626
2627                         MyControl c = new MyControl ();
2628                         Message m = new Message ();
2629                         m.HWnd = c.Handle;
2630                         m.Msg = WM_KEYDOWN;
2631                         m.WParam = (IntPtr)Keys.Down;
2632                         m.LParam = IntPtr.Zero;
2633
2634                         if (testing_callstack) Console.WriteLine ("Start");
2635                         Assert.AreEqual (false, c.PreProcessMessage (ref m), "A1");
2636                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2637
2638                         c.SetState (State.OnPreviewKeyDown);
2639                         if (testing_callstack) Console.WriteLine ("Start");
2640                         Assert.AreEqual (false, c.PreProcessMessage (ref m), "A2");
2641                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2642
2643                         c.SetState (State.ProcessCmdKey);
2644                         if (testing_callstack) Console.WriteLine ("Start");
2645                         Assert.AreEqual (true, c.PreProcessMessage (ref m), "A3");
2646                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2647
2648                         c.SetState (State.IsInputKey);
2649                         if (testing_callstack) Console.WriteLine ("Start");
2650                         Assert.AreEqual (false, c.PreProcessMessage (ref m), "A4");
2651                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2652
2653                         c.SetState (State.ProcessDialogKey);
2654                         if (testing_callstack) Console.WriteLine ("Start");
2655                         Assert.AreEqual (true, c.PreProcessMessage (ref m), "A5");
2656                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2657
2658
2659                         m.Msg = WM_CHAR;
2660                         c.SetState (State.None);
2661                         if (testing_callstack) Console.WriteLine ("Start");
2662                         Assert.AreEqual (false, c.PreProcessMessage (ref m), "A6");
2663                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2664
2665                         c.SetState (State.IsInputChar);
2666                         if (testing_callstack) Console.WriteLine ("Start");
2667                         Assert.AreEqual (false, c.PreProcessMessage (ref m), "A7");
2668                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2669
2670                         c.SetState (State.ProcessDialogChar);
2671                         if (testing_callstack) Console.WriteLine ("Start");
2672                         Assert.AreEqual (true, c.PreProcessMessage (ref m), "A8");
2673                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2674
2675
2676                         m.Msg = WM_SYSCHAR;
2677                         c.SetState (State.None);
2678                         if (testing_callstack) Console.WriteLine ("Start");
2679                         Assert.AreEqual (false, c.PreProcessMessage (ref m), "A9");
2680                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2681
2682                         c.SetState (State.IsInputChar);
2683                         if (testing_callstack) Console.WriteLine ("Start");
2684                         Assert.AreEqual (false, c.PreProcessMessage (ref m), "A10");
2685                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2686
2687                         c.SetState (State.ProcessDialogChar);
2688                         if (testing_callstack) Console.WriteLine ("Start");
2689                         Assert.AreEqual (true, c.PreProcessMessage (ref m), "A11");
2690                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2691
2692
2693                         m.Msg = WM_KEYUP;
2694                         if (testing_callstack) Console.WriteLine ("Start");
2695                         Assert.AreEqual (false, c.PreProcessMessage (ref m), "A12");
2696                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2697
2698                         c.SetState (State.OnPreviewKeyDown);
2699                         if (testing_callstack) Console.WriteLine ("Start");
2700                         Assert.AreEqual (false, c.PreProcessMessage (ref m), "A13");
2701                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2702
2703                         c.SetState (State.ProcessCmdKey);
2704                         if (testing_callstack) Console.WriteLine ("Start");
2705                         Assert.AreEqual (false, c.PreProcessMessage (ref m), "A14");
2706                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2707
2708                         c.SetState (State.IsInputKey);
2709                         if (testing_callstack) Console.WriteLine ("Start");
2710                         Assert.AreEqual (false, c.PreProcessMessage (ref m), "A15");
2711                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2712
2713                         c.SetState (State.ProcessDialogKey);
2714                         if (testing_callstack) Console.WriteLine ("Start");
2715                         Assert.AreEqual (false, c.PreProcessMessage (ref m), "A16");
2716                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2717                 }
2718                 private enum State
2719                 {
2720                         None,
2721                         ProcessCmdKey,
2722                         OnPreviewKeyDown,
2723                         IsInputChar,
2724                         IsInputKey,
2725                         PreProcessMessage,
2726                         ProcessDialogKey,
2727                         ProcessDialogChar
2728                 }
2729
2730                 private class MyControl : Control
2731                 {
2732
2733                         private State current_state;
2734                         bool testing_callstack = false;
2735
2736                         public void SetState (State state)
2737                         {
2738                                 current_state = state;
2739                         }
2740
2741                         protected override bool ProcessCmdKey (ref Message msg, Keys keyData)
2742                         {
2743                                 if (testing_callstack) Console.Write ("ProcessCmdKey[");
2744                                 if (current_state == State.ProcessCmdKey) {
2745                                         if (testing_callstack) Console.WriteLine ("]");
2746                                         return true;
2747                                 }
2748
2749                                 bool retval = base.ProcessCmdKey (ref msg, keyData);
2750                                 if (testing_callstack) Console.WriteLine ("]");
2751                                 return retval;
2752                         }
2753
2754 #if NET_2_0
2755                         protected override void OnPreviewKeyDown (PreviewKeyDownEventArgs e)
2756                         {
2757                                 if (testing_callstack) Console.Write ("OnPreviewKeyDown[");
2758                                 if (current_state == State.OnPreviewKeyDown) {
2759                                         e.IsInputKey = true;
2760                                         if (testing_callstack) Console.WriteLine ("]");
2761                                         return;
2762                                 }
2763
2764                                 base.OnPreviewKeyDown (e);
2765                                 if (testing_callstack) Console.WriteLine ("]");
2766                         }
2767 #endif
2768
2769                         protected override bool IsInputChar (char charCode)
2770                         {
2771                                 if (testing_callstack) Console.Write ("IsInputChar[");
2772                                 if (current_state == State.IsInputChar) {
2773                                         if (testing_callstack) Console.WriteLine ("true]");
2774                                         return true;
2775                                 }
2776
2777                                 bool retval = base.IsInputChar (charCode);
2778                                 if (testing_callstack) Console.WriteLine ("{0}]", retval.ToString ());
2779                                 return retval;
2780                         }
2781
2782                         protected override bool IsInputKey (Keys keyData)
2783                         {
2784                                 if (testing_callstack) Console.Write ("IsInputKey[");
2785                                 if (current_state == State.IsInputKey) {
2786                                         if (testing_callstack) Console.WriteLine ("]");
2787                                         return true;
2788                                 }
2789
2790                                 bool retval = base.IsInputKey (keyData);
2791                                 if (testing_callstack) Console.WriteLine ("]");
2792                                 return retval;
2793                         }
2794
2795                         public override bool PreProcessMessage (ref Message msg)
2796                         {
2797                                 if (testing_callstack) Console.Write ("PreProcessMessage[");
2798                                 if (current_state == State.PreProcessMessage) {
2799                                         if (testing_callstack) Console.WriteLine ("]");
2800                                         return true;
2801                                 }
2802
2803                                 bool retval = base.PreProcessMessage (ref msg);
2804                                 if (testing_callstack) Console.WriteLine ("]");
2805                                 return retval;
2806                         }
2807
2808                         protected override bool ProcessDialogKey (Keys keyData)
2809                         {
2810                                 if (testing_callstack) Console.Write ("ProcessDialogKey[");
2811                                 if (current_state == State.ProcessDialogKey) {
2812                                         if (testing_callstack) Console.WriteLine ("]");
2813                                         return true;
2814                                 }
2815
2816                                 bool retval = base.ProcessDialogKey (keyData);
2817                                 if (testing_callstack) Console.WriteLine ("]");
2818                                 return retval;
2819                         }
2820
2821                         protected override bool ProcessDialogChar (char charCode)
2822                         {
2823                                 if (testing_callstack) Console.Write ("ProcessDialogChar[");
2824                                 if (current_state == State.ProcessDialogChar) {
2825                                         if (testing_callstack) Console.WriteLine ("]");
2826                                         return true;
2827                                 }
2828
2829                                 bool retval = base.ProcessDialogChar (charCode);
2830                                 if (testing_callstack) Console.WriteLine ("]");
2831                                 return retval;
2832                         }
2833                 }
2834                 
2835                 [Test]
2836                 public void MethodIsInputChar ()
2837                 {
2838                         // Basically, show that this method always returns false
2839                         InputCharControl m = new InputCharControl ();
2840                         bool result = false;
2841                         
2842                         for (int i = 0; i < 256; i++)
2843                                 result |= m.PublicIsInputChar ((char)i);
2844                         
2845                         Assert.AreEqual (false, result, "I1");
2846                 }
2847
2848                 private class InputCharControl : Control
2849                 {
2850                         public bool PublicIsInputChar (char charCode)
2851                         {
2852                                 return base.IsInputChar (charCode);
2853                         }
2854
2855                 }
2856
2857                 [Test] // bug #81118, 81718
2858                 public void VisibleTriggersLayout ()
2859                 {
2860                         Form f = new Form ();
2861                         f.ShowInTaskbar = false;
2862                         
2863                         Control c = new Control ();
2864                         c.Visible = false;
2865                         
2866                         f.Controls.Add (c);
2867                         
2868                         c.Dock = DockStyle.Fill;
2869                         c.Visible = true;
2870                         
2871                         Assert.AreEqual (f.ClientSize.Width, c.Width, "L1");
2872                         
2873                         f.Dispose ();
2874                 }
2875                 
2876                 [Test]
2877                 public void ResumeLayoutEffects ()
2878                 {
2879                         Form f = new Form ();
2880                         f.ShowInTaskbar = false;
2881                         f.ClientSize = new Size (300, 300);
2882                         
2883                         Button button1 = new Button ();
2884                         f.Controls.Add (button1);
2885                         button1.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
2886                         button1.Location = new Point (f.ClientSize.Width - button1.Width, f.ClientSize.Height - button1.Height);
2887
2888                         f.Show ();
2889                         
2890                         Assert.AreEqual (new Point (225, 277), button1.Location, "A1");
2891                         
2892                         f.SuspendLayout ();
2893                         f.Height += 10;
2894                         f.ResumeLayout (false);
2895                         f.PerformLayout ();
2896
2897                         Assert.AreEqual (new Point (225, 277), button1.Location, "A2");
2898
2899                         f.SuspendLayout ();
2900                         f.Height += 10;
2901                         f.ResumeLayout ();
2902
2903                         Assert.AreEqual (new Point (225, 287), button1.Location, "A3");
2904                         f.Dispose ();
2905                 }
2906                 
2907                 [Test]
2908                 public void DisposeEnumerator ()
2909                 {
2910                         // We can modify the collection while looping through it without crashing
2911                         Control c = new Control ();
2912
2913                         c.Controls.Add (new Control ());
2914                         c.Controls.Add (new Control ());
2915                         
2916                         foreach (Control c1 in c.Controls)
2917                                 c.Controls.Remove (c1);
2918                 }
2919                 
2920                 [Test]
2921                 public void MethodSetBounds ()
2922                 {
2923                         Control myControl = new Control();
2924                         myControl.SetBounds(10, 20, 30, 40);
2925                         myControl.SetBounds(50, 60, 70, 70, BoundsSpecified.Location);
2926
2927                         Assert.AreEqual (new Rectangle (50, 60, 30, 40), myControl.Bounds, "A1");
2928                 }
2929                 
2930                 [Test]
2931                 public void Bug386450 ()
2932                 {
2933                         // Should not crash.  We have to check for the font object
2934                         // being different, not just if they represent the same font.
2935                         Form f = new Form ();
2936                         Label l = new Label ();
2937                         l.Text = "Hello";
2938
2939                         Font f1 = new Font ("Arial", 12);
2940                         Font f2 = new Font ("Arial", 12);
2941
2942                         l.Font = f1;
2943                         l.Font = f2;
2944
2945                         f1.Dispose ();
2946
2947                         f.Controls.Add (l);
2948                         f.Show ();
2949                         f.Dispose ();
2950                 }
2951         }
2952
2953         [TestFixture]
2954         public class ControlSetTopLevelTest : TestHelper
2955         {
2956                 class ControlPoker : Control {
2957                         public void DoSetTopLevel ()
2958                         {
2959                                 SetTopLevel (true);
2960                         }
2961                         public bool DoGetTopLevel ()
2962                         {
2963                                 return GetTopLevel ();
2964                         }
2965                 }
2966
2967                 [Test]
2968                 public void TestControl ()
2969                 {
2970                         ControlPoker c = new ControlPoker ();
2971                         c.Visible = false;
2972                         c.DoSetTopLevel ();
2973                         Assert.IsTrue (c.DoGetTopLevel (), "1");
2974                         Assert.IsFalse (c.Visible, "2");
2975                 }
2976
2977                 [Test]
2978                 [ExpectedException (typeof (ArgumentException))]
2979                 public void TestChildControl ()
2980                 {
2981                         Control c1 = new Control();
2982                         ControlPoker c2 = new ControlPoker ();
2983
2984                         c1.Controls.Add (c2);
2985                         c2.DoSetTopLevel ();
2986                 }
2987
2988                 [Test]
2989                 [ExpectedException (typeof (ArgumentException))]
2990                 public void TestTopLevelAdd () {
2991                         Form f = new Form();
2992                         Form f1 = new Form();
2993                         f.Controls.Add(f1);
2994                 }
2995                 
2996                 [Test]
2997                 [Category ("NotWorking")]
2998                 public void TestForm ()
2999                 {
3000                         Form f = new Form ();
3001                         Assert.IsFalse (f.Visible, "3");
3002                         f.TopLevel = true;
3003                         Assert.IsFalse (f.Visible, "4");
3004                         
3005                         f.Dispose ();
3006                 }
3007         }
3008
3009         [TestFixture]
3010         public class ControlResizeLayoutTest : TestHelper
3011         {
3012                 class ControlPoker : Control {
3013                         public void DoOnResize ()
3014                         {
3015                                 OnResize (EventArgs.Empty);
3016                         }
3017                 }
3018
3019                 int child_event;
3020                 string child_affected_property;
3021                 void ChildLayoutEvent (object sender, LayoutEventArgs e)
3022                 {
3023                         child_event ++;
3024                         child_affected_property = e.AffectedProperty;
3025                 }
3026
3027                 int parent_event;
3028                 string parent_affected_property;
3029                 void ParentLayoutEvent (object sender, LayoutEventArgs e)
3030                 {
3031                         parent_event ++;
3032                         parent_affected_property = e.AffectedProperty;
3033
3034                         TestHelper.RemoveWarning (parent_affected_property);
3035                 }
3036
3037                 [Test]
3038                 public void Test ()
3039                 {
3040                         Panel p = new Panel ();
3041                         ControlPoker c = new ControlPoker();
3042
3043                         p.Controls.Add (c);
3044
3045                         p.Layout += new LayoutEventHandler (ParentLayoutEvent);
3046                         c.Layout += new LayoutEventHandler (ChildLayoutEvent);
3047
3048                         c.DoOnResize ();
3049
3050                         Assert.AreEqual (1, child_event, "1");
3051                         Assert.AreEqual ("Bounds", child_affected_property, "2");
3052
3053                         Assert.AreEqual (0, parent_event, "3");
3054                 }
3055                 
3056         }
3057
3058         [TestFixture]
3059         [Category ("NotWorking")]
3060         public class ControlInvokeTest  : TestHelper {
3061
3062                 [TearDown]
3063                 protected override void TearDown ()
3064                 {
3065                         if (f != null && !f.IsDisposed)                 
3066                                 f.Dispose ();
3067                         base.TearDown ();
3068                 }
3069
3070                 
3071                 public delegate void TestDelegate ();
3072
3073                 Form f;
3074                 Control c;
3075                 Thread control_t;
3076                 ApplicationContext control_context;
3077                 bool delegateCalled = false;
3078                 bool threadDied = false;
3079
3080                 object m;
3081
3082                 void CreateControl ()
3083                 {
3084                         try {
3085                         f = new Form ();
3086                         f.ShowInTaskbar = false;
3087                         
3088                         c = new Control ();
3089
3090                         f.Controls.Add (c);
3091
3092                         Console.WriteLine ("f.Handle = {0}", f.Handle);
3093                         Console.WriteLine ("c.Handle = {0}", c.Handle);
3094
3095                         control_context = new ApplicationContext (f);
3096
3097                         Monitor.Enter (m);
3098                         Console.WriteLine ("pulsing");
3099                         Monitor.Pulse (m);
3100                         Monitor.Exit (m);
3101                         Console.WriteLine ("control thread running");
3102                         Application.Run (control_context);
3103                         c.Dispose ();
3104                         Console.WriteLine ("dying");
3105                         threadDied = true;
3106                         Monitor.Enter (m);
3107                         Console.WriteLine ("pulsing again");
3108                         Monitor.Pulse (m);
3109                         Monitor.Exit (m);
3110                         } catch (Exception e) { Console.WriteLine (e); }
3111                 }
3112
3113                 [Test]
3114                 public void InvokeTest ()
3115                 {
3116                         m = new object ();
3117
3118                         control_t = new Thread(new ThreadStart(CreateControl));
3119
3120                         Monitor.Enter (m);
3121
3122                         control_t.Start ();
3123
3124                         Console.WriteLine ("waiting on monitor");
3125                         Monitor.Wait (m);
3126
3127                         Console.WriteLine ("making async call");
3128
3129                         IAsyncResult result;
3130                         result = c.BeginInvoke (new TestDelegate (delegate_call));
3131                         c.EndInvoke (result);
3132
3133                         Assert.IsTrue (delegateCalled, "Invoke1");
3134
3135                         Monitor.Wait (m);
3136                         Assert.IsTrue (threadDied, "Invoke2");
3137                 }
3138
3139                 public void delegate_call () {
3140                         try {
3141                         /* invoked on control_context's thread */
3142                         delegateCalled = true;
3143                         f.Dispose ();
3144                         Console.WriteLine ("calling Application.Exit");
3145                         control_context.ExitThread ();
3146                         } catch (Exception e) { Console.WriteLine (e); }
3147                 }
3148                 
3149         }
3150
3151         [TestFixture]
3152         public class ControlWMTest : TestHelper
3153         {
3154                 [Test]
3155                 public void WM_PARENTNOTIFY_Test ()
3156                 {
3157                         WMTester tester;
3158                         Control child;
3159                         int child_handle;
3160                         
3161                         tester = new WMTester ();
3162                         child = new Control ();
3163                         tester.Controls.Add (child);
3164                         
3165                         tester.Visible = true;
3166                         child.Visible = true;
3167
3168                         child_handle = child.Handle.ToInt32 ();
3169
3170                         ArrayList msgs;
3171                         Message m1;
3172                                 
3173                         msgs = tester.Find (WndMsg.WM_PARENTNOTIFY);
3174                         
3175                         Assert.AreEqual (1, msgs.Count, "#1");
3176                         
3177                         m1 = (Message) msgs [0];
3178                         Assert.AreEqual (WndMsg.WM_CREATE, ((WndMsg) LowOrder (m1.WParam)),  "#2");
3179                         //Assert.AreEqual (child.Identifier??, HighOrder (m1.WParam),  "#3");
3180                         Assert.AreEqual (child_handle, m1.LParam.ToInt32 (),  "#4");
3181
3182                         child.Dispose ();
3183
3184                         msgs = tester.Find (WndMsg.WM_PARENTNOTIFY);
3185                         Assert.AreEqual (2, msgs.Count, "#5");
3186                         m1 = (Message) msgs [1];
3187
3188                         Assert.AreEqual (WndMsg.WM_DESTROY, ((WndMsg) LowOrder (m1.WParam)),  "#6");
3189                         //Assert.AreEqual (child.Identifier??, HighOrder (m1.WParam),  "#7");
3190                         Assert.AreEqual (child_handle, m1.LParam.ToInt32 (),  "#8");
3191
3192                         tester.Dispose ();
3193                 }
3194
3195                 internal static int LowOrder (int param) 
3196                 {
3197                         return ((int)(short)(param & 0xffff));
3198                 }
3199
3200                 internal static int HighOrder (int param) 
3201                 {
3202                         return ((int)(short)(param >> 16));
3203                 }
3204
3205                 internal static int LowOrder (IntPtr param) 
3206                 {
3207                         return ((int)(short)(param.ToInt32 () & 0xffff));
3208                 }
3209
3210                 internal static int HighOrder (IntPtr param) 
3211                 {
3212                         return ((int)(short)(param.ToInt32 () >> 16));
3213                 }
3214
3215                 internal class WMTester : Form
3216                 {
3217                         internal ArrayList Messages = new ArrayList ();
3218                         
3219                         internal bool Contains (WndMsg msg)
3220                         {
3221                                 return Contains (msg, Messages);
3222                         }
3223
3224                         internal bool Contains (WndMsg msg, ArrayList list)
3225                         {
3226                                 foreach (Message m in Messages) 
3227                                 {
3228                                         if (m.Msg == (int) msg)
3229                                                 return true;
3230                                 }
3231                                 return false;
3232                         }
3233
3234                         internal ArrayList Find (WndMsg msg)
3235                         {
3236                                 ArrayList result = new ArrayList ();
3237
3238                                 foreach (Message m in Messages)
3239                                 {
3240                                         if (m.Msg == (int) msg)
3241                                                 result.Add (m);
3242                                 }
3243                                 return result;
3244                         }
3245
3246                         protected override void WndProc(ref Message m)
3247                         {
3248                                 Console.WriteLine ("WndProc: " + m.ToString ());
3249                                 Messages.Add (m);
3250                                 base.WndProc (ref m);
3251                         }
3252                 }
3253         }
3254
3255 #if NET_2_0
3256         [TestFixture]
3257         public class ControlLayoutTest : TestHelper
3258         {
3259                 [SetUp]
3260                 protected override void SetUp ()
3261                 {
3262                         _layoutCount = 0;
3263                         base.SetUp ();
3264                 }
3265
3266                 [Test] // bug #80456
3267                 public void LayoutTest ()
3268                 {
3269                         MockLayoutEngine layoutEngine = new MockLayoutEngine ();
3270                         MockControl c = new MockControl (layoutEngine);
3271                         c.Layout += new LayoutEventHandler (LayoutEvent);
3272                         Assert.IsFalse (layoutEngine.LayoutInvoked, "#A1");
3273                         Assert.AreEqual (0, _layoutCount, "#A2");
3274                         c.PerformLayout ();
3275                         Assert.IsTrue (layoutEngine.LayoutInvoked, "#A3");
3276                         Assert.AreEqual (1, _layoutCount, "#A4");
3277
3278                         layoutEngine.Reset ();
3279                         c.OverrideOnLayout = true;
3280                         Assert.IsFalse (layoutEngine.LayoutInvoked, "#B1");
3281                         c.PerformLayout ();
3282                         Assert.IsFalse (layoutEngine.LayoutInvoked, "#B2");
3283                         Assert.AreEqual (1, _layoutCount, "#B3");
3284                 }
3285
3286                 void LayoutEvent (object sender, LayoutEventArgs e)
3287                 {
3288                         _layoutCount++;
3289                 }
3290
3291                 private int _layoutCount;
3292
3293                 class MockControl : Control
3294                 {
3295                         public MockControl (LayoutEngine layoutEngine)
3296                         {
3297                                 _layoutEngine = layoutEngine;
3298                         }
3299
3300                         public bool OverrideOnLayout
3301                         {
3302                                 get { return _overrideOnLayout; }
3303                                 set { _overrideOnLayout = value; }
3304                         }
3305
3306                         protected override void OnLayout (LayoutEventArgs levent)
3307                         {
3308                                 if (!OverrideOnLayout)
3309                                         base.OnLayout (levent);
3310                         }
3311
3312                         public override LayoutEngine LayoutEngine {
3313                                 get {
3314                                         if (_layoutEngine == null)
3315                                                 return base.LayoutEngine;
3316                                         return _layoutEngine;
3317                                 }
3318                         }
3319
3320                         private bool _overrideOnLayout;
3321                         private LayoutEngine _layoutEngine;
3322                 }
3323
3324                 class MockLayoutEngine : LayoutEngine
3325                 {
3326                         public bool LayoutInvoked {
3327                                 get { return _layoutInvoked; }
3328                         }
3329
3330                         public void Reset ()
3331                         {
3332                                 _layoutInvoked = false;
3333                         }
3334
3335                         public override bool Layout (object container, LayoutEventArgs args)
3336                         {
3337                                 _layoutInvoked = true;
3338                                 return true;
3339                         }
3340
3341                         private bool _layoutInvoked;
3342                 }
3343         }
3344 #endif
3345 }