ThemeTests.cs 6.8 KB

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