Unit test for bug #821.
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / RichTextBoxTest.cs
1 //
2 // RichTextBoxTest.cs: Test cases for RichTextBox.
3 //
4 // Author:
5 //   Ritvik Mayank (mritvik@novell.com)
6 //
7 // (C) 2005 Novell, Inc. (http://www.novell.com)
8 //
9
10 using System;
11 using System.ComponentModel;
12 using System.Drawing;
13 using System.Reflection;
14 using System.Windows.Forms;
15
16 using NUnit.Framework;
17
18 namespace MonoTests.System.Windows.Forms
19 {
20         [TestFixture]
21         public class RichTextBoxTest : TestHelper
22         {
23                 [Test]
24                 public void RichTextBoxPropertyTest ()
25                 {
26                         RichTextBox rTBox = new RichTextBox ();
27                         
28                         // A
29                         Assert.AreEqual (false, rTBox.AllowDrop, "#A1");
30                         rTBox.Multiline = true;
31                         rTBox.AcceptsTab = true;
32                         //SendKeys.SendWait ("^%");
33                         Assert.AreEqual (false, rTBox.AutoSize, "#A2");
34                         Assert.AreEqual (false, rTBox.AutoWordSelection, "#A3");
35                         
36                         
37                         // B
38                         rTBox.BackColor = Color.White;
39                         Assert.AreEqual (null, rTBox.BackgroundImage, "#B1");
40                         string gif = "M.gif";
41                         rTBox.BackgroundImage = Image.FromFile (gif);
42                         // comparing image objects fails on MS .Net so using Size property
43                         Assert.AreEqual (Image.FromFile(gif, true).Size, rTBox.BackgroundImage.Size, "#B2");
44                         Assert.AreEqual (0, rTBox.BulletIndent, "#B3");
45                         
46                         // C
47                         //Assert.AreEqual (false, rTBox.CanRedo, "#C1");
48                         //rTBox.Paste ();
49                         //Assert.AreEqual (false, rTBox.CanRedo, "#C2");
50                         //rTBox.ClearUndo ();
51                         //Assert.AreEqual (false, rTBox.CanRedo, "#C3");
52
53                         // D
54                         Assert.AreEqual (true, rTBox.DetectUrls, "#D1");
55
56                         // F 
57                         Assert.AreEqual (FontStyle.Regular, rTBox.Font.Style, "#F1");
58                         Assert.AreEqual ("WindowText", rTBox.ForeColor.Name, "#F2");
59
60                         //M
61                         Assert.AreEqual (2147483647, rTBox.MaxLength, "#M1");
62                         Assert.AreEqual (true, rTBox.Multiline, "#M2");
63                         rTBox.WordWrap = false;
64                         Assert.AreEqual (true, rTBox.Multiline, "#M3");
65                         
66                         // R
67                         Assert.AreEqual ("", rTBox.RedoActionName, "#R1");
68                         Assert.AreEqual (0, rTBox.RightMargin, "#R2");
69                         
70                         // [MonoTODO ("Assert.AreEqual (false, rTBox.Rtf, "#R3");") ]
71
72                         // S
73 #if ONLY_1_1    
74                         Assert.AreEqual (null, rTBox.SelectedText, "#S3");
75 #else
76                         Assert.AreEqual ("", rTBox.SelectedText, "#S3");
77 #endif
78                         rTBox.Text = "sample TextBox";
79                         Assert.AreEqual (HorizontalAlignment.Left, rTBox.SelectionAlignment, "#S5");
80                         Assert.AreEqual (false, rTBox.SelectionBullet, "#S6");
81                         Assert.AreEqual (0, rTBox.SelectionCharOffset, "#S7");
82                         //Assert.AreEqual (Color.Black, rTBox.SelectionColor, "#S8"); // Random color
83                         //Assert.AreEqual ("Courier New", rTBox.SelectionFont.Name, "#S9a");
84                         Assert.AreEqual (FontStyle.Regular, rTBox.SelectionFont.Style, "#S9b");
85                         Assert.AreEqual (0, rTBox.SelectionHangingIndent, "#S10");
86                         Assert.AreEqual (0, rTBox.SelectionIndent, "#S11");
87                         //Assert.AreEqual (0, rTBox.SelectionLength, "#S12");
88                         Assert.AreEqual (false, rTBox.SelectionProtected, "#S13");
89                         Assert.AreEqual (0, rTBox.SelectionRightIndent, "#S14");
90                         Assert.AreEqual (false, rTBox.ShowSelectionMargin, "#S15");
91                         // [MonoTODO ("Assert.AreEqual (, rTBox.SelectionTabs, "#S16");")]
92                         // [MonoTODO("Assert.AreEqual (TypeCode.Empty, rTBox.SelectionType, "#S17");")] 
93                         
94                         // T
95                         Assert.AreEqual ("sample TextBox", rTBox.Text, "#T1");
96                         Assert.AreEqual (14, rTBox.TextLength, "#T2");
97                         
98                         // UVW 
99                         Assert.AreEqual ("", rTBox.UndoActionName, "#U1");
100
101                         // XYZ
102                         Assert.AreEqual (1, rTBox.ZoomFactor, "#Z1");
103                 }
104
105 #if not
106                 [Test]
107                 public void CanPasteTest ()
108                 {
109                         RichTextBox rTextBox = new RichTextBox ();
110                         Bitmap myBitmap = new Bitmap ("M.gif");
111                         Clipboard.SetDataObject (myBitmap);
112                         DataFormats.Format myFormat = DataFormats.GetFormat (DataFormats.Bitmap);
113                         Assert.AreEqual (true, rTextBox.CanPaste (myFormat), "#Mtd1");
114                 }
115 #endif
116
117                 [Test]
118                 public void BackColor ()
119                 {
120                         RichTextBox rtb = new RichTextBox ();
121                         Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#1");
122                         rtb.BackColor = Color.Red;
123                         Assert.AreEqual (Color.Red, rtb.BackColor, "#2");
124                         rtb.BackColor = Color.Empty;
125                         Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#3");
126                 }
127
128                 [Test] // bug #80626
129                 public void BorderStyle_None ()
130                 {
131                         RichTextBox rtb = new RichTextBox ();
132                         rtb.BorderStyle = BorderStyle.None;
133                         Assert.AreEqual (96, rtb.Height, "#1");
134                         rtb.CreateControl ();
135                         Assert.AreEqual (96, rtb.Height, "#2");
136                 }
137
138                 [Test] // bug #80620
139                 public void ClientRectangle_Borders ()
140                 {
141                         RichTextBox rtb = new RichTextBox ();
142                         rtb.CreateControl ();
143                         Assert.AreEqual (rtb.ClientRectangle, new RichTextBox ().ClientRectangle);
144                 }
145
146                 [Test]
147                 public void FindCharTest ()
148                 {
149                         RichTextBox rTextBox = new RichTextBox ();
150                         rTextBox.Text = "something";
151                         Assert.AreEqual (2, rTextBox.Find (new char [] {'m'}), "#Mtd3");
152                         Assert.AreEqual (-1, rTextBox.Find (new char [] {'t'},5), "#Mtd3a");
153                         Assert.AreEqual (4, rTextBox.Find (new char [] {'t'},4,5), "#Mtd3b");
154                 }
155                 
156                 [Test]
157                 public void FindStringTest ()
158                 {
159                         RichTextBox rTextBox = new RichTextBox ();
160                         rTextBox.Text = "sample text for richtextbox";
161                         int indexToText1 = rTextBox.Find ("for");
162                         Assert.AreEqual (12, indexToText1, "#Mtd4");
163                         int indexToText2 = rTextBox.Find ("for", 0, 14, RichTextBoxFinds.MatchCase);
164                         Assert.AreEqual (-1, indexToText2, "#Mtd5");
165                         int indexToText3 = rTextBox.Find ("for", 0, 15, RichTextBoxFinds.MatchCase);
166                         Assert.AreEqual (12, indexToText3, "#Mtd6");
167                         int indexToText4 = rTextBox.Find ("richtextbox", 0, RichTextBoxFinds.MatchCase);
168                         Assert.AreEqual (16, indexToText4, "#Mtd7");
169                         int indexToText5 = rTextBox.Find ("text", RichTextBoxFinds.MatchCase);
170                         Assert.AreEqual (7, indexToText5, "#Mtd8");
171                 }
172
173                 [Test]
174                 public void FindTest() {
175                         RichTextBox t = new RichTextBox();
176
177                         t.Text = "Testtext and arglblah may not be what we're looking for\n, but blah Blah is";
178
179                         Assert.AreEqual(t.Find(new char[] {'b', 'l', 'a', 'h'}), 9, "Find1");
180                         Assert.AreEqual(t.Find(new char[] {'b', 'l', 'a', 'h'}, 20), 20, "Find2");
181                         Assert.AreEqual(t.Find(new char[] {'b', 'l', 'a', 'h'}, 25, 30), -1, "Find3");
182                         Assert.AreEqual(t.Find("blah"), 17, "Find4");
183                         Assert.AreEqual(t.Find("blah", 10, 30, RichTextBoxFinds.None), 17, "Find5");
184                         Assert.AreEqual(t.Find("blah", 10, 30, RichTextBoxFinds.WholeWord), -1, "Find6");
185                         Assert.AreEqual(t.Find("blah", 10, 30, RichTextBoxFinds.MatchCase), 17, "Find7");
186                         Assert.AreEqual(t.Find("blah", 10, 70, RichTextBoxFinds.Reverse), 62, "Find8");
187                         Assert.AreEqual(t.Find("blah", 10, 73, RichTextBoxFinds.Reverse), 67, "Find9");
188                         Assert.AreEqual(t.Find("blah", 10, 73, RichTextBoxFinds.Reverse | RichTextBoxFinds.MatchCase), 62, "Find10");
189                         Assert.AreEqual(t.Find("blah", 10, RichTextBoxFinds.None), 17, "Find11");
190                         Assert.AreEqual(t.Find("blah", 10, RichTextBoxFinds.WholeWord), 62, "Find12");
191                         Assert.AreEqual(t.Find("blah", 10, RichTextBoxFinds.MatchCase), 17, "Find13");
192                         Assert.AreEqual(t.Find("blah", 10, RichTextBoxFinds.Reverse), 67, "Find14");
193                         Assert.AreEqual(t.Find("blah", 10, RichTextBoxFinds.Reverse | RichTextBoxFinds.MatchCase), 62, "Find15");
194                         Assert.AreEqual(t.Find("blah", RichTextBoxFinds.Reverse), 67, "Find16");
195                         Assert.AreEqual(t.Find("blah", RichTextBoxFinds.MatchCase), 17, "Find17");
196                         Assert.AreEqual(t.Find("blah", RichTextBoxFinds.WholeWord), 62, "Find18");
197
198                         // Special cases
199                         Assert.AreEqual(t.Find("blah", 10, 11, RichTextBoxFinds.None), -1, "Find19");   // Range to short to ever match
200                         Assert.AreEqual(t.Find("blah", 17, 18, RichTextBoxFinds.None), -1, "Find20");   // Range to short to ever match, but starts matching
201                         Assert.AreEqual(t.Find("is", RichTextBoxFinds.WholeWord), 72, "Find21");        // Last word in document
202                         Assert.AreEqual(t.Find("for", RichTextBoxFinds.WholeWord), 52, "Find22");       // word followed by \n
203                         Assert.AreEqual(t.Find("Testtext", RichTextBoxFinds.WholeWord), 0, "Find23");   // First word in document
204                         Assert.AreEqual(t.Find("Testtext", RichTextBoxFinds.WholeWord | RichTextBoxFinds.Reverse), 0, "Find24");        // First word in document, searched in reverse
205                 }
206
207                 [Test] // bug #80301
208                 [Ignore ("Depends on specific DPI")]
209                 public void PreferredHeight ()
210                 {
211                         RichTextBox rtb = new RichTextBox ();
212                         rtb.Font = new Font ("Arial", 14);
213                         Assert.AreEqual (29, rtb.PreferredHeight, "#A1");
214                         rtb.Font = new Font ("Arial", 16);
215                         Assert.AreEqual (32, rtb.PreferredHeight, "#A2");
216                         rtb.Font = new Font ("Arial", 17);
217                         Assert.AreEqual (34, rtb.PreferredHeight, "#A3");
218
219                         rtb.BorderStyle = BorderStyle.None;
220
221                         Assert.AreEqual (27, rtb.PreferredHeight, "#B1");
222                         rtb.Font = new Font ("Arial", 14);
223                         Assert.AreEqual (22, rtb.PreferredHeight, "#B2");
224                         rtb.Font = new Font ("Arial", 16);
225                         Assert.AreEqual (25, rtb.PreferredHeight, "#B3");
226                 }
227
228                 [Test]
229                 public void ReadOnly_BackColor_NotSet ()
230                 {
231                         RichTextBox rtb = new RichTextBox ();
232                         rtb.ReadOnly = true;
233                         Assert.IsTrue (rtb.ReadOnly, "#A1");
234 #if NET_2_0
235                         Assert.AreEqual (SystemColors.Control, rtb.BackColor, "#A2");
236 #else
237                         Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#A2");
238 #endif
239
240                         Form form = new Form ();
241                         form.ShowInTaskbar = false;
242                         form.Controls.Add (rtb);
243                         form.Show ();
244
245                         Assert.IsTrue (rtb.ReadOnly, "#B1");
246 #if NET_2_0
247                         Assert.AreEqual (SystemColors.Control, rtb.BackColor, "#B2");
248 #else
249                         Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#B2");
250 #endif
251
252                         rtb.ResetBackColor ();
253                         Assert.IsTrue (rtb.ReadOnly, "#C1");
254 #if NET_2_0
255                         Assert.AreEqual (SystemColors.Control, rtb.BackColor, "#C2");
256 #else
257                         Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#C2");
258 #endif
259
260                         rtb.ReadOnly = false;
261                         Assert.IsFalse (rtb.ReadOnly, "#D1");
262                         Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#D2");
263
264                         rtb.ReadOnly = true;
265                         Assert.IsTrue (rtb.ReadOnly, "#E1");
266 #if NET_2_0
267                         Assert.AreEqual (SystemColors.Control, rtb.BackColor, "#E2");
268 #else
269                         Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#E2");
270 #endif
271
272                         rtb.BackColor = Color.Red;
273                         Assert.IsTrue (rtb.ReadOnly, "#F1");
274                         Assert.AreEqual (Color.Red, rtb.BackColor, "#F2");
275
276                         rtb.ReadOnly = false;
277                         Assert.IsFalse (rtb.ReadOnly, "#G1");
278                         Assert.AreEqual (Color.Red, rtb.BackColor, "#G2");
279
280                         rtb.ReadOnly = true;
281                         Assert.IsTrue (rtb.ReadOnly, "#H1");
282                         Assert.AreEqual (Color.Red, rtb.BackColor, "#H2");
283
284                         rtb.ResetBackColor ();
285                         Assert.IsTrue (rtb.ReadOnly, "#I1");
286 #if NET_2_0
287                         Assert.AreEqual (SystemColors.Control, rtb.BackColor, "#I2");
288 #else
289                         Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#I2");
290 #endif
291                         form.Close ();
292                 }
293
294                 [Test]
295                 public void Modified ()
296                 {
297                         RichTextBox rtb = new RichTextBox ();
298                         Assert.AreEqual (false, rtb.Modified, "#A1");
299
300                         rtb.SelectedText = "mono";
301                         Assert.AreEqual (true, rtb.Modified, "#B1");
302
303                         rtb.Modified = false;
304                         Assert.AreEqual (false, rtb.Modified, "#C1");
305
306                         // Only SelectedText seems to cause a change in Modified, as opposed to Text
307                         rtb.Text = "moon";
308                         Assert.AreEqual (false, rtb.Modified, "#D1");
309                 }
310
311                 [Test]
312                 public void ReadOnly_BackColor_Set ()
313                 {
314                         RichTextBox rtb = new RichTextBox ();
315                         rtb.BackColor = Color.Blue;
316                         rtb.ReadOnly = true;
317                         Assert.IsTrue (rtb.ReadOnly, "#A1");
318                         Assert.AreEqual (Color.Blue, rtb.BackColor, "#A2");
319
320                         Form form = new Form ();
321                         form.ShowInTaskbar = false;
322                         form.Controls.Add (rtb);
323                         form.Show ();
324
325                         Assert.IsTrue (rtb.ReadOnly, "#B1");
326                         Assert.AreEqual (Color.Blue, rtb.BackColor, "#B2");
327
328                         rtb.ReadOnly = false;
329                         Assert.IsFalse (rtb.ReadOnly, "#C1");
330                         Assert.AreEqual (Color.Blue, rtb.BackColor, "#C2");
331
332                         rtb.ReadOnly = true;
333                         Assert.IsTrue (rtb.ReadOnly, "#D1");
334                         Assert.AreEqual (Color.Blue, rtb.BackColor, "#D2");
335
336                         rtb.BackColor = Color.Red;
337                         Assert.IsTrue (rtb.ReadOnly, "#E1");
338                         Assert.AreEqual (Color.Red, rtb.BackColor, "#E2");
339
340                         rtb.ReadOnly = false;
341                         Assert.IsFalse (rtb.ReadOnly, "#F1");
342                         Assert.AreEqual (Color.Red, rtb.BackColor, "#F2");
343
344                         rtb.ReadOnly = true;
345                         rtb.ResetBackColor ();
346                         Assert.IsTrue (rtb.ReadOnly, "#G1");
347 #if NET_2_0
348                         Assert.AreEqual (SystemColors.Control, rtb.BackColor, "#G2");
349 #else
350                         Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#G2");
351 #endif
352
353                         form.Dispose ();
354
355                         rtb = new RichTextBox ();
356                         rtb.ReadOnly = true;
357                         rtb.BackColor = Color.Blue;
358                         Assert.IsTrue (rtb.ReadOnly, "#H1");
359                         Assert.AreEqual (Color.Blue, rtb.BackColor, "#H2");
360
361                         form = new Form ();
362                         form.ShowInTaskbar = false;
363                         form.Controls.Add (rtb);
364                         form.Show ();
365
366                         Assert.IsTrue (rtb.ReadOnly, "#I1");
367                         Assert.AreEqual (Color.Blue, rtb.BackColor, "#I2");
368
369                         rtb.ReadOnly = false;
370                         Assert.IsFalse (rtb.ReadOnly, "#J1");
371                         Assert.AreEqual (Color.Blue, rtb.BackColor, "#J2");
372
373                         rtb.ResetBackColor ();
374                         Assert.IsFalse (rtb.ReadOnly, "#K1");
375                         Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#K2");
376                         
377                         form.Close ();
378                 }
379
380                 [Test]
381                 public void ScrollBarsTest ()
382                 {
383                         RichTextBox rtb = new RichTextBox ();
384                         Assert.AreEqual (RichTextBoxScrollBars.Both, rtb.ScrollBars, "#1");
385                         rtb.ScrollBars = RichTextBoxScrollBars.Vertical;
386                         Assert.AreEqual (RichTextBoxScrollBars.Vertical, rtb.ScrollBars, "#2");
387                 }
388
389                 [Test]
390                 [ExpectedException (typeof (InvalidEnumArgumentException))]
391                 public void ScrollBars_Invalid ()
392                 {
393                         RichTextBox rtb = new RichTextBox ();
394                         rtb.ScrollBars = (RichTextBoxScrollBars) 666;
395                 }
396
397                 [Test]
398                 public void SelectionFontTest ()
399                 {
400                         RichTextBox t = new RichTextBox();
401                         t.Text = "123";
402                         t.SelectionStart = 1;
403                         t.SelectionLength = 1;
404                         Font f = new Font(FontFamily.GenericMonospace, 120);
405                         t.SelectionFont = f;
406
407                         Assert.AreEqual (t.SelectionFont.Size, f.Size, "A1");
408                 }
409
410                 [Test]
411                 public void SelectionLength_Negative ()
412                 {
413                         RichTextBox rtb = new RichTextBox ();
414                         try {
415                                 rtb.SelectionLength = -1;
416                                 Assert.Fail ("#1");
417 #if NET_2_0
418                         } catch (ArgumentOutOfRangeException ex) {
419                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
420                                 Assert.IsNull (ex.InnerException, "#3");
421                                 Assert.IsNotNull (ex.Message, "#4");
422                                 Assert.IsNotNull (ex.ParamName, "#5");
423                                 Assert.AreEqual ("SelectionLength", ex.ParamName, "#6");
424                         }
425 #else
426                         } catch (ArgumentException ex) {
427                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
428                                 Assert.IsNull (ex.InnerException, "#3");
429                                 Assert.IsNotNull (ex.Message, "#4");
430                                 Assert.IsNull (ex.ParamName, "#5");
431                         }
432 #endif
433                 }
434
435                 [Test]
436                 public void SelectionStart_Negative ()
437                 {
438                         RichTextBox rtb = new RichTextBox ();
439                         try {
440                                 rtb.SelectionStart = -1;
441                                 Assert.Fail ("#1");
442 #if NET_2_0
443                         } catch (ArgumentOutOfRangeException ex) {
444                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
445                                 Assert.IsNull (ex.InnerException, "#3");
446                                 Assert.IsNotNull (ex.Message, "#4");
447                                 Assert.IsNotNull (ex.ParamName, "#5");
448                                 Assert.AreEqual ("SelectionStart", ex.ParamName, "#6");
449                         }
450 #else
451                         } catch (ArgumentException ex) {
452                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
453                                 Assert.IsNull (ex.InnerException, "#3");
454                                 Assert.IsNotNull (ex.Message, "#4");
455                                 Assert.IsNull (ex.ParamName, "#5");
456                         }
457 #endif
458                 }
459                 
460                 RichTextBox rtb;
461                 
462                 [Test]  // This test passes if it doesn't throw an NRE.
463                 public void Bug351886 ()
464                 {
465                         Form form = new Form ();
466                         rtb = new RichTextBox ();
467                         rtb.Dock = DockStyle.Fill;
468
469                         rtb.SelectionFont = new Font (FontFamily.GenericSansSerif, 72f);
470                         rtb.AppendText ("Left and make this very long so that it can be word wrapped ");
471
472                         form.Load += new EventHandler (Bug351886_Load);
473
474                         form.Controls.Add (rtb);
475                         form.Show ();
476                         
477                         form.Close ();
478                         form.Dispose ();
479                 }
480
481                 void Bug351886_Load (object sender, EventArgs e)
482                 {
483                         rtb.SelectAll ();
484                         rtb.SelectionAlignment = HorizontalAlignment.Center;
485                 }
486
487         }
488 }