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 case for DataGridView 7 events #11618

Merged

Conversation

John-Qiao
Copy link
Member

@John-Qiao John-Qiao commented Jul 2, 2024

related #10453

Proposed changes

  • Add unit tests for below 7 events
  1. BackColorChanged
  2. BackgroundImageChanged
  3. BackgroundImageLayoutChanged
  4. ForeColorChanged
  5. FontChanged
  6. PaddingChanged
  7. TextChanged
Microsoft Reviewers: Open in CodeFlow

@John-Qiao John-Qiao requested a review from a team as a code owner July 2, 2024 01:54
@John-Qiao John-Qiao added the waiting-review This item is waiting on review by one or more members of team label Jul 2, 2024
Copy link

codecov bot commented Jul 2, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 74.56167%. Comparing base (2a6733d) to head (74e8a72).
Report is 13 commits behind head on main.

Additional details and impacted files
@@                 Coverage Diff                 @@
##                main      #11618         +/-   ##
===================================================
+ Coverage   74.55540%   74.56167%   +0.00627%     
===================================================
  Files           3040        3040                 
  Lines         629493      629605        +112     
  Branches       46835       46839          +4     
===================================================
+ Hits          469321      469444        +123     
+ Misses        156818      156800         -18     
- Partials        3354        3361          +7     
Flag Coverage Δ
Debug 74.56167% <100.00000%> (+0.00627%) ⬆️
integration 17.98157% <ø> (+0.01154%) ⬆️
production 47.50930% <ø> (+0.00790%) ⬆️
test 96.95225% <100.00000%> (-0.00222%) ⬇️
unit 44.54530% <ø> (+0.02649%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Copy link
Member

@LeafShi1 LeafShi1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Copy link
Member

@Epica3055 Epica3055 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe you can simplify your code by something like this:

    [WinFormsFact]
    public void DataGridView_PaddingChangedEvent_Raised_Success()
    {

        TestEvent(nameof(DataGridView.PaddingChanged), nameof(DataGridView.Padding), new Padding(10), new Padding(20));
    }

    [WinFormsFact]
    public void DataGridView_TextChangedEvent_Raised_Success()
    {

        TestEvent(nameof(DataGridView.TextChanged), nameof(DataGridView.Text), "New Text", "Another Text");

    }

    private void TestEvent(string eventName, string propertyName, object propertyValue, object newPropertyValue)
    {
        Type type = typeof(DataGridView);

        int callCount = 0;
        EventHandler handler = (sender, e) =>
        {
            sender.Should().Be(_dataGridView);
            e.Should().Be(EventArgs.Empty);
            callCount++;
        };

        Reflection.EventInfo eventInfo = type.GetEvent(eventName);
        eventInfo.AddEventHandler(_dataGridView, handler);

        Reflection.PropertyInfo propertyInfo = type.GetProperty(propertyName);

        propertyInfo.SetValue(_dataGridView, propertyValue, null);

        callCount.Should().Be(1);

        eventInfo.RemoveEventHandler(_dataGridView, handler);
        propertyInfo.SetValue(_dataGridView, newPropertyValue, null);
        callCount.Should().Be(1);
    }

@LeafShi1
Copy link
Member

LeafShi1 commented Jul 8, 2024

Using InlineData can make the code more concise, like this

Copy link
Member

@lonitra lonitra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@lonitra lonitra added ready-to-merge PRs that are ready to merge but worth notifying the internal team. and removed waiting-review This item is waiting on review by one or more members of team labels Jul 9, 2024
@lonitra lonitra merged commit 1d32d33 into dotnet:main Jul 10, 2024
8 checks passed
@dotnet-policy-service dotnet-policy-service bot added this to the 9.0 Preview7 milestone Jul 10, 2024
@dotnet-policy-service dotnet-policy-service bot removed the ready-to-merge PRs that are ready to merge but worth notifying the internal team. label Jul 10, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Aug 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants