Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test for TrackBar #11066

Merged
merged 6 commits into from
Mar 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/System.Windows.Forms/tests/UnitTests/TrackBarTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3211,6 +3211,68 @@ public void TrackBar_WndProc_InvokeScrollWithHandle_Success(int msg, IntPtr wPar
Assert.Equal(0, createdCallCount);
}

[WinFormsFact]
public void TrackBar_AutoSizeChangedEvent_AddRemove_Success()
{
using TrackBar control = new();
int callCount = 0;
EventHandler handler = (sender, e) =>
{
sender.Should().BeSameAs(control);
e.Should().BeSameAs(EventArgs.Empty);
callCount++;
};

control.AutoSizeChanged += handler;
control.AutoSize = !control.AutoSize;
callCount.Should().Be(1);

control.AutoSizeChanged -= handler;
control.AutoSize = !control.AutoSize;
callCount.Should().Be(1);
}

[WinFormsTheory]
[InlineData(100, 200, 0, 0, 50, 100)]
[InlineData(200, 300, 10, 20, 70, 140)]
[InlineData(300, 400, -10, -20, 70, 140)]
[InlineData(400, 500, 0, 0, 1, 1)]
[InlineData(500, 600, 0, 0, 500, 600)]
public void TrackBar_PaintEvent_AddRemove_Success(int bitmapWidth, int bitmapHeight, int rectX, int rectY, int rectWidth, int rectHeight)
{
using Bitmap bitmap = new(bitmapWidth, bitmapHeight);
using Graphics graphics = Graphics.FromImage(bitmap);
Rectangle rectangle = new(rectX, rectY, rectWidth, rectHeight);

using SubTrackBar trackBar = new();
int callCount = 0;
PaintEventHandler handler = (sender, e) =>
{
sender.Should().BeSameAs(trackBar);
e.Graphics.Should().BeSameAs(graphics);
e.ClipRectangle.Should().Be(rectangle);
callCount++;
};

trackBar.Paint += handler;
using (var eventArgs = new PaintEventArgs(graphics, rectangle))
{
trackBar.OnPaint(eventArgs);
}

callCount.Should().Be(1);

callCount = 0;
trackBar.Paint -= handler;
using (var eventArgs = new PaintEventArgs(graphics, rectangle))
{
trackBar.OnPaint(eventArgs);
}

trackBar.Invalidate();
callCount.Should().Be(0);
}

public class SubTrackBar : TrackBar
{
public new bool CanEnableIme => base.CanEnableIme;
Expand Down