2
0

ThemeManagerTests.cs 10 KB

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