Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More hardening of the completion test #10904

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license. See License.txt in the project root for license information.

using System;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Roslyn.Test.Utilities;
Expand Down Expand Up @@ -212,7 +213,8 @@ private void IncrementCount()
""",
search: "</PageTitle>",
stringsToType: ["{ENTER}", "{ENTER}", "<", "s", "p", "a"],
commitChar: '>');
commitChar: '>',
"span");
}

[IdeFact]
Expand Down Expand Up @@ -253,7 +255,7 @@ private void IncrementCount()
stringsToType: ["{ENTER}", "{ENTER}", "m", "y", "C", "u", "r"]);
}

private async Task VerifyTypeAndCommitCompletionAsync(string input, string output, string search, string[] stringsToType, char? commitChar = null)
private async Task VerifyTypeAndCommitCompletionAsync(string input, string output, string search, string[] stringsToType, char? commitChar = null, string? expectedSelectedItemLabel = null)
{
await TestServices.SolutionExplorer.AddFileAsync(
RazorProjectConstants.BlazorProjectName,
Expand All @@ -270,7 +272,14 @@ await TestServices.SolutionExplorer.AddFileAsync(
TestServices.Input.Send(stringToType);
}

await CommitCompletionAndVerifyAsync(output, commitChar);
if (expectedSelectedItemLabel is not null)
{
await CommitCompletionAndVerifyAsync(output, expectedSelectedItemLabel, commitChar);
}
else
{
await CommitCompletionAndVerifyAsync(output, commitChar);
}
}

[IdeFact]
Expand Down Expand Up @@ -457,4 +466,41 @@ private async Task CommitCompletionAndVerifyAsync(string expected, char? commitC
// tests allow for it as long as the content is correct
AssertEx.AssertEqualToleratingWhitespaceDifferences(expected, text);
}

private async Task CommitCompletionAndVerifyAsync(string expected, string expectedSelectedItemLabel, char? commitChar = null)
{
// Actually open completion UI and wait for it have selected item we are interested in
var session = await TestServices.Editor.OpenCompletionSessionAndWaitForItemAsync(TimeSpan.FromSeconds(10), expectedSelectedItemLabel, HangMitigatingCancellationToken);

Assert.NotNull(session);
if (commitChar.HasValue)
{
// Commit using the specified commit character
session.Commit(commitChar.Value, HangMitigatingCancellationToken);

// session.Commit call above commits as if the commit character was typed,
// but doesn't actually insert the character into the buffer.
// So we still need to insert the character into the buffer ourselves.
TestServices.Input.Send(commitChar.Value.ToString());
}
else
{
Assert.True(session.CommitIfUnique(HangMitigatingCancellationToken));
}

var textView = await TestServices.Editor.GetActiveTextViewAsync(HangMitigatingCancellationToken);

var stopwatch = new Stopwatch();
string text;
while ((text = textView.TextBuffer.CurrentSnapshot.GetText()) != expected && stopwatch.ElapsedMilliseconds < 10000)
{
// Text might get updated *after* completion by something like auto-insert, so wait for the desired text
await Task.Delay(100);
}

// Snippets may have slight whitespace differences due to line endings. These
// tests allow for it as long as the content is correct
Assert.Equal(expected, text);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,43 @@ public async Task DismissCompletionSessionsAsync(CancellationToken cancellationT

return session;
}

/// <summary>
/// Open completion pop-up window UI and wait for the specified item to be present selected
/// </summary>
/// <param name="timeOut"></param>
/// <param name="selectedItemLabel"></param>
/// <param name="cancellationToken"></param>
/// <returns>Completion session that has matching selected item, or null otherwise</returns>
public async Task<IAsyncCompletionSession?> OpenCompletionSessionAndWaitForItemAsync(TimeSpan timeOut, string selectedItemLabel, CancellationToken cancellationToken)
{
await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);

// Returns completion session that might or might not be visible in the IDE
var session = await WaitForCompletionSessionAsync(timeOut, cancellationToken);

if (session is null)
{
return null;
}

var textView = await GetActiveTextViewAsync(cancellationToken);
var stopWatch = Stopwatch.StartNew();

// Actually open the completion pop-up window and force visible items to be computed or re-computed
session.OpenOrUpdate(new CompletionTrigger(CompletionTriggerReason.Insertion, textView.TextSnapshot), textView.Caret.Position.BufferPosition, cancellationToken);
while (session.GetComputedItems(cancellationToken).SelectedItem?.DisplayText != selectedItemLabel)
{
if (stopWatch.ElapsedMilliseconds >= timeOut.TotalMilliseconds)
{
return null;
}

await Task.Delay(TimeSpan.FromMilliseconds(100), cancellationToken);

session.OpenOrUpdate(new CompletionTrigger(CompletionTriggerReason.Insertion, textView.TextSnapshot), textView.Caret.Position.BufferPosition, cancellationToken);
}

return session;
}
}
Loading