2
0

ThemeManagerTests.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. try
  62. {
  63. Assert.False (IsEnabled);
  64. Assert.Equal (ThemeManager.DEFAULT_THEME_NAME, ThemeManager.Theme);
  65. Enable (ConfigLocations.HardCoded);
  66. Assert.Equal ("Default", ThemeManager.Theme);
  67. ThemeManager.Theme = "Test";
  68. Assert.Equal ("Test", ThemeManager.Theme);
  69. Assert.Equal (Settings! ["Theme"].PropertyValue, ThemeManager.Theme);
  70. Assert.Equal ("Test", Settings! ["Theme"].PropertyValue);
  71. ResetToHardCodedDefaults ();
  72. Assert.Equal ("Default", ThemeManager.Theme);
  73. }
  74. finally
  75. {
  76. Disable(true);
  77. }
  78. }
  79. #endregion Tests Settings["Theme"] and ThemeManager.Theme
  80. #region Tests Settings["Themes"] and ThemeManager.Themes
  81. [Fact]
  82. public void Themes_Set_Throws_If_Not_Enabled ()
  83. {
  84. Assert.False (IsEnabled);
  85. Assert.Single (ThemeManager.Themes!);
  86. Assert.Throws<InvalidOperationException> (() => ThemeManager.Themes = new ());
  87. Assert.Single (ThemeManager.Themes!);
  88. }
  89. [Fact]
  90. public void Themes_Set_Sets_If_Enabled ()
  91. {
  92. Assert.False (IsEnabled);
  93. Enable (ConfigLocations.HardCoded);
  94. Assert.Single (ThemeManager.Themes!);
  95. // Use ConcurrentDictionary instead of a regular dictionary
  96. ThemeManager.Themes = new ConcurrentDictionary<string, ThemeScope> (
  97. new Dictionary<string, ThemeScope>
  98. {
  99. { "Default", new ThemeScope() },
  100. { "test", new ThemeScope() }
  101. },
  102. StringComparer.InvariantCultureIgnoreCase
  103. );
  104. Assert.Contains ("test", ThemeManager.Themes!);
  105. Disable (resetToHardCodedDefaults: true);
  106. }
  107. [Fact]
  108. public void Themes_Set_Throws_If_No_Default_Theme_In_Dictionary ()
  109. {
  110. Assert.False (IsEnabled);
  111. Enable (ConfigLocations.HardCoded);
  112. Assert.Single (ThemeManager.Themes!);
  113. Assert.Throws<InvalidOperationException> (
  114. () => ThemeManager.Themes = new ConcurrentDictionary<string, ThemeScope> (
  115. new Dictionary<string, ThemeScope>
  116. {
  117. { "test", new ThemeScope() }
  118. },
  119. StringComparer.InvariantCultureIgnoreCase
  120. ));
  121. Assert.Single (ThemeManager.Themes!);
  122. Disable (resetToHardCodedDefaults: true);
  123. }
  124. [Fact]
  125. public void Themes_Get () { }
  126. #endregion Tests Settings["Themes"] and ThemeManager.Themes
  127. [Fact]
  128. public void Themes_TryAdd_Adds ()
  129. {
  130. Enable (ConfigLocations.HardCoded);
  131. // Verify that the Themes dictionary contains only the Default theme
  132. Assert.Single (ThemeManager.Themes!);
  133. Assert.Contains (ThemeManager.DEFAULT_THEME_NAME, ThemeManager.Themes!);
  134. var theme = new ThemeScope ();
  135. theme.LoadHardCodedDefaults ();
  136. Assert.NotEmpty (theme);
  137. Assert.True (ThemeManager.Themes!.TryAdd ("testTheme", theme));
  138. Assert.Equal (2, ThemeManager.Themes.Count);
  139. Disable (resetToHardCodedDefaults: true);
  140. }
  141. [Fact]
  142. public void Apply_Applies ()
  143. {
  144. Assert.False (IsEnabled);
  145. Enable (ConfigLocations.HardCoded);
  146. var theme = new ThemeScope ();
  147. theme.LoadHardCodedDefaults ();
  148. Assert.NotEmpty (theme);
  149. Assert.True (ThemeManager.Themes!.TryAdd ("testTheme", theme));
  150. Assert.Equal (2, ThemeManager.Themes.Count);
  151. Assert.Equal (LineStyle.Rounded, FrameView.DefaultBorderStyle);
  152. theme ["FrameView.DefaultBorderStyle"].PropertyValue = LineStyle.Double; // default is Single
  153. ThemeManager.Theme = "testTheme";
  154. ThemeManager.Themes! [ThemeManager.Theme]!.Apply ();
  155. Assert.Equal (LineStyle.Double, FrameView.DefaultBorderStyle);
  156. Disable (resetToHardCodedDefaults: true);
  157. }
  158. [Fact]
  159. public void Theme_Reload_Consistency ()
  160. {
  161. try
  162. {
  163. Enable (ConfigLocations.HardCoded);
  164. // BUGBUG: Setting Schemes to empty array is not valid!
  165. // Create a test theme
  166. RuntimeConfig = """
  167. {
  168. "Theme": "TestTheme",
  169. "Themes": [
  170. {
  171. "TestTheme": {
  172. "Schemes": []
  173. }
  174. }
  175. ]
  176. }
  177. """;
  178. // Load the test theme
  179. ThrowOnJsonErrors = true;
  180. Load (ConfigLocations.Runtime);
  181. Assert.Equal ("TestTheme", ThemeManager.Theme);
  182. // Now reset everything and reload
  183. ResetToHardCodedDefaults ();
  184. // Verify we're back to default
  185. Assert.Equal ("Default", ThemeManager.Theme);
  186. }
  187. finally
  188. {
  189. Disable (resetToHardCodedDefaults: true);
  190. }
  191. }
  192. [Fact]
  193. public void In_Memory_Themes_Size_Is_Reasonable ()
  194. {
  195. output.WriteLine ($"Start: Color size: {(MemorySizeEstimator.EstimateSize (Color.Red))} b");
  196. output.WriteLine ($"Start: Attribute size: {(MemorySizeEstimator.EstimateSize (Attribute.Default))} b");
  197. output.WriteLine ($"Start: Base Scheme size: {(MemorySizeEstimator.EstimateSize (Scheme.GetHardCodedSchemes ()))} b");
  198. output.WriteLine ($"Start: PropertyInfo size: {(MemorySizeEstimator.EstimateSize (ConfigurationManager.Settings! ["Application.QuitKey"]))} b");
  199. ThemeScope themeScope = new ThemeScope ();
  200. output.WriteLine ($"Start: ThemeScope ({themeScope.Count}) size: {(MemorySizeEstimator.EstimateSize (themeScope))} b");
  201. themeScope.AddValue ("Schemes", Scheme.GetHardCodedSchemes ());
  202. output.WriteLine ($"Start: ThemeScope ({themeScope.Count}) size: {(MemorySizeEstimator.EstimateSize (themeScope))} b");
  203. output.WriteLine ($"Start: HardCoded Schemes ({SchemeManager.Schemes!.Count}) size: {(MemorySizeEstimator.EstimateSize (SchemeManager.Schemes!))} b");
  204. output.WriteLine ($"Start: Themes dictionary ({ThemeManager.Themes!.Count}) size: {(MemorySizeEstimator.EstimateSize (ThemeManager.Themes!)) / 1024} Kb");
  205. Enable (ConfigLocations.HardCoded);
  206. output.WriteLine ($"Enabled: Themes dictionary ({ThemeManager.Themes.Count}) size: {(MemorySizeEstimator.EstimateSize (ThemeManager.Themes!)) / 1024} Kb");
  207. Load (ConfigLocations.LibraryResources);
  208. output.WriteLine ($"After Load: Themes dictionary ({ThemeManager.Themes!.Count}) size: {(MemorySizeEstimator.EstimateSize (ThemeManager.Themes!)) / 1024} Kb");
  209. output.WriteLine ($"Total Settings Size: {(MemorySizeEstimator.EstimateSize (Settings!)) / 1024} Kb");
  210. string json = ConfigurationManager.SourcesManager?.ToJson (Settings)!;
  211. // In memory size should be less than the size of the json
  212. output.WriteLine ($"JSON size: {json.Length / 1024} Kb");
  213. Assert.True (70000 > MemorySizeEstimator.EstimateSize (ThemeManager.Themes!), $"In memory size ({(MemorySizeEstimator.EstimateSize (Settings!)) / 1024} Kb) is > json size ({json.Length / 1024} Kb)");
  214. Disable (resetToHardCodedDefaults: true);
  215. }
  216. }