2008-11-24 Jonathan Pobst <monkey@jpobst.com>
[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                 [Test]
1023                 [Category ("NotWorking")]
1024                 public void CreatedAccessibilityTest ()
1025                 {
1026                         Control c = new Control ();
1027                         Assert.IsFalse (c.Created, "A1");
1028
1029                         Helper.TestAccessibility(c, null, null, null, AccessibleRole.Default);
1030
1031                         Assert.IsTrue (c.Created, "A2");
1032
1033                         c.Dispose ();
1034
1035                         Assert.IsFalse (c.Created, "A3");
1036                 }
1037
1038                 [Test]
1039                 [Category ("NotWorking")]
1040                 public void BoundsTest ()
1041                 {
1042                         Control c = new Control ();
1043                         Assert.IsTrue (c.Bounds.IsEmpty, "A1");
1044                         Assert.IsTrue (c.Size.IsEmpty, "A2");
1045                         Assert.IsTrue (c.ClientSize.IsEmpty, "A3");
1046                         Assert.IsTrue (c.ClientRectangle.IsEmpty, "A4");
1047
1048                         Assert.AreEqual (((IWin32Window)c).Handle, c.Handle, "A5");
1049
1050                         /* this part fails on linux because we can't allocate X windows which are 0x0,
1051                            and the Control bounds directly reflect the size of the X window */
1052
1053                         Assert.IsTrue (c.Bounds.IsEmpty, "A6");
1054                         Assert.IsTrue (c.Size.IsEmpty, "A7");
1055                         Assert.IsTrue (c.ClientSize.IsEmpty, "A8");
1056                         Assert.IsTrue (c.ClientRectangle.IsEmpty, "A9");
1057                 }
1058
1059                 [Test]
1060                 [Ignore ("Depends on specific DPI")]
1061                 public void FontHeightTest ()
1062                 {
1063                         MockControl c = new MockControl ();
1064                         Assert.AreEqual (13, c.font_height);
1065                 }
1066
1067                 [Test]
1068                 public void FontTest ()
1069                 {
1070                         Control c = new Control ();
1071                         Assert.IsFalse (c.Font.Bold, "#A1");
1072                         //Assert.AreEqual ("Microsoft Sans Serif", c.Font.FontFamily.Name, "#A2");
1073                         Assert.IsFalse (c.Font.Italic, "#A3");
1074                         //Assert.AreEqual ("Microsoft Sans Serif", c.Font.Name, "#A4");
1075                         Assert.AreEqual (8.25, c.Font.Size, "#A5");
1076                         Assert.AreEqual (8.25, c.Font.SizeInPoints, "#A6");
1077                         Assert.IsFalse (c.Font.Strikeout, "#A7");
1078                         Assert.IsFalse (c.Font.Underline, "#A8");
1079                         Assert.AreEqual (GraphicsUnit.Point, c.Font.Unit, "#A9");
1080 #if NET_2_0
1081                         Assert.IsTrue (c.Font.IsSystemFont, "#A10");
1082 #endif
1083
1084                         c.Font = new Font (c.Font.FontFamily, 3, FontStyle.Italic);
1085                         Assert.IsFalse (c.Font.Bold, "#B1");
1086                         //Assert.AreEqual ("Microsoft Sans Serif", c.Font.FontFamily.Name, "#B2");
1087                         Assert.IsTrue (c.Font.Italic, "#B3");
1088                         //Assert.AreEqual ("Microsoft Sans Serif", c.Font.Name, "#B4");
1089                         Assert.AreEqual (3, c.Font.Size, "#B5");
1090                         Assert.AreEqual (3, c.Font.SizeInPoints, "#B6");
1091                         Assert.IsFalse (c.Font.Strikeout, "#B7");
1092                         Assert.IsFalse (c.Font.Underline, "#B8");
1093                         Assert.AreEqual (GraphicsUnit.Point, c.Font.Unit, "#B9");
1094 #if NET_2_0
1095                         Assert.AreEqual (false, c.Font.IsSystemFont, "#B10");
1096 #endif
1097                 }
1098
1099                 [Test]
1100                 [Category ("NotWorking")] // on Unix mapping is done to Bitstream Vera Sans
1101                 public void FontTest_Names ()
1102                 {
1103                         Control c = new Control ();
1104                         Assert.AreEqual ("Microsoft Sans Serif", c.Font.FontFamily.Name, "#1");
1105                         Assert.AreEqual ("Microsoft Sans Serif", c.Font.Name, "#2");
1106                 }
1107
1108                 [Test]
1109                 public void PubPropTest()
1110                 {
1111                         Control c = new Control();
1112
1113                         Assert.IsFalse (c.AllowDrop , "A1");
1114                         Assert.AreEqual(AnchorStyles.Top | AnchorStyles.Left, c.Anchor, "A2");
1115
1116                         Assert.AreEqual ("Control", c.BackColor.Name , "B1");
1117                         Assert.IsNull (c.BackgroundImage, "B2");
1118                         Assert.IsNull (c.BindingContext, "B3");
1119 #if NET_2_0
1120                         Assert.AreEqual (ImageLayout.Tile, c.BackgroundImageLayout, "B4");
1121 #endif
1122
1123                         Assert.IsFalse (c.CanFocus, "C1");
1124                         Assert.IsTrue (c.CanSelect, "C2");
1125                         Assert.IsFalse (c.Capture, "C3");
1126                         Assert.IsTrue (c.CausesValidation, "C4");
1127
1128                         Assert.IsNotNull (c.CompanyName, "C7");
1129                         Assert.IsNull (c.Container, "C8");
1130                         Assert.IsFalse (c.ContainsFocus, "C9");
1131                         Assert.IsNull (c.ContextMenu, "C10");
1132                         Assert.AreEqual (0, c.Controls.Count, "C11");
1133                         Assert.IsFalse (c.Created, "C12");
1134                         Assert.AreEqual (Cursors.Default, c.Cursor, "C13");
1135
1136                         Assert.IsNotNull(c.DataBindings, "D1");
1137                         Assert.AreEqual("Control", Control.DefaultBackColor.Name, "D2");
1138                         Assert.AreEqual("ControlText", Control.DefaultForeColor.Name, "D3");
1139                         Assert.AreEqual(FontStyle.Regular, Control.DefaultFont.Style, "D4");
1140                         Assert.AreEqual (new Rectangle(0, 0, 0, 0), c.DisplayRectangle , "D5");
1141                         Assert.IsFalse (c.Disposing, "D6");
1142                         Assert.AreEqual(DockStyle.None, c.Dock, "D7");
1143
1144                         Assert.IsTrue (c.Enabled, "E1");
1145
1146                         Assert.IsFalse  (c.Focused, "F1");
1147                         Assert.AreEqual (FontStyle.Regular, c.Font.Style, "F2");
1148                         Assert.AreEqual (SystemColors.ControlText, c.ForeColor, "F3");
1149
1150                         Assert.IsFalse  (c.HasChildren, "H2");
1151
1152                         Assert.AreEqual (ImeMode.NoControl, c.ImeMode, "I1");
1153                         Assert.IsFalse (c.InvokeRequired, "I2");
1154                         Assert.IsFalse (c.IsAccessible, "I3");
1155                         Assert.IsFalse (c.IsDisposed, "I4");
1156                         Assert.IsFalse (c.IsHandleCreated, "I5");
1157
1158                         Assert.AreEqual(Point.Empty, c.Location, "L2");
1159
1160 #if NET_2_0
1161                         Assert.IsTrue(c.MaximumSize.IsEmpty);
1162                         Assert.IsTrue(c.MinimumSize.IsEmpty);
1163 #endif
1164                         Assert.AreEqual (Keys.None, Control.ModifierKeys, "M1");
1165                         Assert.IsTrue (Control.MousePosition.X >= 0 && Control.MousePosition.Y >= 0, "M2");
1166                         Assert.AreEqual (MouseButtons.None, Control.MouseButtons, "M3");
1167
1168                         Assert.AreEqual("", c.Name, "N1");
1169                         c.Name = "Control Name";
1170                         Assert.AreEqual("Control Name", c.Name, "N2");
1171
1172                         Assert.IsNull (c.Parent, "P1");
1173                         Assert.IsNotNull (c.ProductName, "P2");
1174                         Assert.IsTrue (c.ProductName != "", "P3");
1175                         Assert.IsNotNull (c.ProductVersion, "P4");
1176                         Assert.IsTrue (c.ProductVersion != "", "P5");
1177
1178                         Assert.IsFalse (c.RecreatingHandle, "R1");
1179                         Assert.IsNull (c.Region, "R2");
1180                         Assert.AreEqual (RightToLeft.No, c.RightToLeft, "R4");
1181
1182                         Assert.IsNull (c.Site, "S1");
1183
1184                         Assert.AreEqual (0, c.TabIndex , "T1");
1185                         Assert.IsTrue (c.TabStop, "T2");
1186                         Assert.IsNull (c.Tag, "T3");
1187                         Assert.AreEqual ("", c.Text, "T4");
1188
1189                         Assert.IsTrue (c.Visible, "V1");
1190                 }
1191
1192                 [Test]
1193                 public void SizeChangeTest ()
1194                 {
1195                         Form f = new Form ();
1196                         Control c = new Control ();
1197                         f.Controls.Add(c);
1198                         f.Show();
1199                         c.Resize += new EventHandler(SizeChangedTest_ResizeHandler);
1200                         c.Tag = true;
1201                         c.Size = c.Size;
1202                         Assert.AreEqual (true, (bool) c.Tag, "#1");
1203                         f.Close ();
1204                 }
1205
1206                 private void SizeChangedTest_ResizeHandler (object sender, EventArgs e)
1207                 {
1208                         ((Control) sender).Tag = false;
1209                 }
1210
1211                 [Test]
1212                 public void NegativeHeightTest ()
1213                 {
1214                         Control c = new Control ();
1215                         IntPtr handle = c.Handle;
1216                         c.Resize += new EventHandler(NegativeHeightTest_ResizeHandler);
1217                         c.Tag = -2;
1218                         c.Height = 2;
1219                         c.Height = -2;
1220                         Assert.AreEqual (0, (int) c.Tag, "#1");
1221                         c.Dispose ();
1222                         Assert.AreEqual (handle, handle, "Removes warning.");
1223                 }
1224                 
1225                 private void NegativeHeightTest_ResizeHandler (object sender, EventArgs e)
1226                 {
1227                         Control c = (Control) sender;
1228                         c.Tag = c.Height;
1229                 }
1230                 
1231                 [Test]
1232                 public void TopLevelControlTest () {
1233                         Control c = new Control ();
1234
1235                         Assert.AreEqual(null, c.TopLevelControl, "T1");
1236
1237                         Panel p = new Panel ();
1238
1239                         p.Controls.Add (c);
1240
1241                         Assert.AreEqual(null, c.TopLevelControl, "T2");
1242
1243                         Form f = new Form ();
1244                         f.ShowInTaskbar = false;
1245
1246                         f.Controls.Add (p);
1247
1248                         Assert.AreEqual (f, c.TopLevelControl, "T3");
1249                         Assert.AreEqual (f, f.TopLevelControl, "T4");
1250                         
1251                         f.Dispose ();
1252                 }
1253
1254                 [Test]
1255                 public void RelationTest() {
1256                         Control c1;
1257                         Control c2;
1258
1259                         c1 = new Control();
1260                         c2 = new Control();
1261
1262                         Assert.AreEqual(true , c1.Visible , "Rel1");
1263                         Assert.AreEqual(false, c1.Contains(c2) , "Rel2");
1264                         Assert.AreEqual("System.Windows.Forms.Control", c1.ToString() , "Rel3");
1265
1266                         c1.Controls.Add(c2);
1267                         Assert.AreEqual(true , c2.Visible , "Rel4");
1268                         Assert.AreEqual(true, c1.Contains(c2) , "Rel5");
1269
1270                         c1.Anchor = AnchorStyles.Top;
1271                         c1.SuspendLayout ();
1272                         c1.Anchor = AnchorStyles.Left ;
1273                         c1.ResumeLayout ();
1274                         Assert.AreEqual(AnchorStyles.Left , c1.Anchor, "Rel6");
1275
1276                         c1.SetBounds(10, 20, 30, 40) ;
1277                         Assert.AreEqual(new Rectangle(10, 20, 30, 40), c1.Bounds, "Rel7");
1278
1279                         Assert.AreEqual(c1, c2.Parent, "Rel8");
1280                 }
1281
1282                 [Test]
1283                 public void AnchorDockTest ()
1284                 {
1285                         Control c = new Control ();
1286
1287                         Assert.AreEqual (DockStyle.None, c.Dock, "1");
1288                         Assert.AreEqual (AnchorStyles.Top | AnchorStyles.Left, c.Anchor, "2");
1289
1290                         c.Dock = DockStyle.Top;
1291                         Assert.AreEqual (DockStyle.Top, c.Dock, "3");
1292                         Assert.AreEqual (AnchorStyles.Top | AnchorStyles.Left, c.Anchor, "4");
1293
1294                         c.Anchor = AnchorStyles.Top;
1295                         Assert.AreEqual (DockStyle.None, c.Dock, "5");
1296                         Assert.AreEqual (AnchorStyles.Top, c.Anchor, "6");
1297                 }
1298
1299                 [Test]
1300                 [Category ("NotWorking")]
1301                 public void TabOrder()
1302                 {
1303                         Form            form;
1304                         //Control               active;
1305
1306                         Label           label1 = new Label();           // To test non-tabstop items as well
1307                         Label           label2 = new Label();
1308
1309                         GroupBox        group1 = new GroupBox();
1310                         GroupBox        group2 = new GroupBox();
1311                         GroupBox        group3 = new GroupBox();
1312
1313                         TextBox         text1 = new TextBox();
1314
1315                         RadioButton     radio11 = new RadioButton();
1316                         RadioButton     radio12 = new RadioButton();
1317                         RadioButton     radio13 = new RadioButton();
1318                         RadioButton     radio14 = new RadioButton();
1319                         RadioButton     radio21 = new RadioButton();
1320                         RadioButton     radio22 = new RadioButton();
1321                         RadioButton     radio23 = new RadioButton();
1322                         RadioButton     radio24 = new RadioButton();
1323                         RadioButton     radio31 = new RadioButton();
1324                         RadioButton     radio32 = new RadioButton();
1325                         RadioButton     radio33 = new RadioButton();
1326                         RadioButton     radio34 = new RadioButton();
1327
1328                         form = new Form();
1329                         form.ShowInTaskbar = false;
1330
1331                         form.ClientSize = new Size (520, 520);
1332                         Assert.AreEqual(new Size(520, 520), form.ClientSize, "Tab1");
1333
1334                         form.Text = "SWF Taborder Test App Form";
1335                         Assert.AreEqual("SWF Taborder Test App Form", form.Text, "Tab2");
1336
1337                         label1.Location = new Point(10, 10);
1338                         Assert.AreEqual(new Point(10, 10), label1.Location, "Tab3");
1339                         label1.Text = "Label1";
1340                         form.Controls.Add(label1);
1341
1342                         label2.Location = new Point(200, 10);
1343                         label2.Text = "Label2";
1344                         form.Controls.Add(label2);
1345
1346                         group1.Text = "Group1";
1347                         group2.Text = "Group2";
1348                         group3.Text = "Group3";
1349
1350                         group1.Size = new Size(200, 400);
1351                         group2.Size = new Size(200, 400);
1352                         group3.Size = new Size(180, 180);
1353                         Assert.AreEqual(new Size(180, 180), group3.Size, "Tab4");
1354
1355                         group1.Location = new Point(10, 40);
1356                         group2.Location = new Point(220, 40);
1357                         group3.Location = new Point(10, 210);
1358
1359                         group1.TabIndex = 30;
1360                         Assert.AreEqual(30, group1.TabIndex, "Tab5");
1361                         group1.TabStop = true;
1362
1363                         // Don't assign, test automatic assignment
1364                         //group2.TabIndex = 0;
1365                         group2.TabStop = true;
1366                         Assert.AreEqual(0, group2.TabIndex, "Tab6");
1367
1368                         group3.TabIndex = 35;
1369                         group3.TabStop = true;
1370
1371                         // Test default tab index
1372                         Assert.AreEqual(0, radio11.TabIndex, "Tab7");
1373
1374                         text1.Text = "Edit Control";
1375
1376                         radio11.Text = "Radio 1-1 [Tab1]";
1377                         radio12.Text = "Radio 1-2 [Tab2]";
1378                         radio13.Text = "Radio 1-3 [Tab3]";
1379                         radio14.Text = "Radio 1-4 [Tab4]";
1380
1381                         radio21.Text = "Radio 2-1 [Tab4]";
1382                         radio22.Text = "Radio 2-2 [Tab3]";
1383                         radio23.Text = "Radio 2-3 [Tab2]";
1384                         radio24.Text = "Radio 2-4 [Tab1]";
1385
1386                         radio31.Text = "Radio 3-1 [Tab1]";
1387                         radio32.Text = "Radio 3-2 [Tab3]";
1388                         radio33.Text = "Radio 3-3 [Tab2]";
1389                         radio34.Text = "Radio 3-4 [Tab4]";
1390
1391                         // We don't assign TabIndex for radio1X; test automatic assignment
1392                         text1.TabStop = true;
1393                         radio11.TabStop = true;
1394
1395                         radio21.TabIndex = 4;
1396                         radio22.TabIndex = 3;
1397                         radio23.TabIndex = 2;
1398                         radio24.TabIndex = 1;
1399                         radio24.TabStop = true;
1400
1401                         radio31.TabIndex = 11;
1402                         radio31.TabStop = true;
1403                         radio32.TabIndex = 13;
1404                         radio33.TabIndex = 12;
1405                         radio34.TabIndex = 14;
1406
1407                         text1.Location = new Point(10, 100);
1408
1409                         radio11.Location = new Point(10, 20);
1410                         radio12.Location = new Point(10, 40);
1411                         radio13.Location = new Point(10, 60);
1412                         radio14.Location = new Point(10, 80);
1413
1414                         radio21.Location = new Point(10, 20);
1415                         radio22.Location = new Point(10, 40);
1416                         radio23.Location = new Point(10, 60);
1417                         radio24.Location = new Point(10, 80);
1418
1419                         radio31.Location = new Point(10, 20);
1420                         radio32.Location = new Point(10, 40);
1421                         radio33.Location = new Point(10, 60);
1422                         radio34.Location = new Point(10, 80);
1423
1424                         text1.Size = new Size(150, text1.PreferredHeight);
1425
1426                         radio11.Size = new Size(150, 20);
1427                         radio12.Size = new Size(150, 20);
1428                         radio13.Size = new Size(150, 20);
1429                         radio14.Size = new Size(150, 20);
1430
1431                         radio21.Size = new Size(150, 20);
1432                         radio22.Size = new Size(150, 20);
1433                         radio23.Size = new Size(150, 20);
1434                         radio24.Size = new Size(150, 20);
1435
1436                         radio31.Size = new Size(150, 20);
1437                         radio32.Size = new Size(150, 20);
1438                         radio33.Size = new Size(150, 20);
1439                         radio34.Size = new Size(150, 20);
1440
1441                         group1.Controls.Add(text1);
1442
1443                         group1.Controls.Add(radio11);
1444                         group1.Controls.Add(radio12);
1445                         group1.Controls.Add(radio13);
1446                         group1.Controls.Add(radio14);
1447
1448                         group2.Controls.Add(radio21);
1449                         group2.Controls.Add(radio22);
1450                         group2.Controls.Add(radio23);
1451                         group2.Controls.Add(radio24);
1452
1453                         group3.Controls.Add(radio31);
1454                         group3.Controls.Add(radio32);
1455                         group3.Controls.Add(radio33);
1456                         group3.Controls.Add(radio34);
1457
1458                         form.Controls.Add(group1);
1459                         form.Controls.Add(group2);
1460                         group2.Controls.Add(group3);
1461
1462                         // Perform some tests, the TabIndex stuff below will alter the outcome
1463                         Assert.AreEqual(null, Helper.TestControl(group2, radio34, true), "Tab8");
1464                         Assert.AreEqual(31, group2.TabIndex, "Tab9");
1465
1466                         // Does the taborder of containers and non-selectable things change behaviour?
1467                         label1.TabIndex = 5;
1468                         label2.TabIndex = 4;
1469                         group1.TabIndex = 3;
1470                         group2.TabIndex = 1;
1471
1472                         // Start verification
1473                         Assert.AreEqual(null, Helper.TestControl(group2, radio34, true), "Tab10");
1474                         Assert.AreEqual(radio24.Text, Helper.TestControl(group2, group2, true), "Tab11");
1475                         Assert.AreEqual(radio31.Text, Helper.TestControl(group2, group3, true), "Tab12");
1476                         Assert.AreEqual(null, Helper.TestControl(group1, radio14, true), "Tab13");
1477                         Assert.AreEqual(radio23.Text, Helper.TestControl(group2, radio24, true), "Tab14");
1478                         Assert.AreEqual(group3.Text, Helper.TestControl(group2, radio21, true), "Tab15");
1479                         Assert.AreEqual(radio13.Text, Helper.TestControl(form, radio12, true), "Tab16");
1480                         Assert.AreEqual(label2.Text, Helper.TestControl(form, radio14, true), "Tab17");
1481                         Assert.AreEqual(group1.Text, Helper.TestControl(form, radio34, true), "Tab18");
1482                         Assert.AreEqual(radio23.Text, Helper.TestControl(group2, radio24, true), "Tab19");
1483
1484                         // Sanity checks
1485                         Assert.AreEqual(null, Helper.TestControl(radio11, radio21, true), "Tab20");
1486                         Assert.AreEqual(text1.Text, Helper.TestControl(group1, radio21, true), "Tab21");
1487
1488                         Assert.AreEqual(radio14.Text, Helper.TestControl(form, label2, false), "Tab22");
1489                         Assert.AreEqual(radio21.Text, Helper.TestControl(group2, group3, false), "Tab23");
1490
1491                         Assert.AreEqual(4, radio21.TabIndex, "Tab24");
1492                         Assert.AreEqual(1, radio11.TabIndex, "Tab25");
1493                         Assert.AreEqual(3, radio13.TabIndex, "Tab26");
1494                         Assert.AreEqual(35, group3.TabIndex, "Tab27");
1495                         Assert.AreEqual(1, group2.TabIndex, "Tab28");
1496
1497                         Assert.AreEqual(label1.Text, Helper.TestControl(form, form, false), "Tab29");
1498                         Assert.AreEqual(radio14.Text, Helper.TestControl(group1, group1, false), "Tab30");
1499                         Assert.AreEqual(radio34.Text, Helper.TestControl(group3, group3, false), "Tab31");
1500
1501                         Assert.AreEqual(null, Helper.TestControl(label1, label1, false), "Tab31");
1502                         Assert.AreEqual(null, Helper.TestControl(radio11, radio21, false), "Tab32");
1503                         form.Dispose ();
1504                 }
1505
1506                 [Test]
1507                 public void ScaleTest()
1508                 {
1509                         Control r1 = new Control();
1510
1511                         r1.Width = 40;
1512                         r1.Height = 20;
1513                         r1.Scale(2);
1514                         Assert.AreEqual(80, r1.Width, "Scale1");
1515                         Assert.AreEqual(40, r1.Height, "Scale2");
1516                 }
1517
1518 #if NET_2_0
1519                 [Test]
1520                 public void ScaleChildrenTest ()
1521                 {
1522                         ScaleChildrenControl c = new ScaleChildrenControl ();
1523                         Assert.AreEqual (true, c.PublicScaleChildren, "A1");
1524                 }
1525                 
1526                 private class ScaleChildrenControl : Control
1527                 {
1528                         public bool PublicScaleChildren {
1529                                 get { return base.ScaleChildren; }
1530                         }
1531                 }
1532                 
1533                 [Test]
1534                 public void ScaleControlTest ()
1535                 {
1536                         ScaleControl c = new ScaleControl ();
1537                         
1538                         c.Location = new Point (5, 10);
1539                         c.Size = new Size (15, 20);
1540                         
1541                         Assert.AreEqual (new Rectangle (5, 10, 15, 20), c.Bounds, "A1");
1542
1543                         c.PublicScaleControl (new SizeF (1.5f, 1.3f), BoundsSpecified.All);
1544                         Assert.AreEqual (new Rectangle (8, 13, 22, 26), c.Bounds, "A2");
1545
1546                         c.PublicScaleControl (new SizeF (2f, 1.5f), BoundsSpecified.Location);
1547                         Assert.AreEqual (new Rectangle (16, 20, 22, 26), c.Bounds, "A3");
1548
1549                         c.PublicScaleControl (new SizeF (1.5f, 2f), BoundsSpecified.Size);
1550                         Assert.AreEqual (new Rectangle (16, 20, 33, 52), c.Bounds, "A4");
1551
1552                         c.PublicScaleControl (new SizeF (1.5f, 1.5f), BoundsSpecified.Width);
1553                         Assert.AreEqual (new Rectangle (16, 20, 50, 52), c.Bounds, "A5");
1554
1555                         c.PublicScaleControl (new SizeF (1.5f, 1.3f), BoundsSpecified.None);
1556                         Assert.AreEqual (new Rectangle (16, 20, 50, 52), c.Bounds, "A6");
1557                         
1558                         // Test with ScaleChildren
1559                         c = new ScaleControl ();
1560
1561                         c.Location = new Point (5, 10);
1562                         c.Size = new Size (50, 50);
1563                         
1564                         Control c2 = new Control ();
1565                         c2.Location = new Point (15, 15);
1566                         c2.Size = new Size (25, 25);
1567                         c.Controls.Add (c2);
1568
1569                         Assert.AreEqual (new Rectangle (5, 10, 50, 50), c.Bounds, "B1");
1570                         Assert.AreEqual (new Rectangle (15, 15, 25, 25), c2.Bounds, "B2");
1571
1572                         c.scale_children = false;
1573
1574                         c.PublicScaleControl (new SizeF (2f, 2f), BoundsSpecified.All);
1575                         Assert.AreEqual (new Rectangle (10, 20, 100, 100), c.Bounds, "B3");
1576                         Assert.AreEqual (new Rectangle (15, 15, 25, 25), c2.Bounds, "B4");
1577
1578                         c.scale_children = true;
1579
1580                         // Will not scale children in ScaleControl
1581                         c.PublicScaleControl (new SizeF (2f, 2f), BoundsSpecified.All);
1582                         Assert.AreEqual (new Rectangle (20, 40, 200, 200), c.Bounds, "B5");
1583                         Assert.AreEqual (new Rectangle (15, 15, 25, 25), c2.Bounds, "B6");
1584                         
1585                         // Does scale children in Scale
1586                         c.Scale (new SizeF (2f, 2f));
1587                         Assert.AreEqual (new Rectangle (40, 80, 400, 400), c.Bounds, "B7");
1588                         Assert.AreEqual (new Rectangle (30, 30, 50, 50), c2.Bounds, "B8");
1589                 }
1590                 
1591                 [Test]
1592                 public void GetScaledBoundsTest ()
1593                 {
1594                         ScaleControl c = new ScaleControl ();
1595                         
1596                         Rectangle r = new Rectangle (10, 20, 30, 40);
1597
1598                         Assert.AreEqual (new Rectangle (20, 10, 60, 20), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.All), "A1");
1599                         Assert.AreEqual (new Rectangle (20, 10, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Location), "A2");
1600                         Assert.AreEqual (new Rectangle (10, 20, 60, 20), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Size), "A3");
1601                         Assert.AreEqual (new Rectangle (10, 20, 30, 20), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Height), "A4");
1602                         Assert.AreEqual (new Rectangle (20, 20, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.X), "A5");
1603                         Assert.AreEqual (new Rectangle (10, 20, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.None), "A6");
1604                         
1605                         c.PublicSetTopLevel (true);
1606
1607                         Assert.AreEqual (new Rectangle (10, 20, 60, 20), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.All), "A7");
1608                         Assert.AreEqual (new Rectangle (10, 20, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Location), "A8");
1609                         Assert.AreEqual (new Rectangle (10, 20, 60, 20), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Size), "A9");
1610                         Assert.AreEqual (new Rectangle (10, 20, 30, 20), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Height), "A10");
1611                         Assert.AreEqual (new Rectangle (10, 20, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.X), "A11");
1612                         Assert.AreEqual (new Rectangle (10, 20, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.None), "A12");
1613
1614                         c = new ScaleControl ();
1615                         c.PublicSetStyle (ControlStyles.FixedHeight, true);
1616                         c.PublicSetStyle (ControlStyles.FixedWidth, true);
1617
1618                         Assert.AreEqual (new Rectangle (20, 10, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.All), "A13");
1619                         Assert.AreEqual (new Rectangle (20, 10, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Location), "A14");
1620                         Assert.AreEqual (new Rectangle (10, 20, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Size), "A15");
1621                         Assert.AreEqual (new Rectangle (10, 20, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Height), "A16");
1622                         Assert.AreEqual (new Rectangle (20, 20, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.X), "A17");
1623                         Assert.AreEqual (new Rectangle (10, 20, 30, 40), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.None), "A18");
1624                 }
1625                 
1626                 private class ScaleControl : Control
1627                 {
1628                         public bool scale_children = true;
1629                         
1630                         public void PublicScaleControl (SizeF factor, BoundsSpecified specified)
1631                         {
1632                                 base.ScaleControl (factor, specified);
1633                         }
1634
1635                         public Rectangle PublicGetScaledBounds (Rectangle bounds, SizeF factor, BoundsSpecified specified)
1636                         {
1637                                 return base.GetScaledBounds (bounds, factor, specified);
1638                         }
1639                         
1640                         public void PublicSetStyle (ControlStyles flag, bool value)
1641                         {
1642                                 base.SetStyle (flag, value);
1643                         }
1644                         
1645                         public void PublicSetTopLevel (bool value)
1646                         {
1647                                 base.SetTopLevel (value);
1648                         }
1649                         
1650                         protected override bool ScaleChildren {
1651                                 get { return scale_children; }
1652                         }
1653                 }
1654 #endif
1655
1656                 [Test]  // Bug #347282
1657                 public void ScaleHierarchy ()
1658                 {
1659                         Form f = new Form ();
1660                         Panel p = new Panel ();
1661                         Button b = new Button ();
1662                         
1663                         f.ClientSize = new Size (300, 300);
1664
1665                         f.Controls.Add (p);
1666                         p.Controls.Add (b);
1667                         
1668                         f.AutoScaleBaseSize = new Size (3, 11);
1669                         f.Show ();
1670                         
1671                         // Due to font differences, all we can guarantee is that
1672                         // the button is larger that the default.
1673                         Assert.IsTrue (b.Width > 75, "A1");
1674                         Assert.IsTrue (b.Height > 23, "A2");
1675                         
1676                         f.Dispose ();
1677                 }
1678                 
1679                 class TestWindowTarget : IWindowTarget
1680                 {
1681                         public void OnHandleChange (IntPtr newHandle) {
1682                         }
1683
1684                         public void OnMessage (ref Message m) {
1685                         }
1686                 }
1687
1688                 [Test]
1689                 public void WindowTargetTest()
1690                 {
1691                         Control c = new Control ();
1692                         Assert.IsNotNull (c.WindowTarget, "WindowTarget1");
1693                         c.WindowTarget = null;
1694                         Assert.IsNull (c.WindowTarget, "WindowTarget2");
1695
1696                         IWindowTarget existing_target = c.WindowTarget;
1697                         IWindowTarget new_target = new TestWindowTarget ();
1698                         c.WindowTarget = new_target;
1699                         Assert.AreSame (new_target, c.WindowTarget, "WindowTarget3");
1700                         
1701                         TestHelper.RemoveWarning (existing_target);
1702                 }
1703
1704                 [Test]
1705                 public void TextTest()
1706                 {
1707                         Control r1 = new Control();
1708                         r1.Text = "Hi" ;
1709                         Assert.AreEqual("Hi" , r1.Text , "Text1");
1710
1711                         r1.ResetText();
1712                         Assert.AreEqual("" , r1.Text , "Text2");
1713                 }
1714
1715                 [Test]
1716                 public void PubMethodTest7()
1717                 {
1718                         Control r1 = new Control();
1719                         r1.RightToLeft = RightToLeft.Yes ;
1720                         r1.ResetRightToLeft() ;
1721                         Assert.AreEqual(RightToLeft.No , r1.RightToLeft , "#81");
1722                         r1.ImeMode = ImeMode.Off ;
1723                         r1.ResetImeMode () ;
1724                         Assert.AreEqual(ImeMode.NoControl , r1.ImeMode , "#82");
1725                         r1.ForeColor= SystemColors.GrayText ;
1726                         r1.ResetForeColor() ;
1727                         Assert.AreEqual(SystemColors.ControlText , r1.ForeColor , "#83");
1728                         //r1.Font = Font.FromHdc();
1729                         r1.ResetFont () ;
1730                         //Assert.AreEqual(FontFamily.GenericSansSerif , r1.Font , "#83");
1731                         r1.Cursor = Cursors.Hand ;
1732                         r1.ResetCursor () ;
1733                         Assert.AreEqual(Cursors.Default , r1.Cursor , "#83");
1734                         //r1.DataBindings = System.Windows.Forms.Binding ;
1735                         //r1.ResetBindings() ;
1736                         //Assert.AreEqual(ControlBindingsCollection , r1.DataBindings  , "#83");
1737                         r1.BackColor = Color.Black ;
1738                         r1.ResetBackColor() ;
1739                         Assert.AreEqual( SystemColors.Control , r1.BackColor  , "#84");
1740                         r1.BackColor = Color.Black ;
1741                         r1.Refresh() ;
1742                         Assert.AreEqual( null , r1.Region , "#85");
1743                         Rectangle M = new Rectangle(10, 20, 30 ,40);
1744                         r1.RectangleToScreen(M) ;
1745                         Assert.AreEqual( null , r1.Region , "#86");
1746                 }
1747
1748                 [Test]
1749                 public void ScreenClientCoords()
1750                 {
1751                         Label l;
1752                         Point p1;
1753                         Point p2;
1754                         Point p3;
1755
1756                         l = new Label();
1757                         l.Left = 10;
1758                         l.Top  = 12;
1759                         l.Visible = true;
1760                         p1 = new Point (10,10);
1761                         p2 = l.PointToScreen(p1);
1762                         p3 = l.PointToClient(p2);
1763
1764                         Assert.AreEqual (p1, p3, "SC1");
1765                 }
1766
1767                 [Test]
1768                 public void ContainsTest ()
1769                 {
1770                         Control t = new Control ();
1771                         Control s = new Control ();
1772
1773                         t.Controls.Add (s);
1774
1775                         Assert.AreEqual (true, t.Contains (s), "Con1");
1776                         Assert.AreEqual (false, s.Contains (t), "Con2");
1777                         Assert.AreEqual (false, s.Contains (null), "Con3");
1778                         Assert.AreEqual (false, t.Contains (new Control ()), "Con4");
1779                 }
1780
1781                 [Test]
1782                 public void IsHandleCreated_NotVisible ()
1783                 {
1784                         Control c = new Control ();
1785                         c.Visible = false;
1786
1787                         Form form = new Form ();
1788                         form.ShowInTaskbar = false;
1789                         form.Controls.Add (c);
1790                         form.Show ();
1791
1792                         Assert.IsFalse (c.IsHandleCreated, "#1");
1793                         c.Visible = true;
1794                         Assert.IsTrue (c.IsHandleCreated, "#2");
1795                         c.Visible = false;
1796                         Assert.IsTrue (c.IsHandleCreated, "#3");
1797                         form.Close ();
1798                 }
1799
1800                 class OnCreateControlTest : Control {
1801                         public bool reached = false;
1802                         protected override void OnCreateControl () {
1803                                 reached = true;
1804                         }
1805                 }
1806
1807                 [Test]
1808                 public void CreateControlVisibleTest ()
1809                 {
1810                         OnCreateControlTest test = new OnCreateControlTest ();
1811                         test.Visible = false;
1812                         Assert.IsFalse (test.IsHandleCreated, "0");
1813                         Assert.IsFalse (test.Visible, "1");
1814                         test.Visible = true;
1815                         Assert.IsTrue (test.Visible, "2");
1816                         Assert.IsTrue (test.reached, "3");
1817                 }
1818
1819
1820                 [Test]
1821                 public void CreateGraphicsTest ()
1822                 {
1823                         Graphics g = null;
1824                         Pen p = null;
1825
1826                         try {
1827                                 Control c = new Control ();
1828                                 c.SetBounds (0,0, 20, 20);
1829                                 g = c.CreateGraphics ();
1830                                 Assert.IsNotNull (g, "Graph1");
1831                         } finally {
1832                                 if (p != null)
1833                                         p.Dispose ();
1834                                 if (g != null)
1835                                         g.Dispose ();
1836                         }
1837                 }
1838
1839                 bool delegateCalled = false;
1840                 public delegate void TestDelegate ();
1841
1842                 public void delegate_call () {
1843                         delegateCalled = true;
1844
1845                         TestHelper.RemoveWarning (delegateCalled);
1846                 }
1847
1848                 [Test]
1849                 [ExpectedException(typeof(InvalidOperationException))]
1850                 public void InvokeException1 () {
1851                         Control c = new Control ();
1852                         IAsyncResult result;
1853
1854                         result = c.BeginInvoke (new TestDelegate (delegate_call));
1855                         c.EndInvoke (result);
1856                 }
1857
1858                 [Test]
1859                 public void FindFormTest () {
1860                         Form f = new Form ();
1861
1862                         f.ShowInTaskbar = false;
1863                         f.Name = "form";
1864                         Control c = null;
1865
1866                         try {
1867                                 f.Controls.Add (c = new Control ());
1868                                 Assert.AreEqual (f.Name, c.FindForm ().Name, "Find1");
1869
1870                                 f.Controls.Remove (c);
1871
1872                                 GroupBox g = new GroupBox ();
1873                                 g.Name = "box";
1874                                 f.Controls.Add (g);
1875                                 g.Controls.Add (c);
1876
1877                                 Assert.AreEqual (f.Name, f.FindForm ().Name, "Find2");
1878
1879                                 g.Controls.Remove (c);
1880                                 Assert.IsNull(c.FindForm (), "Find3");
1881
1882                         } finally {
1883                                 if (c != null)
1884                                         c.Dispose ();
1885                                 if (f != null)
1886                                         f.Dispose ();
1887                         }
1888                 }
1889
1890                 [Test]
1891                 public void FocusTest ()
1892                 {
1893                         Form f = null;
1894                         Button c = null, d = null;
1895
1896                         try {
1897                                 f = new Form ();
1898                                 f.ShowInTaskbar = false;
1899                                 f.Visible = true;
1900                                 c = new Button ();
1901                                 c.Visible = true;
1902                                 f.Controls.Add (c);
1903
1904                                 d = new Button ();
1905                                 d.Visible = false;
1906                                 f.Controls.Add (d);
1907
1908                                 Assert.IsTrue (c.CanFocus, "Focus1");
1909                                 Assert.IsFalse (c.Focused, "Focus2");
1910                                 c.Focus ();
1911                                 Assert.IsTrue (c.Focused, "Focus3");
1912                                 d.Focus ();
1913                                 Assert.IsFalse (d.Focused, "Focus4");
1914
1915                                 d.Visible = true;
1916                                 d.Focus ();
1917                                 Assert.IsTrue (d.Focused, "Focus5");
1918                                 Assert.IsFalse (c.Focused, "Focus6");
1919
1920                                 c.Enabled = false;
1921                                 Assert.IsFalse (c.Focused, "Focus7");
1922                         } finally {
1923                                 if (f != null)
1924                                         f.Dispose ();
1925                                 if (c != null)
1926                                         c.Dispose ();
1927                                 if (d != null)
1928                                         d.Dispose ();
1929                         }
1930                 }
1931
1932                 [Test]
1933                 public void FromHandleTest ()
1934                 {
1935                         Control c1 = null;
1936                         Control c2 = null;
1937
1938                         try {
1939                                 c1 = new Control ();
1940                                 c2 = new Control ();
1941
1942                                 c1.Name = "parent";
1943                                 c2.Name = "child";
1944                                 c1.Controls.Add(c2);
1945
1946                                 // Handle
1947                                 Assert.AreEqual (c1.Name, Control.FromHandle (c1.Handle).Name, "Handle1");
1948                                 Assert.IsNull (Control.FromHandle (IntPtr.Zero), "Handle2");
1949
1950                                 // ChildHandle
1951                                 Assert.AreEqual (c1.Name, Control.FromChildHandle (c1.Handle).Name, "Handle3");
1952                                 Assert.IsNull (Control.FromChildHandle (IntPtr.Zero), "Handle4");
1953
1954
1955                         } finally {
1956                                 if (c1 != null)
1957                                         c1.Dispose ();
1958
1959                                 if (c2 != null)
1960                                         c2.Dispose ();
1961                         }
1962                 }
1963
1964                 [Test]
1965                 public void GetChildAtPointTest ()
1966                 {
1967                         Control c = null, d = null;
1968                         TransparentControl e = null;
1969
1970                         try {
1971                                 c = new Control ();
1972                                 c.Name = "c1";
1973                                 c.SetBounds (0, 0, 100, 100);
1974
1975                                 d = new Control ();
1976                                 d.Name = "d1";
1977                                 d.SetBounds (10, 10, 40, 40);
1978                                 c.Controls.Add (d);
1979
1980                                 e = new TransparentControl ();
1981                                 e.Name = "e1";
1982                                 e.SetBounds (55, 55, 10, 10);
1983
1984                                 Control l = c.GetChildAtPoint (new Point (15, 15));
1985                                 Assert.AreEqual (d.Name, l.Name, "Child1");
1986                                 Assert.IsFalse (e.Name == l.Name, "Child2");
1987
1988                                 l = c.GetChildAtPoint (new Point (57, 57));
1989                                 Assert.AreEqual (null, l, "Child3");
1990
1991                                 l = c.GetChildAtPoint (new Point (10, 10));
1992                                 Assert.AreEqual (d.Name, l.Name, "Child4");
1993
1994                                 // GetChildAtPointSkip is not implemented and the following test is breaking for Net_2_0 profile
1995 #if NET_2_0
1996                                 c.Controls.Add (e);
1997                                 e.Visible = false;
1998                                 l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Invisible);
1999                                 Assert.IsNull (l, "Child5");
2000
2001                                 e.Visible = true;
2002                                 l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Invisible);
2003                                 Assert.AreSame (e.Name, l.Name, "Child6");
2004
2005                                 e.Enabled = false;
2006                                 l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Disabled);
2007                                 Assert.IsNull (l, "Child7");
2008
2009                                 e.Enabled = true;
2010                                 l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Disabled);
2011                                 Assert.AreSame (e.Name, l.Name, "Child8");
2012
2013                                 
2014                                 e.BackColor = Color.Transparent;
2015                                 l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Transparent);
2016                                 Assert.IsNull (l, "Child9");
2017
2018                                 e.BackColor = Color.Green;
2019                                 l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Transparent);
2020                                 Assert.AreSame (e.Name, l.Name, "Child10");
2021
2022 #endif // NET_2_0
2023                         } finally {
2024                                 if (c != null)
2025                                         c.Dispose ();
2026                                 if (d != null)
2027                                         d.Dispose ();
2028                         }
2029                 }
2030
2031                 private class TransparentControl : Control
2032                 {
2033                         public TransparentControl ()
2034                         {
2035                                 SetStyle (ControlStyles.SupportsTransparentBackColor, true);
2036                         }
2037
2038                         protected override CreateParams CreateParams
2039                         {
2040                                 get
2041                                 {
2042                                         CreateParams cp = base.CreateParams;
2043                                         cp.ExStyle |= 0x00000020;
2044                                         return cp;
2045                                 }
2046                         }
2047                 }
2048                 
2049                 [Test]
2050                 public void ResetFontTest ()
2051                 {
2052                         Control c = new Control ();
2053                         c.Font = new Font (c.Font.FontFamily, 3, FontStyle.Italic);
2054                         c.ResetFont ();
2055
2056                         Assert.IsFalse (c.Font.Bold, "#1");
2057                         //Assert.AreEqual ("Microsoft Sans Serif", c.Font.FontFamily.Name, "#2");
2058                         Assert.IsFalse (c.Font.Italic, "#3");
2059                         //Assert.AreEqual ("Microsoft Sans Serif", c.Font.Name, "#4");
2060                         Assert.AreEqual (8.25, c.Font.Size, "#5");
2061                         Assert.AreEqual (8.25, c.Font.SizeInPoints, "#6");
2062                         Assert.IsFalse (c.Font.Strikeout, "#7");
2063                         Assert.IsFalse (c.Font.Underline, "#8");
2064                         Assert.AreEqual (GraphicsUnit.Point, c.Font.Unit, "#9");
2065 #if NET_2_0
2066                         Assert.AreEqual (true, c.Font.IsSystemFont, "#10");
2067 #endif
2068                 }
2069
2070                 public class LayoutTestControl : Control {
2071                         public int LayoutCount;
2072
2073                         public LayoutTestControl () : base() {
2074                                 LayoutCount = 0;
2075                         }
2076
2077                         protected override void OnLayout(LayoutEventArgs levent) {
2078                                 LayoutCount++;
2079                                 base.OnLayout (levent);
2080                         }
2081                 }
2082
2083                 [Test]
2084                 public void LayoutTest() {
2085                         LayoutTestControl c;
2086
2087                         c = new LayoutTestControl();
2088
2089                         c.SuspendLayout();
2090                         c.SuspendLayout();
2091                         c.SuspendLayout();
2092                         c.SuspendLayout();
2093
2094                         c.ResumeLayout(true);
2095                         c.PerformLayout();
2096                         c.ResumeLayout(true);
2097                         c.PerformLayout();
2098                         c.ResumeLayout(true);
2099                         c.PerformLayout();
2100                         c.ResumeLayout(true);
2101                         c.PerformLayout();
2102                         c.ResumeLayout(true);
2103                         c.PerformLayout();
2104                         c.ResumeLayout(true);
2105                         c.PerformLayout();
2106                         c.ResumeLayout(true);
2107                         c.PerformLayout();
2108                         c.SuspendLayout();
2109                         c.PerformLayout();
2110
2111                         Assert.AreEqual(5, c.LayoutCount, "Layout Suspend/Resume locking does not bottom out at 0");
2112                 }
2113
2114                 [Test]
2115                 [ExpectedException(typeof(ArgumentException))]
2116                 public void TransparentBackgroundTest1() {
2117                         Control c;
2118
2119                         c = new Control();
2120                         c.BackColor = Color.Transparent;
2121                 }
2122
2123                 [Test]
2124                 public void TransparentBackgroundTest2() {
2125                         Panel   c;
2126
2127                         c = new Panel();
2128                         c.BackColor = Color.Transparent;
2129                         Assert.AreEqual(Color.Transparent, c.BackColor, "Transparent background not set");
2130                 }
2131
2132                 [Test]
2133                 public void TransparentBackgroundTest3() {
2134                         Control c;
2135
2136                         c = new Control();
2137                         c.BackColor = Color.Empty;
2138                         Assert.AreEqual(Control.DefaultBackColor, c.BackColor, "Setting empty color failed");
2139                 }
2140
2141                 [Test]
2142                 public void Dock_Value_Invalid ()
2143                 {
2144                         Control c = new Control ();
2145                         try {
2146                                 c.Dock = (DockStyle) 666;
2147                                 Assert.Fail ("#1");
2148                         } catch (InvalidEnumArgumentException ex) {
2149                                 Assert.AreEqual (typeof (InvalidEnumArgumentException), ex.GetType (), "#2");
2150                                 Assert.IsNotNull (ex.Message, "#3");
2151                                 Assert.IsNotNull (ex.ParamName, "#4");
2152                                 Assert.AreEqual ("value", ex.ParamName, "#5");
2153                                 Assert.IsNull (ex.InnerException, "#6");
2154                         }
2155                 }
2156
2157                 [Test]
2158                 public void EnabledTest1() {
2159                         Control child;
2160                         Control parent;
2161                         Control grandma;
2162
2163                         grandma = new Control();
2164                         parent = new Control();
2165                         child = new Control();
2166
2167                         grandma.Controls.Add(parent);
2168                         parent.Controls.Add(child);
2169                         grandma.Enabled = false;
2170                         Assert.AreEqual(grandma.Enabled, child.Enabled, "Child did not inherit disabled state");
2171                 }
2172
2173                 int EnabledCalledCount = 0;
2174                 private void EnabledTest2EnabledChanged(object sender, EventArgs e) {
2175                         EnabledCalledCount++;
2176                 }
2177
2178                 [Test]
2179                 public void EnabledTest2() {
2180                         // Check nesting of enabled calls
2181                         // OnEnabled is not called for disabled child controls
2182                         Control child;
2183                         Control parent;
2184                         Control grandma;
2185
2186                         EnabledCalledCount = 0;
2187
2188                         grandma = new Control();
2189                         parent = new Control();
2190                         child = new Control();
2191                         child.EnabledChanged += new EventHandler(EnabledTest2EnabledChanged);
2192
2193                         grandma.Controls.Add(parent);
2194                         parent.Controls.Add(child);
2195                         grandma.Enabled = false;
2196
2197                         Assert.AreEqual(1, EnabledCalledCount, "Child Enabled Event not properly fired");
2198                         grandma.Enabled = true;
2199                         Assert.AreEqual(2, EnabledCalledCount, "Child Enabled Event not properly fired");
2200                         child.Enabled = false;
2201                         grandma.Enabled = false;
2202                         Assert.AreEqual(3, EnabledCalledCount, "Child Enabled Event not properly fired");
2203                 }
2204
2205                 [Test]
2206                 public void ControlsRemoveNullTest ()
2207                 {
2208                         Control c = new Control ();
2209                         c.Controls.Remove (null);
2210                 }
2211
2212                 [Test]
2213                 public void ControlsAddNullTest ()
2214                 {
2215                         Control c = new Control ();
2216                         c.Controls.Add (null);
2217                 }
2218
2219                 [Test]
2220                 [ExpectedException (typeof (ArgumentNullException))]
2221                 public void ControlsSetChildIndexNullTest ()
2222                 {
2223                         Control c = new Control ();
2224                         c.Controls.SetChildIndex (null, 1);
2225                 }
2226
2227                 [Test]
2228                 [ExpectedException (typeof (ArgumentNullException))]
2229                 public void ControlsAddRangeNullTest ()
2230                 {
2231                         Control c = new Control ();
2232                         c.Controls.AddRange (null);
2233                 }
2234
2235                 [Test]
2236                 public void ControlsAddRangeNullElementTest ()
2237                 {
2238                         Control c = new Control ();
2239                         Control[] subcontrols = new Control[2];
2240                         subcontrols[0] = new Control ();
2241                         subcontrols[1] = null;
2242
2243                         c.Controls.AddRange (subcontrols);
2244                 }
2245
2246                 [Test]
2247                 public void RegionTest () {
2248                         Form f = new Form ();
2249                         f.ShowInTaskbar = false;
2250                         Control c = new Control ();
2251                         f.Controls.Add (c);
2252                         Assert.IsNull (c.Region, "#A1");
2253                         f.Show ();
2254                         Assert.IsNull (c.Region, "#A2");
2255                         c.Region = null;
2256                         Assert.IsNull (c.Region, "#A3");
2257                         f.Dispose ();
2258
2259                         Region region = new Region ();
2260                         f = new Form ();
2261                         f.ShowInTaskbar = false;
2262                         c = new Control ();
2263                         f.Controls.Add (c);
2264                         c.Region = region;
2265                         Assert.IsNotNull (c.Region, "#B1");
2266                         Assert.AreSame (region, c.Region, "#B2");
2267                         f.Show ();
2268                         c.Region = null;
2269                         Assert.IsNull (c.Region, "#B3");
2270
2271                         f.Dispose ();
2272                 }
2273
2274                 [Test] // bug #330501
2275                 public void OnValidating_Parent_Close ()
2276                 {
2277                         MockControl control = new MockControl ();
2278
2279                         Form f = new Form ();
2280                         f.Controls.Add (control);
2281                         f.ShowInTaskbar = false;
2282
2283                         f.Show ();
2284                         Assert.AreEqual (0, control.OnValidatingCount, "#A1");
2285                         f.Close ();
2286                         Assert.AreEqual (1, control.OnValidatingCount, "#A2");
2287                         f.Dispose ();
2288                 }
2289
2290                 [Test] // bug #80280
2291                 public void Validated_Multiple_Containers ()
2292                 {
2293                         Form form = new Form ();
2294                         form.ShowInTaskbar = false;
2295
2296                         UserControl control1 = new UserControl();
2297                         UserControl container1 = new UserControl();
2298                         control1.Tag = true;
2299                         control1.Validated += new EventHandler (Control_ValidatedHandler);
2300                         container1.Controls.Add(control1);
2301                         form.Controls.Add (container1);
2302
2303                         UserControl container2 = new UserControl();
2304                         UserControl control2 = new UserControl();
2305                         container2.Controls.Add(control2);
2306                         form.Controls.Add (container2);
2307
2308                         Assert.IsTrue ((bool) control1.Tag, "#1");
2309                         control1.Select();
2310                         Assert.IsTrue ((bool) control1.Tag, "#2");
2311                         control2.Select();
2312                         Assert.IsFalse ((bool) control1.Tag, "#3");
2313
2314                         form.Dispose ();
2315                 }
2316
2317                 private void Control_ValidatedHandler (object sender, EventArgs e)
2318                 {
2319                         ((Control) sender).Tag = false;
2320                 }
2321                 
2322                 [Test]
2323                 public void ControlReparentLocationTest ()
2324                 {
2325                         Form form = new Form ();
2326                         Label l = new Label ();
2327                         l.Location = new Point (0, 0);
2328                         form.Controls.Add (l);
2329                         form.Show ();
2330                         Assert.AreEqual (0, l.Left, "#A1");
2331                         Assert.AreEqual (0, l.Top, "#A2");
2332                         form.Hide ();
2333                         form.Controls.Remove (l);
2334                         form.Show ();
2335                         form.Controls.Add (l);
2336                         Assert.AreEqual (0, l.Left, "#A3");
2337                         Assert.AreEqual (0, l.Top, "#A4");
2338                         
2339                         form.Dispose ();
2340                 }
2341
2342 #if NET_2_0
2343                 [Test]
2344                 public void UseWaitCursorTest ()
2345                 {
2346                         Control c = new Control ();
2347                         Assert.IsFalse (c.UseWaitCursor, "#1");
2348                         c.Cursor = Cursors.Hand;
2349                         c.UseWaitCursor = true;
2350                         Assert.AreEqual (Cursors.WaitCursor, c.Cursor, "#2");
2351                         c.UseWaitCursor = false;
2352                         Assert.AreEqual (Cursors.Hand, c.Cursor, "#3");
2353                         
2354                         c.UseWaitCursor = true;
2355                         c.Cursor = Cursors.Help;
2356                         Assert.AreEqual (Cursors.WaitCursor, c.Cursor, "#4");
2357                         Assert.AreEqual (true, c.UseWaitCursor, "#5");
2358                         
2359                         c.UseWaitCursor = false;
2360                         Assert.AreEqual (Cursors.Help, c.Cursor, "#6");
2361                 }
2362
2363                 [Test] // bug #80621, #81125
2364                 public void DontCallSizeFromClientSize ()
2365                 {
2366                         SizeControl sc = new SizeControl ();
2367                         
2368                         Assert.AreEqual (0, sc.size_from_client_size_count, "A1");
2369                         
2370                         sc.ClientSize = new Size (300, 300);
2371                         Assert.AreEqual (0, sc.size_from_client_size_count, "A2");
2372                         
2373                         SizeForm sf = new SizeForm ();
2374                         sf.ShowInTaskbar = false;
2375                         sf.Show ();
2376                         
2377                         Assert.AreEqual (0, sc.size_from_client_size_count, "A3");
2378
2379                         sc.ClientSize = new Size (300, 300);
2380                         Assert.AreEqual (0, sc.size_from_client_size_count, "A4");      
2381                         
2382                         sf.Dispose ();  
2383                 }
2384                 
2385                 private class SizeControl : Control
2386                 {
2387                         public int size_from_client_size_count = 0;
2388                         
2389                         protected override Size SizeFromClientSize (Size clientSize)
2390                         {
2391                                 size_from_client_size_count++;
2392                                 return base.SizeFromClientSize (clientSize);
2393                         }
2394                 }
2395
2396                 private class SizeForm : Form
2397                 {
2398                         public int size_from_client_size_count = 0;
2399
2400                         protected override Size SizeFromClientSize (Size clientSize)
2401                         {
2402                                 size_from_client_size_count++;
2403                                 return base.SizeFromClientSize (clientSize);
2404                         }
2405                 }
2406 #endif
2407
2408                 public class MockControl : Control
2409                 {
2410                         public int OnValidatingCount;
2411                         public int OnSizeChangedCount;
2412                         public int OnResizeCount;
2413                         public int OnLocationChangedCount;
2414
2415                         public MockControl ()
2416                         {
2417                         }
2418
2419                         public MockControl (string text)
2420                                 : base (text)
2421                         {
2422                         }
2423
2424                         public MockControl (Control parent, string text)
2425                                 : base (parent, text)
2426                         {
2427                         }
2428
2429                         public MockControl (string text, int left, int top, int width, int height)
2430                                 : base (text, left, top, width, height)
2431                         {
2432                         }
2433
2434                         public MockControl (Control parent, string text, int left, int top, int width, int height)
2435                                 : base (parent, text, left, top, width, height)
2436                         {
2437                         }
2438
2439                         public int font_height
2440                         {
2441                                 get { return base.FontHeight; }
2442                                 set { base.FontHeight = value; }
2443                         }
2444
2445                         protected override void OnLocationChanged (EventArgs e)
2446                         {
2447                                 OnLocationChangedCount++;
2448                                 base.OnLocationChanged (e);
2449                         }
2450
2451                         protected override void OnSizeChanged (EventArgs e)
2452                         {
2453                                 OnSizeChangedCount++;
2454                                 base.OnSizeChanged (e);
2455                         }
2456
2457                         protected override void OnResize (EventArgs e)
2458                         {
2459                                 OnResizeCount++;
2460                                 base.OnResize (e);
2461                         }
2462
2463                         protected override void OnValidating (CancelEventArgs e)
2464                         {
2465                                 OnValidatingCount++;
2466                         }
2467                 }
2468
2469                 const int WM_KEYDOWN = 0x0100;
2470                 const int WM_CHAR = 0x0102;
2471                 const int WM_SYSCHAR = 0x0106;
2472                 const int WM_KEYUP = 0x0101;
2473
2474 #if NET_2_0
2475                 [Test]
2476                 public void MethodPreProcessControlMessage ()
2477                 {
2478                         bool testing_callstack = false;
2479
2480                         MyControl c = new MyControl ();
2481                         Message m = new Message ();
2482                         m.HWnd = c.Handle;
2483                         m.Msg = WM_KEYDOWN;
2484                         m.WParam = (IntPtr)Keys.Down;
2485                         m.LParam = IntPtr.Zero;
2486
2487                         if (testing_callstack) Console.WriteLine ("Start");
2488                         Assert.AreEqual (PreProcessControlState.MessageNotNeeded, c.PreProcessControlMessage (ref m), "A1");
2489                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2490
2491                         c.SetState (State.OnPreviewKeyDown);
2492                         if (testing_callstack) Console.WriteLine ("Start");
2493                         Assert.AreEqual (PreProcessControlState.MessageNeeded, c.PreProcessControlMessage (ref m), "A2");
2494                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2495
2496                         c.SetState (State.ProcessCmdKey);
2497                         if (testing_callstack) Console.WriteLine ("Start");
2498                         Assert.AreEqual (PreProcessControlState.MessageProcessed, c.PreProcessControlMessage (ref m), "A3");
2499                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2500
2501                         c.SetState (State.IsInputKey);
2502                         if (testing_callstack) Console.WriteLine ("Start");
2503                         Assert.AreEqual (PreProcessControlState.MessageNeeded, c.PreProcessControlMessage (ref m), "A4");
2504                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2505
2506                         c.SetState (State.ProcessDialogKey);
2507                         if (testing_callstack) Console.WriteLine ("Start");
2508                         Assert.AreEqual (PreProcessControlState.MessageProcessed, c.PreProcessControlMessage (ref m), "A5");
2509                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2510
2511
2512                         m.Msg = WM_CHAR;
2513                         c.SetState (State.None);
2514                         if (testing_callstack) Console.WriteLine ("Start");
2515                         Assert.AreEqual (PreProcessControlState.MessageNotNeeded, c.PreProcessControlMessage (ref m), "A6");
2516                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2517
2518                         c.SetState (State.IsInputChar);
2519                         if (testing_callstack) Console.WriteLine ("Start");
2520                         Assert.AreEqual (PreProcessControlState.MessageNeeded, c.PreProcessControlMessage (ref m), "A7");
2521                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2522
2523                         c.SetState (State.ProcessDialogChar);
2524                         if (testing_callstack) Console.WriteLine ("Start");
2525                         Assert.AreEqual (PreProcessControlState.MessageProcessed, c.PreProcessControlMessage (ref m), "A8");
2526                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2527
2528
2529                         m.Msg = WM_SYSCHAR;
2530                         c.SetState (State.None);
2531                         if (testing_callstack) Console.WriteLine ("Start");
2532                         Assert.AreEqual (PreProcessControlState.MessageNotNeeded, c.PreProcessControlMessage (ref m), "A9");
2533                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2534
2535                         c.SetState (State.IsInputChar);
2536                         if (testing_callstack) Console.WriteLine ("Start");
2537                         Assert.AreEqual (PreProcessControlState.MessageNeeded, c.PreProcessControlMessage (ref m), "A10");
2538                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2539
2540                         c.SetState (State.ProcessDialogChar);
2541                         if (testing_callstack) Console.WriteLine ("Start");
2542                         Assert.AreEqual (PreProcessControlState.MessageProcessed, c.PreProcessControlMessage (ref m), "A11");
2543                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2544
2545
2546                         m.Msg = WM_KEYUP;
2547                         if (testing_callstack) Console.WriteLine ("Start");
2548                         Assert.AreEqual (PreProcessControlState.MessageNotNeeded, c.PreProcessControlMessage (ref m), "A12");
2549                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2550
2551                         c.SetState (State.OnPreviewKeyDown);
2552                         if (testing_callstack) Console.WriteLine ("Start");
2553                         Assert.AreEqual (PreProcessControlState.MessageNotNeeded, c.PreProcessControlMessage (ref m), "A13");
2554                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2555
2556                         c.SetState (State.ProcessCmdKey);
2557                         if (testing_callstack) Console.WriteLine ("Start");
2558                         Assert.AreEqual (PreProcessControlState.MessageNotNeeded, c.PreProcessControlMessage (ref m), "A14");
2559                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2560
2561                         c.SetState (State.IsInputKey);
2562                         if (testing_callstack) Console.WriteLine ("Start");
2563                         Assert.AreEqual (PreProcessControlState.MessageNotNeeded, c.PreProcessControlMessage (ref m), "A15");
2564                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2565
2566                         c.SetState (State.ProcessDialogKey);
2567                         if (testing_callstack) Console.WriteLine ("Start");
2568                         Assert.AreEqual (PreProcessControlState.MessageNotNeeded, c.PreProcessControlMessage (ref m), "A16");
2569                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2570                 }
2571 #endif
2572
2573                 [Test]
2574                 public void MethodPreProcessMessage ()
2575                 {
2576                         bool testing_callstack = false;
2577
2578                         MyControl c = new MyControl ();
2579                         Message m = new Message ();
2580                         m.HWnd = c.Handle;
2581                         m.Msg = WM_KEYDOWN;
2582                         m.WParam = (IntPtr)Keys.Down;
2583                         m.LParam = IntPtr.Zero;
2584
2585                         if (testing_callstack) Console.WriteLine ("Start");
2586                         Assert.AreEqual (false, c.PreProcessMessage (ref m), "A1");
2587                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2588
2589                         c.SetState (State.OnPreviewKeyDown);
2590                         if (testing_callstack) Console.WriteLine ("Start");
2591                         Assert.AreEqual (false, c.PreProcessMessage (ref m), "A2");
2592                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2593
2594                         c.SetState (State.ProcessCmdKey);
2595                         if (testing_callstack) Console.WriteLine ("Start");
2596                         Assert.AreEqual (true, c.PreProcessMessage (ref m), "A3");
2597                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2598
2599                         c.SetState (State.IsInputKey);
2600                         if (testing_callstack) Console.WriteLine ("Start");
2601                         Assert.AreEqual (false, c.PreProcessMessage (ref m), "A4");
2602                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2603
2604                         c.SetState (State.ProcessDialogKey);
2605                         if (testing_callstack) Console.WriteLine ("Start");
2606                         Assert.AreEqual (true, c.PreProcessMessage (ref m), "A5");
2607                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2608
2609
2610                         m.Msg = WM_CHAR;
2611                         c.SetState (State.None);
2612                         if (testing_callstack) Console.WriteLine ("Start");
2613                         Assert.AreEqual (false, c.PreProcessMessage (ref m), "A6");
2614                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2615
2616                         c.SetState (State.IsInputChar);
2617                         if (testing_callstack) Console.WriteLine ("Start");
2618                         Assert.AreEqual (false, c.PreProcessMessage (ref m), "A7");
2619                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2620
2621                         c.SetState (State.ProcessDialogChar);
2622                         if (testing_callstack) Console.WriteLine ("Start");
2623                         Assert.AreEqual (true, c.PreProcessMessage (ref m), "A8");
2624                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2625
2626
2627                         m.Msg = WM_SYSCHAR;
2628                         c.SetState (State.None);
2629                         if (testing_callstack) Console.WriteLine ("Start");
2630                         Assert.AreEqual (false, c.PreProcessMessage (ref m), "A9");
2631                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2632
2633                         c.SetState (State.IsInputChar);
2634                         if (testing_callstack) Console.WriteLine ("Start");
2635                         Assert.AreEqual (false, c.PreProcessMessage (ref m), "A10");
2636                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2637
2638                         c.SetState (State.ProcessDialogChar);
2639                         if (testing_callstack) Console.WriteLine ("Start");
2640                         Assert.AreEqual (true, c.PreProcessMessage (ref m), "A11");
2641                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2642
2643
2644                         m.Msg = WM_KEYUP;
2645                         if (testing_callstack) Console.WriteLine ("Start");
2646                         Assert.AreEqual (false, c.PreProcessMessage (ref m), "A12");
2647                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2648
2649                         c.SetState (State.OnPreviewKeyDown);
2650                         if (testing_callstack) Console.WriteLine ("Start");
2651                         Assert.AreEqual (false, c.PreProcessMessage (ref m), "A13");
2652                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2653
2654                         c.SetState (State.ProcessCmdKey);
2655                         if (testing_callstack) Console.WriteLine ("Start");
2656                         Assert.AreEqual (false, c.PreProcessMessage (ref m), "A14");
2657                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2658
2659                         c.SetState (State.IsInputKey);
2660                         if (testing_callstack) Console.WriteLine ("Start");
2661                         Assert.AreEqual (false, c.PreProcessMessage (ref m), "A15");
2662                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2663
2664                         c.SetState (State.ProcessDialogKey);
2665                         if (testing_callstack) Console.WriteLine ("Start");
2666                         Assert.AreEqual (false, c.PreProcessMessage (ref m), "A16");
2667                         if (testing_callstack) Console.WriteLine ("End {0}\n", m.WParam.ToString ());
2668                 }
2669                 private enum State
2670                 {
2671                         None,
2672                         ProcessCmdKey,
2673                         OnPreviewKeyDown,
2674                         IsInputChar,
2675                         IsInputKey,
2676                         PreProcessMessage,
2677                         ProcessDialogKey,
2678                         ProcessDialogChar
2679                 }
2680
2681                 private class MyControl : Control
2682                 {
2683
2684                         private State current_state;
2685                         bool testing_callstack = false;
2686
2687                         public void SetState (State state)
2688                         {
2689                                 current_state = state;
2690                         }
2691
2692                         protected override bool ProcessCmdKey (ref Message msg, Keys keyData)
2693                         {
2694                                 if (testing_callstack) Console.Write ("ProcessCmdKey[");
2695                                 if (current_state == State.ProcessCmdKey) {
2696                                         if (testing_callstack) Console.WriteLine ("]");
2697                                         return true;
2698                                 }
2699
2700                                 bool retval = base.ProcessCmdKey (ref msg, keyData);
2701                                 if (testing_callstack) Console.WriteLine ("]");
2702                                 return retval;
2703                         }
2704
2705 #if NET_2_0
2706                         protected override void OnPreviewKeyDown (PreviewKeyDownEventArgs e)
2707                         {
2708                                 if (testing_callstack) Console.Write ("OnPreviewKeyDown[");
2709                                 if (current_state == State.OnPreviewKeyDown) {
2710                                         e.IsInputKey = true;
2711                                         if (testing_callstack) Console.WriteLine ("]");
2712                                         return;
2713                                 }
2714
2715                                 base.OnPreviewKeyDown (e);
2716                                 if (testing_callstack) Console.WriteLine ("]");
2717                         }
2718 #endif
2719
2720                         protected override bool IsInputChar (char charCode)
2721                         {
2722                                 if (testing_callstack) Console.Write ("IsInputChar[");
2723                                 if (current_state == State.IsInputChar) {
2724                                         if (testing_callstack) Console.WriteLine ("true]");
2725                                         return true;
2726                                 }
2727
2728                                 bool retval = base.IsInputChar (charCode);
2729                                 if (testing_callstack) Console.WriteLine ("{0}]", retval.ToString ());
2730                                 return retval;
2731                         }
2732
2733                         protected override bool IsInputKey (Keys keyData)
2734                         {
2735                                 if (testing_callstack) Console.Write ("IsInputKey[");
2736                                 if (current_state == State.IsInputKey) {
2737                                         if (testing_callstack) Console.WriteLine ("]");
2738                                         return true;
2739                                 }
2740
2741                                 bool retval = base.IsInputKey (keyData);
2742                                 if (testing_callstack) Console.WriteLine ("]");
2743                                 return retval;
2744                         }
2745
2746                         public override bool PreProcessMessage (ref Message msg)
2747                         {
2748                                 if (testing_callstack) Console.Write ("PreProcessMessage[");
2749                                 if (current_state == State.PreProcessMessage) {
2750                                         if (testing_callstack) Console.WriteLine ("]");
2751                                         return true;
2752                                 }
2753
2754                                 bool retval = base.PreProcessMessage (ref msg);
2755                                 if (testing_callstack) Console.WriteLine ("]");
2756                                 return retval;
2757                         }
2758
2759                         protected override bool ProcessDialogKey (Keys keyData)
2760                         {
2761                                 if (testing_callstack) Console.Write ("ProcessDialogKey[");
2762                                 if (current_state == State.ProcessDialogKey) {
2763                                         if (testing_callstack) Console.WriteLine ("]");
2764                                         return true;
2765                                 }
2766
2767                                 bool retval = base.ProcessDialogKey (keyData);
2768                                 if (testing_callstack) Console.WriteLine ("]");
2769                                 return retval;
2770                         }
2771
2772                         protected override bool ProcessDialogChar (char charCode)
2773                         {
2774                                 if (testing_callstack) Console.Write ("ProcessDialogChar[");
2775                                 if (current_state == State.ProcessDialogChar) {
2776                                         if (testing_callstack) Console.WriteLine ("]");
2777                                         return true;
2778                                 }
2779
2780                                 bool retval = base.ProcessDialogChar (charCode);
2781                                 if (testing_callstack) Console.WriteLine ("]");
2782                                 return retval;
2783                         }
2784                 }
2785                 
2786                 [Test]
2787                 public void MethodIsInputChar ()
2788                 {
2789                         // Basically, show that this method always returns false
2790                         InputCharControl m = new InputCharControl ();
2791                         bool result = false;
2792                         
2793                         for (int i = 0; i < 256; i++)
2794                                 result |= m.PublicIsInputChar ((char)i);
2795                         
2796                         Assert.AreEqual (false, result, "I1");
2797                 }
2798
2799                 private class InputCharControl : Control
2800                 {
2801                         public bool PublicIsInputChar (char charCode)
2802                         {
2803                                 return base.IsInputChar (charCode);
2804                         }
2805
2806                 }
2807
2808                 [Test] // bug #81118, 81718
2809                 public void VisibleTriggersLayout ()
2810                 {
2811                         Form f = new Form ();
2812                         f.ShowInTaskbar = false;
2813                         
2814                         Control c = new Control ();
2815                         c.Visible = false;
2816                         
2817                         f.Controls.Add (c);
2818                         
2819                         c.Dock = DockStyle.Fill;
2820                         c.Visible = true;
2821                         
2822                         Assert.AreEqual (f.ClientSize.Width, c.Width, "L1");
2823                         
2824                         f.Dispose ();
2825                 }
2826                 
2827                 [Test]
2828                 public void ResumeLayoutEffects ()
2829                 {
2830                         Form f = new Form ();
2831                         f.ShowInTaskbar = false;
2832                         f.ClientSize = new Size (300, 300);
2833                         
2834                         Button button1 = new Button ();
2835                         f.Controls.Add (button1);
2836                         button1.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
2837                         button1.Location = new Point (f.ClientSize.Width - button1.Width, f.ClientSize.Height - button1.Height);
2838
2839                         f.Show ();
2840                         
2841                         Assert.AreEqual (new Point (225, 277), button1.Location, "A1");
2842                         
2843                         f.SuspendLayout ();
2844                         f.Height += 10;
2845                         f.ResumeLayout (false);
2846                         f.PerformLayout ();
2847
2848                         Assert.AreEqual (new Point (225, 277), button1.Location, "A2");
2849
2850                         f.SuspendLayout ();
2851                         f.Height += 10;
2852                         f.ResumeLayout ();
2853
2854                         Assert.AreEqual (new Point (225, 287), button1.Location, "A3");
2855                         f.Dispose ();
2856                 }
2857                 
2858                 [Test]
2859                 public void DisposeEnumerator ()
2860                 {
2861                         // We can modify the collection while looping through it without crashing
2862                         Control c = new Control ();
2863
2864                         c.Controls.Add (new Control ());
2865                         c.Controls.Add (new Control ());
2866                         
2867                         foreach (Control c1 in c.Controls)
2868                                 c.Controls.Remove (c1);
2869                 }
2870                 
2871                 [Test]
2872                 public void MethodSetBounds ()
2873                 {
2874                         Control myControl = new Control();
2875                         myControl.SetBounds(10, 20, 30, 40);
2876                         myControl.SetBounds(50, 60, 70, 70, BoundsSpecified.Location);
2877
2878                         Assert.AreEqual (new Rectangle (50, 60, 30, 40), myControl.Bounds, "A1");
2879                 }
2880                 
2881                 [Test]
2882                 public void Bug386450 ()
2883                 {
2884                         // Should not crash.  We have to check for the font object
2885                         // being different, not just if they represent the same font.
2886                         Form f = new Form ();
2887                         Label l = new Label ();
2888                         l.Text = "Hello";
2889
2890                         Font f1 = new Font ("Arial", 12);
2891                         Font f2 = new Font ("Arial", 12);
2892
2893                         l.Font = f1;
2894                         l.Font = f2;
2895
2896                         f1.Dispose ();
2897
2898                         f.Controls.Add (l);
2899                         f.Show ();
2900                         f.Dispose ();
2901                 }
2902         }
2903
2904         [TestFixture]
2905         public class ControlSetTopLevelTest : TestHelper
2906         {
2907                 class ControlPoker : Control {
2908                         public void DoSetTopLevel ()
2909                         {
2910                                 SetTopLevel (true);
2911                         }
2912                         public bool DoGetTopLevel ()
2913                         {
2914                                 return GetTopLevel ();
2915                         }
2916                 }
2917
2918                 [Test]
2919                 public void TestControl ()
2920                 {
2921                         ControlPoker c = new ControlPoker ();
2922                         c.Visible = false;
2923                         c.DoSetTopLevel ();
2924                         Assert.IsTrue (c.DoGetTopLevel (), "1");
2925                         Assert.IsFalse (c.Visible, "2");
2926                 }
2927
2928                 [Test]
2929                 [ExpectedException (typeof (ArgumentException))]
2930                 public void TestChildControl ()
2931                 {
2932                         Control c1 = new Control();
2933                         ControlPoker c2 = new ControlPoker ();
2934
2935                         c1.Controls.Add (c2);
2936                         c2.DoSetTopLevel ();
2937                 }
2938
2939                 [Test]
2940                 [ExpectedException (typeof (ArgumentException))]
2941                 public void TestTopLevelAdd () {
2942                         Form f = new Form();
2943                         Form f1 = new Form();
2944                         f.Controls.Add(f1);
2945                 }
2946                 
2947                 [Test]
2948                 [Category ("NotWorking")]
2949                 public void TestForm ()
2950                 {
2951                         Form f = new Form ();
2952                         Assert.IsFalse (f.Visible, "3");
2953                         f.TopLevel = true;
2954                         Assert.IsFalse (f.Visible, "4");
2955                         
2956                         f.Dispose ();
2957                 }
2958         }
2959
2960         [TestFixture]
2961         public class ControlResizeLayoutTest : TestHelper
2962         {
2963                 class ControlPoker : Control {
2964                         public void DoOnResize ()
2965                         {
2966                                 OnResize (EventArgs.Empty);
2967                         }
2968                 }
2969
2970                 int child_event;
2971                 string child_affected_property;
2972                 void ChildLayoutEvent (object sender, LayoutEventArgs e)
2973                 {
2974                         child_event ++;
2975                         child_affected_property = e.AffectedProperty;
2976                 }
2977
2978                 int parent_event;
2979                 string parent_affected_property;
2980                 void ParentLayoutEvent (object sender, LayoutEventArgs e)
2981                 {
2982                         parent_event ++;
2983                         parent_affected_property = e.AffectedProperty;
2984
2985                         TestHelper.RemoveWarning (parent_affected_property);
2986                 }
2987
2988                 [Test]
2989                 public void Test ()
2990                 {
2991                         Panel p = new Panel ();
2992                         ControlPoker c = new ControlPoker();
2993
2994                         p.Controls.Add (c);
2995
2996                         p.Layout += new LayoutEventHandler (ParentLayoutEvent);
2997                         c.Layout += new LayoutEventHandler (ChildLayoutEvent);
2998
2999                         c.DoOnResize ();
3000
3001                         Assert.AreEqual (1, child_event, "1");
3002                         Assert.AreEqual ("Bounds", child_affected_property, "2");
3003
3004                         Assert.AreEqual (0, parent_event, "3");
3005                 }
3006                 
3007         }
3008
3009         [TestFixture]
3010         [Category ("NotWorking")]
3011         public class ControlInvokeTest  : TestHelper {
3012
3013                 [TearDown]
3014                 protected override void TearDown ()
3015                 {
3016                         if (f != null && !f.IsDisposed)                 
3017                                 f.Dispose ();
3018                         base.TearDown ();
3019                 }
3020
3021                 
3022                 public delegate void TestDelegate ();
3023
3024                 Form f;
3025                 Control c;
3026                 Thread control_t;
3027                 ApplicationContext control_context;
3028                 bool delegateCalled = false;
3029                 bool threadDied = false;
3030
3031                 object m;
3032
3033                 void CreateControl ()
3034                 {
3035                         try {
3036                         f = new Form ();
3037                         f.ShowInTaskbar = false;
3038                         
3039                         c = new Control ();
3040
3041                         f.Controls.Add (c);
3042
3043                         Console.WriteLine ("f.Handle = {0}", f.Handle);
3044                         Console.WriteLine ("c.Handle = {0}", c.Handle);
3045
3046                         control_context = new ApplicationContext (f);
3047
3048                         Monitor.Enter (m);
3049                         Console.WriteLine ("pulsing");
3050                         Monitor.Pulse (m);
3051                         Monitor.Exit (m);
3052                         Console.WriteLine ("control thread running");
3053                         Application.Run (control_context);
3054                         c.Dispose ();
3055                         Console.WriteLine ("dying");
3056                         threadDied = true;
3057                         Monitor.Enter (m);
3058                         Console.WriteLine ("pulsing again");
3059                         Monitor.Pulse (m);
3060                         Monitor.Exit (m);
3061                         } catch (Exception e) { Console.WriteLine (e); }
3062                 }
3063
3064                 [Test]
3065                 public void InvokeTest ()
3066                 {
3067                         m = new object ();
3068
3069                         control_t = new Thread(new ThreadStart(CreateControl));
3070
3071                         Monitor.Enter (m);
3072
3073                         control_t.Start ();
3074
3075                         Console.WriteLine ("waiting on monitor");
3076                         Monitor.Wait (m);
3077
3078                         Console.WriteLine ("making async call");
3079
3080                         IAsyncResult result;
3081                         result = c.BeginInvoke (new TestDelegate (delegate_call));
3082                         c.EndInvoke (result);
3083
3084                         Assert.IsTrue (delegateCalled, "Invoke1");
3085
3086                         Monitor.Wait (m);
3087                         Assert.IsTrue (threadDied, "Invoke2");
3088                 }
3089
3090                 public void delegate_call () {
3091                         try {
3092                         /* invoked on control_context's thread */
3093                         delegateCalled = true;
3094                         f.Dispose ();
3095                         Console.WriteLine ("calling Application.Exit");
3096                         control_context.ExitThread ();
3097                         } catch (Exception e) { Console.WriteLine (e); }
3098                 }
3099                 
3100         }
3101
3102         [TestFixture]
3103         public class ControlWMTest : TestHelper
3104         {
3105                 [Test]
3106                 public void WM_PARENTNOTIFY_Test ()
3107                 {
3108                         WMTester tester;
3109                         Control child;
3110                         int child_handle;
3111                         
3112                         tester = new WMTester ();
3113                         child = new Control ();
3114                         tester.Controls.Add (child);
3115                         
3116                         tester.Visible = true;
3117                         child.Visible = true;
3118
3119                         child_handle = child.Handle.ToInt32 ();
3120
3121                         ArrayList msgs;
3122                         Message m1;
3123                                 
3124                         msgs = tester.Find (WndMsg.WM_PARENTNOTIFY);
3125                         
3126                         Assert.AreEqual (1, msgs.Count, "#1");
3127                         
3128                         m1 = (Message) msgs [0];
3129                         Assert.AreEqual (WndMsg.WM_CREATE, ((WndMsg) LowOrder (m1.WParam)),  "#2");
3130                         //Assert.AreEqual (child.Identifier??, HighOrder (m1.WParam),  "#3");
3131                         Assert.AreEqual (child_handle, m1.LParam.ToInt32 (),  "#4");
3132
3133                         child.Dispose ();
3134
3135                         msgs = tester.Find (WndMsg.WM_PARENTNOTIFY);
3136                         Assert.AreEqual (2, msgs.Count, "#5");
3137                         m1 = (Message) msgs [1];
3138
3139                         Assert.AreEqual (WndMsg.WM_DESTROY, ((WndMsg) LowOrder (m1.WParam)),  "#6");
3140                         //Assert.AreEqual (child.Identifier??, HighOrder (m1.WParam),  "#7");
3141                         Assert.AreEqual (child_handle, m1.LParam.ToInt32 (),  "#8");
3142
3143                         tester.Dispose ();
3144                 }
3145
3146                 internal static int LowOrder (int param) 
3147                 {
3148                         return ((int)(short)(param & 0xffff));
3149                 }
3150
3151                 internal static int HighOrder (int param) 
3152                 {
3153                         return ((int)(short)(param >> 16));
3154                 }
3155
3156                 internal static int LowOrder (IntPtr param) 
3157                 {
3158                         return ((int)(short)(param.ToInt32 () & 0xffff));
3159                 }
3160
3161                 internal static int HighOrder (IntPtr param) 
3162                 {
3163                         return ((int)(short)(param.ToInt32 () >> 16));
3164                 }
3165
3166                 internal class WMTester : Form
3167                 {
3168                         internal ArrayList Messages = new ArrayList ();
3169                         
3170                         internal bool Contains (WndMsg msg)
3171                         {
3172                                 return Contains (msg, Messages);
3173                         }
3174
3175                         internal bool Contains (WndMsg msg, ArrayList list)
3176                         {
3177                                 foreach (Message m in Messages) 
3178                                 {
3179                                         if (m.Msg == (int) msg)
3180                                                 return true;
3181                                 }
3182                                 return false;
3183                         }
3184
3185                         internal ArrayList Find (WndMsg msg)
3186                         {
3187                                 ArrayList result = new ArrayList ();
3188
3189                                 foreach (Message m in Messages)
3190                                 {
3191                                         if (m.Msg == (int) msg)
3192                                                 result.Add (m);
3193                                 }
3194                                 return result;
3195                         }
3196
3197                         protected override void WndProc(ref Message m)
3198                         {
3199                                 Console.WriteLine ("WndProc: " + m.ToString ());
3200                                 Messages.Add (m);
3201                                 base.WndProc (ref m);
3202                         }
3203                 }
3204         }
3205
3206 #if NET_2_0
3207         [TestFixture]
3208         public class ControlLayoutTest : TestHelper
3209         {
3210                 [SetUp]
3211                 protected override void SetUp ()
3212                 {
3213                         _layoutCount = 0;
3214                         base.SetUp ();
3215                 }
3216
3217                 [Test] // bug #80456
3218                 public void LayoutTest ()
3219                 {
3220                         MockLayoutEngine layoutEngine = new MockLayoutEngine ();
3221                         MockControl c = new MockControl (layoutEngine);
3222                         c.Layout += new LayoutEventHandler (LayoutEvent);
3223                         Assert.IsFalse (layoutEngine.LayoutInvoked, "#A1");
3224                         Assert.AreEqual (0, _layoutCount, "#A2");
3225                         c.PerformLayout ();
3226                         Assert.IsTrue (layoutEngine.LayoutInvoked, "#A3");
3227                         Assert.AreEqual (1, _layoutCount, "#A4");
3228
3229                         layoutEngine.Reset ();
3230                         c.OverrideOnLayout = true;
3231                         Assert.IsFalse (layoutEngine.LayoutInvoked, "#B1");
3232                         c.PerformLayout ();
3233                         Assert.IsFalse (layoutEngine.LayoutInvoked, "#B2");
3234                         Assert.AreEqual (1, _layoutCount, "#B3");
3235                 }
3236
3237                 void LayoutEvent (object sender, LayoutEventArgs e)
3238                 {
3239                         _layoutCount++;
3240                 }
3241
3242                 private int _layoutCount;
3243
3244                 class MockControl : Control
3245                 {
3246                         public MockControl (LayoutEngine layoutEngine)
3247                         {
3248                                 _layoutEngine = layoutEngine;
3249                         }
3250
3251                         public bool OverrideOnLayout
3252                         {
3253                                 get { return _overrideOnLayout; }
3254                                 set { _overrideOnLayout = value; }
3255                         }
3256
3257                         protected override void OnLayout (LayoutEventArgs levent)
3258                         {
3259                                 if (!OverrideOnLayout)
3260                                         base.OnLayout (levent);
3261                         }
3262
3263                         public override LayoutEngine LayoutEngine {
3264                                 get {
3265                                         if (_layoutEngine == null)
3266                                                 return base.LayoutEngine;
3267                                         return _layoutEngine;
3268                                 }
3269                         }
3270
3271                         private bool _overrideOnLayout;
3272                         private LayoutEngine _layoutEngine;
3273                 }
3274
3275                 class MockLayoutEngine : LayoutEngine
3276                 {
3277                         public bool LayoutInvoked {
3278                                 get { return _layoutInvoked; }
3279                         }
3280
3281                         public void Reset ()
3282                         {
3283                                 _layoutInvoked = false;
3284                         }
3285
3286                         public override bool Layout (object container, LayoutEventArgs args)
3287                         {
3288                                 _layoutInvoked = true;
3289                                 return true;
3290                         }
3291
3292                         private bool _layoutInvoked;
3293                 }
3294         }
3295 #endif
3296 }