ThemeManagerTests.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. #nullable enable
  2. using System.Collections.Concurrent;
  3. using System.Diagnostics.Metrics;
  4. using System.Text;
  5. using Xunit.Abstractions;
  6. using static Terminal.Gui.Configuration.ConfigurationManager;
  7. namespace Terminal.Gui.ConfigurationTests;
  8. public class ThemeManagerTests (ITestOutputHelper output)
  9. {
  10. [Fact]
  11. public void ResetToCurrentValues_Adds_Default_Theme ()
  12. {
  13. try
  14. {
  15. Enable (ConfigLocations.HardCoded);
  16. Assert.NotEmpty (ThemeManager.Themes!);
  17. ThemeManager.UpdateToCurrentValues ();
  18. Assert.NotEmpty (ThemeManager.Themes!);
  19. // Default theme exists
  20. Assert.NotNull (ThemeManager.Themes? [ThemeManager.DEFAULT_THEME_NAME]);
  21. }
  22. finally
  23. {
  24. Disable (resetToHardCodedDefaults: true);
  25. }
  26. }
  27. // ResetToCurrentValues
  28. // OnThemeChanged
  29. #region Tests Settings["Theme"] and ThemeManager.Theme
  30. [Fact]
  31. public void Theme_Settings_Theme_Equals_ThemeManager_Theme ()
  32. {
  33. Assert.False (IsEnabled);
  34. Assert.Equal (Settings! ["Theme"].PropertyValue, ThemeManager.Theme);
  35. Assert.Equal (ThemeManager.DEFAULT_THEME_NAME, ThemeManager.Theme);
  36. }
  37. [Fact]
  38. public void Theme_Enabled_Settings_Theme_Equals_ThemeManager_Theme ()
  39. {
  40. Assert.False (IsEnabled);
  41. Enable (ConfigLocations.HardCoded);
  42. Assert.Equal (Settings! ["Theme"].PropertyValue, ThemeManager.Theme);
  43. Assert.Equal (ThemeManager.DEFAULT_THEME_NAME, ThemeManager.Theme);
  44. Disable (resetToHardCodedDefaults: true);
  45. }
  46. [Fact]
  47. public void Theme_Set_Sets ()
  48. {
  49. Assert.False (IsEnabled);
  50. Enable (ConfigLocations.HardCoded);
  51. Assert.Equal (ThemeManager.DEFAULT_THEME_NAME, ThemeManager.Theme);
  52. ThemeManager.Theme = "Test";
  53. Assert.Equal ("Test", ThemeManager.Theme);
  54. Assert.Equal (Settings! ["Theme"].PropertyValue, ThemeManager.Theme);
  55. Assert.Equal ("Test", Settings! ["Theme"].PropertyValue);
  56. Disable (resetToHardCodedDefaults: true);
  57. }
  58. [Fact]
  59. public void Theme_ResetToHardCodedDefaults_Sets_To_Default ()
  60. {
  61. Assert.False (IsEnabled);
  62. Assert.Equal (ThemeManager.DEFAULT_THEME_NAME, ThemeManager.Theme);
  63. Enable (ConfigLocations.HardCoded);
  64. Assert.Equal ("Default", ThemeManager.Theme);
  65. ThemeManager.Theme = "Test";
  66. Assert.Equal ("Test", ThemeManager.Theme);
  67. Assert.Equal (Settings! ["Theme"].PropertyValue, ThemeManager.Theme);
  68. Assert.Equal ("Test", Settings! ["Theme"].PropertyValue);
  69. ResetToHardCodedDefaults ();
  70. Assert.Equal ("Default", ThemeManager.Theme);
  71. Disable ();
  72. }
  73. #endregion Tests Settings["Theme"] and ThemeManager.Theme
  74. #region Tests Settings["Themes"] and ThemeManager.Themes
  75. [Fact]
  76. public void Themes_Set_Throws_If_Not_Enabled ()
  77. {
  78. Assert.False (IsEnabled);
  79. Assert.Single (ThemeManager.Themes!);
  80. Assert.Throws<InvalidOperationException> (() => ThemeManager.Themes = new ());
  81. Assert.Single (ThemeManager.Themes!);
  82. }
  83. [Fact]
  84. public void Themes_Set_Sets_If_Enabled ()
  85. {
  86. Assert.False (IsEnabled);
  87. Enable (ConfigLocations.HardCoded);
  88. Assert.Single (ThemeManager.Themes!);
  89. // Use ConcurrentDictionary instead of a regular dictionary
  90. ThemeManager.Themes = new ConcurrentDictionary<string, ThemeScope> (
  91. new Dictionary<string, ThemeScope>
  92. {
  93. { "Default", new ThemeScope() },
  94. { "test", new ThemeScope() }
  95. },
  96. StringComparer.InvariantCultureIgnoreCase
  97. );
  98. Assert.Contains ("test", ThemeManager.Themes!);
  99. Disable (resetToHardCodedDefaults: true);
  100. }
  101. [Fact]
  102. public void Themes_Set_Throws_If_No_Default_Theme_In_Dictionary ()
  103. {
  104. Assert.False (IsEnabled);
  105. Enable (ConfigLocations.HardCoded);
  106. Assert.Single (ThemeManager.Themes!);
  107. Assert.Throws<InvalidOperationException> (
  108. () => ThemeManager.Themes = new ConcurrentDictionary<string, ThemeScope> (
  109. new Dictionary<string, ThemeScope>
  110. {
  111. { "test", new ThemeScope() }
  112. },
  113. StringComparer.InvariantCultureIgnoreCase
  114. ));
  115. Assert.Single (ThemeManager.Themes!);
  116. Disable (resetToHardCodedDefaults: true);
  117. }
  118. [Fact]
  119. public void Themes_Get () { }
  120. #endregion Tests Settings["Themes"] and ThemeManager.Themes
  121. [Fact]
  122. public void Themes_TryAdd_Adds ()
  123. {
  124. Enable (ConfigLocations.HardCoded);
  125. // Verify that the Themes dictionary contains only the Default theme
  126. Assert.Single (ThemeManager.Themes!);
  127. Assert.Contains (ThemeManager.DEFAULT_THEME_NAME, ThemeManager.Themes!);
  128. var theme = new ThemeScope ();
  129. theme.LoadHardCodedDefaults ();
  130. Assert.NotEmpty (theme);
  131. Assert.True (ThemeManager.Themes!.TryAdd ("testTheme", theme));
  132. Assert.Equal (2, ThemeManager.Themes.Count);
  133. Disable (resetToHardCodedDefaults: true);
  134. }
  135. [Fact]
  136. public void Apply_Applies ()
  137. {
  138. Assert.False (IsEnabled);
  139. Enable (ConfigLocations.HardCoded);
  140. var theme = new ThemeScope ();
  141. theme.LoadHardCodedDefaults ();
  142. Assert.NotEmpty (theme);
  143. Assert.True (ThemeManager.Themes!.TryAdd ("testTheme", theme));
  144. Assert.Equal (2, ThemeManager.Themes.Count);
  145. Assert.Equal (LineStyle.Rounded, FrameView.DefaultBorderStyle);
  146. theme ["FrameView.DefaultBorderStyle"].PropertyValue = LineStyle.Double; // default is Single
  147. ThemeManager.Theme = "testTheme";
  148. ThemeManager.Themes! [ThemeManager.Theme]!.Apply ();
  149. Assert.Equal (LineStyle.Double, FrameView.DefaultBorderStyle);
  150. Disable (resetToHardCodedDefaults: true);
  151. }
  152. [Fact]
  153. public void Theme_Reload_Consistency ()
  154. {
  155. try
  156. {
  157. Enable (ConfigLocations.HardCoded);
  158. // BUGBUG: Setting Schemes to empty array is not valid!
  159. // Create a test theme
  160. RuntimeConfig = """
  161. {
  162. "Theme": "TestTheme",
  163. "Themes": [
  164. {
  165. "TestTheme": {
  166. "Schemes": []
  167. }
  168. }
  169. ]
  170. }
  171. """;
  172. // Load the test theme
  173. ThrowOnJsonErrors = true;
  174. Load (ConfigLocations.Runtime);
  175. Assert.Equal ("TestTheme", ThemeManager.Theme);
  176. // Now reset everything and reload
  177. ResetToCurrentValues ();
  178. // Verify we're back to default
  179. Assert.Equal ("Default", ThemeManager.Theme);
  180. }
  181. finally
  182. {
  183. Disable (resetToHardCodedDefaults: true);
  184. }
  185. }
  186. [Fact]
  187. public void In_Memory_Themes_Size_Is_Reasonable ()
  188. {
  189. output.WriteLine ($"Start: Color size: {(MemorySizeEstimator.EstimateSize (Color.Red))} b");
  190. output.WriteLine ($"Start: Attribute size: {(MemorySizeEstimator.EstimateSize (Attribute.Default))} b");
  191. output.WriteLine ($"Start: Base Scheme size: {(MemorySizeEstimator.EstimateSize (Scheme.GetHardCodedSchemes ()))} b");
  192. output.WriteLine ($"Start: PropertyInfo size: {(MemorySizeEstimator.EstimateSize (ConfigurationManager.Settings! ["Application.QuitKey"]))} b");
  193. ThemeScope themeScope = new ThemeScope ();
  194. output.WriteLine ($"Start: ThemeScope ({themeScope.Count}) size: {(MemorySizeEstimator.EstimateSize (themeScope))} b");
  195. themeScope.AddValue ("Schemes", Scheme.GetHardCodedSchemes ());
  196. output.WriteLine ($"Start: ThemeScope ({themeScope.Count}) size: {(MemorySizeEstimator.EstimateSize (themeScope))} b");
  197. output.WriteLine ($"Start: HardCoded Schemes ({SchemeManager.Schemes!.Count}) size: {(MemorySizeEstimator.EstimateSize (SchemeManager.Schemes!))} b");
  198. output.WriteLine ($"Start: Themes dictionary ({ThemeManager.Themes!.Count}) size: {(MemorySizeEstimator.EstimateSize (ThemeManager.Themes!)) / 1024} Kb");
  199. Enable (ConfigLocations.HardCoded);
  200. output.WriteLine ($"Enabled: Themes dictionary ({ThemeManager.Themes.Count}) size: {(MemorySizeEstimator.EstimateSize (ThemeManager.Themes!)) / 1024} Kb");
  201. Load (ConfigLocations.LibraryResources);
  202. output.WriteLine ($"After Load: Themes dictionary ({ThemeManager.Themes!.Count}) size: {(MemorySizeEstimator.EstimateSize (ThemeManager.Themes!)) / 1024} Kb");
  203. output.WriteLine ($"Total Settings Size: {(MemorySizeEstimator.EstimateSize (Settings!)) / 1024} Kb");
  204. string json = ConfigurationManager.SourcesManager?.ToJson (Settings)!;
  205. // In memory size should be less than the size of the json
  206. output.WriteLine ($"JSON size: {json.Length / 1024} Kb");
  207. Assert.True (70000 > MemorySizeEstimator.EstimateSize (ThemeManager.Themes!), $"In memory size ({(MemorySizeEstimator.EstimateSize (Settings!)) / 1024} Kb) is > json size ({json.Length / 1024} Kb)");
  208. Disable (resetToHardCodedDefaults: true);
  209. }
  210. }