ConfigurationMangerTests.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  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.NextTabGroupKey.KeyCode);
  30. Assert.Equal (KeyCode.B, Application.PrevTabGroupKey.KeyCode);
  31. }
  32. // act
  33. Settings! ["Application.QuitKey"].PropertyValue = Key.Q;
  34. Settings ["Application.NextTabGroupKey"].PropertyValue = Key.F;
  35. Settings ["Application.PrevTabGroupKey"].PropertyValue = Key.B;
  36. Apply ();
  37. // assert
  38. Assert.True (fired);
  39. Applied -= ConfigurationManager_Applied;
  40. Reset ();
  41. }
  42. [Fact]
  43. public void DeepMemberWiseCopyTest ()
  44. {
  45. // Value types
  46. var stringDest = "Destination";
  47. var stringSrc = "Source";
  48. object stringCopy = DeepMemberWiseCopy (stringSrc, stringDest);
  49. Assert.Equal (stringSrc, stringCopy);
  50. stringDest = "Destination";
  51. stringSrc = "Destination";
  52. stringCopy = DeepMemberWiseCopy (stringSrc, stringDest);
  53. Assert.Equal (stringSrc, stringCopy);
  54. stringDest = "Destination";
  55. stringSrc = null;
  56. stringCopy = DeepMemberWiseCopy (stringSrc, stringDest);
  57. Assert.Equal (stringSrc, stringCopy);
  58. stringDest = "Destination";
  59. stringSrc = string.Empty;
  60. stringCopy = DeepMemberWiseCopy (stringSrc, stringDest);
  61. Assert.Equal (stringSrc, stringCopy);
  62. var boolDest = true;
  63. var boolSrc = false;
  64. object boolCopy = DeepMemberWiseCopy (boolSrc, boolDest);
  65. Assert.Equal (boolSrc, boolCopy);
  66. boolDest = false;
  67. boolSrc = true;
  68. boolCopy = DeepMemberWiseCopy (boolSrc, boolDest);
  69. Assert.Equal (boolSrc, boolCopy);
  70. boolDest = true;
  71. boolSrc = true;
  72. boolCopy = DeepMemberWiseCopy (boolSrc, boolDest);
  73. Assert.Equal (boolSrc, boolCopy);
  74. boolDest = false;
  75. boolSrc = false;
  76. boolCopy = DeepMemberWiseCopy (boolSrc, boolDest);
  77. Assert.Equal (boolSrc, boolCopy);
  78. // Structs
  79. var attrDest = new Attribute (Color.Black);
  80. var attrSrc = new Attribute (Color.White);
  81. object attrCopy = DeepMemberWiseCopy (attrSrc, attrDest);
  82. Assert.Equal (attrSrc, attrCopy);
  83. // Classes
  84. var colorschemeDest = new ColorScheme { Disabled = new Attribute (Color.Black) };
  85. var colorschemeSrc = new ColorScheme { Disabled = new Attribute (Color.White) };
  86. object colorschemeCopy = DeepMemberWiseCopy (colorschemeSrc, colorschemeDest);
  87. Assert.Equal (colorschemeSrc, colorschemeCopy);
  88. // Dictionaries
  89. Dictionary<string, Attribute> dictDest = new () { { "Disabled", new Attribute (Color.Black) } };
  90. Dictionary<string, Attribute> dictSrc = new () { { "Disabled", new Attribute (Color.White) } };
  91. Dictionary<string, Attribute> dictCopy = (Dictionary<string, Attribute>)DeepMemberWiseCopy (dictSrc, dictDest);
  92. Assert.Equal (dictSrc, dictCopy);
  93. dictDest = new Dictionary<string, Attribute> { { "Disabled", new Attribute (Color.Black) } };
  94. dictSrc = new Dictionary<string, Attribute>
  95. {
  96. { "Disabled", new Attribute (Color.White) }, { "Normal", new Attribute (Color.Blue) }
  97. };
  98. dictCopy = (Dictionary<string, Attribute>)DeepMemberWiseCopy (dictSrc, dictDest);
  99. Assert.Equal (dictSrc, dictCopy);
  100. // src adds an item
  101. dictDest = new Dictionary<string, Attribute> { { "Disabled", new Attribute (Color.Black) } };
  102. dictSrc = new Dictionary<string, Attribute>
  103. {
  104. { "Disabled", new Attribute (Color.White) }, { "Normal", new Attribute (Color.Blue) }
  105. };
  106. dictCopy = (Dictionary<string, Attribute>)DeepMemberWiseCopy (dictSrc, dictDest);
  107. Assert.Equal (2, dictCopy!.Count);
  108. Assert.Equal (dictSrc ["Disabled"], dictCopy ["Disabled"]);
  109. Assert.Equal (dictSrc ["Normal"], dictCopy ["Normal"]);
  110. // src updates only one item
  111. dictDest = new Dictionary<string, Attribute>
  112. {
  113. { "Disabled", new Attribute (Color.Black) }, { "Normal", new Attribute (Color.White) }
  114. };
  115. dictSrc = new Dictionary<string, Attribute> { { "Disabled", new Attribute (Color.White) } };
  116. dictCopy = (Dictionary<string, Attribute>)DeepMemberWiseCopy (dictSrc, dictDest);
  117. Assert.Equal (2, dictCopy!.Count);
  118. Assert.Equal (dictSrc ["Disabled"], dictCopy ["Disabled"]);
  119. Assert.Equal (dictDest ["Normal"], dictCopy ["Normal"]);
  120. }
  121. [Fact]
  122. public void Load_FiresUpdated ()
  123. {
  124. ConfigLocations savedLocations = Locations;
  125. Locations = ConfigLocations.All;
  126. Reset ();
  127. Settings! ["Application.QuitKey"].PropertyValue = Key.Q;
  128. Settings ["Application.NextTabGroupKey"].PropertyValue = Key.F;
  129. Settings ["Application.PrevTabGroupKey"].PropertyValue = Key.B;
  130. Updated += ConfigurationManager_Updated;
  131. var fired = false;
  132. void ConfigurationManager_Updated (object sender, ConfigurationManagerEventArgs obj)
  133. {
  134. fired = true;
  135. // assert
  136. Assert.Equal (Key.Esc, (((Key)Settings! ["Application.QuitKey"].PropertyValue)!).KeyCode);
  137. Assert.Equal (
  138. KeyCode.F6,
  139. (((Key)Settings ["Application.NextTabGroupKey"].PropertyValue)!).KeyCode
  140. );
  141. Assert.Equal (
  142. KeyCode.F6 | KeyCode.ShiftMask,
  143. (((Key)Settings ["Application.PrevTabGroupKey"].PropertyValue)!).KeyCode
  144. );
  145. }
  146. Load (true);
  147. // assert
  148. Assert.True (fired);
  149. Updated -= ConfigurationManager_Updated;
  150. Reset ();
  151. Locations = savedLocations;
  152. }
  153. [Fact]
  154. [AutoInitShutdown]
  155. public void LoadConfigurationFromAllSources_ShouldLoadSettingsFromAllSources ()
  156. {
  157. //var _configFilename = "config.json";
  158. //// Arrange
  159. //// Create a mock of the configuration files in all sources
  160. //// Home directory
  161. //string homeDir = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.UserProfile), ".tui");
  162. //if (!Directory.Exists (homeDir)) {
  163. // Directory.CreateDirectory (homeDir);
  164. //}
  165. //string globalConfigFile = Path.Combine (homeDir, _configFilename);
  166. //string appSpecificConfigFile = Path.Combine (homeDir, "appname.config.json");
  167. //File.WriteAllText (globalConfigFile, "{\"Settings\": {\"TestSetting\":\"Global\"}}");
  168. //File.WriteAllText (appSpecificConfigFile, "{\"Settings\": {\"TestSetting\":\"AppSpecific\"}}");
  169. //// App directory
  170. //string appDir = Directory.GetCurrentDirectory ();
  171. //string appDirGlobalConfigFile = Path.Combine (appDir, _configFilename);
  172. //string appDirAppSpecificConfigFile = Path.Combine (appDir, "appname.config.json");
  173. //File.WriteAllText (appDirGlobalConfigFile, "{\"Settings\": {\"TestSetting\":\"GlobalAppDir\"}}");
  174. //File.WriteAllText (appDirAppSpecificConfigFile, "{\"Settings\": {\"TestSetting\":\"AppSpecificAppDir\"}}");
  175. //// App resources
  176. //// ...
  177. //// Act
  178. //ConfigurationManager.Locations = ConfigurationManager.ConfigLocation.All;
  179. //ConfigurationManager.Load ();
  180. //// Assert
  181. //// Check that the settings from the highest precedence source are loaded
  182. //Assert.Equal ("AppSpecific", ConfigurationManager.Config.Settings.TestSetting);
  183. }
  184. [Fact]
  185. public void Reset_and_ResetLoadWithLibraryResourcesOnly_are_same ()
  186. {
  187. Locations = ConfigLocations.DefaultOnly;
  188. // arrange
  189. Reset ();
  190. Settings! ["Application.QuitKey"].PropertyValue = Key.Q;
  191. Settings ["Application.NextTabGroupKey"].PropertyValue = Key.F;
  192. Settings ["Application.PrevTabGroupKey"].PropertyValue = Key.B;
  193. Settings.Apply ();
  194. // assert apply worked
  195. Assert.Equal (KeyCode.Q, Application.QuitKey.KeyCode);
  196. Assert.Equal (KeyCode.F, Application.NextTabGroupKey.KeyCode);
  197. Assert.Equal (KeyCode.B, Application.PrevTabGroupKey.KeyCode);
  198. //act
  199. Reset ();
  200. // assert
  201. Assert.NotEmpty (Themes!);
  202. Assert.Equal ("Default", Themes.Theme);
  203. Assert.Equal (Key.Esc, Application.QuitKey);
  204. Assert.Equal (Key.F6, Application.NextTabGroupKey);
  205. Assert.Equal (Key.F6.WithShift, Application.PrevTabGroupKey);
  206. // arrange
  207. Settings ["Application.QuitKey"].PropertyValue = Key.Q;
  208. Settings ["Application.NextTabGroupKey"].PropertyValue = Key.F;
  209. Settings ["Application.PrevTabGroupKey"].PropertyValue = Key.B;
  210. Settings.Apply ();
  211. Locations = ConfigLocations.DefaultOnly;
  212. // act
  213. Reset ();
  214. Load ();
  215. // assert
  216. Assert.NotEmpty (Themes);
  217. Assert.Equal ("Default", Themes.Theme);
  218. Assert.Equal (KeyCode.Esc, Application.QuitKey.KeyCode);
  219. Assert.Equal (Key.F6, Application.NextTabGroupKey);
  220. Assert.Equal (Key.F6.WithShift, Application.PrevTabGroupKey);
  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. #pragma warning disable xUnit2029
  324. Assert.Empty (
  325. Settings.Where (
  326. cp => cp.Value.PropertyInfo!.GetCustomAttribute (
  327. typeof (SerializableConfigurationProperty)
  328. )
  329. == null
  330. )
  331. );
  332. #pragma warning restore xUnit2029
  333. // Application is a static class
  334. PropertyInfo pi = typeof (Application).GetProperty ("QuitKey");
  335. Assert.Equal (pi, Settings ["Application.QuitKey"].PropertyInfo);
  336. // FrameView is not a static class and DefaultBorderStyle is Scope.Scheme
  337. pi = typeof (FrameView).GetProperty ("DefaultBorderStyle");
  338. Assert.False (Settings.ContainsKey ("FrameView.DefaultBorderStyle"));
  339. Assert.True (Themes! ["Default"].ContainsKey ("FrameView.DefaultBorderStyle"));
  340. Assert.Equal (pi, Themes! ["Default"] ["FrameView.DefaultBorderStyle"].PropertyInfo);
  341. }
  342. [Fact]
  343. public void TestConfigPropertyOmitClassName ()
  344. {
  345. ConfigLocations savedLocations = Locations;
  346. Locations = ConfigLocations.All;
  347. // Color.ColorSchemes is serialized as "ColorSchemes", not "Colors.ColorSchemes"
  348. PropertyInfo pi = typeof (Colors).GetProperty ("ColorSchemes");
  349. var scp = (SerializableConfigurationProperty)pi!.GetCustomAttribute (typeof (SerializableConfigurationProperty));
  350. Assert.True (scp!.Scope == typeof (ThemeScope));
  351. Assert.True (scp.OmitClassName);
  352. Reset ();
  353. Assert.Equal (pi, Themes! ["Default"] ["ColorSchemes"].PropertyInfo);
  354. Locations = savedLocations;
  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"": ""brownish"",
  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: brownish.", 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. ConfigLocations savedLocations = Locations;
  552. Locations = ConfigLocations.All;
  553. // Arrange
  554. var json = @"
  555. {
  556. ""$schema"": ""https://gui-cs.github.io/Terminal.GuiV2Docs/schemas/tui-config-schema.json"",
  557. ""Application.QuitKey"": ""Alt-Z"",
  558. ""Theme"": ""Default"",
  559. ""Themes"": [
  560. {
  561. ""Default"": {
  562. ""ColorSchemes"": [
  563. {
  564. ""TopLevel"": {
  565. ""Normal"": {
  566. ""Foreground"": ""BrightGreen"",
  567. ""Background"": ""Black""
  568. },
  569. ""Focus"": {
  570. ""Foreground"": ""White"",
  571. ""Background"": ""Cyan""
  572. },
  573. ""HotNormal"": {
  574. ""Foreground"": ""Yellow"",
  575. ""Background"": ""Black""
  576. },
  577. ""HotFocus"": {
  578. ""Foreground"": ""Blue"",
  579. ""Background"": ""Cyan""
  580. },
  581. ""Disabled"": {
  582. ""Foreground"": ""DarkGray"",
  583. ""Background"": ""Black""
  584. }
  585. }
  586. },
  587. {
  588. ""Base"": {
  589. ""Normal"": {
  590. ""Foreground"": ""White"",
  591. ""Background"": ""Blue""
  592. },
  593. ""Focus"": {
  594. ""Foreground"": ""Black"",
  595. ""Background"": ""Gray""
  596. },
  597. ""HotNormal"": {
  598. ""Foreground"": ""BrightCyan"",
  599. ""Background"": ""Blue""
  600. },
  601. ""HotFocus"": {
  602. ""Foreground"": ""BrightBlue"",
  603. ""Background"": ""Gray""
  604. },
  605. ""Disabled"": {
  606. ""Foreground"": ""DarkGray"",
  607. ""Background"": ""Blue""
  608. }
  609. }
  610. },
  611. {
  612. ""Dialog"": {
  613. ""Normal"": {
  614. ""Foreground"": ""Black"",
  615. ""Background"": ""Gray""
  616. },
  617. ""Focus"": {
  618. ""Foreground"": ""White"",
  619. ""Background"": ""DarkGray""
  620. },
  621. ""HotNormal"": {
  622. ""Foreground"": ""Blue"",
  623. ""Background"": ""Gray""
  624. },
  625. ""HotFocus"": {
  626. ""Foreground"": ""BrightYellow"",
  627. ""Background"": ""DarkGray""
  628. },
  629. ""Disabled"": {
  630. ""Foreground"": ""Gray"",
  631. ""Background"": ""DarkGray""
  632. }
  633. }
  634. },
  635. {
  636. ""Menu"": {
  637. ""Normal"": {
  638. ""Foreground"": ""White"",
  639. ""Background"": ""DarkGray""
  640. },
  641. ""Focus"": {
  642. ""Foreground"": ""White"",
  643. ""Background"": ""Black""
  644. },
  645. ""HotNormal"": {
  646. ""Foreground"": ""BrightYellow"",
  647. ""Background"": ""DarkGray""
  648. },
  649. ""HotFocus"": {
  650. ""Foreground"": ""BrightYellow"",
  651. ""Background"": ""Black""
  652. },
  653. ""Disabled"": {
  654. ""Foreground"": ""Gray"",
  655. ""Background"": ""DarkGray""
  656. }
  657. }
  658. },
  659. {
  660. ""Error"": {
  661. ""Normal"": {
  662. ""Foreground"": ""Red"",
  663. ""Background"": ""White""
  664. },
  665. ""Focus"": {
  666. ""Foreground"": ""Black"",
  667. ""Background"": ""BrightRed""
  668. },
  669. ""HotNormal"": {
  670. ""Foreground"": ""Black"",
  671. ""Background"": ""White""
  672. },
  673. ""HotFocus"": {
  674. ""Foreground"": ""White"",
  675. ""Background"": ""BrightRed""
  676. },
  677. ""Disabled"": {
  678. ""Foreground"": ""DarkGray"",
  679. ""Background"": ""White""
  680. }
  681. }
  682. }
  683. ]
  684. }
  685. }
  686. ]
  687. }
  688. ";
  689. Reset ();
  690. ThrowOnJsonErrors = true;
  691. Settings!.Update (json, "TestConfigurationManagerUpdateFromJson");
  692. Assert.Equal (KeyCode.Esc, Application.QuitKey.KeyCode);
  693. Assert.Equal (KeyCode.Z | KeyCode.AltMask, ((Key)Settings ["Application.QuitKey"].PropertyValue)!.KeyCode);
  694. Assert.Equal ("Default", Themes!.Theme);
  695. Assert.Equal (new Color (Color.White), Colors.ColorSchemes ["Base"]!.Normal.Foreground);
  696. Assert.Equal (new Color (Color.Blue), Colors.ColorSchemes ["Base"].Normal.Background);
  697. Dictionary<string, ColorScheme> colorSchemes =
  698. (Dictionary<string, ColorScheme>)Themes.First ().Value ["ColorSchemes"].PropertyValue;
  699. Assert.Equal (new Color (Color.White), colorSchemes! ["Base"].Normal.Foreground);
  700. Assert.Equal (new Color (Color.Blue), colorSchemes ["Base"].Normal.Background);
  701. // Now re-apply
  702. Apply ();
  703. Assert.Equal (KeyCode.Z | KeyCode.AltMask, Application.QuitKey.KeyCode);
  704. Assert.Equal ("Default", Themes.Theme);
  705. Assert.Equal (new Color (Color.White), Colors.ColorSchemes ["Base"].Normal.Foreground);
  706. Assert.Equal (new Color (Color.Blue), Colors.ColorSchemes ["Base"].Normal.Background);
  707. Reset ();
  708. Locations = savedLocations;
  709. }
  710. [Fact]
  711. public void UseWithoutResetDoesNotThrow ()
  712. {
  713. Initialize ();
  714. _ = Settings;
  715. }
  716. }