ContainerControlTest.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 GetContainerControl ()
  132. {
  133. ContainerControl cc = new ContainerControl ();
  134. Assert.IsTrue (Object.ReferenceEquals (cc, cc.GetContainerControl ()), "ContainerControl.GetContainerControl");
  135. Button b = new Button ();
  136. Assert.IsNull (b.GetContainerControl (), "Button.GetContainerControl/without parent");
  137. b.Parent = cc;
  138. Assert.IsTrue (Object.ReferenceEquals (cc, b.GetContainerControl ()), "Button.GetContainerControl");
  139. }
  140. [Test]
  141. public void GetContainerControl_WithoutStyle ()
  142. {
  143. IContainerControlTest cct = new IContainerControlTest ();
  144. Assert.IsNull (cct.GetContainerControl (), "IContainerControlTest.GetContainerControl");
  145. Button b = new Button ();
  146. b.Parent = cct;
  147. Assert.IsNull (b.GetContainerControl (), "Button.GetContainerControl/without parent");
  148. }
  149. [Test]
  150. [ExpectedException (typeof (ArgumentException))]
  151. public void ActiveControlNotChildTest ()
  152. {
  153. ContainerControl c = new ContainerControl ();
  154. c.ActiveControl = new Control ();
  155. }
  156. [Test]
  157. public void Validation() {
  158. Queue events = new Queue();
  159. FormCustom form = new FormCustom("form1", true, events);
  160. ContainerControlCustom container1 = new ContainerControlCustom("container1", true, events);
  161. ContainerControlCustom container2 = new ContainerControlCustom("container2", true, events);
  162. ContainerControlCustom container3 = new ContainerControlCustom("container3", true, events);
  163. UserControlCustom userctl1 = new UserControlCustom("userctl1", true, events);
  164. UserControlCustom userctl2 = new UserControlCustom("userctl2", true, events);
  165. UserControlCustom userctl3 = new UserControlCustom("userctl3", true, events);
  166. container2.Controls.Add(userctl2);
  167. container2.Controls.Add(userctl3);
  168. container1.Controls.Add(userctl1);
  169. form.Controls.Add(container1);
  170. form.Controls.Add(container2);
  171. form.Controls.Add(container3);
  172. form.Show();
  173. object s;
  174. events.Enqueue("START");
  175. container3.Select();
  176. events.Enqueue("END");
  177. events.Enqueue("START");
  178. container1.Select();
  179. events.Enqueue("END");
  180. events.Enqueue("START");
  181. container2.Select();
  182. events.Enqueue("END");
  183. events.Enqueue("START");
  184. userctl1.Select();
  185. events.Enqueue("END");
  186. events.Enqueue("START");
  187. userctl2.Select();
  188. events.Enqueue("END");
  189. events.Enqueue("START");
  190. userctl2.Select();
  191. events.Enqueue("END");
  192. while (events.Count > 0) {
  193. s = events.Dequeue();
  194. Console.WriteLine(s.ToString());
  195. }
  196. events.Clear();
  197. form.Close();
  198. userctl1.Dispose();
  199. userctl2.Dispose();
  200. userctl3.Dispose();
  201. container1.Dispose();
  202. container1.Dispose();
  203. form.Dispose();
  204. }
  205. }
  206. }