2 // ApplicationContextTest.cs
5 // Chris Toshok (toshok@ximian.com)
7 // (C) 2006 Novell, Inc. (http://www.novell.com)
11 using System.ComponentModel;
12 using System.Windows.Forms;
14 using System.Reflection;
15 using NUnit.Framework;
16 using CategoryAttribute=NUnit.Framework.CategoryAttribute;
18 namespace MonoTests.System.Windows.Forms
22 public void DoDestroyHandle ()
30 public class ApplicationContextTest : TestHelper
32 ApplicationContext ctx;
33 int thread_exit_count;
34 bool reached_form_handle_destroyed;
36 void thread_exit (object sender, EventArgs e)
41 void form_handle_destroyed (object sender, EventArgs e)
43 Assert.AreEqual (0, thread_exit_count, "1");
44 Assert.AreEqual (sender, ctx.MainForm, "2");
45 reached_form_handle_destroyed = true;
48 void form_handle_destroyed2 (object sender, EventArgs e)
50 Assert.AreEqual (1, thread_exit_count, "1");
51 Assert.AreEqual (sender, ctx.MainForm, "2");
52 reached_form_handle_destroyed = true;
56 public void TestEventOrdering ()
58 thread_exit_count = 0;
59 reached_form_handle_destroyed = false;
61 MyForm f1 = new MyForm ();
62 f1.ShowInTaskbar = false;
63 f1.HandleDestroyed += new EventHandler (form_handle_destroyed);
65 ctx = new ApplicationContext (f1);
66 ctx.ThreadExit += new EventHandler (thread_exit);
69 f1.DoDestroyHandle ();
71 Assert.AreEqual (true, reached_form_handle_destroyed, "3");
72 Assert.AreEqual (1, thread_exit_count, "4");
78 public void TestEventOrdering2 ()
80 thread_exit_count = 0;
81 reached_form_handle_destroyed = false;
83 MyForm f1 = new MyForm ();
84 f1.ShowInTaskbar = false;
86 ctx = new ApplicationContext (f1);
87 ctx.ThreadExit += new EventHandler (thread_exit);
89 f1.HandleDestroyed += new EventHandler (form_handle_destroyed2);
92 f1.DoDestroyHandle ();
93 Assert.AreEqual (true, reached_form_handle_destroyed, "3");
94 Assert.AreEqual (1, thread_exit_count, "4");
100 public void ThreadExitTest ()
102 thread_exit_count = 0;
104 MyForm f1 = new MyForm ();
105 f1.ShowInTaskbar = false;
106 ctx = new ApplicationContext (f1);
107 ctx.ThreadExit += new EventHandler (thread_exit);
109 Assert.AreEqual (f1, ctx.MainForm, "1");
110 f1.ShowInTaskbar = false;
113 Assert.AreEqual (f1, ctx.MainForm, "2");
114 Assert.AreEqual (1, thread_exit_count, "3");
117 ctx = new ApplicationContext (f1);
118 ctx.ThreadExit += new EventHandler (thread_exit);
119 f1.ShowInTaskbar = false;
121 f1.DoDestroyHandle ();
122 Assert.AreEqual (f1, ctx.MainForm, "4");
123 Assert.AreEqual (2, thread_exit_count, "5");
128 [Category ("NotWorking")]
129 [ExpectedException (typeof (InvalidOperationException))]
130 public void NestedApplicationContextTest ()
132 using (NestedForm frm = new NestedForm ()) {
133 Application.Run (frm);
137 private class NestedForm : Form
139 static int counter = 1;
140 protected override void OnVisibleChanged (EventArgs e)
142 base.OnVisibleChanged (e);
144 Text = counter.ToString ();
148 Application.Run (new NestedForm ());