MenuStripTest.cs 6.2 KB

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