-
Notifications
You must be signed in to change notification settings - Fork 1k
Add code coverage for DataGridViewImageCell #13616
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
Open
Zheng-Li01
wants to merge
7
commits into
dotnet:main
Choose a base branch
from
Zheng-Li01:Add_code_coverage_for_DataGridViewImageCell
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+267
−0
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1a6746a
Add code coverage for DataGridViewImageCell
Zheng-Li01 4755ca3
Address FeedBacks
Zheng-Li01 d7537b2
Address FeedBacks
Zheng-Li01 a7d49a8
Address FeedBacks
Zheng-Li01 d792970
Update DataGridViewImageCellTests.cs
Zheng-Li01 5e9ec3c
Update DataGridViewImageCellTests.cs
Zheng-Li01 8b518fd
Update DataGridViewImageCellTests.cs
Zheng-Li01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
267 changes: 267 additions & 0 deletions
267
src/test/unit/System.Windows.Forms/System/Windows/Forms/DataGridViewImageCellTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,267 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Drawing; | ||
|
||
namespace System.Windows.Forms.Tests; | ||
|
||
public class DataGridViewImageCellTests : IDisposable | ||
{ | ||
private readonly DataGridViewImageCell _dataGridViewImageCell; | ||
|
||
public DataGridViewImageCellTests() => _dataGridViewImageCell = new(); | ||
|
||
public void Dispose() => _dataGridViewImageCell.Dispose(); | ||
|
||
[Fact] | ||
public void Ctor_Default_SetsValueIsIconFalse() => | ||
_dataGridViewImageCell.ValueIsIcon.Should().BeFalse(); | ||
|
||
[WinFormsTheory] | ||
[BoolData] | ||
public void Ctor_ValueIsIcon_SetsValueIsIcon(bool valueIsIcon) | ||
{ | ||
using DataGridViewImageCell dataGridViewImageCell = new(valueIsIcon); | ||
|
||
dataGridViewImageCell.ValueIsIcon.Should().Be(valueIsIcon); | ||
} | ||
|
||
[Fact] | ||
public void DefaultNewRowValue_ReturnsErrorBitmap_WhenValueTypeIsImage() | ||
{ | ||
_dataGridViewImageCell.ValueType = typeof(Image); | ||
var value = _dataGridViewImageCell.DefaultNewRowValue; | ||
|
||
value.Should().BeSameAs(DataGridViewImageCell.ErrorBitmap); | ||
} | ||
|
||
[WinFormsFact] | ||
public void DefaultNewRowValue_ReturnsNull_WhenValueTypeIsOther() | ||
{ | ||
_dataGridViewImageCell.ValueType = typeof(string); | ||
_dataGridViewImageCell.ValueIsIcon = false; | ||
object? value = _dataGridViewImageCell.DefaultNewRowValue; | ||
|
||
value.Should().BeNull(); | ||
} | ||
|
||
[Fact] | ||
public void EditType_IsAlwaysNull() | ||
{ | ||
_dataGridViewImageCell.EditType.Should().BeNull(); | ||
|
||
using DataGridViewImageCell dataGridViewImageCellIcon = new(true); | ||
dataGridViewImageCellIcon.EditType.Should().BeNull(); | ||
} | ||
|
||
[WinFormsFact] | ||
public void FormattedValueType_ReturnsImage_WhenValueIsIconFalse() | ||
{ | ||
_dataGridViewImageCell.ValueIsIcon = false; | ||
_dataGridViewImageCell.FormattedValueType.Should().Be(typeof(Image)); | ||
} | ||
|
||
[WinFormsFact] | ||
public void ValueIsIcon_SetFalse_UpdatesFlag() | ||
{ | ||
_dataGridViewImageCell.ValueIsIcon = true; | ||
_dataGridViewImageCell.ValueIsIcon = false; | ||
_dataGridViewImageCell.ValueIsIcon.Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void ValueType_GetSet_RoundTrips() | ||
Zheng-Li01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
_dataGridViewImageCell.ValueType = typeof(Image); | ||
_dataGridViewImageCell.ValueType.Should().Be(typeof(Image)); | ||
|
||
_dataGridViewImageCell.ValueType = null; | ||
_dataGridViewImageCell.ValueType.Should().Be(typeof(Image)); | ||
|
||
_dataGridViewImageCell.ValueIsIcon = true; | ||
_dataGridViewImageCell.ValueType.Should().Be(typeof(Icon)); | ||
} | ||
|
||
[Fact] | ||
public void ValueType_SetToNull_ResetsValueIsIconToDefault() | ||
{ | ||
Zheng-Li01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
_dataGridViewImageCell.ValueType = typeof(Icon); | ||
_dataGridViewImageCell.ValueIsIcon.Should().BeTrue(); | ||
|
||
_dataGridViewImageCell.ValueType = null; | ||
_dataGridViewImageCell.ValueIsIcon.Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void GetContentBounds_ReturnsEmpty_WhenDataGridViewIsNull() | ||
{ | ||
using Graphics g = Graphics.FromImage(new Bitmap(10, 10)); | ||
DataGridViewCellStyle dataGridViewCellStyle = new(); | ||
Zheng-Li01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
_dataGridViewImageCell.DataGridView = null; | ||
Rectangle bounds = (Rectangle)_dataGridViewImageCell.TestAccessor().Dynamic.GetContentBounds(g, dataGridViewCellStyle, 0); | ||
|
||
bounds.Should().Be(Rectangle.Empty); | ||
} | ||
|
||
[WinFormsFact] | ||
public void GetContentBounds_ReturnsEmpty_WhenRowIndexNegative() | ||
{ | ||
using DataGridView dataGridView = new(); | ||
_dataGridViewImageCell.DataGridView = dataGridView; | ||
using Graphics g = Graphics.FromImage(new Bitmap(10, 10)); | ||
DataGridViewCellStyle dataGridViewCellStyle = new(); | ||
Rectangle bounds = (Rectangle)_dataGridViewImageCell.TestAccessor().Dynamic.GetContentBounds(g, dataGridViewCellStyle, -1); | ||
|
||
bounds.Should().Be(Rectangle.Empty); | ||
} | ||
|
||
[WinFormsFact] | ||
public void GetContentBounds_ReturnsEmpty_WhenOwningColumnIsNull() | ||
{ | ||
using DataGridView dataGridView = new(); | ||
dataGridView.Columns.Add(new DataGridViewImageColumn()); | ||
dataGridView.Rows.Add(); | ||
_dataGridViewImageCell.DataGridView = dataGridView; | ||
_dataGridViewImageCell.OwningColumn = null; | ||
using Graphics g = Graphics.FromImage(new Bitmap(10, 10)); | ||
DataGridViewCellStyle dataGridViewCellStyle = new(); | ||
Rectangle bounds = (Rectangle)_dataGridViewImageCell.TestAccessor().Dynamic.GetContentBounds(g, dataGridViewCellStyle, 0); | ||
|
||
bounds.Should().Be(Rectangle.Empty); | ||
} | ||
Zheng-Li01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
[Fact] | ||
public void GetErrorIconBounds_ReturnsEmpty_WhenDataGridViewIsNull() | ||
{ | ||
using Graphics g = Graphics.FromImage(new Bitmap(10, 10)); | ||
DataGridViewCellStyle dataGridViewCellStyle = new(); | ||
Rectangle bounds = (Rectangle)_dataGridViewImageCell.TestAccessor().Dynamic.GetErrorIconBounds(g, dataGridViewCellStyle, 0); | ||
|
||
bounds.Should().Be(Rectangle.Empty); | ||
} | ||
|
||
[WinFormsFact] | ||
public void GetErrorIconBounds_ReturnsEmpty_WhenRowIndexNegative() | ||
{ | ||
using DataGridView dataGridView = new(); | ||
dataGridView.Columns.Add(new DataGridViewImageColumn()); | ||
dataGridView.Rows.Add(); | ||
_dataGridViewImageCell.DataGridView = dataGridView; | ||
using Graphics g = Graphics.FromImage(new Bitmap(10, 10)); | ||
DataGridViewCellStyle dataGridViewCellStyle = new(); | ||
Rectangle bounds = (Rectangle)_dataGridViewImageCell.TestAccessor().Dynamic.GetErrorIconBounds(g, dataGridViewCellStyle, -1); | ||
|
||
bounds.Should().Be(Rectangle.Empty); | ||
} | ||
|
||
[WinFormsFact] | ||
public void GetErrorIconBounds_ReturnsEmpty_WhenOwningColumnIsNull() | ||
{ | ||
using DataGridView dataGridView = new(); | ||
dataGridView.Columns.Add(new DataGridViewImageColumn()); | ||
dataGridView.Rows.Add(); | ||
_dataGridViewImageCell.DataGridView = dataGridView; | ||
_dataGridViewImageCell.OwningColumn = null; | ||
using Graphics g = Graphics.FromImage(new Bitmap(10, 10)); | ||
DataGridViewCellStyle dataGridViewCellStyle = new(); | ||
Rectangle bounds = (Rectangle)_dataGridViewImageCell.TestAccessor().Dynamic.GetErrorIconBounds(g, dataGridViewCellStyle, 0); | ||
|
||
bounds.Should().Be(Rectangle.Empty); | ||
} | ||
|
||
[WinFormsFact] | ||
public void GetErrorIconBounds_ReturnsEmpty_WhenShowCellErrorsIsFalse() | ||
{ | ||
using DataGridView dataGridView = new(); | ||
using DataGridViewImageColumn dataGridViewImageColumn = new(); | ||
dataGridView.Columns.Add(dataGridViewImageColumn); | ||
dataGridView.Rows.Add(); | ||
dataGridView.ShowCellErrors = false; | ||
_dataGridViewImageCell.DataGridView = dataGridView; | ||
using Graphics g = Graphics.FromImage(new Bitmap(10, 10)); | ||
DataGridViewCellStyle dataGridViewCellStyle = new(); | ||
Rectangle bounds = (Rectangle)_dataGridViewImageCell.TestAccessor().Dynamic.GetErrorIconBounds(g, dataGridViewCellStyle, 0); | ||
|
||
bounds.Should().Be(Rectangle.Empty); | ||
} | ||
|
||
[WinFormsFact] | ||
public void GetErrorIconBounds_ReturnsEmpty_WhenErrorTextIsNullOrEmpty() | ||
{ | ||
using DataGridView dataGridView = new(); | ||
using DataGridViewImageColumn dataGridViewImageColumn = new(); | ||
dataGridView.Columns.Add(dataGridViewImageColumn); | ||
dataGridView.Rows.Add(); | ||
dataGridView.ShowCellErrors = true; | ||
_dataGridViewImageCell.DataGridView = dataGridView; | ||
_dataGridViewImageCell.ErrorText = null; | ||
_dataGridViewImageCell.OwningColumn = null; | ||
using Graphics g = Graphics.FromImage(new Bitmap(10, 10)); | ||
DataGridViewCellStyle dataGridViewCellStyle = new(); | ||
Rectangle bounds = (Rectangle)_dataGridViewImageCell.TestAccessor().Dynamic.GetErrorIconBounds(g, dataGridViewCellStyle, 0); | ||
Zheng-Li01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
bounds.Should().Be(Rectangle.Empty); | ||
} | ||
|
||
[Fact] | ||
public void GetPreferredSize_ReturnsMinusOne_WhenDataGridViewIsNull() | ||
{ | ||
using DataGridViewImageCell dataGridViewImageCell = _dataGridViewImageCell; | ||
using Graphics g = Graphics.FromImage(new Bitmap(10, 10)); | ||
DataGridViewCellStyle dataGridViewCellStyle = new(); | ||
Size size = (Size)dataGridViewImageCell.TestAccessor().Dynamic.GetPreferredSize(g, dataGridViewCellStyle, 0, new Size(100, 100)); | ||
|
||
size.Should().Be(new Size(-1, -1)); | ||
} | ||
|
||
[Fact] | ||
public void GetPreferredSize_ThrowsNullReferenceException_WhenCellStyleIsNull() | ||
{ | ||
using DataGridViewImageCell dataGridViewImageCell = _dataGridViewImageCell; | ||
using Graphics g = Graphics.FromImage(new Bitmap(10, 10)); | ||
Action action = () => dataGridViewImageCell.TestAccessor().Dynamic.GetPreferredSize(g, null, 0, new Size(100, 100)); | ||
|
||
action.Should().Throw<NullReferenceException>(); | ||
} | ||
|
||
[WinFormsFact] | ||
public void Paint_DoesNotThrow_WithValidArguments_UsingTestAccessor() | ||
{ | ||
using DataGridView dataGridView = new(); | ||
using DataGridViewImageColumn dataGridViewImageColumn = new(); | ||
dataGridView.Columns.Add(dataGridViewImageColumn); | ||
dataGridView.Rows.Add(); | ||
using DataGridViewImageCell dataGridViewImageCell = _dataGridViewImageCell; | ||
dataGridView.Rows[0].Cells[0] = dataGridViewImageCell; | ||
using Graphics g = Graphics.FromImage(new Bitmap(10, 10)); | ||
DataGridViewCellStyle dataGridViewCellStyle = new(); | ||
DataGridViewAdvancedBorderStyle borderStyle = new(); | ||
|
||
Action action = () => dataGridViewImageCell.TestAccessor().Dynamic.Paint( | ||
g, | ||
new Rectangle(0, 0, 10, 10), | ||
new Rectangle(0, 0, 10, 10), | ||
0, | ||
DataGridViewElementStates.None, | ||
null, | ||
null, | ||
null, | ||
dataGridViewCellStyle, | ||
borderStyle, | ||
DataGridViewPaintParts.All); | ||
|
||
action.Should().NotThrow(); | ||
} | ||
|
||
[WinFormsFact] | ||
public void ColumnIndex_ReturnsExpectedFormat() | ||
{ | ||
using DataGridView dataGridView = new(); | ||
dataGridView.Columns.Add(new DataGridViewImageColumn()); | ||
dataGridView.Rows.Add(); | ||
using DataGridViewImageCell dataGridViewImageCell = _dataGridViewImageCell; | ||
dataGridView.Rows[0].Cells[0] = dataGridViewImageCell; | ||
|
||
dataGridViewImageCell.ColumnIndex.Should().Be(0); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.