ApplicationTest.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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
  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. protected override void OnActivated (EventArgs e)
  150. {
  151. Assert.AreEqual (true, IsInOpenForms (), "#OnActivated-A");
  152. base.OnActivated (e);
  153. Assert.AreEqual (true, IsInOpenForms (), "#OnActivated-B");
  154. }
  155. protected override void OnClosed (EventArgs e)
  156. {
  157. Assert.AreEqual (have_been_opened, IsInOpenForms (), "#OnClosed-A");
  158. base.OnClosed (e);
  159. Assert.AreEqual (have_been_opened, IsInOpenForms (), "#OnClosed-B");
  160. }
  161. protected override void OnClosing (CancelEventArgs e)
  162. {
  163. Assert.AreEqual (have_been_opened, IsInOpenForms (), "#OnClosing-A");
  164. base.OnClosing (e);
  165. Assert.AreEqual (have_been_opened, IsInOpenForms (), "#OnClosing-B");
  166. }
  167. protected override void OnDeactivate (EventArgs e)
  168. {
  169. Assert.AreEqual (true, IsInOpenForms (), "#OnDeactivate-A");
  170. base.OnDeactivate (e);
  171. Assert.AreEqual (true, IsInOpenForms (), "#OnDeactivate-B");
  172. }
  173. protected override void OnFormClosed (FormClosedEventArgs e)
  174. {
  175. Assert.AreEqual (have_been_opened, IsInOpenForms (), "#OnFormClosed-A");
  176. base.OnFormClosed (e);
  177. Assert.AreEqual (false, IsInOpenForms (), "#OnFormClosed-B");
  178. }
  179. protected override void OnFormClosing (FormClosingEventArgs e)
  180. {
  181. Assert.AreEqual (have_been_opened, IsInOpenForms (), "#OnFormClosing-A");
  182. base.OnFormClosing (e);
  183. Assert.AreEqual (have_been_opened, IsInOpenForms (), "#OnFormClosing-B");
  184. }
  185. protected override void OnHandleCreated (EventArgs e)
  186. {
  187. Assert.AreEqual (false, IsInOpenForms (), "#OnHandleCreated-A");
  188. base.OnHandleCreated (e);
  189. Assert.AreEqual (false, IsInOpenForms (), "#OnHandleCreated-B");
  190. }
  191. protected override void OnHandleDestroyed (EventArgs e)
  192. {
  193. //Assert.AreEqual (have_been_opened, IsInOpenForms (), "#OnHandleDestroyed-A");
  194. base.OnHandleDestroyed (e);
  195. Assert.AreEqual (false, IsInOpenForms (), "#OnHandleDestroyed-B");
  196. }
  197. protected override void SetVisibleCore (bool value)
  198. {
  199. if (value)
  200. Assert.AreEqual (false && value, IsInOpenForms (), "#SetVisibleCore-A");
  201. base.SetVisibleCore (value);
  202. Assert.AreEqual (true, IsInOpenForms (), "#SetVisibleCore-B");
  203. }
  204. protected override void OnVisibleChanged (EventArgs e)
  205. {
  206. Assert.AreEqual (true, IsInOpenForms (), "#OnVisibleChanged-A");
  207. base.OnVisibleChanged (e);
  208. Assert.AreEqual (true, IsInOpenForms (), "#OnVisibleChanged-B");
  209. }
  210. protected override void CreateHandle ()
  211. {
  212. Assert.AreEqual (false, IsInOpenForms (), "#CreateHandle-A");
  213. base.CreateHandle ();
  214. // We have a different stack trace here, so we're not matching MS
  215. // Assert.AreEqual (false, IsInOpenForms (), "#CreateHandle-B");
  216. }
  217. protected override void DestroyHandle ()
  218. {
  219. //Dispose may be called several times, so this isn't correct
  220. //Assert.AreEqual (have_been_opened, IsInOpenForms (), "#DestroyHandle-A");
  221. base.DestroyHandle ();
  222. Assert.AreEqual (false, IsInOpenForms (), "#DestroyHandle-B");
  223. }
  224. protected override void Dispose (bool disposing)
  225. {
  226. //Dispose may be called several times, so this isn't correct
  227. //Assert.AreEqual (have_been_opened, IsInOpenForms (), "#Dispose-A");
  228. base.Dispose (disposing);
  229. Assert.AreEqual (false, IsInOpenForms (), "#Dispose-B");
  230. }
  231. }
  232. #endif
  233. }
  234. }