Skip to content

Commit

Permalink
Merge pull request #1139 from TestCentric/FixRunSelectedTests
Browse files Browse the repository at this point in the history
Fix crash when 'run selected tests' but no tests are selected
  • Loading branch information
rowo360 authored Oct 16, 2024
2 parents 03b79d6 + 7ef7135 commit 23c860c
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ void OnRunFinished(ResultNode result)
}
};

_model.Events.SelectedTestsChanged += (e) => UpdateViewCommands();

_settings.Changed += (s, e) =>
{
switch (e.SettingName)
Expand Down Expand Up @@ -754,10 +756,11 @@ private void UpdateViewCommands(bool testLoading = false)
bool hasFailures = _model.HasResults && _model.ResultSummary.FailedCount > 0;

_view.RunAllButton.Enabled =
_view.RunSelectedButton.Enabled =
_view.DisplayFormatButton.Enabled =
_view.RunParametersButton.Enabled = testLoaded && !testRunning;

_view.RunSelectedButton.Enabled = testLoaded && !testRunning && _model.SelectedTests != null && _model.SelectedTests.Any();

_view.RerunButton.Enabled = testLoaded && !testRunning && hasResults;

_view.RunFailedButton.Enabled = testLoaded && !testRunning && hasFailures;
Expand Down
44 changes: 42 additions & 2 deletions src/TestCentric/tests/Presenters/Main/CommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,10 @@ public void RunAllButton_RunsAllTests()
[Test]
public void RunButton_RunsSelectedTests()
{
// TODO: Specify Results and test with specific argument
var testSelection = new TestSelection();
_model.SelectedTests = testSelection;
_view.RunSelectedButton.Execute += Raise.Event<CommandHandler>();
_model.Received().RunTests(Arg.Any<TestSelection>());
_model.Received().RunTests(testSelection);
}

[Test]
Expand Down Expand Up @@ -309,5 +310,44 @@ public void ForceStopButton_ForcesTestsToStop()
_view.ForceStopButton.Execute += Raise.Event<CommandHandler>();
_model.Received().StopTestRun(true);
}

[Test]
public void RunSelectedTestCommand_NoTestsSelected_IsDisabled()
{
// Arrange
_model.HasTests.Returns(true);
_model.SelectedTests = null;

// Act + Assert
Assert.That(_view.RunSelectedButton.Enabled, Is.False);
}

[Test]
public void SelectedTestsChanged_NoTestSelected_CommandIsDisabled()
{
// Arrange
_model.HasTests.Returns(true);
_model.SelectedTests.Returns(new TestSelection());

// Act
_model.Events.SelectedTestsChanged += Raise.Event<TestSelectionEventHandler>(new TestSelectionEventArgs(null));

// Assert
Assert.That(_view.RunSelectedButton.Enabled, Is.False);
}

[Test]
public void SelectedTestsChanged_TestSelected_CommandIsEnabled()
{
// Arrange
_model.HasTests.Returns(true);
_model.SelectedTests.Returns(new TestSelection(new[] { new TestNode("<test-case id='1' />") }));

// Act
_model.Events.SelectedTestsChanged += Raise.Event<TestSelectionEventHandler>(new TestSelectionEventArgs(null));

// Assert
Assert.That(_view.RunSelectedButton.Enabled, Is.True);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public void SimulateTestRunFinish()
_model.HasResults.Returns(true);
_model.ResultSummary.Returns(new ResultSummary() { FailureCount = 1 });
_model.IsTestRunning.Returns(false);
_model.SelectedTests.Returns(new TestSelection(new[] { new TestNode("<test-case id='1' />") }));

var resultNode = new ResultNode("<test-run id='XXX' result='Failed' />");
FireRunFinishedEvent(resultNode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public void SimulateTestLoad()

TestNode testNode = new TestNode("<test-suite id='1'/>");
_model.LoadedTests.Returns(testNode);
_model.SelectedTests.Returns(new TestSelection(new[] { testNode }));

var project = new TestCentricProject(_model, "dummy.dll");
_model.TestCentricProject.Returns(project);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public void SimulateTestReload()

TestNode testNode = new TestNode("<test-suite id='1'/>");
_model.LoadedTests.Returns(testNode);
_model.SelectedTests.Returns(new TestSelection(new[] { testNode }));

FireTestReloadedEvent(testNode);
}
Expand Down
2 changes: 2 additions & 0 deletions src/TestModel/model/ITestEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace TestCentric.Gui.Model
public delegate void TestNodeEventHandler(TestNodeEventArgs args);
public delegate void TestResultEventHandler(TestResultEventArgs args);
public delegate void TestItemEventHandler(TestItemEventArgs args);
public delegate void TestSelectionEventHandler(TestSelectionEventArgs args);
public delegate void TestOutputEventHandler(TestOutputEventArgs args);
public delegate void UnhandledExceptionEventHandler(UnhandledExceptionEventArgs args);
public delegate void TestFilesLoadingEventHandler(TestFilesLoadingEventArgs args);
Expand Down Expand Up @@ -57,6 +58,7 @@ public interface ITestEvents
// Event used to broadcast a change in the selected
// item, so that all presenters may be notified.
event TestItemEventHandler SelectedItemChanged;
event TestSelectionEventHandler SelectedTestsChanged;

event TestEventHandler CategorySelectionChanged;
}
Expand Down
10 changes: 10 additions & 0 deletions src/TestModel/model/TestEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ public TestItemEventArgs(ITestItem testItem)
public ITestItem TestItem { get; private set; }
}

public class TestSelectionEventArgs : TestEventArgs
{
public TestSelectionEventArgs(TestSelection testSelection)
{
TestSelection = testSelection;
}

public TestSelection TestSelection { get; private set; }
}

public class TestOutputEventArgs : TestEventArgs
{
public TestOutputEventArgs(string testName, string stream, string text)
Expand Down
6 changes: 6 additions & 0 deletions src/TestModel/model/TestEventDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ public void FireActiveItemChanged(ITestItem testItem)
SelectedItemChanged?.Invoke(new TestItemEventArgs(testItem));
}

public void FireSelectedTestsChanged(TestSelection testSelection)
{
SelectedTestsChanged?.Invoke(new TestSelectionEventArgs(testSelection));
}

public void FireCategorySelectionChanged()
{
CategorySelectionChanged?.Invoke(new TestEventArgs());
Expand Down Expand Up @@ -156,6 +161,7 @@ public void FireCategorySelectionChanged()

// Test Selection Event
public event TestItemEventHandler SelectedItemChanged;
public event TestSelectionEventHandler SelectedTestsChanged;

public event TestEventHandler CategorySelectionChanged;

Expand Down
7 changes: 6 additions & 1 deletion src/TestModel/model/TestModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,12 @@ public ITestItem ActiveTestItem
/// <summary>
/// Gets or sets the list of selected tests.
/// </summary>
public TestSelection SelectedTests { get; set; }
public TestSelection SelectedTests
{
get { return _selectedTests; }
set { _selectedTests = value; _events?.FireSelectedTestsChanged(_selectedTests); }
}
private TestSelection _selectedTests;

public List<string> SelectedCategories { get; private set; }

Expand Down

0 comments on commit 23c860c

Please sign in to comment.