-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add code coverage for ControlCodeDomSerializer (#13011)
Related #10773 Proposed changes Add unit test file: ControlCodeDomSerializerTests.cs for public methods of the ControlCodeDomSerializer.cs.
- Loading branch information
1 parent
0d3e6f6
commit 2dc1fbe
Showing
1 changed file
with
80 additions
and
0 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
...Forms.Design/tests/UnitTests/System/Windows/Forms/Design/ControlCodeDomSerializerTests.cs
This file contains 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,80 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#nullable enable | ||
|
||
using System.CodeDom; | ||
using System.ComponentModel; | ||
using System.ComponentModel.Design; | ||
using System.ComponentModel.Design.Serialization; | ||
using Moq; | ||
|
||
namespace System.Windows.Forms.Design.Tests; | ||
|
||
public class ControlCodeDomSerializerTests : IDisposable | ||
{ | ||
private readonly Mock<IDesignerSerializationManager> _managerMock; | ||
private readonly Mock<IDesignerHost> _hostMock; | ||
private readonly Mock<CodeDomSerializer> _serializerMock; | ||
private readonly ControlCodeDomSerializer _controlCodeDomSerializer; | ||
private readonly TestControl _testControl; | ||
private readonly CodeStatementCollection _codeStatementCollection; | ||
|
||
public ControlCodeDomSerializerTests() | ||
{ | ||
_managerMock = new(); | ||
_hostMock = new(); | ||
_serializerMock = new(); | ||
_controlCodeDomSerializer = new(); | ||
_testControl = new(); | ||
_codeStatementCollection = []; | ||
|
||
_managerMock.Setup(m => m.GetService(typeof(IDesignerHost))).Returns(_hostMock.Object); | ||
_managerMock.Setup(m => m.GetSerializer(typeof(Component), typeof(CodeDomSerializer))).Returns(_serializerMock.Object); | ||
} | ||
|
||
public void Dispose() => _testControl.Dispose(); | ||
|
||
[Fact] | ||
public void Deserialize_SuspendsAndResumesLayout() | ||
{ | ||
Mock<IContainer> mockContainer = new(); | ||
_managerMock.Setup(m => m.GetService(typeof(IContainer))).Returns(mockContainer.Object); | ||
mockContainer.Setup(c => c.Components).Returns(new ComponentCollection([_testControl])); | ||
|
||
object codeObject = new(); | ||
object expectedObjectGraphData = new(); | ||
_serializerMock.Setup(s => s.Deserialize(_managerMock.Object, codeObject)).Returns(expectedObjectGraphData); | ||
|
||
_controlCodeDomSerializer.Deserialize(_managerMock.Object, codeObject).Should().Be(expectedObjectGraphData); | ||
} | ||
|
||
[Fact] | ||
public void Serialize_ShouldReturnSerializedObject() | ||
{ | ||
_serializerMock.Setup(s => s.Serialize(_managerMock.Object, _testControl)).Returns(_codeStatementCollection); | ||
|
||
CodeStatementCollection? result = _controlCodeDomSerializer.Serialize(_managerMock.Object, _testControl) as CodeStatementCollection; | ||
|
||
result.Should().NotBeNull(); | ||
result.Should().BeOfType<CodeStatementCollection>(); | ||
} | ||
|
||
public class TestControl : Control | ||
{ | ||
public bool SuspendLayoutCalled { get; private set; } | ||
public bool ResumeLayoutCalled { get; private set; } | ||
|
||
public new void SuspendLayout() | ||
{ | ||
SuspendLayoutCalled = true; | ||
base.SuspendLayout(); | ||
} | ||
|
||
public new void ResumeLayout(bool performLayout) | ||
{ | ||
ResumeLayoutCalled = true; | ||
base.ResumeLayout(performLayout); | ||
} | ||
} | ||
} |