Skip to content

Commit

Permalink
Invoke IdeScope.Actions.ShowProblem on UI thread
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandor Tardi committed Sep 21, 2021
1 parent 6e0fe3f commit dbdba88
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 20 deletions.
14 changes: 13 additions & 1 deletion SpecFlow.VisualStudio.Package/ProjectSystem/VsIdeActionsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,21 @@ public void ShowProblem(string description, string title = null)
{
Logger.LogWarning($"User Notification: {description}");
var caption = title == null ? "Visual Studio Extension for SpecFlow" : $"SpecFlow: {title}";
MessageBox.Show(description, caption, MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK);
IdeScope.RunOnUiThread(() =>
{
MessageBox.Show(
description,
caption,
MessageBoxButton.OK,
MessageBoxImage.Warning,
MessageBoxResult.OK);
});
}

/// <summary>
/// Have to be called from UI thread
/// </summary>
/// <param name="questionDescription"></param>
public void ShowQuestion(QuestionDescription questionDescription)
{
Logger.LogInfo($"User Question: {questionDescription.Description}");
Expand Down
2 changes: 1 addition & 1 deletion SpecFlow.VisualStudio.UI/Dialogs/RenameStepDialog.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Grid.Row="0" VerticalAlignment="Center">Original expression</Label>
<Label Grid.Column="1" Grid.Row="0" VerticalAlignment="Center" Content="{Binding OriginalStepText}" />
<TextBlock Grid.Column="1" Grid.Row="0" VerticalAlignment="Center" Text="{Binding OriginalStepText}" TextWrapping="WrapWithOverflow"/>
<Label Grid.Row="1" Grid.Column="0" VerticalAlignment="Center">Step expression:</Label>
<TextBox Grid.Row="1" Grid.Column="1" VerticalAlignment="Center" Text="{Binding StepText, UpdateSourceTrigger=PropertyChanged}"/>
<Label Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2" VerticalAlignment="Center" Foreground="Red" Content="{Binding ValidationError}" />
Expand Down
9 changes: 5 additions & 4 deletions SpecFlow.VisualStudio/Editor/Commands/RenameStepCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ private void PerformActions(IReadOnlyList<(IProjectScope specFlowTestProject, Pr
switch (stepDefinitions.Count)
{
case 0:
IdeScope.Actions.ShowProblem("No step definition found that is related to this position");
MonitoringService.MonitorCommandRenameStepExecuted(ctx);
ctx.AddCriticalProblem("No step definition found that is related to this position");
NotifyUserAboutIssues(ctx);
break;
case 1:
{
Expand Down Expand Up @@ -205,8 +205,9 @@ private bool Erroneous(RenameStepCommandContext ctx)

private void ShowProblem(RenameStepCommandContext ctx)
{
var problem = string.Join(Environment.NewLine, ctx.Issues.Select(issue => issue.Description));
IdeScope.Actions.ShowProblem(problem);
var problems = string.Join(Environment.NewLine, ctx.Issues.Select(issue => issue.Description));
IdeScope.Actions.ShowProblem(
$"The following problems occurred:{Environment.NewLine}{problems}", "Rename Step");
MonitoringService.MonitorCommandRenameStepExecuted(ctx);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace SpecFlow.VisualStudio.Tests.Editor.Commands
{
public class RenameStepCommandTests
{
private static readonly string _warningHeader = "ShowProblem: User Notification: The following problems occurred:" + Environment.NewLine;
private readonly ITestOutputHelper _testOutputHelper;
private readonly InMemoryStubProjectScope _projectScope;

Expand Down Expand Up @@ -63,7 +64,7 @@ private static StubLogger GetStubLogger(StubIdeScope ideScope)

private static bool SpecflowProjectMustHaveFeatureFiles(Tuple<TraceLevel, string> msg)
{
return msg.Item2 == "ShowProblem: User Notification: Unable to find step definition usages: could not find any SpecFlow project with feature files.";
return WithoutWarningHeader(msg.Item2) == "Unable to find step definition usages: could not find any SpecFlow project with feature files.";
}

private TestFeatureFile ArrangeOneFeatureFile(string featureFileContent)
Expand Down Expand Up @@ -186,6 +187,11 @@ private void ThereWereNoWarnings()
stubLogger.Messages.Should().NotContain(msg => msg.Item2.Contains("ShowProblem:"));
}

public static string WithoutWarningHeader(string message)
{
return message.Replace(_warningHeader, "");
}

[Fact]
public void There_is_a_project_in_ide()
{
Expand All @@ -197,7 +203,7 @@ public void There_is_a_project_in_ide()
command.PreExec(textView, command.Targets.First());

var stubLogger = GetStubLogger(emptyIde);
stubLogger.Messages.Last().Item2.Should().Be("ShowProblem: User Notification: Unable to find step definition usages: the project is not initialized yet.");
WithoutWarningHeader(stubLogger.Messages.Last().Item2).Should().Be("Unable to find step definition usages: the project is not initialized yet.");
}

[Fact]
Expand All @@ -210,7 +216,7 @@ public void Only_specflow_projects_are_supported()
command.PreExec(textView, command.Targets.First());

var stubLogger = GetStubLogger();
stubLogger.Messages.Last().Item2.Should().Be("ShowProblem: User Notification: Unable to find step definition usages: the project is not detected to be a SpecFlow project.");
WithoutWarningHeader(stubLogger.Messages.Last().Item2).Should().Be("Unable to find step definition usages: the project is not detected to be a SpecFlow project.");
}

[Fact]
Expand All @@ -230,7 +236,7 @@ public void Specflow_projects_must_have_feature_files()
[Fact]
public async Task There_must_be_at_lest_one_step_definition()
{
await StepDefinitionMustHaveValidExpression(TestStepDefinition.Void, "ShowProblem: User Notification: No step definition found that is related to this position");
await StepDefinitionMustHaveValidExpression(TestStepDefinition.Void, "No step definition found that is related to this position");
}

[Fact]
Expand All @@ -240,7 +246,7 @@ public async Task StepDefinition_regex_must_be_valid()
stepDefinition.TestExpression = SyntaxFactory.MissingToken(SyntaxKind.StringLiteralToken);
stepDefinition.Regex = default;

await StepDefinitionMustHaveValidExpression(stepDefinition, "ShowProblem: User Notification: Unable to rename step, the step definition expression cannot be detected.");
await StepDefinitionMustHaveValidExpression(stepDefinition, "Unable to rename step, the step definition expression cannot be detected.");
}

[Theory]
Expand All @@ -261,7 +267,7 @@ public async Task StepDefinition_expression_cannot_be_modified(int _, string emp
{
var stepDefinition = ArrangeStepDefinition(emptyExpression);

await StepDefinitionMustHaveValidExpression(stepDefinition, "ShowProblem: User Notification: The non-parameter parts cannot contain expression operators");
await StepDefinitionMustHaveValidExpression(stepDefinition, "The non-parameter parts cannot contain expression operators");
}

[Theory]
Expand All @@ -270,7 +276,7 @@ public async Task StepDefinition_expression_must_be_valid(string invalidExpressi
{
var stepDefinition = ArrangeStepDefinition(invalidExpression);

await StepDefinitionMustHaveValidExpression(stepDefinition, "ShowProblem: User Notification: Step definition expression is invalid");
await StepDefinitionMustHaveValidExpression(stepDefinition, "Step definition expression is invalid");
}

[Fact]
Expand All @@ -279,7 +285,7 @@ public async Task Constant_is_not_supported_in_step_definition_expression()
var stepDefinition = ArrangeStepDefinition("ConstantValue");
stepDefinition.Regex = "^I press add$";

await StepDefinitionMustHaveValidExpression(stepDefinition, "ShowProblem: User Notification: No expressions found to replace for [When(I press add)]: WhenIPressAdd");
await StepDefinitionMustHaveValidExpression(stepDefinition, "No expressions found to replace for [When(I press add)]: WhenIPressAdd");
}

private async Task StepDefinitionMustHaveValidExpression(TestStepDefinition stepDefinition, string errorMessage)
Expand All @@ -291,7 +297,7 @@ private async Task StepDefinitionMustHaveValidExpression(TestStepDefinition step

Dump(textView, "Step definition class after rename");
var stubLogger = GetStubLogger();
stubLogger.Messages.Last().Item2.Should().Be(errorMessage);
WithoutWarningHeader(stubLogger.Messages.Last().Item2).Should().Be(errorMessage);
}

private Task<IAnalyticsEvent> Invoke(RenameStepCommand command, StubWpfTextView textView)
Expand Down Expand Up @@ -367,10 +373,8 @@ public async Task User_cannot_type_invalid_expression(string _, string testExpre
await Invoke(command, textView);

var stubLogger = GetStubLogger();
var logMessage = stubLogger.Messages.Last().Item2;
const string userNotificationPrefix = "ShowProblem: User Notification: ";
logMessage.Should().StartWith(userNotificationPrefix);
var actualErrorMessages = logMessage.Substring(userNotificationPrefix.Length).Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
var logMessage = WithoutWarningHeader(stubLogger.Messages.Last().Item2);
var actualErrorMessages = logMessage.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
actualErrorMessages.Should().BeEquivalentTo(errorMessages);
}

Expand Down Expand Up @@ -514,7 +518,7 @@ public async Task Step_of_scenario_outline_in_the_feature_cannot_be_renamed(stri
await OneFeatureFileRename(originalExpression, updatedExpression, featureFile);

var stubLogger = GetStubLogger();
stubLogger.Messages.Last().Item2.Should().Be("ShowProblem: User Notification: " + _projectScope.ProjectFolder + string.Join(Environment.NewLine, errorMessages));
WithoutWarningHeader(stubLogger.Messages.Last().Item2).Should().Be(_projectScope.ProjectFolder + string.Join(Environment.NewLine, errorMessages));
}

private async Task<TestText> OneFeatureFileRename(string originalExpression, string updatedExpression,
Expand Down

0 comments on commit dbdba88

Please sign in to comment.