/// <summary>
		/// Creates a new TestNamespaceTreeNode
		/// </summary>
		/// <remarks>
		/// Note that the Namespace child nodes are added in
		/// the constructor not whilst the node is expanding
		/// via the Initialize method. This is so the icon for the
		/// node can be updated even if the parent node is not 
		/// expanded. The alternative is to have each namespace node,
		/// even if it does not have any class child nodes, to
		/// store all the classes that are below it in the tree and
		/// update the icon based on their results. The assumption
		/// is that there are fewer namespace nodes than classes so
		/// adding the namespace nodes here does not matter. 
		/// </remarks>
		/// <param name="namespacePrefix">The first part of the
		/// namespace (including any dot characters) before this 
		/// particular namespace.</param>
		/// <param name="name">The name of the namespace without any
		/// dot characters (e.g. the name at this particular 
		/// location in the tree).</param>
		public TestNamespaceTreeNode(TestProject testProject, string namespacePrefix, string name) 
			: base(testProject, name)
		{
			ns = name;
			this.namespacePrefix = namespacePrefix;
			fullNamespace = GetFullNamespace(namespacePrefix, ns);
			GetTestClasses();
			testProject.TestClasses.TestClassAdded += TestClassAdded;
			testProject.TestClasses.TestClassRemoved += TestClassRemoved;
			
			// Add namespace nodes - do not add them on node expansion.
			foreach (string namespaceName in TestProject.GetChildNamespaces(fullNamespace)) {
				TestNamespaceTreeNode node = new TestNamespaceTreeNode(TestProject, fullNamespace, namespaceName);
				node.AddTo(this);
				namespaceChildNodes.Add(node);
				node.ImageIndexChanged += TestNamespaceNodeImageIndexChanged;
			}
			
			// Add a dummy node if there are no namespaces since
			// there might be class nodes which will be added 
			// lazily when the node is expanded.
			if (namespaceChildNodes.Count == 0) {
				dummyNode = new ExtTreeNode();
				Nodes.Add(dummyNode);
			}
			
			UpdateImageListIndex();
		}
        /// <summary>
        /// Creates a new TestNamespaceTreeNode
        /// </summary>
        /// <remarks>
        /// Note that the Namespace child nodes are added in
        /// the constructor not whilst the node is expanding
        /// via the Initialize method. This is so the icon for the
        /// node can be updated even if the parent node is not
        /// expanded. The alternative is to have each namespace node,
        /// even if it does not have any class child nodes, to
        /// store all the classes that are below it in the tree and
        /// update the icon based on their results. The assumption
        /// is that there are fewer namespace nodes than classes so
        /// adding the namespace nodes here does not matter.
        /// </remarks>
        /// <param name="namespacePrefix">The first part of the
        /// namespace (including any dot characters) before this
        /// particular namespace.</param>
        /// <param name="name">The name of the namespace without any
        /// dot characters (e.g. the name at this particular
        /// location in the tree).</param>
        public TestNamespaceTreeNode(TestProject testProject, string namespacePrefix, string name)
            : base(testProject, name)
        {
            ns = name;
            this.namespacePrefix = namespacePrefix;
            fullNamespace        = GetFullNamespace(namespacePrefix, ns);
            GetTestClasses();
            testProject.TestClasses.TestClassAdded   += TestClassAdded;
            testProject.TestClasses.TestClassRemoved += TestClassRemoved;

            // Add namespace nodes - do not add them on node expansion.
            foreach (string namespaceName in TestProject.GetChildNamespaces(fullNamespace))
            {
                TestNamespaceTreeNode node = new TestNamespaceTreeNode(TestProject, fullNamespace, namespaceName);
                node.AddTo(this);
                namespaceChildNodes.Add(node);
                node.ImageIndexChanged += TestNamespaceNodeImageIndexChanged;
            }

            // Add a dummy node if there are no namespaces since
            // there might be class nodes which will be added
            // lazily when the node is expanded.
            if (namespaceChildNodes.Count == 0)
            {
                dummyNode = new ExtTreeNode();
                Nodes.Add(dummyNode);
            }

            UpdateImageListIndex();
        }
        /// <summary>
        /// A new test class has been added to the project so a new
        /// tree node is added if the class belongs to this namespace.
        /// </summary>
        void TestClassAdded(object source, TestClassEventArgs e)
        {
            if (e.TestClass.Namespace == fullNamespace)
            {
                // Add test class to our monitored test classes.
                testClasses.Add(e.TestClass);

                // Add a new tree node.
                TestClassTreeNode classNode = new TestClassTreeNode(TestProject, e.TestClass);
                classNode.AddTo(this);

                // Sort the nodes.
                SortChildNodes();
            }
            else if (isInitialized && NamespaceStartsWith(e.TestClass.Namespace))
            {
                // Check if there is a child namespace node for the class.
                string childNamespace = TestClass.GetChildNamespace(e.TestClass.Namespace, fullNamespace);
                if (!NamespaceNodeExists(childNamespace))
                {
                    // Add a new namespace node.
                    TestNamespaceTreeNode node = new TestNamespaceTreeNode(TestProject, fullNamespace, childNamespace);
                    node.AddTo(this);

                    // Sort the nodes.
                    SortChildNodes();
                }
            }
        }
Beispiel #4
0
 /// <summary>
 /// Checks all the child nodes of this node to see
 /// if the root namespace (i.e. the first part of the
 /// namespace) exists in the tree.
 /// </summary>
 /// <param name="rootNamespace">The first dotted part
 /// of the namespace.</param>
 protected bool NamespaceNodeExists(string rootNamespace)
 {
     foreach (ExtTreeNode node in Nodes)
     {
         TestNamespaceTreeNode namespaceNode = node as TestNamespaceTreeNode;
         if (namespaceNode != null && namespaceNode.Text == rootNamespace)
         {
             return(true);
         }
     }
     return(false);
 }
 /// <summary>
 /// Removes this node if it has no child nodes. This
 /// method also calls the same method on the parent
 /// namespace node so it can check whether it should
 /// remove itself.
 /// </summary>
 void RemoveIfEmpty()
 {
     if (IsEmpty)
     {
         Remove();
         Dispose();
         TestNamespaceTreeNode parentNode = Parent as TestNamespaceTreeNode;
         if (parentNode != null)
         {
             parentNode.RemoveIfEmpty();
         }
     }
 }
		/// <summary>
		/// Adds a new class node to this project node if the
		/// class added has no root namespace.
		/// </summary>
		void TestClassAdded(object source, TestClassEventArgs e)
		{
			if (e.TestClass.Namespace == String.Empty) {
				AddClassNode(e.TestClass);
				SortChildNodes();
			} else if (isInitialized) {
				// Check that we have a namespace node for this class.
				if (!NamespaceNodeExists(e.TestClass.RootNamespace)) {
					// Add a new namespace node.
					TestNamespaceTreeNode node = new TestNamespaceTreeNode(TestProject, e.TestClass.RootNamespace);
					node.AddTo(this);
					SortChildNodes();
				}
			}
		}
		/// <summary>
		/// Adds a new class node to this project node if the
		/// class added has no root namespace.
		/// </summary>
		void TestClassAdded(object source, TestClassEventArgs e)
		{
			if (e.TestClass.Namespace == String.Empty) {
				AddClassNode(e.TestClass);
				SortChildNodes();
			} else if (isInitialized) {
				// Check that we have a namespace node for this class.
				if (!NamespaceNodeExists(e.TestClass.RootNamespace)) {
					// Add a new namespace node.
					TestNamespaceTreeNode node = new TestNamespaceTreeNode(TestProject, e.TestClass.RootNamespace);
					node.AddTo(this);
					SortChildNodes();
				}
			}
		}
		/// <summary>
		/// Adds the child nodes after this node has been expanded.
		/// </summary>
		protected override void Initialize()
		{
			Nodes.Clear();
			
			// Add namespace nodes.
			foreach (string rootNamespace in TestProject.RootNamespaces) {
				TestNamespaceTreeNode node = new TestNamespaceTreeNode(TestProject, rootNamespace);
				node.AddTo(this);
			}
			
			// Add class nodes.
			foreach (TestClass c in TestProject.GetTestClasses(String.Empty)) {
				AddClassNode(c);
			}
			
			// Sort the nodes.
			SortChildNodes();
		}
		/// <summary>
		/// Adds the child nodes after this node has been expanded.
		/// </summary>
		protected override void Initialize()
		{
			Nodes.Clear();
			
			// Add namespace nodes.
			foreach (string rootNamespace in TestProject.RootNamespaces) {
				TestNamespaceTreeNode node = new TestNamespaceTreeNode(TestProject, rootNamespace);
				node.AddTo(this);
			}
			
			// Add class nodes.
			foreach (TestClass c in TestProject.GetTestClasses(String.Empty)) {
				AddClassNode(c);
			}
			
			// Sort the nodes.
			SortChildNodes();
		}
		public void Init()
		{
			// Create a project to display in the test tree view.
			MockCSharpProject project = new MockCSharpProject();
			project.Name = "TestProject";
			ReferenceProjectItem nunitFrameworkReferenceItem = new ReferenceProjectItem(project);
			nunitFrameworkReferenceItem.Include = "NUnit.Framework";
			ProjectService.AddProjectItem(project, nunitFrameworkReferenceItem);
			List<IProject> projects = new List<IProject>();
			projects.Add(project);
					
			// Add a test class with a TestFixture attributes.
			projectContent = new MockProjectContent();
			projectContent.Language = LanguageProperties.None;
			TestClass testClass = CreateTestClass("MyTests.MyTestFixture");
			projectContent.Classes.Add(testClass.Class);
			
			// Add two methods to the test class only
			// one of which has test attributes.
			MockMethod testMethod = new MockMethod("NameExists");
			testMethod.Attributes.Add(new MockAttribute("Test"));
			testMethod.DeclaringType = testClass.Class;
			testClass.Class.Methods.Add(testMethod);
					
			// Init mock project content to be returned.
			treeView = new DummyParserServiceTestTreeView();
			treeView.ProjectContentForProject = projectContent;
			
			// Load the projects into the test tree view.
			treeView.AddProjects(projects);
			projectNode = (TestProjectTreeNode)treeView.Nodes[0];
			testProject = projectNode.TestProject;
			
			// Initialise the root node so the child nodes are created.
			projectNode.PerformInitialization();
			myTestsNamespaceNode = (TestNamespaceTreeNode)projectNode.FirstNode;
			
			// Initialise the first namespace node.
			myTestsNamespaceNode.PerformInitialization();
			testFixtureNode = (TestClassTreeNode)myTestsNamespaceNode.FirstNode;
		
			// Initialise the test method tree nodes.
			testFixtureNode.PerformInitialization();
		}
		/// <summary>
		/// A new test class has been added to the project so a new 
		/// tree node is added if the class belongs to this namespace.
		/// </summary>
		void TestClassAdded(object source, TestClassEventArgs e)
		{
			if (e.TestClass.Namespace == fullNamespace) {
				// Add test class to our monitored test classes.
				testClasses.Add(e.TestClass);
				
				// Add a new tree node.
				TestClassTreeNode classNode = new TestClassTreeNode(TestProject, e.TestClass);
				classNode.AddTo(this);
				
				// Sort the nodes.
				SortChildNodes();
			} else if (isInitialized && NamespaceStartsWith(e.TestClass.Namespace)) {
				// Check if there is a child namespace node for the class.
				string childNamespace = TestClass.GetChildNamespace(e.TestClass.Namespace, fullNamespace);
				if (!NamespaceNodeExists(childNamespace)) {
					// Add a new namespace node.
					TestNamespaceTreeNode node = new TestNamespaceTreeNode(TestProject, fullNamespace, childNamespace);
					node.AddTo(this);
					
					// Sort the nodes.
					SortChildNodes();
				}
			}
		}