ThemeTests.cs 6.8 KB

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