Unit test for bug #821.
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / ApplicationTest.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 System.Threading;
16 using NUnit.Framework;
17
18 namespace MonoTests.System.Windows.Forms
19 {
20         [TestFixture]
21         public class ApplicationTest : TestHelper
22         {
23                 ApplicationContext ctx;
24
25                 void form_visible_changed (object sender, EventArgs e)
26                 {
27                         Assert.AreEqual (sender, ctx.MainForm, "1");
28                         ((Form)sender).Close();
29                 }
30
31                 [Test]
32                 public void ContextMainFormTest ()
33                 {
34                         Form f1 = new Form ();
35                         f1.ShowInTaskbar = false;
36                         ctx = new ApplicationContext (f1);
37
38                         f1.VisibleChanged += new EventHandler (form_visible_changed);
39
40                         Application.Run (ctx);
41
42                         Assert.IsNull (ctx.MainForm, "2");
43                         f1.Dispose ();
44                 }
45
46                 [Test]
47                 public void Bug694908 ()
48                 {
49                         Application.ThreadException += CrashingForm.HandleThreadException;
50
51                         using (var form = new CrashingForm ())
52                         {
53                                 form.Show ();
54                                 Application.DoEvents ();
55                         }
56                         // with bug 694908 we don't come here. Instead NUnit exits.
57                         Assert.IsTrue (CrashingForm.HasHandledException);
58                 }
59
60                 class CrashingForm: Form
61                 {
62                         private static Form _thisForm;
63
64                         public CrashingForm ()
65                         {
66                                 _thisForm = this;
67                                 var btn = new Button ();
68                                 SuspendLayout ();
69
70                                 btn.Paint += OnButtonPaint;
71                                 Controls.Add (btn);
72
73                                 ResumeLayout (false);
74                                 PerformLayout ();
75                         }
76
77                         private void OnButtonPaint (object sender, PaintEventArgs e)
78                         {
79                                 throw new ArgumentException ();
80                         }
81
82                         public static bool HasHandledException { get; private set; }
83
84                         public static void HandleThreadException (object sender, ThreadExceptionEventArgs args)
85                         {
86                                 _thisForm.Refresh ();
87                                 Application.DoEvents ();
88                                 HasHandledException = true;
89                                 _thisForm.Close ();
90                         }
91                 }
92
93 #if NET_2_0
94                 [Test]
95                 [ExpectedException (typeof (NotSupportedException))]
96                 public void RestartNotSupportedExceptionTest ()
97                 {
98                         Application.Restart ();
99                 }
100                 
101                 [Test]
102                 public void OpenFormsTest ()
103                 {
104                         IntPtr dummy = IntPtr.Zero;
105                         
106                         Form f1 = null, f2 = null;
107                         try {
108                                 f1 = new OpenFormsTestForm ();
109                                 Assert.AreEqual (0, Application.OpenForms.Count, "#1");
110                                 dummy = f1.Handle; 
111                                 Assert.AreEqual (0, Application.OpenForms.Count, "#2");
112                                 f1.Close ();
113                                 Assert.AreEqual (0, Application.OpenForms.Count, "#3");
114                                 f1.Dispose ();
115                                 Assert.AreEqual (0, Application.OpenForms.Count, "#4");
116
117                                 f1 = new OpenFormsTestForm ();
118                                 Assert.AreEqual (0, Application.OpenForms.Count, "#5");
119                                 f1.Show ();
120                                 Assert.AreEqual (1, Application.OpenForms.Count, "#6");
121                                 f1.Close ();
122                                 Assert.AreEqual (0, Application.OpenForms.Count, "#7");
123                                 f1.Dispose ();
124                                 Assert.AreEqual (0, Application.OpenForms.Count, "#8");
125
126                                 f1 = new OpenFormsTestForm ();
127                                 Assert.AreEqual (0, Application.OpenForms.Count, "#9");
128                                 dummy = f1.Handle; 
129                                 Assert.AreEqual (0, Application.OpenForms.Count, "#10");
130                                 f1.GetType ().GetMethod ("RecreateHandle", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.ExactBinding).Invoke (f1, new object [] {});
131                                 Assert.AreEqual (0, Application.OpenForms.Count, "#11");
132                                 f1.Dispose ();
133                                 Assert.AreEqual (0, Application.OpenForms.Count, "#12");
134
135                                 f1 = new OpenFormsTestForm ();
136                                 Assert.AreEqual (0, Application.OpenForms.Count, "#13");
137                                 f1.Show ();
138                                 Assert.AreEqual (1, Application.OpenForms.Count, "#14");
139                                 f2 = new OpenFormsTestForm ();
140                                 Assert.AreEqual (1, Application.OpenForms.Count, "#15");
141                                 f2.Show ();
142                                 Assert.AreEqual (2, Application.OpenForms.Count, "#16");
143                                 f1.Dispose ();
144                                 Assert.AreEqual (1, Application.OpenForms.Count, "#17");
145                                 f2.Close ();
146                                 Assert.AreEqual (0, Application.OpenForms.Count, "#18");
147
148
149                                 f1 = new OpenFormsTestForm ();
150                                 Assert.AreEqual (0, Application.OpenForms.Count, "#19");
151                                 f1.Show ();
152                                 Assert.AreEqual (1, Application.OpenForms.Count, "#20");
153                                 f2 = new OpenFormsTestForm ();
154                                 Assert.AreEqual (1, Application.OpenForms.Count, "#21");
155                                 f2.Show ();
156                                 Assert.AreEqual (2, Application.OpenForms.Count, "#22");
157                                 f1.Visible = false;
158                                 Assert.AreEqual (2, Application.OpenForms.Count, "#23");
159                                 f2.Visible = false;
160                                 Assert.AreEqual (2, Application.OpenForms.Count, "#24");
161                                 f1.Dispose ();
162                                 Assert.AreEqual (1, Application.OpenForms.Count, "#25");
163                                 f2.Close ();
164                                 Assert.AreEqual (0, Application.OpenForms.Count, "#26");
165
166                         } finally {
167                                 if (f1 != null) {
168                                         f1.Dispose ();
169                                 }
170                                 if (f2 != null) {
171                                         f2.Dispose ();
172                                 }
173                         }
174                         
175                         TestHelper.RemoveWarning (dummy);
176                 }
177                 
178                 [Test]
179                 public void MethodRaiseIdle ()
180                 {
181                         bool idle_raised = false;
182
183                         Application.Idle += new EventHandler (delegate (Object obj, EventArgs e) { idle_raised = true; });
184                         Application.RaiseIdle (EventArgs.Empty);
185                         
186                         Assert.AreEqual (true, idle_raised, "R1");
187                 }
188
189                 void Application_Idle (object sender, EventArgs e)
190                 {
191                         throw new Exception ("The method or operation is not implemented.");
192                 }
193                 
194                 class OpenFormsTestForm : Form 
195                 {
196                         public bool have_been_opened;
197                         
198                         public bool IsInOpenForms ()
199                         {
200                                 foreach (Form form in Application.OpenForms) {
201                                         if (form == this) {
202                                                 have_been_opened = true;
203                                                 return true;
204                                         }
205                                 }
206                                 return false;
207                         }
208
209                         protected override void OnLoad (EventArgs e)
210                         {
211                                 Assert.AreEqual (false, IsInOpenForms (), "#OnLoad-A");
212                                 base.OnLoad (e);
213                                 Assert.AreEqual (true, IsInOpenForms (), "#OnLoad-B");
214                         }
215
216                         protected override void OnCreateControl ()
217                         {
218                                 Assert.AreEqual (false, IsInOpenForms (), "#OnCreateControl-A");
219                                 base.OnCreateControl ();
220                                 Assert.AreEqual (true, IsInOpenForms (), "#OnCreateControl-B");
221                         }
222
223                         // Activation may not be synchronous, causing too many false positives
224                         protected override void OnActivated (EventArgs e)
225                         {
226                                 bool dummy = IsInOpenForms ();
227                                 //Assert.AreEqual (true, IsInOpenForms (), "#OnActivated-A");
228                                 base.OnActivated (e);
229                                 dummy = IsInOpenForms ();
230                                 //Assert.AreEqual (true, IsInOpenForms (), "#OnActivated-B");
231                         }
232
233                         protected override void OnClosed (EventArgs e)
234                         {
235                                 Assert.AreEqual (have_been_opened, IsInOpenForms (), "#OnClosed-A");
236                                 base.OnClosed (e);
237                                 Assert.AreEqual (have_been_opened, IsInOpenForms (), "#OnClosed-B");
238                         }
239
240                         protected override void OnClosing (CancelEventArgs e)
241                         {
242                                 Assert.AreEqual (have_been_opened, IsInOpenForms (), "#OnClosing-A");
243                                 base.OnClosing (e);
244                                 Assert.AreEqual (have_been_opened, IsInOpenForms (), "#OnClosing-B");
245                         }
246
247                         protected override void OnDeactivate (EventArgs e)
248                         {
249                                 Assert.AreEqual (true, IsInOpenForms (), "#OnDeactivate-A");
250                                 base.OnDeactivate (e);
251                                 Assert.AreEqual (true, IsInOpenForms (), "#OnDeactivate-B");
252                         }
253
254                         protected override void OnFormClosed (FormClosedEventArgs e)
255                         {
256                                 Assert.AreEqual (have_been_opened, IsInOpenForms (), "#OnFormClosed-A");
257                                 base.OnFormClosed (e);
258                                 Assert.AreEqual (false, IsInOpenForms (), "#OnFormClosed-B");
259                         }
260
261                         protected override void OnFormClosing (FormClosingEventArgs e)
262                         {
263                                 Assert.AreEqual (have_been_opened, IsInOpenForms (), "#OnFormClosing-A");
264                                 base.OnFormClosing (e);
265                                 Assert.AreEqual (have_been_opened, IsInOpenForms (), "#OnFormClosing-B");
266                         }
267
268                         protected override void OnHandleCreated (EventArgs e)
269                         {
270                                 Assert.AreEqual (false, IsInOpenForms (), "#OnHandleCreated-A");
271                                 base.OnHandleCreated (e);
272                                 Assert.AreEqual (false, IsInOpenForms (), "#OnHandleCreated-B");
273                         }
274
275                         protected override void OnHandleDestroyed (EventArgs e)
276                         {
277                                 //Assert.AreEqual (have_been_opened, IsInOpenForms (), "#OnHandleDestroyed-A");
278                                 base.OnHandleDestroyed (e);
279                                 Assert.AreEqual (false, IsInOpenForms (), "#OnHandleDestroyed-B");
280                         }
281
282                         protected override void SetVisibleCore (bool value)
283                         {
284                                 if (value)
285                                         Assert.AreEqual (false && value, IsInOpenForms (), "#SetVisibleCore-A");
286                                 base.SetVisibleCore (value);
287                                 Assert.AreEqual (true, IsInOpenForms (), "#SetVisibleCore-B");
288                         }
289
290                         protected override void OnVisibleChanged (EventArgs e)
291                         {
292                                 Assert.AreEqual (true, IsInOpenForms (), "#OnVisibleChanged-A");
293                                 base.OnVisibleChanged (e);
294                                 Assert.AreEqual (true, IsInOpenForms (), "#OnVisibleChanged-B");
295                         }
296
297                         protected override void CreateHandle ()
298                         {
299                                 Assert.AreEqual (false, IsInOpenForms (), "#CreateHandle-A");
300                                 base.CreateHandle ();
301                                 // We have a different stack trace here, so we're not matching MS
302                                 // Assert.AreEqual (false, IsInOpenForms (), "#CreateHandle-B");
303                         }
304
305                         protected override void DestroyHandle ()
306                         {
307                                 //Dispose may be called several times, so this isn't correct
308                                 //Assert.AreEqual (have_been_opened, IsInOpenForms (), "#DestroyHandle-A");
309                                 base.DestroyHandle ();
310                                 Assert.AreEqual (false, IsInOpenForms (), "#DestroyHandle-B");
311                         }
312
313                         protected override void Dispose (bool disposing)
314                         {
315                                 //Dispose may be called several times, so this isn't correct
316                                 //Assert.AreEqual (have_been_opened, IsInOpenForms (), "#Dispose-A");
317                                 base.Dispose (disposing);
318                                 Assert.AreEqual (false, IsInOpenForms (), "#Dispose-B");
319                         }
320                 }
321 #endif
322         }
323 }