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 test coverage for TreeView #11361

Merged
merged 4 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace System.Windows.Forms.Tests;

public class TreeViewImageKeyConverterTests
{
private readonly TreeViewImageKeyConverter _converter = new();

[WinFormsTheory]
[InlineData(null, "(default)")]
[InlineData("", "(default)")]
[InlineData("non-empty", "non-empty")]
public void TreeViewImageKeyConverter_ConvertTo_StringDestinationType_ReturnsExpected(string value, string expected)
{
object result = _converter.ConvertTo(context: null, culture: null, value: value, destinationType: typeof(string));
result.Should().Be(expected);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7363,6 +7363,167 @@ public void TreeView_TreeNodeAddRangeSequence()
childNode8.NextVisibleNode.Should().BeNull();
}

[WinFormsTheory]
[InlineData(false, null)]
[InlineData(true, null)]
[InlineData(false, "node")]
[InlineData(true, "node")]
public void TreeView_TopNode_Test(bool createHandle, string nodeName)
{
using TreeView treeView = new();
TreeNode node = nodeName is null ? null : new TreeNode(nodeName);

if (node is not null)
{
treeView.Nodes.Add(node);
}

if (createHandle)
{
_ = treeView.Handle;
}

treeView.TopNode = node;

treeView.TopNode.Should().Be(node);
treeView.IsHandleCreated.Should().Be(createHandle);
}

[WinFormsFact]
public void TreeViewLabelEditNativeWindow_AccessibilityObject_ReturnsExpected()
{
using TreeView treeView = new();

TreeViewLabelEditNativeWindow nativeWindow = new(treeView);
var accessibilityObject = nativeWindow.AccessibilityObject;

accessibilityObject.Should().NotBeNull();
accessibilityObject.Should().BeOfType<TreeViewLabelEditAccessibleObject>();
}

private void AddNodes(TreeView control, string rootNodeName, string childNodeName = null)
{
TreeNode rootNode = new(rootNodeName);
if (childNodeName is not null)
{
rootNode.Nodes.Add(new TreeNode(childNodeName));
}

control.Nodes.Add(rootNode);
}

private TreeView InitializeTreeViewWithNodes()
{
TreeView treeView = new();
AddNodes(treeView, "Root1", "Child1");
AddNodes(treeView, "Root2");
Zheng-Li01 marked this conversation as resolved.
Show resolved Hide resolved

return treeView;
}

[WinFormsFact]
public void TreeView_CollapseAll_Invoke_CollapsesAllNodes()
{
using TreeView treeView = InitializeTreeViewWithNodes();

treeView.CollapseAll();

treeView.Nodes[0].IsExpanded.Should().BeFalse();
treeView.Nodes[1].IsExpanded.Should().BeFalse();
}

[WinFormsFact]
public void TreeView_ExpandAll_Invoke_UpdatesAllNodes()
{
using TreeView treeView = InitializeTreeViewWithNodes();

treeView.ExpandAll();

treeView.Nodes[0].IsExpanded.Should().BeTrue();
treeView.Nodes[1].IsExpanded.Should().BeTrue();
}

[WinFormsFact]
public void TreeView_GetNodeCount_Invoke_ReturnsExpected()
{
using TreeView treeView = InitializeTreeViewWithNodes();

int countWithoutSubTrees = treeView.GetNodeCount(false);
countWithoutSubTrees.Should().Be(2);

int countWithSubTrees = treeView.GetNodeCount(true);
countWithSubTrees.Should().Be(3);
}

[WinFormsFact]
public void TreeView_ResetIndent_Invoke_Success()
{
using TreeView treeView = new();

treeView.Indent = 10;
var accessor = treeView.TestAccessor();
accessor.Dynamic.ResetIndent();

treeView.Indent.Should().Be(19);
}

[WinFormsFact]
public void TreeView_ResetItemHeight_Invoke_Success()
{
using TreeView treeView = new();

treeView.ItemHeight = 10;
var accessor = treeView.TestAccessor();
accessor.Dynamic.ResetItemHeight();

treeView.ItemHeight.Should().Be(19);
}

[WinFormsFact]
public void TreeView_ShouldSerializeIndent_Invoke_ReturnsExpected()
{
using TreeView treeView = new();

var accessor = treeView.TestAccessor();
bool result = accessor.Dynamic.ShouldSerializeIndent();

result.Should().BeFalse();

treeView.Indent = 10;
result = accessor.Dynamic.ShouldSerializeIndent();

result.Should().BeTrue();
}

[WinFormsFact]
public void TreeView_ShouldSerializeItemHeight_Invoke_ReturnsExpected()
{
using TreeView treeView = new();

var accessor = treeView.TestAccessor();
bool result = accessor.Dynamic.ShouldSerializeItemHeight();

result.Should().BeFalse();

treeView.ItemHeight = 10;
result = accessor.Dynamic.ShouldSerializeItemHeight();

result.Should().BeTrue();
}

[WinFormsFact]
public void TreeView_ToString_Invoke_ReturnsExpected()
{
using TreeView treeView = new();

treeView.ToString().Should().Be("System.Windows.Forms.TreeView, Nodes.Count: 0");

treeView.Nodes.Add(new TreeNode("Node1"));
treeView.Nodes.Add(new TreeNode("Node2"));

treeView.ToString().Should().Be($"System.Windows.Forms.TreeView, Nodes.Count: 2, Nodes[0]: {treeView.Nodes[0]}");
}

private class SubTreeView : TreeView
{
public new bool CanEnableIme => base.CanEnableIme;
Expand Down