* Makefile.am: Build `docs` after `runtime`, so that it can depend
[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 {
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 #if NET_2_0
324                 [Test]
325                 [NUnit.Framework.Category ("NotWorking")]  // Depends on fonts *AND* DPI, how useless is that? (Values are Vista/96DPI)
326                 public void AutoScaling ()
327                 {
328                         ContainerControl c = new ContainerControl ();
329                         c.ClientSize = new Size (100, 100);
330
331                         Assert.AreEqual (new SizeF (0, 0), c.CurrentAutoScaleDimensions, "A1");
332                         Assert.AreEqual (new SizeF (0, 0), c.AutoScaleDimensions, "A2");
333                         Assert.AreEqual (new Size (100, 100), c.ClientSize, "A3");
334
335                         c.AutoScaleMode = AutoScaleMode.Dpi;
336                         Assert.AreEqual (new SizeF (96, 96), c.CurrentAutoScaleDimensions, "A4");
337                         Assert.AreEqual (new SizeF (96, 96), c.AutoScaleDimensions, "A5");
338                         Assert.AreEqual (new Size (100, 100), c.ClientSize, "A6");
339
340                         c.AutoScaleMode = AutoScaleMode.Font;
341                         Assert.AreEqual (new SizeF (6, 13), c.CurrentAutoScaleDimensions, "A7");
342                         Assert.AreEqual (new SizeF (6, 13), c.AutoScaleDimensions, "A8");
343                         Assert.AreEqual (new Size (100, 100), c.ClientSize, "A9");
344
345                         c.Font = new Font ("Arial", 15);
346                         Assert.AreEqual (new SizeF (11, 23), c.CurrentAutoScaleDimensions, "A10");
347                         Assert.AreEqual (new SizeF (11, 23), c.AutoScaleDimensions, "A11");
348                         Assert.AreEqual (new Size (183, 177), c.ClientSize, "A12");
349
350                         c.Font = new Font ("Tahoma", 12);
351                         Assert.AreEqual (new SizeF (9, 19), c.CurrentAutoScaleDimensions, "A13");
352                         Assert.AreEqual (new SizeF (9, 19), c.AutoScaleDimensions, "A14");
353                         Assert.AreEqual (new Size (150, 146), c.ClientSize, "A15");
354
355                         c.Font = new Font ("Times New Roman", 14);
356                         Assert.AreEqual (new SizeF (10, 21), c.CurrentAutoScaleDimensions, "A16");
357                         Assert.AreEqual (new SizeF (10, 21), c.AutoScaleDimensions, "A17");
358                         Assert.AreEqual (new Size (167, 161), c.ClientSize, "A18");
359                 }
360                 
361                 [Test]
362                 public void ValidationChildren ()
363                 {
364                         string validating = string.Empty;
365                         string validated = string.Empty;
366                         
367                         Form f = new Form ();
368                         f.ShowInTaskbar = false;
369                         
370                         ContainerControl cc = new ContainerControl ();
371                         f.Controls.Add (cc);
372                         
373                         TextBox t = new TextBox ();
374                         t.Validating += new CancelEventHandler (delegate (Object obj, CancelEventArgs e) { validating += ("t;"); });
375                         t.Validated += new EventHandler (delegate (Object obj, EventArgs e) { validated += ("t;"); });
376                         cc.Controls.Add (t);
377
378                         TextBox t1 = new TextBox ();
379                         t1.Validating += new CancelEventHandler (delegate (Object obj, CancelEventArgs e) { validating += ("t1;"); });
380                         t1.Validated += new EventHandler (delegate (Object obj, EventArgs e) { validated += ("t1;"); });
381                         t1.TabStop = false;
382                         cc.Controls.Add (t1);
383
384                         TextBox t2 = new TextBox ();
385                         t2.Validating += new CancelEventHandler (delegate (Object obj, CancelEventArgs e) { validating += ("t2;"); });
386                         t2.Validated += new EventHandler (delegate (Object obj, EventArgs e) { validated += ("t2;"); });
387                         t2.Visible = false;
388                         cc.Controls.Add (t2);
389
390                         TextBox t3 = new TextBox ();
391                         t3.Validating += new CancelEventHandler (delegate (Object obj, CancelEventArgs e) { validating += ("t3;"); });
392                         t3.Validated += new EventHandler (delegate (Object obj, EventArgs e) { validated += ("t3;"); });
393                         t3.Enabled = false;
394                         cc.Controls.Add (t3);
395
396                         Panel p = new Panel ();
397                         cc.Controls.Add (p);
398                         
399                         TextBox t4 = new TextBox ();
400                         t4.Validating += new CancelEventHandler (delegate (Object obj, CancelEventArgs e) { validating += ("t4;"); });
401                         t4.Validated += new EventHandler (delegate (Object obj, EventArgs e) { validated += ("t4;"); });
402                         p.Controls.Add (t4);
403
404                         Label l = new Label ();
405                         l.Validating += new CancelEventHandler (delegate (Object obj, CancelEventArgs e) { validating += ("l;"); });
406                         l.Validated += new EventHandler (delegate (Object obj, EventArgs e) { validated += ("l;"); });
407                         cc.Controls.Add (l);
408         
409                         f.Show ();
410                         
411                         cc.ValidateChildren ();
412                         Assert.AreEqual ("t;t1;t2;t3;t4;", validating, "A4a");
413                         Assert.AreEqual ("t;t1;t2;t3;t4;", validated, "A4b");
414                         validating = string.Empty; validated = string.Empty;
415
416                         cc.ValidateChildren (ValidationConstraints.Enabled);
417                         Assert.AreEqual ("t;t1;t2;t4;l;", validating, "A5a");
418                         Assert.AreEqual ("t;t1;t2;t4;l;", validated, "A5b");
419                         validating = string.Empty; validated = string.Empty;
420
421                         cc.ValidateChildren (ValidationConstraints.ImmediateChildren);
422                         Assert.AreEqual ("t;t1;t2;t3;l;", validating, "A6a");
423                         Assert.AreEqual ("t;t1;t2;t3;l;", validated, "A6b");
424                         validating = string.Empty; validated = string.Empty;
425
426                         cc.ValidateChildren (ValidationConstraints.None);
427                         Assert.AreEqual ("t;t1;t2;t3;t4;l;", validating, "A7a");
428                         Assert.AreEqual ("t;t1;t2;t3;t4;l;", validated, "A7b");
429                         validating = string.Empty; validated = string.Empty;
430
431                         cc.ValidateChildren (ValidationConstraints.Selectable);
432                         Assert.AreEqual ("t;t1;t2;t3;t4;", validating, "A8a");
433                         Assert.AreEqual ("t;t1;t2;t3;t4;", validated, "A8b");
434                         validating = string.Empty; validated = string.Empty;
435
436                         cc.ValidateChildren (ValidationConstraints.TabStop);
437                         Assert.AreEqual ("t;t2;t3;t4;", validating, "A9a");
438                         Assert.AreEqual ("t;t2;t3;t4;", validated, "A9b");
439                         validating = string.Empty; validated = string.Empty;
440
441                         cc.ValidateChildren (ValidationConstraints.Visible);
442                         Assert.AreEqual ("t;t1;t3;t4;l;", validating, "A10a");
443                         Assert.AreEqual ("t;t1;t3;t4;l;", validated, "A10b");
444                         validating = string.Empty; validated = string.Empty;
445
446                         cc.ValidateChildren (ValidationConstraints.Enabled | ValidationConstraints.Visible | ValidationConstraints.Selectable);
447                         Assert.AreEqual ("t;t1;t4;", validating, "A11a");
448                         Assert.AreEqual ("t;t1;t4;", validated, "A11b");
449                         validating = string.Empty; validated = string.Empty;
450                 }
451 #endif
452         }
453 }