2 // RichTextBoxTest.cs: Test cases for RichTextBox.
5 // Ritvik Mayank (mritvik@novell.com)
7 // (C) 2005 Novell, Inc. (http://www.novell.com)
11 using System.ComponentModel;
13 using System.Reflection;
14 using System.Windows.Forms;
16 using NUnit.Framework;
18 namespace MonoTests.System.Windows.Forms
21 public class RichTextBoxTest : TestHelper
24 public void RichTextBoxPropertyTest ()
26 RichTextBox rTBox = new RichTextBox ();
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");
38 rTBox.BackColor = Color.White;
39 Assert.AreEqual (null, rTBox.BackgroundImage, "#B1");
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");
47 //Assert.AreEqual (false, rTBox.CanRedo, "#C1");
49 //Assert.AreEqual (false, rTBox.CanRedo, "#C2");
51 //Assert.AreEqual (false, rTBox.CanRedo, "#C3");
54 Assert.AreEqual (true, rTBox.DetectUrls, "#D1");
57 Assert.AreEqual (FontStyle.Regular, rTBox.Font.Style, "#F1");
58 Assert.AreEqual ("WindowText", rTBox.ForeColor.Name, "#F2");
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");
67 Assert.AreEqual ("", rTBox.RedoActionName, "#R1");
68 Assert.AreEqual (0, rTBox.RightMargin, "#R2");
70 // [MonoTODO ("Assert.AreEqual (false, rTBox.Rtf, "#R3");") ]
74 Assert.AreEqual (null, rTBox.SelectedText, "#S3");
76 Assert.AreEqual ("", rTBox.SelectedText, "#S3");
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");")]
95 Assert.AreEqual ("sample TextBox", rTBox.Text, "#T1");
96 Assert.AreEqual (14, rTBox.TextLength, "#T2");
99 Assert.AreEqual ("", rTBox.UndoActionName, "#U1");
102 Assert.AreEqual (1, rTBox.ZoomFactor, "#Z1");
107 public void CanPasteTest ()
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");
118 public void BackColor ()
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");
129 public void BorderStyle_None ()
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");
139 public void ClientRectangle_Borders ()
141 RichTextBox rtb = new RichTextBox ();
142 rtb.CreateControl ();
143 Assert.AreEqual (rtb.ClientRectangle, new RichTextBox ().ClientRectangle);
147 public void FindCharTest ()
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");
157 public void FindStringTest ()
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");
174 public void FindTest() {
175 RichTextBox t = new RichTextBox();
177 t.Text = "Testtext and arglblah may not be what we're looking for\n, but blah Blah is";
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");
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
208 [Ignore ("Depends on specific DPI")]
209 public void PreferredHeight ()
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");
219 rtb.BorderStyle = BorderStyle.None;
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");
229 public void ReadOnly_BackColor_NotSet ()
231 RichTextBox rtb = new RichTextBox ();
233 Assert.IsTrue (rtb.ReadOnly, "#A1");
235 Assert.AreEqual (SystemColors.Control, rtb.BackColor, "#A2");
237 Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#A2");
240 Form form = new Form ();
241 form.ShowInTaskbar = false;
242 form.Controls.Add (rtb);
245 Assert.IsTrue (rtb.ReadOnly, "#B1");
247 Assert.AreEqual (SystemColors.Control, rtb.BackColor, "#B2");
249 Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#B2");
252 rtb.ResetBackColor ();
253 Assert.IsTrue (rtb.ReadOnly, "#C1");
255 Assert.AreEqual (SystemColors.Control, rtb.BackColor, "#C2");
257 Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#C2");
260 rtb.ReadOnly = false;
261 Assert.IsFalse (rtb.ReadOnly, "#D1");
262 Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#D2");
265 Assert.IsTrue (rtb.ReadOnly, "#E1");
267 Assert.AreEqual (SystemColors.Control, rtb.BackColor, "#E2");
269 Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#E2");
272 rtb.BackColor = Color.Red;
273 Assert.IsTrue (rtb.ReadOnly, "#F1");
274 Assert.AreEqual (Color.Red, rtb.BackColor, "#F2");
276 rtb.ReadOnly = false;
277 Assert.IsFalse (rtb.ReadOnly, "#G1");
278 Assert.AreEqual (Color.Red, rtb.BackColor, "#G2");
281 Assert.IsTrue (rtb.ReadOnly, "#H1");
282 Assert.AreEqual (Color.Red, rtb.BackColor, "#H2");
284 rtb.ResetBackColor ();
285 Assert.IsTrue (rtb.ReadOnly, "#I1");
287 Assert.AreEqual (SystemColors.Control, rtb.BackColor, "#I2");
289 Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#I2");
295 public void Modified ()
297 RichTextBox rtb = new RichTextBox ();
298 Assert.AreEqual (false, rtb.Modified, "#A1");
300 rtb.SelectedText = "mono";
301 Assert.AreEqual (true, rtb.Modified, "#B1");
303 rtb.Modified = false;
304 Assert.AreEqual (false, rtb.Modified, "#C1");
306 // Only SelectedText seems to cause a change in Modified, as opposed to Text
308 Assert.AreEqual (false, rtb.Modified, "#D1");
312 public void ReadOnly_BackColor_Set ()
314 RichTextBox rtb = new RichTextBox ();
315 rtb.BackColor = Color.Blue;
317 Assert.IsTrue (rtb.ReadOnly, "#A1");
318 Assert.AreEqual (Color.Blue, rtb.BackColor, "#A2");
320 Form form = new Form ();
321 form.ShowInTaskbar = false;
322 form.Controls.Add (rtb);
325 Assert.IsTrue (rtb.ReadOnly, "#B1");
326 Assert.AreEqual (Color.Blue, rtb.BackColor, "#B2");
328 rtb.ReadOnly = false;
329 Assert.IsFalse (rtb.ReadOnly, "#C1");
330 Assert.AreEqual (Color.Blue, rtb.BackColor, "#C2");
333 Assert.IsTrue (rtb.ReadOnly, "#D1");
334 Assert.AreEqual (Color.Blue, rtb.BackColor, "#D2");
336 rtb.BackColor = Color.Red;
337 Assert.IsTrue (rtb.ReadOnly, "#E1");
338 Assert.AreEqual (Color.Red, rtb.BackColor, "#E2");
340 rtb.ReadOnly = false;
341 Assert.IsFalse (rtb.ReadOnly, "#F1");
342 Assert.AreEqual (Color.Red, rtb.BackColor, "#F2");
345 rtb.ResetBackColor ();
346 Assert.IsTrue (rtb.ReadOnly, "#G1");
348 Assert.AreEqual (SystemColors.Control, rtb.BackColor, "#G2");
350 Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#G2");
355 rtb = new RichTextBox ();
357 rtb.BackColor = Color.Blue;
358 Assert.IsTrue (rtb.ReadOnly, "#H1");
359 Assert.AreEqual (Color.Blue, rtb.BackColor, "#H2");
362 form.ShowInTaskbar = false;
363 form.Controls.Add (rtb);
366 Assert.IsTrue (rtb.ReadOnly, "#I1");
367 Assert.AreEqual (Color.Blue, rtb.BackColor, "#I2");
369 rtb.ReadOnly = false;
370 Assert.IsFalse (rtb.ReadOnly, "#J1");
371 Assert.AreEqual (Color.Blue, rtb.BackColor, "#J2");
373 rtb.ResetBackColor ();
374 Assert.IsFalse (rtb.ReadOnly, "#K1");
375 Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#K2");
381 public void ScrollBarsTest ()
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");
390 [ExpectedException (typeof (InvalidEnumArgumentException))]
391 public void ScrollBars_Invalid ()
393 RichTextBox rtb = new RichTextBox ();
394 rtb.ScrollBars = (RichTextBoxScrollBars) 666;
398 public void SelectionFontTest ()
400 RichTextBox t = new RichTextBox();
402 t.SelectionStart = 1;
403 t.SelectionLength = 1;
404 Font f = new Font(FontFamily.GenericMonospace, 120);
407 Assert.AreEqual (t.SelectionFont.Size, f.Size, "A1");
411 public void SelectionLength_Negative ()
413 RichTextBox rtb = new RichTextBox ();
415 rtb.SelectionLength = -1;
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");
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");
436 public void SelectionStart_Negative ()
438 RichTextBox rtb = new RichTextBox ();
440 rtb.SelectionStart = -1;
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");
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");
462 [Test] // This test passes if it doesn't throw an NRE.
463 public void Bug351886 ()
465 Form form = new Form ();
466 rtb = new RichTextBox ();
467 rtb.Dock = DockStyle.Fill;
469 rtb.SelectionFont = new Font (FontFamily.GenericSansSerif, 72f);
470 rtb.AppendText ("Left and make this very long so that it can be word wrapped ");
472 form.Load += new EventHandler (Bug351886_Load);
474 form.Controls.Add (rtb);
481 void Bug351886_Load (object sender, EventArgs e)
484 rtb.SelectionAlignment = HorizontalAlignment.Center;