ThemeTests.cs 5.9 KB

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