// // // // // $Revision$ // using System; using System.Collections.Generic; using ICSharpCode.Core; using ICSharpCode.SharpDevelop; using ICSharpCode.SharpDevelop.Dom; using ICSharpCode.SharpDevelop.Gui; using ICSharpCode.SharpDevelop.Project; namespace ICSharpCode.UnitTesting { /// /// Tree view that shows all the unit tests in a project. /// public class TestTreeView : ExtTreeView, ITestTreeView, IOwnerState { /// /// The current state of the tree view. /// [Flags] public enum TestTreeViewState { None = 0, SourceCodeItemSelected = 1 } public TestTreeView() { ImageList = TestTreeViewImageList.ImageList; } /// /// Gets the current state of the test tree view. /// public Enum InternalState { get { TestTreeNode selectedNode = SelectedNode as TestTreeNode; if (selectedNode is TestClassTreeNode || selectedNode is TestMethodTreeNode) { return TestTreeViewState.SourceCodeItemSelected; } return TestTreeViewState.None; } } /// /// Adds the solution's projects to the test tree view. /// Only test projects are displayed. /// public void AddSolution(Solution solution) { Clear(); AddProjects(solution.Projects); } /// /// Adds the projects to the test tree view. Only those projects /// which are determined to have a reference to a supported /// test framework will be added to the tree. /// public void AddProjects(IEnumerable projects) { foreach (IProject project in projects) { AddProject(project); } } /// /// Adds the project to the test tree view if the project /// has a reference to a supported test framework. /// /// /// If the project is already in the tree then it will /// not be added again. /// public void AddProject(IProject project) { if (TestProject.IsTestProject(project)) { if (GetProjectTreeNode(project) == null) { // Add a new tree node. IProjectContent projectContent = GetProjectContent(project); if (projectContent != null) { TestProject testProject = new TestProject(project, projectContent); TestProjectTreeNode node = new TestProjectTreeNode(testProject); node.AddTo(this); // Sort the nodes. SortNodes(Nodes, true); } } } } /// /// Removes the specified project from the test tree view. /// public void RemoveProject(IProject project) { RemoveProjectNode(GetProjectTreeNode(project)); } /// /// Gets the projects displayed in the tree. /// public IProject[] GetProjects() { List projects = new List(); foreach (TestProjectTreeNode projectNode in Nodes) { projects.Add(projectNode.Project); } return projects.ToArray(); } /// /// Returns the TestProject associated with the specified project. /// public TestProject GetTestProject(IProject project) { TestProjectTreeNode node = GetProjectTreeNode(project); if (node != null) { return node.TestProject; } return null; } /// /// Gets the method of the currently selected tree node. /// public IMember SelectedMethod { get { TestMethodTreeNode methodNode = SelectedNode as TestMethodTreeNode; if (methodNode != null) { return methodNode.Method; } return null; } } /// /// Gets the class of the currently selected tree node. /// public IClass SelectedClass { get { TestClassTreeNode classNode = SelectedNode as TestClassTreeNode; if (classNode == null) { TestMethodTreeNode methodNode = SelectedNode as TestMethodTreeNode; if (methodNode != null) { classNode = methodNode.Parent as TestClassTreeNode; } } if (classNode != null) { return classNode.Class; } return null; } } /// /// Gets the project associated with the currently selected /// tree node. /// public IProject SelectedProject { get { TestProject testProject = SelectedTestProject; if (testProject != null) { return testProject.Project; } return null; } } /// /// If a namespace node is selected then the fully qualified namespace /// for this node is returned (i.e. includes the parent namespace prefixed /// to it). For all other nodes this returns null. /// public string SelectedNamespace { get { TestNamespaceTreeNode selectedNode = SelectedNode as TestNamespaceTreeNode; if (selectedNode != null) { return selectedNode.FullNamespace; } return null; } } /// /// Gets the selected test project. /// public TestProject SelectedTestProject { get { TestTreeNode selectedNode = SelectedNode as TestTreeNode; if (selectedNode != null) { return selectedNode.TestProject; } return null; } } /// /// Updates the classes and methods in the test tree view based on the /// parse information. /// public void UpdateParseInfo(ICompilationUnit oldUnit, ICompilationUnit newUnit) { foreach (TestProjectTreeNode projectNode in Nodes) { TestProject testProject = projectNode.TestProject; testProject.UpdateParseInfo(oldUnit, newUnit); } } /// /// Resets the test results for all the projects in the /// test tree view. /// public void ResetTestResults() { foreach (TestProjectTreeNode projectNode in Nodes) { TestProject testProject = projectNode.TestProject; testProject.ResetTestResults(); } } /// /// Returns the project content for the specified project. /// public virtual IProjectContent GetProjectContent(IProject project) { return ParserService.GetProjectContent(project); } /// /// Adds or removes a project from the test tree view based on /// whether a reference to a testing framework has been added or /// removed. /// public void ProjectReferencesChanged(IProject project) { TestProjectTreeNode projectNode = GetProjectTreeNode(project); if (TestProject.IsTestProject(project)) { if (projectNode == null) { TestProject testProject = new TestProject(project, GetProjectContent(project)); projectNode = new TestProjectTreeNode(testProject); projectNode.AddTo(this); } } else { RemoveProjectNode(projectNode); } } /// /// Returns the project tree node that is associated with the /// specified project. /// TestProjectTreeNode GetProjectTreeNode(IProject project) { foreach (TestProjectTreeNode projectNode in Nodes) { if (Object.ReferenceEquals(projectNode.Project, project)) { return projectNode; } } return null; } void RemoveProjectNode(TestProjectTreeNode projectNode) { if (projectNode != null) { projectNode.Remove(); } } } }