ToolStripControlHostTest.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. //
  2. // ToolStripControlHostTests.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 ToolStripControlHostTests
  40. {
  41. [Test]
  42. public void Constructor ()
  43. {
  44. Control t = new Control ();
  45. ToolStripControlHost tsi = new ToolStripControlHost (t);
  46. Assert.AreEqual (SystemColors.Control, tsi.BackColor, "A1");
  47. Assert.AreEqual (null, tsi.BackgroundImage, "A2");
  48. Assert.AreEqual (ImageLayout.Tile, tsi.BackgroundImageLayout, "A3");
  49. Assert.AreEqual (true, tsi.CanSelect, "A4");
  50. Assert.AreEqual (true, tsi.CausesValidation, "A5");
  51. Assert.AreSame (t, tsi.Control, "A6");
  52. Assert.AreEqual (ContentAlignment.MiddleCenter, tsi.ControlAlign, "A7");
  53. Assert.AreEqual (true, tsi.Enabled, "A8");
  54. Assert.AreEqual (false, tsi.Focused, "A9");
  55. Assert.AreEqual (t.Font, tsi.Font, "A10");
  56. Assert.AreEqual (SystemColors.ControlText, tsi.ForeColor, "A11");
  57. //Assert.AreEqual (RightToLeft.No, tsi.RightToLeft, "A12");
  58. Assert.AreEqual (false, tsi.Selected, "A13");
  59. Assert.AreEqual (null, tsi.Site, "A14");
  60. Assert.AreEqual (new Size (0, 0), tsi.Size, "A15");
  61. Assert.AreEqual (string.Empty, tsi.Text, "A16");
  62. tsi = new ToolStripControlHost (t, "Bob");
  63. Assert.AreEqual ("Bob", tsi.Name, "A17");
  64. Assert.AreSame (t, tsi.Control, "A18");
  65. Assert.AreEqual (string.Empty, tsi.Control.Name, "A19");
  66. }
  67. [Test]
  68. [ExpectedException (typeof (ArgumentNullException))]
  69. public void ConstructorANE ()
  70. {
  71. new ToolStripControlHost (null);
  72. }
  73. [Test]
  74. [ExpectedException (typeof (ArgumentNullException))]
  75. public void ConstructorANE2 ()
  76. {
  77. new ToolStripControlHost (null, string.Empty);
  78. }
  79. [Test]
  80. public void ProtectedProperties ()
  81. {
  82. ExposeProtectedProperties epp = new ExposeProtectedProperties ();
  83. Assert.AreEqual (new Size (0, 0), epp.DefaultSize, "C1");
  84. }
  85. [Test]
  86. public void PropertyBackColor ()
  87. {
  88. ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
  89. EventWatcher ew = new EventWatcher (tsi);
  90. tsi.BackColor = Color.BurlyWood;
  91. Assert.AreEqual (Color.BurlyWood, tsi.BackColor, "B1");
  92. Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  93. ew.Clear ();
  94. tsi.BackColor = Color.BurlyWood;
  95. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  96. // BackColor initially comes from hosted control
  97. tsi = new ToolStripControlHost (new Button ());
  98. Assert.AreEqual (SystemColors.Control, tsi.BackColor, "B4");
  99. tsi = new ToolStripControlHost (new TextBox ());
  100. Assert.AreEqual (SystemColors.Window, tsi.BackColor, "B5");
  101. tsi = new ToolStripControlHost (new ProgressBar ());
  102. Assert.AreEqual (SystemColors.Control, tsi.BackColor, "B6");
  103. }
  104. [Test]
  105. public void PropertyBackgroundImage ()
  106. {
  107. ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
  108. EventWatcher ew = new EventWatcher (tsi);
  109. Image i = new Bitmap (1, 1);
  110. tsi.BackgroundImage = i;
  111. Assert.AreSame (i, tsi.BackgroundImage, "B1");
  112. Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  113. ew.Clear ();
  114. tsi.BackgroundImage = i;
  115. Assert.AreSame (string.Empty, ew.ToString (), "B3");
  116. }
  117. [Test]
  118. public void PropertyBackgroundImageLayout ()
  119. {
  120. ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
  121. EventWatcher ew = new EventWatcher (tsi);
  122. tsi.BackgroundImageLayout = ImageLayout.Zoom;
  123. Assert.AreEqual (ImageLayout.Zoom, tsi.BackgroundImageLayout, "B1");
  124. Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  125. ew.Clear ();
  126. tsi.BackgroundImageLayout = ImageLayout.Zoom;
  127. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  128. }
  129. [Test]
  130. public void PropertyCausesValidation ()
  131. {
  132. ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
  133. EventWatcher ew = new EventWatcher (tsi);
  134. tsi.CausesValidation = false;
  135. Assert.AreEqual (false, tsi.CausesValidation, "B1");
  136. Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  137. ew.Clear ();
  138. tsi.CausesValidation = false;
  139. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  140. }
  141. [Test]
  142. public void PropertyControlAlign ()
  143. {
  144. ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
  145. EventWatcher ew = new EventWatcher (tsi);
  146. tsi.ControlAlign = ContentAlignment.TopRight ;
  147. Assert.AreEqual (ContentAlignment.TopRight, tsi.ControlAlign, "B1");
  148. Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  149. ew.Clear ();
  150. tsi.ControlAlign = ContentAlignment.TopRight;
  151. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  152. }
  153. [Test]
  154. public void PropertyEnabled ()
  155. {
  156. ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
  157. EventWatcher ew = new EventWatcher (tsi);
  158. tsi.Enabled = false;
  159. Assert.AreEqual (false, tsi.Enabled, "B1");
  160. Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  161. ew.Clear ();
  162. tsi.Enabled = false;
  163. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  164. }
  165. [Test]
  166. public void PropertyFont ()
  167. {
  168. ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
  169. EventWatcher ew = new EventWatcher (tsi);
  170. Font f = new Font ("Arial", 12);
  171. tsi.Font = f;
  172. Assert.AreSame (f, tsi.Font, "B1");
  173. Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  174. ew.Clear ();
  175. tsi.Font = f;
  176. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  177. }
  178. [Test]
  179. public void PropertyForeColor ()
  180. {
  181. ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
  182. EventWatcher ew = new EventWatcher (tsi);
  183. tsi.ForeColor = Color.BurlyWood;
  184. Assert.AreEqual (Color.BurlyWood, tsi.ForeColor, "B1");
  185. Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  186. ew.Clear ();
  187. tsi.ForeColor = Color.BurlyWood;
  188. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  189. // CausesValidation initially comes from hosted control
  190. tsi = new ToolStripControlHost (new Button ());
  191. Assert.AreEqual (SystemColors.ControlText, tsi.ForeColor, "B4");
  192. tsi = new ToolStripControlHost (new TextBox ());
  193. Assert.AreEqual (SystemColors.WindowText, tsi.ForeColor, "B5");
  194. }
  195. //[Test]
  196. //public void PropertyRightToLeft ()
  197. //{
  198. // ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
  199. // EventWatcher ew = new EventWatcher (tsi);
  200. // tsi.RightToLeft = RightToLeft.Yes;
  201. // Assert.AreEqual (RightToLeft.Yes, tsi.RightToLeft, "B1");
  202. // Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  203. // ew.Clear ();
  204. // tsi.RightToLeft = RightToLeft.Yes;
  205. // Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  206. //}
  207. [Test]
  208. public void PropertySite ()
  209. {
  210. ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
  211. EventWatcher ew = new EventWatcher (tsi);
  212. ISite i = new Form ().Site;
  213. tsi.Site = i;
  214. Assert.AreSame (i, tsi.Site, "B1");
  215. Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  216. ew.Clear ();
  217. tsi.Site = i;
  218. Assert.AreSame (string.Empty, ew.ToString (), "B3");
  219. }
  220. [Test]
  221. public void PropertySize ()
  222. {
  223. ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
  224. EventWatcher ew = new EventWatcher (tsi);
  225. tsi.Size = new Size (42, 42);
  226. Assert.AreEqual (new Size (42, 42), tsi.Size, "B1");
  227. Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  228. ew.Clear ();
  229. tsi.Size = new Size (42, 42);
  230. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  231. }
  232. [Test]
  233. public void PropertyText ()
  234. {
  235. ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
  236. EventWatcher ew = new EventWatcher (tsi);
  237. tsi.Text = "Text";
  238. Assert.AreEqual ("Text", tsi.Text, "B1");
  239. Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  240. ew.Clear ();
  241. tsi.Text = "Text";
  242. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  243. }
  244. //[Test]
  245. //public void PropertyAnchorAndDocking ()
  246. //{
  247. // ToolStripItem ts = new NullToolStripItem ();
  248. // ts.Anchor = AnchorStyles.Top | AnchorStyles.Bottom;
  249. // Assert.AreEqual (AnchorStyles.Top | AnchorStyles.Bottom, ts.Anchor, "A1");
  250. // Assert.AreEqual (DockStyle.None, ts.Dock, "A2");
  251. // ts.Anchor = AnchorStyles.Left | AnchorStyles.Right;
  252. // Assert.AreEqual (AnchorStyles.Left | AnchorStyles.Right, ts.Anchor, "A1");
  253. // Assert.AreEqual (DockStyle.None, ts.Dock, "A2");
  254. // ts.Dock = DockStyle.Left;
  255. // Assert.AreEqual (AnchorStyles.Top | AnchorStyles.Left, ts.Anchor, "A1");
  256. // Assert.AreEqual (DockStyle.Left, ts.Dock, "A2");
  257. // ts.Dock = DockStyle.None;
  258. // Assert.AreEqual (AnchorStyles.Top | AnchorStyles.Left, ts.Anchor, "A1");
  259. // Assert.AreEqual (DockStyle.None, ts.Dock, "A2");
  260. // ts.Dock = DockStyle.Top;
  261. // Assert.AreEqual (AnchorStyles.Top | AnchorStyles.Left, ts.Anchor, "A1");
  262. // Assert.AreEqual (DockStyle.Top, ts.Dock, "A2");
  263. //}
  264. [Test]
  265. [Ignore ("Accessibility still needs some work")]
  266. public void Accessibility ()
  267. {
  268. ToolStripControlHost tsi = new ToolStripControlHost (new Button ());
  269. AccessibleObject ao = tsi.AccessibilityObject;
  270. Assert.AreEqual ("Press", ao.DefaultAction, "L2");
  271. Assert.AreEqual (null, ao.Description, "L3");
  272. Assert.AreEqual (null, ao.Help, "L4");
  273. Assert.AreEqual (null, ao.KeyboardShortcut, "L5");
  274. Assert.AreEqual (null, ao.Name, "L6");
  275. Assert.AreEqual (AccessibleRole.PushButton, ao.Role, "L8");
  276. Assert.AreEqual (AccessibleStates.None, ao.State, "L9");
  277. Assert.AreEqual (null, ao.Value, "L10");
  278. tsi.Name = "Label1";
  279. tsi.Text = "Test Label";
  280. tsi.AccessibleDescription = "Label Desc";
  281. Assert.AreEqual ("Press", ao.DefaultAction, "L12");
  282. Assert.AreEqual ("Label Desc", ao.Description, "L13");
  283. Assert.AreEqual (null, ao.Help, "L14");
  284. Assert.AreEqual (null, ao.KeyboardShortcut, "L15");
  285. //Assert.AreEqual ("Test Label", ao.Name, "L16");
  286. Assert.AreEqual (AccessibleRole.PushButton, ao.Role, "L18");
  287. Assert.AreEqual (AccessibleStates.None, ao.State, "L19");
  288. Assert.AreEqual (null, ao.Value, "L20");
  289. tsi.AccessibleName = "Access Label";
  290. Assert.AreEqual ("Access Label", ao.Name, "L21");
  291. tsi.Text = "Test Label";
  292. Assert.AreEqual ("Access Label", ao.Name, "L22");
  293. tsi.AccessibleDefaultActionDescription = "AAA";
  294. Assert.AreEqual ("AAA", tsi.AccessibleDefaultActionDescription, "L23");
  295. }
  296. [Test]
  297. public void BehaviorBackColor ()
  298. {
  299. ToolStrip ts = new ToolStrip ();
  300. ToolStripItem tsi = new ToolStripControlHost (new Control ());
  301. ts.Items.Add (tsi);
  302. Assert.AreEqual (SystemColors.Control, ts.BackColor, "C1");
  303. Assert.AreEqual (SystemColors.Control, tsi.BackColor, "C2");
  304. ts.BackColor = Color.BlueViolet;
  305. Assert.AreEqual (Color.BlueViolet, ts.BackColor, "C3");
  306. Assert.AreEqual (SystemColors.Control, tsi.BackColor, "C4");
  307. tsi.BackColor = Color.Snow;
  308. Assert.AreEqual (Color.BlueViolet, ts.BackColor, "C5");
  309. Assert.AreEqual (Color.Snow, tsi.BackColor, "C6");
  310. tsi.ResetBackColor ();
  311. Assert.AreEqual (SystemColors.Control, tsi.BackColor, "C7");
  312. }
  313. [Test]
  314. [Ignore ("Need some AutoSize work done in ToolStripControlHost")]
  315. public void BehaviorSize ()
  316. {
  317. Control c = new Control ();
  318. ToolStripControlHost tsi = new ToolStripControlHost (c);
  319. ToolStrip ts = new ToolStrip ();
  320. Assert.AreEqual (new Size (0, 0), c.Size, "H1");
  321. Assert.AreEqual (new Size (0, 0), tsi.Size, "H2");
  322. c = new TextBox ();
  323. tsi = new ToolStripControlHost (c);
  324. Assert.AreEqual (new Size (100, 20), c.Size, "H3");
  325. Assert.AreEqual (new Size (100, 20), tsi.Size, "H4");
  326. c = new ComboBox ();
  327. tsi = new ToolStripControlHost (c);
  328. Assert.AreEqual (new Size (121, 21), c.Size, "H5");
  329. Assert.AreEqual (new Size (121, 21), tsi.Size, "H6");
  330. c = new ProgressBar ();
  331. tsi = new ToolStripControlHost (c);
  332. Assert.AreEqual (new Size (100, 23), c.Size, "H7");
  333. Assert.AreEqual (new Size (100, 23), tsi.Size, "H8");
  334. c = new PictureBox ();
  335. tsi = new ToolStripControlHost (c);
  336. Assert.AreEqual (new Size (100, 50), c.Size, "H7");
  337. Assert.AreEqual (new Size (100, 50), tsi.Size, "H8");
  338. Form f = new Form ();
  339. f.Controls.Add (ts);
  340. ts.Items.Add (tsi);
  341. f.Show ();
  342. Assert.AreEqual (new Size (100, 50), c.Size, "H9");
  343. Assert.AreEqual (new Size (100, 50), tsi.Size, "H10");
  344. Assert.AreEqual (new Size (292, 53), ts.Size, "H11");
  345. }
  346. private class EventWatcher
  347. {
  348. private string events = string.Empty;
  349. public EventWatcher (ToolStripControlHost tsi)
  350. {
  351. tsi.Enter += new EventHandler (delegate (Object obj, EventArgs e) { events += ("Enter;"); });
  352. tsi.GotFocus += new EventHandler (delegate (Object obj, EventArgs e) { events += ("GotFocus;"); });
  353. tsi.KeyDown += new KeyEventHandler (delegate (Object obj, KeyEventArgs e) { events += ("KeyDown;"); });
  354. tsi.KeyPress += new KeyPressEventHandler (delegate (Object obj, KeyPressEventArgs e) { events += ("KeyPress;"); });
  355. tsi.KeyUp += new KeyEventHandler (delegate (Object obj, KeyEventArgs e) { events += ("KeyUp;"); });
  356. tsi.Leave += new EventHandler (delegate (Object obj, EventArgs e) { events += ("Leave;"); });
  357. tsi.LostFocus += new EventHandler (delegate (Object obj, EventArgs e) { events += ("LostFocus;"); });
  358. tsi.Validated += new EventHandler (delegate (Object obj, EventArgs e) { events += ("Validated;"); });
  359. tsi.Validating += new CancelEventHandler (delegate (Object obj, CancelEventArgs e) { events += ("Validating;"); });
  360. }
  361. public override string ToString ()
  362. {
  363. return events.TrimEnd (';');
  364. }
  365. public void Clear ()
  366. {
  367. events = string.Empty;
  368. }
  369. }
  370. private class ExposeProtectedProperties : ToolStripControlHost
  371. {
  372. public ExposeProtectedProperties () : base (new Control ()) {}
  373. public new Size DefaultSize { get { return base.DefaultSize; } }
  374. }
  375. }
  376. }
  377. #endif