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 1 commit
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
56 changes: 55 additions & 1 deletion src/System.Windows.Forms/tests/UnitTests/TrackBarTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Windows.Forms.TestUtilities;
using Point = System.Drawing.Point;
using Size = System.Drawing.Size;

Syareel-Sukeri marked this conversation as resolved.
Show resolved Hide resolved
namespace System.Windows.Forms.Tests;

public class TrackBarTests
Expand Down Expand Up @@ -3211,6 +3210,61 @@ 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);
}

public static IEnumerable<object[]> PaintEvent_TestData()
{
yield return new object[] { new PaintEventArgs(Graphics.FromImage(new Bitmap(100, 100)), new Rectangle(0, 0, 100, 100)) };
yield return new object[] { new PaintEventArgs(Graphics.FromImage(new Bitmap(200, 200)), new Rectangle(10, 10, 50, 50)) };
yield return new object[] { new PaintEventArgs(Graphics.FromImage(new Bitmap(300, 300)), new Rectangle(20, 20, 100, 100)) };
yield return new object[] { new PaintEventArgs(Graphics.FromImage(new Bitmap(400, 400)), new Rectangle(30, 30, 150, 150)) };
yield return new object[] { new PaintEventArgs(Graphics.FromImage(new Bitmap(500, 500)), new Rectangle(40, 40, 200, 200)) };
}

Syareel-Sukeri marked this conversation as resolved.
Show resolved Hide resolved
[WinFormsTheory]
[MemberData(nameof(PaintEvent_TestData))]
public void TrackBar_PaintEvent_AddRemove_Success(PaintEventArgs paintEventArgs)
{
using SubTrackBar trackBar = new();
int callCount = 0;
PaintEventHandler handler = (sender, e) =>
{
sender.Should().BeSameAs(trackBar);
e.Should().NotBeNull();
Syareel-Sukeri marked this conversation as resolved.
Show resolved Hide resolved
callCount++;
};

// Add and remove the event handler and test the Paint event.
trackBar.Paint += handler;
trackBar.OnPaint(paintEventArgs);
callCount.Should().BeGreaterThan(0);
Syareel-Sukeri marked this conversation as resolved.
Show resolved Hide resolved

callCount = 0;
trackBar.Paint -= handler;
trackBar.OnPaint(paintEventArgs);
trackBar.Invalidate();
callCount.Should().Be(0);
}

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