2 // ToolStripTextBoxTests.cs
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:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
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.
23 // Copyright (c) 2006 Jonathan Pobst
26 // Jonathan Pobst (monkey@jpobst.com)
30 using System.Collections.Generic;
32 using NUnit.Framework;
34 using System.Windows.Forms;
36 namespace MonoTests.System.Windows.Forms
39 public class ToolStripTextBoxTests : TestHelper
42 public void Constructor ()
44 ToolStripTextBox tsi = new ToolStripTextBox ();
46 Assert.AreEqual (false, tsi.AcceptsReturn, "A1");
47 Assert.AreEqual (false, tsi.AcceptsTab, "A2");
48 Assert.AreEqual ("System.Windows.Forms.AutoCompleteStringCollection", tsi.AutoCompleteCustomSource.GetType ().ToString (), "A3");
49 Assert.AreEqual (AutoCompleteMode.None, tsi.AutoCompleteMode, "A4");
50 Assert.AreEqual (AutoCompleteSource.None, tsi.AutoCompleteSource, "A5");
51 Assert.AreEqual (BorderStyle.Fixed3D, tsi.BorderStyle, "A6");
52 Assert.AreEqual (false, tsi.CanUndo, "A7");
53 Assert.AreEqual (CharacterCasing.Normal, tsi.CharacterCasing, "A8");
54 Assert.AreEqual (true, tsi.HideSelection, "A9");
55 Assert.AreEqual ("System.String[]", tsi.Lines.GetType ().ToString (), "A10");
56 Assert.AreEqual (32767, tsi.MaxLength, "A11");
58 //Assert.AreEqual (false, tsi.Modified, "A12");
59 Assert.AreEqual (false, tsi.ReadOnly, "A13");
60 Assert.AreEqual (string.Empty, tsi.SelectedText, "A14");
61 Assert.AreEqual (0, tsi.SelectionLength, "A15");
62 Assert.AreEqual (0, tsi.SelectionStart, "A16");
63 Assert.AreEqual (true, tsi.ShortcutsEnabled, "A17");
64 Assert.AreEqual ("System.Windows.Forms.ToolStripTextBox+ToolStripTextBoxControl", tsi.TextBox.GetType ().ToString (), "A18");
65 Assert.AreEqual (HorizontalAlignment.Left, tsi.TextBoxTextAlign, "A19");
66 Assert.AreEqual (0, tsi.TextLength, "A20");
68 tsi = new ToolStripTextBox ("Bob");
69 Assert.AreEqual ("Bob", tsi.Name, "A21");
70 Assert.AreEqual (string.Empty, tsi.Control.Name, "A22");
74 [ExpectedException (typeof (NotSupportedException))]
75 public void ConstructorNSE ()
77 new ToolStripTextBox (new TextBox ());
81 public void ProtectedProperties ()
83 ExposeProtectedProperties epp = new ExposeProtectedProperties ();
85 Assert.AreEqual (new Padding (1, 0, 1, 0), epp.DefaultMargin, "C1");
86 Assert.AreEqual (new Size (100, 22), epp.DefaultSize, "C2");
90 public void PropertyAcceptsReturn ()
92 ToolStripTextBox tsi = new ToolStripTextBox ();
93 EventWatcher ew = new EventWatcher (tsi);
95 tsi.AcceptsReturn = true;
96 Assert.AreEqual (true, tsi.AcceptsReturn, "B1");
97 Assert.AreEqual (string.Empty, ew.ToString (), "B2");
100 tsi.AcceptsReturn = true;
101 Assert.AreEqual (string.Empty, ew.ToString (), "B3");
105 public void PropertyAcceptsTab ()
107 ToolStripTextBox tsi = new ToolStripTextBox ();
108 EventWatcher ew = new EventWatcher (tsi);
110 tsi.AcceptsTab = true;
111 Assert.AreEqual (true, tsi.AcceptsTab, "B1");
112 Assert.AreEqual ("AcceptsTabChanged", ew.ToString (), "B2");
115 tsi.AcceptsTab = true;
116 Assert.AreEqual (string.Empty, ew.ToString (), "B3");
120 public void PropertyAutoCompleteCustomSource ()
122 ToolStripTextBox tsi = new ToolStripTextBox ();
123 EventWatcher ew = new EventWatcher (tsi);
125 AutoCompleteStringCollection acsc = new AutoCompleteStringCollection ();
126 acsc.AddRange (new string[] { "Apple", "Banana" });
128 tsi.AutoCompleteCustomSource = acsc;
129 Assert.AreSame (acsc, tsi.AutoCompleteCustomSource, "B1");
130 Assert.AreEqual (string.Empty, ew.ToString (), "B2");
133 tsi.AutoCompleteCustomSource = acsc;
134 Assert.AreEqual (string.Empty, ew.ToString (), "B3");
138 public void PropertyAutoCompleteMode ()
140 ToolStripTextBox tsi = new ToolStripTextBox ();
141 EventWatcher ew = new EventWatcher (tsi);
143 tsi.AutoCompleteMode = AutoCompleteMode.Append;
144 Assert.AreEqual (AutoCompleteMode.Append, tsi.AutoCompleteMode, "B1");
145 Assert.AreEqual (string.Empty, ew.ToString (), "B2");
148 tsi.AutoCompleteMode = AutoCompleteMode.Append;
149 Assert.AreEqual (string.Empty, ew.ToString (), "B3");
153 public void PropertyAutoCompleteSource ()
155 ToolStripTextBox tsi = new ToolStripTextBox ();
156 EventWatcher ew = new EventWatcher (tsi);
158 tsi.AutoCompleteSource = AutoCompleteSource.RecentlyUsedList;
159 Assert.AreEqual (AutoCompleteSource.RecentlyUsedList, tsi.AutoCompleteSource, "B1");
160 Assert.AreEqual (string.Empty, ew.ToString (), "B2");
163 tsi.AutoCompleteSource = AutoCompleteSource.RecentlyUsedList;
164 Assert.AreEqual (string.Empty, ew.ToString (), "B3");
168 public void PropertyBorderStyle ()
170 ToolStripTextBox tsi = new ToolStripTextBox ();
171 EventWatcher ew = new EventWatcher (tsi);
173 tsi.BorderStyle = BorderStyle.None;
174 Assert.AreEqual (BorderStyle.None, tsi.BorderStyle, "B1");
175 Assert.AreEqual ("BorderStyleChanged", ew.ToString (), "B2");
178 tsi.BorderStyle = BorderStyle.None;
179 Assert.AreEqual (string.Empty, ew.ToString (), "B3");
183 public void PropertyCharacterCasing ()
185 ToolStripTextBox tsi = new ToolStripTextBox ();
186 EventWatcher ew = new EventWatcher (tsi);
188 tsi.CharacterCasing = CharacterCasing.Lower;
189 Assert.AreEqual (CharacterCasing.Lower, tsi.CharacterCasing, "B1");
190 Assert.AreEqual (string.Empty, ew.ToString (), "B2");
193 tsi.CharacterCasing = CharacterCasing.Lower;
194 Assert.AreEqual (string.Empty, ew.ToString (), "B3");
198 public void PropertyHideSelection ()
200 ToolStripTextBox tsi = new ToolStripTextBox ();
201 EventWatcher ew = new EventWatcher (tsi);
203 tsi.HideSelection = false;
204 Assert.AreEqual (false, tsi.HideSelection, "B1");
205 Assert.AreEqual ("HideSelectionChanged", ew.ToString (), "B2");
208 tsi.HideSelection = false;
209 Assert.AreEqual (string.Empty, ew.ToString (), "B3");
213 [Ignore ("Problems in TextBox")]
214 public void PropertyLines ()
216 ToolStripTextBox tsi = new ToolStripTextBox ();
217 EventWatcher ew = new EventWatcher (tsi);
219 string[] lines = new string[] {"Apple", "Banana"};
222 Assert.AreEqual (lines, tsi.Lines, "B1");
223 Assert.AreEqual (string.Empty, ew.ToString (), "B2");
227 Assert.AreEqual (string.Empty, ew.ToString (), "B3");
231 public void PropertyMaxLength ()
233 ToolStripTextBox tsi = new ToolStripTextBox ();
234 EventWatcher ew = new EventWatcher (tsi);
237 Assert.AreEqual (15, tsi.MaxLength, "B1");
238 Assert.AreEqual (string.Empty, ew.ToString (), "B2");
242 Assert.AreEqual (string.Empty, ew.ToString (), "B3");
246 [Ignore ("TextBox does not raise ModifiedChanged")]
247 // When this works, also uncomment A12 in Constructor test above please
248 public void PropertyModified ()
250 ToolStripTextBox tsi = new ToolStripTextBox ();
251 EventWatcher ew = new EventWatcher (tsi);
254 Assert.AreEqual (true, tsi.Modified, "B1");
255 Assert.AreEqual ("ModifiedChanged", ew.ToString (), "B2");
259 Assert.AreEqual (string.Empty, ew.ToString (), "B3");
263 public void PropertyReadOnly ()
265 ToolStripTextBox tsi = new ToolStripTextBox ();
266 EventWatcher ew = new EventWatcher (tsi);
269 Assert.AreEqual (true, tsi.ReadOnly, "B1");
270 Assert.AreEqual ("ReadOnlyChanged", ew.ToString (), "B2");
274 Assert.AreEqual (string.Empty, ew.ToString (), "B3");
278 [Ignore ("TextBox does not raise ModifiedChanged")]
279 public void PropertySelectedText ()
281 ToolStripTextBox tsi = new ToolStripTextBox ();
282 EventWatcher ew = new EventWatcher (tsi);
284 tsi.Text = "Crumbelievable";
285 tsi.SelectedText = "lie";
287 Assert.AreEqual (string.Empty, tsi.SelectedText, "B1");
288 Assert.AreEqual ("ModifiedChanged", ew.ToString (), "B2");
291 tsi.SelectedText = "lie";
292 Assert.AreEqual (string.Empty, ew.ToString (), "B3");
296 public void PropertySelectionLength ()
298 ToolStripTextBox tsi = new ToolStripTextBox ();
299 EventWatcher ew = new EventWatcher (tsi);
301 tsi.Text = "Crumbelievable";
302 tsi.SelectionLength = 6;
303 Assert.AreEqual (6, tsi.SelectionLength, "B1");
304 Assert.AreEqual (string.Empty, ew.ToString (), "B2");
307 tsi.SelectionLength = 6;
308 Assert.AreEqual (string.Empty, ew.ToString (), "B3");
312 public void PropertySelectionStart ()
314 ToolStripTextBox tsi = new ToolStripTextBox ();
315 EventWatcher ew = new EventWatcher (tsi);
317 tsi.Text = "Crumbelievable";
318 tsi.SelectionStart = 4;
319 Assert.AreEqual (4, tsi.SelectionStart, "B1");
320 Assert.AreEqual (string.Empty, ew.ToString (), "B2");
323 tsi.SelectionStart = 4;
324 Assert.AreEqual (string.Empty, ew.ToString (), "B3");
328 public void PropertyShortcutsEnabled ()
330 ToolStripTextBox tsi = new ToolStripTextBox ();
331 EventWatcher ew = new EventWatcher (tsi);
333 tsi.ShortcutsEnabled = false;
334 Assert.AreEqual (false, tsi.ShortcutsEnabled, "B1");
335 Assert.AreEqual (string.Empty, ew.ToString (), "B2");
338 tsi.ShortcutsEnabled = false;
339 Assert.AreEqual (string.Empty, ew.ToString (), "B3");
343 public void PropertyTextBoxTextAlign ()
345 ToolStripTextBox tsi = new ToolStripTextBox ();
346 EventWatcher ew = new EventWatcher (tsi);
348 tsi.TextBoxTextAlign = HorizontalAlignment.Right;
349 Assert.AreEqual (HorizontalAlignment.Right, tsi.TextBoxTextAlign, "B1");
350 Assert.AreEqual ("TextBoxTextAlignChanged", ew.ToString (), "B2");
353 tsi.TextBoxTextAlign = HorizontalAlignment.Right;
354 Assert.AreEqual (string.Empty, ew.ToString (), "B3");
357 private class EventWatcher
359 private string events = string.Empty;
361 public EventWatcher (ToolStripTextBox tsi)
363 tsi.AcceptsTabChanged += new EventHandler (delegate (Object obj, EventArgs e) { events += ("AcceptsTabChanged;"); });
364 tsi.BorderStyleChanged += new EventHandler (delegate (Object obj, EventArgs e) { events += ("BorderStyleChanged;"); });
365 tsi.HideSelectionChanged += new EventHandler (delegate (Object obj, EventArgs e) { events += ("HideSelectionChanged;"); });
366 tsi.ModifiedChanged += new EventHandler (delegate (Object obj, EventArgs e) { events += ("ModifiedChanged;"); });
367 tsi.ReadOnlyChanged += new EventHandler (delegate (Object obj, EventArgs e) { events += ("ReadOnlyChanged;"); });
368 tsi.TextBoxTextAlignChanged += new EventHandler (delegate (Object obj, EventArgs e) { events += ("TextBoxTextAlignChanged;"); });
371 public override string ToString ()
373 return events.TrimEnd (';');
378 events = string.Empty;
382 private class ExposeProtectedProperties : ToolStripTextBox
384 public ExposeProtectedProperties () : base () {}
386 public new Padding DefaultMargin { get { return base.DefaultMargin; } }
387 public new Size DefaultSize { get { return base.DefaultSize; } }