ApplicationContextTest.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. using CategoryAttribute=NUnit.Framework.CategoryAttribute;
  16. namespace MonoTests.System.Windows.Forms
  17. {
  18. class MyForm : Form
  19. {
  20. public void DoDestroyHandle ()
  21. {
  22. DestroyHandle();
  23. }
  24. }
  25. [TestFixture]
  26. public class ApplicationContextTest
  27. {
  28. ApplicationContext ctx;
  29. int thread_exit_count;
  30. bool reached_form_handle_destroyed;
  31. void thread_exit (object sender, EventArgs e)
  32. {
  33. thread_exit_count++;
  34. }
  35. void form_handle_destroyed (object sender, EventArgs e)
  36. {
  37. Assert.AreEqual (0, thread_exit_count, "1");
  38. Assert.AreEqual (sender, ctx.MainForm, "2");
  39. reached_form_handle_destroyed = true;
  40. }
  41. void form_handle_destroyed2 (object sender, EventArgs e)
  42. {
  43. Assert.AreEqual (1, thread_exit_count, "1");
  44. Assert.AreEqual (sender, ctx.MainForm, "2");
  45. reached_form_handle_destroyed = true;
  46. }
  47. [Test]
  48. public void TestEventOrdering ()
  49. {
  50. thread_exit_count = 0;
  51. reached_form_handle_destroyed = false;
  52. MyForm f1 = new MyForm ();
  53. f1.ShowInTaskbar = false;
  54. f1.HandleDestroyed += new EventHandler (form_handle_destroyed);
  55. ctx = new ApplicationContext (f1);
  56. ctx.ThreadExit += new EventHandler (thread_exit);
  57. f1.Show ();
  58. f1.DoDestroyHandle ();
  59. Assert.AreEqual (true, reached_form_handle_destroyed, "3");
  60. Assert.AreEqual (1, thread_exit_count, "4");
  61. }
  62. [Test]
  63. public void TestEventOrdering2 ()
  64. {
  65. thread_exit_count = 0;
  66. reached_form_handle_destroyed = false;
  67. MyForm f1 = new MyForm ();
  68. f1.ShowInTaskbar = false;
  69. ctx = new ApplicationContext (f1);
  70. ctx.ThreadExit += new EventHandler (thread_exit);
  71. f1.HandleDestroyed += new EventHandler (form_handle_destroyed2);
  72. f1.Show ();
  73. f1.DoDestroyHandle ();
  74. Assert.AreEqual (true, reached_form_handle_destroyed, "3");
  75. Assert.AreEqual (1, thread_exit_count, "4");
  76. }
  77. [Test]
  78. public void ThreadExitTest ()
  79. {
  80. thread_exit_count = 0;
  81. MyForm f1 = new MyForm ();
  82. f1.ShowInTaskbar = false;
  83. ctx = new ApplicationContext (f1);
  84. ctx.ThreadExit += new EventHandler (thread_exit);
  85. Assert.AreEqual (f1, ctx.MainForm, "1");
  86. f1.ShowInTaskbar = false;
  87. f1.Show ();
  88. f1.Dispose ();
  89. Assert.AreEqual (f1, ctx.MainForm, "2");
  90. Assert.AreEqual (1, thread_exit_count, "3");
  91. f1 = new MyForm ();
  92. ctx = new ApplicationContext (f1);
  93. ctx.ThreadExit += new EventHandler (thread_exit);
  94. f1.ShowInTaskbar = false;
  95. f1.Show ();
  96. f1.DoDestroyHandle ();
  97. Assert.AreEqual (f1, ctx.MainForm, "4");
  98. Assert.AreEqual (2, thread_exit_count, "5");
  99. }
  100. [Test]
  101. [Category ("NotWorking")]
  102. [ExpectedException (typeof (InvalidOperationException))]
  103. public void NestedApplicationContextTest ()
  104. {
  105. using (NestedForm frm = new NestedForm ()) {
  106. Application.Run (frm);
  107. }
  108. }
  109. private class NestedForm : Form
  110. {
  111. static int counter = 1;
  112. protected override void OnVisibleChanged (EventArgs e)
  113. {
  114. base.OnVisibleChanged (e);
  115. Text = counter.ToString ();
  116. if (counter <= 3) {
  117. counter++;
  118. Application.Run (new NestedForm ());
  119. }
  120. Close ();
  121. }
  122. }
  123. }
  124. }