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