ThemeTests.cs 7.0 KB

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