|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Drawing; |
| 5 | + |
| 6 | +namespace System.Windows.Forms.Tests; |
| 7 | + |
| 8 | +public class DataGridViewImageCellTests : IDisposable |
| 9 | +{ |
| 10 | + private readonly DataGridViewImageCell _dataGridViewImageCell; |
| 11 | + |
| 12 | + public DataGridViewImageCellTests() => _dataGridViewImageCell = new(); |
| 13 | + |
| 14 | + public void Dispose() => _dataGridViewImageCell.Dispose(); |
| 15 | + |
| 16 | + [Fact] |
| 17 | + public void Ctor_Default_SetsValueIsIconFalse() => |
| 18 | + _dataGridViewImageCell.ValueIsIcon.Should().BeFalse(); |
| 19 | + |
| 20 | + [WinFormsTheory] |
| 21 | + [BoolData] |
| 22 | + public void Ctor_ValueIsIcon_SetsValueIsIcon(bool valueIsIcon) |
| 23 | + { |
| 24 | + using DataGridViewImageCell dataGridViewImageCell = new(valueIsIcon); |
| 25 | + |
| 26 | + dataGridViewImageCell.ValueIsIcon.Should().Be(valueIsIcon); |
| 27 | + } |
| 28 | + |
| 29 | + [Fact] |
| 30 | + public void DefaultNewRowValue_ReturnsErrorBitmap_WhenValueTypeIsImage() |
| 31 | + { |
| 32 | + _dataGridViewImageCell.ValueType = typeof(Image); |
| 33 | + var value = _dataGridViewImageCell.DefaultNewRowValue; |
| 34 | + |
| 35 | + value.Should().BeSameAs(DataGridViewImageCell.ErrorBitmap); |
| 36 | + } |
| 37 | + |
| 38 | + [WinFormsFact] |
| 39 | + public void DefaultNewRowValue_ReturnsNull_WhenValueTypeIsOther() |
| 40 | + { |
| 41 | + _dataGridViewImageCell.ValueType = typeof(string); |
| 42 | + _dataGridViewImageCell.ValueIsIcon = false; |
| 43 | + object? value = _dataGridViewImageCell.DefaultNewRowValue; |
| 44 | + |
| 45 | + value.Should().BeNull(); |
| 46 | + } |
| 47 | + |
| 48 | + [Fact] |
| 49 | + public void EditType_IsAlwaysNull() |
| 50 | + { |
| 51 | + _dataGridViewImageCell.EditType.Should().BeNull(); |
| 52 | + |
| 53 | + using DataGridViewImageCell dataGridViewImageCellIcon = new(true); |
| 54 | + dataGridViewImageCellIcon.EditType.Should().BeNull(); |
| 55 | + } |
| 56 | + |
| 57 | + [WinFormsFact] |
| 58 | + public void FormattedValueType_ReturnsImage_WhenValueIsIconFalse() |
| 59 | + { |
| 60 | + _dataGridViewImageCell.ValueIsIcon = false; |
| 61 | + _dataGridViewImageCell.FormattedValueType.Should().Be(typeof(Image)); |
| 62 | + } |
| 63 | + |
| 64 | + [WinFormsFact] |
| 65 | + public void ValueIsIcon_SetFalse_UpdatesFlag() |
| 66 | + { |
| 67 | + _dataGridViewImageCell.ValueIsIcon = true; |
| 68 | + _dataGridViewImageCell.ValueIsIcon = false; |
| 69 | + _dataGridViewImageCell.ValueIsIcon.Should().BeFalse(); |
| 70 | + } |
| 71 | + |
| 72 | + [Fact] |
| 73 | + public void ValueType_GetSet_RoundTrips() |
| 74 | + { |
| 75 | + _dataGridViewImageCell.ValueType = typeof(Image); |
| 76 | + _dataGridViewImageCell.ValueType.Should().Be(typeof(Image)); |
| 77 | + |
| 78 | + _dataGridViewImageCell.ValueType = null; |
| 79 | + _dataGridViewImageCell.ValueType.Should().Be(typeof(Image)); |
| 80 | + |
| 81 | + _dataGridViewImageCell.ValueIsIcon = true; |
| 82 | + _dataGridViewImageCell.ValueType.Should().Be(typeof(Icon)); |
| 83 | + } |
| 84 | + |
| 85 | + [Fact] |
| 86 | + public void ValueType_SetToNull_ResetsValueIsIconToDefault() |
| 87 | + { |
| 88 | + _dataGridViewImageCell.ValueType = typeof(Icon); |
| 89 | + _dataGridViewImageCell.ValueIsIcon.Should().BeTrue(); |
| 90 | + |
| 91 | + _dataGridViewImageCell.ValueType = null; |
| 92 | + _dataGridViewImageCell.ValueIsIcon.Should().BeFalse(); |
| 93 | + } |
| 94 | + |
| 95 | + [Fact] |
| 96 | + public void GetContentBounds_ReturnsEmpty_WhenDataGridViewIsNull() |
| 97 | + { |
| 98 | + using Graphics g = Graphics.FromImage(new Bitmap(10, 10)); |
| 99 | + DataGridViewCellStyle dataGridViewCellStyle = new(); |
| 100 | + _dataGridViewImageCell.DataGridView = null; |
| 101 | + Rectangle bounds = (Rectangle)_dataGridViewImageCell.TestAccessor().Dynamic.GetContentBounds(g, dataGridViewCellStyle, 0); |
| 102 | + |
| 103 | + bounds.Should().Be(Rectangle.Empty); |
| 104 | + } |
| 105 | + |
| 106 | + [WinFormsFact] |
| 107 | + public void GetContentBounds_ReturnsEmpty_WhenRowIndexNegative() |
| 108 | + { |
| 109 | + using DataGridView dataGridView = new(); |
| 110 | + _dataGridViewImageCell.DataGridView = dataGridView; |
| 111 | + using Graphics g = Graphics.FromImage(new Bitmap(10, 10)); |
| 112 | + DataGridViewCellStyle dataGridViewCellStyle = new(); |
| 113 | + Rectangle bounds = (Rectangle)_dataGridViewImageCell.TestAccessor().Dynamic.GetContentBounds(g, dataGridViewCellStyle, -1); |
| 114 | + |
| 115 | + bounds.Should().Be(Rectangle.Empty); |
| 116 | + } |
| 117 | + |
| 118 | + [WinFormsFact] |
| 119 | + public void GetContentBounds_ReturnsEmpty_WhenOwningColumnIsNull() |
| 120 | + { |
| 121 | + using DataGridView dataGridView = new(); |
| 122 | + dataGridView.Columns.Add(new DataGridViewImageColumn()); |
| 123 | + dataGridView.Rows.Add(); |
| 124 | + _dataGridViewImageCell.DataGridView = dataGridView; |
| 125 | + _dataGridViewImageCell.OwningColumn = null; |
| 126 | + using Graphics g = Graphics.FromImage(new Bitmap(10, 10)); |
| 127 | + DataGridViewCellStyle dataGridViewCellStyle = new(); |
| 128 | + Rectangle bounds = (Rectangle)_dataGridViewImageCell.TestAccessor().Dynamic.GetContentBounds(g, dataGridViewCellStyle, 0); |
| 129 | + |
| 130 | + bounds.Should().Be(Rectangle.Empty); |
| 131 | + } |
| 132 | + |
| 133 | + [Fact] |
| 134 | + public void GetErrorIconBounds_ReturnsEmpty_WhenDataGridViewIsNull() |
| 135 | + { |
| 136 | + using Graphics g = Graphics.FromImage(new Bitmap(10, 10)); |
| 137 | + DataGridViewCellStyle dataGridViewCellStyle = new(); |
| 138 | + Rectangle bounds = (Rectangle)_dataGridViewImageCell.TestAccessor().Dynamic.GetErrorIconBounds(g, dataGridViewCellStyle, 0); |
| 139 | + |
| 140 | + bounds.Should().Be(Rectangle.Empty); |
| 141 | + } |
| 142 | + |
| 143 | + [WinFormsFact] |
| 144 | + public void GetErrorIconBounds_ReturnsEmpty_WhenRowIndexNegative() |
| 145 | + { |
| 146 | + using DataGridView dataGridView = new(); |
| 147 | + dataGridView.Columns.Add(new DataGridViewImageColumn()); |
| 148 | + dataGridView.Rows.Add(); |
| 149 | + _dataGridViewImageCell.DataGridView = dataGridView; |
| 150 | + using Graphics g = Graphics.FromImage(new Bitmap(10, 10)); |
| 151 | + DataGridViewCellStyle dataGridViewCellStyle = new(); |
| 152 | + Rectangle bounds = (Rectangle)_dataGridViewImageCell.TestAccessor().Dynamic.GetErrorIconBounds(g, dataGridViewCellStyle, -1); |
| 153 | + |
| 154 | + bounds.Should().Be(Rectangle.Empty); |
| 155 | + } |
| 156 | + |
| 157 | + [WinFormsFact] |
| 158 | + public void GetErrorIconBounds_ReturnsEmpty_WhenOwningColumnIsNull() |
| 159 | + { |
| 160 | + using DataGridView dataGridView = new(); |
| 161 | + dataGridView.Columns.Add(new DataGridViewImageColumn()); |
| 162 | + dataGridView.Rows.Add(); |
| 163 | + _dataGridViewImageCell.DataGridView = dataGridView; |
| 164 | + _dataGridViewImageCell.OwningColumn = null; |
| 165 | + using Graphics g = Graphics.FromImage(new Bitmap(10, 10)); |
| 166 | + DataGridViewCellStyle dataGridViewCellStyle = new(); |
| 167 | + Rectangle bounds = (Rectangle)_dataGridViewImageCell.TestAccessor().Dynamic.GetErrorIconBounds(g, dataGridViewCellStyle, 0); |
| 168 | + |
| 169 | + bounds.Should().Be(Rectangle.Empty); |
| 170 | + } |
| 171 | + |
| 172 | + [WinFormsFact] |
| 173 | + public void GetErrorIconBounds_ReturnsEmpty_WhenShowCellErrorsIsFalse() |
| 174 | + { |
| 175 | + using DataGridView dataGridView = new(); |
| 176 | + using DataGridViewImageColumn dataGridViewImageColumn = new(); |
| 177 | + dataGridView.Columns.Add(dataGridViewImageColumn); |
| 178 | + dataGridView.Rows.Add(); |
| 179 | + dataGridView.ShowCellErrors = false; |
| 180 | + _dataGridViewImageCell.DataGridView = dataGridView; |
| 181 | + using Graphics g = Graphics.FromImage(new Bitmap(10, 10)); |
| 182 | + DataGridViewCellStyle dataGridViewCellStyle = new(); |
| 183 | + Rectangle bounds = (Rectangle)_dataGridViewImageCell.TestAccessor().Dynamic.GetErrorIconBounds(g, dataGridViewCellStyle, 0); |
| 184 | + |
| 185 | + bounds.Should().Be(Rectangle.Empty); |
| 186 | + } |
| 187 | + |
| 188 | + [WinFormsFact] |
| 189 | + public void GetErrorIconBounds_ReturnsEmpty_WhenErrorTextIsNullOrEmpty() |
| 190 | + { |
| 191 | + using DataGridView dataGridView = new(); |
| 192 | + using DataGridViewImageColumn dataGridViewImageColumn = new(); |
| 193 | + dataGridView.Columns.Add(dataGridViewImageColumn); |
| 194 | + dataGridView.Rows.Add(); |
| 195 | + dataGridView.ShowCellErrors = true; |
| 196 | + _dataGridViewImageCell.DataGridView = dataGridView; |
| 197 | + _dataGridViewImageCell.ErrorText = null; |
| 198 | + _dataGridViewImageCell.OwningColumn = null; |
| 199 | + using Graphics g = Graphics.FromImage(new Bitmap(10, 10)); |
| 200 | + DataGridViewCellStyle dataGridViewCellStyle = new(); |
| 201 | + Rectangle bounds = (Rectangle)_dataGridViewImageCell.TestAccessor().Dynamic.GetErrorIconBounds(g, dataGridViewCellStyle, 0); |
| 202 | + |
| 203 | + bounds.Should().Be(Rectangle.Empty); |
| 204 | + } |
| 205 | + |
| 206 | + [Fact] |
| 207 | + public void GetPreferredSize_ReturnsMinusOne_WhenDataGridViewIsNull() |
| 208 | + { |
| 209 | + using DataGridViewImageCell dataGridViewImageCell = _dataGridViewImageCell; |
| 210 | + using Graphics g = Graphics.FromImage(new Bitmap(10, 10)); |
| 211 | + DataGridViewCellStyle dataGridViewCellStyle = new(); |
| 212 | + Size size = (Size)dataGridViewImageCell.TestAccessor().Dynamic.GetPreferredSize(g, dataGridViewCellStyle, 0, new Size(100, 100)); |
| 213 | + |
| 214 | + size.Should().Be(new Size(-1, -1)); |
| 215 | + } |
| 216 | + |
| 217 | + [Fact] |
| 218 | + public void GetPreferredSize_ThrowsNullReferenceException_WhenCellStyleIsNull() |
| 219 | + { |
| 220 | + using DataGridViewImageCell dataGridViewImageCell = _dataGridViewImageCell; |
| 221 | + using Graphics g = Graphics.FromImage(new Bitmap(10, 10)); |
| 222 | + Action action = () => dataGridViewImageCell.TestAccessor().Dynamic.GetPreferredSize(g, null, 0, new Size(100, 100)); |
| 223 | + |
| 224 | + action.Should().Throw<NullReferenceException>(); |
| 225 | + } |
| 226 | + |
| 227 | + [WinFormsFact] |
| 228 | + public void Paint_DoesNotThrow_WithValidArguments_UsingTestAccessor() |
| 229 | + { |
| 230 | + using DataGridView dataGridView = new(); |
| 231 | + using DataGridViewImageColumn dataGridViewImageColumn = new(); |
| 232 | + dataGridView.Columns.Add(dataGridViewImageColumn); |
| 233 | + dataGridView.Rows.Add(); |
| 234 | + using DataGridViewImageCell dataGridViewImageCell = _dataGridViewImageCell; |
| 235 | + dataGridView.Rows[0].Cells[0] = dataGridViewImageCell; |
| 236 | + using Graphics g = Graphics.FromImage(new Bitmap(10, 10)); |
| 237 | + DataGridViewCellStyle dataGridViewCellStyle = new(); |
| 238 | + DataGridViewAdvancedBorderStyle borderStyle = new(); |
| 239 | + |
| 240 | + Action action = () => dataGridViewImageCell.TestAccessor().Dynamic.Paint( |
| 241 | + g, |
| 242 | + new Rectangle(0, 0, 10, 10), |
| 243 | + new Rectangle(0, 0, 10, 10), |
| 244 | + 0, |
| 245 | + DataGridViewElementStates.None, |
| 246 | + null, |
| 247 | + null, |
| 248 | + null, |
| 249 | + dataGridViewCellStyle, |
| 250 | + borderStyle, |
| 251 | + DataGridViewPaintParts.All); |
| 252 | + |
| 253 | + action.Should().NotThrow(); |
| 254 | + } |
| 255 | + |
| 256 | + [WinFormsFact] |
| 257 | + public void ColumnIndex_ReturnsExpectedFormat() |
| 258 | + { |
| 259 | + using DataGridView dataGridView = new(); |
| 260 | + dataGridView.Columns.Add(new DataGridViewImageColumn()); |
| 261 | + dataGridView.Rows.Add(); |
| 262 | + using DataGridViewImageCell dataGridViewImageCell = _dataGridViewImageCell; |
| 263 | + dataGridView.Rows[0].Cells[0] = dataGridViewImageCell; |
| 264 | + |
| 265 | + dataGridViewImageCell.ColumnIndex.Should().Be(0); |
| 266 | + } |
| 267 | +} |
0 commit comments