ContainerControlTest.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. namespace MonoTests.System.Windows.Forms {
  35. public class IContainerControlTest : Control, IContainerControl {
  36. public bool ActivateControl (Control active)
  37. {
  38. return true;
  39. }
  40. public Control ActiveControl {
  41. get { return null; }
  42. set { ; }
  43. }
  44. }
  45. public class FormCustom: Form {
  46. public bool record;
  47. public Queue events;
  48. public FormCustom(string name, bool record, Queue events) {
  49. base.Name = name;
  50. this.record = record;
  51. this.events = events;
  52. }
  53. protected override void OnValidating(C.CancelEventArgs e) {
  54. if (this.record)
  55. events.Enqueue(String.Format("{0}:{1}:OnValidating", this, this.Name));
  56. base.OnValidating (e);
  57. }
  58. protected override void OnValidated(EventArgs e) {
  59. if (this.record)
  60. events.Enqueue(String.Format("{0}:{1}:OnValidated", this, this.Name));
  61. base.OnValidated (e);
  62. }
  63. protected override void OnGotFocus(EventArgs e) {
  64. if (this.record)
  65. events.Enqueue(String.Format("{0}:{1}:OnGotFocus", this, this.Name));
  66. base.OnGotFocus (e);
  67. }
  68. }
  69. public class ContainerControlCustom: ContainerControl {
  70. public bool record;
  71. public Queue events;
  72. public ContainerControlCustom(string name, bool record, Queue events) {
  73. base.Name = name;
  74. this.record = record;
  75. this.events = events;
  76. }
  77. protected override void OnValidating(C.CancelEventArgs e) {
  78. if (this.record)
  79. events.Enqueue(String.Format("{0}:{1}:OnValidating", this, this.Name));
  80. base.OnValidating (e);
  81. }
  82. protected override void OnValidated(EventArgs e) {
  83. if (this.record)
  84. events.Enqueue(String.Format("{0}:{1}:OnValidated", this, this.Name));
  85. base.OnValidated (e);
  86. }
  87. protected override void OnGotFocus(EventArgs e) {
  88. if (this.record)
  89. events.Enqueue(String.Format("{0}:{1}:OnGotFocus", this, this.Name));
  90. base.OnGotFocus (e);
  91. }
  92. protected override void Select(bool directed, bool forward) {
  93. if (this.record)
  94. events.Enqueue(String.Format("{0}:{1}:Select", this, this.Name));
  95. base.Select (directed, forward);
  96. }
  97. }
  98. public class UserControlCustom: UserControl {
  99. public bool record;
  100. public Queue events;
  101. public UserControlCustom(string name, bool record, Queue events) {
  102. base.Name = name;
  103. this.record = record;
  104. this.events = events;
  105. }
  106. protected override void OnValidating(C.CancelEventArgs e) {
  107. if (this.record)
  108. events.Enqueue(String.Format("{0}:{1}:OnValidating", this, this.Name));
  109. base.OnValidating (e);
  110. }
  111. protected override void OnValidated(EventArgs e) {
  112. if (this.record)
  113. events.Enqueue(String.Format("{0}:{1}:OnValidated", this, this.Name));
  114. base.OnValidated (e);
  115. }
  116. protected override void OnGotFocus(EventArgs e) {
  117. if (this.record)
  118. events.Enqueue(String.Format("{0}:{1}:OnGotFocus", this, this.Name));
  119. base.OnGotFocus (e);
  120. }
  121. protected override void Select(bool directed, bool forward) {
  122. if (this.record)
  123. events.Enqueue(String.Format("{0}:{1}:Select", this, this.Name));
  124. base.Select (directed, forward);
  125. }
  126. }
  127. [TestFixture]
  128. [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
  129. public class ContainerControlTest {
  130. [Test]
  131. public void RemoveActiveControlTest ()
  132. {
  133. ContainerControl cc = new ContainerControl();
  134. TextBox txt = new TextBox ();
  135. cc.Controls.Add (txt);
  136. Assert.IsFalse (cc.ActiveControl == txt, "#01");
  137. cc.ActiveControl = txt;
  138. Assert.AreSame (cc.ActiveControl, txt, "#02");
  139. cc.Controls.Remove (txt);
  140. Assert.IsNull (cc.ActiveControl, "#03");
  141. }
  142. [Test]
  143. public void GetContainerControl ()
  144. {
  145. ContainerControl cc = new ContainerControl ();
  146. Assert.IsTrue (Object.ReferenceEquals (cc, cc.GetContainerControl ()), "ContainerControl.GetContainerControl");
  147. Button b = new Button ();
  148. Assert.IsNull (b.GetContainerControl (), "Button.GetContainerControl/without parent");
  149. b.Parent = cc;
  150. Assert.IsTrue (Object.ReferenceEquals (cc, b.GetContainerControl ()), "Button.GetContainerControl");
  151. }
  152. [Test]
  153. public void GetContainerControl_WithoutStyle ()
  154. {
  155. IContainerControlTest cct = new IContainerControlTest ();
  156. Assert.IsNull (cct.GetContainerControl (), "IContainerControlTest.GetContainerControl");
  157. Button b = new Button ();
  158. b.Parent = cct;
  159. Assert.IsNull (b.GetContainerControl (), "Button.GetContainerControl/without parent");
  160. }
  161. [Test]
  162. [ExpectedException (typeof (ArgumentException))]
  163. public void ActiveControlNotChildTest ()
  164. {
  165. ContainerControl c = new ContainerControl ();
  166. c.ActiveControl = new Control ();
  167. }
  168. [Test]
  169. public void Validation() {
  170. Queue events = new Queue();
  171. FormCustom form = new FormCustom("form1", true, events);
  172. ContainerControlCustom container1 = new ContainerControlCustom("container1", true, events);
  173. ContainerControlCustom container2 = new ContainerControlCustom("container2", true, events);
  174. ContainerControlCustom container3 = new ContainerControlCustom("container3", true, events);
  175. UserControlCustom userctl1 = new UserControlCustom("userctl1", true, events);
  176. UserControlCustom userctl2 = new UserControlCustom("userctl2", true, events);
  177. UserControlCustom userctl3 = new UserControlCustom("userctl3", true, events);
  178. container2.Controls.Add(userctl2);
  179. container2.Controls.Add(userctl3);
  180. container1.Controls.Add(userctl1);
  181. form.Controls.Add(container1);
  182. form.Controls.Add(container2);
  183. form.Controls.Add(container3);
  184. form.Show();
  185. object s;
  186. events.Enqueue("START");
  187. container3.Select();
  188. events.Enqueue("END");
  189. events.Enqueue("START");
  190. container1.Select();
  191. events.Enqueue("END");
  192. events.Enqueue("START");
  193. container2.Select();
  194. events.Enqueue("END");
  195. events.Enqueue("START");
  196. userctl1.Select();
  197. events.Enqueue("END");
  198. events.Enqueue("START");
  199. userctl2.Select();
  200. events.Enqueue("END");
  201. events.Enqueue("START");
  202. userctl2.Select();
  203. events.Enqueue("END");
  204. while (events.Count > 0) {
  205. s = events.Dequeue();
  206. Console.WriteLine(s.ToString());
  207. }
  208. events.Clear();
  209. form.Close();
  210. userctl1.Dispose();
  211. userctl2.Dispose();
  212. userctl3.Dispose();
  213. container1.Dispose();
  214. container1.Dispose();
  215. form.Dispose();
  216. }
  217. }
  218. }