ApplicationContextTest.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 : TestHelper
  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. f1.Dispose ();
  62. }
  63. [Test]
  64. public void TestEventOrdering2 ()
  65. {
  66. thread_exit_count = 0;
  67. reached_form_handle_destroyed = false;
  68. MyForm f1 = new MyForm ();
  69. f1.ShowInTaskbar = false;
  70. ctx = new ApplicationContext (f1);
  71. ctx.ThreadExit += new EventHandler (thread_exit);
  72. f1.HandleDestroyed += new EventHandler (form_handle_destroyed2);
  73. f1.Show ();
  74. f1.DoDestroyHandle ();
  75. Assert.AreEqual (true, reached_form_handle_destroyed, "3");
  76. Assert.AreEqual (1, thread_exit_count, "4");
  77. f1.Dispose ();
  78. }
  79. [Test]
  80. public void ThreadExitTest ()
  81. {
  82. thread_exit_count = 0;
  83. MyForm f1 = new MyForm ();
  84. f1.ShowInTaskbar = false;
  85. ctx = new ApplicationContext (f1);
  86. ctx.ThreadExit += new EventHandler (thread_exit);
  87. Assert.AreEqual (f1, ctx.MainForm, "1");
  88. f1.ShowInTaskbar = false;
  89. f1.Show ();
  90. f1.Dispose ();
  91. Assert.AreEqual (f1, ctx.MainForm, "2");
  92. Assert.AreEqual (1, thread_exit_count, "3");
  93. f1 = new MyForm ();
  94. ctx = new ApplicationContext (f1);
  95. ctx.ThreadExit += new EventHandler (thread_exit);
  96. f1.ShowInTaskbar = false;
  97. f1.Show ();
  98. f1.DoDestroyHandle ();
  99. Assert.AreEqual (f1, ctx.MainForm, "4");
  100. Assert.AreEqual (2, thread_exit_count, "5");
  101. f1.Dispose ();
  102. }
  103. [Test]
  104. [Category ("NotWorking")]
  105. [ExpectedException (typeof (InvalidOperationException))]
  106. public void NestedApplicationContextTest ()
  107. {
  108. using (NestedForm frm = new NestedForm ()) {
  109. Application.Run (frm);
  110. }
  111. }
  112. private class NestedForm : Form
  113. {
  114. static int counter = 1;
  115. protected override void OnVisibleChanged (EventArgs e)
  116. {
  117. base.OnVisibleChanged (e);
  118. Text = counter.ToString ();
  119. if (counter <= 3) {
  120. counter++;
  121. Application.Run (new NestedForm ());
  122. }
  123. Close ();
  124. }
  125. }
  126. }
  127. }