-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1179 from TestCentric/issue-1149
Sort tree nodes by name or duration
- Loading branch information
Showing
10 changed files
with
462 additions
and
7 deletions.
There are no files selected for viewing
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
112 changes: 112 additions & 0 deletions
112
src/TestCentric/testcentric.gui/Presenters/TreeViewNodeComparer.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,112 @@ | ||
// *********************************************************************** | ||
// Copyright (c) Charlie Poole and TestCentric contributors. | ||
// Licensed under the MIT License. See LICENSE file in root directory. | ||
// *********************************************************************** | ||
|
||
using System.Collections; | ||
using System.Windows.Forms; | ||
using TestCentric.Gui.Model; | ||
|
||
namespace TestCentric.Gui.Presenters | ||
{ | ||
/// <summary> | ||
/// This class is responsible for the sorting functionality of tree nodes | ||
/// by providing different IComparer implementations. | ||
/// </summary> | ||
public class TreeViewNodeComparer | ||
{ | ||
public const string Name = "Name"; | ||
public const string Duration = "Duration"; | ||
public const string Ascending = "Ascending"; | ||
public const string Descending = "Descending"; | ||
|
||
|
||
/// <summary> | ||
/// Get a IComparer implementation used to sort the tree nodes | ||
/// The sortModes are used by the context menu items in their Tag property | ||
/// </summary> | ||
public static IComparer GetComparer(ITestModel model, string sortMode, string sortDirection) | ||
{ | ||
bool ascending = sortDirection == Ascending; | ||
|
||
if (sortMode == Duration) | ||
return new DurationComparer(model, ascending); | ||
|
||
return new NameComparer(ascending); | ||
} | ||
|
||
/// <summary> | ||
/// The NameComparer uses the Name of the TestNodes (either Namespace or class or method name) | ||
/// It's invoked by Windows Forms for all nodes of one hierarchy level to provide a proper ordering | ||
/// </summary> | ||
private class NameComparer : IComparer | ||
{ | ||
private bool _ascending; | ||
|
||
internal NameComparer(bool ascending) | ||
{ | ||
_ascending = ascending; | ||
} | ||
|
||
public int Compare(object x, object y) | ||
{ | ||
TreeNode node1 = x as TreeNode; | ||
TreeNode node2 = y as TreeNode; | ||
|
||
if (!_ascending) | ||
Swap(ref node1, ref node2); | ||
|
||
if (node1 != null && node2 != null) | ||
return node1.Text.CompareTo(node2.Text); | ||
|
||
return 1; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// The DurationComparer uses the Duration of the TestResults | ||
/// It no test results are available yet, it uses the Name. | ||
/// </summary> | ||
private class DurationComparer : IComparer | ||
{ | ||
private ITestModel _model; | ||
private bool _ascending; | ||
|
||
internal DurationComparer(ITestModel model, bool ascending) | ||
{ | ||
_model = model; | ||
_ascending = ascending; | ||
} | ||
|
||
public int Compare(object x, object y) | ||
{ | ||
TreeNode node1 = x as TreeNode; | ||
TreeNode node2 = y as TreeNode; | ||
|
||
if (!_ascending) | ||
Swap(ref node1, ref node2); | ||
|
||
TestNode testNode1 = node1?.Tag as TestNode; | ||
TestNode testNode2 = node2?.Tag as TestNode; | ||
|
||
if (testNode1 == null || testNode2 == null) | ||
return 1; | ||
|
||
ResultNode resultNode1 = _model.GetResultForTest(testNode1.Id); | ||
ResultNode resultNode2 = _model.GetResultForTest(testNode2.Id); | ||
|
||
if (resultNode1 != null && resultNode2 != null) | ||
return resultNode1.Duration.CompareTo(resultNode2.Duration); | ||
|
||
return node1.Text.CompareTo(node2.Text); ; | ||
} | ||
} | ||
|
||
internal static void Swap<T>(ref T x, ref T y) | ||
{ | ||
T tmp = x; | ||
x = y; | ||
y = tmp; | ||
} | ||
} | ||
} |
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
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
73 changes: 73 additions & 0 deletions
73
src/TestCentric/testcentric.gui/Views/TestTreeView.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.