column name and ordinal fix...tested on 10.1
[mono.git] / mcs / class / System.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                 [Test]
94                 [ExpectedException (typeof (NotSupportedException))]
95                 public void RestartNotSupportedExceptionTest ()
96                 {
97                         Application.Restart ();
98                 }
99                 
100                 [Test]
101                 public void OpenFormsTest ()
102                 {
103                         IntPtr dummy = IntPtr.Zero;
104                         
105                         Form f1 = null, f2 = null;
106                         try {
107                                 f1 = new OpenFormsTestForm ();
108                                 Assert.AreEqual (0, Application.OpenForms.Count, "#1");
109                                 dummy = f1.Handle; 
110                                 Assert.AreEqual (0, Application.OpenForms.Count, "#2");
111                                 f1.Close ();
112                                 Assert.AreEqual (0, Application.OpenForms.Count, "#3");
113                                 f1.Dispose ();
114                                 Assert.AreEqual (0, Application.OpenForms.Count, "#4");
115
116                                 f1 = new OpenFormsTestForm ();
117                                 Assert.AreEqual (0, Application.OpenForms.Count, "#5");
118                                 f1.Show ();
119                                 Assert.AreEqual (1, Application.OpenForms.Count, "#6");
120                                 f1.Close ();
121                                 Assert.AreEqual (0, Application.OpenForms.Count, "#7");
122                                 f1.Dispose ();
123                                 Assert.AreEqual (0, Application.OpenForms.Count, "#8");
124
125                                 f1 = new OpenFormsTestForm ();
126                                 Assert.AreEqual (0, Application.OpenForms.Count, "#9");
127                                 dummy = f1.Handle; 
128                                 Assert.AreEqual (0, Application.OpenForms.Count, "#10");
129                                 f1.GetType ().GetMethod ("RecreateHandle", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.ExactBinding).Invoke (f1, new object [] {});
130                                 Assert.AreEqual (0, Application.OpenForms.Count, "#11");
131                                 f1.Dispose ();
132                                 Assert.AreEqual (0, Application.OpenForms.Count, "#12");
133
134                                 f1 = new OpenFormsTestForm ();
135                                 Assert.AreEqual (0, Application.OpenForms.Count, "#13");
136                                 f1.Show ();
137                                 Assert.AreEqual (1, Application.OpenForms.Count, "#14");
138                                 f2 = new OpenFormsTestForm ();
139                                 Assert.AreEqual (1, Application.OpenForms.Count, "#15");
140                                 f2.Show ();
141                                 Assert.AreEqual (2, Application.OpenForms.Count, "#16");
142                                 f1.Dispose ();
143                                 Assert.AreEqual (1, Application.OpenForms.Count, "#17");
144                                 f2.Close ();
145                                 Assert.AreEqual (0, Application.OpenForms.Count, "#18");
146
147
148                                 f1 = new OpenFormsTestForm ();
149                                 Assert.AreEqual (0, Application.OpenForms.Count, "#19");
150                                 f1.Show ();
151                                 Assert.AreEqual (1, Application.OpenForms.Count, "#20");
152                                 f2 = new OpenFormsTestForm ();
153                                 Assert.AreEqual (1, Application.OpenForms.Count, "#21");
154                                 f2.Show ();
155                                 Assert.AreEqual (2, Application.OpenForms.Count, "#22");
156                                 f1.Visible = false;
157                                 Assert.AreEqual (2, Application.OpenForms.Count, "#23");
158                                 f2.Visible = false;
159                                 Assert.AreEqual (2, Application.OpenForms.Count, "#24");
160                                 f1.Dispose ();
161                                 Assert.AreEqual (1, Application.OpenForms.Count, "#25");
162                                 f2.Close ();
163                                 Assert.AreEqual (0, Application.OpenForms.Count, "#26");
164
165                         } finally {
166                                 if (f1 != null) {
167                                         f1.Dispose ();
168                                 }
169                                 if (f2 != null) {
170                                         f2.Dispose ();
171                                 }
172                         }
173                         
174                         TestHelper.RemoveWarning (dummy);
175                 }
176                 
177                 [Test]
178                 public void MethodRaiseIdle ()
179                 {
180                         bool idle_raised = false;
181
182                         Application.Idle += new EventHandler (delegate (Object obj, EventArgs e) { idle_raised = true; });
183                         Application.RaiseIdle (EventArgs.Empty);
184                         
185                         Assert.AreEqual (true, idle_raised, "R1");
186                 }
187
188                 void Application_Idle (object sender, EventArgs e)
189                 {
190                         throw new Exception ("The method or operation is not implemented.");
191                 }
192                 
193                 class OpenFormsTestForm : Form 
194                 {
195                         public bool have_been_opened;
196                         
197                         public bool IsInOpenForms ()
198                         {
199                                 foreach (Form form in Application.OpenForms) {
200                                         if (form == this) {
201                                                 have_been_opened = true;
202                                                 return true;
203                                         }
204                                 }
205                                 return false;
206                         }
207
208                         protected override void OnLoad (EventArgs e)
209                         {
210                                 Assert.AreEqual (false, IsInOpenForms (), "#OnLoad-A");
211                                 base.OnLoad (e);
212                                 Assert.AreEqual (true, IsInOpenForms (), "#OnLoad-B");
213                         }
214
215                         protected override void OnCreateControl ()
216                         {
217                                 Assert.AreEqual (false, IsInOpenForms (), "#OnCreateControl-A");
218                                 base.OnCreateControl ();
219                                 Assert.AreEqual (true, IsInOpenForms (), "#OnCreateControl-B");
220                         }
221
222                         // Activation may not be synchronous, causing too many false positives
223                         protected override void OnActivated (EventArgs e)
224                         {
225                                 bool dummy = IsInOpenForms ();
226                                 //Assert.AreEqual (true, IsInOpenForms (), "#OnActivated-A");
227                                 base.OnActivated (e);
228                                 dummy = IsInOpenForms ();
229                                 //Assert.AreEqual (true, IsInOpenForms (), "#OnActivated-B");
230                         }
231
232                         protected override void OnClosed (EventArgs e)
233                         {
234                                 Assert.AreEqual (have_been_opened, IsInOpenForms (), "#OnClosed-A");
235                                 base.OnClosed (e);
236                                 Assert.AreEqual (have_been_opened, IsInOpenForms (), "#OnClosed-B");
237                         }
238
239                         protected override void OnClosing (CancelEventArgs e)
240                         {
241                                 Assert.AreEqual (have_been_opened, IsInOpenForms (), "#OnClosing-A");
242                                 base.OnClosing (e);
243                                 Assert.AreEqual (have_been_opened, IsInOpenForms (), "#OnClosing-B");
244                         }
245
246                         protected override void OnDeactivate (EventArgs e)
247                         {
248                                 Assert.AreEqual (true, IsInOpenForms (), "#OnDeactivate-A");
249                                 base.OnDeactivate (e);
250                                 Assert.AreEqual (true, IsInOpenForms (), "#OnDeactivate-B");
251                         }
252
253                         protected override void OnFormClosed (FormClosedEventArgs e)
254                         {
255                                 Assert.AreEqual (have_been_opened, IsInOpenForms (), "#OnFormClosed-A");
256                                 base.OnFormClosed (e);
257                                 Assert.AreEqual (false, IsInOpenForms (), "#OnFormClosed-B");
258                         }
259
260                         protected override void OnFormClosing (FormClosingEventArgs e)
261                         {
262                                 Assert.AreEqual (have_been_opened, IsInOpenForms (), "#OnFormClosing-A");
263                                 base.OnFormClosing (e);
264                                 Assert.AreEqual (have_been_opened, IsInOpenForms (), "#OnFormClosing-B");
265                         }
266
267                         protected override void OnHandleCreated (EventArgs e)
268                         {
269                                 Assert.AreEqual (false, IsInOpenForms (), "#OnHandleCreated-A");
270                                 base.OnHandleCreated (e);
271                                 Assert.AreEqual (false, IsInOpenForms (), "#OnHandleCreated-B");
272                         }
273
274                         protected override void OnHandleDestroyed (EventArgs e)
275                         {
276                                 //Assert.AreEqual (have_been_opened, IsInOpenForms (), "#OnHandleDestroyed-A");
277                                 base.OnHandleDestroyed (e);
278                                 Assert.AreEqual (false, IsInOpenForms (), "#OnHandleDestroyed-B");
279                         }
280
281                         protected override void SetVisibleCore (bool value)
282                         {
283                                 if (value)
284                                         Assert.AreEqual (false && value, IsInOpenForms (), "#SetVisibleCore-A");
285                                 base.SetVisibleCore (value);
286                                 Assert.AreEqual (true, IsInOpenForms (), "#SetVisibleCore-B");
287                         }
288
289                         protected override void OnVisibleChanged (EventArgs e)
290                         {
291                                 Assert.AreEqual (true, IsInOpenForms (), "#OnVisibleChanged-A");
292                                 base.OnVisibleChanged (e);
293                                 Assert.AreEqual (true, IsInOpenForms (), "#OnVisibleChanged-B");
294                         }
295
296                         protected override void CreateHandle ()
297                         {
298                                 Assert.AreEqual (false, IsInOpenForms (), "#CreateHandle-A");
299                                 base.CreateHandle ();
300                                 // We have a different stack trace here, so we're not matching MS
301                                 // Assert.AreEqual (false, IsInOpenForms (), "#CreateHandle-B");
302                         }
303
304                         protected override void DestroyHandle ()
305                         {
306                                 //Dispose may be called several times, so this isn't correct
307                                 //Assert.AreEqual (have_been_opened, IsInOpenForms (), "#DestroyHandle-A");
308                                 base.DestroyHandle ();
309                                 Assert.AreEqual (false, IsInOpenForms (), "#DestroyHandle-B");
310                         }
311
312                         protected override void Dispose (bool disposing)
313                         {
314                                 //Dispose may be called several times, so this isn't correct
315                                 //Assert.AreEqual (have_been_opened, IsInOpenForms (), "#Dispose-A");
316                                 base.Dispose (disposing);
317                                 Assert.AreEqual (false, IsInOpenForms (), "#Dispose-B");
318                         }
319                 }
320         }
321 }