PageRenderTime 54ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/UnitTests/GitUI.Tests/UserControls/FilterToolBarTests.cs

https://github.com/gitextensions/gitextensions
C# | 245 lines | 205 code | 40 blank | 0 comment | 0 complexity | 28b354acb130f07c84d1dbba2dd629f6 MD5 | raw file
Possible License(s): GPL-3.0
  1. using System;
  2. using System.ComponentModel;
  3. using System.Threading;
  4. using FluentAssertions;
  5. using GitCommands;
  6. using GitUI;
  7. using GitUI.UserControls;
  8. using GitUI.UserControls.RevisionGrid;
  9. using GitUIPluginInterfaces;
  10. using NSubstitute;
  11. using NUnit.Framework;
  12. namespace GitUITests.UserControls
  13. {
  14. [SetCulture("en-US")]
  15. [SetUICulture("en-US")]
  16. [TestFixture]
  17. [Apartment(ApartmentState.STA)]
  18. public class FilterToolBarTests
  19. {
  20. private bool _originalShowFirstParent;
  21. private bool _originalShowReflogReferences;
  22. private FilterToolBar _filterToolBar;
  23. private IGitModule _gitModule;
  24. private IRevisionGridFilter _revisionGridFilter;
  25. [SetUp]
  26. public void Setup()
  27. {
  28. _originalShowFirstParent = AppSettings.ShowFirstParent;
  29. _originalShowReflogReferences = AppSettings.ShowReflogReferences;
  30. AppSettings.ShowFirstParent = false;
  31. AppSettings.ShowReflogReferences = false;
  32. _gitModule = Substitute.For<IGitModule>();
  33. _revisionGridFilter = Substitute.For<IRevisionGridFilter>();
  34. _filterToolBar = new();
  35. _filterToolBar.Bind(() => _gitModule, _revisionGridFilter);
  36. }
  37. [TearDown]
  38. public void TearDown()
  39. {
  40. AppSettings.ShowFirstParent = _originalShowFirstParent;
  41. AppSettings.ShowReflogReferences = _originalShowReflogReferences;
  42. }
  43. [Test]
  44. public void RevisionGridFilter_should_throw_if_unbound()
  45. {
  46. ((Action)(() => _ = new FilterToolBar().GetTestAccessor().RevisionGridFilter))
  47. .Should().Throw<InvalidOperationException>();
  48. }
  49. [Test]
  50. public void GetModule_should_throw_if_unbound()
  51. {
  52. ((Action)(() => _ = new FilterToolBar().GetTestAccessor().GetModule()))
  53. .Should().Throw<InvalidOperationException>();
  54. }
  55. [Test]
  56. public void ApplyBranchFilter_should_invoke_RevisionGridFilter_with_no_branches()
  57. {
  58. _filterToolBar.GetTestAccessor().tscboBranchFilter.Items.Count.Should().Be(0);
  59. _filterToolBar.GetTestAccessor().ApplyCustomBranchFilter();
  60. _revisionGridFilter.Received().SetAndApplyBranchFilter(string.Empty);
  61. _filterToolBar.GetTestAccessor()._isApplyingFilter.Should().BeFalse();
  62. }
  63. [Test]
  64. public void ApplyBranchFilter_should_invoke_RevisionGridFilter_with_NoResultsFound()
  65. {
  66. _filterToolBar.GetTestAccessor().tscboBranchFilter.Items.AddRange(new[] { "one", "two" });
  67. _filterToolBar.GetTestAccessor().tscboBranchFilter.Text = TranslatedStrings.NoResultsFound;
  68. _filterToolBar.GetTestAccessor().ApplyCustomBranchFilter();
  69. _revisionGridFilter.Received().SetAndApplyBranchFilter(string.Empty);
  70. _filterToolBar.GetTestAccessor()._isApplyingFilter.Should().BeFalse();
  71. }
  72. [Test]
  73. public void ApplyBranchFilter_should_invoke_RevisionGridFilter_with_branches()
  74. {
  75. _filterToolBar.GetTestAccessor().tscboBranchFilter.Items.AddRange(new[] { "one", "two" });
  76. _filterToolBar.GetTestAccessor().tscboBranchFilter.Text = "on";
  77. _filterToolBar.GetTestAccessor().ApplyCustomBranchFilter();
  78. _revisionGridFilter.Received().SetAndApplyBranchFilter("on");
  79. _filterToolBar.GetTestAccessor()._isApplyingFilter.Should().BeFalse();
  80. }
  81. [TestCase(false, false, false, false)]
  82. [TestCase(true, true, true, true)]
  83. public void ApplyRevisionFilter_should_invoke_RevisionGridFilter(bool byCommit, bool byCommitter, bool byAuthor, bool byDiffContains)
  84. {
  85. _filterToolBar.GetTestAccessor().tstxtRevisionFilter.Text = "on";
  86. _filterToolBar.GetTestAccessor().tsmiCommitFilter.Checked = byCommit;
  87. _filterToolBar.GetTestAccessor().tsmiCommitterFilter.Checked = byCommitter;
  88. _filterToolBar.GetTestAccessor().tsmiAuthorFilter.Checked = byAuthor;
  89. _filterToolBar.GetTestAccessor().tsmiDiffContainsFilter.Checked = byDiffContains;
  90. _filterToolBar.GetTestAccessor().ApplyRevisionFilter();
  91. _revisionGridFilter.Received().SetAndApplyRevisionFilter(
  92. Arg.Is<RevisionFilter>(revisionFilter => AssertFilter(revisionFilter, "on", byCommit, byCommitter, byAuthor, byDiffContains)));
  93. _filterToolBar.GetTestAccessor()._isApplyingFilter.Should().BeFalse();
  94. }
  95. [Test]
  96. public void SetBranchFilter_should_reset_text()
  97. {
  98. _filterToolBar.GetTestAccessor().tscboBranchFilter.Text = "bla";
  99. _filterToolBar.SetBranchFilter(string.Empty);
  100. _filterToolBar.GetTestAccessor().tscboBranchFilter.Text.Should().Be(string.Empty);
  101. }
  102. [TestCase(null, "")]
  103. [TestCase("", "")]
  104. [TestCase("foo", "foo")]
  105. public void SetBranchFilter(string given, string expected)
  106. {
  107. _filterToolBar.GetTestAccessor().tscboBranchFilter.Items.AddRange(new[] { "one", "two" });
  108. _filterToolBar.SetBranchFilter(given);
  109. _filterToolBar.GetTestAccessor().tscboBranchFilter.Text.Should().Be(expected);
  110. _revisionGridFilter.Received().SetAndApplyBranchFilter(expected);
  111. }
  112. [TestCase(null, "")]
  113. [TestCase("", "")]
  114. public void SetRevisionFilter_when_empty(string given, string expected)
  115. {
  116. _filterToolBar.SetRevisionFilter(given);
  117. _filterToolBar.GetTestAccessor().tstxtRevisionFilter.Text.Should().Be(expected);
  118. _revisionGridFilter.DidNotReceive().SetAndApplyRevisionFilter(Arg.Any<RevisionFilter>());
  119. }
  120. [TestCase("foo", "foo")]
  121. public void SetRevisionFilter(string given, string expected)
  122. {
  123. _filterToolBar.SetRevisionFilter(given);
  124. _filterToolBar.GetTestAccessor().tstxtRevisionFilter.Text.Should().Be(expected);
  125. _revisionGridFilter.Received().SetAndApplyRevisionFilter(
  126. Arg.Is<RevisionFilter>(revisionFilter => AssertFilter(revisionFilter, expected,
  127. _filterToolBar.GetTestAccessor().tsmiCommitFilter.Checked,
  128. _filterToolBar.GetTestAccessor().tsmiCommitterFilter.Checked,
  129. _filterToolBar.GetTestAccessor().tsmiAuthorFilter.Checked,
  130. _filterToolBar.GetTestAccessor().tsmiDiffContainsFilter.Checked)));
  131. }
  132. private static bool AssertFilter(RevisionFilter receivedRevisionFilter, string filter, bool byCommit, bool byCommitter, bool byAuthor, bool byDiffContains)
  133. {
  134. receivedRevisionFilter.Should().NotBeNull();
  135. receivedRevisionFilter.Text.Should().Be(filter);
  136. receivedRevisionFilter.FilterByAuthor.Should().Be(byAuthor);
  137. receivedRevisionFilter.FilterByCommit.Should().Be(byCommit);
  138. receivedRevisionFilter.FilterByCommitter.Should().Be(byCommitter);
  139. receivedRevisionFilter.FilterByDiffContent.Should().Be(byDiffContains);
  140. return true;
  141. }
  142. [TestCase(false)]
  143. [TestCase(true)]
  144. public void ShowFirstParent_should_be_bound_via_FilterChanged(bool settingValue)
  145. {
  146. bool original = AppSettings.ShowFirstParent;
  147. try
  148. {
  149. AppSettings.ShowFirstParent = settingValue;
  150. _revisionGridFilter.FilterChanged += Raise.EventWith(_revisionGridFilter, new FilterChangedEventArgs(new()));
  151. _filterToolBar.GetTestAccessor().tsmiShowFirstParent.Checked.Should().Be(settingValue);
  152. }
  153. finally
  154. {
  155. AppSettings.ShowFirstParent = original;
  156. }
  157. }
  158. [Test]
  159. public void ShowFirstParent_should_invoke_ToggleShowFirstParent()
  160. {
  161. _filterToolBar.GetTestAccessor().tsmiShowFirstParent.PerformClick();
  162. _revisionGridFilter.Received(1).ToggleShowFirstParent();
  163. }
  164. [TestCase(false)]
  165. [TestCase(true)]
  166. public void ShowReflogs_should_be_bound_via_FilterChanged(bool settingValue)
  167. {
  168. bool original = AppSettings.ShowReflogReferences;
  169. try
  170. {
  171. AppSettings.ShowReflogReferences = settingValue;
  172. _revisionGridFilter.FilterChanged += Raise.EventWith(_revisionGridFilter, new FilterChangedEventArgs(new()));
  173. _filterToolBar.GetTestAccessor().tsmiShowReflogs.Checked.Should().Be(settingValue);
  174. }
  175. finally
  176. {
  177. AppSettings.ShowReflogReferences = original;
  178. }
  179. }
  180. [Test]
  181. public void ShowReflogs_should_invoke_ToggleShowReflogReferences()
  182. {
  183. _filterToolBar.GetTestAccessor().tsmiShowReflogs.PerformClick();
  184. _revisionGridFilter.Received(1).ToggleShowReflogReferences();
  185. }
  186. [Test]
  187. public void ShowBranches_ShowAll_should_invoke_ShowAllBranches()
  188. {
  189. _filterToolBar.GetTestAccessor().tsmiShowBranchesAll.PerformClick();
  190. _revisionGridFilter.Received(1).ShowAllBranches();
  191. }
  192. [Test]
  193. public void ShowBranches_ShowCurrent_should_invoke_ShowCurrentBranchOnly()
  194. {
  195. _filterToolBar.GetTestAccessor().tsmiShowBranchesCurrent.PerformClick();
  196. _revisionGridFilter.Received(1).ShowCurrentBranchOnly();
  197. }
  198. [Test]
  199. public void ShowBranches_ShowFiltered_should_invoke_ShowFilteredBranches()
  200. {
  201. _filterToolBar.GetTestAccessor().tsmiShowBranchesFiltered.PerformClick();
  202. _revisionGridFilter.Received(1).ShowFilteredBranches();
  203. }
  204. }
  205. }