ThemeTests.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System.Text.Json;
  2. using UnitTests;
  3. using static Terminal.Gui.ConfigurationManager;
  4. namespace Terminal.Gui.ConfigurationTests;
  5. public class ThemeTests
  6. {
  7. public static readonly JsonSerializerOptions _jsonOptions = new ()
  8. {
  9. Converters = { new AttributeJsonConverter (), new ColorJsonConverter () }
  10. };
  11. [Fact]
  12. [AutoInitShutdown (configLocation: ConfigLocations.Default)]
  13. public void TestApply ()
  14. {
  15. Reset ();
  16. var theme = new ThemeScope ();
  17. Assert.NotEmpty (theme);
  18. Themes.Add ("testTheme", theme);
  19. Assert.Equal (LineStyle.Single, FrameView.DefaultBorderStyle);
  20. theme ["FrameView.DefaultBorderStyle"].PropertyValue = LineStyle.Double; // default is Single
  21. Themes.Theme = "testTheme";
  22. Themes! [ThemeManager.SelectedTheme]!.Apply ();
  23. Assert.Equal (LineStyle.Double, FrameView.DefaultBorderStyle);
  24. Reset ();
  25. }
  26. [Fact]
  27. [AutoInitShutdown (configLocation: ConfigLocations.Default)]
  28. public void TestApply_UpdatesColors ()
  29. {
  30. // Arrange
  31. Reset ();
  32. Assert.False (Colors.ColorSchemes.ContainsKey ("test"));
  33. var theme = new ThemeScope ();
  34. Assert.NotEmpty (theme);
  35. Themes.Add ("testTheme", theme);
  36. var colorScheme = new ColorScheme { Normal = new Attribute (Color.Red, Color.Green) };
  37. theme ["ColorSchemes"].PropertyValue = new Dictionary<string, ColorScheme> { { "test", colorScheme } };
  38. Assert.Equal (
  39. new Color (Color.Red),
  40. ((Dictionary<string, ColorScheme>)theme ["ColorSchemes"].PropertyValue) ["test"].Normal.Foreground
  41. );
  42. Assert.Equal (
  43. new Color (Color.Green),
  44. ((Dictionary<string, ColorScheme>)theme ["ColorSchemes"].PropertyValue) ["test"].Normal.Background
  45. );
  46. // Act
  47. Themes.Theme = "testTheme";
  48. Themes! [ThemeManager.SelectedTheme]!.Apply ();
  49. // Assert
  50. ColorScheme updatedScheme = Colors.ColorSchemes ["test"];
  51. Assert.Equal (new Color (Color.Red), updatedScheme.Normal.Foreground);
  52. Assert.Equal (new Color (Color.Green), updatedScheme.Normal.Background);
  53. // remove test ColorScheme from Colors to avoid failures on others unit tests with ColorScheme
  54. Colors.ColorSchemes.Remove ("test");
  55. Assert.Equal (5, Colors.ColorSchemes.Count);
  56. Reset ();
  57. }
  58. [Fact]
  59. public void TestSerialize_RoundTrip ()
  60. {
  61. // This is needed to test only this alone
  62. Reset ();
  63. var theme = new ThemeScope ();
  64. theme ["Dialog.DefaultButtonAlignment"].PropertyValue = Alignment.End;
  65. string json = JsonSerializer.Serialize (theme, _jsonOptions);
  66. var deserialized = JsonSerializer.Deserialize<ThemeScope> (json, _jsonOptions);
  67. Assert.Equal (
  68. Alignment.End,
  69. (Alignment)deserialized ["Dialog.DefaultButtonAlignment"].PropertyValue
  70. );
  71. Reset ();
  72. }
  73. [Fact]
  74. public void TestUpdatFrom_Add ()
  75. {
  76. // arrange
  77. Reset ();
  78. var theme = new ThemeScope ();
  79. Assert.NotEmpty (theme);
  80. Assert.Equal (5, Colors.ColorSchemes.Count);
  81. theme ["ColorSchemes"].PropertyValue = Colors.ColorSchemes;
  82. Dictionary<string, ColorScheme> colorSchemes =
  83. (Dictionary<string, ColorScheme>)theme ["ColorSchemes"].PropertyValue;
  84. Assert.Equal (Colors.ColorSchemes.Count, colorSchemes.Count);
  85. var newTheme = new ThemeScope ();
  86. var colorScheme = new ColorScheme
  87. {
  88. // note: ColorScheme's can't be partial; default for each attribute
  89. // is always White/Black
  90. Normal = new Attribute (Color.Red, Color.Green),
  91. Focus = new Attribute (Color.Cyan, Color.BrightCyan),
  92. HotNormal = new Attribute (Color.Yellow, Color.BrightYellow),
  93. HotFocus = new Attribute (Color.Green, Color.BrightGreen),
  94. Disabled = new Attribute (Color.Gray, Color.DarkGray)
  95. };
  96. newTheme ["ColorSchemes"].PropertyValue = Colors.Reset ();
  97. Assert.Equal (5, Colors.ColorSchemes.Count);
  98. // add a new ColorScheme to the newTheme
  99. ((Dictionary<string, ColorScheme>)theme ["ColorSchemes"].PropertyValue) ["test"] = colorScheme;
  100. colorSchemes = (Dictionary<string, ColorScheme>)theme ["ColorSchemes"].PropertyValue;
  101. Assert.Equal (Colors.ColorSchemes.Count, colorSchemes.Count);
  102. // Act
  103. theme.Update (newTheme);
  104. // Assert
  105. colorSchemes = (Dictionary<string, ColorScheme>)theme ["ColorSchemes"].PropertyValue;
  106. Assert.Equal (colorSchemes ["Test"].Normal, colorScheme.Normal);
  107. Assert.Equal (colorSchemes ["Test"].Focus, colorScheme.Focus);
  108. Reset ();
  109. }
  110. [Fact]
  111. public void TestUpdatFrom_Change ()
  112. {
  113. // arrange
  114. Reset ();
  115. var theme = new ThemeScope ();
  116. Assert.NotEmpty (theme);
  117. var colorScheme = new ColorScheme
  118. {
  119. // note: ColorScheme's can't be partial; default for each attribute
  120. // is always White/Black
  121. Normal = new Attribute (Color.Red, Color.Green),
  122. Focus = new Attribute (Color.Cyan, Color.BrightCyan),
  123. HotNormal = new Attribute (Color.Yellow, Color.BrightYellow),
  124. HotFocus = new Attribute (Color.Green, Color.BrightGreen),
  125. Disabled = new Attribute (Color.Gray, Color.DarkGray)
  126. };
  127. theme ["ColorSchemes"].PropertyValue = Colors.Reset ();
  128. ((Dictionary<string, ColorScheme>)theme ["ColorSchemes"].PropertyValue) ["test"] = colorScheme;
  129. Dictionary<string, ColorScheme> colorSchemes =
  130. (Dictionary<string, ColorScheme>)theme ["ColorSchemes"].PropertyValue;
  131. Assert.Equal (colorScheme.Normal, colorSchemes ["Test"].Normal);
  132. Assert.Equal (colorScheme.Focus, colorSchemes ["Test"].Focus);
  133. // Change just Normal
  134. var newTheme = new ThemeScope ();
  135. var newColorScheme = new ColorScheme
  136. {
  137. Normal = new Attribute (Color.Blue, Color.BrightBlue),
  138. Focus = colorScheme.Focus,
  139. HotNormal = colorScheme.HotNormal,
  140. HotFocus = colorScheme.HotFocus,
  141. Disabled = colorScheme.Disabled
  142. };
  143. newTheme ["ColorSchemes"].PropertyValue = Colors.Reset ();
  144. ((Dictionary<string, ColorScheme>)newTheme ["ColorSchemes"].PropertyValue) ["test"] = newColorScheme;
  145. // Act
  146. theme.Update (newTheme);
  147. // Assert
  148. colorSchemes = (Dictionary<string, ColorScheme>)theme ["ColorSchemes"].PropertyValue;
  149. // Normal should have changed
  150. Assert.Equal (new Color (Color.Blue), colorSchemes ["Test"].Normal.Foreground);
  151. Assert.Equal (new Color (Color.BrightBlue), colorSchemes ["Test"].Normal.Background);
  152. Assert.Equal (new Color (Color.Cyan), colorSchemes ["Test"].Focus.Foreground);
  153. Assert.Equal (new Color (Color.BrightCyan), colorSchemes ["Test"].Focus.Background);
  154. Reset ();
  155. }
  156. }