ThemeTests.cs 7.0 KB

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