2 // ContainerControl class testing unit
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
30 using C=System.ComponentModel;
31 using System.Security.Permissions;
32 using System.Windows.Forms;
33 using System.Collections;
34 using NUnit.Framework;
36 using System.ComponentModel;
38 namespace MonoTests.System.Windows.Forms {
40 public class IContainerControlTest : Control, IContainerControl {
42 public bool ActivateControl (Control active)
47 public Control ActiveControl {
53 public class FormCustom: Form {
57 public FormCustom(string name, bool record, Queue events) {
63 protected override void OnValidating(C.CancelEventArgs e) {
65 events.Enqueue(String.Format("{0}:{1}:OnValidating", this, this.Name));
66 base.OnValidating (e);
69 protected override void OnValidated(EventArgs e) {
71 events.Enqueue(String.Format("{0}:{1}:OnValidated", this, this.Name));
75 protected override void OnGotFocus(EventArgs e) {
77 events.Enqueue(String.Format("{0}:{1}:OnGotFocus", this, this.Name));
83 public class ContainerControlCustom: ContainerControl {
87 public ContainerControlCustom(string name, bool record, Queue events) {
93 protected override void OnValidating(C.CancelEventArgs e) {
95 events.Enqueue(String.Format("{0}:{1}:OnValidating", this, this.Name));
96 base.OnValidating (e);
99 protected override void OnValidated(EventArgs e) {
101 events.Enqueue(String.Format("{0}:{1}:OnValidated", this, this.Name));
102 base.OnValidated (e);
105 protected override void OnGotFocus(EventArgs e) {
107 events.Enqueue(String.Format("{0}:{1}:OnGotFocus", this, this.Name));
111 protected override void Select(bool directed, bool forward) {
113 events.Enqueue(String.Format("{0}:{1}:Select", this, this.Name));
114 base.Select (directed, forward);
118 public class UserControlCustom: UserControl {
122 public UserControlCustom(string name, bool record, Queue events) {
124 this.record = record;
125 this.events = events;
127 protected override void OnValidating(C.CancelEventArgs e) {
129 events.Enqueue(String.Format("{0}:{1}:OnValidating", this, this.Name));
130 base.OnValidating (e);
133 protected override void OnValidated(EventArgs e) {
135 events.Enqueue(String.Format("{0}:{1}:OnValidated", this, this.Name));
136 base.OnValidated (e);
139 protected override void OnGotFocus(EventArgs e) {
141 events.Enqueue(String.Format("{0}:{1}:OnGotFocus", this, this.Name));
145 protected override void Select(bool directed, bool forward) {
147 events.Enqueue(String.Format("{0}:{1}:Select", this, this.Name));
148 base.Select (directed, forward);
153 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
154 public class ContainerControlTest {
156 public void RemoveActiveControlTest ()
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");
169 /// Test Bug 329718 - Forms: ArgEx: "Cannot activate invisible...", when open color picker twice
172 public void RemoveActiveControlTest2 ()
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);
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");
191 public void GetContainerControl ()
193 ContainerControl cc = new ContainerControl ();
194 Assert.IsTrue (Object.ReferenceEquals (cc, cc.GetContainerControl ()), "ContainerControl.GetContainerControl");
196 Button b = new Button ();
197 Assert.IsNull (b.GetContainerControl (), "Button.GetContainerControl/without parent");
199 Assert.IsTrue (Object.ReferenceEquals (cc, b.GetContainerControl ()), "Button.GetContainerControl");
203 public void GetContainerControl_WithoutStyle ()
205 IContainerControlTest cct = new IContainerControlTest ();
206 Assert.IsNull (cct.GetContainerControl (), "IContainerControlTest.GetContainerControl");
208 Button b = new Button ();
210 Assert.IsNull (b.GetContainerControl (), "Button.GetContainerControl/without parent");
214 [ExpectedException (typeof (ArgumentException))]
215 public void ActiveControlNotChildTest ()
217 ContainerControl c = new ContainerControl ();
218 c.ActiveControl = new Control ();
222 public void Validation() {
223 Queue events = new Queue();
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);
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);
244 events.Enqueue("START");
246 events.Enqueue("END");
247 events.Enqueue("START");
249 events.Enqueue("END");
250 events.Enqueue("START");
252 events.Enqueue("END");
253 events.Enqueue("START");
255 events.Enqueue("END");
256 events.Enqueue("START");
258 events.Enqueue("END");
259 events.Enqueue("START");
261 events.Enqueue("END");
264 while (events.Count > 0) {
265 s = events.Dequeue();
266 Console.WriteLine(s.ToString());
275 container1.Dispose();
276 container1.Dispose();
282 public void MnemonicCalledWhenCanSelectFalse ()
284 MyForm f = new MyForm ();
285 f.ShowInTaskbar = false;
287 MyControl c = new MyControl ();
292 Assert.AreEqual (false, c.CanSelect, "A1");
293 f.PublicProcessMnemonic ('b');
295 Assert.AreEqual (true, c.mnemonic_called, "A2");
299 private class MyForm : Form
301 public bool PublicProcessMnemonic (char charCode)
303 return this.ProcessMnemonic (charCode);
307 private class MyControl : Control
309 public bool mnemonic_called;
313 SetStyle (ControlStyles.Selectable, false);
316 protected override bool ProcessMnemonic (char charCode)
318 mnemonic_called = true;
319 return base.ProcessMnemonic (charCode);
325 [NUnit.Framework.Category ("NotWorking")] // Depends on fonts *AND* DPI, how useless is that? (Values are Vista/96DPI)
326 public void AutoScaling ()
328 ContainerControl c = new ContainerControl ();
329 c.ClientSize = new Size (100, 100);
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");
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");
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");
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");
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");
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");
362 public void ValidationChildren ()
364 string validating = string.Empty;
365 string validated = string.Empty;
367 Form f = new Form ();
368 f.ShowInTaskbar = false;
370 ContainerControl cc = new ContainerControl ();
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;"); });
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;"); });
382 cc.Controls.Add (t1);
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;"); });
388 cc.Controls.Add (t2);
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;"); });
394 cc.Controls.Add (t3);
396 Panel p = new Panel ();
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;"); });
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;"); });
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;
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;
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;
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;
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;
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;
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;
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;