public void AddElement(CodeElement element)
        {
            if (testsTreeView.Nodes.Count == 0)
            {
                return;
            }

            if (element is VCCodeClass || element is VCCodeStruct)
            {
                TestSuiteCollector collector = new TestSuiteCollector();
                collector.Process(element);

                foreach (TestSuite suite in collector.Suites)
                {
                    AddTestSuiteToNode(suite, testsTreeView.Nodes[0], null);
                }

                testsTreeView.Sort();
            }
            else if (element is VCCodeFunction)
            {
                VCCodeFunction function = (VCCodeFunction)element;

                TreeNode suiteNode = FindNodeForTestSuite(function.Parent);

                TestCaseCollector collector = new TestCaseCollector();
                collector.Process((CodeElement)function);

                foreach (TestCase testCase in collector.TestCases)
                {
                    AddTestCaseToNode((TestSuite)suiteNode.Tag, testCase, suiteNode, null);
                }

                testsTreeView.Sort();
            }
        }
        public void ChangeElement(CodeElement element, vsCMChangeKind changeKind)
        {
            if (testsTreeView.Nodes.Count == 0)
            {
                return;
            }

            if (element is VCCodeClass || element is VCCodeStruct)
            {
                testsTreeView.BeginUpdate();

                VCCodeModel model = ((VCCodeElement)element).CodeModel;

                TestSuiteCollector collector = new TestSuiteCollector();
                collector.Process(model);
                List <TestSuite> suitesInModel = new List <TestSuite>(collector.Suites);

                // Iterate through the current suites in the view and pull out any
                // that are no longer in the model.

                TreeNode solutionNode = testsTreeView.Nodes[0];

                for (int i = 0; i < solutionNode.Nodes.Count;)
                {
                    TreeNode viewNode = solutionNode.Nodes[i];
                    int      index    = suitesInModel.FindIndex(
                        new Predicate <TestSuite>(delegate(TestSuite suite)
                    {
                        return(suite.Name == viewNode.Text);
                    }));

                    if (index == -1)
                    {
                        viewNode.Remove();
                    }
                    else
                    {
                        // The suites that are left over will be those in the
                        // model that aren't yet in the tree, so they need to
                        // be added.

                        suitesInModel.RemoveAt(index);
                        i++;
                    }
                }

                // Make sure a suite with the same name isn't already in the
                // view (this could happen if the name changes and then quickly
                // changes back before the event fires. If it's not there, add
                // it.

                foreach (TestSuite suite in suitesInModel)
                {
                    AddTestSuiteToNode(suite, solutionNode, null).Expand();
                }

                testsTreeView.Sort();
                testsTreeView.EndUpdate();
            }
            else if (element is VCCodeFunction)
            {
                testsTreeView.BeginUpdate();

                VCCodeElement parent = (VCCodeElement)((VCCodeElement)element).Parent;

                TestCaseCollector collector = new TestCaseCollector();
                collector.Process(parent.Children);
                List <TestCase> casesInModel = new List <TestCase>(collector.TestCases);

                // Iterate through the current test cases for this suite in the
                // view and pull out any that are no longer in the model.

                TreeNode suiteNode = FindNodeForTestSuite(parent);

                if (suiteNode == null)
                {
                    return;
                }

                for (int i = 0; i < suiteNode.Nodes.Count;)
                {
                    TreeNode viewNode = suiteNode.Nodes[i];
                    int      index    = casesInModel.FindIndex(
                        new Predicate <TestCase>(delegate(TestCase testCase)
                    {
                        return(testCase.Name == viewNode.Text);
                    }));

                    if (index == -1)
                    {
                        viewNode.Remove();
                    }
                    else
                    {
                        // The test cases that are left over will be those in
                        // the model that aren't yet in the tree, so they need
                        // to be added.

                        casesInModel.RemoveAt(index);
                        i++;
                    }
                }

                foreach (TestCase testCase in casesInModel)
                {
                    AddTestCaseToNode((TestSuite)suiteNode.Tag, testCase, suiteNode, null);
                }

                testsTreeView.Sort();
                testsTreeView.EndUpdate();
            }
        }