Skip to content

Commit

Permalink
Issue 38/change active document (#40)
Browse files Browse the repository at this point in the history
* Preview image updated

* Begun implementing failing test case

#38

* Fixed test condition

#38

* Cleanup

* Source document activation added to search result activation logic

#38

* Different variations of "source reattachment" added for further study

#38

* Failing test added as starting point for implementation

* Handling of most search result source document use cases implemented

Resolves #38

* Version bumped to v6.0.1
  • Loading branch information
uli-weltersbach authored Mar 10, 2019
1 parent 0b22b99 commit af9bfe0
Show file tree
Hide file tree
Showing 17 changed files with 252 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
[assembly: AssemblyCompany("Reason→Code→Example (http://reasoncodeexample.com)")]
[assembly: AssemblyProduct("ReasonCodeExample.XPathTools.Tests")]
[assembly: ComVisible(false)]
[assembly: AssemblyVersion("6.0.0.*")]
[assembly: AssemblyVersion("6.0.1.*")]
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private void WaitUntillStarted(TimeSpan timeoutDuration)
throw new TimeoutException($"Visual Studio wasn't started within {timeoutDuration.TotalSeconds} seconds.");
}

public void OpenXmlFile(string content, int? caretPosition)
public FileInfo OpenXmlFile(string content, int? caretPosition)
{
var temporaryFile = CreateTemporaryFile(content);
OpenFilePickerDialog();
Expand All @@ -135,6 +135,8 @@ public void OpenXmlFile(string content, int? caretPosition)
{
SetCaretPosition(caretPosition.Value);
}

return temporaryFile;
}

private FileInfo CreateTemporaryFile(string content)
Expand Down Expand Up @@ -222,5 +224,14 @@ where descendant.GetSupportedProperties().Contains(AutomationElement.NamePropert
where commandName.IsMatch(elementName.ToString())
select descendant).Distinct().ToArray();
}

public AutomationElement GetSelectedDocument()
{
var classNameCondition = new PropertyCondition(AutomationElement.ClassNameProperty, "TabItem", PropertyConditionFlags.IgnoreCase);
var isSelectedCondition = new PropertyCondition(SelectionItemPattern.IsSelectedProperty, true);
var activeDocumentCondition = new AndCondition(classNameCondition, isSelectedCondition);
var selectedDocument = MainWindow.FindDescendant(activeDocumentCondition);
return selectedDocument;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Linq;
using System.Xml.Linq;
using System.Xml.XPath;
using NSubstitute;
using NSubstitute.ReturnsExtensions;
using NUnit.Framework;
using ReasonCodeExample.XPathTools.Workbench;

Expand All @@ -9,6 +11,15 @@ namespace ReasonCodeExample.XPathTools.Tests.Workbench
[TestFixture]
public class SearchResultFactoryTests
{
private ActiveDocument _activeDocument;

[OneTimeSetUp]
public void InitializeActiveDocument()
{
_activeDocument = Substitute.For<ActiveDocument>();
_activeDocument.Current.ReturnsNull();
}

private const string Xml = @"<xml>
<child name='first'>Hello World!</child>
<child name='second'>Guten Morgen Welt!</child>
Expand All @@ -21,7 +32,7 @@ public class SearchResultFactoryTests
public void SetsLineNumber(string xpath, int lineNumber)
{
// Arrange
var factory = new SearchResultFactory();
var factory = new SearchResultFactory(_activeDocument);
var evaluationResult = XElement.Parse(Xml, LoadOptions.SetLineInfo).XPathEvaluate(xpath);

// Act
Expand All @@ -37,7 +48,7 @@ public void SetsLineNumber(string xpath, int lineNumber)
public void SetsLinePosition(string xpath, int linePosition)
{
// Arrange
var factory = new SearchResultFactory();
var factory = new SearchResultFactory(_activeDocument);
var evaluationResult = XElement.Parse(Xml, LoadOptions.SetLineInfo).XPathEvaluate(xpath);

// Act
Expand All @@ -51,7 +62,7 @@ public void SetsLinePosition(string xpath, int linePosition)
public void HandlesNullGracefully()
{
// Arrange
var factory = new SearchResultFactory();
var factory = new SearchResultFactory(_activeDocument);

// Act
var results = factory.Parse(null);
Expand All @@ -64,7 +75,7 @@ public void HandlesNullGracefully()
public void ParsesXPathWhichEvaluatesToAttribute()
{
// Arrange
var factory = new SearchResultFactory();
var factory = new SearchResultFactory(_activeDocument);
var evaluationResult = XElement.Parse(Xml).XPathEvaluate("/child/@name");
var expectedAttributeValues = new[]
{
Expand All @@ -82,7 +93,7 @@ public void ParsesXPathWhichEvaluatesToAttribute()
public void ParsesXPathWhichEvaluatesToBoolean()
{
// Arrange
var factory = new SearchResultFactory();
var factory = new SearchResultFactory(_activeDocument);
var evaluationResult = XElement.Parse(Xml).XPathEvaluate("count(/child/@name) > 0");

// Act
Expand All @@ -96,7 +107,7 @@ public void ParsesXPathWhichEvaluatesToBoolean()
public void ParsesXPathWhichEvaluatesToElements()
{
// Arrange
var factory = new SearchResultFactory();
var factory = new SearchResultFactory(_activeDocument);
var evaluationResult = XElement.Parse(Xml).XPathEvaluate("/child");

// Act
Expand All @@ -110,7 +121,7 @@ public void ParsesXPathWhichEvaluatesToElements()
public void ParsesXPathWhichEvaluatesToNumber()
{
// Arrange
var factory = new SearchResultFactory();
var factory = new SearchResultFactory(_activeDocument);
var evaluationResult = XElement.Parse(Xml).XPathEvaluate("count(/child/@name)");

// Act
Expand All @@ -124,7 +135,7 @@ public void ParsesXPathWhichEvaluatesToNumber()
public void ParsesXPathWhichEvaluatesToString()
{
// Arrange
var factory = new SearchResultFactory();
var factory = new SearchResultFactory(_activeDocument);
var evaluationResult = XElement.Parse(Xml).XPathEvaluate("/child/text()");
var expectedTextValues = new[]
{
Expand All @@ -145,7 +156,7 @@ public void ParsesXPathWhichEvaluatesToString()
public void SetsSelectionLength(string xpath, int expectedLength)
{
// Arrange
var factory = new SearchResultFactory();
var factory = new SearchResultFactory(_activeDocument);
var evaluationResult = XElement.Parse(Xml, LoadOptions.SetLineInfo).XPathEvaluate(xpath);

// Act
Expand All @@ -155,4 +166,4 @@ public void SetsSelectionLength(string xpath, int expectedLength)
Assert.That(result.SelectionLength, Is.EqualTo(expectedLength));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,12 @@ public void Search(string xpath)
SearchText = xpath;
SendKeys.SendWait("{ENTER}");
}

public AutomationElement GetSearchResult(int searchResultIndex)
{
var searchResultCondition = new PropertyCondition(AutomationElement.ClassNameProperty, "DataGridRow", PropertyConditionFlags.IgnoreCase);
var searchResults = ToolWindowPane.FindAll(TreeScope.Descendants, searchResultCondition);
return searchResults.Count > searchResultIndex ? searchResults[searchResultIndex] : null;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using NUnit.Framework;
using System.IO;
using System.Windows.Forms;
using NUnit.Framework;
using ReasonCodeExample.XPathTools.Tests.VisualStudioIntegration;
using ReasonCodeExample.XPathTools.VisualStudioIntegration;

Expand All @@ -9,13 +11,14 @@ namespace ReasonCodeExample.XPathTools.Tests.Workbench
public class XPathWorkbenchTests
{
private readonly VisualStudioExperimentalInstance _visualStudio = new VisualStudioExperimentalInstance();
private FileInfo _defaultXmlFile;

[OneTimeSetUp]
public void StartVisualStudio()
{
_visualStudio.ReStart();
var xml = "<assemblyBinding xmlns=\"urn:schemas-microsoft-com:asm.v1\" xmlns:urn=\"urn:schemas-microsoft-com:asm.v1\"><dependentAssembly /></assemblyBinding>";
_visualStudio.OpenXmlFile(xml, null);
_defaultXmlFile = _visualStudio.OpenXmlFile(xml, null);
_visualStudio.ClickContextMenuEntry(PackageResources.ShowXPathWorkbenchCommandText);
}

Expand Down Expand Up @@ -73,5 +76,37 @@ public void WorkbenchHandlesXmlNamespaces()
// Assert
Assert.That(xpathWorkbench.SearchResultText, Does.Contain(expectedResultText));
}

[Test]
public void WorkbenchActivatesCorrectDocumentWindow()
{
// Arrange
var xpathWorkbench = new XPathWorkbenchAutomationModel(_visualStudio.MainWindow);
xpathWorkbench.Search("/urn:assemblyBinding/urn:dependentAssembly");
var xml = "<!-- This XML file is not the search result source --><root />";
_visualStudio.OpenXmlFile(xml, null);

// Act
xpathWorkbench.GetSearchResult(0).LeftClick();

// Assert
Assert.That(_visualStudio.GetSelectedDocument().GetText(), Is.EqualTo(_defaultXmlFile.Name));
}

[Test]
public void WorkbenchReattachesCorrectDocumentWindow()
{
// Arrange - open a document, run a search, close the document
var xpathWorkbench = new XPathWorkbenchAutomationModel(_visualStudio.MainWindow);
xpathWorkbench.Search("/urn:assemblyBinding/urn:dependentAssembly");
xpathWorkbench.GetSearchResult(0).LeftClick();
SendKeys.SendWait("^{F4}"); // Close the document using CTRL + F4

// Act - click the search result
xpathWorkbench.GetSearchResult(0).LeftClick();

// Assert - verify that the document is reopened
Assert.That(_visualStudio.GetSelectedDocument().GetText(), Is.EqualTo(_defaultXmlFile.Name));
}
}
}
16 changes: 11 additions & 5 deletions ReasonCodeExample.XPathTools/ActiveDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,21 @@ namespace ReasonCodeExample.XPathTools
{
internal class ActiveDocument
{
public bool IsXmlDocument
public virtual Document Current
{
get
{
ThreadHelper.ThrowIfNotOnUIThread();
var dte = (DTE)Package.GetGlobalService(typeof(DTE));
var isXmlDocument = string.Equals(dte?.ActiveDocument?.Language, Constants.XmlContentTypeName, StringComparison.InvariantCultureIgnoreCase);
return dte?.ActiveDocument;
}
}

public bool IsXmlDocument
{
get
{
var isXmlDocument = string.Equals(Current?.Language, Constants.XmlContentTypeName, StringComparison.InvariantCultureIgnoreCase);
return isXmlDocument;
}
}
Expand All @@ -22,9 +30,7 @@ public string AbsolutePath
{
get
{
ThreadHelper.ThrowIfNotOnUIThread();
var dte = (DTE)Package.GetGlobalService(typeof(DTE));
return dte?.ActiveDocument?.FullName;
return Current?.FullName;
}
}
}
Expand Down
Binary file modified ReasonCodeExample.XPathTools/Assets/PackagePreviewImage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion ReasonCodeExample.XPathTools/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[assembly: AssemblyCompany("Reason→Code→Example (http://reasoncodeexample.com)")]
[assembly: AssemblyProduct("ReasonCodeExample.XPathTools")]
[assembly: ComVisible(false)]
[assembly: AssemblyVersion("6.0.0.*")]
[assembly: AssemblyVersion("6.0.1.*")]
[assembly: InternalsVisibleTo(InternalsVisibleTo.ReasonCodeExampleXPathToolsTests)]
[assembly: InternalsVisibleTo(InternalsVisibleTo.DynamicProxyGenAssembly2)]
[assembly: InternalsVisibleTo(InternalsVisibleTo.CastleCore)]
Expand Down
2 changes: 1 addition & 1 deletion ReasonCodeExample.XPathTools/Registry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private static void RegisterDefaultServices(ServiceContainer serviceContainer)
var writerFactory = new XPathWriterFactory(GetCurrentConfiguration);
serviceContainer.Set(writerFactory);

var searchResultFactory = new SearchResultFactory();
var searchResultFactory = new SearchResultFactory(activeDocument);
serviceContainer.Set(searchResultFactory);

ThreadHelper.ThrowIfNotOnUIThread();
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@
<data name="XPathWorkbenchNavigationErrorTitle" xml:space="preserve">
<value>Error</value>
</data>
<data name="XPathWorkbenchNavigationSourceActivationFailureText" xml:space="preserve">
<value>Can't open the source document. It may have been closed, renamed, moved or deleted.</value>
</data>
<data name="XPathWorkbenchSearchWatermark" xml:space="preserve">
<value>Enter XPath...</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="ReasonCodeExample.XPathInformation.v1.0" Version="6.0.0" Language="en-US" Publisher="Uli Weltersbach" />
<Identity Id="ReasonCodeExample.XPathInformation.v1.0" Version="6.0.1" Language="en-US" Publisher="Uli Weltersbach" />
<DisplayName>XPath Tools</DisplayName>
<Description xml:space="preserve">Run XPaths and XPath functions. Browse through results at the click of a button.
Track and copy XPaths incl. XML namespaces in various formats, taking the hassle out of complex documents.</Description>
Expand Down
17 changes: 16 additions & 1 deletion ReasonCodeExample.XPathTools/Workbench/SearchResult.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System.IO;
using EnvDTE;

namespace ReasonCodeExample.XPathTools.Workbench
{
public class SearchResult
Expand All @@ -24,5 +27,17 @@ public int? SelectionLength
get;
set;
}

public Document SourceDocument
{
get;
set;
}

public FileInfo SourceFile
{
get;
set;
}
}
}
}
Loading

0 comments on commit af9bfe0

Please sign in to comment.