ContainerControlTest.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 System.Security.Permissions;
  30. using System.Windows.Forms;
  31. using NUnit.Framework;
  32. namespace MonoTests.System.Windows.Forms {
  33. public class IContainerControlTest : Control, IContainerControl {
  34. public bool ActivateControl (Control active)
  35. {
  36. return true;
  37. }
  38. public Control ActiveControl {
  39. get { return null; }
  40. set { ; }
  41. }
  42. }
  43. [TestFixture]
  44. [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
  45. public class ContainerControlTest {
  46. [Test]
  47. public void GetContainerControl ()
  48. {
  49. ContainerControl cc = new ContainerControl ();
  50. Assert.IsTrue (Object.ReferenceEquals (cc, cc.GetContainerControl ()), "ContainerControl.GetContainerControl");
  51. Button b = new Button ();
  52. Assert.IsNull (b.GetContainerControl (), "Button.GetContainerControl/without parent");
  53. b.Parent = cc;
  54. Assert.IsTrue (Object.ReferenceEquals (cc, b.GetContainerControl ()), "Button.GetContainerControl");
  55. }
  56. [Test]
  57. public void GetContainerControl_WithoutStyle ()
  58. {
  59. IContainerControlTest cct = new IContainerControlTest ();
  60. Assert.IsNull (cct.GetContainerControl (), "IContainerControlTest.GetContainerControl");
  61. Button b = new Button ();
  62. b.Parent = cct;
  63. Assert.IsNull (b.GetContainerControl (), "Button.GetContainerControl/without parent");
  64. }
  65. [Test]
  66. [Category ("NotWorking")]
  67. public void ActiveControl ()
  68. {
  69. ContainerControl cc = new ContainerControl ();
  70. Control c1 = new Control ();
  71. cc.Controls.Add (c1);
  72. Control c2 = new Control ();
  73. cc.Controls.Add (c2);
  74. Control c3 = new Control ();
  75. cc.Controls.Add (c3);
  76. Assert.IsFalse (c1.Focused, "#A1");
  77. Assert.IsFalse (c2.Focused, "#A2");
  78. Assert.IsFalse (c3.Focused, "#A3");
  79. Assert.IsNull (cc.ActiveControl);
  80. cc.ActiveControl = c1;
  81. Assert.IsFalse (c1.Focused, "#B1");
  82. Assert.IsFalse (c2.Focused, "#B2");
  83. Assert.IsFalse (c3.Focused, "#B3");
  84. Assert.AreSame (c1, cc.ActiveControl, "#B4");
  85. cc.ActiveControl = c2;
  86. Assert.IsFalse (c1.Focused, "#C1");
  87. Assert.IsFalse (c2.Focused, "#C2");
  88. Assert.IsFalse (c3.Focused, "#C3");
  89. Assert.AreSame (c2, cc.ActiveControl, "#C4");
  90. c1.Focus ();
  91. Assert.IsFalse (c1.Focused, "#D1");
  92. Assert.IsFalse (c2.Focused, "#D2");
  93. Assert.IsFalse (c3.Focused, "#D3");
  94. Assert.AreSame (c2, cc.ActiveControl, "#D4");
  95. cc.ActiveControl = c2;
  96. Assert.IsFalse (c1.Focused, "#E1");
  97. Assert.IsFalse (c2.Focused, "#E2");
  98. Assert.IsFalse (c3.Focused, "#E3");
  99. Assert.AreSame (c2, cc.ActiveControl, "#E4");
  100. cc.Controls.Remove (c2);
  101. Assert.IsFalse (c1.Focused, "#F1");
  102. Assert.IsFalse (c2.Focused, "#F2");
  103. Assert.IsFalse (c3.Focused, "#F3");
  104. Assert.AreSame (c1, cc.ActiveControl, "#F3");
  105. cc.ActiveControl = c3;
  106. Assert.IsFalse (c1.Focused, "#G1");
  107. Assert.IsFalse (c2.Focused, "#G2");
  108. Assert.IsFalse (c3.Focused, "#G3");
  109. Assert.AreSame (c3, cc.ActiveControl, "#G4");
  110. Form form = new Form ();
  111. form.ShowInTaskbar = false;
  112. form.Controls.Add (cc);
  113. form.Show ();
  114. Assert.IsTrue (c1.Focused, "#H1");
  115. Assert.IsFalse (c2.Focused, "#H2");
  116. Assert.IsFalse (c3.Focused, "#H3");
  117. Assert.AreSame (c1, cc.ActiveControl, "#H4");
  118. cc.ActiveControl = c3;
  119. Assert.IsFalse (c1.Focused, "#I1");
  120. Assert.IsFalse (c2.Focused, "#I2");
  121. Assert.IsTrue (c3.Focused, "#I3");
  122. Assert.AreSame (c3, cc.ActiveControl, "#I4");
  123. c1.Focus ();
  124. Assert.IsTrue (c1.Focused, "#J1");
  125. Assert.IsFalse (c2.Focused, "#J2");
  126. Assert.IsFalse (c3.Focused, "#J3");
  127. Assert.AreSame (c1, cc.ActiveControl, "#J4");
  128. }
  129. [Test] // bug #80411
  130. [Category ("NotWorking")]
  131. public void ActiveControl_NoChild ()
  132. {
  133. ContainerControl cc = new ContainerControl ();
  134. try {
  135. cc.ActiveControl = new Control ();
  136. Assert.Fail ("#1");
  137. } catch (ArgumentException ex) {
  138. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  139. Assert.IsNotNull (ex.Message, "#3");
  140. Assert.IsNull (ex.ParamName, "#4");
  141. Assert.IsNull (ex.InnerException, "#5");
  142. }
  143. }
  144. [Test]
  145. [Category ("NotWorking")]
  146. public void ActiveControl_Invisible ()
  147. {
  148. ContainerControl cc = new ContainerControl ();
  149. Control c1 = new Control ();
  150. c1.Visible = false;
  151. cc.Controls.Add (c1);
  152. Control c2 = new Control ();
  153. cc.Controls.Add (c2);
  154. cc.ActiveControl = c1;
  155. Assert.IsFalse (c1.Focused, "#A1");
  156. Assert.IsFalse (c2.Focused, "#A2");
  157. Assert.AreSame (c1, cc.ActiveControl, "#A3");
  158. Form form = new Form ();
  159. form.ShowInTaskbar = false;
  160. form.Controls.Add (cc);
  161. form.Show ();
  162. Assert.IsFalse (c1.Focused, "#B1");
  163. Assert.IsTrue (c2.Focused, "#B2");
  164. Assert.AreSame (c2, cc.ActiveControl, "#B3");
  165. cc.ActiveControl = c1;
  166. Assert.IsFalse (c1.Focused, "#C1");
  167. Assert.IsFalse (c2.Focused, "#C2");
  168. Assert.AreSame (c1, cc.ActiveControl, "#C3");
  169. }
  170. [Test]
  171. [Category ("NotWorking")]
  172. public void ActiveControl_Disabled ()
  173. {
  174. ContainerControl cc = new ContainerControl ();
  175. Control c1 = new Control ();
  176. c1.Enabled = false;
  177. cc.Controls.Add (c1);
  178. Control c2 = new Control ();
  179. cc.Controls.Add (c2);
  180. cc.ActiveControl = c1;
  181. Assert.IsFalse (c1.Focused, "#A1");
  182. Assert.IsFalse (c2.Focused, "#A2");
  183. Assert.AreSame (c1, cc.ActiveControl, "#A3");
  184. Form form = new Form ();
  185. form.ShowInTaskbar = false;
  186. form.Controls.Add (cc);
  187. form.Show ();
  188. Assert.IsFalse (c1.Focused, "#B1");
  189. Assert.IsTrue (c2.Focused, "#B2");
  190. Assert.AreSame (c2, cc.ActiveControl, "#B3");
  191. cc.ActiveControl = c1;
  192. Assert.IsFalse (c1.Focused, "#C1");
  193. Assert.IsTrue (c2.Focused, "#C2");
  194. Assert.AreSame (c1, cc.ActiveControl, "#C3");
  195. }
  196. [Test]
  197. [Category ("NotWorking")]
  198. public void ActiveControl_Null ()
  199. {
  200. ContainerControl cc = new ContainerControl ();
  201. Control c1 = new Control ();
  202. cc.Controls.Add (c1);
  203. Control c2 = new Control ();
  204. cc.Controls.Add (c2);
  205. cc.ActiveControl = c1;
  206. Assert.IsFalse (c1.Focused, "#A1");
  207. Assert.IsFalse (c2.Focused, "#A2");
  208. Assert.AreSame (c1, cc.ActiveControl, "#A3");
  209. cc.ActiveControl = null;
  210. Assert.IsFalse (c1.Focused, "#B1");
  211. Assert.IsFalse (c2.Focused, "#B2");
  212. Assert.IsNull (cc.ActiveControl, "#B3");
  213. Form form = new Form ();
  214. form.ShowInTaskbar = false;
  215. form.Controls.Add (cc);
  216. form.Show ();
  217. Assert.IsTrue (c1.Focused, "#C1");
  218. Assert.IsFalse (c2.Focused, "#C2");
  219. Assert.AreSame (c1, cc.ActiveControl, "#C3");
  220. cc.ActiveControl = c2;
  221. Assert.IsFalse (c1.Focused, "#D1");
  222. Assert.IsTrue (c2.Focused, "#D2");
  223. Assert.AreSame (c2, cc.ActiveControl, "#D3");
  224. cc.ActiveControl = null;
  225. Assert.IsFalse (c1.Focused, "#E1");
  226. Assert.IsFalse (c2.Focused, "#E2");
  227. Assert.IsNull (cc.ActiveControl, "#E3");
  228. }
  229. }
  230. }