ConfigurationMangerTests.cs 26 KB

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