diff --git a/src/Razor/test/Microsoft.VisualStudio.Razor.IntegrationTests/AbstractRazorEditorTest.cs b/src/Razor/test/Microsoft.VisualStudio.Razor.IntegrationTests/AbstractRazorEditorTest.cs index 2897919d2fa..d14625a0411 100644 --- a/src/Razor/test/Microsoft.VisualStudio.Razor.IntegrationTests/AbstractRazorEditorTest.cs +++ b/src/Razor/test/Microsoft.VisualStudio.Razor.IntegrationTests/AbstractRazorEditorTest.cs @@ -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"; @@ -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); } } } diff --git a/src/Razor/test/Microsoft.VisualStudio.Razor.IntegrationTests/GoToDefinitionTests.cs b/src/Razor/test/Microsoft.VisualStudio.Razor.IntegrationTests/GoToDefinitionTests.cs new file mode 100644 index 00000000000..2e7c981e824 --- /dev/null +++ b/src/Razor/test/Microsoft.VisualStudio.Razor.IntegrationTests/GoToDefinitionTests.cs @@ -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(@"(cancellationToken); + ErrorHandler.ThrowOnFailure(dispatcher.Exec(commandGuid, commandId, (uint)OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, IntPtr.Zero, IntPtr.Zero)); + } + } +} diff --git a/src/Razor/test/Microsoft.VisualStudio.Razor.IntegrationTests/InProcess/EditorInProcess_Text.cs b/src/Razor/test/Microsoft.VisualStudio.Razor.IntegrationTests/InProcess/EditorInProcess_Text.cs index bdef91eea63..4744f6e3bea 100644 --- a/src/Razor/test/Microsoft.VisualStudio.Razor.IntegrationTests/InProcess/EditorInProcess_Text.cs +++ b/src/Razor/test/Microsoft.VisualStudio.Razor.IntegrationTests/InProcess/EditorInProcess_Text.cs @@ -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 @@ -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);