SchemeJsonConverterTests.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System.Text.Json;
  2. namespace UnitTests_Parallelizable.ConfigurationTests;
  3. public class SchemeJsonConverterTests
  4. {
  5. //string json = @"
  6. // {
  7. // ""Schemes"": {
  8. // ""Base"": {
  9. // ""normal"": {
  10. // ""foreground"": ""White"",
  11. // ""background"": ""Blue""
  12. // },
  13. // ""focus"": {
  14. // ""foreground"": ""Black"",
  15. // ""background"": ""Gray""
  16. // },
  17. // ""hotNormal"": {
  18. // ""foreground"": ""BrightCyan"",
  19. // ""background"": ""Blue""
  20. // },
  21. // ""hotFocus"": {
  22. // ""foreground"": ""BrightBlue"",
  23. // ""background"": ""Gray""
  24. // },
  25. // ""disabled"": {
  26. // ""foreground"": ""DarkGray"",
  27. // ""background"": ""Blue""
  28. // }
  29. // }
  30. // }
  31. // }";
  32. [Fact]
  33. public void TestSchemesSerialization_Equality ()
  34. {
  35. // Arrange
  36. var expectedScheme = new Scheme
  37. {
  38. Normal = new (Color.White, Color.Blue),
  39. Focus = new (Color.Black, Color.Gray),
  40. HotNormal = new (Color.BrightCyan, Color.Blue),
  41. HotFocus = new (Color.BrightBlue, Color.Gray),
  42. Active = new (Color.Gray, Color.Black),
  43. HotActive = new (Color.Blue, Color.Gray),
  44. Highlight = new (Color.Gray, Color.Black),
  45. Editable = new (Color.Gray, Color.Black),
  46. ReadOnly = new (Color.Gray, Color.Black),
  47. Disabled = new (Color.Gray, Color.Black),
  48. };
  49. string serializedScheme =
  50. JsonSerializer.Serialize (expectedScheme, ConfigurationManager.SerializerContext.Options);
  51. // Act
  52. var actualScheme =
  53. JsonSerializer.Deserialize<Scheme> (serializedScheme, ConfigurationManager.SerializerContext.Options);
  54. // Assert
  55. Assert.Equal (expectedScheme, actualScheme);
  56. }
  57. [Fact]
  58. public void TestSchemesSerialization ()
  59. {
  60. var expected = new Scheme
  61. {
  62. Normal = new (Color.White, Color.Blue),
  63. Focus = new (Color.Black, Color.Gray),
  64. HotNormal = new (Color.BrightCyan, Color.Blue),
  65. HotFocus = new (Color.BrightBlue, Color.Gray),
  66. Active = new (Color.Gray, Color.Black),
  67. HotActive = new (Color.Blue, Color.Gray),
  68. Highlight = new (Color.Gray, Color.Black),
  69. Editable = new (Color.Gray, Color.Black),
  70. ReadOnly = new (Color.Gray, Color.Black),
  71. Disabled = new (Color.Gray, Color.Black),
  72. };
  73. string json = JsonSerializer.Serialize (expected, ConfigurationManager.SerializerContext.Options);
  74. Scheme actual = JsonSerializer.Deserialize<Scheme> (json, ConfigurationManager.SerializerContext.Options);
  75. Assert.NotNull (actual);
  76. foreach (VisualRole role in Enum.GetValues<VisualRole> ())
  77. {
  78. Attribute expectedAttr = expected.GetAttributeForRole (role);
  79. Attribute actualAttr = actual.GetAttributeForRole (role);
  80. Assert.Equal (expectedAttr, actualAttr);
  81. }
  82. }
  83. [Fact]
  84. public void Deserialized_Attributes_AreExplicitlySet ()
  85. {
  86. const string json = """
  87. {
  88. "Normal": { "Foreground": "White", "Background": "Blue" },
  89. "Focus": { "Foreground": "Black", "Background": "Gray" },
  90. "HotNormal": { "Foreground": "BrightCyan", "Background": "Blue" },
  91. "HotFocus": { "Foreground": "BrightBlue", "Background": "Gray" },
  92. "Active": { "Foreground": "DarkGray", "Background": "Blue" },
  93. "HotActive": { "Foreground": "DarkGray", "Background": "Blue" },
  94. "Highlight": { "Foreground": "DarkGray", "Background": "Blue" },
  95. "Editable": { "Foreground": "DarkGray", "Background": "Blue" },
  96. "Readonly": { "Foreground": "DarkGray", "Background": "Blue" },
  97. "Disabled": { "Foreground": "DarkGray", "Background": "Blue" }
  98. }
  99. """;
  100. Scheme scheme = JsonSerializer.Deserialize<Scheme> (json, ConfigurationManager.SerializerContext.Options)!;
  101. Assert.True (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.Normal, out _));
  102. Assert.True (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.HotNormal, out _));
  103. Assert.True (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.Focus, out _));
  104. Assert.True (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.HotFocus, out _));
  105. Assert.True (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.Active, out _));
  106. Assert.True (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.HotActive, out _));
  107. Assert.True (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.Highlight, out _));
  108. Assert.True (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.Editable, out _));
  109. Assert.True (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.ReadOnly, out _));
  110. Assert.True (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.Disabled, out _));
  111. }
  112. [Fact]
  113. public void Deserialized_Attributes_NotSpecified_AreImplicit ()
  114. {
  115. const string json = """
  116. {
  117. "Normal": { "Foreground": "White", "Background": "Black" }
  118. }
  119. """;
  120. Scheme scheme = JsonSerializer.Deserialize<Scheme> (json, ConfigurationManager.SerializerContext.Options)!;
  121. // explicitly set
  122. Assert.True (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.Normal, out _));
  123. // derived from Normal
  124. Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.HotNormal, out _));
  125. Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.Focus, out _));
  126. Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.HotFocus, out _));
  127. Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.Active, out _));
  128. Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.HotActive, out _));
  129. Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.Highlight, out _));
  130. Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.Editable, out _));
  131. Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.ReadOnly, out _));
  132. Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.Disabled, out _));
  133. }
  134. }