2007-03-28 Igor Zelmanovich <igorz@mainsoft.com>
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / MdiFormTest.cs
index ddcb0eb531b2454078d756da848aabe4214475db..eaa7d8973d06eb8115fd2fa6207614475cc42bb4 100644 (file)
@@ -11,8 +11,9 @@ using System;
 using System.Drawing;
 using System.Reflection;
 using System.Windows.Forms;
-
+using System.ComponentModel;
 using NUnit.Framework;
+using CategoryAttribute = NUnit.Framework.CategoryAttribute;
 
 namespace MonoTests.System.Windows.Forms
 {
@@ -44,27 +45,196 @@ namespace MonoTests.System.Windows.Forms
                // No attribute here since this is supposed to be called from 
                // each test directly, not by nunit.
                public void SetUp (bool only_create, bool only_text)
+               {
+                       SetUp (only_create, only_text, false);
+               }       
+                               
+               // No attribute here since this is supposed to be called from 
+               // each test directly, not by nunit.
+               public void SetUp (bool only_create, bool only_text, bool set_parent)
                {
                        TearDown ();
+                       
                        main = new Form ();
                        child1 = new Form ();
                        child2 = new Form ();
                        child3 = new Form ();
-                       
+
                        if (only_create)
                                return;
 
                        main.Text = main.Name = "main";
+                       main.ShowInTaskbar = false;
                        child1.Text = child1.Name = "child1";
                        child2.Text = child2.Name = "child2";
                        child3.Text = child3.Name = "child3";
-                       
+
                        if (only_text)
                                return;
 
                        main.IsMdiContainer = true;
+                       
+                       if (set_parent) {
+                               child1.MdiParent = main;
+                               child2.MdiParent = main;
+                               child3.MdiParent = main;
+                       }
+               }
+               
+               [Test]
+               public void ActiveControlTest ()
+               {
+                       SetUp (false, false, true);
+                       
+                       main.Show ();
+                       
+                       Assert.IsNull (main.ActiveControl, "#01");                      
+                       child1.Visible = true;
+                       Assert.AreSame (child1, main.ActiveControl, "#02");
+                       child2.Visible = true;
+                       Assert.AreSame (child2, main.ActiveControl, "#03");
+                       child3.Visible = true;
+                       Assert.AreSame (child3, main.ActiveControl, "#04");
+                       TearDown ();
+               }
+               
+               [Test]
+               public void SelectNextControlTest ()
+               {
+                       SetUp (false, false, true);
+
+                       main.Show ();
+                       
+                       child1.Visible = true;
+                       child2.Visible = true;
+                       child3.Visible = true;
+                       
+                       main.SelectNextControl (main.ActiveControl, true, false, true, true);
+                       Assert.AreSame (child1, main.ActiveControl, "#01");
+                       main.SelectNextControl (main.ActiveControl, true, false, true, true);
+                       Assert.AreSame (child2, main.ActiveControl, "#02");
+                       main.SelectNextControl (main.ActiveControl, true, false, true, true);
+                       Assert.AreSame (child3, main.ActiveControl, "#03");
+                                               
+                       TearDown ();
                }
 
+               [Test]
+               public void SelectPreviousControlTest ()
+               {
+                       SetUp (false, false, true);
+
+                       main.Show ();
+                       
+                       child1.Visible = true;
+                       child2.Visible = true;
+                       child3.Visible = true;
+
+                       main.SelectNextControl (main.ActiveControl, false, false, true, true);
+                       Assert.AreSame (child2, main.ActiveControl, "#01");
+                       main.SelectNextControl (main.ActiveControl, false, false, true, true);
+                       Assert.AreSame (child1, main.ActiveControl, "#02");
+                       main.SelectNextControl (main.ActiveControl, false, false, true, true);
+                       Assert.AreSame (child3, main.ActiveControl, "#03");
+
+                       TearDown ();
+               }
+               
+               [TestFixture]
+               public class CloseTest
+               {
+                       class ChildForm : Form
+                       {
+                               public ChildForm ()
+                               {
+                                       Closed += new EventHandler (ChildForm_Closed);
+                                       Closing += new CancelEventHandler (ChildForm_Closing);
+#if NET_2_0                                    
+                                       FormClosed += new FormClosedEventHandler (ChildForm_FormClosed);
+                                       FormClosing += new FormClosingEventHandler (ChildForm_FormClosing);
+#endif
+                               }
+#if NET_2_0
+                               void ChildForm_FormClosing (object sender, FormClosingEventArgs e)
+                               {
+                                       Assert.IsNotNull (MdiParent, "ChildForm_FormClosing");
+                               }
+
+                               void ChildForm_FormClosed (object sender, FormClosedEventArgs e)
+                               {
+                                       Assert.IsNotNull (MdiParent, "ChildForm_FormClosed");
+                               }
+#endif
+                               void ChildForm_Closing (object sender, CancelEventArgs e)
+                               {
+                                       Assert.IsNotNull (MdiParent, "ChildForm_Closing");
+                               }
+
+                               void ChildForm_Closed (object sender, EventArgs e)
+                               {
+                                       Assert.IsNotNull (MdiParent, "ChildForm_Closed");
+                               }
+                       
+                               protected override void OnClosed (EventArgs e)
+                               {
+                                       Assert.IsNotNull (MdiParent, "OnClosed 1");
+                                       base.OnClosed (e);
+                                       Assert.IsNotNull (MdiParent, "OnClosed 2");
+                               }
+
+                               protected override void OnClosing (CancelEventArgs e)
+                               {
+                                       Assert.IsNotNull (MdiParent, "OnClosing 1");
+                                       base.OnClosing (e);
+                                       Assert.IsNotNull (MdiParent, "OnClosing 2");
+                               }
+                               
+                       }
+                       [Test]
+                       public void Test () {
+                               using (Form main = new Form ()) {
+                                       main.IsMdiContainer = true;
+                                       main.ShowInTaskbar = false;
+                                       main.Visible = true;
+
+                                       ChildForm child = new ChildForm ();
+                                       EventLogger log = new EventLogger (child);
+                                       child.MdiParent = main;
+                                       child.Visible = true;
+                                       child.Close ();
+                                       
+                                       Assert.AreEqual (1, log.CountEvents ("Closed"), "#01");
+                                       Assert.AreEqual (1, log.CountEvents ("Closing"), "#02");
+                                       Assert.IsNull (child.MdiParent, "#03");
+                                       Assert.AreEqual (0, main.MdiChildren.Length, "#04");            
+                                       Assert.AreEqual (false, child.IsMdiChild, "#05");               
+                               }
+                       }
+               }
+               [Test]
+               [Category ("NotWorking")]
+               public void StartLocationTest ()
+               {
+                       SetUp (false, false);
+
+                       child1.MdiParent = main;
+                       child2.MdiParent = main;
+
+                       child1.Visible = true;
+                       child2.Visible = true;
+
+                       main.Show ();
+
+                       Assert.AreEqual ("{X=0,Y=0}", child1.Location.ToString (), "#1");
+                       Assert.AreEqual (true, "{X=0,Y=0}" != child2.Location.ToString (), "#2");
+
+                       TearDown ();
+               }
+               
+               /* These tests are all failing on WinXP with XP Theme. 
+                * offset seems to be 22,22 normally, and 22,29 with XP theme.
+                * Find a way to test this reliably.
+                * /
                [Category("NotWorking")]
                [Test]
                public void StartLocationTest1 ()
@@ -458,6 +628,7 @@ namespace MonoTests.System.Windows.Forms
                        TearDown ();
                }
 
+               [Category("NotWorking")]
                [Test]
                public void StartLocationTest15 ()
                {
@@ -597,7 +768,7 @@ namespace MonoTests.System.Windows.Forms
 
                        TearDown ();
                }
-
+*/
                [Test]
                public void StartSizeTest1 ()
                {
@@ -751,6 +922,8 @@ namespace MonoTests.System.Windows.Forms
                        child.Show ();
 
                        Assert.AreEqual ("main - [child]", main.Text, "#2");
+
+                       main.Dispose ();
                }
 
                [Test] // bug 80038
@@ -775,6 +948,8 @@ namespace MonoTests.System.Windows.Forms
 
                        child.Close ();
                        Assert.AreEqual ("main", main.Text, "#3");
+
+                       main.Dispose ();
                }
 
                [Test]
@@ -809,8 +984,29 @@ namespace MonoTests.System.Windows.Forms
                        child1.WindowState = FormWindowState.Maximized;
 
                        Assert.AreEqual ("main - [child1]", main.Text, "#4");
+
+                       main.Dispose ();
                }
+               
+               [Test]
+               [Category ("NotWorking")]
+               public void TopLevelTest ()
+               {
+                       Form main, child1;
+
+                       main = new Form ();
+                       main.IsMdiContainer = true;
+                       main.Name = "main";
 
+                       child1 = new Form ();
+                       child1.Name = "child1";
+                       Assert.AreEqual (true, child1.TopLevel, "#01");
+                       child1.MdiParent = main;
+                       Assert.AreEqual (false, child1.TopLevel, "#02");
+                       
+                       child1.Dispose ();
+                       main.Dispose ();
+               }
                [Test]
                public void ActiveMdiChild ()
                {
@@ -905,12 +1101,14 @@ namespace MonoTests.System.Windows.Forms
 
                                child1 = new Form ();
                                child1.Name = "child1";
+                               child1.Text = "child1";
                                child1.MdiParent = main;
                                child1.WindowState = FormWindowState.Maximized;
                                child1.Show ();
                                
                                child2 = new Form ();
                                child2.Name = "child2";
+                               child2.Text = "child2";
                                child2.MdiParent = main;
                                child2.Show();