ApplicationContextTest.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. class MyForm : Form
  18. {
  19. public void DoDestroyHandle ()
  20. {
  21. DestroyHandle();
  22. }
  23. }
  24. [TestFixture]
  25. public class ApplicationContextTest
  26. {
  27. ApplicationContext ctx;
  28. int thread_exit_count;
  29. bool reached_form_handle_destroyed;
  30. void thread_exit (object sender, EventArgs e)
  31. {
  32. thread_exit_count++;
  33. }
  34. void form_handle_destroyed (object sender, EventArgs e)
  35. {
  36. Assert.AreEqual (0, thread_exit_count, "1");
  37. Assert.AreEqual (sender, ctx.MainForm, "2");
  38. reached_form_handle_destroyed = true;
  39. }
  40. void form_handle_destroyed2 (object sender, EventArgs e)
  41. {
  42. Assert.AreEqual (1, thread_exit_count, "1");
  43. Assert.AreEqual (sender, ctx.MainForm, "2");
  44. reached_form_handle_destroyed = true;
  45. }
  46. [Test]
  47. public void TestEventOrdering ()
  48. {
  49. thread_exit_count = 0;
  50. reached_form_handle_destroyed = false;
  51. MyForm f1 = new MyForm ();
  52. f1.HandleDestroyed += new EventHandler (form_handle_destroyed);
  53. ctx = new ApplicationContext (f1);
  54. ctx.ThreadExit += new EventHandler (thread_exit);
  55. f1.Show ();
  56. f1.DoDestroyHandle ();
  57. Assert.AreEqual (true, reached_form_handle_destroyed, "3");
  58. Assert.AreEqual (1, thread_exit_count, "4");
  59. }
  60. [Test]
  61. public void TestEventOrdering2 ()
  62. {
  63. thread_exit_count = 0;
  64. reached_form_handle_destroyed = false;
  65. MyForm f1 = new MyForm ();
  66. ctx = new ApplicationContext (f1);
  67. ctx.ThreadExit += new EventHandler (thread_exit);
  68. f1.HandleDestroyed += new EventHandler (form_handle_destroyed2);
  69. f1.Show ();
  70. f1.DoDestroyHandle ();
  71. Assert.AreEqual (true, reached_form_handle_destroyed, "3");
  72. Assert.AreEqual (1, thread_exit_count, "4");
  73. }
  74. [Test]
  75. public void ThreadExitTest ()
  76. {
  77. thread_exit_count = 0;
  78. MyForm f1 = new MyForm ();
  79. ctx = new ApplicationContext (f1);
  80. ctx.ThreadExit += new EventHandler (thread_exit);
  81. Assert.AreEqual (f1, ctx.MainForm, "1");
  82. f1.ShowInTaskbar = false;
  83. f1.Show ();
  84. f1.Dispose ();
  85. Assert.AreEqual (f1, ctx.MainForm, "2");
  86. Assert.AreEqual (1, thread_exit_count, "3");
  87. f1 = new MyForm ();
  88. ctx = new ApplicationContext (f1);
  89. ctx.ThreadExit += new EventHandler (thread_exit);
  90. f1.ShowInTaskbar = false;
  91. f1.Show ();
  92. f1.DoDestroyHandle ();
  93. Assert.AreEqual (f1, ctx.MainForm, "4");
  94. Assert.AreEqual (2, thread_exit_count, "5");
  95. }
  96. }
  97. }