SettingsScopeTests.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using UnitTests;
  2. using static Terminal.Gui.ConfigurationManager;
  3. namespace Terminal.Gui.ConfigurationTests;
  4. public class SettingsScopeTests
  5. {
  6. [Fact]
  7. public void Update_Overrides_Defaults ()
  8. {
  9. // arrange
  10. Locations = ConfigLocations.Default;
  11. Load (true);
  12. Assert.Equal (Key.Esc, (Key)Settings ["Application.QuitKey"].PropertyValue);
  13. ThrowOnJsonErrors = true;
  14. // act
  15. var json = """
  16. {
  17. "Application.QuitKey": "Ctrl-Q"
  18. }
  19. """;
  20. Settings!.Update (json, "test", ConfigLocations.Runtime);
  21. // assert
  22. Assert.Equal (Key.Q.WithCtrl, (Key)Settings ["Application.QuitKey"].PropertyValue);
  23. // clean up
  24. Locations = ConfigLocations.All;
  25. Reset ();
  26. }
  27. [Fact]
  28. public void Apply_ShouldApplyProperties ()
  29. {
  30. Locations = ConfigLocations.Default;
  31. Reset();
  32. // arrange
  33. Assert.Equal (Key.Esc, (Key)Settings ["Application.QuitKey"].PropertyValue);
  34. Assert.Equal (
  35. Key.F6,
  36. (Key)Settings ["Application.NextTabGroupKey"].PropertyValue
  37. );
  38. Assert.Equal (
  39. Key.F6.WithShift,
  40. (Key)Settings ["Application.PrevTabGroupKey"].PropertyValue
  41. );
  42. // act
  43. Settings ["Application.QuitKey"].PropertyValue = Key.Q;
  44. Settings ["Application.NextTabGroupKey"].PropertyValue = Key.F;
  45. Settings ["Application.PrevTabGroupKey"].PropertyValue = Key.B;
  46. Settings.Apply ();
  47. // assert
  48. Assert.Equal (Key.Q, Application.QuitKey);
  49. Assert.Equal (Key.F, Application.NextTabGroupKey);
  50. Assert.Equal (Key.B, Application.PrevTabGroupKey);
  51. Locations = ConfigLocations.All;
  52. Reset ();
  53. }
  54. [Fact]
  55. [AutoInitShutdown]
  56. public void CopyUpdatedPropertiesFrom_ShouldCopyChangedPropertiesOnly ()
  57. {
  58. Settings ["Application.QuitKey"].PropertyValue = Key.End;
  59. var updatedSettings = new SettingsScope ();
  60. ///Don't set Quitkey
  61. updatedSettings ["Application.NextTabGroupKey"].PropertyValue = Key.F;
  62. updatedSettings ["Application.PrevTabGroupKey"].PropertyValue = Key.B;
  63. Settings.Update (updatedSettings);
  64. Assert.Equal (KeyCode.End, ((Key)Settings ["Application.QuitKey"].PropertyValue).KeyCode);
  65. Assert.Equal (KeyCode.F, ((Key)updatedSettings ["Application.NextTabGroupKey"].PropertyValue).KeyCode);
  66. Assert.Equal (KeyCode.B, ((Key)updatedSettings ["Application.PrevTabGroupKey"].PropertyValue).KeyCode);
  67. }
  68. [Fact]
  69. public void GetHardCodedDefaults_ShouldSetProperties ()
  70. {
  71. ConfigLocations savedLocations = Locations;
  72. Locations = ConfigLocations.Default;
  73. Reset ();
  74. Assert.Equal (5, ((Dictionary<string, ThemeScope>)Settings ["Themes"].PropertyValue).Count);
  75. GetHardCodedDefaults ();
  76. Assert.NotEmpty (Themes);
  77. Assert.Equal ("Default", Themes.Theme);
  78. Assert.True (Settings ["Application.QuitKey"].PropertyValue is Key);
  79. Assert.True (Settings ["Application.NextTabGroupKey"].PropertyValue is Key);
  80. Assert.True (Settings ["Application.PrevTabGroupKey"].PropertyValue is Key);
  81. Assert.True (Settings ["Theme"].PropertyValue is string);
  82. Assert.Equal ("Default", Settings ["Theme"].PropertyValue as string);
  83. Assert.True (Settings ["Themes"].PropertyValue is Dictionary<string, ThemeScope>);
  84. Assert.Single ((Dictionary<string, ThemeScope>)Settings ["Themes"].PropertyValue);
  85. Locations = ConfigLocations.All;
  86. Reset ();
  87. }
  88. }