Skip to content

Commit

Permalink
Port BasicLineCommit to the new integration test project
Browse files Browse the repository at this point in the history
Closes #59855
  • Loading branch information
sharwell committed Mar 7, 2022
1 parent 1850a4b commit b302a80
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 158 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
using IComponentModel = Microsoft.VisualStudio.ComponentModelHost.IComponentModel;
using IObjectWithSite = Microsoft.VisualStudio.OLE.Interop.IObjectWithSite;
using IOleServiceProvider = Microsoft.VisualStudio.OLE.Interop.IServiceProvider;
using IPersistFile = Microsoft.VisualStudio.OLE.Interop.IPersistFile;
using OLECMDEXECOPT = Microsoft.VisualStudio.OLE.Interop.OLECMDEXECOPT;
using SComponentModel = Microsoft.VisualStudio.ComponentModelHost.SComponentModel;
using TextSpan = Microsoft.CodeAnalysis.Text.TextSpan;
Expand Down Expand Up @@ -74,6 +75,34 @@ public async Task WaitForEditorOperationsAsync(CancellationToken cancellationTok
}
}

public async Task<bool> IsSavedAsync(CancellationToken cancellationToken)
{
await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);

var vsView = await GetActiveVsTextViewAsync(cancellationToken);
ErrorHandler.ThrowOnFailure(vsView.GetBuffer(out var buffer));

// From CVsDocument::get_Saved
if (buffer is IVsPersistDocData persistDocData)
{
ErrorHandler.ThrowOnFailure(persistDocData.IsDocDataDirty(out var dirty));
return dirty == 0;
}
else if (buffer is IPersistFile persistFile)
{
return persistFile.IsDirty() == 0;
}
else if (buffer is IPersistFileFormat persistFileFormat)
{
ErrorHandler.ThrowOnFailure(persistFileFormat.IsDirty(out var dirty));
return dirty == 0;
}
else
{
throw new InvalidOperationException("Unsupported document");
}
}

public async Task SetTextAsync(string text, CancellationToken cancellationToken)
{
await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public async Task ResetGlobalOptionsAsync(CancellationToken cancellationToken)
ResetPerLanguageOption(globalOptions, NavigationBarViewOptions.ShowNavigationBar);
ResetPerLanguageOption2(globalOptions, VisualStudioNavigationOptions.NavigateToObjectBrowser);
ResetPerLanguageOption2(globalOptions, FeatureOnOffOptions.AddImportsOnPaste);
ResetPerLanguageOption2(globalOptions, FeatureOnOffOptions.PrettyListing);

static void ResetOption2<T>(IGlobalOptionService globalOptions, Option2<T> option)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Editor.Shared.Options;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Test.Utilities;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.IntegrationTest.Utilities.Input;
using Roslyn.Test.Utilities;
using Roslyn.VisualStudio.IntegrationTests;
using Xunit;

namespace Roslyn.VisualStudio.NewIntegrationTests.VisualBasic
{
[Trait(Traits.Feature, Traits.Features.LineCommit)]
public class BasicLineCommit : AbstractEditorTest
{
protected override string LanguageName => LanguageNames.VisualBasic;

public BasicLineCommit()
: base(nameof(BasicLineCommit))
{
}

[IdeFact]
public async Task CaseCorrection()
{
await TestServices.Editor.SetTextAsync(@"Module Goo
Sub M()
Dim x = Sub()
End Sub
End Module", HangMitigatingCancellationToken);

await TestServices.Editor.PlaceCaretAsync("Sub()", charsOffset: 1, HangMitigatingCancellationToken);
await TestServices.Input.SendAsync(VirtualKey.Enter);
Assert.Equal(48, await TestServices.Editor.GetCaretPositionAsync(HangMitigatingCancellationToken));
}

[IdeFact]
public async Task UndoWithEndConstruct()
{
await TestServices.Editor.SetTextAsync(@"Module Module1
Sub Main()
End Sub
REM
End Module", HangMitigatingCancellationToken);

await TestServices.Editor.PlaceCaretAsync(" REM", charsOffset: 0, HangMitigatingCancellationToken);
await TestServices.Input.SendAsync("sub", VirtualKey.Escape, " goo()", VirtualKey.Enter);
AssertEx.EqualOrDiff(@"Module Module1
Sub Main()
End Sub
Sub goo()
End Sub
End Module", await TestServices.Editor.GetTextAsync(HangMitigatingCancellationToken));
await TestServices.Shell.ExecuteCommandAsync(VSConstants.VSStd97CmdID.Undo, HangMitigatingCancellationToken);
Assert.Equal(54, await TestServices.Editor.GetCaretPositionAsync(HangMitigatingCancellationToken));
}

[IdeFact]
public async Task UndoWithoutEndConstruct()
{
await TestServices.Editor.SetTextAsync(@"Module Module1
''' <summary></summary>
Sub Main()
End Sub
End Module", HangMitigatingCancellationToken);

await TestServices.Editor.PlaceCaretAsync("Module1", charsOffset: 0, HangMitigatingCancellationToken);
await TestServices.Input.SendAsync(VirtualKey.Down, VirtualKey.Enter);
AssertEx.EqualOrDiff(@"Module Module1
''' <summary></summary>
Sub Main()
End Sub
End Module", await TestServices.Editor.GetTextAsync(HangMitigatingCancellationToken));
Assert.Equal(18, await TestServices.Editor.GetCaretPositionAsync(HangMitigatingCancellationToken));
await TestServices.Shell.ExecuteCommandAsync(VSConstants.VSStd97CmdID.Undo, HangMitigatingCancellationToken);
Assert.Equal(16, await TestServices.Editor.GetCaretPositionAsync(HangMitigatingCancellationToken));
}

[IdeFact]
public async Task CommitOnSave()
{
await TestServices.Editor.SetTextAsync(@"Module Module1
Sub Main()
End Sub
End Module
", HangMitigatingCancellationToken);

await TestServices.Editor.PlaceCaretAsync("(", charsOffset: 1, HangMitigatingCancellationToken);
await TestServices.Input.SendAsync("x As integer", VirtualKey.Tab);

Assert.False(await TestServices.Editor.IsSavedAsync(HangMitigatingCancellationToken));
await TestServices.Input.SendAsync(new KeyPress(VirtualKey.S, ShiftState.Ctrl));
Assert.True(await TestServices.Editor.IsSavedAsync(HangMitigatingCancellationToken));
AssertEx.EqualOrDiff(@"Module Module1
Sub Main(x As Integer)
End Sub
End Module
", await TestServices.Editor.GetTextAsync(HangMitigatingCancellationToken));

await TestServices.Shell.ExecuteCommandAsync(VSConstants.VSStd97CmdID.Undo, HangMitigatingCancellationToken);
AssertEx.EqualOrDiff(@"Module Module1
Sub Main(x As Integer)
End Sub
End Module
", await TestServices.Editor.GetTextAsync(HangMitigatingCancellationToken));
Assert.Equal(45, await TestServices.Editor.GetCaretPositionAsync(HangMitigatingCancellationToken));
}

[IdeFact]
public async Task CommitOnFocusLost()
{
await TestServices.Editor.SetTextAsync(@"Module M
Sub M()
End Sub
End Module", HangMitigatingCancellationToken);

await TestServices.Editor.PlaceCaretAsync("End Sub", charsOffset: -1, HangMitigatingCancellationToken);
await TestServices.Input.SendAsync(" ");
await TestServices.SolutionExplorer.AddFileAsync(ProjectName, "TestZ.vb", open: true, cancellationToken: HangMitigatingCancellationToken); // Cause focus lost
await TestServices.SolutionExplorer.OpenFileAsync(ProjectName, "TestZ.vb", HangMitigatingCancellationToken); // Work around https://github.com/dotnet/roslyn/issues/18488
await TestServices.Input.SendAsync(" ");
await TestServices.SolutionExplorer.CloseCodeFileAsync(ProjectName, "TestZ.vb", saveFile: false, cancellationToken: HangMitigatingCancellationToken);
AssertEx.EqualOrDiff(@"Module M
Sub M()
End Sub
End Module", await TestServices.Editor.GetTextAsync(HangMitigatingCancellationToken));
}

[IdeFact]
public async Task CommitOnFocusLostDoesNotFormatWithPrettyListingOff()
{
var globalOptions = await TestServices.Shell.GetComponentModelServiceAsync<IGlobalOptionService>(HangMitigatingCancellationToken);
globalOptions.SetGlobalOption(new OptionKey(FeatureOnOffOptions.PrettyListing, LanguageNames.VisualBasic), false);

await TestServices.Editor.SetTextAsync(@"Module M
Sub M()
End Sub
End Module", HangMitigatingCancellationToken);

await TestServices.Editor.PlaceCaretAsync("End Sub", charsOffset: -1, HangMitigatingCancellationToken);
await TestServices.Input.SendAsync(" ");
await TestServices.SolutionExplorer.AddFileAsync(ProjectName, "TestZ.vb", open: true, cancellationToken: HangMitigatingCancellationToken); // Cause focus lost
await TestServices.SolutionExplorer.OpenFileAsync(ProjectName, "TestZ.vb", HangMitigatingCancellationToken); // Work around https://github.com/dotnet/roslyn/issues/18488
await TestServices.Input.SendAsync(" ");
await TestServices.SolutionExplorer.CloseCodeFileAsync(ProjectName, "TestZ.vb", saveFile: false, HangMitigatingCancellationToken);
AssertEx.EqualOrDiff(@"Module M
Sub M()
End Sub
End Module", await TestServices.Editor.GetTextAsync(HangMitigatingCancellationToken));
}
}
}

0 comments on commit b302a80

Please sign in to comment.