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 using CategoryAttribute=NUnit.Framework.CategoryAttribute; 17 18 namespace MonoTests.System.Windows.Forms 19 { 20 class MyForm : Form 21 { DoDestroyHandle()22 public void DoDestroyHandle () 23 { 24 DestroyHandle(); 25 } 26 } 27 28 29 [TestFixture] 30 public class ApplicationContextTest : TestHelper 31 { 32 ApplicationContext ctx; 33 int thread_exit_count; 34 bool reached_form_handle_destroyed; 35 thread_exit(object sender, EventArgs e)36 void thread_exit (object sender, EventArgs e) 37 { 38 thread_exit_count++; 39 } 40 form_handle_destroyed(object sender, EventArgs e)41 void form_handle_destroyed (object sender, EventArgs e) 42 { 43 Assert.AreEqual (0, thread_exit_count, "1"); 44 Assert.AreEqual (sender, ctx.MainForm, "2"); 45 reached_form_handle_destroyed = true; 46 } 47 form_handle_destroyed2(object sender, EventArgs e)48 void form_handle_destroyed2 (object sender, EventArgs e) 49 { 50 Assert.AreEqual (1, thread_exit_count, "1"); 51 Assert.AreEqual (sender, ctx.MainForm, "2"); 52 reached_form_handle_destroyed = true; 53 } 54 55 [Test] TestEventOrdering()56 public void TestEventOrdering () 57 { 58 thread_exit_count = 0; 59 reached_form_handle_destroyed = false; 60 61 MyForm f1 = new MyForm (); 62 f1.ShowInTaskbar = false; 63 f1.HandleDestroyed += new EventHandler (form_handle_destroyed); 64 65 ctx = new ApplicationContext (f1); 66 ctx.ThreadExit += new EventHandler (thread_exit); 67 68 f1.Show (); 69 f1.DoDestroyHandle (); 70 71 Assert.AreEqual (true, reached_form_handle_destroyed, "3"); 72 Assert.AreEqual (1, thread_exit_count, "4"); 73 74 f1.Dispose (); 75 } 76 77 [Test] TestEventOrdering2()78 public void TestEventOrdering2 () 79 { 80 thread_exit_count = 0; 81 reached_form_handle_destroyed = false; 82 83 MyForm f1 = new MyForm (); 84 f1.ShowInTaskbar = false; 85 86 ctx = new ApplicationContext (f1); 87 ctx.ThreadExit += new EventHandler (thread_exit); 88 89 f1.HandleDestroyed += new EventHandler (form_handle_destroyed2); 90 91 f1.Show (); 92 f1.DoDestroyHandle (); 93 Assert.AreEqual (true, reached_form_handle_destroyed, "3"); 94 Assert.AreEqual (1, thread_exit_count, "4"); 95 96 f1.Dispose (); 97 } 98 99 [Test] ThreadExitTest()100 public void ThreadExitTest () 101 { 102 thread_exit_count = 0; 103 104 MyForm f1 = new MyForm (); 105 f1.ShowInTaskbar = false; 106 ctx = new ApplicationContext (f1); 107 ctx.ThreadExit += new EventHandler (thread_exit); 108 109 Assert.AreEqual (f1, ctx.MainForm, "1"); 110 f1.ShowInTaskbar = false; 111 f1.Show (); 112 f1.Dispose (); 113 Assert.AreEqual (f1, ctx.MainForm, "2"); 114 Assert.AreEqual (1, thread_exit_count, "3"); 115 116 f1 = new MyForm (); 117 ctx = new ApplicationContext (f1); 118 ctx.ThreadExit += new EventHandler (thread_exit); 119 f1.ShowInTaskbar = false; 120 f1.Show (); 121 f1.DoDestroyHandle (); 122 Assert.AreEqual (f1, ctx.MainForm, "4"); 123 Assert.AreEqual (2, thread_exit_count, "5"); 124 f1.Dispose (); 125 } 126 127 [Test] 128 [Category ("NotWorking")] 129 [ExpectedException (typeof (InvalidOperationException))] NestedApplicationContextTest()130 public void NestedApplicationContextTest () 131 { 132 using (NestedForm frm = new NestedForm ()) { 133 Application.Run (frm); 134 } 135 } 136 137 private class NestedForm : Form 138 { 139 static int counter = 1; OnVisibleChanged(EventArgs e)140 protected override void OnVisibleChanged (EventArgs e) 141 { 142 base.OnVisibleChanged (e); 143 144 Text = counter.ToString (); 145 146 if (counter <= 3) { 147 counter++; 148 Application.Run (new NestedForm ()); 149 } 150 Close (); 151 } 152 } 153 } 154 } 155