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;
17 namespace MonoTests.System.Windows.Forms
20 public class ApplicationTest : TestHelper
22 ApplicationContext ctx;
24 void form_visible_changed (object sender, EventArgs e)
26 Assert.AreEqual (sender, ctx.MainForm, "1");
27 ((Form)sender).Close();
31 public void ContextMainFormTest ()
33 Form f1 = new Form ();
34 f1.ShowInTaskbar = false;
35 ctx = new ApplicationContext (f1);
37 f1.VisibleChanged += new EventHandler (form_visible_changed);
39 Application.Run (ctx);
41 Assert.IsNull (ctx.MainForm, "2");
46 [ExpectedException (typeof (NotSupportedException))]
47 public void RestartNotSupportedExceptionTest ()
49 Application.Restart ();
53 public void OpenFormsTest ()
55 IntPtr dummy = IntPtr.Zero;
57 Form f1 = null, f2 = null;
59 f1 = new OpenFormsTestForm ();
60 Assert.AreEqual (0, Application.OpenForms.Count, "#1");
62 Assert.AreEqual (0, Application.OpenForms.Count, "#2");
64 Assert.AreEqual (0, Application.OpenForms.Count, "#3");
66 Assert.AreEqual (0, Application.OpenForms.Count, "#4");
68 f1 = new OpenFormsTestForm ();
69 Assert.AreEqual (0, Application.OpenForms.Count, "#5");
71 Assert.AreEqual (1, Application.OpenForms.Count, "#6");
73 Assert.AreEqual (0, Application.OpenForms.Count, "#7");
75 Assert.AreEqual (0, Application.OpenForms.Count, "#8");
77 f1 = new OpenFormsTestForm ();
78 Assert.AreEqual (0, Application.OpenForms.Count, "#9");
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");
84 Assert.AreEqual (0, Application.OpenForms.Count, "#12");
86 f1 = new OpenFormsTestForm ();
87 Assert.AreEqual (0, Application.OpenForms.Count, "#13");
89 Assert.AreEqual (1, Application.OpenForms.Count, "#14");
90 f2 = new OpenFormsTestForm ();
91 Assert.AreEqual (1, Application.OpenForms.Count, "#15");
93 Assert.AreEqual (2, Application.OpenForms.Count, "#16");
95 Assert.AreEqual (1, Application.OpenForms.Count, "#17");
97 Assert.AreEqual (0, Application.OpenForms.Count, "#18");
100 f1 = new OpenFormsTestForm ();
101 Assert.AreEqual (0, Application.OpenForms.Count, "#19");
103 Assert.AreEqual (1, Application.OpenForms.Count, "#20");
104 f2 = new OpenFormsTestForm ();
105 Assert.AreEqual (1, Application.OpenForms.Count, "#21");
107 Assert.AreEqual (2, Application.OpenForms.Count, "#22");
109 Assert.AreEqual (2, Application.OpenForms.Count, "#23");
111 Assert.AreEqual (2, Application.OpenForms.Count, "#24");
113 Assert.AreEqual (1, Application.OpenForms.Count, "#25");
115 Assert.AreEqual (0, Application.OpenForms.Count, "#26");
126 TestHelper.RemoveWarning (dummy);
130 public void MethodRaiseIdle ()
132 bool idle_raised = false;
134 Application.Idle += new EventHandler (delegate (Object obj, EventArgs e) { idle_raised = true; });
135 Application.RaiseIdle (EventArgs.Empty);
137 Assert.AreEqual (true, idle_raised, "R1");
140 void Application_Idle (object sender, EventArgs e)
142 throw new Exception ("The method or operation is not implemented.");
145 class OpenFormsTestForm : Form
147 public bool have_been_opened;
149 public bool IsInOpenForms ()
151 foreach (Form form in Application.OpenForms) {
153 have_been_opened = true;
160 protected override void OnLoad (EventArgs e)
162 Assert.AreEqual (false, IsInOpenForms (), "#OnLoad-A");
164 Assert.AreEqual (true, IsInOpenForms (), "#OnLoad-B");
167 protected override void OnCreateControl ()
169 Assert.AreEqual (false, IsInOpenForms (), "#OnCreateControl-A");
170 base.OnCreateControl ();
171 Assert.AreEqual (true, IsInOpenForms (), "#OnCreateControl-B");
174 // Activation may not be synchronous, causing too many false positives
175 protected override void OnActivated (EventArgs e)
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");
184 protected override void OnClosed (EventArgs e)
186 Assert.AreEqual (have_been_opened, IsInOpenForms (), "#OnClosed-A");
188 Assert.AreEqual (have_been_opened, IsInOpenForms (), "#OnClosed-B");
191 protected override void OnClosing (CancelEventArgs e)
193 Assert.AreEqual (have_been_opened, IsInOpenForms (), "#OnClosing-A");
195 Assert.AreEqual (have_been_opened, IsInOpenForms (), "#OnClosing-B");
198 protected override void OnDeactivate (EventArgs e)
200 Assert.AreEqual (true, IsInOpenForms (), "#OnDeactivate-A");
201 base.OnDeactivate (e);
202 Assert.AreEqual (true, IsInOpenForms (), "#OnDeactivate-B");
205 protected override void OnFormClosed (FormClosedEventArgs e)
207 Assert.AreEqual (have_been_opened, IsInOpenForms (), "#OnFormClosed-A");
208 base.OnFormClosed (e);
209 Assert.AreEqual (false, IsInOpenForms (), "#OnFormClosed-B");
212 protected override void OnFormClosing (FormClosingEventArgs e)
214 Assert.AreEqual (have_been_opened, IsInOpenForms (), "#OnFormClosing-A");
215 base.OnFormClosing (e);
216 Assert.AreEqual (have_been_opened, IsInOpenForms (), "#OnFormClosing-B");
219 protected override void OnHandleCreated (EventArgs e)
221 Assert.AreEqual (false, IsInOpenForms (), "#OnHandleCreated-A");
222 base.OnHandleCreated (e);
223 Assert.AreEqual (false, IsInOpenForms (), "#OnHandleCreated-B");
226 protected override void OnHandleDestroyed (EventArgs e)
228 //Assert.AreEqual (have_been_opened, IsInOpenForms (), "#OnHandleDestroyed-A");
229 base.OnHandleDestroyed (e);
230 Assert.AreEqual (false, IsInOpenForms (), "#OnHandleDestroyed-B");
233 protected override void SetVisibleCore (bool value)
236 Assert.AreEqual (false && value, IsInOpenForms (), "#SetVisibleCore-A");
237 base.SetVisibleCore (value);
238 Assert.AreEqual (true, IsInOpenForms (), "#SetVisibleCore-B");
241 protected override void OnVisibleChanged (EventArgs e)
243 Assert.AreEqual (true, IsInOpenForms (), "#OnVisibleChanged-A");
244 base.OnVisibleChanged (e);
245 Assert.AreEqual (true, IsInOpenForms (), "#OnVisibleChanged-B");
248 protected override void CreateHandle ()
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");
256 protected override void DestroyHandle ()
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");
264 protected override void Dispose (bool disposing)
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");