ToolStripTextBoxTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. //
  2. // ToolStripTextBoxTests.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. namespace MonoTests.System.Windows.Forms
  36. {
  37. [TestFixture]
  38. public class ToolStripTextBoxTests
  39. {
  40. [Test]
  41. public void Constructor ()
  42. {
  43. ToolStripTextBox tsi = new ToolStripTextBox ();
  44. Assert.AreEqual (false, tsi.AcceptsReturn, "A1");
  45. Assert.AreEqual (false, tsi.AcceptsTab, "A2");
  46. //Assert.AreEqual ("System.Windows.Forms.AutoCompleteStringCollection", tsi.AutoCompleteCustomSource.GetType ().ToString (), "A3");
  47. //Assert.AreEqual (AutoCompleteMode.None, tsi.AutoCompleteMode, "A4");
  48. //Assert.AreEqual (AutoCompleteSource.None, tsi.AutoCompleteSource, "A5");
  49. Assert.AreEqual (BorderStyle.Fixed3D, tsi.BorderStyle, "A6");
  50. Assert.AreEqual (false, tsi.CanUndo, "A7");
  51. Assert.AreEqual (CharacterCasing.Normal, tsi.CharacterCasing, "A8");
  52. Assert.AreEqual (true, tsi.HideSelection, "A9");
  53. Assert.AreEqual ("System.String[]", tsi.Lines.GetType ().ToString (), "A10");
  54. Assert.AreEqual (32767, tsi.MaxLength, "A11");
  55. //Bug in TextBox
  56. //Assert.AreEqual (false, tsi.Modified, "A12");
  57. Assert.AreEqual (false, tsi.ReadOnly, "A13");
  58. Assert.AreEqual (string.Empty, tsi.SelectedText, "A14");
  59. Assert.AreEqual (0, tsi.SelectionLength, "A15");
  60. Assert.AreEqual (0, tsi.SelectionStart, "A16");
  61. //Assert.AreEqual (true, tsi.ShortcutsEnabled, "A17");
  62. Assert.AreEqual ("System.Windows.Forms.ToolStripTextBox+ToolStripTextBoxControl", tsi.TextBox.GetType ().ToString (), "A18");
  63. Assert.AreEqual (HorizontalAlignment.Left, tsi.TextBoxTextAlign, "A19");
  64. Assert.AreEqual (0, tsi.TextLength, "A20");
  65. tsi = new ToolStripTextBox ("Bob");
  66. Assert.AreEqual ("Bob", tsi.Name, "A21");
  67. Assert.AreEqual (string.Empty, tsi.Control.Name, "A22");
  68. }
  69. [Test]
  70. [ExpectedException (typeof (NotSupportedException))]
  71. public void ConstructorNSE ()
  72. {
  73. new ToolStripTextBox (new TextBox ());
  74. }
  75. [Test]
  76. public void ProtectedProperties ()
  77. {
  78. ExposeProtectedProperties epp = new ExposeProtectedProperties ();
  79. Assert.AreEqual (new Padding (1, 0, 1, 0), epp.DefaultMargin, "C1");
  80. Assert.AreEqual (new Size (100, 22), epp.DefaultSize, "C2");
  81. }
  82. [Test]
  83. public void PropertyAcceptsReturn ()
  84. {
  85. ToolStripTextBox tsi = new ToolStripTextBox ();
  86. EventWatcher ew = new EventWatcher (tsi);
  87. tsi.AcceptsReturn = true;
  88. Assert.AreEqual (true, tsi.AcceptsReturn, "B1");
  89. Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  90. ew.Clear ();
  91. tsi.AcceptsReturn = true;
  92. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  93. }
  94. [Test]
  95. public void PropertyAcceptsTab ()
  96. {
  97. ToolStripTextBox tsi = new ToolStripTextBox ();
  98. EventWatcher ew = new EventWatcher (tsi);
  99. tsi.AcceptsTab = true;
  100. Assert.AreEqual (true, tsi.AcceptsTab, "B1");
  101. Assert.AreEqual ("AcceptsTabChanged", ew.ToString (), "B2");
  102. ew.Clear ();
  103. tsi.AcceptsTab = true;
  104. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  105. }
  106. //[Test]
  107. //public void PropertyAutoCompleteCustomSource ()
  108. //{
  109. // ToolStripTextBox tsi = new ToolStripTextBox ();
  110. // EventWatcher ew = new EventWatcher (tsi);
  111. // AutoCompleteStringCollection acsc = new AutoCompleteStringCollection ();
  112. // acsc.AddRange (new string[] {"Apple", "Banana"});
  113. // tsi.AutoCompleteCustomSource = acsc;
  114. // Assert.AreSame (acsc, tsi.AutoCompleteCustomSource, "B1");
  115. // Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  116. // ew.Clear ();
  117. // tsi.AutoCompleteCustomSource = acsc;
  118. // Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  119. //}
  120. //[Test]
  121. //public void PropertyAutoCompleteMode ()
  122. //{
  123. // ToolStripTextBox tsi = new ToolStripTextBox ();
  124. // EventWatcher ew = new EventWatcher (tsi);
  125. // tsi.AutoCompleteMode = AutoCompleteMode.Append;
  126. // Assert.AreEqual (AutoCompleteMode.Append, tsi.AutoCompleteMode, "B1");
  127. // Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  128. // ew.Clear ();
  129. // tsi.AutoCompleteMode = AutoCompleteMode.Append;
  130. // Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  131. //}
  132. //[Test]
  133. //public void PropertyAutoCompleteSource ()
  134. //{
  135. // ToolStripTextBox tsi = new ToolStripTextBox ();
  136. // EventWatcher ew = new EventWatcher (tsi);
  137. // tsi.AutoCompleteSource = AutoCompleteSource.RecentlyUsedList;
  138. // Assert.AreEqual (AutoCompleteSource.RecentlyUsedList, tsi.AutoCompleteSource, "B1");
  139. // Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  140. // ew.Clear ();
  141. // tsi.AutoCompleteSource = AutoCompleteSource.RecentlyUsedList;
  142. // Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  143. //}
  144. [Test]
  145. public void PropertyBorderStyle ()
  146. {
  147. ToolStripTextBox tsi = new ToolStripTextBox ();
  148. EventWatcher ew = new EventWatcher (tsi);
  149. tsi.BorderStyle = BorderStyle.None;
  150. Assert.AreEqual (BorderStyle.None, tsi.BorderStyle, "B1");
  151. Assert.AreEqual ("BorderStyleChanged", ew.ToString (), "B2");
  152. ew.Clear ();
  153. tsi.BorderStyle = BorderStyle.None;
  154. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  155. }
  156. [Test]
  157. public void PropertyCharacterCasing ()
  158. {
  159. ToolStripTextBox tsi = new ToolStripTextBox ();
  160. EventWatcher ew = new EventWatcher (tsi);
  161. tsi.CharacterCasing = CharacterCasing.Lower;
  162. Assert.AreEqual (CharacterCasing.Lower, tsi.CharacterCasing, "B1");
  163. Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  164. ew.Clear ();
  165. tsi.CharacterCasing = CharacterCasing.Lower;
  166. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  167. }
  168. [Test]
  169. public void PropertyHideSelection ()
  170. {
  171. ToolStripTextBox tsi = new ToolStripTextBox ();
  172. EventWatcher ew = new EventWatcher (tsi);
  173. tsi.HideSelection = false;
  174. Assert.AreEqual (false, tsi.HideSelection, "B1");
  175. Assert.AreEqual ("HideSelectionChanged", ew.ToString (), "B2");
  176. ew.Clear ();
  177. tsi.HideSelection = false;
  178. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  179. }
  180. [Test]
  181. [Ignore ("Problems in TextBox")]
  182. public void PropertyLines ()
  183. {
  184. ToolStripTextBox tsi = new ToolStripTextBox ();
  185. EventWatcher ew = new EventWatcher (tsi);
  186. string[] lines = new string[] {"Apple", "Banana"};
  187. tsi.Lines = lines;
  188. Assert.AreEqual (lines, tsi.Lines, "B1");
  189. Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  190. ew.Clear ();
  191. tsi.Lines = lines;
  192. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  193. }
  194. [Test]
  195. public void PropertyMaxLength ()
  196. {
  197. ToolStripTextBox tsi = new ToolStripTextBox ();
  198. EventWatcher ew = new EventWatcher (tsi);
  199. tsi.MaxLength = 15;
  200. Assert.AreEqual (15, tsi.MaxLength, "B1");
  201. Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  202. ew.Clear ();
  203. tsi.MaxLength = 15;
  204. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  205. }
  206. [Test]
  207. [Ignore ("TextBox does not raise ModifiedChanged")]
  208. // When this works, also uncomment A12 in Constructor test above please
  209. public void PropertyModified ()
  210. {
  211. ToolStripTextBox tsi = new ToolStripTextBox ();
  212. EventWatcher ew = new EventWatcher (tsi);
  213. tsi.Modified = true;
  214. Assert.AreEqual (true, tsi.Modified, "B1");
  215. Assert.AreEqual ("ModifiedChanged", ew.ToString (), "B2");
  216. ew.Clear ();
  217. tsi.Modified = true;
  218. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  219. }
  220. [Test]
  221. public void PropertyReadOnly ()
  222. {
  223. ToolStripTextBox tsi = new ToolStripTextBox ();
  224. EventWatcher ew = new EventWatcher (tsi);
  225. tsi.ReadOnly = true;
  226. Assert.AreEqual (true, tsi.ReadOnly, "B1");
  227. Assert.AreEqual ("ReadOnlyChanged", ew.ToString (), "B2");
  228. ew.Clear ();
  229. tsi.ReadOnly = true;
  230. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  231. }
  232. [Test]
  233. [Ignore ("TextBox does not raise ModifiedChanged")]
  234. public void PropertySelectedText ()
  235. {
  236. ToolStripTextBox tsi = new ToolStripTextBox ();
  237. EventWatcher ew = new EventWatcher (tsi);
  238. tsi.Text = "Crumbelievable";
  239. tsi.SelectedText = "lie";
  240. Assert.AreEqual (string.Empty, tsi.SelectedText, "B1");
  241. Assert.AreEqual ("ModifiedChanged", ew.ToString (), "B2");
  242. ew.Clear ();
  243. tsi.SelectedText = "lie";
  244. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  245. }
  246. [Test]
  247. public void PropertySelectionLength ()
  248. {
  249. ToolStripTextBox tsi = new ToolStripTextBox ();
  250. EventWatcher ew = new EventWatcher (tsi);
  251. tsi.Text = "Crumbelievable";
  252. tsi.SelectionLength = 6;
  253. Assert.AreEqual (6, tsi.SelectionLength, "B1");
  254. Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  255. ew.Clear ();
  256. tsi.SelectionLength = 6;
  257. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  258. }
  259. [Test]
  260. public void PropertySelectionStart ()
  261. {
  262. ToolStripTextBox tsi = new ToolStripTextBox ();
  263. EventWatcher ew = new EventWatcher (tsi);
  264. tsi.Text = "Crumbelievable";
  265. tsi.SelectionStart = 4;
  266. Assert.AreEqual (4, tsi.SelectionStart, "B1");
  267. Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  268. ew.Clear ();
  269. tsi.SelectionStart = 4;
  270. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  271. }
  272. //[Test]
  273. //public void PropertyShortcutsEnabled ()
  274. //{
  275. // ToolStripTextBox tsi = new ToolStripTextBox ();
  276. // EventWatcher ew = new EventWatcher (tsi);
  277. // tsi.ShortcutsEnabled = false;
  278. // Assert.AreEqual (false, tsi.ShortcutsEnabled, "B1");
  279. // Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  280. // ew.Clear ();
  281. // tsi.ShortcutsEnabled = false;
  282. // Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  283. //}
  284. [Test]
  285. public void PropertyTextBoxTextAlign ()
  286. {
  287. ToolStripTextBox tsi = new ToolStripTextBox ();
  288. EventWatcher ew = new EventWatcher (tsi);
  289. tsi.TextBoxTextAlign = HorizontalAlignment.Right;
  290. Assert.AreEqual (HorizontalAlignment.Right, tsi.TextBoxTextAlign, "B1");
  291. Assert.AreEqual ("TextBoxTextAlignChanged", ew.ToString (), "B2");
  292. ew.Clear ();
  293. tsi.TextBoxTextAlign = HorizontalAlignment.Right;
  294. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  295. }
  296. private class EventWatcher
  297. {
  298. private string events = string.Empty;
  299. public EventWatcher (ToolStripTextBox tsi)
  300. {
  301. tsi.AcceptsTabChanged += new EventHandler (delegate (Object obj, EventArgs e) { events += ("AcceptsTabChanged;"); });
  302. tsi.BorderStyleChanged += new EventHandler (delegate (Object obj, EventArgs e) { events += ("BorderStyleChanged;"); });
  303. tsi.HideSelectionChanged += new EventHandler (delegate (Object obj, EventArgs e) { events += ("HideSelectionChanged;"); });
  304. tsi.ModifiedChanged += new EventHandler (delegate (Object obj, EventArgs e) { events += ("ModifiedChanged;"); });
  305. tsi.ReadOnlyChanged += new EventHandler (delegate (Object obj, EventArgs e) { events += ("ReadOnlyChanged;"); });
  306. tsi.TextBoxTextAlignChanged += new EventHandler (delegate (Object obj, EventArgs e) { events += ("TextBoxTextAlignChanged;"); });
  307. }
  308. public override string ToString ()
  309. {
  310. return events.TrimEnd (';');
  311. }
  312. public void Clear ()
  313. {
  314. events = string.Empty;
  315. }
  316. }
  317. private class ExposeProtectedProperties : ToolStripTextBox
  318. {
  319. public ExposeProtectedProperties () : base () {}
  320. public new Padding DefaultMargin { get { return base.DefaultMargin; } }
  321. public new Size DefaultSize { get { return base.DefaultSize; } }
  322. }
  323. }
  324. }
  325. #endif