Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public abstract class AbstractRazorEditorTest : AbstractEditorTest
private static readonly string s_pagesDir = Path.Combine("Pages");
private static readonly string s_sharedDir = Path.Combine("Shared");
internal static readonly string CounterRazorFile = Path.Combine(s_pagesDir, "Counter.razor");
internal static readonly string IndexRazorFile = Path.Combine(s_pagesDir, "Index.razor");
internal static readonly string SemanticTokensFile = Path.Combine(s_pagesDir, "SemanticTokens.razor");
internal static readonly string MainLayoutFile = Path.Combine(s_sharedDir, "MainLayout.razor");
internal static readonly string ImportsRazorFile = "_Imports.razor";
Expand All @@ -30,6 +31,14 @@ public override async Task InitializeAsync()
await TestServices.Workspace.WaitForProjectSystemAsync(HangMitigatingCancellationToken);

await TestServices.Workspace.WaitForAsyncOperationsAsync(FeatureAttribute.LanguageServer, HangMitigatingCancellationToken);

// We open the Index.razor file, and wait for the SurveyPrompt component to be classified, as that
// way we know the LSP server is up and running and responding
await TestServices.SolutionExplorer.OpenFileAsync(BlazorProjectName, IndexRazorFile, HangMitigatingCancellationToken);
await TestServices.Editor.WaitForClassificationAsync(HangMitigatingCancellationToken, expectedClassification: "RazorComponentElement");

// Close the file we opened, just in case, so the test can start with a clean slate
await TestServices.Editor.CloseDocumentWindowAsync(HangMitigatingCancellationToken);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.

using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Host;
using Xunit;

namespace Microsoft.VisualStudio.Razor.IntegrationTests
{
public class GoToDefinitionTests : AbstractRazorEditorTest
{
[IdeFact]
public async Task GoToDefinition_MethodInSameFile()
{
// Open the file
await TestServices.SolutionExplorer.OpenFileAsync(BlazorProjectName, CounterRazorFile, HangMitigatingCancellationToken);

await TestServices.Editor.PlaceCaretAsync("IncrementCount", charsOffset: -1, HangMitigatingCancellationToken);

// Act
await TestServices.Editor.InvokeGoToDefinitionAsync(HangMitigatingCancellationToken);

// Assert
await TestServices.Editor.WaitForCurrentLineTextAsync("private void IncrementCount()", HangMitigatingCancellationToken);
}

[IdeFact]
public async Task GoToDefinition_CSharpClass()
{
// Open the file
await TestServices.SolutionExplorer.OpenFileAsync(BlazorProjectName, IndexRazorFile, HangMitigatingCancellationToken);

// Change text to refer back to Program class
await TestServices.Editor.SetTextAsync(@"<SurveyPrompt Title=""@nameof(Program)", HangMitigatingCancellationToken);
await TestServices.Editor.PlaceCaretAsync("Program", charsOffset: -1, HangMitigatingCancellationToken);

// Act
await TestServices.Editor.InvokeGoToDefinitionAsync(HangMitigatingCancellationToken);

// Assert
await TestServices.Editor.WaitForActiveWindowAsync("Program.cs", HangMitigatingCancellationToken);
}

[IdeFact]
public async Task GoToDefinition_Component()
{
// Open the file
await TestServices.SolutionExplorer.OpenFileAsync(BlazorProjectName, IndexRazorFile, HangMitigatingCancellationToken);

await TestServices.Editor.PlaceCaretAsync("SurveyPrompt", charsOffset: -1, HangMitigatingCancellationToken);

// Act
await TestServices.Editor.InvokeGoToDefinitionAsync(HangMitigatingCancellationToken);

// Assert
await TestServices.Editor.WaitForActiveWindowAsync("SurveyPrompt.razor", HangMitigatingCancellationToken);
}

[IdeFact]
public async Task GoToDefinition_ComponentAttribute()
{
var version = await TestServices.Shell.GetVersionAsync(HangMitigatingCancellationToken);
if (version < new System.Version(17, 1, 32113, 165))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filed microsoft/vs-extension-testing#101. I think this'd be nice to have as part of the framework. Maybe instead of returning ("Success") we should do Assert.Ignore("Wrong VS Version"); so we know the test didn't really run.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I was looking for Assert.Inconclusive but didn't notice Ignore.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope. no Assert.Ignore in xUnit 😛

{
// Functionality under test was added in v17 Preview 3 (17.1.32113.165) so this test will
// file until CI is updated, so we'll skip it.
return;
}

// Open the file
await TestServices.SolutionExplorer.OpenFileAsync(BlazorProjectName, IndexRazorFile, HangMitigatingCancellationToken);

await TestServices.Editor.PlaceCaretAsync("Title=", charsOffset: -1, HangMitigatingCancellationToken);

// Act
await TestServices.Editor.InvokeGoToDefinitionAsync(HangMitigatingCancellationToken);

// Assert
await TestServices.Editor.WaitForActiveWindowAsync("SurveyPrompt.razor", HangMitigatingCancellationToken);
await TestServices.Editor.WaitForCurrentLineTextAsync("public string? Title { get; set; }", HangMitigatingCancellationToken);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.

using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Threading;
using static Microsoft.VisualStudio.VSConstants;

namespace Microsoft.VisualStudio.Extensibility.Testing
{
internal partial class EditorInProcess
{
public async Task InvokeGoToDefinitionAsync(CancellationToken cancellationToken)
{
var commandGuid = typeof(VSStd97CmdID).GUID;
var commandId = VSStd97CmdID.GotoDefn;
await ExecuteCommandAsync(commandGuid, (uint)commandId, cancellationToken);
}

public async Task CloseDocumentWindowAsync(CancellationToken cancellationToken)
{
var commandGuid = typeof(VSStd97CmdID).GUID;
var commandId = VSStd97CmdID.CloseDocument;
await ExecuteCommandAsync(commandGuid, (uint)commandId, cancellationToken);
}

private async Task ExecuteCommandAsync(Guid commandGuid, uint commandId, CancellationToken cancellationToken)
{
await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);

var dispatcher = await GetRequiredGlobalServiceAsync<SUIHostCommandDispatcher, IOleCommandTarget>(cancellationToken);
ErrorHandler.ThrowOnFailure(dispatcher.Exec(commandGuid, commandId, (uint)OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, IntPtr.Zero, IntPtr.Zero));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.

using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.Razor.IntegrationTests.InProcess;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Xunit;

namespace Microsoft.VisualStudio.Extensibility.Testing
Expand All @@ -28,6 +30,56 @@ public async Task VerifyTextContainsAsync(string text, CancellationToken cancell
Assert.Contains(text, content);
}

public async Task WaitForCurrentLineTextAsync(string text, CancellationToken cancellationToken)
{
var view = await GetActiveTextViewAsync(cancellationToken);

using var semaphore = new SemaphoreSlim(1);
await semaphore.WaitAsync(cancellationToken);

var caret = view.Caret.Position.BufferPosition;
var line = view.TextBuffer.CurrentSnapshot.GetLineFromPosition(caret).GetText();
if (line.Trim() == text.Trim())
{
semaphore.Release();
view.Caret.PositionChanged -= Caret_PositionChanged;
return;
}

view.Caret.PositionChanged += Caret_PositionChanged;

try
{
await semaphore.WaitAsync(cancellationToken);
}
finally
{
view.Caret.PositionChanged -= Caret_PositionChanged;
}

void Caret_PositionChanged(object sender, CaretPositionChangedEventArgs e)
{
var caret = view.Caret.Position.BufferPosition;
var line = view.TextBuffer.CurrentSnapshot.GetLineFromPosition(caret).GetText();
if (line.Trim() == text.Trim())
{
semaphore.Release();
}
}
}

public async Task WaitForActiveWindowAsync(string windowTitle, CancellationToken cancellationToken)
{
await Helper.RetryAsync(async ct =>
{
var activeWindowCaption = await TestServices.Shell.GetActiveWindowCaptionAsync(cancellationToken);

return activeWindowCaption == windowTitle;
},
TimeSpan.FromMilliseconds(50),
cancellationToken);
}

public async Task WaitForProjectReadyAsync(CancellationToken cancellationToken)
{
await TestServices.Workspace.WaitForAsyncOperationsAsync(FeatureAttribute.LanguageServer, cancellationToken);
Expand Down