BarTests.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using JetBrains.Annotations;
  2. namespace UnitTests_Parallelizable.ViewsTests;
  3. [TestSubject (typeof (Bar))]
  4. public class BarTests
  5. {
  6. [Fact]
  7. public void Constructor_Defaults ()
  8. {
  9. var bar = new Bar ();
  10. Assert.NotNull (bar);
  11. Assert.True (bar.CanFocus);
  12. Assert.IsType<DimAuto> (bar.Width);
  13. Assert.IsType<DimAuto> (bar.Height);
  14. // TOOD: more
  15. }
  16. [Fact]
  17. public void Constructor_InitializesEmpty_WhenNoShortcutsProvided ()
  18. {
  19. var bar = new Bar ();
  20. Assert.Empty (bar.SubViews);
  21. }
  22. [Fact]
  23. public void Constructor_InitializesWithShortcuts_WhenProvided ()
  24. {
  25. var shortcuts = new List<Shortcut>
  26. {
  27. new Shortcut(Key.Empty, "Command1", null, null),
  28. new Shortcut(Key.Empty, "Command2", null, null)
  29. };
  30. var bar = new Bar (shortcuts);
  31. Assert.Equal (shortcuts.Count, bar.SubViews.Count);
  32. for (int i = 0; i < shortcuts.Count; i++)
  33. {
  34. Assert.Same (shortcuts [i], bar.SubViews.ElementAt (i));
  35. }
  36. }
  37. [Fact]
  38. public void OrientationProperty_SetsCorrectly ()
  39. {
  40. var bar = new Bar ();
  41. Assert.Equal (Orientation.Horizontal, bar.Orientation); // Default value
  42. bar.Orientation = Orientation.Vertical;
  43. Assert.Equal (Orientation.Vertical, bar.Orientation);
  44. }
  45. [Fact]
  46. public void AlignmentModesProperty_SetsCorrectly ()
  47. {
  48. var bar = new Bar ();
  49. Assert.Equal (AlignmentModes.StartToEnd, bar.AlignmentModes); // Default value
  50. bar.AlignmentModes = AlignmentModes.EndToStart;
  51. Assert.Equal (AlignmentModes.EndToStart, bar.AlignmentModes);
  52. }
  53. [Fact]
  54. public void AddShortcutAt_InsertsShortcutCorrectly ()
  55. {
  56. var bar = new Bar ();
  57. var shortcut = new Shortcut (Key.Empty, "Command", null, null);
  58. bar.AddShortcutAt (0, shortcut);
  59. Assert.Contains (shortcut, bar.SubViews);
  60. }
  61. [Fact]
  62. public void RemoveShortcut_RemovesShortcutCorrectly ()
  63. {
  64. var shortcut1 = new Shortcut (Key.Empty, "Command1", null, null);
  65. var shortcut2 = new Shortcut (Key.Empty, "Command2", null, null);
  66. var bar = new Bar (new List<Shortcut> { shortcut1, shortcut2 });
  67. var removedShortcut = bar.RemoveShortcut (0);
  68. Assert.Same (shortcut1, removedShortcut);
  69. Assert.DoesNotContain (shortcut1, bar.SubViews);
  70. Assert.Contains (shortcut2, bar.SubViews);
  71. }
  72. [Fact]
  73. public void Layout_ChangesBasedOnOrientation ()
  74. {
  75. var shortcut1 = new Shortcut (Key.Empty, "Command1", null, null);
  76. var shortcut2 = new Shortcut (Key.Empty, "Command2", null, null);
  77. var bar = new Bar (new List<Shortcut> { shortcut1, shortcut2 });
  78. bar.Orientation = Orientation.Horizontal;
  79. bar.LayoutSubViews ();
  80. // TODO: Assert specific layout expectations for horizontal orientation
  81. bar.Orientation = Orientation.Vertical;
  82. bar.LayoutSubViews ();
  83. // TODO: Assert specific layout expectations for vertical orientation
  84. }
  85. [Fact]
  86. public void GetAttributeForRole_DoesNotDeferToSuperView_WhenSchemeNameIsSet ()
  87. {
  88. // This test would fail before the fix that checks SchemeName in GetAttributeForRole
  89. // StatusBar and MenuBar set SchemeName = "Menu", and should use Menu scheme
  90. // instead of deferring to parent's customized attributes
  91. var parentView = new View { SchemeName = "Base" };
  92. var statusBar = new StatusBar ();
  93. parentView.Add (statusBar);
  94. // Parent customizes attribute resolution
  95. var customAttribute = new Attribute (Color.BrightMagenta, Color.BrightGreen);
  96. parentView.GettingAttributeForRole += (sender, args) =>
  97. {
  98. if (args.Role == VisualRole.Normal)
  99. {
  100. args.Result = customAttribute;
  101. args.Handled = true;
  102. }
  103. };
  104. // StatusBar sets SchemeName = "Menu" in its constructor
  105. // Before the fix: StatusBar would defer to parent and get customAttribute (WRONG)
  106. // After the fix: StatusBar uses Menu scheme (CORRECT)
  107. var menuScheme = SchemeManager.GetHardCodedSchemes ()? ["Menu"];
  108. Assert.NotEqual (customAttribute, statusBar.GetAttributeForRole (VisualRole.Normal));
  109. Assert.Equal (menuScheme!.Normal, statusBar.GetAttributeForRole (VisualRole.Normal));
  110. statusBar.Dispose ();
  111. parentView.Dispose ();
  112. }
  113. }