ApplicationTest.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. //
  2. // ApplicationContextTest.cs
  3. //
  4. // Author:
  5. // Chris Toshok ([email protected])
  6. //
  7. // (C) 2006 Novell, Inc. (http://www.novell.com)
  8. //
  9. using System;
  10. using System.ComponentModel;
  11. using System.Windows.Forms;
  12. using System.Drawing;
  13. using System.Reflection;
  14. using NUnit.Framework;
  15. namespace MonoTests.System.Windows.Forms
  16. {
  17. [TestFixture]
  18. public class ApplicationTest : TestHelper
  19. {
  20. ApplicationContext ctx;
  21. void form_visible_changed (object sender, EventArgs e)
  22. {
  23. Assert.AreEqual (sender, ctx.MainForm, "1");
  24. ((Form)sender).Close();
  25. }
  26. [Test]
  27. public void ContextMainFormTest ()
  28. {
  29. Form f1 = new Form ();
  30. f1.ShowInTaskbar = false;
  31. ctx = new ApplicationContext (f1);
  32. f1.VisibleChanged += new EventHandler (form_visible_changed);
  33. Application.Run (ctx);
  34. Assert.IsNull (ctx.MainForm, "2");
  35. f1.Dispose ();
  36. }
  37. #if NET_2_0
  38. [Test]
  39. [ExpectedException (typeof (NotSupportedException))]
  40. public void RestartNotSupportedExceptionTest ()
  41. {
  42. Application.Restart ();
  43. }
  44. [Test]
  45. public void OpenFormsTest ()
  46. {
  47. IntPtr dummy = IntPtr.Zero;
  48. Form f1 = null, f2 = null;
  49. try {
  50. f1 = new OpenFormsTestForm ();
  51. Assert.AreEqual (0, Application.OpenForms.Count, "#1");
  52. dummy = f1.Handle;
  53. Assert.AreEqual (0, Application.OpenForms.Count, "#2");
  54. f1.Close ();
  55. Assert.AreEqual (0, Application.OpenForms.Count, "#3");
  56. f1.Dispose ();
  57. Assert.AreEqual (0, Application.OpenForms.Count, "#4");
  58. f1 = new OpenFormsTestForm ();
  59. Assert.AreEqual (0, Application.OpenForms.Count, "#5");
  60. f1.Show ();
  61. Assert.AreEqual (1, Application.OpenForms.Count, "#6");
  62. f1.Close ();
  63. Assert.AreEqual (0, Application.OpenForms.Count, "#7");
  64. f1.Dispose ();
  65. Assert.AreEqual (0, Application.OpenForms.Count, "#8");
  66. f1 = new OpenFormsTestForm ();
  67. Assert.AreEqual (0, Application.OpenForms.Count, "#9");
  68. dummy = f1.Handle;
  69. Assert.AreEqual (0, Application.OpenForms.Count, "#10");
  70. f1.GetType ().GetMethod ("RecreateHandle", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.ExactBinding).Invoke (f1, new object [] {});
  71. Assert.AreEqual (0, Application.OpenForms.Count, "#11");
  72. f1.Dispose ();
  73. Assert.AreEqual (0, Application.OpenForms.Count, "#12");
  74. f1 = new OpenFormsTestForm ();
  75. Assert.AreEqual (0, Application.OpenForms.Count, "#13");
  76. f1.Show ();
  77. Assert.AreEqual (1, Application.OpenForms.Count, "#14");
  78. f2 = new OpenFormsTestForm ();
  79. Assert.AreEqual (1, Application.OpenForms.Count, "#15");
  80. f2.Show ();
  81. Assert.AreEqual (2, Application.OpenForms.Count, "#16");
  82. f1.Dispose ();
  83. Assert.AreEqual (1, Application.OpenForms.Count, "#17");
  84. f2.Close ();
  85. Assert.AreEqual (0, Application.OpenForms.Count, "#18");
  86. f1 = new OpenFormsTestForm ();
  87. Assert.AreEqual (0, Application.OpenForms.Count, "#19");
  88. f1.Show ();
  89. Assert.AreEqual (1, Application.OpenForms.Count, "#20");
  90. f2 = new OpenFormsTestForm ();
  91. Assert.AreEqual (1, Application.OpenForms.Count, "#21");
  92. f2.Show ();
  93. Assert.AreEqual (2, Application.OpenForms.Count, "#22");
  94. f1.Visible = false;
  95. Assert.AreEqual (2, Application.OpenForms.Count, "#23");
  96. f2.Visible = false;
  97. Assert.AreEqual (2, Application.OpenForms.Count, "#24");
  98. f1.Dispose ();
  99. Assert.AreEqual (1, Application.OpenForms.Count, "#25");
  100. f2.Close ();
  101. Assert.AreEqual (0, Application.OpenForms.Count, "#26");
  102. } finally {
  103. if (f1 != null) {
  104. f1.Dispose ();
  105. }
  106. if (f2 != null) {
  107. f2.Dispose ();
  108. }
  109. }
  110. TestHelper.RemoveWarning (dummy);
  111. }
  112. [Test]
  113. public void MethodRaiseIdle ()
  114. {
  115. bool idle_raised = false;
  116. Application.Idle += new EventHandler (delegate (Object obj, EventArgs e) { idle_raised = true; });
  117. Application.RaiseIdle (EventArgs.Empty);
  118. Assert.AreEqual (true, idle_raised, "R1");
  119. }
  120. void Application_Idle (object sender, EventArgs e)
  121. {
  122. throw new Exception ("The method or operation is not implemented.");
  123. }
  124. class OpenFormsTestForm : Form
  125. {
  126. public bool have_been_opened;
  127. public bool IsInOpenForms ()
  128. {
  129. foreach (Form form in Application.OpenForms) {
  130. if (form == this) {
  131. have_been_opened = true;
  132. return true;
  133. }
  134. }
  135. return false;
  136. }
  137. protected override void OnLoad (EventArgs e)
  138. {
  139. Assert.AreEqual (false, IsInOpenForms (), "#OnLoad-A");
  140. base.OnLoad (e);
  141. Assert.AreEqual (true, IsInOpenForms (), "#OnLoad-B");
  142. }
  143. protected override void OnCreateControl ()
  144. {
  145. Assert.AreEqual (false, IsInOpenForms (), "#OnCreateControl-A");
  146. base.OnCreateControl ();
  147. Assert.AreEqual (true, IsInOpenForms (), "#OnCreateControl-B");
  148. }
  149. // Activation may not be synchronous, causing too many false positives
  150. protected override void OnActivated (EventArgs e)
  151. {
  152. bool dummy = IsInOpenForms ();
  153. //Assert.AreEqual (true, IsInOpenForms (), "#OnActivated-A");
  154. base.OnActivated (e);
  155. dummy = IsInOpenForms ();
  156. //Assert.AreEqual (true, IsInOpenForms (), "#OnActivated-B");
  157. }
  158. protected override void OnClosed (EventArgs e)
  159. {
  160. Assert.AreEqual (have_been_opened, IsInOpenForms (), "#OnClosed-A");
  161. base.OnClosed (e);
  162. Assert.AreEqual (have_been_opened, IsInOpenForms (), "#OnClosed-B");
  163. }
  164. protected override void OnClosing (CancelEventArgs e)
  165. {
  166. Assert.AreEqual (have_been_opened, IsInOpenForms (), "#OnClosing-A");
  167. base.OnClosing (e);
  168. Assert.AreEqual (have_been_opened, IsInOpenForms (), "#OnClosing-B");
  169. }
  170. protected override void OnDeactivate (EventArgs e)
  171. {
  172. Assert.AreEqual (true, IsInOpenForms (), "#OnDeactivate-A");
  173. base.OnDeactivate (e);
  174. Assert.AreEqual (true, IsInOpenForms (), "#OnDeactivate-B");
  175. }
  176. protected override void OnFormClosed (FormClosedEventArgs e)
  177. {
  178. Assert.AreEqual (have_been_opened, IsInOpenForms (), "#OnFormClosed-A");
  179. base.OnFormClosed (e);
  180. Assert.AreEqual (false, IsInOpenForms (), "#OnFormClosed-B");
  181. }
  182. protected override void OnFormClosing (FormClosingEventArgs e)
  183. {
  184. Assert.AreEqual (have_been_opened, IsInOpenForms (), "#OnFormClosing-A");
  185. base.OnFormClosing (e);
  186. Assert.AreEqual (have_been_opened, IsInOpenForms (), "#OnFormClosing-B");
  187. }
  188. protected override void OnHandleCreated (EventArgs e)
  189. {
  190. Assert.AreEqual (false, IsInOpenForms (), "#OnHandleCreated-A");
  191. base.OnHandleCreated (e);
  192. Assert.AreEqual (false, IsInOpenForms (), "#OnHandleCreated-B");
  193. }
  194. protected override void OnHandleDestroyed (EventArgs e)
  195. {
  196. //Assert.AreEqual (have_been_opened, IsInOpenForms (), "#OnHandleDestroyed-A");
  197. base.OnHandleDestroyed (e);
  198. Assert.AreEqual (false, IsInOpenForms (), "#OnHandleDestroyed-B");
  199. }
  200. protected override void SetVisibleCore (bool value)
  201. {
  202. if (value)
  203. Assert.AreEqual (false && value, IsInOpenForms (), "#SetVisibleCore-A");
  204. base.SetVisibleCore (value);
  205. Assert.AreEqual (true, IsInOpenForms (), "#SetVisibleCore-B");
  206. }
  207. protected override void OnVisibleChanged (EventArgs e)
  208. {
  209. Assert.AreEqual (true, IsInOpenForms (), "#OnVisibleChanged-A");
  210. base.OnVisibleChanged (e);
  211. Assert.AreEqual (true, IsInOpenForms (), "#OnVisibleChanged-B");
  212. }
  213. protected override void CreateHandle ()
  214. {
  215. Assert.AreEqual (false, IsInOpenForms (), "#CreateHandle-A");
  216. base.CreateHandle ();
  217. // We have a different stack trace here, so we're not matching MS
  218. // Assert.AreEqual (false, IsInOpenForms (), "#CreateHandle-B");
  219. }
  220. protected override void DestroyHandle ()
  221. {
  222. //Dispose may be called several times, so this isn't correct
  223. //Assert.AreEqual (have_been_opened, IsInOpenForms (), "#DestroyHandle-A");
  224. base.DestroyHandle ();
  225. Assert.AreEqual (false, IsInOpenForms (), "#DestroyHandle-B");
  226. }
  227. protected override void Dispose (bool disposing)
  228. {
  229. //Dispose may be called several times, so this isn't correct
  230. //Assert.AreEqual (have_been_opened, IsInOpenForms (), "#Dispose-A");
  231. base.Dispose (disposing);
  232. Assert.AreEqual (false, IsInOpenForms (), "#Dispose-B");
  233. }
  234. }
  235. #endif
  236. }
  237. }