ThemeTests.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. // This is needed to test only this alone
  59. Reset ();
  60. var theme = new ThemeScope ();
  61. theme ["Dialog.DefaultButtonAlignment"].PropertyValue = Alignment.End;
  62. string json = JsonSerializer.Serialize (theme, _jsonOptions);
  63. var deserialized = JsonSerializer.Deserialize<ThemeScope> (json, _jsonOptions);
  64. Assert.Equal (
  65. Alignment.End,
  66. (Alignment)deserialized ["Dialog.DefaultButtonAlignment"].PropertyValue
  67. );
  68. Reset ();
  69. }
  70. [Fact]
  71. public void TestUpdatFrom_Add ()
  72. {
  73. // arrange
  74. Reset ();
  75. var theme = new ThemeScope ();
  76. Assert.NotEmpty (theme);
  77. Assert.Equal (5, Colors.ColorSchemes.Count);
  78. theme ["ColorSchemes"].PropertyValue = Colors.ColorSchemes;
  79. Dictionary<string, ColorScheme> colorSchemes =
  80. (Dictionary<string, ColorScheme>)theme ["ColorSchemes"].PropertyValue;
  81. Assert.Equal (Colors.ColorSchemes.Count, colorSchemes.Count);
  82. var newTheme = new ThemeScope ();
  83. var colorScheme = new ColorScheme
  84. {
  85. // note: ColorScheme's can't be partial; default for each attribute
  86. // is always White/Black
  87. Normal = new Attribute (Color.Red, Color.Green),
  88. Focus = new Attribute (Color.Cyan, Color.BrightCyan),
  89. HotNormal = new Attribute (Color.Yellow, Color.BrightYellow),
  90. HotFocus = new Attribute (Color.Green, Color.BrightGreen),
  91. Disabled = new Attribute (Color.Gray, Color.DarkGray)
  92. };
  93. newTheme ["ColorSchemes"].PropertyValue = Colors.Reset ();
  94. Assert.Equal (5, Colors.ColorSchemes.Count);
  95. // add a new ColorScheme to the newTheme
  96. ((Dictionary<string, ColorScheme>)theme ["ColorSchemes"].PropertyValue) ["test"] = colorScheme;
  97. colorSchemes = (Dictionary<string, ColorScheme>)theme ["ColorSchemes"].PropertyValue;
  98. Assert.Equal (Colors.ColorSchemes.Count, colorSchemes.Count);
  99. // Act
  100. theme.Update (newTheme);
  101. // Assert
  102. colorSchemes = (Dictionary<string, ColorScheme>)theme ["ColorSchemes"].PropertyValue;
  103. Assert.Equal (colorSchemes ["Test"].Normal, colorScheme.Normal);
  104. Assert.Equal (colorSchemes ["Test"].Focus, colorScheme.Focus);
  105. Reset ();
  106. }
  107. [Fact]
  108. public void TestUpdatFrom_Change ()
  109. {
  110. // arrange
  111. Reset ();
  112. var theme = new ThemeScope ();
  113. Assert.NotEmpty (theme);
  114. var colorScheme = new ColorScheme
  115. {
  116. // note: ColorScheme's can't be partial; default for each attribute
  117. // is always White/Black
  118. Normal = new Attribute (Color.Red, Color.Green),
  119. Focus = new Attribute (Color.Cyan, Color.BrightCyan),
  120. HotNormal = new Attribute (Color.Yellow, Color.BrightYellow),
  121. HotFocus = new Attribute (Color.Green, Color.BrightGreen),
  122. Disabled = new Attribute (Color.Gray, Color.DarkGray)
  123. };
  124. theme ["ColorSchemes"].PropertyValue = Colors.Reset ();
  125. ((Dictionary<string, ColorScheme>)theme ["ColorSchemes"].PropertyValue) ["test"] = colorScheme;
  126. Dictionary<string, ColorScheme> colorSchemes =
  127. (Dictionary<string, ColorScheme>)theme ["ColorSchemes"].PropertyValue;
  128. Assert.Equal (colorScheme.Normal, colorSchemes ["Test"].Normal);
  129. Assert.Equal (colorScheme.Focus, colorSchemes ["Test"].Focus);
  130. // Change just Normal
  131. var newTheme = new ThemeScope ();
  132. var newColorScheme = new ColorScheme
  133. {
  134. Normal = new Attribute (Color.Blue, Color.BrightBlue),
  135. Focus = colorScheme.Focus,
  136. HotNormal = colorScheme.HotNormal,
  137. HotFocus = colorScheme.HotFocus,
  138. Disabled = colorScheme.Disabled
  139. };
  140. newTheme ["ColorSchemes"].PropertyValue = Colors.Reset ();
  141. ((Dictionary<string, ColorScheme>)newTheme ["ColorSchemes"].PropertyValue) ["test"] = newColorScheme;
  142. // Act
  143. theme.Update (newTheme);
  144. // Assert
  145. colorSchemes = (Dictionary<string, ColorScheme>)theme ["ColorSchemes"].PropertyValue;
  146. // Normal should have changed
  147. Assert.Equal (new Color (Color.Blue), colorSchemes ["Test"].Normal.Foreground);
  148. Assert.Equal (new Color (Color.BrightBlue), colorSchemes ["Test"].Normal.Background);
  149. Assert.Equal (new Color (Color.Cyan), colorSchemes ["Test"].Focus.Foreground);
  150. Assert.Equal (new Color (Color.BrightCyan), colorSchemes ["Test"].Focus.Background);
  151. Reset ();
  152. }
  153. }