Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Fix bug to check parent file when meta is checked #924

Merged
merged 10 commits into from
Oct 5, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
97 changes: 45 additions & 52 deletions src/GitHub.Api/UI/TreeBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,23 +183,9 @@ protected bool PromoteNode(TNode previouslyAddedNode, string nextLabel)

public void SetCheckStateOnAll(bool isChecked)
{
var nodeCheckState = isChecked ? CheckState.Checked : CheckState.Empty;
foreach (var node in Nodes)
{
var wasChecked = node.CheckState == CheckState.Checked;
node.CheckState = nodeCheckState;

if (!node.IsFolder)
{
if (isChecked && !wasChecked)
{
AddCheckedNode(node);
}
else if (!isChecked && wasChecked)
{
RemoveCheckedNode(node);
}
}
SetCheckStateOnNode(node, isChecked);
}
}

Expand Down Expand Up @@ -250,60 +236,41 @@ protected void ToggleNodeVisibility(int idx, TNode node)

protected void ToggleNodeChecked(int idx, TNode node)
{
CheckState checkState;
var isChecked = false;

switch (node.CheckState)
{
case CheckState.Mixed:
case CheckState.Empty:
node.CheckState = CheckState.Checked;
checkState = CheckState.Checked;
isChecked = true;
break;

case CheckState.Checked:
node.CheckState = CheckState.Empty;
checkState = CheckState.Empty;
break;
}

if (!node.IsFolder)
{
if (isChecked)
{
AddCheckedNode(node);
}
else
{
RemoveCheckedNode(node);
}
default:
throw new ArgumentOutOfRangeException("Unknown CheckState");
}

SetCheckStateOnNode(node, checkState);

if (node.IsFolderOrContainer)
{
ToggleChildrenChecked(idx, node, isChecked);
}

ToggleParentFoldersChecked(idx, node, isChecked);
ToggleParentFolderAndContainersChecked(idx, node, checkState);
}

private void ToggleChildrenChecked(int idx, TNode node, bool isChecked)
{
for (var i = idx + 1; i < Nodes.Count && node.Level < Nodes[i].Level; i++)
{
var childNode = Nodes[i];
var wasChecked = childNode.CheckState == CheckState.Checked;
childNode.CheckState = isChecked ? CheckState.Checked : CheckState.Empty;

if (!childNode.IsFolder)
{
if (isChecked && !wasChecked)
{
AddCheckedNode(childNode);
}
else if (!isChecked && wasChecked)
{
RemoveCheckedNode(childNode);
}
}
SetCheckStateOnNode(childNode, isChecked);

if (childNode.IsFolderOrContainer)
{
Expand Down Expand Up @@ -332,9 +299,36 @@ private List<TNode> GetLeafNodes(TNode node, int idx)
return results;
}

private void SetCheckStateOnNode(TNode node, bool setChecked)
{
SetCheckStateOnNode(node, setChecked ? CheckState.Checked : CheckState.Empty);
}

private void SetCheckStateOnNode(TNode node, CheckState setCheckState)
{
var isChecked = setCheckState == CheckState.Checked
|| setCheckState == CheckState.Mixed;

var wasChecked = node.CheckState == CheckState.Checked;

node.CheckState = setCheckState;

if (!node.IsFolder)
{
if (isChecked && !wasChecked)
{
AddCheckedNode(node);
}
else if (!isChecked && wasChecked)
{
RemoveCheckedNode(node);
}
}
}

private void ToggleParentFoldersChecked(int idx, TNode node, bool isChecked)
private void ToggleParentFolderAndContainersChecked(int idx, TNode node, CheckState checkState)
{
var isChecked = checkState != CheckState.Empty;
while (true)
{
if (node.Level > 0)
Expand Down Expand Up @@ -384,14 +378,13 @@ private void ToggleParentFoldersChecked(int idx, TNode node, bool isChecked)

var parentIndex = firstSiblingIndex - 1;
var parentNode = Nodes[parentIndex];
if (siblingsInSameState)
{
parentNode.CheckState = isChecked ? CheckState.Checked : CheckState.Empty;
}
else
{
parentNode.CheckState = CheckState.Mixed;
}

var parentNodeState =
siblingsInSameState
? node.CheckState
: CheckState.Mixed;

SetCheckStateOnNode(parentNode, parentNodeState);

idx = parentIndex;
node = parentNode;
Expand Down
210 changes: 210 additions & 0 deletions src/tests/UnitTests/UI/TreeBaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ public override TestTreeNode SelectedNode
}
}

public new void ToggleNodeChecked(int idx, TestTreeNode node)
{
base.ToggleNodeChecked(idx, node);
}

protected override List<TestTreeNode> Nodes
{
get
Expand Down Expand Up @@ -606,6 +611,211 @@ public void ShouldPopulateTreeWithSingleEntryWithMetaInPath()
}
});
}

[Test]
public void ShouldCheckParentOfMetaFile()
{
var testTree = new TestTree(true);
var testTreeListener = testTree.TestTreeListener;

testTreeListener.GetCollapsedFolders().Returns(new string[0]);
testTreeListener.SelectedNode.Returns((TestTreeNode)null);
testTreeListener.GetCheckedFiles().Returns(new string[0]);
testTreeListener.Nodes.Returns(new List<TestTreeNode>());
testTreeListener.PathSeparator.Returns(@"\");
testTreeListener.DisplayRootNode.Returns(true);
testTreeListener.IsSelectable.Returns(false);
testTreeListener.Title.Returns("Test Tree");
testTreeListener.PromoteMetaFiles.Returns(true);

var testTreeData = new[] {
new TestTreeData {
Path = "Folder\\Default Scene.unity"
},
new TestTreeData {
Path = "Folder\\Default Scene.unity.meta"
}
};
testTree.Load(testTreeData);

testTree.CreatedTreeNodes.ShouldAllBeEquivalentTo(new[] {
new TestTreeNode {
Path = "Test Tree",
Label = "Test Tree",
IsFolder = true
},
new TestTreeNode {
Path = "Folder",
Label = "Folder",
Level = 1,
IsFolder = true
},
new TestTreeNode {
Path = "Folder\\Default Scene.unity",
Label = "Default Scene.unity",
Level = 2,
TreeData = testTreeData[0],
IsContainer = true
},
new TestTreeNode {
Path = "Folder\\Default Scene.unity.meta",
Label = "Default Scene.unity.meta",
Level = 3,
TreeData = testTreeData[1]
}
});

var sceneNode = testTree.CreatedTreeNodes[2];
var sceneMetaNode = testTree.CreatedTreeNodes[3];

Assert.AreEqual(CheckState.Empty, sceneNode.CheckState);
Assert.AreEqual(CheckState.Empty, sceneMetaNode.CheckState);

testTree.ToggleNodeChecked(3, sceneMetaNode);

Assert.AreEqual(CheckState.Checked, sceneNode.CheckState);
Assert.AreEqual(CheckState.Checked, sceneMetaNode.CheckState);

testTreeListener.Received(2).AddCheckedNode(Arg.Any<TestTreeNode>());
}

[Test]
public void ShouldRippleChecksCorrectly()
{
var testTree = new TestTree(true);
var testTreeListener = testTree.TestTreeListener;

testTreeListener.GetCollapsedFolders().Returns(new string[0]);
testTreeListener.SelectedNode.Returns((TestTreeNode)null);
testTreeListener.GetCheckedFiles().Returns(new string[0]);
testTreeListener.Nodes.Returns(new List<TestTreeNode>());
testTreeListener.PathSeparator.Returns(@"\");
testTreeListener.DisplayRootNode.Returns(true);
testTreeListener.IsSelectable.Returns(false);
testTreeListener.Title.Returns("Test Tree");
testTreeListener.PromoteMetaFiles.Returns(true);

var testTreeData = new[] {
new TestTreeData {
Path = "Root\\Parent\\A.txt"
},
new TestTreeData {
Path = "Root\\Parent\\B.txt"
},
new TestTreeData {
Path = "Root\\Parent\\C.txt"
}
};

testTree.Load(testTreeData);

testTree.CreatedTreeNodes.ShouldAllBeEquivalentTo(new[] {
new TestTreeNode {
Path = "Test Tree",
Label = "Test Tree",
IsFolder = true
},
new TestTreeNode {
Path = "Root",
Label = "Root",
Level = 1,
IsFolder = true
},
new TestTreeNode {
Path = "Root\\Parent",
Label = "Parent",
Level = 2,
IsFolder = true
},
new TestTreeNode {
Path = "Root\\Parent\\A.txt",
Label = "A.txt",
Level = 3,
TreeData = testTreeData[0],
},
new TestTreeNode {
Path = "Root\\Parent\\B.txt",
Label = "B.txt",
Level = 3,
TreeData = testTreeData[1],
},
new TestTreeNode {
Path = "Root\\Parent\\C.txt",
Label = "C.txt",
Level = 3,
TreeData = testTreeData[2],
}
});

var rootNode = testTree.CreatedTreeNodes[1];
var parentNode = testTree.CreatedTreeNodes[2];
var aNode = testTree.CreatedTreeNodes[3];
var bNode = testTree.CreatedTreeNodes[4];
var cNode = testTree.CreatedTreeNodes[5];

// Initial state, everything unchecked

Assert.AreEqual(CheckState.Empty, rootNode.CheckState);
Assert.AreEqual(CheckState.Empty, parentNode.CheckState);
Assert.AreEqual(CheckState.Empty, aNode.CheckState);
Assert.AreEqual(CheckState.Empty, bNode.CheckState);
Assert.AreEqual(CheckState.Empty, cNode.CheckState);

testTree.ToggleNodeChecked(1, rootNode);

// Checked the root node, everything checked

Assert.AreEqual(CheckState.Checked, rootNode.CheckState);
Assert.AreEqual(CheckState.Checked, parentNode.CheckState);
Assert.AreEqual(CheckState.Checked, aNode.CheckState);
Assert.AreEqual(CheckState.Checked, bNode.CheckState);
Assert.AreEqual(CheckState.Checked, cNode.CheckState);

testTreeListener.Received(3).AddCheckedNode(Arg.Any<TestTreeNode>());
testTreeListener.ClearReceivedCalls();

// Unchecked c.txt, c.txt unchecked, parents mixed

testTree.ToggleNodeChecked(5, cNode);

Assert.AreEqual(CheckState.Mixed, rootNode.CheckState);
Assert.AreEqual(CheckState.Mixed, parentNode.CheckState);
Assert.AreEqual(CheckState.Checked, aNode.CheckState);
Assert.AreEqual(CheckState.Checked, bNode.CheckState);
Assert.AreEqual(CheckState.Empty, cNode.CheckState);

testTreeListener.Received(1).RemoveCheckedNode(Arg.Any<TestTreeNode>());
testTreeListener.ClearReceivedCalls();

testTree.ToggleNodeChecked(5, cNode);

// Checked c.txt, everything checked

Assert.AreEqual(CheckState.Checked, rootNode.CheckState);
Assert.AreEqual(CheckState.Checked, parentNode.CheckState);
Assert.AreEqual(CheckState.Checked, aNode.CheckState);
Assert.AreEqual(CheckState.Checked, bNode.CheckState);
Assert.AreEqual(CheckState.Checked, cNode.CheckState);

testTreeListener.Received(1).AddCheckedNode(Arg.Any<TestTreeNode>());
testTreeListener.ClearReceivedCalls();

// Unchecked a.txt b.txt and c.txt, everything checked

testTree.ToggleNodeChecked(3, aNode);
testTree.ToggleNodeChecked(4, bNode);
testTree.ToggleNodeChecked(5, cNode);

Assert.AreEqual(CheckState.Empty, rootNode.CheckState);
Assert.AreEqual(CheckState.Empty, parentNode.CheckState);
Assert.AreEqual(CheckState.Empty, aNode.CheckState);
Assert.AreEqual(CheckState.Empty, bNode.CheckState);
Assert.AreEqual(CheckState.Empty, cNode.CheckState);

testTreeListener.Received(3).RemoveCheckedNode(Arg.Any<TestTreeNode>());
testTreeListener.ClearReceivedCalls();
}

[Test]
public void ShouldPopulateTreeWithSingleEntryWithNonPromotedMetaInPath()
{
Expand Down