📄 testsuitetreeview.cs
字号:
{
MenuItem collapseMenuItem = new MenuItem(
"&Collapse", new EventHandler( collapseMenuItem_Click ) );
collapseMenuItem.DefaultItem = !runCommandEnabled;
this.ContextMenu.MenuItems.Add( collapseMenuItem );
}
else
{
MenuItem expandMenuItem = new MenuItem(
"&Expand", new EventHandler( expandMenuItem_Click ) );
expandMenuItem.DefaultItem = !runCommandEnabled;
this.ContextMenu.MenuItems.Add( expandMenuItem );
}
}
if ( this.ContextMenu.MenuItems.Count > 0 )
this.ContextMenu.MenuItems.Add( "-" );
MenuItem propertiesMenuItem = new MenuItem(
"&Properties", new EventHandler( propertiesMenuItem_Click ) );
this.ContextMenu.MenuItems.Add( propertiesMenuItem );
}
/// <summary>
/// When Expand context menu item is clicked, expand the node
/// </summary>
private void expandMenuItem_Click(object sender, System.EventArgs e)
{
contextNode.Expand();
}
/// <summary>
/// When Collapse context menu item is clicked, collapse the node
/// </summary>
private void collapseMenuItem_Click(object sender, System.EventArgs e)
{
contextNode.Collapse();
}
/// <summary>
/// When Run context menu item is clicked, run the test that
/// was selected when the right click was done.
/// </summary>
private void runMenuItem_Click(object sender, System.EventArgs e)
{
//TODO: some sort of lock on these booleans?
if ( runCommandEnabled )
{
runCommandEnabled = false;
RunTest( contextNode.Test );
}
}
private void propertiesMenuItem_Click( object sender, System.EventArgs e)
{
if ( contextNode != null )
ShowPropertiesDialog( contextNode );
}
#endregion
#region Drag and drop
/// <summary>
/// Helper method to determine if an IDataObject is valid
/// for dropping on the tree view. It must be a the drop
/// of a single file with a valid assembly file type.
/// </summary>
/// <param name="data">IDataObject to be tested</param>
/// <returns>True if dropping is allowed</returns>
private bool IsValidFileDrop( IDataObject data )
{
if ( !data.GetDataPresent( DataFormats.FileDrop ) )
return false;
string [] fileNames = data.GetData( DataFormats.FileDrop ) as string [];
if ( fileNames == null || fileNames.Length == 0 )
return false;
// We can't open more than one project at a time
if ( fileNames.Length == 1 )
{
if ( NUnitProject.IsProjectFile( fileNames[0] ) )
return true;
if ( UserSettings.Options.VisualStudioSupport )
{
if ( VSProject.IsProjectFile( fileNames[0] ) ||
VSProject.IsSolutionFile( fileNames[0] ) )
return true;
}
}
// Multiple assemblies are allowed - we
// assume they are all in the same directory
// since they are being dragged together.
foreach( string fileName in fileNames )
{
if ( !ProjectPath.IsAssemblyFileType( fileName ) )
return false;
}
return true;
}
private void TestSuiteTreeView_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
if ( IsValidFileDrop( e.Data ) )
{
string[] fileNames = (string[])e.Data.GetData( DataFormats.FileDrop );
if ( fileNames.Length == 1 )
loader.LoadProject( fileNames[0] );
else
loader.LoadProject( fileNames );
if (loader.IsProjectLoaded && loader.TestProject.IsLoadable)
loader.LoadTest();
}
}
private void TestSuiteTreeView_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
if ( IsValidFileDrop( e.Data ) )
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
#endregion
#region UI Event Handlers
private void TestSuiteTreeView_DoubleClick(object sender, System.EventArgs e)
{
if ( runCommandSupported && runCommandEnabled && SelectedNode.Nodes.Count == 0 )
{
runCommandEnabled = false;
// Since this is a terminal node, don't use a filter
loader.SetFilter( null );
loader.RunTest( SelectedTest );
}
}
protected override void OnAfterSelect(System.Windows.Forms.TreeViewEventArgs e)
{
if ( SelectedTestChanged != null )
SelectedTestChanged( SelectedTest );
base.OnAfterSelect( e );
}
protected override void OnAfterCheck(TreeViewEventArgs e)
{
if (CheckedTestChanged != null)
CheckedTestChanged(CheckedTests);
base.OnAfterCheck (e);
((TestSuiteTreeNode)e.Node).WasChecked = e.Node.Checked;
}
#endregion
#region Public methods to manipulate the tree
/// <summary>
/// Clear all the results in the tree.
/// </summary>
public void ClearResults()
{
foreach ( TestSuiteTreeNode rootNode in Nodes )
rootNode.ClearResults();
}
/// <summary>
/// Load the tree with a test hierarchy
/// </summary>
/// <param name="test">Test to be loaded</param>
public void Load( UITestNode test )
{
using( new CP.Windows.Forms.WaitCursor() )
{
Clear();
BeginUpdate();
try
{
AddTreeNodes( Nodes, test, false );
SetInitialExpansion();
}
finally
{
EndUpdate();
contextNode = null;
}
}
}
/// <summary>
/// Load the tree from a test result
/// </summary>
/// <param name="result"></param>
public void Load( TestResult result )
{
using ( new CP.Windows.Forms.WaitCursor( ) )
{
Clear();
BeginUpdate();
try
{
AddTreeNodes( Nodes, result, false );
SetInitialExpansion();
}
finally
{
EndUpdate();
}
}
}
/// <summary>
/// Reload the tree with a changed test hierarchy
/// while maintaining as much gui state as possible
/// </summary>
/// <param name="test">Test suite to be loaded</param>
public void Reload( UITestNode test )
{
TestSuiteTreeNode rootNode = (TestSuiteTreeNode) Nodes[0];
// Temporary change till framework is updated
// if ( !Match( rootNode, test ) )
// throw( new ArgumentException( "Reload called with non-matching test" ) );
UpdateNode( rootNode, test );
}
/// <summary>
/// Clear all the info in the tree.
/// </summary>
public void Clear()
{
treeMap.Clear();
Nodes.Clear();
}
private TreeNode savedTopNode;
public void SaveVisualState()
{
this.savedTopNode = this.TopNode;
}
public void RestoreVisualState()
{
if ( savedTopNode != null )
{
foreach ( TestSuiteTreeNode node in this.Nodes )
node.RestoreVisualState();
this.savedTopNode.EnsureVisible();
}
}
protected override void OnAfterCollapse(TreeViewEventArgs e)
{
base.OnAfterCollapse (e);
((TestSuiteTreeNode)e.Node).WasExpanded = false;
}
protected override void OnAfterExpand(TreeViewEventArgs e)
{
base.OnAfterExpand (e);
((TestSuiteTreeNode)e.Node).WasExpanded = true;
}
public void Accept(TestSuiteTreeNodeVisitor visitor)
{
foreach(TestSuiteTreeNode node in Nodes)
{
node.Accept(visitor);
}
}
public void ClearCheckedNodes()
{
Accept(new ClearCheckedNodesVisitor());
}
public void CheckFailedNodes()
{
Accept(new CheckFailedNodesVisitor());
}
public void ToggleCheckBoxes(bool visible)
{
this.CheckBoxes = visible;
if (!visible)
{
ClearCheckedNodes();
SetInitialExpansion();
}
}
/// <summary>
/// Add the result of a test to the tree
/// </summary>
/// <param name="result">The result of the test</param>
public void SetTestResult(TestResult result)
{
TestSuiteTreeNode node = this[result];
if ( node == null )
throw new ArgumentException( "Test not found in tree" );
node.SetResult( result );
if ( DisplayTestProgress )
{
Invalidate( node.Bounds );
Update();
}
}
/// <summary>
/// Collapse all fixtures in the tree
/// </summary>
public void CollapseFixtures()
{
foreach( TestSuiteTreeNode node in Nodes )
CollapseFixturesUnderNode( node );
}
/// <summary>
/// Expand all fixtures in the tree
/// </summary>
public void ExpandFixtures()
{
foreach( TestSuiteTreeNode node in Nodes )
ExpandFixturesUnderNode( node );
}
public void ShowPropertiesDialog( UITestNode test )
{
ShowPropertiesDialog( this[ test ] );
}
private void ShowPropertiesDialog( TestSuiteTreeNode node )
{
if ( propertiesDialog == null )
{
Form owner = this.FindForm();
propertiesDialog = new TestPropertiesDialog( node );
propertiesDialog.Owner = owner;
propertiesDialog.StartPosition = FormStartPosition.Manual;
propertiesDialog.Left = owner.Left + ( owner.Width - propertiesDialog.Width ) / 2;
propertiesDialog.Top = owner.Top + ( owner.Height - propertiesDialog.Height ) / 2;
propertiesDialog.Show();
propertiesDialog.Closed += new EventHandler( OnPropertiesDialogClosed );
}
else
{
propertiesDialog.DisplayProperties( node );
}
}
private void ClosePropertiesDialog()
{
if ( propertiesDialog != null )
propertiesDialog.Close();
}
private void CheckPropertiesDialog()
{
if ( propertiesDialog != null && !propertiesDialog.Pinned )
propertiesDialog.Close();
}
private void OnPropertiesDialogClosed( object sender, System.EventArgs e )
{
propertiesDialog = null;
}
public void RunTests()
{
RunTests( SelectedTests );
}
private void RunTest( UITestNode test )
{
RunTests( new UITestNode[] { test } );
}
private void RunTests( UITestNode[] tests )
{
if ( SelectedCategories != null && SelectedCategories.Length > 0 )
loader.SetFilter( new CategoryFilter( this.SelectedCategories, this.ExcludeSelectedCategories ) );
else
loader.SetFilter( null );
loader.RunTests( tests );
}
#endregion
#region Helper Methods
/// <summary>
/// Add nodes to the tree constructed from a test
/// </summary>
/// <param name="nodes">The TreeNodeCollection to which the new node should be added</param>
/// <param name="rootTest">The test for which a node is to be built</param>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -