ThemeScopeTests.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System.Collections.Concurrent;
  2. using System.Text.Json;
  3. using static Terminal.Gui.Configuration.ConfigurationManager;
  4. namespace Terminal.Gui.ConfigurationTests;
  5. public class ThemeScopeTests
  6. {
  7. [Fact]
  8. public void Load_AllThemesPresent ()
  9. {
  10. Enable (ConfigLocations.HardCoded);
  11. Load (ConfigLocations.All);
  12. Assert.True (ThemeManager.Themes!.ContainsKey ("Default"));
  13. Assert.True (ThemeManager.Themes.ContainsKey ("Dark"));
  14. Assert.True (ThemeManager.Themes.ContainsKey ("Light"));
  15. Disable (true);
  16. }
  17. [Fact]
  18. public void Apply_ShouldApplyUpdatedProperties ()
  19. {
  20. Enable (ConfigLocations.HardCoded);
  21. Assert.NotEmpty (ThemeManager.Themes!);
  22. Alignment savedButtonAlignment = Dialog.DefaultButtonAlignment;
  23. Alignment newButtonAlignment = Alignment.Center != savedButtonAlignment ? Alignment.Center : Alignment.Start;
  24. ThemeManager.GetCurrentTheme () ["Dialog.DefaultButtonAlignment"].PropertyValue = newButtonAlignment;
  25. LineStyle savedBorderStyle = Dialog.DefaultBorderStyle;
  26. LineStyle newBorderStyle = LineStyle.HeavyDotted;
  27. ThemeManager.GetCurrentTheme () ["Dialog.DefaultBorderStyle"].PropertyValue = newBorderStyle;
  28. ThemeManager.Themes! [ThemeManager.Theme]!.Apply ();
  29. Assert.Equal (newButtonAlignment, Dialog.DefaultButtonAlignment);
  30. Assert.Equal (newBorderStyle, Dialog.DefaultBorderStyle);
  31. // Replace with the savedValue to avoid failures on other unit tests that rely on the default value
  32. ThemeManager.GetCurrentTheme () ["Dialog.DefaultButtonAlignment"].PropertyValue = savedButtonAlignment;
  33. ThemeManager.GetCurrentTheme () ["Dialog.DefaultBorderStyle"].PropertyValue = savedBorderStyle;
  34. ThemeManager.GetCurrentTheme ().Apply ();
  35. Assert.Equal (savedButtonAlignment, Dialog.DefaultButtonAlignment);
  36. Assert.Equal (savedBorderStyle, Dialog.DefaultBorderStyle);
  37. Disable (true);
  38. }
  39. [Fact]
  40. public void UpdateToHardCodedDefaults_Resets_Config_Does_Not_Apply ()
  41. {
  42. Enable (ConfigLocations.HardCoded);
  43. Load (ConfigLocations.LibraryResources);
  44. Assert.Equal ("Default", ThemeManager.Theme);
  45. ThemeManager.Theme = "Dark";
  46. Assert.Equal ("Dark", ThemeManager.Theme);
  47. Apply ();
  48. Assert.Equal ("Dark", ThemeManager.Theme);
  49. // Act
  50. ThemeManager.ResetToHardCodedDefaults ();
  51. Assert.Equal ("Default", ThemeManager.Theme);
  52. Disable (true);
  53. }
  54. [Fact]
  55. public void Serialize_Themes_RoundTrip ()
  56. {
  57. Enable (ConfigLocations.HardCoded);
  58. IDictionary<string, ThemeScope> initial = ThemeManager.Themes;
  59. string serialized = JsonSerializer.Serialize (ThemeManager.Themes, SerializerContext.Options);
  60. ConcurrentDictionary<string, ThemeScope> deserialized =
  61. JsonSerializer.Deserialize<ConcurrentDictionary<string, ThemeScope>> (serialized, SerializerContext.Options);
  62. Assert.NotEqual (initial, deserialized);
  63. Assert.Equal (deserialized.Count, initial!.Count);
  64. Disable (true);
  65. }
  66. [Fact]
  67. public void Serialize_New_RoundTrip ()
  68. {
  69. Enable (ConfigLocations.HardCoded);
  70. var theme = new ThemeScope ();
  71. theme.LoadHardCodedDefaults ();
  72. theme ["Dialog.DefaultButtonAlignment"].PropertyValue = Alignment.End;
  73. string json = JsonSerializer.Serialize (theme, SerializerContext.Options);
  74. var deserialized = JsonSerializer.Deserialize<ThemeScope> (json, SerializerContext.Options);
  75. Assert.Equal (
  76. Alignment.End,
  77. (Alignment)deserialized ["Dialog.DefaultButtonAlignment"].PropertyValue!
  78. );
  79. Disable (true);
  80. }
  81. }