ToolStripMenuItemTest.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // ToolStripMenuItemTest.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 ([email protected])
  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. namespace MonoTests.System.Windows.Forms
  37. {
  38. [TestFixture]
  39. public class ToolStripMenuItemTest : TestHelper
  40. {
  41. [Test]
  42. public void Constructor ()
  43. {
  44. ToolStripMenuItem tsi = new ToolStripMenuItem ();
  45. Assert.AreEqual (false, tsi.Checked, "A1");
  46. Assert.AreEqual (false, tsi.CheckOnClick, "A2");
  47. Assert.AreEqual (CheckState.Unchecked, tsi.CheckState, "A3");
  48. Assert.AreEqual (true, tsi.Enabled, "A4");
  49. Assert.AreEqual (false, tsi.IsMdiWindowListEntry, "A5");
  50. Assert.AreEqual (ToolStripItemOverflow.Never, tsi.Overflow, "A6");
  51. Assert.AreEqual (null, tsi.ShortcutKeyDisplayString, "A7");
  52. Assert.AreEqual (Keys.None, tsi.ShortcutKeys, "A8");
  53. Assert.AreEqual (true, tsi.ShowShortcutKeys, "A9");
  54. Assert.AreEqual (SystemColors.ControlText, tsi.ForeColor, "A9-1");
  55. Assert.AreEqual ("System.Windows.Forms.ToolStripMenuItem+ToolStripMenuItemAccessibleObject", tsi.AccessibilityObject.GetType ().ToString (), "A10");
  56. int count = 0;
  57. EventHandler oc = new EventHandler (delegate (object sender, EventArgs e) { count++; });
  58. Image i = new Bitmap (1, 1);
  59. tsi = new ToolStripMenuItem (i);
  60. tsi.PerformClick ();
  61. Assert.AreEqual (null, tsi.Text, "A10-1");
  62. Assert.AreSame (i, tsi.Image, "A10-2");
  63. Assert.AreEqual (0, count, "A10-3");
  64. Assert.AreEqual (string.Empty, tsi.Name, "A10-4");
  65. tsi = new ToolStripMenuItem ("Text");
  66. tsi.PerformClick ();
  67. Assert.AreEqual ("Text", tsi.Text, "A10-5");
  68. Assert.AreSame (null, tsi.Image, "A11");
  69. Assert.AreEqual (0, count, "A12");
  70. Assert.AreEqual (string.Empty, tsi.Name, "A13");
  71. tsi = new ToolStripMenuItem ("Text", i);
  72. tsi.PerformClick ();
  73. Assert.AreEqual ("Text", tsi.Text, "A14");
  74. Assert.AreSame (i, tsi.Image, "A15");
  75. Assert.AreEqual (0, count, "A16");
  76. Assert.AreEqual (string.Empty, tsi.Name, "A17");
  77. tsi = new ToolStripMenuItem ("Text", i, oc);
  78. tsi.PerformClick ();
  79. Assert.AreEqual ("Text", tsi.Text, "A18");
  80. Assert.AreSame (i, tsi.Image, "A19");
  81. Assert.AreEqual (1, count, "A20");
  82. Assert.AreEqual (string.Empty, tsi.Name, "A21");
  83. tsi = new ToolStripMenuItem ("Text", i, oc, "Name");
  84. tsi.PerformClick ();
  85. Assert.AreEqual ("Text", tsi.Text, "A22");
  86. Assert.AreSame (i, tsi.Image, "A23");
  87. Assert.AreEqual (2, count, "A24");
  88. Assert.AreEqual ("Name", tsi.Name, "A25");
  89. }
  90. [Test]
  91. public void BehaviorKeyboardShortcuts ()
  92. {
  93. ExposeProtectedMethods tsmi = new ExposeProtectedMethods ();
  94. tsmi.ShortcutKeys = Keys.Control | Keys.D;
  95. Message m = new Message ();
  96. Assert.AreEqual (false, tsmi.PublicProcessCmdKey (ref m, Keys.D), "A1");
  97. Assert.AreEqual (false, tsmi.PublicProcessCmdKey (ref m, Keys.Control), "A2");
  98. Assert.AreEqual (true, tsmi.PublicProcessCmdKey (ref m, Keys.Control | Keys.D), "A3");
  99. Assert.AreEqual (false, tsmi.PublicProcessCmdKey (ref m, Keys.A), "A4");
  100. Assert.AreEqual (false, tsmi.PublicProcessCmdKey (ref m, Keys.Control | Keys.A), "A5");
  101. tsmi.ShowShortcutKeys = false;
  102. Assert.AreEqual (true, tsmi.PublicProcessCmdKey (ref m, Keys.Control | Keys.D), "A6");
  103. tsmi.ShortcutKeyDisplayString = "Moose";
  104. Assert.AreEqual (true, tsmi.PublicProcessCmdKey (ref m, Keys.Control | Keys.D), "A7");
  105. }
  106. [Test]
  107. public void BehaviorMdiWindowMenuItem ()
  108. {
  109. Form f = new Form ();
  110. f.ShowInTaskbar = false;
  111. f.IsMdiContainer = true;
  112. Form c1 = new Form ();
  113. c1.MdiParent = f;
  114. Form c2 = new Form ();
  115. c2.MdiParent = f;
  116. MenuStrip ms = new MenuStrip ();
  117. ToolStripMenuItem tsmi = (ToolStripMenuItem)ms.Items.Add ("Window");
  118. f.Controls.Add (ms);
  119. ms.MdiWindowListItem = tsmi;
  120. f.MainMenuStrip = ms;
  121. c1.Show ();
  122. f.Show ();
  123. Assert.AreEqual (true, (tsmi.DropDownItems[0] as ToolStripMenuItem).IsMdiWindowListEntry, "R1");
  124. f.Close ();
  125. }
  126. [Test]
  127. public void BehaviorShortcutText ()
  128. {
  129. ToolStripMenuItem tsmi = new ToolStripMenuItem ();
  130. tsmi.ShortcutKeys = Keys.Control | Keys.O;
  131. Assert.AreEqual (null, tsmi.ShortcutKeyDisplayString, "A1");
  132. tsmi.ShortcutKeyDisplayString = "Test String";
  133. Assert.AreEqual ("Test String", tsmi.ShortcutKeyDisplayString, "A2");
  134. tsmi.ShortcutKeys = Keys.Control | Keys.P;
  135. Assert.AreEqual ("Test String", tsmi.ShortcutKeyDisplayString, "A3");
  136. tsmi.ShortcutKeyDisplayString = string.Empty;
  137. Assert.AreEqual (string.Empty, tsmi.ShortcutKeyDisplayString, "A4");
  138. tsmi.ShortcutKeyDisplayString = null;
  139. Assert.AreEqual (null, tsmi.ShortcutKeyDisplayString, "A5");
  140. }
  141. private class ExposeProtectedMethods : ToolStripMenuItem
  142. {
  143. public bool PublicProcessCmdKey (ref Message m, Keys keys)
  144. {
  145. return this.ProcessCmdKey (ref m, keys);
  146. }
  147. }
  148. }
  149. }
  150. #endif