ThemeTests.cs 6.2 KB

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