Merge pull request #325 from adbre/iss5464
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / ApplicationTest.cs
index 655a78811026302a7918bf8647926c1e13f011ad..78a082ded286b2d21c68ed239fe2a22fd0e6ffcb 100644 (file)
@@ -12,12 +12,13 @@ using System.ComponentModel;
 using System.Windows.Forms;
 using System.Drawing;
 using System.Reflection;
+using System.Threading;
 using NUnit.Framework;
 
 namespace MonoTests.System.Windows.Forms
 {
        [TestFixture]
-       public class ApplicationTest
+       public class ApplicationTest : TestHelper
        {
                ApplicationContext ctx;
 
@@ -41,6 +42,54 @@ namespace MonoTests.System.Windows.Forms
                        Assert.IsNull (ctx.MainForm, "2");
                        f1.Dispose ();
                }
+
+               [Test]
+               public void Bug694908 ()
+               {
+                       Application.ThreadException += CrashingForm.HandleThreadException;
+
+                       using (var form = new CrashingForm ())
+                       {
+                               form.Show ();
+                               Application.DoEvents ();
+                       }
+                       // with bug 694908 we don't come here. Instead NUnit exits.
+                       Assert.IsTrue (CrashingForm.HasHandledException);
+               }
+
+               class CrashingForm: Form
+               {
+                       private static Form _thisForm;
+
+                       public CrashingForm ()
+                       {
+                               _thisForm = this;
+                               var btn = new Button ();
+                               SuspendLayout ();
+
+                               btn.Paint += OnButtonPaint;
+                               Controls.Add (btn);
+
+                               ResumeLayout (false);
+                               PerformLayout ();
+                       }
+
+                       private void OnButtonPaint (object sender, PaintEventArgs e)
+                       {
+                               throw new ArgumentException ();
+                       }
+
+                       public static bool HasHandledException { get; private set; }
+
+                       public static void HandleThreadException (object sender, ThreadExceptionEventArgs args)
+                       {
+                               _thisForm.Refresh ();
+                               Application.DoEvents ();
+                               HasHandledException = true;
+                               _thisForm.Close ();
+                       }
+               }
+
 #if NET_2_0
                [Test]
                [ExpectedException (typeof (NotSupportedException))]
@@ -126,6 +175,22 @@ namespace MonoTests.System.Windows.Forms
                        TestHelper.RemoveWarning (dummy);
                }
                
+               [Test]
+               public void MethodRaiseIdle ()
+               {
+                       bool idle_raised = false;
+
+                       Application.Idle += new EventHandler (delegate (Object obj, EventArgs e) { idle_raised = true; });
+                       Application.RaiseIdle (EventArgs.Empty);
+                       
+                       Assert.AreEqual (true, idle_raised, "R1");
+               }
+
+               void Application_Idle (object sender, EventArgs e)
+               {
+                       throw new Exception ("The method or operation is not implemented.");
+               }
+               
                class OpenFormsTestForm : Form 
                {
                        public bool have_been_opened;
@@ -155,11 +220,14 @@ namespace MonoTests.System.Windows.Forms
                                Assert.AreEqual (true, IsInOpenForms (), "#OnCreateControl-B");
                        }
 
+                       // Activation may not be synchronous, causing too many false positives
                        protected override void OnActivated (EventArgs e)
                        {
-                               Assert.AreEqual (true, IsInOpenForms (), "#OnActivated-A");
+                               bool dummy = IsInOpenForms ();
+                               //Assert.AreEqual (true, IsInOpenForms (), "#OnActivated-A");
                                base.OnActivated (e);
-                               Assert.AreEqual (true, IsInOpenForms (), "#OnActivated-B");
+                               dummy = IsInOpenForms ();
+                               //Assert.AreEqual (true, IsInOpenForms (), "#OnActivated-B");
                        }
 
                        protected override void OnClosed (EventArgs e)