[bcl] Remove more NET_2_0 checks from class libs
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / ContainerControlTest.cs
1 //
2 // ContainerControl class testing unit
3 //
4 // Authors:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30 using C=System.ComponentModel;
31 using System.Security.Permissions;
32 using System.Windows.Forms;
33 using System.Collections;
34 using NUnit.Framework;
35 using System.Drawing;
36 using System.ComponentModel;
37
38 namespace MonoTests.System.Windows.Forms {
39
40         public class IContainerControlTest : Control, IContainerControl {
41
42                 public bool ActivateControl (Control active)
43                 {
44                         return true;
45                 }
46
47                 public Control  ActiveControl {
48                         get { return null; }
49                         set { ; }
50                 }
51         }
52
53         public class FormCustom: Form {
54                 public bool record;
55                 public Queue events;
56
57                 public FormCustom(string name, bool record, Queue events) {
58                         base.Name = name;
59                         this.record = record;
60                         this.events = events;
61                 }
62
63                 protected override void OnValidating(C.CancelEventArgs e) {
64                         if (this.record)
65                                 events.Enqueue(String.Format("{0}:{1}:OnValidating", this, this.Name));
66                         base.OnValidating (e);
67                 }
68
69                 protected override void OnValidated(EventArgs e) {
70                         if (this.record)
71                                 events.Enqueue(String.Format("{0}:{1}:OnValidated", this, this.Name));
72                         base.OnValidated (e);
73                 }
74
75                 protected override void OnGotFocus(EventArgs e) {
76                         if (this.record)
77                                 events.Enqueue(String.Format("{0}:{1}:OnGotFocus", this, this.Name));
78                         base.OnGotFocus (e);
79                 }
80
81         }
82
83         public class ContainerControlCustom: ContainerControl {
84                 public bool record;
85                 public Queue events;
86
87                 public ContainerControlCustom(string name, bool record, Queue events) {
88                         base.Name = name;
89                         this.record = record;
90                         this.events = events;
91                 }
92
93                 protected override void OnValidating(C.CancelEventArgs e) {
94                         if (this.record)
95                                 events.Enqueue(String.Format("{0}:{1}:OnValidating", this, this.Name));
96                         base.OnValidating (e);
97                 }
98
99                 protected override void OnValidated(EventArgs e) {
100                         if (this.record)
101                                 events.Enqueue(String.Format("{0}:{1}:OnValidated", this, this.Name));
102                         base.OnValidated (e);
103                 }
104                 
105                 protected override void OnGotFocus(EventArgs e) {
106                         if (this.record)
107                                 events.Enqueue(String.Format("{0}:{1}:OnGotFocus", this, this.Name));
108                         base.OnGotFocus (e);
109                 }
110
111                 protected override void Select(bool directed, bool forward) {
112                         if (this.record)
113                                 events.Enqueue(String.Format("{0}:{1}:Select", this, this.Name));
114                         base.Select (directed, forward);
115                 }
116         }
117
118         public class UserControlCustom: UserControl {
119                 public bool record;
120                 public Queue events;
121
122                 public UserControlCustom(string name, bool record, Queue events) {
123                         base.Name = name;
124                         this.record = record;
125                         this.events = events;
126                 }
127                 protected override void OnValidating(C.CancelEventArgs e) {
128                         if (this.record)
129                                 events.Enqueue(String.Format("{0}:{1}:OnValidating", this, this.Name));
130                         base.OnValidating (e);
131                 }
132
133                 protected override void OnValidated(EventArgs e) {
134                         if (this.record)
135                                 events.Enqueue(String.Format("{0}:{1}:OnValidated", this, this.Name));
136                         base.OnValidated (e);
137                 }
138
139                 protected override void OnGotFocus(EventArgs e) {
140                         if (this.record)
141                                 events.Enqueue(String.Format("{0}:{1}:OnGotFocus", this, this.Name));
142                         base.OnGotFocus (e);
143                 }
144
145                 protected override void Select(bool directed, bool forward) {
146                         if (this.record)
147                                 events.Enqueue(String.Format("{0}:{1}:Select", this, this.Name));
148                         base.Select (directed, forward);
149                 }
150         }
151
152         [TestFixture]
153         [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
154         public class ContainerControlTest  : TestHelper {
155                 [Test]
156                 public void RemoveActiveControlTest ()
157                 {
158                         ContainerControl cc = new ContainerControl();
159                         TextBox txt = new TextBox ();
160                         cc.Controls.Add (txt);
161                         Assert.IsFalse (cc.ActiveControl == txt, "#01");
162                         cc.ActiveControl = txt;
163                         Assert.AreSame (cc.ActiveControl, txt, "#02");
164                         cc.Controls.Remove (txt);
165                         Assert.IsNull (cc.ActiveControl, "#03");
166                 }
167
168                 /// <summary>
169                 /// Test Bug 329718 - Forms: ArgEx: "Cannot activate invisible...", when open color picker twice
170                 /// </summary>
171                 [Test]
172                 public void RemoveActiveControlTest2 ()
173                 {
174                         ContainerControl cc = new ContainerControl ();
175                         TabControl tab = new TabControl ();
176                         TabPage page = new TabPage ();
177                         TextBox txt = new TextBox ();
178                         page.Controls.Add (txt);
179                         tab.TabPages.Add (page);
180                         cc.Controls.Add (tab);
181
182                         Assert.IsFalse (cc.ActiveControl == txt, "#01");
183                         cc.ActiveControl = txt;
184                         Console.WriteLine (cc.ActiveControl);
185                         Assert.AreSame (cc.ActiveControl, txt, "#02");
186                         cc.Controls.Remove (tab);
187                         Console.WriteLine (cc.ActiveControl);
188                         Assert.IsNull (cc.ActiveControl, "#03");
189                 }
190                 [Test]
191                 public void GetContainerControl ()
192                 {
193                         ContainerControl cc = new ContainerControl ();
194                         Assert.IsTrue (Object.ReferenceEquals (cc, cc.GetContainerControl ()), "ContainerControl.GetContainerControl");
195
196                         Button b = new Button ();
197                         Assert.IsNull (b.GetContainerControl (), "Button.GetContainerControl/without parent");
198                         b.Parent = cc;
199                         Assert.IsTrue (Object.ReferenceEquals (cc, b.GetContainerControl ()), "Button.GetContainerControl");
200                 }
201
202                 [Test]
203                 public void GetContainerControl_WithoutStyle ()
204                 {
205                         IContainerControlTest cct = new IContainerControlTest ();
206                         Assert.IsNull (cct.GetContainerControl (), "IContainerControlTest.GetContainerControl");
207
208                         Button b = new Button ();
209                         b.Parent = cct;
210                         Assert.IsNull (b.GetContainerControl (), "Button.GetContainerControl/without parent");
211                 }
212
213                 [Test]
214                 [ExpectedException (typeof (ArgumentException))]
215                 public void ActiveControlNotChildTest ()
216                 {
217                         ContainerControl c = new ContainerControl ();
218                         c.ActiveControl = new Control ();
219                 }
220
221 //              [Test]
222                 public void Validation() {
223                         Queue events = new Queue();
224
225                         FormCustom form = new FormCustom("form1", true, events);
226                         ContainerControlCustom container1 = new ContainerControlCustom("container1", true, events);
227                         ContainerControlCustom container2 = new ContainerControlCustom("container2", true, events);
228                         ContainerControlCustom container3 = new ContainerControlCustom("container3", true, events);
229                         UserControlCustom userctl1 = new UserControlCustom("userctl1", true, events);
230                         UserControlCustom userctl2 = new UserControlCustom("userctl2", true, events);
231                         UserControlCustom userctl3 = new UserControlCustom("userctl3", true, events);
232
233                         container2.Controls.Add(userctl2);
234                         container2.Controls.Add(userctl3);
235                         container1.Controls.Add(userctl1);
236                         form.Controls.Add(container1);
237                         form.Controls.Add(container2);
238                         form.Controls.Add(container3);
239
240                         form.Show();
241
242                         object s;
243
244                         events.Enqueue("START");
245                         container3.Select();
246                         events.Enqueue("END");
247                         events.Enqueue("START");
248                         container1.Select();
249                         events.Enqueue("END");
250                         events.Enqueue("START");
251                         container2.Select();
252                         events.Enqueue("END");
253                         events.Enqueue("START");
254                         userctl1.Select();
255                         events.Enqueue("END");
256                         events.Enqueue("START");
257                         userctl2.Select();
258                         events.Enqueue("END");
259                         events.Enqueue("START");
260                         userctl2.Select();
261                         events.Enqueue("END");
262
263
264                         while (events.Count > 0) {
265                                 s = events.Dequeue();
266                                 Console.WriteLine(s.ToString());
267                         }
268
269                         events.Clear();
270
271                         form.Close();
272                         userctl1.Dispose();
273                         userctl2.Dispose();
274                         userctl3.Dispose();
275                         container1.Dispose();
276                         container1.Dispose();
277                         form.Dispose();
278
279                 }
280                 
281                 [Test]
282                 public void MnemonicCalledWhenCanSelectFalse ()
283                 {
284                         MyForm f = new MyForm ();
285                         f.ShowInTaskbar = false;
286                         
287                         MyControl c = new MyControl ();
288                         
289                         f.Controls.Add (c);
290                         f.Show ();
291                         
292                         Assert.AreEqual (false, c.CanSelect, "A1");
293                         f.PublicProcessMnemonic ('b');
294                         
295                         Assert.AreEqual (true, c.mnemonic_called, "A2");
296                         f.Dispose ();
297                 }
298                 
299                 private class MyForm : Form
300                 {
301                         public bool PublicProcessMnemonic (char charCode)
302                         {
303                                 return this.ProcessMnemonic (charCode);
304                         }
305                 }
306                 
307                 private class MyControl : Control
308                 {
309                         public bool mnemonic_called;
310                         
311                         public MyControl ()
312                         {
313                                 SetStyle (ControlStyles.Selectable, false);
314                         }
315                         
316                         protected override bool ProcessMnemonic (char charCode)
317                         {
318                                 mnemonic_called = true;
319                                 return base.ProcessMnemonic (charCode);
320                         }
321                 }
322
323                 [Test]
324                 [NUnit.Framework.Category ("NotWorking")]  // Depends on fonts *AND* DPI, how useless is that? (Values are Vista/96DPI)
325                 public void AutoScaling ()
326                 {
327                         ContainerControl c = new ContainerControl ();
328                         c.ClientSize = new Size (100, 100);
329
330                         Assert.AreEqual (new SizeF (0, 0), c.CurrentAutoScaleDimensions, "A1");
331                         Assert.AreEqual (new SizeF (0, 0), c.AutoScaleDimensions, "A2");
332                         Assert.AreEqual (new Size (100, 100), c.ClientSize, "A3");
333
334                         c.AutoScaleMode = AutoScaleMode.Dpi;
335                         Assert.AreEqual (new SizeF (96, 96), c.CurrentAutoScaleDimensions, "A4");
336                         Assert.AreEqual (new SizeF (96, 96), c.AutoScaleDimensions, "A5");
337                         Assert.AreEqual (new Size (100, 100), c.ClientSize, "A6");
338
339                         c.AutoScaleMode = AutoScaleMode.Font;
340                         Assert.AreEqual (new SizeF (6, 13), c.CurrentAutoScaleDimensions, "A7");
341                         Assert.AreEqual (new SizeF (6, 13), c.AutoScaleDimensions, "A8");
342                         Assert.AreEqual (new Size (100, 100), c.ClientSize, "A9");
343
344                         c.Font = new Font ("Arial", 15);
345                         Assert.AreEqual (new SizeF (11, 23), c.CurrentAutoScaleDimensions, "A10");
346                         Assert.AreEqual (new SizeF (11, 23), c.AutoScaleDimensions, "A11");
347                         Assert.AreEqual (new Size (183, 177), c.ClientSize, "A12");
348
349                         c.Font = new Font ("Tahoma", 12);
350                         Assert.AreEqual (new SizeF (9, 19), c.CurrentAutoScaleDimensions, "A13");
351                         Assert.AreEqual (new SizeF (9, 19), c.AutoScaleDimensions, "A14");
352                         Assert.AreEqual (new Size (150, 146), c.ClientSize, "A15");
353
354                         c.Font = new Font ("Times New Roman", 14);
355                         Assert.AreEqual (new SizeF (10, 21), c.CurrentAutoScaleDimensions, "A16");
356                         Assert.AreEqual (new SizeF (10, 21), c.AutoScaleDimensions, "A17");
357                         Assert.AreEqual (new Size (167, 161), c.ClientSize, "A18");
358                 }
359                 
360                 [Test]
361                 public void ValidationChildren ()
362                 {
363                         string validating = string.Empty;
364                         string validated = string.Empty;
365                         
366                         Form f = new Form ();
367                         f.ShowInTaskbar = false;
368                         
369                         ContainerControl cc = new ContainerControl ();
370                         f.Controls.Add (cc);
371                         
372                         TextBox t = new TextBox ();
373                         t.Validating += new CancelEventHandler (delegate (Object obj, CancelEventArgs e) { validating += ("t;"); });
374                         t.Validated += new EventHandler (delegate (Object obj, EventArgs e) { validated += ("t;"); });
375                         cc.Controls.Add (t);
376
377                         TextBox t1 = new TextBox ();
378                         t1.Validating += new CancelEventHandler (delegate (Object obj, CancelEventArgs e) { validating += ("t1;"); });
379                         t1.Validated += new EventHandler (delegate (Object obj, EventArgs e) { validated += ("t1;"); });
380                         t1.TabStop = false;
381                         cc.Controls.Add (t1);
382
383                         TextBox t2 = new TextBox ();
384                         t2.Validating += new CancelEventHandler (delegate (Object obj, CancelEventArgs e) { validating += ("t2;"); });
385                         t2.Validated += new EventHandler (delegate (Object obj, EventArgs e) { validated += ("t2;"); });
386                         t2.Visible = false;
387                         cc.Controls.Add (t2);
388
389                         TextBox t3 = new TextBox ();
390                         t3.Validating += new CancelEventHandler (delegate (Object obj, CancelEventArgs e) { validating += ("t3;"); });
391                         t3.Validated += new EventHandler (delegate (Object obj, EventArgs e) { validated += ("t3;"); });
392                         t3.Enabled = false;
393                         cc.Controls.Add (t3);
394
395                         Panel p = new Panel ();
396                         cc.Controls.Add (p);
397                         
398                         TextBox t4 = new TextBox ();
399                         t4.Validating += new CancelEventHandler (delegate (Object obj, CancelEventArgs e) { validating += ("t4;"); });
400                         t4.Validated += new EventHandler (delegate (Object obj, EventArgs e) { validated += ("t4;"); });
401                         p.Controls.Add (t4);
402
403                         Label l = new Label ();
404                         l.Validating += new CancelEventHandler (delegate (Object obj, CancelEventArgs e) { validating += ("l;"); });
405                         l.Validated += new EventHandler (delegate (Object obj, EventArgs e) { validated += ("l;"); });
406                         cc.Controls.Add (l);
407         
408                         f.Show ();
409                         
410                         cc.ValidateChildren ();
411                         Assert.AreEqual ("t;t1;t2;t3;t4;", validating, "A4a");
412                         Assert.AreEqual ("t;t1;t2;t3;t4;", validated, "A4b");
413                         validating = string.Empty; validated = string.Empty;
414
415                         cc.ValidateChildren (ValidationConstraints.Enabled);
416                         Assert.AreEqual ("t;t1;t2;t4;l;", validating, "A5a");
417                         Assert.AreEqual ("t;t1;t2;t4;l;", validated, "A5b");
418                         validating = string.Empty; validated = string.Empty;
419
420                         cc.ValidateChildren (ValidationConstraints.ImmediateChildren);
421                         Assert.AreEqual ("t;t1;t2;t3;l;", validating, "A6a");
422                         Assert.AreEqual ("t;t1;t2;t3;l;", validated, "A6b");
423                         validating = string.Empty; validated = string.Empty;
424
425                         cc.ValidateChildren (ValidationConstraints.None);
426                         Assert.AreEqual ("t;t1;t2;t3;t4;l;", validating, "A7a");
427                         Assert.AreEqual ("t;t1;t2;t3;t4;l;", validated, "A7b");
428                         validating = string.Empty; validated = string.Empty;
429
430                         cc.ValidateChildren (ValidationConstraints.Selectable);
431                         Assert.AreEqual ("t;t1;t2;t3;t4;", validating, "A8a");
432                         Assert.AreEqual ("t;t1;t2;t3;t4;", validated, "A8b");
433                         validating = string.Empty; validated = string.Empty;
434
435                         cc.ValidateChildren (ValidationConstraints.TabStop);
436                         Assert.AreEqual ("t;t2;t3;t4;", validating, "A9a");
437                         Assert.AreEqual ("t;t2;t3;t4;", validated, "A9b");
438                         validating = string.Empty; validated = string.Empty;
439
440                         cc.ValidateChildren (ValidationConstraints.Visible);
441                         Assert.AreEqual ("t;t1;t3;t4;l;", validating, "A10a");
442                         Assert.AreEqual ("t;t1;t3;t4;l;", validated, "A10b");
443                         validating = string.Empty; validated = string.Empty;
444
445                         cc.ValidateChildren (ValidationConstraints.Enabled | ValidationConstraints.Visible | ValidationConstraints.Selectable);
446                         Assert.AreEqual ("t;t1;t4;", validating, "A11a");
447                         Assert.AreEqual ("t;t1;t4;", validated, "A11b");
448                         validating = string.Empty; validated = string.Empty;
449                 }
450         }
451 }