ToolStripTextBoxTest.cs 12 KB

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