2007-10-11 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / MenuStripTest.cs
1 //
2 // MenuStripTest.cs
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 //
23 // Copyright (c) 2006 Jonathan Pobst
24 //
25 // Authors:
26 //      Jonathan Pobst (monkey@jpobst.com)
27 //
28 #if NET_2_0
29 using System;
30 using System.Collections.Generic;
31 using System.Text;
32 using NUnit.Framework;
33 using System.Drawing;
34 using System.Windows.Forms;
35 using System.ComponentModel;
36
37 namespace MonoTests.System.Windows.Forms
38 {
39         [TestFixture]
40         public class MenuStripTest
41         {
42                 [Test]
43                 public void Constructor ()
44                 {
45                         MenuStrip ms = new MenuStrip ();
46
47                         Assert.AreEqual (false, ms.CanSelect, "A0");
48                         Assert.AreEqual (false, ms.CanOverflow, "A1");
49                         Assert.AreEqual (ToolStripGripStyle.Hidden, ms.GripStyle, "A2");
50                         Assert.AreEqual (null, ms.MdiWindowListItem, "A3");
51                         Assert.AreEqual (false, ms.ShowItemToolTips, "A4");
52                         Assert.AreEqual (true, ms.Stretch, "A5");
53                         Assert.AreEqual (ToolStripLayoutStyle.HorizontalStackWithOverflow, ms.LayoutStyle, "A6");
54                         
55                         Assert.AreEqual ("System.Windows.Forms.MenuStrip+MenuStripAccessibleObject", ms.AccessibilityObject.GetType ().ToString (), "A7");
56                 }
57
58                 [Test]
59                 public void ControlStyle ()
60                 {
61                         ExposeProtectedProperties epp = new ExposeProtectedProperties ();
62
63                         ControlStyles cs = ControlStyles.ContainerControl;
64                         cs |= ControlStyles.UserPaint;
65                         cs |= ControlStyles.StandardClick;
66                         cs |= ControlStyles.SupportsTransparentBackColor;
67                         cs |= ControlStyles.StandardDoubleClick;
68                         cs |= ControlStyles.AllPaintingInWmPaint;
69                         cs |= ControlStyles.OptimizedDoubleBuffer;
70                         cs |= ControlStyles.UseTextForAccessibility;
71
72                         Assert.AreEqual (cs, epp.GetControlStyles (), "Styles");
73                 }
74                 
75                 [Test]
76                 public void ProtectedProperties ()
77                 {
78                         ExposeProtectedProperties epp = new ExposeProtectedProperties ();
79
80                         Assert.AreEqual (new Padding (2, 2, 0, 2), epp.DefaultGripMargin, "C1");
81                         Assert.AreEqual (new Padding (6, 2, 0, 2), epp.DefaultPadding, "C2");
82                         Assert.AreEqual (false, epp.DefaultShowItemToolTips, "C3");
83                         Assert.AreEqual (new Size (200, 24), epp.DefaultSize, "C4");
84                 }
85
86                 [Test]
87                 public void PropertyCanOverflow ()
88                 {
89                         StatusStrip ts = new StatusStrip ();
90
91                         ts.CanOverflow = true;
92                         Assert.AreEqual (true, ts.CanOverflow, "B1");
93                 }
94
95                 [Test]
96                 public void PropertyGripStyle ()
97                 {
98                         StatusStrip ts = new StatusStrip ();
99
100                         ts.GripStyle = ToolStripGripStyle.Visible;
101                         Assert.AreEqual (ToolStripGripStyle.Visible, ts.GripStyle, "B1");
102                 }
103
104                 [Test]
105                 [ExpectedException (typeof (InvalidEnumArgumentException))]
106                 public void PropertyGripStyleIEAE ()
107                 {
108                         StatusStrip ts = new StatusStrip ();
109
110                         ts.GripStyle = (ToolStripGripStyle)42;
111                 }
112
113                 [Test]
114                 public void PropertyShowItemToolTips ()
115                 {
116                         StatusStrip ts = new StatusStrip ();
117
118                         ts.ShowItemToolTips = true;
119                         Assert.AreEqual (true, ts.ShowItemToolTips, "B1");
120                 }
121                 
122                 [Test]
123                 public void PropertyStretch ()
124                 {
125                         StatusStrip ts = new StatusStrip ();
126
127                         ts.Stretch = false;
128                         Assert.AreEqual (false, ts.Stretch, "B1");
129                 }
130
131                 [Test]
132                 public void BehaviorMdiWindowMenuItem ()
133                 {
134                         Form f = new Form ();
135                         f.ShowInTaskbar = false;
136                         f.IsMdiContainer = true;
137                         Form c1 = new Form ();
138                         c1.MdiParent = f;
139                         Form c2 = new Form ();
140                         c2.MdiParent = f;                               
141                 
142                         MenuStrip ms = new MenuStrip ();
143                         ToolStripMenuItem tsmi = (ToolStripMenuItem)ms.Items.Add ("Window");
144                         f.Controls.Add (ms);
145                         ms.MdiWindowListItem = tsmi;
146                         
147                         Assert.AreSame (tsmi, ms.MdiWindowListItem, "Q1");
148                         Assert.AreEqual (0, tsmi.DropDownItems.Count, "Q2");
149                         
150                         f.MainMenuStrip = ms;
151                         Assert.AreEqual (0, tsmi.DropDownItems.Count, "Q3");
152
153                         c1.Show ();
154                         Assert.AreEqual (0, tsmi.DropDownItems.Count, "Q4");
155
156                         f.Show ();
157                         Assert.AreEqual (1, tsmi.DropDownItems.Count, "Q5");
158                         Assert.AreEqual (true, (tsmi.DropDownItems[0] as ToolStripMenuItem).Checked, "Q6");
159                         
160                         c2.Show ();
161                         Assert.AreEqual (2, tsmi.DropDownItems.Count, "Q7");
162                         Assert.AreEqual (true, (tsmi.DropDownItems[1] as ToolStripMenuItem).Checked, "Q8");
163
164                         Form c3 = new Form ();
165                         c3.MdiParent = f;
166                         Assert.AreEqual (2, tsmi.DropDownItems.Count, "Q9");
167
168                         c3.Show ();
169                         Assert.AreEqual (3, tsmi.DropDownItems.Count, "Q10");
170                         Assert.AreEqual (true, (tsmi.DropDownItems[2] as ToolStripMenuItem).Checked, "Q11");
171
172                         c3.Hide ();
173                         Assert.AreEqual (2, tsmi.DropDownItems.Count, "Q12");
174 //                      Assert.AreEqual (true, (tsmi.DropDownItems[1] as ToolStripMenuItem).Checked, "Q13");
175
176                         // Technically, adding the Cascade item adds it to the end of the list until
177                         // anything regarding Mdi is clicked, which then moves it to the top of
178                         // the list and adds the separator.  
179                         // Calling c3.Show() forces the Cascade menu to the top.
180                         tsmi.DropDownItems.Add ("Cascade");
181                         c3.Show ();
182                         Assert.AreEqual (5, tsmi.DropDownItems.Count, "Q14");
183                         Assert.AreEqual (true, (tsmi.DropDownItems[4] as ToolStripMenuItem).Checked, "Q15");
184                         
185                         f.Close ();
186                 }
187
188                 private class ExposeProtectedProperties : MenuStrip
189                 {
190                         public new Padding DefaultGripMargin { get { return base.DefaultGripMargin; } }
191                         public new Padding DefaultPadding { get { return base.DefaultPadding; } }
192                         public new bool DefaultShowItemToolTips { get { return base.DefaultShowItemToolTips; } }
193                         public new Size DefaultSize { get { return base.DefaultSize; } }
194
195                         public ControlStyles GetControlStyles ()
196                         {
197                                 ControlStyles retval = (ControlStyles)0;
198
199                                 foreach (ControlStyles cs in Enum.GetValues (typeof (ControlStyles)))
200                                         if (this.GetStyle (cs) == true)
201                                                 retval |= cs;
202
203                                 return retval;
204                         }
205                 }
206         }
207 }
208 #endif