📄 testsuitetreeview.cs
字号:
/// <param name="highlight">If true, highlight the text for this node in the tree</param>
/// <returns>A newly constructed TestNode, possibly with descendant nodes</returns>
private TestSuiteTreeNode AddTreeNodes( IList nodes, ITest rootTest, bool highlight )
{
TestSuiteTreeNode node = new TestSuiteTreeNode( rootTest );
// if ( highlight ) node.ForeColor = Color.Blue;
treeMap.Add( node.Test.UniqueName, node );
nodes.Add( node );
if ( rootTest.IsSuite )
{
foreach( UITestNode test in rootTest.Tests )
AddTreeNodes( node.Nodes, test, highlight );
}
return node;
}
private TestSuiteTreeNode AddTreeNodes( IList nodes, TestResult rootResult, bool highlight )
{
TestSuiteTreeNode node = new TestSuiteTreeNode( rootResult );
// if ( highlight ) node.ForeColor = Color.Blue;
treeMap.Add( node.Test.UniqueName, node );
nodes.Add( node );
TestSuiteResult suiteResult = rootResult as TestSuiteResult;
if ( suiteResult != null )
{
foreach( TestResult result in suiteResult.Results )
AddTreeNodes( node.Nodes, result, highlight );
}
node.UpdateImageIndex();
return node;
}
private void RemoveFromMap( TestSuiteTreeNode node )
{
foreach( TestSuiteTreeNode child in node.Nodes )
RemoveFromMap( child );
treeMap.Remove( node.Test.UniqueName );
}
/// <summary>
/// Remove a node from the tree itself and the hashtable
/// </summary>
/// <param name="node">Node to remove</param>
private void RemoveNode( TestSuiteTreeNode node )
{
if ( contextNode == node )
contextNode = null;
RemoveFromMap( node );
node.Remove();
}
/// <summary>
/// Helper routine that compares a node with a test
/// </summary>
/// <param name="node">Node to compare</param>
/// <param name="test">Test to compare</param>
/// <returns>True if the test has the same name</returns>
private bool Match( TestSuiteTreeNode node, UITestNode test )
{
return node.Test.FullName == test.FullName;
}
/// <summary>
/// A node has been matched with a test, so update it
/// and then process child nodes and tests recursively.
/// If a child was added or removed, then this node
/// will expand itself.
/// </summary>
/// <param name="node">Node to be updated</param>
/// <param name="test">Test to plug into node</param>
/// <returns>True if a child node was added or deleted</returns>
private bool UpdateNode( TestSuiteTreeNode node, UITestNode test )
{
node.UpdateTest( test );
if ( !test.IsSuite )
return false;
bool showChildren = UpdateNodes( node.Nodes, test.Tests );
if ( showChildren ) node.Expand();
return showChildren;
}
/// <summary>
/// Match a set of nodes against a set of tests.
/// Remove nodes that are no longer represented
/// in the tests. Update any nodes that match.
/// Add new nodes for new tests.
/// </summary>
/// <param name="nodes">List of nodes to be matched</param>
/// <param name="tests">List of tests to be matched</param>
/// <returns>True if the parent should expand to show that something was added or deleted</returns>
private bool UpdateNodes( IList nodes, IList tests )
{
bool showChanges = false;
foreach( TestSuiteTreeNode node in nodes )
if ( NodeWasDeleted( node, tests ) )
{
RemoveNode( node );
showChanges = true;
}
foreach( UITestNode test in tests )
{
TestSuiteTreeNode node = this[ test ];
if ( node == null )
{
AddTreeNodes( nodes, test, true );
showChanges = true;
}
else
UpdateNode( node, test );
}
return showChanges;
}
/// <summary>
/// Helper returns true if the node test is not in
/// the list of tests provided.
/// </summary>
/// <param name="node">Node to examine</param>
/// <param name="tests">List of tests to match with node</param>
private bool NodeWasDeleted( TestSuiteTreeNode node, IList tests )
{
foreach ( UITestNode test in tests )
if( Match( node, test ) )
return false;
return true;
}
/// <summary>
/// Delegate for use in invoking the tree loader
/// from the watcher thread.
/// </summary>
private delegate void LoadHandler( UITestNode test );
private delegate void PropertiesDisplayHandler();
/// <summary>
/// Helper collapses all fixtures under a node
/// </summary>
/// <param name="node">Node under which to collapse fixtures</param>
private void CollapseFixturesUnderNode( TestSuiteTreeNode node )
{
if ( node.Test.IsFixture )
node.Collapse();
else
foreach( TestSuiteTreeNode child in node.Nodes )
CollapseFixturesUnderNode( child );
}
/// <summary>
/// Helper expands all fixtures under a node
/// </summary>
/// <param name="node">Node under which to expand fixtures</param>
private void ExpandFixturesUnderNode( TestSuiteTreeNode node )
{
if ( node.Test.IsFixture )
node.Expand();
else
foreach( TestSuiteTreeNode child in node.Nodes )
ExpandFixturesUnderNode( child );
}
/// <summary>
/// Helper used to figure out the display style
/// to use when the setting is Auto
/// </summary>
/// <returns>DisplayStyle to be used</returns>
private DisplayStyle GetDisplayStyle()
{
DisplayStyle initialDisplay =
(DisplayStyle)UserSettings.Options.InitialTreeDisplay;
if ( initialDisplay != DisplayStyle.Auto )
return initialDisplay;
if ( VisibleCount >= this.GetNodeCount( true ) )
return DisplayStyle.Expand;
return DisplayStyle.HideTests;
}
public void SetInitialExpansion()
{
CollapseAll();
switch ( GetDisplayStyle() )
{
case DisplayStyle.Expand:
ExpandAll();
break;
case DisplayStyle.HideTests:
ExpandAll();
CollapseFixtures();
break;
case DisplayStyle.Collapse:
default:
break;
}
SelectedNode = Nodes[0];
SelectedNode.EnsureVisible();
}
#endregion
}
internal class ClearCheckedNodesVisitor : TestSuiteTreeNodeVisitor
{
public override void Visit(TestSuiteTreeNode node)
{
node.Checked = false;
}
}
internal class CheckFailedNodesVisitor : TestSuiteTreeNodeVisitor
{
public override void Visit(TestSuiteTreeNode node)
{
if (node.Test.IsTestCase && node.Result != null && node.Result.IsFailure)
{
node.Checked = true;
node.EnsureVisible();
}
else
node.Checked = false;
}
}
public class SelectedCategoriesVisitor : TestSuiteTreeNodeVisitor
{
private string[] categories;
private bool exclude;
public SelectedCategoriesVisitor( string[] categories ) : this( categories, false ) { }
public SelectedCategoriesVisitor( string[] categories, bool exclude )
{
this.categories = categories;
this.exclude = exclude;
}
public override void Visit( TestSuiteTreeNode node )
{
// If there are no categories selected
if ( categories.Length == 0 )
{
//node.Checked = false;
node.Included = true; //TODO: Look for explicit categories
}
else
{
node.Included = exclude;
TestSuiteTreeNode parent = node.Parent as TestSuiteTreeNode;
if ( parent != null )
node.Included = parent.Included;
foreach( string category in categories )
{
if ( node.Test.Categories.Contains( category ) )
{
node.Included = !exclude;
break;
}
}
}
}
}
internal class CheckedTestFinder
{
[Flags]
public enum SelectionFlags
{
Top= 1,
Sub = 2,
Explicit = 4,
All = Top + Sub
}
private ArrayList checkedTests = new ArrayList();
private struct CheckedTestInfo
{
public UITestNode Test;
public bool TopLevel;
public CheckedTestInfo( UITestNode test, bool topLevel )
{
this.Test = test;
this.TopLevel = topLevel;
}
}
public UITestNode[] GetCheckedTests( SelectionFlags flags )
{
int count = 0;
foreach( CheckedTestInfo info in checkedTests )
if ( isSelected( info, flags ) ) count++;
UITestNode[] result = new UITestNode[count];
int index = 0;
foreach( CheckedTestInfo info in checkedTests )
if ( isSelected( info, flags ) )
result[index++] = info.Test;
return result;
}
private bool isSelected( CheckedTestInfo info, SelectionFlags flags )
{
if ( info.TopLevel && (flags & SelectionFlags.Top) != 0 )
return true;
else if ( !info.TopLevel && (flags & SelectionFlags.Sub) != 0 )
return true;
else if ( info.Test.IsExplicit && (flags & SelectionFlags.Explicit) != 0 )
return true;
else
return false;
}
// public UITestNode[] AllCheckedTests
// {
// get
// {
// UITestNode[] result = new UITestNode[checkedTests.Count];
//
// int index = 0;
// foreach( CheckedTestInfo info in checkedTests )
// result[index++] = info.Test;
//
// return result;
// }
// }
//
// public UITestNode[] TopLevelCheckedTests
// {
// get
// {
// int count = 0;
// foreach( CheckedTestInfo info in checkedTests )
// if ( info.TopLevel ) count++;
//
// UITestNode[] result = new UITestNode[count];
//
// int index = 0;
// foreach( CheckedTestInfo info in checkedTests )
// if ( info.TopLevel )
// result[index++] = info.Test;
//
// return result;
// }
// }
//
// public UITestNode[] SubordinateCheckedTests
// {
// get
// {
// int count = 0;
// foreach( CheckedTestInfo info in checkedTests )
// if ( !info.TopLevel ) count++;
//
// UITestNode[] result = new UITestNode[count];
//
// int index = 0;
// foreach( CheckedTestInfo info in checkedTests )
// if ( !info.TopLevel )
// result[index++] = info.Test;
//
// return result;
// }
// }
public CheckedTestFinder( TestSuiteTreeView treeView )
{
FindCheckedNodes( treeView.Nodes, true );
}
private void FindCheckedNodes( TestSuiteTreeNode node, bool topLevel )
{
if ( node.Checked )
{
checkedTests.Add( new CheckedTestInfo( node.Test, topLevel ) );
topLevel = false;
}
FindCheckedNodes( node.Nodes, topLevel );
}
private void FindCheckedNodes( TreeNodeCollection nodes, bool topLevel )
{
foreach( TestSuiteTreeNode node in nodes )
FindCheckedNodes( node, topLevel );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -