ThemeScopeTests.cs 2.8 KB

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