Xamarin Public Jenkins 6992685b86 Imported Upstream version 4.2.0.179
Former-commit-id: 0a113cb3a6feb7873f632839b1307cc6033cd595
2015-11-10 14:54:39 +00:00

155 lines
3.3 KiB
C#

//
// ApplicationContextTest.cs
//
// Author:
// Chris Toshok (toshok@ximian.com)
//
// (C) 2006 Novell, Inc. (http://www.novell.com)
//
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Reflection;
using NUnit.Framework;
using CategoryAttribute=NUnit.Framework.CategoryAttribute;
namespace MonoTests.System.Windows.Forms
{
class MyForm : Form
{
public void DoDestroyHandle ()
{
DestroyHandle();
}
}
[TestFixture]
public class ApplicationContextTest : TestHelper
{
ApplicationContext ctx;
int thread_exit_count;
bool reached_form_handle_destroyed;
void thread_exit (object sender, EventArgs e)
{
thread_exit_count++;
}
void form_handle_destroyed (object sender, EventArgs e)
{
Assert.AreEqual (0, thread_exit_count, "1");
Assert.AreEqual (sender, ctx.MainForm, "2");
reached_form_handle_destroyed = true;
}
void form_handle_destroyed2 (object sender, EventArgs e)
{
Assert.AreEqual (1, thread_exit_count, "1");
Assert.AreEqual (sender, ctx.MainForm, "2");
reached_form_handle_destroyed = true;
}
[Test]
public void TestEventOrdering ()
{
thread_exit_count = 0;
reached_form_handle_destroyed = false;
MyForm f1 = new MyForm ();
f1.ShowInTaskbar = false;
f1.HandleDestroyed += new EventHandler (form_handle_destroyed);
ctx = new ApplicationContext (f1);
ctx.ThreadExit += new EventHandler (thread_exit);
f1.Show ();
f1.DoDestroyHandle ();
Assert.AreEqual (true, reached_form_handle_destroyed, "3");
Assert.AreEqual (1, thread_exit_count, "4");
f1.Dispose ();
}
[Test]
public void TestEventOrdering2 ()
{
thread_exit_count = 0;
reached_form_handle_destroyed = false;
MyForm f1 = new MyForm ();
f1.ShowInTaskbar = false;
ctx = new ApplicationContext (f1);
ctx.ThreadExit += new EventHandler (thread_exit);
f1.HandleDestroyed += new EventHandler (form_handle_destroyed2);
f1.Show ();
f1.DoDestroyHandle ();
Assert.AreEqual (true, reached_form_handle_destroyed, "3");
Assert.AreEqual (1, thread_exit_count, "4");
f1.Dispose ();
}
[Test]
public void ThreadExitTest ()
{
thread_exit_count = 0;
MyForm f1 = new MyForm ();
f1.ShowInTaskbar = false;
ctx = new ApplicationContext (f1);
ctx.ThreadExit += new EventHandler (thread_exit);
Assert.AreEqual (f1, ctx.MainForm, "1");
f1.ShowInTaskbar = false;
f1.Show ();
f1.Dispose ();
Assert.AreEqual (f1, ctx.MainForm, "2");
Assert.AreEqual (1, thread_exit_count, "3");
f1 = new MyForm ();
ctx = new ApplicationContext (f1);
ctx.ThreadExit += new EventHandler (thread_exit);
f1.ShowInTaskbar = false;
f1.Show ();
f1.DoDestroyHandle ();
Assert.AreEqual (f1, ctx.MainForm, "4");
Assert.AreEqual (2, thread_exit_count, "5");
f1.Dispose ();
}
[Test]
[Category ("NotWorking")]
[ExpectedException (typeof (InvalidOperationException))]
public void NestedApplicationContextTest ()
{
using (NestedForm frm = new NestedForm ()) {
Application.Run (frm);
}
}
private class NestedForm : Form
{
static int counter = 1;
protected override void OnVisibleChanged (EventArgs e)
{
base.OnVisibleChanged (e);
Text = counter.ToString ();
if (counter <= 3) {
counter++;
Application.Run (new NestedForm ());
}
Close ();
}
}
}
}