ThemeScopeTests.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System.Text.Json;
  2. using static Terminal.Gui.ConfigurationManager;
  3. namespace Terminal.Gui.ConfigurationTests;
  4. public class ThemeScopeTests
  5. {
  6. public static readonly JsonSerializerOptions _jsonOptions = new ()
  7. {
  8. Converters =
  9. {
  10. //new AttributeJsonConverter (),
  11. //new ColorJsonConverter ()
  12. }
  13. };
  14. [Fact]
  15. [AutoInitShutdown (configLocation: ConfigLocations.DefaultOnly)]
  16. public void AllThemesPresent ()
  17. {
  18. Reset ();
  19. Assert.True (Themes.ContainsKey ("Default"));
  20. Assert.True (Themes.ContainsKey ("Dark"));
  21. Assert.True (Themes.ContainsKey ("Light"));
  22. }
  23. [Fact]
  24. [AutoInitShutdown (configLocation: ConfigLocations.DefaultOnly)]
  25. public void Apply_ShouldApplyUpdatedProperties ()
  26. {
  27. Reset ();
  28. Assert.NotEmpty (Themes);
  29. Alignment savedValue = Dialog.DefaultButtonAlignment;
  30. Alignment newValue = Alignment.Center != savedValue ? Alignment.Center : Alignment.Start;
  31. Themes ["Default"] ["Dialog.DefaultButtonAlignment"].PropertyValue = newValue;
  32. ThemeManager.Themes! [ThemeManager.SelectedTheme]!.Apply ();
  33. Assert.Equal (newValue, Dialog.DefaultButtonAlignment);
  34. // Replace with the savedValue to avoid failures on other unit tests that rely on the default value
  35. Themes ["Default"] ["Dialog.DefaultButtonAlignment"].PropertyValue = savedValue;
  36. ThemeManager.Themes! [ThemeManager.SelectedTheme]!.Apply ();
  37. Assert.Equal (savedValue, Dialog.DefaultButtonAlignment);
  38. }
  39. [Fact]
  40. public void GetHardCodedDefaults_ShouldSetProperties ()
  41. {
  42. Reset ();
  43. GetHardCodedDefaults ();
  44. Assert.NotEmpty (Themes);
  45. Assert.Equal ("Default", Themes.Theme);
  46. }
  47. [Fact]
  48. [AutoInitShutdown (configLocation: ConfigLocations.DefaultOnly)]
  49. public void TestSerialize_RoundTrip ()
  50. {
  51. Reset ();
  52. Dictionary<string, ThemeScope> initial = ThemeManager.Themes;
  53. string serialized = JsonSerializer.Serialize<IDictionary<string, ThemeScope>> (Themes, _jsonOptions);
  54. IDictionary<string, ThemeScope> deserialized =
  55. JsonSerializer.Deserialize<IDictionary<string, ThemeScope>> (serialized, _jsonOptions);
  56. Assert.NotEqual (initial, deserialized);
  57. Assert.Equal (deserialized.Count, initial.Count);
  58. }
  59. [Fact]
  60. [AutoInitShutdown (configLocation: ConfigLocations.DefaultOnly)]
  61. public void ThemeManager_ClassMethodsWork ()
  62. {
  63. Reset ();
  64. Assert.Equal (ThemeManager.Instance, Themes);
  65. Assert.NotEmpty (ThemeManager.Themes);
  66. ThemeManager.SelectedTheme = "foo";
  67. Assert.Equal ("foo", ThemeManager.SelectedTheme);
  68. ThemeManager.Reset ();
  69. Assert.Equal (string.Empty, ThemeManager.SelectedTheme);
  70. Assert.Empty (ThemeManager.Themes);
  71. }
  72. }