2 // ToolStripMenuItemTest.cs
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:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
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.
23 // Copyright (c) 2006 Jonathan Pobst
26 // Jonathan Pobst (monkey@jpobst.com)
30 using System.Collections.Generic;
32 using NUnit.Framework;
34 using System.Windows.Forms;
35 using System.ComponentModel;
37 namespace MonoTests.System.Windows.Forms
40 public class ToolStripMenuItemTest : TestHelper
43 public void Constructor ()
45 ToolStripMenuItem tsi = new ToolStripMenuItem ();
47 Assert.AreEqual (false, tsi.Checked, "A1");
48 Assert.AreEqual (false, tsi.CheckOnClick, "A2");
49 Assert.AreEqual (CheckState.Unchecked, tsi.CheckState, "A3");
50 Assert.AreEqual (true, tsi.Enabled, "A4");
51 Assert.AreEqual (false, tsi.IsMdiWindowListEntry, "A5");
52 Assert.AreEqual (ToolStripItemOverflow.Never, tsi.Overflow, "A6");
53 Assert.AreEqual (null, tsi.ShortcutKeyDisplayString, "A7");
54 Assert.AreEqual (Keys.None, tsi.ShortcutKeys, "A8");
55 Assert.AreEqual (true, tsi.ShowShortcutKeys, "A9");
56 Assert.AreEqual (SystemColors.ControlText, tsi.ForeColor, "A9-1");
58 Assert.AreEqual ("System.Windows.Forms.ToolStripMenuItem+ToolStripMenuItemAccessibleObject", tsi.AccessibilityObject.GetType ().ToString (), "A10");
60 EventHandler oc = new EventHandler (delegate (object sender, EventArgs e) { count++; });
61 Image i = new Bitmap (1, 1);
63 tsi = new ToolStripMenuItem (i);
65 Assert.AreEqual (null, tsi.Text, "A10-1");
66 Assert.AreSame (i, tsi.Image, "A10-2");
67 Assert.AreEqual (0, count, "A10-3");
68 Assert.AreEqual (string.Empty, tsi.Name, "A10-4");
70 tsi = new ToolStripMenuItem ("Text");
72 Assert.AreEqual ("Text", tsi.Text, "A10-5");
73 Assert.AreSame (null, tsi.Image, "A11");
74 Assert.AreEqual (0, count, "A12");
75 Assert.AreEqual (string.Empty, tsi.Name, "A13");
77 tsi = new ToolStripMenuItem ("Text", i);
79 Assert.AreEqual ("Text", tsi.Text, "A14");
80 Assert.AreSame (i, tsi.Image, "A15");
81 Assert.AreEqual (0, count, "A16");
82 Assert.AreEqual (string.Empty, tsi.Name, "A17");
84 tsi = new ToolStripMenuItem ("Text", i, oc);
86 Assert.AreEqual ("Text", tsi.Text, "A18");
87 Assert.AreSame (i, tsi.Image, "A19");
88 Assert.AreEqual (1, count, "A20");
89 Assert.AreEqual (string.Empty, tsi.Name, "A21");
91 tsi = new ToolStripMenuItem ("Text", i, oc, "Name");
93 Assert.AreEqual ("Text", tsi.Text, "A22");
94 Assert.AreSame (i, tsi.Image, "A23");
95 Assert.AreEqual (2, count, "A24");
96 Assert.AreEqual ("Name", tsi.Name, "A25");
100 public void BehaviorKeyboardShortcuts ()
102 ExposeProtectedMethods tsmi = new ExposeProtectedMethods ();
103 tsmi.ShortcutKeys = Keys.Control | Keys.D;
105 Message m = new Message ();
106 Assert.AreEqual (false, tsmi.PublicProcessCmdKey (ref m, Keys.D), "A1");
107 Assert.AreEqual (false, tsmi.PublicProcessCmdKey (ref m, Keys.Control), "A2");
108 Assert.AreEqual (true, tsmi.PublicProcessCmdKey (ref m, Keys.Control | Keys.D), "A3");
109 Assert.AreEqual (false, tsmi.PublicProcessCmdKey (ref m, Keys.A), "A4");
110 Assert.AreEqual (false, tsmi.PublicProcessCmdKey (ref m, Keys.Control | Keys.A), "A5");
112 tsmi.ShowShortcutKeys = false;
113 Assert.AreEqual (true, tsmi.PublicProcessCmdKey (ref m, Keys.Control | Keys.D), "A6");
115 tsmi.ShortcutKeyDisplayString = "Moose";
116 Assert.AreEqual (true, tsmi.PublicProcessCmdKey (ref m, Keys.Control | Keys.D), "A7");
120 public void BehaviorMdiWindowMenuItem ()
122 Form f = new Form ();
123 f.ShowInTaskbar = false;
124 f.IsMdiContainer = true;
125 Form c1 = new Form ();
127 Form c2 = new Form ();
130 MenuStrip ms = new MenuStrip ();
131 ToolStripMenuItem tsmi = (ToolStripMenuItem)ms.Items.Add ("Window");
133 ms.MdiWindowListItem = tsmi;
135 f.MainMenuStrip = ms;
139 Assert.AreEqual (true, (tsmi.DropDownItems[0] as ToolStripMenuItem).IsMdiWindowListEntry, "R1");
145 public void BehaviorShortcutText ()
147 ToolStripMenuItem tsmi = new ToolStripMenuItem ();
149 tsmi.ShortcutKeys = Keys.Control | Keys.O;
151 Assert.AreEqual (null, tsmi.ShortcutKeyDisplayString, "A1");
153 tsmi.ShortcutKeyDisplayString = "Test String";
154 Assert.AreEqual ("Test String", tsmi.ShortcutKeyDisplayString, "A2");
156 tsmi.ShortcutKeys = Keys.Control | Keys.P;
157 Assert.AreEqual ("Test String", tsmi.ShortcutKeyDisplayString, "A3");
159 tsmi.ShortcutKeyDisplayString = string.Empty;
160 Assert.AreEqual (string.Empty, tsmi.ShortcutKeyDisplayString, "A4");
162 tsmi.ShortcutKeyDisplayString = null;
163 Assert.AreEqual (null, tsmi.ShortcutKeyDisplayString, "A5");
167 [NUnit.Framework.Category ("NotWorking")]
168 public void GetCurrentParent ()
170 ToolStripMenuItem tsmiFile = new ToolStripMenuItem ("File");
171 ToolStripMenuItem tsmiHelp = new ToolStripMenuItem ("Help");
172 ToolStripMenuItem tsmiQuit = new ToolStripMenuItem ("Quit");
173 ToolStripMenuItem tsmiAbout = new ToolStripMenuItem ("About");
174 tsmiFile.DropDownItems.Add (tsmiQuit);
175 tsmiHelp.DropDownItems.Add (tsmiAbout);
176 MenuStrip menu = new MenuStrip ();
177 menu.Items.Add (tsmiFile);
178 menu.Items.Add (tsmiHelp);
180 ToolStrip parent = tsmiFile.GetCurrentParent ();
181 Assert.IsNotNull (parent, "A1");
182 Assert.AreEqual (parent.GetType ().Name, typeof (MenuStrip).Name, "A2");
183 Assert.AreEqual (parent, menu, "A3");
185 //because it's not visible?:
186 Assert.IsNull (tsmiQuit.GetCurrentParent (), "A4");
192 ToolStripMenuItem tsmiFile = new ToolStripMenuItem ("File");
193 ToolStripMenuItem tsmiHelp = new ToolStripMenuItem ("Help");
194 ToolStripMenuItem tsmiQuit = new ToolStripMenuItem ("Quit");
195 ToolStripMenuItem tsmiNew = new ToolStripMenuItem ("New");
196 ToolStripMenuItem tsmiAbout = new ToolStripMenuItem ("About");
197 tsmiQuit.DropDownItems.Add (tsmiNew);
198 tsmiFile.DropDownItems.Add (tsmiQuit);
199 tsmiHelp.DropDownItems.Add (tsmiAbout);
200 MenuStrip menu = new MenuStrip ();
201 menu.Items.Add (tsmiFile);
202 menu.Items.Add (tsmiHelp);
204 Assert.IsNotNull (tsmiFile.Owner);
205 Assert.AreEqual (tsmiFile.Owner.GetType().Name, typeof (MenuStrip).Name);
206 Assert.AreEqual (tsmiFile.Owner, menu);
208 Assert.IsNotNull (tsmiQuit.Owner);
209 Assert.AreEqual (tsmiQuit.Owner.GetType().Name, typeof (ToolStripDropDownMenu).Name);
210 Assert.AreEqual (tsmiQuit.Owner, tsmiFile.DropDown);
214 public void OwnerItem ()
216 ToolStripMenuItem tsmiFile = new ToolStripMenuItem ("File");
217 ToolStripMenuItem tsmiHelp = new ToolStripMenuItem ("Help");
218 ToolStripMenuItem tsmiQuit = new ToolStripMenuItem ("Quit");
219 ToolStripMenuItem tsmiNew = new ToolStripMenuItem ("New");
220 ToolStripMenuItem tsmiAbout = new ToolStripMenuItem ("About");
221 tsmiQuit.DropDownItems.Add (tsmiNew);
222 tsmiFile.DropDownItems.Add (tsmiQuit);
223 tsmiHelp.DropDownItems.Add (tsmiAbout);
224 MenuStrip menu = new MenuStrip ();
225 menu.Items.Add (tsmiFile);
226 menu.Items.Add (tsmiHelp);
228 Assert.IsNull (tsmiFile.OwnerItem);
230 Assert.IsNotNull (tsmiQuit.OwnerItem);
231 Assert.AreEqual (tsmiQuit.OwnerItem.GetType ().Name, typeof (ToolStripMenuItem).Name);
232 Assert.AreEqual (tsmiQuit.OwnerItem, tsmiFile);
236 public void ToolStripDropDownButton_SelectChild ()
238 ToolStripDropDownButton tsddb = new ToolStripDropDownButton ();
239 tsddb.DropDownClosed += Helper.FireEvent1;
240 tsddb.DropDownItemClicked += Helper.FireEvent2;
241 tsddb.DropDownOpened += Helper.FireEvent1;
242 tsddb.DropDownOpening += Helper.FireEvent1;
243 tsddb.Click += Helper.FireEvent1;
245 Helper item1 = new Helper ();
246 Helper item2 = new Helper ();
248 tsddb.DropDownItems.Add (item1);
249 tsddb.DropDownItems.Add (item2);
250 ToolStripDropDownButton_SelectChildVerify (item1);
251 ToolStrip ts = new ToolStrip ();
252 ts.Items.Add (tsddb);
253 ToolStripDropDownButton_SelectChildVerify (item2);
256 private static void ToolStripDropDownButton_SelectChildVerify (Helper item)
258 Assert.IsNull (item.MyParent);
259 Assert.IsTrue (item.CanSelect);
260 Assert.IsFalse (item.Selected);
262 Assert.IsTrue (item.Selected);
263 Assert.IsFalse (Helper.eventFired);
266 private class Helper : ToolStripMenuItem
269 this.DropDownClosed += Helper.FireEvent1;
270 this.DropDownItemClicked += Helper.FireEvent2;
271 this.DropDownOpened += Helper.FireEvent1;
272 this.DropDownOpening += Helper.FireEvent1;
273 this.Click += Helper.FireEvent1;
276 internal ToolStrip MyParent { get { return this.Parent; } }
278 internal static bool eventFired = false;
279 internal static void FireEvent1 (object o, EventArgs args) { eventFired = true; }
280 internal static void FireEvent2 (object o, ToolStripItemClickedEventArgs args) { FireEvent1 (null, null); }
283 private class ExposeProtectedMethods : ToolStripMenuItem
285 public bool PublicProcessCmdKey (ref Message m, Keys keys)
287 return this.ProcessCmdKey (ref m, keys);