/// <summary> /// Set opened-state of the specified files /// </summary> /// <param name="testFiles"></param> /// <param name="shouldBeOpened">True if files should be opened, false otherwise</param> protected void SetFilesOpened(string[] testFiles, bool shouldBeOpened) { foreach (string sourcePath in testFiles) { if (!shouldBeOpened && RDTManager.IsFileOpen(sourcePath)) { var win = VsShellUtilities.GetWindowObject(VLDocumentViewsManager.GetWindowFrameForFile(sourcePath, false)); // close the window win.Detach(); win.Close(vsSaveChanges.vsSaveChangesNo); } if (shouldBeOpened) { Window win = null; // open the file and activate the window if (!RDTManager.IsFileOpen(sourcePath)) { win = Agent.GetDTE().OpenFile(null, sourcePath); } else { win = VsShellUtilities.GetWindowObject(VLDocumentViewsManager.GetWindowFrameForFile(sourcePath, true)); } Assert.IsNotNull(win, "Window cannot be opened " + sourcePath); win.Visible = true; win.Activate(); } } }
/// <summary> /// Generic test method /// </summary> /// <param name="openFiles">True if the files should be opened first</param> /// <param name="fullName">True if the "Use full names" policy should be applied</param> /// <param name="mark">True if the "Mark with VL_NO_LOC policy should be applied"</param> /// <param name="testFiles">Files to test</param> /// <param name="targetFiles">ResX files where resources can be moved</param> private void InternalFileMoveTest(bool openFiles, bool fullName, bool mark, string[] testFiles, string[] targetFiles) { // backup the files Dictionary <string, string> backups = CreateBackupsOf(testFiles); // run the "batch move" command to get result items List <CodeStringResultItem> items = BatchMoveLookup(testFiles); // open the necessary files SetFilesOpened(testFiles, openFiles); // initialize ResX destination files Dictionary <string, ResXProjectItem> resxItems = InitResxItems(targetFiles, Agent.GetDTE().Solution.FindProjectItem(testFiles[0]).ContainingProject); // initialize the "batch move" tool window and grid Dictionary <ResXProjectItem, int> resxCounts; Dictionary <ProjectItem, int> sourceItemCounts; int expectedToBeMarked; BatchMoveToResourcesToolWindow_Accessor window = InitBatchToolWindow(resxItems.Values.ToList(), items, fullName, mark, out resxCounts, out sourceItemCounts, out expectedToBeMarked); try { // execute window.RunClick(null, null); VLDocumentViewsManager.CloseInvisibleWindows(typeof(BatchMoveCommand), false); // verify every ResX file contains the correct number of resources foreach (ResXProjectItem target in resxItems.Values) { Assert.AreEqual(resxCounts.ContainsKey(target) ? resxCounts[target] : 0, GetResourcesCountIn(target)); } // run the "inline" command to verify all references are valid int checkedCount = resxCounts.Sum((pair) => { return(pair.Value); }); List <CodeReferenceResultItem> inlineResults = BatchInlineLookup(testFiles); VLDocumentViewsManager.CloseInvisibleWindows(typeof(BatchInlineCommand), false); Assert.AreEqual(checkedCount, inlineResults.Count); // test if every unchecked string result item was marked if (mark) { int markedAfter = BatchMoveLookup(testFiles).Count((item) => { return(item.IsMarkedWithUnlocalizableComment); }); VLDocumentViewsManager.CloseInvisibleWindows(typeof(BatchMoveCommand), false); Assert.AreEqual(expectedToBeMarked, markedAfter, "mark"); } // test if qualified name was really used if (fullName) { foreach (var result in inlineResults) { AspNetCodeReferenceResultItem citem = result as AspNetCodeReferenceResultItem; if (citem == null || !citem.ComesFromWebSiteResourceReference) { Assert.AreEqual(result.OriginalReferenceText, result.FullReferenceText); } } } // check if no import statement has been added twice foreach (string path in testFiles) { CheckForDuplicateUsings(path); } if (openFiles) { // use undo manager to revert the changes foreach (string file in testFiles) { var win = VsShellUtilities.GetWindowObject(VLDocumentViewsManager.GetWindowFrameForFile(file, false)); ProjectItem pitem = Agent.GetDTE().Solution.FindProjectItem(file); int count = sourceItemCounts.ContainsKey(pitem) ? sourceItemCounts[pitem] : 0; IOleUndoManager manager; VLDocumentViewsManager.GetTextLinesForFile(file, false).GetUndoManager(out manager); List <IOleUndoUnit> list = manager.RemoveTopFromUndoStack(count); foreach (AbstractUndoUnit unit in list) { unit.Undo(); } Assert.AreEqual(File.ReadAllText(backups[file]), File.ReadAllText(file)); } // check that all changes were fully reverted int sum = resxItems.Values.Sum((item) => { return(GetResourcesCountIn(item)); }); Assert.AreEqual(0, sum); } } finally { // close the files SetFilesOpened(testFiles, false); // restore the backups RestoreBackups(backups); // clear ResX files foreach (ResXProjectItem target in resxItems.Values) { ClearFile(target); } VLDocumentViewsManager.CloseInvisibleWindows(typeof(BatchMoveCommand), false); } }
/// <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); }