2006-12-19 Daniel Nauck <dna@mono-project.de>
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / ApplicationContextTest.cs
1 //
2 // ApplicationContextTest.cs
3 //
4 // Author:
5 //   Chris Toshok (toshok@ximian.com)
6 //
7 // (C) 2006 Novell, Inc. (http://www.novell.com)
8 //
9
10 using System;
11 using System.ComponentModel;
12 using System.Windows.Forms;
13 using System.Drawing;
14 using System.Reflection;
15 using NUnit.Framework;
16
17 namespace MonoTests.System.Windows.Forms
18 {
19         class MyForm : Form
20         {
21                 public void DoDestroyHandle ()
22                 {
23                         DestroyHandle();
24                 }
25         }
26
27
28         [TestFixture]
29         public class ApplicationContextTest
30         {
31                 ApplicationContext ctx;
32                 int thread_exit_count;
33                 bool reached_form_handle_destroyed;
34
35                 void thread_exit (object sender, EventArgs e)
36                 {
37                         thread_exit_count++;
38                 }
39
40                 void form_handle_destroyed (object sender, EventArgs e)
41                 {
42                         Assert.AreEqual (0, thread_exit_count, "1");
43                         Assert.AreEqual (sender, ctx.MainForm, "2");
44                         reached_form_handle_destroyed = true;
45                 }
46
47                 void form_handle_destroyed2 (object sender, EventArgs e)
48                 {
49                         Assert.AreEqual (1, thread_exit_count, "1");
50                         Assert.AreEqual (sender, ctx.MainForm, "2");
51                         reached_form_handle_destroyed = true;
52                 }
53
54                 [Test]
55                 public void TestEventOrdering ()
56                 {
57                         thread_exit_count = 0;
58                         reached_form_handle_destroyed = false;
59
60                         MyForm f1 = new MyForm ();
61                         f1.HandleDestroyed += new EventHandler (form_handle_destroyed);
62
63                         ctx = new ApplicationContext (f1);
64                         ctx.ThreadExit += new EventHandler (thread_exit);
65
66                         f1.Show ();
67                         f1.DoDestroyHandle ();
68
69                         Assert.AreEqual (true, reached_form_handle_destroyed, "3");
70                         Assert.AreEqual (1, thread_exit_count, "4");
71                 }
72
73                 [Test]
74                 public void TestEventOrdering2 ()
75                 {
76                         thread_exit_count = 0;
77                         reached_form_handle_destroyed = false;
78
79                         MyForm f1 = new MyForm ();
80
81                         ctx = new ApplicationContext (f1);
82                         ctx.ThreadExit += new EventHandler (thread_exit);
83
84                         f1.HandleDestroyed += new EventHandler (form_handle_destroyed2);
85
86                         f1.Show ();
87                         f1.DoDestroyHandle ();
88                         Assert.AreEqual (true, reached_form_handle_destroyed, "3");
89                         Assert.AreEqual (1, thread_exit_count, "4");
90                 }
91
92                 [Test]
93                 public void ThreadExitTest ()
94                 {
95                         thread_exit_count = 0;
96
97                         MyForm f1 = new MyForm ();
98                         ctx = new ApplicationContext (f1);
99                         ctx.ThreadExit += new EventHandler (thread_exit);
100
101                         Assert.AreEqual (f1, ctx.MainForm, "1");
102                         f1.ShowInTaskbar = false;
103                         f1.Show ();
104                         f1.Dispose ();
105                         Assert.AreEqual (f1, ctx.MainForm, "2");
106                         Assert.AreEqual (1, thread_exit_count, "3");
107
108                         f1 = new MyForm ();
109                         ctx = new ApplicationContext (f1);
110                         ctx.ThreadExit += new EventHandler (thread_exit);
111                         f1.ShowInTaskbar = false;
112                         f1.Show ();
113                         f1.DoDestroyHandle ();
114                         Assert.AreEqual (f1, ctx.MainForm, "4");
115                         Assert.AreEqual (2, thread_exit_count, "5");
116                 }
117
118                 
119         }
120 }