|
| 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.ComponentModel; |
| 5 | +using System.Reflection; |
| 6 | +using Moq; |
| 7 | + |
| 8 | +namespace System.Windows.Forms.Design.Tests; |
| 9 | + |
| 10 | +public class ListViewGroupCollectionEditorTests |
| 11 | +{ |
| 12 | + private readonly Mock<ListViewGroupCollectionEditor> _mockEditor; |
| 13 | + |
| 14 | + public ListViewGroupCollectionEditorTests() |
| 15 | + { |
| 16 | + _mockEditor = new(typeof(ListViewGroup)) { CallBase = true }; |
| 17 | + } |
| 18 | + |
| 19 | + [Fact] |
| 20 | + public void Constructor_InitializesCollectionType() |
| 21 | + { |
| 22 | + Type expectedType = typeof(ListViewGroup); |
| 23 | + |
| 24 | + ListViewGroupCollectionEditor editor = new(expectedType); |
| 25 | + |
| 26 | + Type actualType = editor.TestAccessor().Dynamic.CollectionType; |
| 27 | + |
| 28 | + actualType.Should().Be(expectedType); |
| 29 | + } |
| 30 | + |
| 31 | + [Fact] |
| 32 | + public void EditValue_SetsAndResetsEditValue() |
| 33 | + { |
| 34 | + Mock<ITypeDescriptorContext> mockContext = new(); |
| 35 | + Mock<IServiceProvider> mockProvider = new(); |
| 36 | + object inputValue = new(); |
| 37 | + object expectedResult = new(); |
| 38 | + |
| 39 | + _mockEditor |
| 40 | + .Setup(e => e.EditValue(mockContext.Object, mockProvider.Object, inputValue)) |
| 41 | + .Returns(expectedResult); |
| 42 | + |
| 43 | + object result = _mockEditor.Object.EditValue(mockContext.Object, mockProvider.Object, inputValue); |
| 44 | + |
| 45 | + result.Should().Be(expectedResult); |
| 46 | + } |
| 47 | + |
| 48 | + [Fact] |
| 49 | + public void CreateInstance_CreatesListViewGroupWithUniqueName() |
| 50 | + { |
| 51 | + Mock<ListViewGroupCollection> mockCollection = new(new ListView()); |
| 52 | + _mockEditor.Object.TestAccessor().Dynamic._editValue = mockCollection.Object; |
| 53 | + |
| 54 | + ListViewGroup? result = _mockEditor.Object.TestAccessor().Dynamic.CreateInstance(typeof(ListViewGroup)) as ListViewGroup; |
| 55 | + |
| 56 | + result?.Name.Should().NotBeNull(); |
| 57 | + result?.Name.Should().StartWith("ListViewGroup"); |
| 58 | + result?.GetType().Should().Be(typeof(ListViewGroup)); |
| 59 | + } |
| 60 | + |
| 61 | + [Fact] |
| 62 | + public void CreateInstance_ThrowsException_WhenEditValueIsNull() |
| 63 | + { |
| 64 | + _mockEditor.Object.TestAccessor().Dynamic._editValue = null; |
| 65 | + |
| 66 | + Action action = () => _mockEditor.Object.TestAccessor().Dynamic.CreateInstance(typeof(ListViewGroup)); |
| 67 | + |
| 68 | + action.Should().Throw<TargetInvocationException>(); |
| 69 | + } |
| 70 | +} |
0 commit comments