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 : TestHelper {
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);
324 [NUnit.Framework.Category ("NotWorking")] // Depends on fonts *AND* DPI, how useless is that? (Values are Vista/96DPI)
325 public void AutoScaling ()
327 ContainerControl c = new ContainerControl ();
328 c.ClientSize = new Size (100, 100);
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");
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");
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");
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");
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");
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");
361 public void ValidationChildren ()
363 string validating = string.Empty;
364 string validated = string.Empty;
366 Form f = new Form ();
367 f.ShowInTaskbar = false;
369 ContainerControl cc = new ContainerControl ();
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;"); });
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;"); });
381 cc.Controls.Add (t1);
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;"); });
387 cc.Controls.Add (t2);
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;"); });
393 cc.Controls.Add (t3);
395 Panel p = new Panel ();
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;"); });
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;"); });
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;
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;
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;
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;
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;
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;
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;
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;