Skip to content

Commit 022b1fe

Browse files
authored
Add code coverage for RichTextBox (#10994)
* Add code coverage for RichTextBox * 1. Update feedback 2. Add unit tests for Label to test its events: DragDrop, DragEnter, DragLeave, DragOver, GiveFeedback,QueryContinueDrag * Adopting Fluent Assertions for tests * Update RichTextBoxTests.cs resolved the feedbacks * Update RichTextBoxTests.cs * Update RichTextBoxTests.cs Handle FeedBacks
1 parent e59c8ab commit 022b1fe

File tree

1 file changed

+282
-0
lines changed

1 file changed

+282
-0
lines changed

src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/RichTextBoxTests.cs

+282
Original file line numberDiff line numberDiff line change
@@ -10667,6 +10667,278 @@ public void RichTextBox_GetCharIndexFromPosition_Invoke_ReturnsExpected(int x, i
1066710667
Assert.Equal(0, createdCallCount);
1066810668
}
1066910669

10670+
// DrawToBitmap doesn't work for this control, so we should hide it. We'll
10671+
// still call base so that this has a chance to work if it can.
10672+
[WinFormsFact]
10673+
public void RichTextBox_DrawToBitmap_Invoke_Success()
10674+
{
10675+
using Bitmap bitmap1 = new(10, 10);
10676+
using RichTextBox richTextBox1 = new();
10677+
richTextBox1.DrawToBitmap(bitmap1, new Rectangle(0, 0, 10, 10));
10678+
10679+
bitmap1.Width.Should().Be(10);
10680+
bitmap1.Height.Should().Be(10);
10681+
}
10682+
10683+
[WinFormsFact]
10684+
public void RichTextBox_SaveFilePath_Invoke_Success()
10685+
{
10686+
using RichTextBox richTextBox1 = new()
10687+
{
10688+
Rtf = @"{\rtf1\ansi{Sample for {\v HIDDEN }text}}"
10689+
};
10690+
using RichTextBox richTextBox2 = new();
10691+
10692+
string fileName = "SaveRichTextBox.rtf";
10693+
string projectDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..", "..", "..", "..", "..");
10694+
string filePath = $"{projectDirectory}/src/System.Windows.Forms/tests/UnitTests/TestResources/Files/{fileName}";
10695+
10696+
try
10697+
{
10698+
richTextBox1.SaveFile(filePath);
10699+
richTextBox2.LoadFile(filePath);
10700+
int startOfSample = richTextBox2.Text.IndexOf("Sample", StringComparison.Ordinal);
10701+
int endOfText = richTextBox2.Text.IndexOf("text", StringComparison.Ordinal) + "text".Length;
10702+
richTextBox2.Select(startOfSample, endOfText - startOfSample);
10703+
10704+
richTextBox2.Rtf.Should().NotBeNullOrEmpty();
10705+
richTextBox2.SelectedText.Should().Be("Sample for HIDDEN text");
10706+
}
10707+
finally
10708+
{
10709+
File.Delete(filePath);
10710+
}
10711+
}
10712+
10713+
[WinFormsTheory]
10714+
[InlineData(RichTextBoxStreamType.RichText)]
10715+
[InlineData(RichTextBoxStreamType.PlainText)]
10716+
[InlineData(RichTextBoxStreamType.UnicodePlainText)]
10717+
[InlineData(RichTextBoxStreamType.RichNoOleObjs)]
10718+
[InlineData(RichTextBoxStreamType.TextTextOleObjs)]
10719+
public void RichTextBox_SaveFile_Invoke_Success(RichTextBoxStreamType fileType)
10720+
{
10721+
using RichTextBox richTextBox1 = new();
10722+
10723+
string projectDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..", "..", "..", "..", "..");
10724+
string filePath = Path.Combine(projectDirectory, "src", "System.Windows.Forms", "tests", "UnitTests", "TestResources", "Files", "Test");
10725+
10726+
try
10727+
{
10728+
richTextBox1.SaveFile(filePath, fileType);
10729+
File.Exists(filePath).Should().BeTrue();
10730+
}
10731+
finally
10732+
{
10733+
File.Delete(filePath);
10734+
}
10735+
}
10736+
10737+
public static TheoryData<string> PlainTextData => new()
10738+
{
10739+
{ "Hello World"},
10740+
{ new string('a', 10000) },
10741+
{ "Special characters: !@#$%^&*()" },
10742+
};
10743+
10744+
[WinFormsTheory]
10745+
[MemberData(nameof(PlainTextData))]
10746+
public void RichTextBox_Paste_PlainText_Data(string value)
10747+
{
10748+
using RichTextBox richTextBox1 = new();
10749+
10750+
if (!string.IsNullOrEmpty(value))
10751+
{
10752+
Clipboard.SetText(value);
10753+
richTextBox1.Paste(DataFormats.GetFormat(DataFormats.Text));
10754+
10755+
richTextBox1.Text.Should().Be(value);
10756+
}
10757+
}
10758+
10759+
[WinFormsFact]
10760+
public void RichTextBox_Paste_EmptyString_Data()
10761+
{
10762+
using RichTextBox richTextBox1 = new();
10763+
10764+
Clipboard.SetText("non-empty");
10765+
Clipboard.Clear();
10766+
richTextBox1.Paste(DataFormats.GetFormat(DataFormats.Text));
10767+
10768+
richTextBox1.Text.Should().Be("");
10769+
}
10770+
10771+
public static TheoryData<string> RtfData => new()
10772+
{
10773+
{ "{\\rtf Hello World}" },
10774+
{ "{\\rtf1\\ansi{Sample for {\\v HIDDEN }text}}" },
10775+
{ "{\\rtf1\\ansi{Invalid RTF data" },
10776+
};
10777+
10778+
[WinFormsTheory]
10779+
[MemberData(nameof(RtfData))]
10780+
public void RichTextBox_Paste_Rtf_Data(string rtf)
10781+
{
10782+
using RichTextBox richTextBox1 = new();
10783+
10784+
if (!string.IsNullOrEmpty(rtf))
10785+
{
10786+
Clipboard.SetText(rtf);
10787+
richTextBox1.Paste(DataFormats.GetFormat(DataFormats.Rtf));
10788+
10789+
richTextBox1.Rtf.Should().StartWith("{\\rtf");
10790+
}
10791+
}
10792+
10793+
[WinFormsFact]
10794+
public void RichTextBox_DragDropEvent_AddRemove_Success()
10795+
{
10796+
using SubRichTextBox richTextBox1 = new();
10797+
int callCount = 0;
10798+
DragEventHandler handler = (sender, e) =>
10799+
{
10800+
sender.Should().Be(richTextBox1);
10801+
callCount++;
10802+
};
10803+
10804+
DragEventArgs dragEventArgs = new DragEventArgs(
10805+
data: null,
10806+
keyState: 0,
10807+
x: 0,
10808+
y: 0,
10809+
allowedEffect: DragDropEffects.None,
10810+
effect: DragDropEffects.None);
10811+
10812+
richTextBox1.DragDrop += handler;
10813+
richTextBox1.OnDragDrop(dragEventArgs);
10814+
callCount.Should().Be(1);
10815+
10816+
richTextBox1.DragDrop -= handler;
10817+
richTextBox1.OnDragDrop(dragEventArgs);
10818+
callCount.Should().Be(1);
10819+
}
10820+
10821+
[WinFormsFact]
10822+
public void RichTextBox_DragEnterEvent_AddRemove_Success()
10823+
{
10824+
using SubRichTextBox richTextBox1 = new();
10825+
int callCount = 0;
10826+
DragEventHandler handler = (sender, e) =>
10827+
{
10828+
sender.Should().Be(richTextBox1);
10829+
callCount++;
10830+
};
10831+
10832+
DragEventArgs dragEventArgs = new DragEventArgs(
10833+
data: null,
10834+
keyState: 0,
10835+
x: 0,
10836+
y: 0,
10837+
allowedEffect: DragDropEffects.None,
10838+
effect: DragDropEffects.None);
10839+
10840+
richTextBox1.DragEnter += handler;
10841+
richTextBox1.OnDragDrop(dragEventArgs);
10842+
callCount.Should().Be(0);
10843+
10844+
richTextBox1.DragEnter -= handler;
10845+
richTextBox1.OnDragDrop(dragEventArgs);
10846+
callCount.Should().Be(0);
10847+
}
10848+
10849+
[WinFormsFact]
10850+
public void RichTextBox_DragLeaveEvent_AddRemove_Success()
10851+
{
10852+
using SubRichTextBox richTextBox1 = new();
10853+
int callCount = 0;
10854+
EventHandler handler = (sender, e) =>
10855+
{
10856+
sender.Should().Be(richTextBox1);
10857+
e.Should().Be(EventArgs.Empty);
10858+
callCount++;
10859+
};
10860+
10861+
richTextBox1.DragLeave += handler;
10862+
richTextBox1.OnDragLeave(EventArgs.Empty);
10863+
callCount.Should().Be(1);
10864+
10865+
richTextBox1.DragLeave -= handler;
10866+
richTextBox1.OnDragLeave(EventArgs.Empty);
10867+
callCount.Should().Be(1);
10868+
}
10869+
10870+
[WinFormsFact]
10871+
public void RichTextBox_DragOverEvent_AddRemove_Success()
10872+
{
10873+
using SubRichTextBox richTextBox1 = new();
10874+
int callCount = 0;
10875+
DragEventHandler handler = (sender, e) =>
10876+
{
10877+
sender.Should().Be(richTextBox1);
10878+
callCount++;
10879+
};
10880+
10881+
DragEventArgs dragEventArgs = new DragEventArgs(
10882+
data: null,
10883+
keyState: 0,
10884+
x: 0,
10885+
y: 0,
10886+
allowedEffect: DragDropEffects.None,
10887+
effect: DragDropEffects.None);
10888+
10889+
richTextBox1.DragOver += handler;
10890+
richTextBox1.OnDragOver(dragEventArgs);
10891+
callCount.Should().Be(1);
10892+
10893+
richTextBox1.DragOver -= handler;
10894+
richTextBox1.OnDragOver(dragEventArgs);
10895+
callCount.Should().Be(1);
10896+
}
10897+
10898+
[WinFormsFact]
10899+
public void RichTextBox_GiveFeedbackEvent_AddRemove_Success()
10900+
{
10901+
using SubRichTextBox richTextBox1 = new();
10902+
int callCount = 0;
10903+
GiveFeedbackEventHandler handler = (sender, e) =>
10904+
{
10905+
sender.Should().Be(richTextBox1);
10906+
callCount++;
10907+
};
10908+
10909+
GiveFeedbackEventArgs giveFeedbackEventArgs = new(DragDropEffects.None, useDefaultCursors: true);
10910+
10911+
richTextBox1.GiveFeedback += handler;
10912+
richTextBox1.OnGiveFeedback(giveFeedbackEventArgs);
10913+
callCount.Should().Be(1);
10914+
10915+
richTextBox1.GiveFeedback -= handler;
10916+
richTextBox1.OnGiveFeedback(giveFeedbackEventArgs);
10917+
callCount.Should().Be(1);
10918+
}
10919+
10920+
[WinFormsFact]
10921+
public void RichTextBox_QueryContinueDragEvent_AddRemove_Success()
10922+
{
10923+
using SubRichTextBox richTextBox1 = new();
10924+
int callCount = 0;
10925+
QueryContinueDragEventHandler handler = (sender, e) =>
10926+
{
10927+
sender.Should().Be(richTextBox1);
10928+
callCount++;
10929+
};
10930+
10931+
QueryContinueDragEventArgs queryContinueDragEventArgs = new(keyState: 0, escapePressed: true, action: DragAction.Continue);
10932+
10933+
richTextBox1.QueryContinueDrag += handler;
10934+
richTextBox1.OnQueryContinueDrag(queryContinueDragEventArgs);
10935+
callCount.Should().Be(1);
10936+
10937+
richTextBox1.QueryContinueDrag -= handler;
10938+
richTextBox1.OnQueryContinueDrag(queryContinueDragEventArgs);
10939+
callCount.Should().Be(1);
10940+
}
10941+
1067010942
private class CustomGetParaFormatRichTextBox : RichTextBox
1067110943
{
1067210944
public bool MakeCustom { get; set; }
@@ -10798,6 +11070,16 @@ private class SubRichTextBox : RichTextBox
1079811070
public new void SetStyle(ControlStyles flag, bool value) => base.SetStyle(flag, value);
1079911071

1080011072
public new void WndProc(ref Message m) => base.WndProc(ref m);
11073+
11074+
public new void OnDragDrop(DragEventArgs e) => base.OnDragDrop(e);
11075+
11076+
public new void OnDragLeave(EventArgs e) => base.OnDragLeave(e);
11077+
11078+
public new void OnDragOver(DragEventArgs e) => base.OnDragOver(e);
11079+
11080+
public new void OnGiveFeedback(GiveFeedbackEventArgs e) => base.OnGiveFeedback(e);
11081+
11082+
public new void OnQueryContinueDrag(QueryContinueDragEventArgs e) => base.OnQueryContinueDrag(e);
1080111083
}
1080211084

1080311085
private static unsafe string GetClassName(HWND hWnd)

0 commit comments

Comments
 (0)