SettingsScopeTests.cs 3.7 KB

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