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

Add toolbar button to reset test filters #1180

Merged
merged 2 commits into from
Feb 10, 2025
Merged
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
12 changes: 12 additions & 0 deletions src/TestCentric/testcentric.gui/Dialogs/CategoryFilterDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ internal void Init(IEnumerable<string> allCategories, IEnumerable<string> select
checkedListBoxCategory.ResumeLayout();
}

internal void UpdateCheckedItems(IEnumerable<string> selectedCategories)
{
checkedListBoxCategory.SuspendLayout();
for (int i = 0; i < checkedListBoxCategory.Items.Count; i++)
{
string category = checkedListBoxCategory.Items[i].ToString();
bool isChecked = selectedCategories.Contains(category);
checkedListBoxCategory.SetItemChecked(i, isChecked);
}
checkedListBoxCategory.ResumeLayout();
}

private void OnApplyAndCloseButtonClicked(object sender, EventArgs e)
{
ApplyButtonClicked?.Invoke(SelectedCategories);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public IEnumerable<string> SelectedItems
{
_selectedItems = value;
UpdateFont();

// If dialog is currently displayed => updated checked items
if (_dialog != null)
_dialog.UpdateCheckedItems(_selectedItems);
}
}

Expand Down Expand Up @@ -79,7 +83,8 @@ protected void OnButtonClicked(object sender, EventArgs e)

_dialog.ApplyButtonClicked += (selectedItems) =>
{
SelectedItems = selectedItems;
_selectedItems = selectedItems;
UpdateFont();
SelectionChanged?.Invoke();
};

Expand All @@ -103,7 +108,7 @@ private void SetDialogSizeAndPosition()
{
// Restore previous position and size of dialog
_dialog.Size = _dialogSize;
_dialogLocation = _dialog.Location;
_dialog.Location = _dialogLocation;
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public ToolStripTextBoxElement(ToolStripTextBox textBox, string placeHolderText)
{
TextBox = textBox;
PlaceHolderText = placeHolderText;
TextBox.TextChanged += OnTextChanged;
TextBox.KeyUp += OnKeyUp;

TextBox.LostFocus += OnTextBoxLostFocus;
TextBox.GotFocus += OnTextBoxGotFocus;
Expand Down Expand Up @@ -63,7 +63,11 @@ private void OnTextBoxLostFocus(object sender, EventArgs e)
}
}

private void OnTextChanged(object sender, EventArgs e)
/// <summary>
/// Use KeyUp event instead of TextChanged event
/// If the Text property is set from client programmatically, a TextChanged event would be triggered => that's not intended
/// </summary>
private void OnKeyUp(object sender, KeyEventArgs e)
{
if (IsPlaceHolderTextShown)
return;
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions src/TestCentric/testcentric.gui/Presenters/TreeViewPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ private void WireUpEvents()
{
EnsureNonRunnableFilesAreVisible(ea.Test);

// Handle category filter identically to close/load project
ResetTestFilterUIElements();
_view.CategoryFilter.Close();
_view.CategoryFilter.Init(_model);

Strategy.OnTestLoaded(ea.Test, null);
_view.CheckBoxes = _view.ShowCheckBoxes.Checked; // TODO: View should handle this
};
Expand All @@ -81,6 +86,7 @@ private void WireUpEvents()
{
Strategy.OnTestUnloaded();
_view.CategoryFilter.Close();
ResetTestFilterUIElements();
};

_model.Events.TestsUnloading += ea =>
Expand Down Expand Up @@ -272,6 +278,8 @@ private void WireUpEvents()
_model.TestCentricTestFilter.CategoryFilter = _view.CategoryFilter.SelectedItems;
};

_view.ResetFilterCommand.Execute += () => ResetTestFilter();

// Node selected in tree
//_treeView.SelectedNodesChanged += (nodes) =>
//{
Expand Down Expand Up @@ -302,6 +310,18 @@ private void WireUpEvents()
//};
}

private void ResetTestFilter()
{
_model.TestCentricTestFilter.ResetAll();
ResetTestFilterUIElements();
}

private void ResetTestFilterUIElements()
{
_view.TextFilter.Text = "";
_view.OutcomeFilter.SelectedItems = _model.TestCentricTestFilter.OutcomeFilter;
_view.CategoryFilter.SelectedItems = _model.TestCentricTestFilter.CategoryFilter;
}
private void UpdateTreeViewSortMode()
{
var sortMode = _view.SortCommand.SelectedItem;
Expand Down
2 changes: 2 additions & 0 deletions src/TestCentric/testcentric.gui/Views/ITestTreeView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public interface ITestTreeView : IView

ICategoryFilterSelection CategoryFilter { get; }

ICommand ResetFilterCommand { get; }

void SetTestFilterVisibility(bool visible);

void EnableTestFilter(bool enable);
Expand Down
11 changes: 11 additions & 0 deletions src/TestCentric/testcentric.gui/Views/TestTreeView.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/TestCentric/testcentric.gui/Views/TestTreeView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public TestTreeView()
OutcomeFilter = new MultiCheckedToolStripButtonGroup(new[] { filterOutcomePassedButton, filterOutcomeFailedButton, filterOutcomeWarningButton, filterOutcomeNotRunButton });
TextFilter = new ToolStripTextBoxElement(filterTextBox, "Filter...");
CategoryFilter = new ToolStripCategoryFilterButton(filterByCategory);
ResetFilterCommand = new ToolStripButtonElement(filterResetButton);
TreeView = treeView;

// NOTE: We use MouseDown here rather than MouseUp because
Expand Down Expand Up @@ -135,6 +136,7 @@ public bool CheckBoxes

public IMultiSelection OutcomeFilter { get; private set; }
public ICategoryFilterSelection CategoryFilter { get; private set; }
public ICommand ResetFilterCommand { get; private set; }

public IChanged TextFilter { get; private set; }

Expand Down
4 changes: 4 additions & 0 deletions src/TestCentric/testcentric.gui/Views/TestTreeView.resx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="ResetFilter.Image" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Images\ResetFilter.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="testTreeContextMenu.TrayLocation" type="System.Drawing.Point, System.Drawing">
<value>186, 16</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,34 @@ public void SortChanged_ToDuration_ShowDuration_IsSet()
_view.ShowTestDuration.Received().Checked = true;
}

[Test]
public void ResetFilterCommand_TestCentricTestFilter_AreReset()
{
// 1. Arrange
_view.SortCommand.SelectedItem.Returns(TreeViewNodeComparer.Duration);

// 2. Act
_view.ResetFilterCommand.Execute += Raise.Event<CommandHandler>();

// 3. Assert
_model.TestCentricTestFilter.Received().ResetAll();
}

[Test]
public void ResetFilterCommand_Filter_UIElements_AreReset()
{
// 1. Arrange
_view.SortCommand.SelectedItem.Returns(TreeViewNodeComparer.Duration);

// 2. Act
_view.ResetFilterCommand.Execute += Raise.Event<CommandHandler>();

// 3. Assert
_view.TextFilter.Received().Text = "";
_view.OutcomeFilter.ReceivedWithAnyArgs().SelectedItems = null;
_view.CategoryFilter.ReceivedWithAnyArgs().SelectedItems = null;
}

// TODO: Version 1 Test - Make it work if needed.
//[Test]
//public void WhenContextNodeIsNotNull_RunCommandExecutesThatTest()
Expand Down
33 changes: 31 additions & 2 deletions src/TestCentric/tests/Presenters/TestTree/WhenTestsAreReloaded.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,47 @@ namespace TestCentric.Gui.Presenters.TestTree
public class WhenTestsAreReloaded : TreeViewPresenterTestBase
{
[SetUp]
public void SimulateTestReload()
public void Setup()
{
ClearAllReceivedCalls();

_model.HasTests.Returns(true);
_model.IsTestRunning.Returns(false);
}

[Test]
public void TestFilters_AreReset()
{
// Arrange
var project = new TestCentricProject(_model, "dummy.dll");
TestNode testNode = new TestNode("<test-suite id='1'/>");
_model.LoadedTests.Returns(testNode);
_model.TestCentricProject.Returns(new TestCentricProject(_model, "dummy.dll"));
_model.TestCentricProject.Returns(project);

// Act
FireTestReloadedEvent(testNode);

// Assert
_view.TextFilter.Received().Text = "";
_view.OutcomeFilter.ReceivedWithAnyArgs().SelectedItems = null;
_view.CategoryFilter.ReceivedWithAnyArgs().SelectedItems = null;
}

[Test]
public void CategoryFilter_IsClosed_And_Init()
{
// Arrange
var project = new TestCentricProject(_model, "dummy.dll");
TestNode testNode = new TestNode("<test-suite id='1'/>");
_model.LoadedTests.Returns(testNode);
_model.TestCentricProject.Returns(project);

// Act
FireTestReloadedEvent(testNode);

// Assert
_view.CategoryFilter.Received().Close();
_view.CategoryFilter.Received().Init(_model);
}

#if NYI // Add after implementation of project or package saving
Expand Down
12 changes: 12 additions & 0 deletions src/TestCentric/tests/Presenters/TestTree/WhenTestsAreUnloaded.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ public void TestUnloaded_CategoryFilter_IsClosed()
_view.CategoryFilter.Received().Close();
}

[Test]
public void TestUnloaded_TestFilters_AreReset()
{
// Act: unload tests
FireTestUnloadedEvent();

// Assert
_view.TextFilter.Received().Text = "";
_view.OutcomeFilter.ReceivedWithAnyArgs().SelectedItems = null;
_view.CategoryFilter.ReceivedWithAnyArgs().SelectedItems = null;
}

#if NYI // Add after implementation of project or package saving
[TestCase("NewProjectCommand", true)]
[TestCase("OpenProjectCommand", true)]
Expand Down
2 changes: 1 addition & 1 deletion src/TestModel/model/Filter/ITestCentricTestFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public interface ITestCentricTestFilter
/// <summary>
/// Clear all actives filters and reset them to default
/// </summary>
void ClearAllFilters();
void ResetAll(bool suppressFilterChangedEvent = false);

/// <summary>
/// Init filter after a project is loaded
Expand Down
9 changes: 6 additions & 3 deletions src/TestModel/model/Filter/TestCentricTestFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,18 @@ public IEnumerable<string> AllCategories

public bool IsActive => _filters.Any(x => x.IsActive);

public void ClearAllFilters()
public void ResetAll(bool suppressFilterChangedEvent = false)
{
foreach (ITestFilter filter in _filters)
{
filter.Reset();
}

FilterNodes(TestModel.LoadedTests);
FireFilterChangedEvent();
if (!suppressFilterChangedEvent)
{
FilterNodes(TestModel.LoadedTests);
FireFilterChangedEvent();
}
}

public void Init()
Expand Down
3 changes: 3 additions & 0 deletions src/TestModel/model/TestModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,8 @@ public void UnloadTests()

UnloadTestsIgnoringErrors();
Runner.Dispose();

TestCentricTestFilter.ResetAll(true);
LoadedTests = null;
AvailableCategories = null;
ClearResults();
Expand Down Expand Up @@ -448,6 +450,7 @@ public void ReloadTests()
// Discover tests
LoadedTests = new TestNode(Runner.Explore(Engine.TestFilter.Empty));
AvailableCategories = GetAvailableCategories();
TestCentricTestFilter.Init();

ClearResults();
#endif
Expand Down
26 changes: 24 additions & 2 deletions src/TestModel/tests/Filter/TestCentricTestFilterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public void AllCategories_CategoriesDefinedInModel_ReturnsModelAndDefaultCategor
}

[Test]
public void ClearFilter_AllFiltersAreReset()
public void ResetFilter_AllFiltersAreReset()
{
// Arrange
var testNode = new TestNode($"<test-case id='1' name='TestA' />");
Expand All @@ -122,7 +122,7 @@ public void ClearFilter_AllFiltersAreReset()
testFilter.OutcomeFilter = new List<string>() { "Passed" };

// Act
testFilter.ClearAllFilters();
testFilter.ResetAll();

// Assert
var allCategories = testFilter.AllCategories;
Expand All @@ -136,6 +136,28 @@ public void ClearFilter_AllFiltersAreReset()
Assert.That(testFilter.TextFilter, Is.Empty);
}

[Test]
public void ResetFilter_InvokeFilterChangedEvent()
{
// Arrange
var testNode = new TestNode($"<test-case id='1' name='TestA' />");
_model.LoadedTests.Returns(testNode);
_model.AvailableCategories.Returns(new List<string>() { "Feature_1" });

bool isInvoked = false;
TestCentricTestFilter testFilter = new TestCentricTestFilter(_model, () => isInvoked = true);
testFilter.Init();
testFilter.TextFilter = "TestA";
testFilter.CategoryFilter = new List<string>() { "Feature_1" };
testFilter.OutcomeFilter = new List<string>() { "Passed" };

// Act
testFilter.ResetAll();

// Assert
Assert.That(isInvoked, Is.True);
}

private static object[] FilterByOutcomeTestCases =
{
new object[] { new List<string>() { "Passed" }, new List<string>() { "3-1000", "3-1001", "3-1010", "3-1011", "3-1012", "3-1020", "3-1022" } },
Expand Down