SchemeTests.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #nullable enable
  2. using System.Reflection;
  3. namespace Terminal.Gui.DrawingTests;
  4. public class SchemeTests
  5. {
  6. [Fact]
  7. public void Colors_Schemes_Property_Has_Private_Setter ()
  8. {
  9. // Resharper Code Cleanup likes to remove the `private set; `
  10. // from the Schemes property. This test will fail if
  11. // that happens.
  12. PropertyInfo? property = typeof (SchemeManager).GetProperty ("Schemes");
  13. Assert.NotNull (property);
  14. Assert.NotNull (property.SetMethod);
  15. Assert.True (property.GetSetMethod (true)!.IsPrivate);
  16. }
  17. [Fact]
  18. public void New ()
  19. {
  20. var scheme = new Scheme ();
  21. var lbl = new Label ();
  22. lbl.SetScheme (scheme);
  23. lbl.Draw ();
  24. }
  25. [Fact]
  26. public void Built_Ins ()
  27. {
  28. Dictionary<string, Scheme?> schemes = SchemeManager.GetSchemes ();
  29. Assert.NotNull (schemes);
  30. Assert.Equal (5, schemes.Count);
  31. Assert.True (schemes.ContainsKey ("Base"));
  32. Assert.True (schemes.ContainsKey ("Dialog"));
  33. Assert.True (schemes.ContainsKey ("Error"));
  34. Assert.True (schemes.ContainsKey ("Menu"));
  35. Assert.True (schemes.ContainsKey ("TopLevel"));
  36. }
  37. [Fact]
  38. public void GetHardCodedSchemes_Have_Expected_Normal_Attributes ()
  39. {
  40. var schemes = Scheme.GetHardCodedSchemes ();
  41. Assert.NotNull (schemes);
  42. // Base
  43. var baseScheme = schemes! ["Base"];
  44. Assert.NotNull (baseScheme);
  45. Assert.Equal (new Attribute (StandardColor.LightBlue, StandardColor.RaisinBlack), baseScheme!.Normal);
  46. // Dialog
  47. var dialogScheme = schemes ["Dialog"];
  48. Assert.NotNull (dialogScheme);
  49. Assert.Equal (new Attribute (StandardColor.LightSkyBlue, StandardColor.OuterSpace), dialogScheme!.Normal);
  50. // Error
  51. var errorScheme = schemes ["Error"];
  52. Assert.NotNull (errorScheme);
  53. Assert.Equal (new Attribute (StandardColor.IndianRed, StandardColor.RaisinBlack), errorScheme!.Normal);
  54. // Menu (Bold style)
  55. var menuScheme = schemes ["Menu"];
  56. Assert.NotNull (menuScheme);
  57. Assert.Equal (new Attribute (StandardColor.Charcoal, StandardColor.LightBlue, TextStyle.Bold), menuScheme!.Normal);
  58. // Toplevel
  59. var toplevelScheme = schemes ["Toplevel"];
  60. Assert.NotNull (toplevelScheme);
  61. Assert.Equal (new Attribute (StandardColor.CadetBlue, StandardColor.Charcoal).ToString (), toplevelScheme!.Normal.ToString ());
  62. }
  63. [Fact]
  64. public void Built_Ins_Are_Implicit ()
  65. {
  66. Dictionary<string, Scheme?> schemes = SchemeManager.GetSchemes ();
  67. Assert.True (schemes ["Base"]!.TryGetExplicitlySetAttributeForRole (VisualRole.Normal, out _));
  68. Assert.False (schemes ["Base"]!.TryGetExplicitlySetAttributeForRole (VisualRole.HotNormal, out _));
  69. }
  70. [Fact]
  71. public void With_Same_Attributes_AreEqual ()
  72. {
  73. Attribute attr = new (Color.Red, Color.Blue, TextStyle.Bold);
  74. Scheme s1 = new (attr);
  75. Scheme s2 = new (attr);
  76. Assert.Equal (s1, s2);
  77. Assert.Equal (s1.GetHashCode (), s2.GetHashCode ());
  78. }
  79. [Fact]
  80. public void Scheme_Properties_Are_Immutable ()
  81. {
  82. Scheme scheme = new (new Attribute ("Red", "Blue"));
  83. // The following line should not compile if uncommented:
  84. // scheme.Normal = new Attribute("Green", "Yellow");
  85. // Immutability is enforced by the C# compiler for init-only properties.
  86. Assert.True (true); // This test is a placeholder for documentation purposes.
  87. }
  88. [Fact]
  89. public void ObjectInitializer_Sets_Properties ()
  90. {
  91. Scheme scheme = new ()
  92. {
  93. Normal = new Attribute ("Red", "Blue"),
  94. Focus = new Attribute ("Green", "Yellow"),
  95. HotNormal = new Attribute ("White", "Black"),
  96. HotFocus = new Attribute ("Black", "White"),
  97. Active = new Attribute ("Cyan", "Magenta"),
  98. HotActive = new Attribute ("Magenta", "Cyan"),
  99. Highlight = new Attribute ("Yellow", "Red"),
  100. Editable = new Attribute ("Blue", "Yellow"),
  101. ReadOnly = new Attribute ("Gray", "Black"),
  102. Disabled = new Attribute ("DarkGray", "White")
  103. };
  104. Assert.Equal (new Attribute ("Red", "Blue"), scheme.Normal);
  105. Assert.Equal (new Attribute ("Green", "Yellow"), scheme.Focus);
  106. Assert.Equal (new Attribute ("White", "Black"), scheme.HotNormal);
  107. Assert.Equal (new Attribute ("Black", "White"), scheme.HotFocus);
  108. Assert.Equal (new Attribute ("Cyan", "Magenta"), scheme.Active);
  109. Assert.Equal (new Attribute ("Magenta", "Cyan"), scheme.HotActive);
  110. Assert.Equal (new Attribute ("Yellow", "Red"), scheme.Highlight);
  111. Assert.Equal (new Attribute ("Blue", "Yellow"), scheme.Editable);
  112. Assert.Equal (new Attribute ("Gray", "Black"), scheme.ReadOnly);
  113. Assert.Equal (new Attribute ("DarkGray", "White"), scheme.Disabled);
  114. }
  115. [Fact]
  116. public void With_Different_Attributes_AreNotEqual ()
  117. {
  118. Scheme s1 = new (new Attribute ("Red", "Blue"));
  119. Scheme s2 = new (new Attribute ("Green", "Yellow"));
  120. Assert.NotEqual (s1, s2);
  121. Assert.NotEqual (s1.GetHashCode (), s2.GetHashCode ());
  122. }
  123. [Fact]
  124. public void Default_Constructor_Has_Default_Values ()
  125. {
  126. Scheme scheme = new ();
  127. Assert.True (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.Normal, out _));
  128. // All other roles should be implicit and derived from Normal
  129. Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.Active, out _));
  130. Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.HotNormal, out _));
  131. Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.Focus, out _));
  132. Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.HotFocus, out _));
  133. Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.Active, out _));
  134. Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.HotActive, out _));
  135. Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.Highlight, out _));
  136. Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.Editable, out _));
  137. Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.ReadOnly, out _));
  138. Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.Disabled, out _));
  139. }
  140. [Fact]
  141. public void ToString_Outputs_All_Properties ()
  142. {
  143. Scheme scheme = new (new Attribute ("Red", "Blue"));
  144. string str = scheme.ToString ();
  145. Assert.Contains ("Normal", str, StringComparison.OrdinalIgnoreCase);
  146. Assert.Contains ("HotNormal", str, StringComparison.OrdinalIgnoreCase);
  147. Assert.Contains ("Focus", str, StringComparison.OrdinalIgnoreCase);
  148. Assert.Contains ("HotFocus", str, StringComparison.OrdinalIgnoreCase);
  149. Assert.Contains ("Active", str, StringComparison.OrdinalIgnoreCase);
  150. Assert.Contains ("HotActive", str, StringComparison.OrdinalIgnoreCase);
  151. Assert.Contains ("Highlight", str, StringComparison.OrdinalIgnoreCase);
  152. Assert.Contains ("Editable", str, StringComparison.OrdinalIgnoreCase);
  153. Assert.Contains ("ReadOnly", str, StringComparison.OrdinalIgnoreCase);
  154. Assert.Contains ("Disabled", str, StringComparison.OrdinalIgnoreCase);
  155. }
  156. [Fact]
  157. public void CopyConstructor_Null_Throws ()
  158. {
  159. Assert.Throws<ArgumentNullException> (() => new Scheme (null));
  160. }
  161. [Fact]
  162. public void Is_Thread_Safe_For_Concurrent_Reads ()
  163. {
  164. Scheme scheme = new (new Attribute ("Red", "Blue"));
  165. Parallel.For (0, 1000, i =>
  166. {
  167. // All threads can safely read properties
  168. _ = scheme.Normal;
  169. _ = scheme.GetAttributeForRole (VisualRole.Focus);
  170. _ = scheme.ToString ();
  171. });
  172. }
  173. }