コード例 #1
0
        public void TestAutomationOnProjectItem()
        {
            UIThreadInvoker.Invoke((ThreadInvoker) delegate()
            {
                //Get the global service provider and the dte
                IServiceProvider sp = VsIdeTestHostContext.ServiceProvider;
                DTE dte             = (DTE)sp.GetService(typeof(DTE));

                string destination = Path.Combine(TestContext.TestDir, TestContext.TestName);
                Utilities.CreateMyNestedProject(sp, dte, TestContext.TestName, destination, true);

                OAProject automation = Utilities.FindExtObject(sp, Utilities.NestedProjectGuid, TestContext.TestName) as OAProject;
                Assert.IsNotNull(automation, "Failed to create a project using automation");

                ProjectNode project = automation.Project;

                // Get the AssemblyInfo.cs, try to open it and then ask using automation that it is opened.
                EnvDTE.ProjectItem item = automation.ProjectItems.Item("AssemblyInfo.cs");
                Assert.IsNotNull(item, "Could not retrieve AssemblyInfo.cs");

                EnvDTE.Window window = item.Open(VSConstants.LOGVIEWID_Primary.ToString());
                Assert.IsNotNull(window, "Could not open the AssemblyInfo.cs");
                window.Activate();

                bool isOpen = item.get_IsOpen(VSConstants.LOGVIEWID_Primary.ToString());
                Assert.IsTrue(isOpen, "The AssemblyInfo.cs file should have been opened");

                // Now save it
                item.Save("");

                Assert.IsTrue(item.Saved, "The renamed AssemblyInfo.cs has not been saved");

                // Get the Document
                EnvDTE.Document document = item.Document;
                Assert.IsNotNull(document, "Could not retrieve the document object");
                Assert.IsTrue(document.Name == "AssemblyInfo.cs", "The document for the file item is incorrect. It's name should be AssemblyInfo.cs");

                // Try the properties on a nested item
                EnvDTE.ProjectItem nestedProject     = automation.ProjectItems.Item("ANestedProject");
                EnvDTE.ProjectItem nestedProjectItem = nestedProject.ProjectItems.Item("Program.cs");
                EnvDTE.Properties nesteditemsProps   = nestedProjectItem.Properties;
                EnvDTE.Property nestedItemProperty   = nesteditemsProps.Item("BuildAction");
                Assert.IsNotNull(nestedItemProperty, "Could not retrieve the BuildAction property from the nested project item");
                nestedItemProperty.Value = ProjectFileConstants.Content;
                Assert.AreEqual(nestedItemProperty.Value, ProjectFileConstants.Content);

                // Now try the properties on the top project item
                EnvDTE.Properties props = item.Properties;
                Assert.IsNotNull(props, "Could not retrieve the BuildAction property from the nested project item");

                EnvDTE.Property itemProperty = props.Item("BuildAction");
                Assert.IsNotNull(itemProperty, "Could not retrieve the BuildAction property from the nested project item");
                Assert.IsFalse(itemProperty is OANullProperty, "Could not retrieve the BuildAction property from the nested project item");
                itemProperty.Value = ProjectFileConstants.Content;
                Assert.AreEqual(itemProperty.Value, ProjectFileConstants.Content);

                // Now save as
                Assert.IsTrue(item.SaveAs("AssemblyInfo1.cs"), "The file AssemblyInfo.cs could not be reanmed to AssemblyInfo1.cs");
                Assert.IsTrue(item.Name == "AssemblyInfo1.cs", "File item has been renamed to AssemblyInfo1.cs but the Name property has not");

                // Now try the Program.cs. That should not be opened
                EnvDTE.ProjectItem item1 = automation.ProjectItems.Item("Program.cs");

                Assert.IsNotNull(item1, "Could not retrieve AssemblyInfo.cs");

                isOpen = item1.get_IsOpen(VSConstants.LOGVIEWID_Primary.ToString());

                Assert.IsFalse(isOpen, "The Program.cs should not have been opened");

                // Now get the Reference folder as a project item and expand it.
                EnvDTE.ProjectItem references = automation.ProjectItems.Item("References");
                references.ExpandView();

                // Check that actually it was expanded.
                IVsUIHierarchyWindow uiHierarchy     = VsShellUtilities.GetUIHierarchyWindow(project.Site, HierarchyNode.SolutionExplorer);
                ReferenceContainerNode containerNode = (ReferenceContainerNode)project.FindChild("References");

                __VSHIERARCHYITEMSTATE state;
                uint stateAsInt;
                uiHierarchy.GetItemState(project, (uint)containerNode.Id, (uint)__VSHIERARCHYITEMSTATE.HIS_Expanded, out stateAsInt);
                state = (__VSHIERARCHYITEMSTATE)stateAsInt;
                Assert.IsTrue(state == __VSHIERARCHYITEMSTATE.HIS_Expanded, "The References folder has not been expanded");
            });
        }
コード例 #2
0
        public void TestInterfaceMethodsOnProperty()
        {
            UIThreadInvoker.Invoke((ThreadInvoker) delegate()
            {
                //Get the global service provider and the dte
                IServiceProvider sp = VsIdeTestHostContext.ServiceProvider;
                DTE dte             = (DTE)sp.GetService(typeof(DTE));

                string destination = Path.Combine(TestContext.TestDir, TestContext.TestName);
                Utilities.CreateMyNestedProject(sp, dte, TestContext.TestName, destination, true);

                OAProject automation = Utilities.FindExtObject(sp, Utilities.NestedProjectGuid, TestContext.TestName) as OAProject;
                Assert.IsNotNull(automation, "Failed to create a project using automation");

                ProjectNode projectNode = automation.Project;

                // Get Project Property object
                EnvDTE.Property property = automation.Properties.Item("RootNamespace");
                Assert.IsNotNull(property, "Could not retrieve valid RootNamespace property");
                Assert.IsFalse(property is OANullProperty, "Could not retrieve valid RootNamespace property");
                object retValue = property.Application;
                Assert.IsNull(retValue);

                Assert.IsTrue((string)property.Value == "Application", "Failed to retrieve the Value property.");
                property.Value = "Test1";
                Assert.AreEqual(property.Value, "Test1");


                // Get Collection object from property object
                EnvDTE.Properties properties = property.Collection;
                Assert.IsNotNull(properties, "Collection property failed to retrieve an object");

                // Get the DTE
                retValue = property.DTE;
                Assert.IsNotNull(retValue);

                // Get the Indexed value
                retValue = property.get_IndexedValue(1, 2, 3, 4);
                Assert.IsNull(retValue);

                property.let_Value(1);
                Assert.AreEqual(property.Value, "1");

                // Check the name.
                string name = property.Name;
                Assert.IsNotNull(name);
                Assert.IsTrue(name == "RootNamespace", "RootNamespace property was not set correctly");

                short numIndeces = property.NumIndices;
                //Currently it gives back 0
                //It must be Assertd when the method changes
                Assert.IsTrue(numIndeces == 0);

                // Assert the Object property
                retValue = property.Object;
                Assert.AreEqual(retValue, property.Value);
                property.Object = "test1";
                retValue        = property.Object;
                Assert.AreEqual(retValue, "test1");

                // Test the parent property
                EnvDTE.Properties parent = property.Parent;
                Assert.IsTrue(parent is OAProperties, "Parent property failed to return the parent of a property");

                //It does nothing currently. Cannot be Assertd.
                property.set_IndexedValue(1, 2, 3, 4, 5);

                // Try a non string value on the Value.
                ArrayList list = new ArrayList();
                property.Value = list;
                retValue       = property.Value;
                Assert.AreEqual(retValue, list.ToString());

                // Test the iterators for enumeration.
                // We are interested to see that we advance with the iteration.
                bool[] found = new bool[2];

                foreach (EnvDTE.Property aProperty in automation.Properties)
                {
                    if (aProperty.Name == "RootNamespace")
                    {
                        found[0] = true;
                    }
                    else if (aProperty.Name == "AssemblyName")
                    {
                        found[1] = true;
                    }
                }

                foreach (bool foundValue in found)
                {
                    Assert.IsTrue(foundValue, "The iterator on property collection has not been implemented correctly");
                }
            });
        }
コード例 #3
0
        private void BadFileNameChecks(EnvDTE.Project automation, bool forSave)
        {
            string errorMessage = Utilities.GetResourceStringFromTheProjectAssembly("ErrorInvalidFileName");

            foreach (string newBadFileName in Utilities.BadFileNames)
            {
                Assert.IsTrue(this.CheckForSaveWithBadFileName <InvalidOperationException>(automation, forSave, newBadFileName, errorMessage), "The file named " + newBadFileName + " could be saved");
            }
            string badFileName = "....nestedProj";

            Assert.IsTrue(this.CheckForSaveWithBadFileName <InvalidOperationException>(automation, forSave, badFileName, errorMessage), "The file named " + badFileName + " could be saved");

            badFileName = "  ";
            Assert.IsTrue(this.CheckForSaveWithBadFileName <InvalidOperationException>(automation, forSave, badFileName, errorMessage), "The file named " + badFileName + " could be saved");

            badFileName = ".....";
            Assert.IsTrue(this.CheckForSaveWithBadFileName <InvalidOperationException>(automation, forSave, badFileName, errorMessage), "The file named " + badFileName + " could be saved");

            errorMessage = String.Format(System.Globalization.CultureInfo.CurrentCulture, Utilities.GetResourceStringFromTheProjectAssembly("PathTooLong"), Path.Combine(Path.GetDirectoryName(automation.FullName), Utilities.LongFileName));
            Assert.IsTrue(this.CheckForSaveWithBadFileName <InvalidOperationException>(automation, forSave, Utilities.LongFileName, errorMessage), "The file named " + Utilities.LongFileName + " could be saved");
        }
コード例 #4
0
        public void TestAutomationOnProjectItems()
        {
            UIThreadInvoker.Invoke((ThreadInvoker) delegate()
            {
                //Get the global service provider and the dte
                IServiceProvider sp = VsIdeTestHostContext.ServiceProvider;
                DTE dte             = (DTE)sp.GetService(typeof(DTE));

                string destination = Path.Combine(TestContext.TestDir, TestContext.TestName);
                Utilities.CreateMyNestedProject(sp, dte, TestContext.TestName, destination, true);

                OAProject automation = Utilities.FindExtObject(sp, Utilities.NestedProjectGuid, TestContext.TestName) as OAProject;
                Assert.IsNotNull(automation, "Failed to create a project using automation");

                string newModelFilePath = TestUtils.GetNewFileName(TestContext.TestDir, TestContext.TestName, "model");
                EnvDTE.ProjectItem pi   = automation.ProjectItems.AddFromFileCopy(Path.GetFullPath(newModelFilePath));
                Assert.IsNotNull(pi, "Failed to create a modeling project item through automation");
                Assert.IsTrue(pi.Name == Path.GetFileName(newModelFilePath), "ProjectItems AddFromFileCopy has not returned the item that was added");

                bool[] found = new bool[3];
                // Test enumerators on project items
                foreach (EnvDTE.ProjectItem item in automation.ProjectItems)
                {
                    if (item.Name == Path.GetFileName(newModelFilePath))
                    {
                        found[0] = true;
                    }
                    else if (item.Name == "Program.cs")
                    {
                        found[1] = true;
                    }
                    else if (item.Name == "AssemblyInfo.cs")
                    {
                        found[2] = true;
                    }
                }

                foreach (bool foundValue in found)
                {
                    Assert.IsTrue(foundValue, "The iterator on item collection has not been implemented correctly");
                }

                /*****Test the AddFolder method*****/
                //Add a simple folder to the project
                ProjectItem folder = automation.ProjectItems.AddFolder("directory", null);
                //Add a subfolder to the folder
                ProjectItem subfolder = folder.ProjectItems.AddFolder("subdirectory", string.Empty);
                //Add another subfolder to that folder
                subfolder.ProjectItems.AddFolder("subsubdirectory", EnvDTE.Constants.vsProjectItemKindPhysicalFolder);

                //Verify that we have the following structure:
                //Project
                //	-directory
                //		-subdirectory
                //			-subsubdirectory
                ProjectItem directory       = automation.ProjectItems.Item("directory") as ProjectItem;
                ProjectItem subdirectory    = directory.ProjectItems.Item("subdirectory") as ProjectItem;
                ProjectItem subsubdirectory = subdirectory.ProjectItems.Item("subsubdirectory") as ProjectItem;
                Assert.IsNotNull(directory);
                Assert.IsNotNull(subdirectory);
                Assert.IsNotNull(subsubdirectory);


                bool argumentExceptionThrown = false;
                try
                {
                    //We expect virtual folders to fail this way.
                    folder.ProjectItems.AddFolder("virtualfolder", EnvDTE.Constants.vsProjectItemKindVirtualFolder);
                }
                catch (ArgumentException) { argumentExceptionThrown = true; }
                Assert.IsTrue(argumentExceptionThrown);

                argumentExceptionThrown = false;
                try
                {
                    //Verify that you can't add a folder where a node with that name already exists
                    folder.ProjectItems.AddFolder("subdirectory", string.Empty);
                }
                catch (ArgumentException) { argumentExceptionThrown = true; }
                Assert.IsTrue(argumentExceptionThrown);
            });
        }