2006-12-19 Daniel Nauck <dna@mono-project.de>
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / TextBoxTest.cs
1 //
2 // Copyright (c) 2005 Novell, Inc.
3 //
4 // Authors:
5 //      Ritvik Mayank (mritvik@novell.com)
6 //
7
8 using System;
9 using System.Windows.Forms;
10 using System.Drawing;
11 using System.Reflection;
12 using NUnit.Framework;
13
14 namespace MonoTests.System.Windows.Forms
15 {
16         [TestFixture]
17         public class TextBoxTest
18         {
19                 TextBox textBox;
20
21                 [SetUp]
22                 public void SetUp()
23                 {
24                         textBox = new TextBox();
25                 }
26
27                 [Test]
28                 [Category ("NotWorking")]
29                 public void TextBoxBasePropertyTest ()
30                 {
31                         Assert.AreEqual (false, textBox.AcceptsTab, "#1a");
32                         textBox.Multiline = true;
33                         textBox.AcceptsTab = true;
34                         //      SendKeys.SendWait ("^%");
35                         Assert.AreEqual (true, textBox.AcceptsTab, "#1b");
36                         Assert.AreEqual (true, textBox.AutoSize, "#2");
37                         Assert.AreEqual ("Window", textBox.BackColor.Name, "#3a");
38                         textBox.BackColor = Color.White;
39                         Assert.AreEqual ("White", textBox.BackColor.Name, "#3b");
40                         Assert.AreEqual (null, textBox.BackgroundImage, "#4a");
41                         string gif = "M.gif";
42                         textBox.BackgroundImage = Image.FromFile (gif);
43                         // comparing image objects fails on MS .Net so using Size property
44                         Assert.AreEqual (Image.FromFile(gif, true).Size, textBox.BackgroundImage.Size, "#4b");
45                         
46                         Assert.AreEqual (BorderStyle.Fixed3D, textBox.BorderStyle, "#5");
47                         Assert.AreEqual (false, textBox.CanUndo, "#6a");
48
49                         Clipboard.SetDataObject ("TEST");
50                         textBox.Paste ();
51                         Assert.AreEqual (true, textBox.CanUndo, "#6b");
52                         textBox.Undo ();
53                         textBox.ClearUndo ();
54                         Assert.AreEqual (false, textBox.CanUndo, "#6c");
55
56                         Assert.AreEqual ("WindowText", textBox.ForeColor.Name, "#7");
57                         Assert.AreEqual (true, textBox.HideSelection, "#8");
58                         Assert.AreEqual (1, textBox.Lines.Length, "#9");
59                         Assert.AreEqual (32767, textBox.MaxLength, "#10");
60                         Assert.AreEqual (true, textBox.Modified, "#11");
61                         Assert.AreEqual (true, textBox.Multiline, "#12a");
62                         textBox.WordWrap = false;
63                         Assert.AreEqual (true, textBox.Multiline, "#12b");
64                         textBox.AcceptsReturn = true;
65                         Assert.AreEqual (true, textBox.Multiline, "#12c");
66                         Assert.AreEqual (20, textBox.PreferredHeight, "#13");
67                         Assert.AreEqual (false, textBox.ReadOnly, "#14");
68                         Assert.AreEqual ("", textBox.SelectedText, "#15");
69                         textBox.Text = "sample TextBox";
70                         Assert.AreEqual (0, textBox.SelectionLength, "#16b");
71                         Assert.AreEqual (0, textBox.SelectionStart, "#17");
72                         textBox.WordWrap = false;
73                         textBox.AcceptsReturn = true;
74                         Assert.AreEqual ("sample TextBox", textBox.Text, "#18");
75                         Assert.AreEqual (14, textBox.TextLength, "#19");
76                         Assert.AreEqual (false, textBox.WordWrap, "#20");
77                 }
78
79                 [Test]
80                 public void TextBoxPropertyTest ()
81                 {
82                         Assert.AreEqual (false, textBox.AcceptsReturn, "#21");
83                         Assert.AreEqual (CharacterCasing.Normal, textBox.CharacterCasing, "#22");
84                         Assert.AreEqual ('\0', textBox.PasswordChar, "#23");
85                         textBox.PasswordChar = '*';
86                         Assert.AreEqual ('*', textBox.PasswordChar, "#23b");
87                         Assert.AreEqual (ScrollBars.None, textBox.ScrollBars, "#24");
88                         Assert.AreEqual (-1, textBox.SelectionLength, "#25");
89                         Assert.AreEqual (HorizontalAlignment.Left , textBox.TextAlign, "#26");
90                 }
91
92 #if NET_2_0
93                 [Test]
94                 public void UseSystemPasswordCharDefault()
95                 {
96                         Assert.IsFalse(textBox.UseSystemPasswordChar);
97                 }
98
99                 [Test]
100                 public void UseSystemPasswordCharOverridesPasswordChar()
101                 {
102                         textBox.PasswordChar = '!';
103                         textBox.UseSystemPasswordChar = true;
104                         Assert.AreEqual('*', textBox.PasswordChar);
105                 }
106 #endif
107
108                 [Test]
109                 public void AppendTextTest ()
110                 {
111                         Form f = new Form (); 
112                         f.ShowInTaskbar = false;
113                         f.Visible = true;
114                         textBox.Visible = true;
115                         textBox.Text = "TextBox1";
116                         TextBox textBox2 = new TextBox ();
117                         textBox2.Visible = true;
118                         f.Controls.Add (textBox);
119                         f.Controls.Add (textBox2);
120                         textBox2.AppendText (textBox.Text);
121                         Assert.AreEqual ("TextBox1", textBox2.Text, "#27");
122                         f.Dispose ();
123                 }
124
125                 [Test]
126                 public void AppendTextTest2 ()
127                 {
128                         TextBox textBox2 = new TextBox ();
129                         textBox2.AppendText ("hi");
130                         textBox2.AppendText ("ho");
131                         Assert.AreEqual ("hiho", textBox2.Text, "#1");
132                         Assert.IsNotNull (textBox2.Lines, "#2");
133                         Assert.AreEqual (1, textBox2.Lines.Length, "#3");
134                         Assert.AreEqual ("hiho", textBox2.Lines [0], "#4");
135                 }
136
137                 [Test]
138                 public void AppendText_Multiline_CRLF ()
139                 {
140                         TextBox textBox = new TextBox ();
141                         textBox.Text = "ha";
142                         textBox.AppendText ("hi\r\n\r\n");
143                         textBox.AppendText ("ho\r\n");
144                         Assert.AreEqual ("hahi\r\n\r\nho\r\n", textBox.Text, "#A1");
145                         Assert.IsNotNull (textBox.Lines, "#A2");
146                         Assert.AreEqual (4, textBox.Lines.Length, "#A3");
147                         Assert.AreEqual ("hahi", textBox.Lines [0], "#A4");
148                         Assert.AreEqual (string.Empty, textBox.Lines [1], "#A5");
149                         Assert.AreEqual ("ho", textBox.Lines [2], "#A6");
150                         Assert.AreEqual (string.Empty, textBox.Lines [3], "#A7");
151
152                         textBox.Multiline = true;
153
154                         textBox.Text = "ha";
155                         textBox.AppendText ("hi\r\n\r\n");
156                         textBox.AppendText ("ho\r\n");
157                         Assert.AreEqual ("hahi\r\n\r\nho\r\n", textBox.Text, "#B1");
158                         Assert.IsNotNull (textBox.Lines, "#B2");
159                         Assert.AreEqual (4, textBox.Lines.Length, "#B3");
160                         Assert.AreEqual ("hahi", textBox.Lines [0], "#B4");
161                         Assert.AreEqual (string.Empty, textBox.Lines [1], "#B5");
162                         Assert.AreEqual ("ho", textBox.Lines [2], "#B6");
163                         Assert.AreEqual (string.Empty, textBox.Lines [3], "#B7");
164                 }
165
166                 [Test]
167                 public void AppendText_Multiline_LF ()
168                 {
169                         TextBox textBox = new TextBox ();
170
171                         textBox.Text = "ha";
172                         textBox.AppendText ("hi\n\n");
173                         textBox.AppendText ("ho\n");
174                         Assert.AreEqual ("hahi\n\nho\n", textBox.Text, "#A1");
175                         Assert.IsNotNull (textBox.Lines, "#A2");
176                         Assert.AreEqual (4, textBox.Lines.Length, "#A3");
177                         Assert.AreEqual ("hahi", textBox.Lines [0], "#A4");
178                         Assert.AreEqual (string.Empty, textBox.Lines [1], "#A5");
179                         Assert.AreEqual ("ho", textBox.Lines [2], "#A6");
180                         Assert.AreEqual (string.Empty, textBox.Lines [3], "#A7");
181
182                         textBox.Multiline = true;
183
184                         textBox.Text = "ha";
185                         textBox.AppendText ("hi\n\n");
186                         textBox.AppendText ("ho\n");
187                         Assert.AreEqual ("hahi\n\nho\n", textBox.Text, "#B1");
188                         Assert.IsNotNull (textBox.Lines, "#B2");
189                         Assert.AreEqual (4, textBox.Lines.Length, "#B3");
190                         Assert.AreEqual ("hahi", textBox.Lines [0], "#B4");
191                         Assert.AreEqual (string.Empty, textBox.Lines [1], "#B5");
192                         Assert.AreEqual ("ho", textBox.Lines [2], "#B6");
193                         Assert.AreEqual (string.Empty, textBox.Lines [3], "#B7");
194                 }
195
196                 [Test]
197                 public void ClearTest ()
198                 {
199                         textBox.Text = "TextBox1";
200                         Assert.AreEqual ("TextBox1", textBox.Text, "#28a" );
201                         textBox.Clear ();
202                         Assert.AreEqual ("", textBox.Text, "#28b");
203                 }
204
205                 [Test]
206                 public void ClearUndoTest ()
207                 {
208                         textBox.Text = "TextBox1";
209                         textBox.SelectionLength = 4;
210                         textBox.Copy ();
211                         Assert.AreEqual ("Text", textBox.SelectedText, "#29a");
212                         textBox.Paste ();
213                         Assert.AreEqual (true, textBox.CanUndo, "#29b");
214                         textBox.ClearUndo ();
215                         Assert.AreEqual (false, textBox.CanUndo, "#29c");
216                 }
217
218                 [Test] // bug #80163
219                 public void ContextMenu ()
220                 {
221                         TextBox textBox = new TextBox ();
222                         Assert.IsNull (textBox.ContextMenu);
223                 }
224
225                 [Test]
226                 public void CopyTest ()
227                 {
228                         textBox.Text = "ABCDE";
229                         textBox.SelectionLength = 4;
230                         textBox.Copy ();
231                         Assert.AreEqual ("ABCD", textBox.SelectedText, "#30");
232                 }
233
234                 [Test]
235                 public void CutTest ()
236                 {
237                         textBox.Text = "ABCDE";
238                         textBox.SelectionLength = 4;
239                         textBox.Cut ();
240                         Assert.AreEqual ("E", textBox.Text, "#31");
241                 }
242
243                 [Test]
244                 public void PasteTest ()
245                 {
246                         textBox.Text = "ABCDE";
247                         textBox.SelectionLength = 4;
248                         textBox.Copy ();
249                         textBox.SelectionStart = textBox.SelectionStart + textBox.SelectionLength;
250                         textBox.Paste ();
251                         Assert.AreEqual ("ABCDABCD", textBox.Text, "#32");
252                 }
253
254                 [Test]
255                 public void SelectTest ()
256                 {
257                         textBox.Text = "This is a sample test.";
258                         textBox.Select (0, 4);
259                         Assert.AreEqual ("This", textBox.SelectedText, "#33");
260                 }
261
262                 [Test]
263                 public void SelectAllTest ()
264                 {
265                         textBox.Text = "This is a sample test.";
266                         textBox.SelectAll ();
267                         Assert.AreEqual ("This is a sample test.", textBox.SelectedText, "#34");
268                 }
269
270                 [Test]
271                 public void FocusSelectsAllTest ()
272                 {
273                         textBox.Text = "This is a sample test.";
274                         textBox.CreateControl ();
275                         textBox.Focus ();
276                         Assert.AreEqual ("This is a sample test.", textBox.SelectedText, "#34");
277                 }
278
279                 [Test]
280                 public void ToStringTest ()
281                 {
282                         Assert.AreEqual ("System.Windows.Forms.TextBox, Text: ", textBox.ToString(), "#35");
283                 }
284
285                 [Test]
286                 public void UndoTest1 ()
287                 {
288                         textBox.Text = "ABCDE";
289                         textBox.SelectionLength = 4;
290                         textBox.Copy ();
291                         textBox.SelectionStart = textBox.SelectionStart + textBox.SelectionLength;
292                         textBox.Paste ();
293                         Console.WriteLine ("pre paste text:  {0}", textBox.Text);
294                         textBox.Undo ();
295                         Assert.AreEqual ("ABCDE", textBox.Text, "#36");
296                 }
297
298                 [Test] // bug #79851
299                 public void WrappedText ()
300                 {
301                         string text = "blabla blablabalbalbalbalbalbal blabla blablabl bal " +
302                                 "bal bla bal balajkdhfk dskfk ersd dsfjksdhf sdkfjshd f";
303
304                         textBox.Multiline = true;
305                         textBox.Size = new Size (30, 168);
306                         textBox.Text = text;
307
308                         Form form = new Form ();
309                         form.Controls.Add (textBox);
310                         form.ShowInTaskbar = false;
311                         form.Show ();
312
313                         Assert.AreEqual (text, textBox.Text);
314                 }
315
316                 [Test] // bug #79909
317                 public void MultilineText ()
318                 {
319                         string text = "line1\n\nline2\nline3\r\nline4";
320
321                         textBox.Size = new Size (300, 168);
322                         textBox.Text = text;
323
324                         Form form = new Form ();
325                         form.Controls.Add (textBox);
326                         form.ShowInTaskbar = false;
327                         form.Show ();
328
329                         Assert.AreEqual (text, textBox.Text, "#1");
330
331                         text = "line1\n\nline2\nline3\r\nline4\rline5\r\n\nline6\n\n\nline7";
332
333                         textBox.Text = text;
334
335                         form.Visible = false;
336                         form.Show ();
337
338                         Assert.AreEqual (text, textBox.Text, "#2");
339                 }
340
341                 
342                 [Test]
343                 public void ModifiedTest ()
344                 {
345                         Assert.AreEqual (true, textBox.Modified, "modified-1");
346
347                         textBox.Modified = false;
348                         Assert.AreEqual (false, textBox.Modified, "modified-2");
349
350                         textBox.Modified = true;
351                         Assert.AreEqual (true, textBox.Modified, "modified-2");
352
353                         textBox.Text = "TEXT";
354                         Assert.AreEqual (false, textBox.Modified, "modified-3");
355                 }
356         }
357 }