Example #1
0
        /// <summary>
        /// Checks active document whether it can be searched
        /// </summary>
        protected void CheckActiveDocument()
        {
            Document currentDocument = VisualLocalizerPackage.Instance.DTE.ActiveDocument;

            if (currentDocument == null)
            {
                throw new Exception("No selected document");
            }
            if (currentDocument.ProjectItem == null)
            {
                throw new Exception("Selected document has no corresponding Project Item.");
            }
            if (currentDocument.ProjectItem.ContainingProject == null)
            {
                throw new Exception("Selected document is not a part of any Project.");
            }
            if (RDTManager.IsFileReadonly(currentDocument.FullName) || VLDocumentViewsManager.IsFileLocked(currentDocument.FullName))
            {
                throw new Exception("Cannot perform this operation - active document is readonly");
            }
            if (VisualLocalizerPackage.Instance.DTE.Solution.FindProjectItem(currentDocument.FullName) == null)
            {
                throw new Exception("Selected document is not a part of an open Solution.");
            }
        }
        public void ProcessTest1()
        {
            Agent.EnsureSolutionOpen();

            DTE2             DTE    = Agent.GetDTE();
            BatchMoveCommand target = Agent.BatchMoveCommand;

            for (int i = 0; i < 3; i++)
            {
                var window = DTE.OpenFile(null, Agent.VBStringsTestFile1);
                window.Activate();

                target.Process(true);

                Assert.IsTrue(VLDocumentViewsManager.IsFileLocked(Agent.VBStringsTestFile1));

                ValidateResults(GetExpectedResultsFor(Agent.VBStringsTestFile1), target.Results);

                window.Detach();
                window.Close(EnvDTE.vsSaveChanges.vsSaveChangesNo);
                Assert.IsTrue(VLDocumentViewsManager.IsFileLocked(Agent.VBStringsTestFile1));

                VLDocumentViewsManager.ReleaseLocks();
                Assert.IsFalse(VLDocumentViewsManager.IsFileLocked(Agent.VBStringsTestFile1));
            }
        }
Example #3
0
        public void ProcessTest()
        {
            Agent.EnsureSolutionOpen();

            DTE2 DTE = Agent.GetDTE();
            BatchInlineCommand target = Agent.BatchInlineCommand;

            var window = DTE.OpenFile(null, Agent.VBReferencesTestFile1);

            window.Activate();

            target.Process(true);

            Assert.IsTrue(VLDocumentViewsManager.IsFileLocked(Agent.VBReferencesTestFile1));

            ValidateResults(GetExpectedResultsFor(Agent.VBReferencesTestFile1), target.Results);

            Assert.IsTrue(VLDocumentViewsManager.IsFileLocked(Agent.VBReferencesTestFile1));

            VLDocumentViewsManager.SetFileReadonly(Agent.VBReferencesTestFile1, false);
            Assert.IsFalse(VLDocumentViewsManager.IsFileLocked(Agent.VBReferencesTestFile1));

            window.Detach();
            window.Close(EnvDTE.vsSaveChanges.vsSaveChangesNo);
        }
Example #4
0
        /// <summary>
        /// Generic test for classic variant of batch commands
        /// </summary>
        /// <param name="target">Command to test</param>
        /// <param name="itemsToSelect">List of items that should be marked as selected in the Solution Explorer</param>
        /// <param name="expectedFiles">List of files that are expected to be searched</param>
        /// <param name="getExpected">Function that returns list of expected result items for specified file</param>
        protected void GenericTest(AbstractBatchCommand target, string[] itemsToSelect, string[] expectedFiles, Func <string, List <AbstractResultItem> > getExpected)
        {
            Agent.EnsureSolutionOpen();
            try {
                // select the items in Solution Explorer
                UIHierarchyItem[] selectedItems = new UIHierarchyItem[itemsToSelect.Length];
                for (int i = 0; i < itemsToSelect.Length; i++)
                {
                    selectedItems[i] = Agent.FindUIHierarchyItem(Agent.GetUIHierarchy().UIHierarchyItems, itemsToSelect[i]);
                    Assert.IsNotNull(selectedItems[i]);
                }

                // run the command on the selection
                target.Process(selectedItems, true);

                // test if all expected files were processed
                for (int i = 0; i < expectedFiles.Length; i++)
                {
                    Assert.IsTrue(VLDocumentViewsManager.IsFileLocked(expectedFiles[i]));
                }

                // create the list of expected results
                List <AbstractResultItem> list = new List <AbstractResultItem>();
                for (int i = 0; i < expectedFiles.Length; i++)
                {
                    list.AddRange(getExpected(expectedFiles[i]));
                }

                // compare the results
                if (target is BatchMoveCommand)
                {
                    ValidateResults(list, (target as BatchMoveCommand).Results);
                }
                else if (target is BatchInlineCommand)
                {
                    ValidateResults(list, (target as BatchInlineCommand).Results);
                }
                else
                {
                    Assert.Fail("Unkown parent command type");
                }
            } finally {
                VLDocumentViewsManager.ReleaseLocks();
                for (int i = 0; i < expectedFiles.Length; i++)
                {
                    Assert.IsFalse(VLDocumentViewsManager.IsFileLocked(expectedFiles[i]));
                }
            }
        }
        /// <summary>
        /// If the given item is ResX file, adds it to the list of ResX files
        /// </summary>
        private void SearchForResxFiles(ProjectItem item, List <GlobalTranslateProjectItem> resxFiles)
        {
            if (searchedProjectItems.Contains(item))
            {
                return;
            }
            SearchForResxFiles(item.ProjectItems, resxFiles);

            if (item.IsItemResX())
            {
                GlobalTranslateProjectItem r = new GlobalTranslateProjectItem(item);
                r.Checked  = false;
                r.Readonly = VLDocumentViewsManager.IsFileLocked(item.GetFullPath()) || RDTManager.IsFileReadonly(item.GetFullPath());

                resxFiles.Add(r);
            }
        }
        public void ProcessEmptySelectionTest()
        {
            Agent.EnsureSolutionOpen();

            BatchMoveCommand_Accessor target = Agent.BatchMoveCommand_Accessor;

            Window      window = Agent.GetDTE().OpenFile(null, Agent.VBStringsTestFile1);
            IVsTextView view   = VLDocumentViewsManager.GetTextViewForFile(Agent.VBStringsTestFile1, false, true);

            view.SetSelection(40, 17, 44, 26);

            List <CodeStringResultItem> emptyList = new List <CodeStringResultItem>();

            target.ProcessSelection(true);
            ValidateResults(emptyList, target.Results);
            Assert.IsFalse(VLDocumentViewsManager.IsFileLocked(Agent.VBStringsTestFile1));

            window.Detach();
            window.Close(vsSaveChanges.vsSaveChangesNo);
        }
Example #7
0
        public void ProcessEmptySelectionTest()
        {
            Agent.EnsureSolutionOpen();

            DTE2 DTE = Agent.GetDTE();
            BatchInlineCommand_Accessor target = Agent.BatchInlineCommand_Accessor;

            Window      window = DTE.OpenFile(null, Agent.VBReferencesTestFile1);
            IVsTextView view   = VLDocumentViewsManager.GetTextViewForFile(Agent.VBReferencesTestFile1, false, true);

            view.SetSelection(28, 4, 35, 31);

            List <AbstractResultItem> emptyList = new List <AbstractResultItem>();

            target.ProcessSelection(true);
            ValidateResults(emptyList, target.Results);
            Assert.IsFalse(VLDocumentViewsManager.IsFileLocked(Agent.VBReferencesTestFile1));

            window.Detach();
            window.Close(vsSaveChanges.vsSaveChangesNo);
        }
Example #8
0
        /// <summary>
        /// Search given ProjectItem, using predicate to determine whether a code element should be explored (used when processing selection)
        /// </summary>
        /// <param name="projectItem">Item to search</param>
        /// <param name="exploreable">Predicate returning true, if given code element should be searched for result items</param>
        /// <param name="verbose"></param>
        protected virtual void Process(ProjectItem projectItem, Predicate <CodeElement> exploreable, bool verbose)
        {
            if (searchedProjectItems.Contains(projectItem))
            {
                return;
            }
            searchedProjectItems.Add(projectItem);

            invisibleWindowsAuthor = GetType();

            if (VLDocumentViewsManager.IsFileLocked(projectItem.GetFullPath()) || RDTManager.IsFileReadonly(projectItem.GetFullPath()))
            {
                if (verbose)
                {
                    VLOutputWindow.VisualLocalizerPane.WriteLine("\tSkipping {0} - document is readonly", projectItem.Name);
                }
            }
            else
            {
                try {
                    switch (projectItem.GetFileType())
                    {
                    case FILETYPE.CSHARP: ProcessCSharp(projectItem, exploreable, verbose); break;

                    case FILETYPE.ASPX: ProcessAspNet(projectItem, verbose); break;

                    case FILETYPE.VB: ProcessVB(projectItem, exploreable, verbose); break;

                    default: break;     // do nothing if file type is not known
                    }
                } catch (Exception ex) {
                    if (verbose)
                    {
                        VLOutputWindow.VisualLocalizerPane.WriteLine("\tException occured while processing " + projectItem.Name);
                        VLOutputWindow.VisualLocalizerPane.WriteException(ex);
                    }
                }
            }
        }
Example #9
0
        /// <summary>
        /// Generic test for the "(selection)" commands
        /// </summary>
        /// <param name="target">Command to process</param>
        /// <param name="file">File path</param>
        /// <param name="view"></param>
        /// <param name="lines"></param>
        /// <param name="getExpected">Function that returns list of expected results for specified file path</param>
        protected void GenericSelectionTest(AbstractBatchCommand_Accessor target, string file, IVsTextView view, IVsTextLines lines, Func <string, List <AbstractResultItem> > getExpected)
        {
            Agent.EnsureSolutionOpen();

            int lineCount;

            lines.GetLineCount(out lineCount);
            Random rnd = new Random();

            for (int i = 0; i < 20; i++)
            {
                // initialize selection range
                int beginLine = rnd.Next(lineCount);
                int endLine   = beginLine + rnd.Next(Math.Min(lineCount, beginLine + i) - beginLine);

                int beginLineLength, endLineLength;
                lines.GetLengthOfLine(beginLine, out beginLineLength);
                lines.GetLengthOfLine(endLine, out endLineLength);
                int beginColumn = rnd.Next(beginLineLength);
                int endColumn   = beginLine == endLine ? beginColumn + (rnd.Next(Math.Min(endLineLength, beginColumn + i) - beginColumn)) : rnd.Next(endLineLength);
                if (beginLine == endLine && beginColumn == endColumn)
                {
                    endColumn++;
                }

                // set the selection
                view.SetSelection(beginLine, beginColumn, endLine, endColumn);
                target.InitializeSelection();

                // obtain the list of expected results
                List <AbstractResultItem> expectedList = new List <AbstractResultItem>();
                foreach (AbstractResultItem expected in getExpected(file))
                {
                    if (!target.IsItemOutsideSelection(expected))
                    {
                        expectedList.Add(expected);
                    }
                }

                // run the command
                target.ProcessSelection(true);

                // compare the results
                if (target is BatchMoveCommand_Accessor)
                {
                    ValidateResults(expectedList, (target as BatchMoveCommand_Accessor).Results);
                }
                else if (target is BatchInlineCommand_Accessor)
                {
                    ValidateResults(expectedList, (target as BatchInlineCommand_Accessor).Results);
                }
                else
                {
                    Assert.Fail("Unkown parent command type");
                }

                Assert.IsTrue(expectedList.Count == 0 || VLDocumentViewsManager.IsFileLocked(file));

                VLDocumentViewsManager.ReleaseLocks();
                Assert.IsFalse(VLDocumentViewsManager.IsFileLocked(file));
            }

            // close the window
            Window win = VsShellUtilities.GetWindowObject(VLDocumentViewsManager.GetWindowFrameForFile(file, false));

            win.Detach();
            win.Close(vsSaveChanges.vsSaveChangesNo);
        }