ConfigurationMangerTests.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. using System.Reflection;
  2. using System.Text.Json;
  3. using static Terminal.Gui.ConfigurationManager;
  4. #pragma warning disable IDE1006
  5. namespace Terminal.Gui.ConfigurationTests;
  6. public class ConfigurationManagerTests
  7. {
  8. public static readonly JsonSerializerOptions _jsonOptions = new ()
  9. {
  10. Converters = { new AttributeJsonConverter (), new ColorJsonConverter () }
  11. };
  12. [Fact]
  13. public void Apply_FiresApplied ()
  14. {
  15. Reset ();
  16. Applied += ConfigurationManager_Applied;
  17. var fired = false;
  18. void ConfigurationManager_Applied (object sender, ConfigurationManagerEventArgs obj)
  19. {
  20. fired = true;
  21. // assert
  22. Assert.Equal (KeyCode.Q, Application.QuitKey.KeyCode);
  23. Assert.Equal (KeyCode.F, Application.AlternateForwardKey.KeyCode);
  24. Assert.Equal (KeyCode.B, Application.AlternateBackwardKey.KeyCode);
  25. Assert.True (Application.IsMouseDisabled);
  26. }
  27. // act
  28. Settings ["Application.QuitKey"].PropertyValue = Key.Q;
  29. Settings ["Application.AlternateForwardKey"].PropertyValue = Key.F;
  30. Settings ["Application.AlternateBackwardKey"].PropertyValue = Key.B;
  31. Settings ["Application.IsMouseDisabled"].PropertyValue = true;
  32. Apply ();
  33. // assert
  34. Assert.True (fired);
  35. Applied -= ConfigurationManager_Applied;
  36. Reset ();
  37. }
  38. [Fact]
  39. public void DeepMemberWiseCopyTest ()
  40. {
  41. // Value types
  42. var stringDest = "Destination";
  43. var stringSrc = "Source";
  44. object stringCopy = DeepMemberWiseCopy (stringSrc, stringDest);
  45. Assert.Equal (stringSrc, stringCopy);
  46. stringDest = "Destination";
  47. stringSrc = "Destination";
  48. stringCopy = DeepMemberWiseCopy (stringSrc, stringDest);
  49. Assert.Equal (stringSrc, stringCopy);
  50. stringDest = "Destination";
  51. stringSrc = null;
  52. stringCopy = DeepMemberWiseCopy (stringSrc, stringDest);
  53. Assert.Equal (stringSrc, stringCopy);
  54. stringDest = "Destination";
  55. stringSrc = string.Empty;
  56. stringCopy = DeepMemberWiseCopy (stringSrc, stringDest);
  57. Assert.Equal (stringSrc, stringCopy);
  58. var boolDest = true;
  59. var boolSrc = false;
  60. object boolCopy = DeepMemberWiseCopy (boolSrc, boolDest);
  61. Assert.Equal (boolSrc, boolCopy);
  62. boolDest = false;
  63. boolSrc = true;
  64. boolCopy = DeepMemberWiseCopy (boolSrc, boolDest);
  65. Assert.Equal (boolSrc, boolCopy);
  66. boolDest = true;
  67. boolSrc = true;
  68. boolCopy = DeepMemberWiseCopy (boolSrc, boolDest);
  69. Assert.Equal (boolSrc, boolCopy);
  70. boolDest = false;
  71. boolSrc = false;
  72. boolCopy = DeepMemberWiseCopy (boolSrc, boolDest);
  73. Assert.Equal (boolSrc, boolCopy);
  74. // Structs
  75. var attrDest = new Attribute (Color.Black);
  76. var attrSrc = new Attribute (Color.White);
  77. object attrCopy = DeepMemberWiseCopy (attrSrc, attrDest);
  78. Assert.Equal (attrSrc, attrCopy);
  79. // Classes
  80. var colorschemeDest = new ColorScheme { Disabled = new Attribute (Color.Black) };
  81. var colorschemeSrc = new ColorScheme { Disabled = new Attribute (Color.White) };
  82. object colorschemeCopy = DeepMemberWiseCopy (colorschemeSrc, colorschemeDest);
  83. Assert.Equal (colorschemeSrc, colorschemeCopy);
  84. // Dictionaries
  85. Dictionary<string, Attribute> dictDest = new () { { "Disabled", new Attribute (Color.Black) } };
  86. Dictionary<string, Attribute> dictSrc = new () { { "Disabled", new Attribute (Color.White) } };
  87. Dictionary<string, Attribute> dictCopy = (Dictionary<string, Attribute>)DeepMemberWiseCopy (dictSrc, dictDest);
  88. Assert.Equal (dictSrc, dictCopy);
  89. dictDest = new Dictionary<string, Attribute> { { "Disabled", new Attribute (Color.Black) } };
  90. dictSrc = new Dictionary<string, Attribute>
  91. {
  92. { "Disabled", new Attribute (Color.White) }, { "Normal", new Attribute (Color.Blue) }
  93. };
  94. dictCopy = (Dictionary<string, Attribute>)DeepMemberWiseCopy (dictSrc, dictDest);
  95. Assert.Equal (dictSrc, dictCopy);
  96. // src adds an item
  97. dictDest = new Dictionary<string, Attribute> { { "Disabled", new Attribute (Color.Black) } };
  98. dictSrc = new Dictionary<string, Attribute>
  99. {
  100. { "Disabled", new Attribute (Color.White) }, { "Normal", new Attribute (Color.Blue) }
  101. };
  102. dictCopy = (Dictionary<string, Attribute>)DeepMemberWiseCopy (dictSrc, dictDest);
  103. Assert.Equal (2, dictCopy.Count);
  104. Assert.Equal (dictSrc ["Disabled"], dictCopy ["Disabled"]);
  105. Assert.Equal (dictSrc ["Normal"], dictCopy ["Normal"]);
  106. // src updates only one item
  107. dictDest = new Dictionary<string, Attribute>
  108. {
  109. { "Disabled", new Attribute (Color.Black) }, { "Normal", new Attribute (Color.White) }
  110. };
  111. dictSrc = new Dictionary<string, Attribute> { { "Disabled", new Attribute (Color.White) } };
  112. dictCopy = (Dictionary<string, Attribute>)DeepMemberWiseCopy (dictSrc, dictDest);
  113. Assert.Equal (2, dictCopy.Count);
  114. Assert.Equal (dictSrc ["Disabled"], dictCopy ["Disabled"]);
  115. Assert.Equal (dictDest ["Normal"], dictCopy ["Normal"]);
  116. }
  117. [Fact]
  118. public void Load_FiresUpdated ()
  119. {
  120. Reset ();
  121. Settings ["Application.QuitKey"].PropertyValue = Key.Q;
  122. Settings ["Application.AlternateForwardKey"].PropertyValue = Key.F;
  123. Settings ["Application.AlternateBackwardKey"].PropertyValue = Key.B;
  124. Settings ["Application.IsMouseDisabled"].PropertyValue = true;
  125. Updated += ConfigurationManager_Updated;
  126. var fired = false;
  127. void ConfigurationManager_Updated (object sender, ConfigurationManagerEventArgs obj)
  128. {
  129. fired = true;
  130. // assert
  131. Assert.Equal (KeyCode.Q | KeyCode.CtrlMask, ((Key)Settings ["Application.QuitKey"].PropertyValue).KeyCode);
  132. Assert.Equal (
  133. KeyCode.PageDown | KeyCode.CtrlMask,
  134. ((Key)Settings ["Application.AlternateForwardKey"].PropertyValue).KeyCode
  135. );
  136. Assert.Equal (
  137. KeyCode.PageUp | KeyCode.CtrlMask,
  138. ((Key)Settings ["Application.AlternateBackwardKey"].PropertyValue).KeyCode
  139. );
  140. Assert.False ((bool)Settings ["Application.IsMouseDisabled"].PropertyValue);
  141. }
  142. Load (true);
  143. // assert
  144. Assert.True (fired);
  145. Updated -= ConfigurationManager_Updated;
  146. Reset ();
  147. }
  148. [Fact]
  149. [AutoInitShutdown]
  150. public void LoadConfigurationFromAllSources_ShouldLoadSettingsFromAllSources ()
  151. {
  152. //var _configFilename = "config.json";
  153. //// Arrange
  154. //// Create a mock of the configuration files in all sources
  155. //// Home directory
  156. //string homeDir = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.UserProfile), ".tui");
  157. //if (!Directory.Exists (homeDir)) {
  158. // Directory.CreateDirectory (homeDir);
  159. //}
  160. //string globalConfigFile = Path.Combine (homeDir, _configFilename);
  161. //string appSpecificConfigFile = Path.Combine (homeDir, "appname.config.json");
  162. //File.WriteAllText (globalConfigFile, "{\"Settings\": {\"TestSetting\":\"Global\"}}");
  163. //File.WriteAllText (appSpecificConfigFile, "{\"Settings\": {\"TestSetting\":\"AppSpecific\"}}");
  164. //// App directory
  165. //string appDir = Directory.GetCurrentDirectory ();
  166. //string appDirGlobalConfigFile = Path.Combine (appDir, _configFilename);
  167. //string appDirAppSpecificConfigFile = Path.Combine (appDir, "appname.config.json");
  168. //File.WriteAllText (appDirGlobalConfigFile, "{\"Settings\": {\"TestSetting\":\"GlobalAppDir\"}}");
  169. //File.WriteAllText (appDirAppSpecificConfigFile, "{\"Settings\": {\"TestSetting\":\"AppSpecificAppDir\"}}");
  170. //// App resources
  171. //// ...
  172. //// Act
  173. //ConfigurationManager.Locations = ConfigurationManager.ConfigLocation.All;
  174. //ConfigurationManager.Load ();
  175. //// Assert
  176. //// Check that the settings from the highest precedence source are loaded
  177. //Assert.Equal ("AppSpecific", ConfigurationManager.Config.Settings.TestSetting);
  178. }
  179. [Fact]
  180. public void Reset_and_ResetLoadWithLibraryResourcesOnly_are_same ()
  181. {
  182. Locations = ConfigLocations.DefaultOnly;
  183. // arrange
  184. Reset ();
  185. Settings ["Application.QuitKey"].PropertyValue = Key.Q;
  186. Settings ["Application.AlternateForwardKey"].PropertyValue = Key.F;
  187. Settings ["Application.AlternateBackwardKey"].PropertyValue = Key.B;
  188. Settings ["Application.IsMouseDisabled"].PropertyValue = true;
  189. Settings.Apply ();
  190. // assert apply worked
  191. Assert.Equal (KeyCode.Q, Application.QuitKey.KeyCode);
  192. Assert.Equal (KeyCode.F, Application.AlternateForwardKey.KeyCode);
  193. Assert.Equal (KeyCode.B, Application.AlternateBackwardKey.KeyCode);
  194. Assert.True (Application.IsMouseDisabled);
  195. //act
  196. Reset ();
  197. // assert
  198. Assert.NotEmpty (Themes);
  199. Assert.Equal ("Default", Themes.Theme);
  200. Assert.Equal (KeyCode.Q | KeyCode.CtrlMask, Application.QuitKey.KeyCode);
  201. Assert.Equal (KeyCode.PageDown | KeyCode.CtrlMask, Application.AlternateForwardKey.KeyCode);
  202. Assert.Equal (KeyCode.PageUp | KeyCode.CtrlMask, Application.AlternateBackwardKey.KeyCode);
  203. Assert.False (Application.IsMouseDisabled);
  204. // arrange
  205. Settings ["Application.QuitKey"].PropertyValue = Key.Q;
  206. Settings ["Application.AlternateForwardKey"].PropertyValue = Key.F;
  207. Settings ["Application.AlternateBackwardKey"].PropertyValue = Key.B;
  208. Settings ["Application.IsMouseDisabled"].PropertyValue = true;
  209. Settings.Apply ();
  210. Locations = ConfigLocations.DefaultOnly;
  211. // act
  212. Reset ();
  213. Load ();
  214. // assert
  215. Assert.NotEmpty (Themes);
  216. Assert.Equal ("Default", Themes.Theme);
  217. Assert.Equal (KeyCode.Q | KeyCode.CtrlMask, Application.QuitKey.KeyCode);
  218. Assert.Equal (KeyCode.PageDown | KeyCode.CtrlMask, Application.AlternateForwardKey.KeyCode);
  219. Assert.Equal (KeyCode.PageUp | KeyCode.CtrlMask, Application.AlternateBackwardKey.KeyCode);
  220. Assert.False (Application.IsMouseDisabled);
  221. Reset ();
  222. }
  223. [Fact]
  224. public void Reset_Resets ()
  225. {
  226. Locations = ConfigLocations.DefaultOnly;
  227. Reset ();
  228. Assert.NotEmpty (Themes);
  229. Assert.Equal ("Default", Themes.Theme);
  230. }
  231. //[Fact ()]
  232. //public void LoadFromJsonTest ()
  233. //{
  234. // Assert.True (false, "This test needs an implementation");
  235. //}
  236. //[Fact ()]
  237. //public void ToJsonTest ()
  238. //{
  239. // Assert.True (false, "This test needs an implementation");
  240. //}
  241. //[Fact ()]
  242. //public void UpdateConfigurationTest ()
  243. //{
  244. // Assert.True (false, "This test needs an implementation");
  245. //}
  246. //[Fact ()]
  247. //public void UpdateConfigurationFromFileTest ()
  248. //{
  249. // Assert.True (false, "This test needs an implementation");
  250. //}
  251. //[Fact ()]
  252. //public void SaveHardCodedDefaultsTest ()
  253. //{
  254. // Assert.True (false, "This test needs an implementation");
  255. //}
  256. //[Fact ()]
  257. //public void LoadGlobalFromLibraryResourceTest ()
  258. //{
  259. // Assert.True (false, "This test needs an implementation");
  260. //}
  261. //[Fact ()]
  262. //public void LoadGlobalFromAppDirectoryTest ()
  263. //{
  264. // Assert.True (false, "This test needs an implementation");
  265. //}
  266. //[Fact ()]
  267. //public void LoadGlobalFromHomeDirectoryTest ()
  268. //{
  269. // Assert.True (false, "This test needs an implementation");
  270. //}
  271. //[Fact ()]
  272. //public void LoadAppFromAppResourcesTest ()
  273. //{
  274. // Assert.True (false, "This test needs an implementation");
  275. //}
  276. //[Fact ()]
  277. //public void LoadAppFromAppDirectoryTest ()
  278. //{
  279. // Assert.True (false, "This test needs an implementation");
  280. //}
  281. //[Fact ()]
  282. //public void LoadAppFromHomeDirectoryTest ()
  283. //{
  284. // Assert.True (false, "This test needs an implementation");
  285. //}
  286. //[Fact ()]
  287. //public void LoadTest ()
  288. //{
  289. // Assert.True (false, "This test needs an implementation");
  290. //}
  291. /// <summary>Save the `config.json` file; this can be used to update the file in `Terminal.Gui.Resources.config.json'.</summary>
  292. /// <remarks>
  293. /// IMPORTANT: For the file generated to be valid, this must be the ONLY test run. Config Properties are all
  294. /// static and thus can be overwritten by other tests.
  295. /// </remarks>
  296. [Fact]
  297. public void SaveDefaults ()
  298. {
  299. Initialize ();
  300. Reset ();
  301. // Get the hard coded settings
  302. GetHardCodedDefaults ();
  303. // Serialize to a JSON string
  304. string json = ToJson ();
  305. // Write the JSON string to the file
  306. File.WriteAllText ("config.json", json);
  307. }
  308. [Fact]
  309. public void TestConfigProperties ()
  310. {
  311. Locations = ConfigLocations.All;
  312. Reset ();
  313. Assert.NotEmpty (Settings);
  314. // test that all ConfigProperties have our attribute
  315. Assert.All (
  316. Settings,
  317. item => Assert.NotEmpty (
  318. item.Value.PropertyInfo.CustomAttributes.Where (
  319. a => a.AttributeType == typeof (SerializableConfigurationProperty)
  320. )
  321. )
  322. );
  323. Assert.Empty (
  324. Settings.Where (
  325. cp => cp.Value.PropertyInfo.GetCustomAttribute (
  326. typeof (SerializableConfigurationProperty)
  327. )
  328. == null
  329. )
  330. );
  331. // Application is a static class
  332. PropertyInfo pi = typeof (Application).GetProperty ("QuitKey");
  333. Assert.Equal (pi, Settings ["Application.QuitKey"].PropertyInfo);
  334. // FrameView is not a static class and DefaultBorderStyle is Scope.Scheme
  335. pi = typeof (FrameView).GetProperty ("DefaultBorderStyle");
  336. Assert.False (Settings.ContainsKey ("FrameView.DefaultBorderStyle"));
  337. Assert.True (Themes ["Default"].ContainsKey ("FrameView.DefaultBorderStyle"));
  338. }
  339. [Fact]
  340. public void TestConfigPropertyOmitClassName ()
  341. {
  342. // Color.ColorSchemes is serialized as "ColorSchemes", not "Colors.ColorSchemes"
  343. PropertyInfo pi = typeof (Colors).GetProperty ("ColorSchemes");
  344. var scp = (SerializableConfigurationProperty)pi.GetCustomAttribute (typeof (SerializableConfigurationProperty));
  345. Assert.True (scp.Scope == typeof (ThemeScope));
  346. Assert.True (scp.OmitClassName);
  347. Reset ();
  348. Assert.Equal (pi, Themes ["Default"] ["ColorSchemes"].PropertyInfo);
  349. }
  350. [Fact]
  351. [AutoInitShutdown (configLocation: ConfigLocations.DefaultOnly)]
  352. public void TestConfigurationManagerInitDriver ()
  353. {
  354. Assert.Equal ("Default", Themes.Theme);
  355. Assert.True (Themes.ContainsKey ("Default"));
  356. Assert.Equal (KeyCode.Q | KeyCode.CtrlMask, Application.QuitKey.KeyCode);
  357. Assert.Equal (new Color (Color.White), Colors.ColorSchemes ["Base"].Normal.Foreground);
  358. Assert.Equal (new Color (Color.Blue), Colors.ColorSchemes ["Base"].Normal.Background);
  359. // Change Base
  360. Stream json = ToStream ();
  361. Settings.Update (json, "TestConfigurationManagerInitDriver");
  362. Dictionary<string, ColorScheme> colorSchemes =
  363. (Dictionary<string, ColorScheme>)Themes [Themes.Theme] ["ColorSchemes"].PropertyValue;
  364. Assert.Equal (Colors.ColorSchemes ["Base"], colorSchemes ["Base"]);
  365. Assert.Equal (Colors.ColorSchemes ["TopLevel"], colorSchemes ["TopLevel"]);
  366. Assert.Equal (Colors.ColorSchemes ["Error"], colorSchemes ["Error"]);
  367. Assert.Equal (Colors.ColorSchemes ["Dialog"], colorSchemes ["Dialog"]);
  368. Assert.Equal (Colors.ColorSchemes ["Menu"], colorSchemes ["Menu"]);
  369. Colors.ColorSchemes ["Base"] = colorSchemes ["Base"];
  370. Colors.ColorSchemes ["TopLevel"] = colorSchemes ["TopLevel"];
  371. Colors.ColorSchemes ["Error"] = colorSchemes ["Error"];
  372. Colors.ColorSchemes ["Dialog"] = colorSchemes ["Dialog"];
  373. Colors.ColorSchemes ["Menu"] = colorSchemes ["Menu"];
  374. Assert.Equal (colorSchemes ["Base"], Colors.ColorSchemes ["Base"]);
  375. Assert.Equal (colorSchemes ["TopLevel"], Colors.ColorSchemes ["TopLevel"]);
  376. Assert.Equal (colorSchemes ["Error"], Colors.ColorSchemes ["Error"]);
  377. Assert.Equal (colorSchemes ["Dialog"], Colors.ColorSchemes ["Dialog"]);
  378. Assert.Equal (colorSchemes ["Menu"], Colors.ColorSchemes ["Menu"]);
  379. }
  380. [Fact]
  381. [AutoInitShutdown (configLocation: ConfigLocations.None)]
  382. public void TestConfigurationManagerInitDriver_NoLocations () { }
  383. [Fact]
  384. public void TestConfigurationManagerInvalidJsonLogs ()
  385. {
  386. Application.Init (new FakeDriver ());
  387. ThrowOnJsonErrors = false;
  388. // "brown" is not a color
  389. var json = @"
  390. {
  391. ""Themes"" : [
  392. {
  393. ""Default"" : {
  394. ""ColorSchemes"": [
  395. {
  396. ""UserDefined"": {
  397. ""hotNormal"": {
  398. ""foreground"": ""brown"",
  399. ""background"": ""1234""
  400. }
  401. }
  402. }
  403. ]
  404. }
  405. }
  406. }
  407. }";
  408. Settings.Update (json, "test");
  409. // AbNormal is not a ColorScheme attribute
  410. json = @"
  411. {
  412. ""Themes"" : [
  413. {
  414. ""Default"" : {
  415. ""ColorSchemes"": [
  416. {
  417. ""UserDefined"": {
  418. ""AbNormal"": {
  419. ""foreground"": ""green"",
  420. ""background"": ""black""
  421. }
  422. }
  423. }
  424. ]
  425. }
  426. }
  427. }
  428. }";
  429. Settings.Update (json, "test");
  430. // Modify hotNormal background only
  431. json = @"
  432. {
  433. ""Themes"" : [
  434. {
  435. ""Default"" : {
  436. ""ColorSchemes"": [
  437. {
  438. ""UserDefined"": {
  439. ""hotNormal"": {
  440. ""background"": ""cyan""
  441. }
  442. }
  443. }
  444. ]
  445. }
  446. }
  447. }
  448. }";
  449. Settings.Update (json, "test");
  450. Settings.Update ("{}}", "test");
  451. Assert.NotEqual (0, _jsonErrors.Length);
  452. Application.Shutdown ();
  453. ThrowOnJsonErrors = false;
  454. }
  455. [Fact]
  456. [AutoInitShutdown]
  457. public void TestConfigurationManagerInvalidJsonThrows ()
  458. {
  459. ThrowOnJsonErrors = true;
  460. // "yellow" is not a color
  461. var json = @"
  462. {
  463. ""Themes"" : [
  464. {
  465. ""Default"" : {
  466. ""ColorSchemes"": [
  467. {
  468. ""UserDefined"": {
  469. ""hotNormal"": {
  470. ""foreground"": ""brown"",
  471. ""background"": ""1234""
  472. }
  473. }
  474. }
  475. ]
  476. }
  477. }
  478. ]
  479. }";
  480. var jsonException = Assert.Throws<JsonException> (() => Settings.Update (json, "test"));
  481. Assert.Equal ("Unexpected color name: brown.", jsonException.Message);
  482. // AbNormal is not a ColorScheme attribute
  483. json = @"
  484. {
  485. ""Themes"" : [
  486. {
  487. ""Default"" : {
  488. ""ColorSchemes"": [
  489. {
  490. ""UserDefined"": {
  491. ""AbNormal"": {
  492. ""foreground"": ""green"",
  493. ""background"": ""black""
  494. }
  495. }
  496. }
  497. ]
  498. }
  499. }
  500. ]
  501. }";
  502. jsonException = Assert.Throws<JsonException> (() => Settings.Update (json, "test"));
  503. Assert.Equal ("Unrecognized ColorScheme Attribute name: AbNormal.", jsonException.Message);
  504. // Modify hotNormal background only
  505. json = @"
  506. {
  507. ""Themes"" : [
  508. {
  509. ""Default"" : {
  510. ""ColorSchemes"": [
  511. {
  512. ""UserDefined"": {
  513. ""hotNormal"": {
  514. ""background"": ""cyan""
  515. }
  516. }
  517. }
  518. ]
  519. }
  520. }
  521. ]
  522. }";
  523. jsonException = Assert.Throws<JsonException> (() => Settings.Update (json, "test"));
  524. Assert.Equal ("Both Foreground and Background colors must be provided.", jsonException.Message);
  525. // Unknown property
  526. json = @"
  527. {
  528. ""Unknown"" : ""Not known""
  529. }";
  530. jsonException = Assert.Throws<JsonException> (() => Settings.Update (json, "test"));
  531. Assert.StartsWith ("Unknown property", jsonException.Message);
  532. Assert.Equal (0, _jsonErrors.Length);
  533. ThrowOnJsonErrors = false;
  534. }
  535. [Fact]
  536. [AutoInitShutdown]
  537. public void TestConfigurationManagerToJson ()
  538. {
  539. Reset ();
  540. GetHardCodedDefaults ();
  541. Stream stream = ToStream ();
  542. Settings.Update (stream, "TestConfigurationManagerToJson");
  543. }
  544. [Fact]
  545. public void TestConfigurationManagerUpdateFromJson ()
  546. {
  547. // Arrange
  548. var json = @"
  549. {
  550. ""$schema"": ""https://gui-cs.github.io/Terminal.Gui/schemas/tui-config-schema.json"",
  551. ""Application.QuitKey"": ""Alt-Z"",
  552. ""Theme"": ""Default"",
  553. ""Themes"": [
  554. {
  555. ""Default"": {
  556. ""ColorSchemes"": [
  557. {
  558. ""TopLevel"": {
  559. ""Normal"": {
  560. ""Foreground"": ""BrightGreen"",
  561. ""Background"": ""Black""
  562. },
  563. ""Focus"": {
  564. ""Foreground"": ""White"",
  565. ""Background"": ""Cyan""
  566. },
  567. ""HotNormal"": {
  568. ""Foreground"": ""Yellow"",
  569. ""Background"": ""Black""
  570. },
  571. ""HotFocus"": {
  572. ""Foreground"": ""Blue"",
  573. ""Background"": ""Cyan""
  574. },
  575. ""Disabled"": {
  576. ""Foreground"": ""DarkGray"",
  577. ""Background"": ""Black""
  578. }
  579. }
  580. },
  581. {
  582. ""Base"": {
  583. ""Normal"": {
  584. ""Foreground"": ""White"",
  585. ""Background"": ""Blue""
  586. },
  587. ""Focus"": {
  588. ""Foreground"": ""Black"",
  589. ""Background"": ""Gray""
  590. },
  591. ""HotNormal"": {
  592. ""Foreground"": ""BrightCyan"",
  593. ""Background"": ""Blue""
  594. },
  595. ""HotFocus"": {
  596. ""Foreground"": ""BrightBlue"",
  597. ""Background"": ""Gray""
  598. },
  599. ""Disabled"": {
  600. ""Foreground"": ""DarkGray"",
  601. ""Background"": ""Blue""
  602. }
  603. }
  604. },
  605. {
  606. ""Dialog"": {
  607. ""Normal"": {
  608. ""Foreground"": ""Black"",
  609. ""Background"": ""Gray""
  610. },
  611. ""Focus"": {
  612. ""Foreground"": ""White"",
  613. ""Background"": ""DarkGray""
  614. },
  615. ""HotNormal"": {
  616. ""Foreground"": ""Blue"",
  617. ""Background"": ""Gray""
  618. },
  619. ""HotFocus"": {
  620. ""Foreground"": ""BrightYellow"",
  621. ""Background"": ""DarkGray""
  622. },
  623. ""Disabled"": {
  624. ""Foreground"": ""Gray"",
  625. ""Background"": ""DarkGray""
  626. }
  627. }
  628. },
  629. {
  630. ""Menu"": {
  631. ""Normal"": {
  632. ""Foreground"": ""White"",
  633. ""Background"": ""DarkGray""
  634. },
  635. ""Focus"": {
  636. ""Foreground"": ""White"",
  637. ""Background"": ""Black""
  638. },
  639. ""HotNormal"": {
  640. ""Foreground"": ""BrightYellow"",
  641. ""Background"": ""DarkGray""
  642. },
  643. ""HotFocus"": {
  644. ""Foreground"": ""BrightYellow"",
  645. ""Background"": ""Black""
  646. },
  647. ""Disabled"": {
  648. ""Foreground"": ""Gray"",
  649. ""Background"": ""DarkGray""
  650. }
  651. }
  652. },
  653. {
  654. ""Error"": {
  655. ""Normal"": {
  656. ""Foreground"": ""Red"",
  657. ""Background"": ""White""
  658. },
  659. ""Focus"": {
  660. ""Foreground"": ""Black"",
  661. ""Background"": ""BrightRed""
  662. },
  663. ""HotNormal"": {
  664. ""Foreground"": ""Black"",
  665. ""Background"": ""White""
  666. },
  667. ""HotFocus"": {
  668. ""Foreground"": ""White"",
  669. ""Background"": ""BrightRed""
  670. },
  671. ""Disabled"": {
  672. ""Foreground"": ""DarkGray"",
  673. ""Background"": ""White""
  674. }
  675. }
  676. }
  677. ]
  678. }
  679. }
  680. ]
  681. }
  682. ";
  683. Reset ();
  684. ThrowOnJsonErrors = true;
  685. Settings.Update (json, "TestConfigurationManagerUpdateFromJson");
  686. Assert.Equal (KeyCode.Q | KeyCode.CtrlMask, Application.QuitKey.KeyCode);
  687. Assert.Equal (KeyCode.Z | KeyCode.AltMask, ((Key)Settings ["Application.QuitKey"].PropertyValue).KeyCode);
  688. Assert.Equal ("Default", Themes.Theme);
  689. Assert.Equal (new Color (Color.White), Colors.ColorSchemes ["Base"].Normal.Foreground);
  690. Assert.Equal (new Color (Color.Blue), Colors.ColorSchemes ["Base"].Normal.Background);
  691. Dictionary<string, ColorScheme> colorSchemes =
  692. (Dictionary<string, ColorScheme>)Themes.First ().Value ["ColorSchemes"].PropertyValue;
  693. Assert.Equal (new Color (Color.White), colorSchemes ["Base"].Normal.Foreground);
  694. Assert.Equal (new Color (Color.Blue), colorSchemes ["Base"].Normal.Background);
  695. // Now re-apply
  696. Apply ();
  697. Assert.Equal (KeyCode.Z | KeyCode.AltMask, Application.QuitKey.KeyCode);
  698. Assert.Equal ("Default", Themes.Theme);
  699. Assert.Equal (new Color (Color.White), Colors.ColorSchemes ["Base"].Normal.Foreground);
  700. Assert.Equal (new Color (Color.Blue), Colors.ColorSchemes ["Base"].Normal.Background);
  701. Reset ();
  702. }
  703. [Fact]
  704. public void UseWithoutResetDoesNotThrow ()
  705. {
  706. Initialize ();
  707. _ = Settings;
  708. }
  709. }