Fix a few CRLF issues
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / ToolStripTextBoxTest.cs
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 (monkey@jpobst.com)
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
36 namespace MonoTests.System.Windows.Forms
37 {
38         [TestFixture]
39         public class ToolStripTextBoxTests : TestHelper
40         {
41                 [Test]
42                 public void Constructor ()
43                 {
44                         ToolStripTextBox tsi = new ToolStripTextBox ();
45
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");
57                         //Bug in TextBox
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");
67
68                         tsi = new ToolStripTextBox ("Bob");
69                         Assert.AreEqual ("Bob", tsi.Name, "A21");
70                         Assert.AreEqual (string.Empty, tsi.Control.Name, "A22");
71                 }
72         
73                 [Test]
74                 [ExpectedException (typeof (NotSupportedException))]
75                 public void ConstructorNSE ()
76                 {
77                         new ToolStripTextBox (new TextBox ());
78                 }
79                 
80                 [Test]
81                 public void ProtectedProperties ()
82                 {
83                         ExposeProtectedProperties epp = new ExposeProtectedProperties ();
84
85                         Assert.AreEqual (new Padding (1, 0, 1, 0), epp.DefaultMargin, "C1");
86                         Assert.AreEqual (new Size (100, 22), epp.DefaultSize, "C2");
87                 }
88
89                 [Test]
90                 public void PropertyAcceptsReturn ()
91                 {
92                         ToolStripTextBox tsi = new ToolStripTextBox ();
93                         EventWatcher ew = new EventWatcher (tsi);
94
95                         tsi.AcceptsReturn = true;
96                         Assert.AreEqual (true, tsi.AcceptsReturn, "B1");
97                         Assert.AreEqual (string.Empty, ew.ToString (), "B2");
98
99                         ew.Clear ();
100                         tsi.AcceptsReturn = true;
101                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
102                 }
103
104                 [Test]
105                 public void PropertyAcceptsTab ()
106                 {
107                         ToolStripTextBox tsi = new ToolStripTextBox ();
108                         EventWatcher ew = new EventWatcher (tsi);
109
110                         tsi.AcceptsTab = true;
111                         Assert.AreEqual (true, tsi.AcceptsTab, "B1");
112                         Assert.AreEqual ("AcceptsTabChanged", ew.ToString (), "B2");
113
114                         ew.Clear ();
115                         tsi.AcceptsTab = true;
116                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
117                 }
118
119                 [Test]
120                 public void PropertyAutoCompleteCustomSource ()
121                 {
122                         ToolStripTextBox tsi = new ToolStripTextBox ();
123                         EventWatcher ew = new EventWatcher (tsi);
124
125                         AutoCompleteStringCollection acsc = new AutoCompleteStringCollection ();
126                         acsc.AddRange (new string[] { "Apple", "Banana" });
127
128                         tsi.AutoCompleteCustomSource = acsc;
129                         Assert.AreSame (acsc, tsi.AutoCompleteCustomSource, "B1");
130                         Assert.AreEqual (string.Empty, ew.ToString (), "B2");
131
132                         ew.Clear ();
133                         tsi.AutoCompleteCustomSource = acsc;
134                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
135                 }
136
137                 [Test]
138                 public void PropertyAutoCompleteMode ()
139                 {
140                         ToolStripTextBox tsi = new ToolStripTextBox ();
141                         EventWatcher ew = new EventWatcher (tsi);
142
143                         tsi.AutoCompleteMode = AutoCompleteMode.Append;
144                         Assert.AreEqual (AutoCompleteMode.Append, tsi.AutoCompleteMode, "B1");
145                         Assert.AreEqual (string.Empty, ew.ToString (), "B2");
146
147                         ew.Clear ();
148                         tsi.AutoCompleteMode = AutoCompleteMode.Append;
149                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
150                 }
151
152                 [Test]
153                 public void PropertyAutoCompleteSource ()
154                 {
155                         ToolStripTextBox tsi = new ToolStripTextBox ();
156                         EventWatcher ew = new EventWatcher (tsi);
157
158                         tsi.AutoCompleteSource = AutoCompleteSource.RecentlyUsedList;
159                         Assert.AreEqual (AutoCompleteSource.RecentlyUsedList, tsi.AutoCompleteSource, "B1");
160                         Assert.AreEqual (string.Empty, ew.ToString (), "B2");
161
162                         ew.Clear ();
163                         tsi.AutoCompleteSource = AutoCompleteSource.RecentlyUsedList;
164                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
165                 }
166
167                 [Test]
168                 public void PropertyBorderStyle ()
169                 {
170                         ToolStripTextBox tsi = new ToolStripTextBox ();
171                         EventWatcher ew = new EventWatcher (tsi);
172
173                         tsi.BorderStyle = BorderStyle.None;
174                         Assert.AreEqual (BorderStyle.None, tsi.BorderStyle, "B1");
175                         Assert.AreEqual ("BorderStyleChanged", ew.ToString (), "B2");
176
177                         ew.Clear ();
178                         tsi.BorderStyle = BorderStyle.None;
179                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
180                 }
181
182                 [Test]
183                 public void PropertyCharacterCasing ()
184                 {
185                         ToolStripTextBox tsi = new ToolStripTextBox ();
186                         EventWatcher ew = new EventWatcher (tsi);
187
188                         tsi.CharacterCasing = CharacterCasing.Lower;
189                         Assert.AreEqual (CharacterCasing.Lower, tsi.CharacterCasing, "B1");
190                         Assert.AreEqual (string.Empty, ew.ToString (), "B2");
191
192                         ew.Clear ();
193                         tsi.CharacterCasing = CharacterCasing.Lower;
194                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
195                 }
196
197                 [Test]
198                 public void PropertyHideSelection ()
199                 {
200                         ToolStripTextBox tsi = new ToolStripTextBox ();
201                         EventWatcher ew = new EventWatcher (tsi);
202
203                         tsi.HideSelection = false;
204                         Assert.AreEqual (false, tsi.HideSelection, "B1");
205                         Assert.AreEqual ("HideSelectionChanged", ew.ToString (), "B2");
206
207                         ew.Clear ();
208                         tsi.HideSelection = false;
209                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
210                 }
211
212                 [Test]
213                 [Ignore ("Problems in TextBox")]
214                 public void PropertyLines ()
215                 {
216                         ToolStripTextBox tsi = new ToolStripTextBox ();
217                         EventWatcher ew = new EventWatcher (tsi);
218
219                         string[] lines = new string[] {"Apple", "Banana"};
220                         tsi.Lines = lines;
221                         
222                         Assert.AreEqual (lines, tsi.Lines, "B1");
223                         Assert.AreEqual (string.Empty, ew.ToString (), "B2");
224
225                         ew.Clear ();
226                         tsi.Lines = lines;
227                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
228                 }
229
230                 [Test]
231                 public void PropertyMaxLength ()
232                 {
233                         ToolStripTextBox tsi = new ToolStripTextBox ();
234                         EventWatcher ew = new EventWatcher (tsi);
235
236                         tsi.MaxLength = 15;
237                         Assert.AreEqual (15, tsi.MaxLength, "B1");
238                         Assert.AreEqual (string.Empty, ew.ToString (), "B2");
239
240                         ew.Clear ();
241                         tsi.MaxLength = 15;
242                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
243                 }
244
245                 [Test]
246                 [Ignore ("TextBox does not raise ModifiedChanged")]
247                 // When this works, also uncomment A12 in Constructor test above please
248                 public void PropertyModified ()
249                 {
250                         ToolStripTextBox tsi = new ToolStripTextBox ();
251                         EventWatcher ew = new EventWatcher (tsi);
252
253                         tsi.Modified = true;
254                         Assert.AreEqual (true, tsi.Modified, "B1");
255                         Assert.AreEqual ("ModifiedChanged", ew.ToString (), "B2");
256
257                         ew.Clear ();
258                         tsi.Modified = true;
259                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
260                 }
261
262                 [Test]
263                 public void PropertyReadOnly ()
264                 {
265                         ToolStripTextBox tsi = new ToolStripTextBox ();
266                         EventWatcher ew = new EventWatcher (tsi);
267
268                         tsi.ReadOnly = true;
269                         Assert.AreEqual (true, tsi.ReadOnly, "B1");
270                         Assert.AreEqual ("ReadOnlyChanged", ew.ToString (), "B2");
271
272                         ew.Clear ();
273                         tsi.ReadOnly = true;
274                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
275                 }
276
277                 [Test]
278                 [Ignore ("TextBox does not raise ModifiedChanged")]
279                 public void PropertySelectedText ()
280                 {
281                         ToolStripTextBox tsi = new ToolStripTextBox ();
282                         EventWatcher ew = new EventWatcher (tsi);
283
284                         tsi.Text = "Crumbelievable";
285                         tsi.SelectedText = "lie";
286
287                         Assert.AreEqual (string.Empty, tsi.SelectedText, "B1");
288                         Assert.AreEqual ("ModifiedChanged", ew.ToString (), "B2");
289
290                         ew.Clear ();
291                         tsi.SelectedText = "lie";
292                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
293                 }
294
295                 [Test]
296                 public void PropertySelectionLength ()
297                 {
298                         ToolStripTextBox tsi = new ToolStripTextBox ();
299                         EventWatcher ew = new EventWatcher (tsi);
300
301                         tsi.Text = "Crumbelievable";
302                         tsi.SelectionLength = 6;
303                         Assert.AreEqual (6, tsi.SelectionLength, "B1");
304                         Assert.AreEqual (string.Empty, ew.ToString (), "B2");
305
306                         ew.Clear ();
307                         tsi.SelectionLength = 6;
308                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
309                 }
310
311                 [Test]
312                 public void PropertySelectionStart ()
313                 {
314                         ToolStripTextBox tsi = new ToolStripTextBox ();
315                         EventWatcher ew = new EventWatcher (tsi);
316
317                         tsi.Text = "Crumbelievable";
318                         tsi.SelectionStart = 4;
319                         Assert.AreEqual (4, tsi.SelectionStart, "B1");
320                         Assert.AreEqual (string.Empty, ew.ToString (), "B2");
321
322                         ew.Clear ();
323                         tsi.SelectionStart = 4;
324                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
325                 }
326
327                 [Test]
328                 public void PropertyShortcutsEnabled ()
329                 {
330                         ToolStripTextBox tsi = new ToolStripTextBox ();
331                         EventWatcher ew = new EventWatcher (tsi);
332
333                         tsi.ShortcutsEnabled = false;
334                         Assert.AreEqual (false, tsi.ShortcutsEnabled, "B1");
335                         Assert.AreEqual (string.Empty, ew.ToString (), "B2");
336
337                         ew.Clear ();
338                         tsi.ShortcutsEnabled = false;
339                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
340                 }
341
342                 [Test]
343                 public void PropertyTextBoxTextAlign ()
344                 {
345                         ToolStripTextBox tsi = new ToolStripTextBox ();
346                         EventWatcher ew = new EventWatcher (tsi);
347
348                         tsi.TextBoxTextAlign = HorizontalAlignment.Right;
349                         Assert.AreEqual (HorizontalAlignment.Right, tsi.TextBoxTextAlign, "B1");
350                         Assert.AreEqual ("TextBoxTextAlignChanged", ew.ToString (), "B2");
351
352                         ew.Clear ();
353                         tsi.TextBoxTextAlign = HorizontalAlignment.Right;
354                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
355                 }
356
357                 private class EventWatcher
358                 {
359                         private string events = string.Empty;
360                         
361                         public EventWatcher (ToolStripTextBox tsi)
362                         {
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;"); });
369                         }
370
371                         public override string ToString ()
372                         {
373                                 return events.TrimEnd (';');
374                         }
375                         
376                         public void Clear ()
377                         {
378                                 events = string.Empty;
379                         }
380                 }
381                 
382                 private class ExposeProtectedProperties : ToolStripTextBox
383                 {
384                         public ExposeProtectedProperties () : base () {}
385
386                         public new Padding DefaultMargin { get { return base.DefaultMargin; } }
387                         public new Size DefaultSize { get { return base.DefaultSize; } }
388                 }
389         }
390 }
391 #endif