ContainerControlTest.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. //
  2. // ContainerControl class testing unit
  3. //
  4. // Authors:
  5. // Sebastien Pouliot <[email protected]>
  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. using System;
  29. using C=System.ComponentModel;
  30. using System.Security.Permissions;
  31. using System.Windows.Forms;
  32. using System.Collections;
  33. using NUnit.Framework;
  34. using System.Drawing;
  35. using System.ComponentModel;
  36. namespace MonoTests.System.Windows.Forms {
  37. public class IContainerControlTest : Control, IContainerControl {
  38. public bool ActivateControl (Control active)
  39. {
  40. return true;
  41. }
  42. public Control ActiveControl {
  43. get { return null; }
  44. set { ; }
  45. }
  46. }
  47. public class FormCustom: Form {
  48. public bool record;
  49. public Queue events;
  50. public FormCustom(string name, bool record, Queue events) {
  51. base.Name = name;
  52. this.record = record;
  53. this.events = events;
  54. }
  55. protected override void OnValidating(C.CancelEventArgs e) {
  56. if (this.record)
  57. events.Enqueue(String.Format("{0}:{1}:OnValidating", this, this.Name));
  58. base.OnValidating (e);
  59. }
  60. protected override void OnValidated(EventArgs e) {
  61. if (this.record)
  62. events.Enqueue(String.Format("{0}:{1}:OnValidated", this, this.Name));
  63. base.OnValidated (e);
  64. }
  65. protected override void OnGotFocus(EventArgs e) {
  66. if (this.record)
  67. events.Enqueue(String.Format("{0}:{1}:OnGotFocus", this, this.Name));
  68. base.OnGotFocus (e);
  69. }
  70. }
  71. public class ContainerControlCustom: ContainerControl {
  72. public bool record;
  73. public Queue events;
  74. public ContainerControlCustom(string name, bool record, Queue events) {
  75. base.Name = name;
  76. this.record = record;
  77. this.events = events;
  78. }
  79. protected override void OnValidating(C.CancelEventArgs e) {
  80. if (this.record)
  81. events.Enqueue(String.Format("{0}:{1}:OnValidating", this, this.Name));
  82. base.OnValidating (e);
  83. }
  84. protected override void OnValidated(EventArgs e) {
  85. if (this.record)
  86. events.Enqueue(String.Format("{0}:{1}:OnValidated", this, this.Name));
  87. base.OnValidated (e);
  88. }
  89. protected override void OnGotFocus(EventArgs e) {
  90. if (this.record)
  91. events.Enqueue(String.Format("{0}:{1}:OnGotFocus", this, this.Name));
  92. base.OnGotFocus (e);
  93. }
  94. protected override void Select(bool directed, bool forward) {
  95. if (this.record)
  96. events.Enqueue(String.Format("{0}:{1}:Select", this, this.Name));
  97. base.Select (directed, forward);
  98. }
  99. }
  100. public class UserControlCustom: UserControl {
  101. public bool record;
  102. public Queue events;
  103. public UserControlCustom(string name, bool record, Queue events) {
  104. base.Name = name;
  105. this.record = record;
  106. this.events = events;
  107. }
  108. protected override void OnValidating(C.CancelEventArgs e) {
  109. if (this.record)
  110. events.Enqueue(String.Format("{0}:{1}:OnValidating", this, this.Name));
  111. base.OnValidating (e);
  112. }
  113. protected override void OnValidated(EventArgs e) {
  114. if (this.record)
  115. events.Enqueue(String.Format("{0}:{1}:OnValidated", this, this.Name));
  116. base.OnValidated (e);
  117. }
  118. protected override void OnGotFocus(EventArgs e) {
  119. if (this.record)
  120. events.Enqueue(String.Format("{0}:{1}:OnGotFocus", this, this.Name));
  121. base.OnGotFocus (e);
  122. }
  123. protected override void Select(bool directed, bool forward) {
  124. if (this.record)
  125. events.Enqueue(String.Format("{0}:{1}:Select", this, this.Name));
  126. base.Select (directed, forward);
  127. }
  128. }
  129. [TestFixture]
  130. [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
  131. public class ContainerControlTest : TestHelper {
  132. [Test]
  133. public void RemoveActiveControlTest ()
  134. {
  135. ContainerControl cc = new ContainerControl();
  136. TextBox txt = new TextBox ();
  137. cc.Controls.Add (txt);
  138. Assert.IsFalse (cc.ActiveControl == txt, "#01");
  139. cc.ActiveControl = txt;
  140. Assert.AreSame (cc.ActiveControl, txt, "#02");
  141. cc.Controls.Remove (txt);
  142. Assert.IsNull (cc.ActiveControl, "#03");
  143. }
  144. /// <summary>
  145. /// Test Bug 329718 - Forms: ArgEx: "Cannot activate invisible...", when open color picker twice
  146. /// </summary>
  147. [Test]
  148. public void RemoveActiveControlTest2 ()
  149. {
  150. ContainerControl cc = new ContainerControl ();
  151. TabControl tab = new TabControl ();
  152. TabPage page = new TabPage ();
  153. TextBox txt = new TextBox ();
  154. page.Controls.Add (txt);
  155. tab.TabPages.Add (page);
  156. cc.Controls.Add (tab);
  157. Assert.IsFalse (cc.ActiveControl == txt, "#01");
  158. cc.ActiveControl = txt;
  159. Console.WriteLine (cc.ActiveControl);
  160. Assert.AreSame (cc.ActiveControl, txt, "#02");
  161. cc.Controls.Remove (tab);
  162. Console.WriteLine (cc.ActiveControl);
  163. Assert.IsNull (cc.ActiveControl, "#03");
  164. }
  165. [Test]
  166. public void GetContainerControl ()
  167. {
  168. ContainerControl cc = new ContainerControl ();
  169. Assert.IsTrue (Object.ReferenceEquals (cc, cc.GetContainerControl ()), "ContainerControl.GetContainerControl");
  170. Button b = new Button ();
  171. Assert.IsNull (b.GetContainerControl (), "Button.GetContainerControl/without parent");
  172. b.Parent = cc;
  173. Assert.IsTrue (Object.ReferenceEquals (cc, b.GetContainerControl ()), "Button.GetContainerControl");
  174. }
  175. [Test]
  176. public void GetContainerControl_WithoutStyle ()
  177. {
  178. IContainerControlTest cct = new IContainerControlTest ();
  179. Assert.IsNull (cct.GetContainerControl (), "IContainerControlTest.GetContainerControl");
  180. Button b = new Button ();
  181. b.Parent = cct;
  182. Assert.IsNull (b.GetContainerControl (), "Button.GetContainerControl/without parent");
  183. }
  184. [Test]
  185. [ExpectedException (typeof (ArgumentException))]
  186. public void ActiveControlNotChildTest ()
  187. {
  188. ContainerControl c = new ContainerControl ();
  189. c.ActiveControl = new Control ();
  190. }
  191. // [Test]
  192. public void Validation() {
  193. Queue events = new Queue();
  194. FormCustom form = new FormCustom("form1", true, events);
  195. ContainerControlCustom container1 = new ContainerControlCustom("container1", true, events);
  196. ContainerControlCustom container2 = new ContainerControlCustom("container2", true, events);
  197. ContainerControlCustom container3 = new ContainerControlCustom("container3", true, events);
  198. UserControlCustom userctl1 = new UserControlCustom("userctl1", true, events);
  199. UserControlCustom userctl2 = new UserControlCustom("userctl2", true, events);
  200. UserControlCustom userctl3 = new UserControlCustom("userctl3", true, events);
  201. container2.Controls.Add(userctl2);
  202. container2.Controls.Add(userctl3);
  203. container1.Controls.Add(userctl1);
  204. form.Controls.Add(container1);
  205. form.Controls.Add(container2);
  206. form.Controls.Add(container3);
  207. form.Show();
  208. object s;
  209. events.Enqueue("START");
  210. container3.Select();
  211. events.Enqueue("END");
  212. events.Enqueue("START");
  213. container1.Select();
  214. events.Enqueue("END");
  215. events.Enqueue("START");
  216. container2.Select();
  217. events.Enqueue("END");
  218. events.Enqueue("START");
  219. userctl1.Select();
  220. events.Enqueue("END");
  221. events.Enqueue("START");
  222. userctl2.Select();
  223. events.Enqueue("END");
  224. events.Enqueue("START");
  225. userctl2.Select();
  226. events.Enqueue("END");
  227. while (events.Count > 0) {
  228. s = events.Dequeue();
  229. Console.WriteLine(s.ToString());
  230. }
  231. events.Clear();
  232. form.Close();
  233. userctl1.Dispose();
  234. userctl2.Dispose();
  235. userctl3.Dispose();
  236. container1.Dispose();
  237. container1.Dispose();
  238. form.Dispose();
  239. }
  240. [Test]
  241. public void MnemonicCalledWhenCanSelectFalse ()
  242. {
  243. MyForm f = new MyForm ();
  244. f.ShowInTaskbar = false;
  245. MyControl c = new MyControl ();
  246. f.Controls.Add (c);
  247. f.Show ();
  248. Assert.AreEqual (false, c.CanSelect, "A1");
  249. f.PublicProcessMnemonic ('b');
  250. Assert.AreEqual (true, c.mnemonic_called, "A2");
  251. f.Dispose ();
  252. }
  253. private class MyForm : Form
  254. {
  255. public bool PublicProcessMnemonic (char charCode)
  256. {
  257. return this.ProcessMnemonic (charCode);
  258. }
  259. }
  260. private class MyControl : Control
  261. {
  262. public bool mnemonic_called;
  263. public MyControl ()
  264. {
  265. SetStyle (ControlStyles.Selectable, false);
  266. }
  267. protected override bool ProcessMnemonic (char charCode)
  268. {
  269. mnemonic_called = true;
  270. return base.ProcessMnemonic (charCode);
  271. }
  272. }
  273. #if NET_2_0
  274. [Test]
  275. [NUnit.Framework.Category ("NotWorking")] // Depends on fonts *AND* DPI, how useless is that? (Values are Vista/96DPI)
  276. public void AutoScaling ()
  277. {
  278. ContainerControl c = new ContainerControl ();
  279. c.ClientSize = new Size (100, 100);
  280. Assert.AreEqual (new SizeF (0, 0), c.CurrentAutoScaleDimensions, "A1");
  281. Assert.AreEqual (new SizeF (0, 0), c.AutoScaleDimensions, "A2");
  282. Assert.AreEqual (new Size (100, 100), c.ClientSize, "A3");
  283. c.AutoScaleMode = AutoScaleMode.Dpi;
  284. Assert.AreEqual (new SizeF (96, 96), c.CurrentAutoScaleDimensions, "A4");
  285. Assert.AreEqual (new SizeF (96, 96), c.AutoScaleDimensions, "A5");
  286. Assert.AreEqual (new Size (100, 100), c.ClientSize, "A6");
  287. c.AutoScaleMode = AutoScaleMode.Font;
  288. Assert.AreEqual (new SizeF (6, 13), c.CurrentAutoScaleDimensions, "A7");
  289. Assert.AreEqual (new SizeF (6, 13), c.AutoScaleDimensions, "A8");
  290. Assert.AreEqual (new Size (100, 100), c.ClientSize, "A9");
  291. c.Font = new Font ("Arial", 15);
  292. Assert.AreEqual (new SizeF (11, 23), c.CurrentAutoScaleDimensions, "A10");
  293. Assert.AreEqual (new SizeF (11, 23), c.AutoScaleDimensions, "A11");
  294. Assert.AreEqual (new Size (183, 177), c.ClientSize, "A12");
  295. c.Font = new Font ("Tahoma", 12);
  296. Assert.AreEqual (new SizeF (9, 19), c.CurrentAutoScaleDimensions, "A13");
  297. Assert.AreEqual (new SizeF (9, 19), c.AutoScaleDimensions, "A14");
  298. Assert.AreEqual (new Size (150, 146), c.ClientSize, "A15");
  299. c.Font = new Font ("Times New Roman", 14);
  300. Assert.AreEqual (new SizeF (10, 21), c.CurrentAutoScaleDimensions, "A16");
  301. Assert.AreEqual (new SizeF (10, 21), c.AutoScaleDimensions, "A17");
  302. Assert.AreEqual (new Size (167, 161), c.ClientSize, "A18");
  303. }
  304. [Test]
  305. public void ValidationChildren ()
  306. {
  307. string validating = string.Empty;
  308. string validated = string.Empty;
  309. Form f = new Form ();
  310. f.ShowInTaskbar = false;
  311. ContainerControl cc = new ContainerControl ();
  312. f.Controls.Add (cc);
  313. TextBox t = new TextBox ();
  314. t.Validating += new CancelEventHandler (delegate (Object obj, CancelEventArgs e) { validating += ("t;"); });
  315. t.Validated += new EventHandler (delegate (Object obj, EventArgs e) { validated += ("t;"); });
  316. cc.Controls.Add (t);
  317. TextBox t1 = new TextBox ();
  318. t1.Validating += new CancelEventHandler (delegate (Object obj, CancelEventArgs e) { validating += ("t1;"); });
  319. t1.Validated += new EventHandler (delegate (Object obj, EventArgs e) { validated += ("t1;"); });
  320. t1.TabStop = false;
  321. cc.Controls.Add (t1);
  322. TextBox t2 = new TextBox ();
  323. t2.Validating += new CancelEventHandler (delegate (Object obj, CancelEventArgs e) { validating += ("t2;"); });
  324. t2.Validated += new EventHandler (delegate (Object obj, EventArgs e) { validated += ("t2;"); });
  325. t2.Visible = false;
  326. cc.Controls.Add (t2);
  327. TextBox t3 = new TextBox ();
  328. t3.Validating += new CancelEventHandler (delegate (Object obj, CancelEventArgs e) { validating += ("t3;"); });
  329. t3.Validated += new EventHandler (delegate (Object obj, EventArgs e) { validated += ("t3;"); });
  330. t3.Enabled = false;
  331. cc.Controls.Add (t3);
  332. Panel p = new Panel ();
  333. cc.Controls.Add (p);
  334. TextBox t4 = new TextBox ();
  335. t4.Validating += new CancelEventHandler (delegate (Object obj, CancelEventArgs e) { validating += ("t4;"); });
  336. t4.Validated += new EventHandler (delegate (Object obj, EventArgs e) { validated += ("t4;"); });
  337. p.Controls.Add (t4);
  338. Label l = new Label ();
  339. l.Validating += new CancelEventHandler (delegate (Object obj, CancelEventArgs e) { validating += ("l;"); });
  340. l.Validated += new EventHandler (delegate (Object obj, EventArgs e) { validated += ("l;"); });
  341. cc.Controls.Add (l);
  342. f.Show ();
  343. cc.ValidateChildren ();
  344. Assert.AreEqual ("t;t1;t2;t3;t4;", validating, "A4a");
  345. Assert.AreEqual ("t;t1;t2;t3;t4;", validated, "A4b");
  346. validating = string.Empty; validated = string.Empty;
  347. cc.ValidateChildren (ValidationConstraints.Enabled);
  348. Assert.AreEqual ("t;t1;t2;t4;l;", validating, "A5a");
  349. Assert.AreEqual ("t;t1;t2;t4;l;", validated, "A5b");
  350. validating = string.Empty; validated = string.Empty;
  351. cc.ValidateChildren (ValidationConstraints.ImmediateChildren);
  352. Assert.AreEqual ("t;t1;t2;t3;l;", validating, "A6a");
  353. Assert.AreEqual ("t;t1;t2;t3;l;", validated, "A6b");
  354. validating = string.Empty; validated = string.Empty;
  355. cc.ValidateChildren (ValidationConstraints.None);
  356. Assert.AreEqual ("t;t1;t2;t3;t4;l;", validating, "A7a");
  357. Assert.AreEqual ("t;t1;t2;t3;t4;l;", validated, "A7b");
  358. validating = string.Empty; validated = string.Empty;
  359. cc.ValidateChildren (ValidationConstraints.Selectable);
  360. Assert.AreEqual ("t;t1;t2;t3;t4;", validating, "A8a");
  361. Assert.AreEqual ("t;t1;t2;t3;t4;", validated, "A8b");
  362. validating = string.Empty; validated = string.Empty;
  363. cc.ValidateChildren (ValidationConstraints.TabStop);
  364. Assert.AreEqual ("t;t2;t3;t4;", validating, "A9a");
  365. Assert.AreEqual ("t;t2;t3;t4;", validated, "A9b");
  366. validating = string.Empty; validated = string.Empty;
  367. cc.ValidateChildren (ValidationConstraints.Visible);
  368. Assert.AreEqual ("t;t1;t3;t4;l;", validating, "A10a");
  369. Assert.AreEqual ("t;t1;t3;t4;l;", validated, "A10b");
  370. validating = string.Empty; validated = string.Empty;
  371. cc.ValidateChildren (ValidationConstraints.Enabled | ValidationConstraints.Visible | ValidationConstraints.Selectable);
  372. Assert.AreEqual ("t;t1;t4;", validating, "A11a");
  373. Assert.AreEqual ("t;t1;t4;", validated, "A11b");
  374. validating = string.Empty; validated = string.Empty;
  375. }
  376. #endif
  377. }
  378. }