MenuStripTest.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 ([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 MenuStripTest : TestHelper
  40. {
  41. [Test]
  42. public void Constructor ()
  43. {
  44. MenuStrip ms = new MenuStrip ();
  45. Assert.AreEqual (false, ms.CanSelect, "A0");
  46. Assert.AreEqual (false, ms.CanOverflow, "A1");
  47. Assert.AreEqual (ToolStripGripStyle.Hidden, ms.GripStyle, "A2");
  48. Assert.AreEqual (null, ms.MdiWindowListItem, "A3");
  49. Assert.AreEqual (false, ms.ShowItemToolTips, "A4");
  50. Assert.AreEqual (true, ms.Stretch, "A5");
  51. Assert.AreEqual (ToolStripLayoutStyle.HorizontalStackWithOverflow, ms.LayoutStyle, "A6");
  52. Assert.AreEqual ("System.Windows.Forms.MenuStrip+MenuStripAccessibleObject", ms.AccessibilityObject.GetType ().ToString (), "A7");
  53. }
  54. [Test]
  55. public void ControlStyle ()
  56. {
  57. ExposeProtectedProperties epp = new ExposeProtectedProperties ();
  58. ControlStyles cs = ControlStyles.ContainerControl;
  59. cs |= ControlStyles.UserPaint;
  60. cs |= ControlStyles.StandardClick;
  61. cs |= ControlStyles.SupportsTransparentBackColor;
  62. cs |= ControlStyles.StandardDoubleClick;
  63. cs |= ControlStyles.AllPaintingInWmPaint;
  64. cs |= ControlStyles.OptimizedDoubleBuffer;
  65. cs |= ControlStyles.UseTextForAccessibility;
  66. Assert.AreEqual (cs, epp.GetControlStyles (), "Styles");
  67. }
  68. [Test]
  69. public void ProtectedProperties ()
  70. {
  71. ExposeProtectedProperties epp = new ExposeProtectedProperties ();
  72. Assert.AreEqual (new Padding (2, 2, 0, 2), epp.DefaultGripMargin, "C1");
  73. Assert.AreEqual (new Padding (6, 2, 0, 2), epp.DefaultPadding, "C2");
  74. Assert.AreEqual (false, epp.DefaultShowItemToolTips, "C3");
  75. Assert.AreEqual (new Size (200, 24), epp.DefaultSize, "C4");
  76. }
  77. [Test]
  78. public void PropertyCanOverflow ()
  79. {
  80. StatusStrip ts = new StatusStrip ();
  81. ts.CanOverflow = true;
  82. Assert.AreEqual (true, ts.CanOverflow, "B1");
  83. }
  84. [Test]
  85. public void PropertyGripStyle ()
  86. {
  87. StatusStrip ts = new StatusStrip ();
  88. ts.GripStyle = ToolStripGripStyle.Visible;
  89. Assert.AreEqual (ToolStripGripStyle.Visible, ts.GripStyle, "B1");
  90. }
  91. [Test]
  92. [ExpectedException (typeof (InvalidEnumArgumentException))]
  93. public void PropertyGripStyleIEAE ()
  94. {
  95. StatusStrip ts = new StatusStrip ();
  96. ts.GripStyle = (ToolStripGripStyle)42;
  97. }
  98. [Test]
  99. public void PropertyShowItemToolTips ()
  100. {
  101. StatusStrip ts = new StatusStrip ();
  102. ts.ShowItemToolTips = true;
  103. Assert.AreEqual (true, ts.ShowItemToolTips, "B1");
  104. }
  105. [Test]
  106. public void PropertyStretch ()
  107. {
  108. StatusStrip ts = new StatusStrip ();
  109. ts.Stretch = false;
  110. Assert.AreEqual (false, ts.Stretch, "B1");
  111. }
  112. [Test]
  113. public void BehaviorMdiWindowMenuItem ()
  114. {
  115. Form f = new Form ();
  116. f.ShowInTaskbar = false;
  117. f.IsMdiContainer = true;
  118. Form c1 = new Form ();
  119. c1.MdiParent = f;
  120. Form c2 = new Form ();
  121. c2.MdiParent = f;
  122. MenuStrip ms = new MenuStrip ();
  123. ToolStripMenuItem tsmi = (ToolStripMenuItem)ms.Items.Add ("Window");
  124. f.Controls.Add (ms);
  125. ms.MdiWindowListItem = tsmi;
  126. Assert.AreSame (tsmi, ms.MdiWindowListItem, "Q1");
  127. Assert.AreEqual (0, tsmi.DropDownItems.Count, "Q2");
  128. f.MainMenuStrip = ms;
  129. Assert.AreEqual (0, tsmi.DropDownItems.Count, "Q3");
  130. c1.Show ();
  131. Assert.AreEqual (0, tsmi.DropDownItems.Count, "Q4");
  132. f.Show ();
  133. Assert.AreEqual (1, tsmi.DropDownItems.Count, "Q5");
  134. Assert.AreEqual (true, (tsmi.DropDownItems[0] as ToolStripMenuItem).Checked, "Q6");
  135. c2.Show ();
  136. Assert.AreEqual (2, tsmi.DropDownItems.Count, "Q7");
  137. Assert.AreEqual (true, (tsmi.DropDownItems[1] as ToolStripMenuItem).Checked, "Q8");
  138. Form c3 = new Form ();
  139. c3.MdiParent = f;
  140. Assert.AreEqual (2, tsmi.DropDownItems.Count, "Q9");
  141. c3.Show ();
  142. Assert.AreEqual (3, tsmi.DropDownItems.Count, "Q10");
  143. Assert.AreEqual (true, (tsmi.DropDownItems[2] as ToolStripMenuItem).Checked, "Q11");
  144. c3.Hide ();
  145. Assert.AreEqual (2, tsmi.DropDownItems.Count, "Q12");
  146. // Assert.AreEqual (true, (tsmi.DropDownItems[1] as ToolStripMenuItem).Checked, "Q13");
  147. // Technically, adding the Cascade item adds it to the end of the list until
  148. // anything regarding Mdi is clicked, which then moves it to the top of
  149. // the list and adds the separator.
  150. // Calling c3.Show() forces the Cascade menu to the top.
  151. tsmi.DropDownItems.Add ("Cascade");
  152. c3.Show ();
  153. Assert.AreEqual (5, tsmi.DropDownItems.Count, "Q14");
  154. Assert.AreEqual (true, (tsmi.DropDownItems[4] as ToolStripMenuItem).Checked, "Q15");
  155. f.Close ();
  156. }
  157. [Test] // bug #342358
  158. public void MdiWindowListItem_NoMdiContainer ()
  159. {
  160. Form f = new Form ();
  161. MenuStrip ms = new MenuStrip ();
  162. f.MainMenuStrip = ms;
  163. f.Controls.Add (ms);
  164. ms.MdiWindowListItem = new ToolStripMenuItem ("Window");
  165. Assert.IsNotNull (ms.MdiWindowListItem);
  166. }
  167. private class ExposeProtectedProperties : MenuStrip
  168. {
  169. public new Padding DefaultGripMargin { get { return base.DefaultGripMargin; } }
  170. public new Padding DefaultPadding { get { return base.DefaultPadding; } }
  171. public new bool DefaultShowItemToolTips { get { return base.DefaultShowItemToolTips; } }
  172. public new Size DefaultSize { get { return base.DefaultSize; } }
  173. public ControlStyles GetControlStyles ()
  174. {
  175. ControlStyles retval = (ControlStyles)0;
  176. foreach (ControlStyles cs in Enum.GetValues (typeof (ControlStyles)))
  177. if (this.GetStyle (cs) == true)
  178. retval |= cs;
  179. return retval;
  180. }
  181. }
  182. }
  183. }
  184. #endif