ConfigurationMangerTests.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  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_Raises_Applied ()
  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_Raises_Updated ()
  123. {
  124. Locations = ConfigLocations.All;
  125. Reset ();
  126. Assert.Equal (Key.Esc, (((Key)Settings! ["Application.QuitKey"].PropertyValue)!).KeyCode);
  127. Updated += ConfigurationManager_Updated;
  128. var fired = false;
  129. void ConfigurationManager_Updated (object? sender, ConfigurationManagerEventArgs obj)
  130. {
  131. fired = true;
  132. }
  133. // Act
  134. // Do NOT reset
  135. Load (false);
  136. // assert
  137. Assert.True (fired);
  138. Updated -= ConfigurationManager_Updated;
  139. // clean up
  140. Locations = ConfigLocations.Default;
  141. Reset ();
  142. }
  143. [Fact]
  144. public void Load_Loads_Custom_Json ()
  145. {
  146. // arrange
  147. Locations = ConfigLocations.All;
  148. Reset ();
  149. ThrowOnJsonErrors = true;
  150. Assert.Equal (Key.Esc, (Key)Settings! ["Application.QuitKey"].PropertyValue);
  151. // act
  152. Memory = """
  153. {
  154. "Application.QuitKey": "Ctrl-Q"
  155. }
  156. """;
  157. Load (false);
  158. // assert
  159. Assert.Equal (Key.Q.WithCtrl, (Key)Settings ["Application.QuitKey"].PropertyValue);
  160. // clean up
  161. Locations = ConfigLocations.Default;
  162. Reset ();
  163. }
  164. [Fact]
  165. [AutoInitShutdown]
  166. public void LoadConfigurationFromAllSources_ShouldLoadSettingsFromAllSources ()
  167. {
  168. //var _configFilename = "config.json";
  169. //// Arrange
  170. //// Create a mock of the configuration files in all sources
  171. //// Home directory
  172. //string homeDir = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.UserProfile), ".tui");
  173. //if (!Directory.Exists (homeDir)) {
  174. // Directory.CreateDirectory (homeDir);
  175. //}
  176. //string globalConfigFile = Path.Combine (homeDir, _configFilename);
  177. //string appSpecificConfigFile = Path.Combine (homeDir, "appname.config.json");
  178. //File.WriteAllText (globalConfigFile, "{\"Settings\": {\"TestSetting\":\"Global\"}}");
  179. //File.WriteAllText (appSpecificConfigFile, "{\"Settings\": {\"TestSetting\":\"AppSpecific\"}}");
  180. //// App directory
  181. //string appDir = Directory.GetCurrentDirectory ();
  182. //string appDirGlobalConfigFile = Path.Combine (appDir, _configFilename);
  183. //string appDirAppSpecificConfigFile = Path.Combine (appDir, "appname.config.json");
  184. //File.WriteAllText (appDirGlobalConfigFile, "{\"Settings\": {\"TestSetting\":\"GlobalAppDir\"}}");
  185. //File.WriteAllText (appDirAppSpecificConfigFile, "{\"Settings\": {\"TestSetting\":\"AppSpecificAppDir\"}}");
  186. //// App resources
  187. //// ...
  188. //// Act
  189. //ConfigurationManager.Locations = ConfigurationManager.ConfigLocation.All;
  190. //ConfigurationManager.Load ();
  191. //// Assert
  192. //// Check that the settings from the highest precedence source are loaded
  193. //Assert.Equal ("AppSpecific", ConfigurationManager.Config.Settings.TestSetting);
  194. }
  195. [Fact]
  196. public void Reset_Raises_Updated ()
  197. {
  198. ConfigLocations savedLocations = Locations;
  199. Locations = ConfigLocations.All;
  200. Reset ();
  201. Settings! ["Application.QuitKey"].PropertyValue = Key.Q;
  202. Updated += ConfigurationManager_Updated;
  203. var fired = false;
  204. void ConfigurationManager_Updated (object? sender, ConfigurationManagerEventArgs obj)
  205. {
  206. fired = true;
  207. }
  208. // Act
  209. Reset ();
  210. // assert
  211. Assert.True (fired);
  212. Updated -= ConfigurationManager_Updated;
  213. Reset ();
  214. Locations = savedLocations;
  215. }
  216. [Fact]
  217. public void Reset_and_ResetLoadWithLibraryResourcesOnly_are_same ()
  218. {
  219. Locations = ConfigLocations.Default;
  220. // arrange
  221. Reset ();
  222. Settings! ["Application.QuitKey"].PropertyValue = Key.Q;
  223. Settings ["Application.NextTabGroupKey"].PropertyValue = Key.F;
  224. Settings ["Application.PrevTabGroupKey"].PropertyValue = Key.B;
  225. Settings.Apply ();
  226. // assert apply worked
  227. Assert.Equal (KeyCode.Q, Application.QuitKey.KeyCode);
  228. Assert.Equal (KeyCode.F, Application.NextTabGroupKey.KeyCode);
  229. Assert.Equal (KeyCode.B, Application.PrevTabGroupKey.KeyCode);
  230. //act
  231. Reset ();
  232. // assert
  233. Assert.NotEmpty (Themes!);
  234. Assert.Equal ("Default", Themes.Theme);
  235. Assert.Equal (Key.Esc, Application.QuitKey);
  236. Assert.Equal (Key.F6, Application.NextTabGroupKey);
  237. Assert.Equal (Key.F6.WithShift, Application.PrevTabGroupKey);
  238. // arrange
  239. Settings ["Application.QuitKey"].PropertyValue = Key.Q;
  240. Settings ["Application.NextTabGroupKey"].PropertyValue = Key.F;
  241. Settings ["Application.PrevTabGroupKey"].PropertyValue = Key.B;
  242. Settings.Apply ();
  243. Locations = ConfigLocations.Default;
  244. // act
  245. Reset ();
  246. Load ();
  247. // assert
  248. Assert.NotEmpty (Themes);
  249. Assert.Equal ("Default", Themes.Theme);
  250. Assert.Equal (KeyCode.Esc, Application.QuitKey.KeyCode);
  251. Assert.Equal (Key.F6, Application.NextTabGroupKey);
  252. Assert.Equal (Key.F6.WithShift, Application.PrevTabGroupKey);
  253. Reset ();
  254. }
  255. [Fact]
  256. public void Reset_Resets ()
  257. {
  258. Locations = ConfigLocations.Default;
  259. Reset ();
  260. Assert.NotEmpty (Themes!);
  261. Assert.Equal ("Default", Themes.Theme);
  262. }
  263. //[Fact ()]
  264. //public void LoadFromJsonTest ()
  265. //{
  266. // Assert.True (false, "This test needs an implementation");
  267. //}
  268. //[Fact ()]
  269. //public void ToJsonTest ()
  270. //{
  271. // Assert.True (false, "This test needs an implementation");
  272. //}
  273. //[Fact ()]
  274. //public void UpdateConfigurationTest ()
  275. //{
  276. // Assert.True (false, "This test needs an implementation");
  277. //}
  278. //[Fact ()]
  279. //public void UpdateConfigurationFromFileTest ()
  280. //{
  281. // Assert.True (false, "This test needs an implementation");
  282. //}
  283. //[Fact ()]
  284. //public void SaveHardCodedDefaultsTest ()
  285. //{
  286. // Assert.True (false, "This test needs an implementation");
  287. //}
  288. //[Fact ()]
  289. //public void LoadGlobalFromLibraryResourceTest ()
  290. //{
  291. // Assert.True (false, "This test needs an implementation");
  292. //}
  293. //[Fact ()]
  294. //public void LoadGlobalFromAppDirectoryTest ()
  295. //{
  296. // Assert.True (false, "This test needs an implementation");
  297. //}
  298. //[Fact ()]
  299. //public void LoadGlobalFromHomeDirectoryTest ()
  300. //{
  301. // Assert.True (false, "This test needs an implementation");
  302. //}
  303. //[Fact ()]
  304. //public void LoadAppFromAppResourcesTest ()
  305. //{
  306. // Assert.True (false, "This test needs an implementation");
  307. //}
  308. //[Fact ()]
  309. //public void LoadAppFromAppDirectoryTest ()
  310. //{
  311. // Assert.True (false, "This test needs an implementation");
  312. //}
  313. //[Fact ()]
  314. //public void LoadAppFromHomeDirectoryTest ()
  315. //{
  316. // Assert.True (false, "This test needs an implementation");
  317. //}
  318. //[Fact ()]
  319. //public void LoadTest ()
  320. //{
  321. // Assert.True (false, "This test needs an implementation");
  322. //}
  323. /// <summary>Save the `config.json` file; this can be used to update the file in `Terminal.Gui.Resources.config.json'.</summary>
  324. /// <remarks>
  325. /// IMPORTANT: For the file generated to be valid, this must be the ONLY test run. Config Properties are all
  326. /// static and thus can be overwritten by other tests.
  327. /// </remarks>
  328. [Fact]
  329. public void SaveDefaults ()
  330. {
  331. Initialize ();
  332. Reset ();
  333. // Get the hard coded settings
  334. GetHardCodedDefaults ();
  335. // Serialize to a JSON string
  336. string json = ToJson ();
  337. // Write the JSON string to the file
  338. File.WriteAllText ("config.json", json);
  339. }
  340. [Fact]
  341. public void TestConfigProperties ()
  342. {
  343. Locations = ConfigLocations.All;
  344. Reset ();
  345. Assert.NotEmpty (Settings!);
  346. // test that all ConfigProperties have our attribute
  347. Assert.All (
  348. Settings,
  349. item => Assert.NotEmpty (
  350. item.Value.PropertyInfo!.CustomAttributes.Where (
  351. a => a.AttributeType == typeof (SerializableConfigurationProperty)
  352. )
  353. )
  354. );
  355. #pragma warning disable xUnit2029
  356. Assert.Empty (
  357. Settings.Where (
  358. cp => cp.Value.PropertyInfo!.GetCustomAttribute (
  359. typeof (SerializableConfigurationProperty)
  360. )
  361. == null
  362. )
  363. );
  364. #pragma warning restore xUnit2029
  365. // Application is a static class
  366. PropertyInfo pi = typeof (Application).GetProperty ("QuitKey");
  367. Assert.Equal (pi, Settings ["Application.QuitKey"].PropertyInfo);
  368. // FrameView is not a static class and DefaultBorderStyle is Scope.Scheme
  369. pi = typeof (FrameView).GetProperty ("DefaultBorderStyle");
  370. Assert.False (Settings.ContainsKey ("FrameView.DefaultBorderStyle"));
  371. Assert.True (Themes! ["Default"].ContainsKey ("FrameView.DefaultBorderStyle"));
  372. Assert.Equal (pi, Themes! ["Default"] ["FrameView.DefaultBorderStyle"].PropertyInfo);
  373. }
  374. [Fact]
  375. public void TestConfigPropertyOmitClassName ()
  376. {
  377. ConfigLocations savedLocations = Locations;
  378. Locations = ConfigLocations.All;
  379. // Color.ColorSchemes is serialized as "ColorSchemes", not "Colors.ColorSchemes"
  380. PropertyInfo pi = typeof (Colors).GetProperty ("ColorSchemes");
  381. var scp = (SerializableConfigurationProperty)pi!.GetCustomAttribute (typeof (SerializableConfigurationProperty));
  382. Assert.True (scp!.Scope == typeof (ThemeScope));
  383. Assert.True (scp.OmitClassName);
  384. Reset ();
  385. Assert.Equal (pi, Themes! ["Default"] ["ColorSchemes"].PropertyInfo);
  386. Locations = savedLocations;
  387. }
  388. [Fact]
  389. [AutoInitShutdown (configLocation: ConfigLocations.Default)]
  390. public void TestConfigurationManagerInitDriver ()
  391. {
  392. Assert.Equal ("Default", Themes!.Theme);
  393. Assert.Equal (new Color (Color.White), Colors.ColorSchemes ["Base"]!.Normal.Foreground);
  394. Assert.Equal (new Color (Color.Blue), Colors.ColorSchemes ["Base"].Normal.Background);
  395. // Change Base
  396. Stream json = ToStream ();
  397. Settings!.Update (json, "TestConfigurationManagerInitDriver");
  398. Dictionary<string, ColorScheme> colorSchemes =
  399. (Dictionary<string, ColorScheme>)Themes [Themes.Theme] ["ColorSchemes"].PropertyValue;
  400. Assert.Equal (Colors.ColorSchemes ["Base"], colorSchemes! ["Base"]);
  401. Assert.Equal (Colors.ColorSchemes ["TopLevel"], colorSchemes ["TopLevel"]);
  402. Assert.Equal (Colors.ColorSchemes ["Error"], colorSchemes ["Error"]);
  403. Assert.Equal (Colors.ColorSchemes ["Dialog"], colorSchemes ["Dialog"]);
  404. Assert.Equal (Colors.ColorSchemes ["Menu"], colorSchemes ["Menu"]);
  405. Colors.ColorSchemes ["Base"] = colorSchemes ["Base"];
  406. Colors.ColorSchemes ["TopLevel"] = colorSchemes ["TopLevel"];
  407. Colors.ColorSchemes ["Error"] = colorSchemes ["Error"];
  408. Colors.ColorSchemes ["Dialog"] = colorSchemes ["Dialog"];
  409. Colors.ColorSchemes ["Menu"] = colorSchemes ["Menu"];
  410. Assert.Equal (colorSchemes ["Base"], Colors.ColorSchemes ["Base"]);
  411. Assert.Equal (colorSchemes ["TopLevel"], Colors.ColorSchemes ["TopLevel"]);
  412. Assert.Equal (colorSchemes ["Error"], Colors.ColorSchemes ["Error"]);
  413. Assert.Equal (colorSchemes ["Dialog"], Colors.ColorSchemes ["Dialog"]);
  414. Assert.Equal (colorSchemes ["Menu"], Colors.ColorSchemes ["Menu"]);
  415. }
  416. [Fact]
  417. [AutoInitShutdown (configLocation: ConfigLocations.None)]
  418. public void TestConfigurationManagerInitDriver_NoLocations ()
  419. {
  420. // TODO: Write this test
  421. }
  422. [Fact]
  423. public void TestConfigurationManagerInvalidJsonLogs ()
  424. {
  425. Application.Init (new FakeDriver ());
  426. ThrowOnJsonErrors = false;
  427. // "brown" is not a color
  428. var json = @"
  429. {
  430. ""Themes"" : [
  431. {
  432. ""Default"" : {
  433. ""ColorSchemes"": [
  434. {
  435. ""UserDefined"": {
  436. ""hotNormal"": {
  437. ""foreground"": ""brown"",
  438. ""background"": ""1234""
  439. }
  440. }
  441. }
  442. ]
  443. }
  444. }
  445. }
  446. }";
  447. Settings!.Update (json, "test");
  448. // AbNormal is not a ColorScheme attribute
  449. json = @"
  450. {
  451. ""Themes"" : [
  452. {
  453. ""Default"" : {
  454. ""ColorSchemes"": [
  455. {
  456. ""UserDefined"": {
  457. ""AbNormal"": {
  458. ""foreground"": ""green"",
  459. ""background"": ""black""
  460. }
  461. }
  462. }
  463. ]
  464. }
  465. }
  466. }
  467. }";
  468. Settings.Update (json, "test");
  469. // Modify hotNormal background only
  470. json = @"
  471. {
  472. ""Themes"" : [
  473. {
  474. ""Default"" : {
  475. ""ColorSchemes"": [
  476. {
  477. ""UserDefined"": {
  478. ""hotNormal"": {
  479. ""background"": ""cyan""
  480. }
  481. }
  482. }
  483. ]
  484. }
  485. }
  486. }
  487. }";
  488. Settings.Update (json, "test");
  489. Settings.Update ("{}}", "test");
  490. Assert.NotEqual (0, _jsonErrors.Length);
  491. Application.Shutdown ();
  492. ThrowOnJsonErrors = false;
  493. }
  494. [Fact]
  495. [AutoInitShutdown]
  496. public void TestConfigurationManagerInvalidJsonThrows ()
  497. {
  498. ThrowOnJsonErrors = true;
  499. // "yellow" is not a color
  500. var json = @"
  501. {
  502. ""Themes"" : [
  503. {
  504. ""Default"" : {
  505. ""ColorSchemes"": [
  506. {
  507. ""UserDefined"": {
  508. ""hotNormal"": {
  509. ""foreground"": ""brownish"",
  510. ""background"": ""1234""
  511. }
  512. }
  513. }
  514. ]
  515. }
  516. }
  517. ]
  518. }";
  519. var jsonException = Assert.Throws<JsonException> (() => Settings!.Update (json, "test"));
  520. Assert.Equal ("Unexpected color name: brownish.", jsonException.Message);
  521. // AbNormal is not a ColorScheme attribute
  522. json = @"
  523. {
  524. ""Themes"" : [
  525. {
  526. ""Default"" : {
  527. ""ColorSchemes"": [
  528. {
  529. ""UserDefined"": {
  530. ""AbNormal"": {
  531. ""foreground"": ""green"",
  532. ""background"": ""black""
  533. }
  534. }
  535. }
  536. ]
  537. }
  538. }
  539. ]
  540. }";
  541. jsonException = Assert.Throws<JsonException> (() => Settings!.Update (json, "test"));
  542. Assert.Equal ("Unrecognized ColorScheme Attribute name: AbNormal.", jsonException.Message);
  543. // Modify hotNormal background only
  544. json = @"
  545. {
  546. ""Themes"" : [
  547. {
  548. ""Default"" : {
  549. ""ColorSchemes"": [
  550. {
  551. ""UserDefined"": {
  552. ""hotNormal"": {
  553. ""background"": ""cyan""
  554. }
  555. }
  556. }
  557. ]
  558. }
  559. }
  560. ]
  561. }";
  562. jsonException = Assert.Throws<JsonException> (() => Settings!.Update (json, "test"));
  563. Assert.Equal ("Both Foreground and Background colors must be provided.", jsonException.Message);
  564. // Unknown property
  565. json = @"
  566. {
  567. ""Unknown"" : ""Not known""
  568. }";
  569. jsonException = Assert.Throws<JsonException> (() => Settings!.Update (json, "test"));
  570. Assert.StartsWith ("Unknown property", jsonException.Message);
  571. Assert.Equal (0, _jsonErrors.Length);
  572. ThrowOnJsonErrors = false;
  573. }
  574. [Fact]
  575. [AutoInitShutdown]
  576. public void TestConfigurationManagerToJson ()
  577. {
  578. Reset ();
  579. GetHardCodedDefaults ();
  580. Stream stream = ToStream ();
  581. Settings!.Update (stream, "TestConfigurationManagerToJson");
  582. }
  583. [Fact]
  584. public void TestConfigurationManagerUpdateFromJson ()
  585. {
  586. ConfigLocations savedLocations = Locations;
  587. Locations = ConfigLocations.All;
  588. // Arrange
  589. var json = @"
  590. {
  591. ""$schema"": ""https://gui-cs.github.io/Terminal.GuiV2Docs/schemas/tui-config-schema.json"",
  592. ""Application.QuitKey"": ""Alt-Z"",
  593. ""Theme"": ""Default"",
  594. ""Themes"": [
  595. {
  596. ""Default"": {
  597. ""ColorSchemes"": [
  598. {
  599. ""TopLevel"": {
  600. ""Normal"": {
  601. ""Foreground"": ""BrightGreen"",
  602. ""Background"": ""Black""
  603. },
  604. ""Focus"": {
  605. ""Foreground"": ""White"",
  606. ""Background"": ""Cyan""
  607. },
  608. ""HotNormal"": {
  609. ""Foreground"": ""Yellow"",
  610. ""Background"": ""Black""
  611. },
  612. ""HotFocus"": {
  613. ""Foreground"": ""Blue"",
  614. ""Background"": ""Cyan""
  615. },
  616. ""Disabled"": {
  617. ""Foreground"": ""DarkGray"",
  618. ""Background"": ""Black""
  619. }
  620. }
  621. },
  622. {
  623. ""Base"": {
  624. ""Normal"": {
  625. ""Foreground"": ""White"",
  626. ""Background"": ""Blue""
  627. },
  628. ""Focus"": {
  629. ""Foreground"": ""Black"",
  630. ""Background"": ""Gray""
  631. },
  632. ""HotNormal"": {
  633. ""Foreground"": ""BrightCyan"",
  634. ""Background"": ""Blue""
  635. },
  636. ""HotFocus"": {
  637. ""Foreground"": ""BrightBlue"",
  638. ""Background"": ""Gray""
  639. },
  640. ""Disabled"": {
  641. ""Foreground"": ""DarkGray"",
  642. ""Background"": ""Blue""
  643. }
  644. }
  645. },
  646. {
  647. ""Dialog"": {
  648. ""Normal"": {
  649. ""Foreground"": ""Black"",
  650. ""Background"": ""Gray""
  651. },
  652. ""Focus"": {
  653. ""Foreground"": ""White"",
  654. ""Background"": ""DarkGray""
  655. },
  656. ""HotNormal"": {
  657. ""Foreground"": ""Blue"",
  658. ""Background"": ""Gray""
  659. },
  660. ""HotFocus"": {
  661. ""Foreground"": ""BrightYellow"",
  662. ""Background"": ""DarkGray""
  663. },
  664. ""Disabled"": {
  665. ""Foreground"": ""Gray"",
  666. ""Background"": ""DarkGray""
  667. }
  668. }
  669. },
  670. {
  671. ""Menu"": {
  672. ""Normal"": {
  673. ""Foreground"": ""White"",
  674. ""Background"": ""DarkGray""
  675. },
  676. ""Focus"": {
  677. ""Foreground"": ""White"",
  678. ""Background"": ""Black""
  679. },
  680. ""HotNormal"": {
  681. ""Foreground"": ""BrightYellow"",
  682. ""Background"": ""DarkGray""
  683. },
  684. ""HotFocus"": {
  685. ""Foreground"": ""BrightYellow"",
  686. ""Background"": ""Black""
  687. },
  688. ""Disabled"": {
  689. ""Foreground"": ""Gray"",
  690. ""Background"": ""DarkGray""
  691. }
  692. }
  693. },
  694. {
  695. ""Error"": {
  696. ""Normal"": {
  697. ""Foreground"": ""Red"",
  698. ""Background"": ""White""
  699. },
  700. ""Focus"": {
  701. ""Foreground"": ""Black"",
  702. ""Background"": ""BrightRed""
  703. },
  704. ""HotNormal"": {
  705. ""Foreground"": ""Black"",
  706. ""Background"": ""White""
  707. },
  708. ""HotFocus"": {
  709. ""Foreground"": ""White"",
  710. ""Background"": ""BrightRed""
  711. },
  712. ""Disabled"": {
  713. ""Foreground"": ""DarkGray"",
  714. ""Background"": ""White""
  715. }
  716. }
  717. }
  718. ]
  719. }
  720. }
  721. ]
  722. }
  723. ";
  724. Reset ();
  725. ThrowOnJsonErrors = true;
  726. Settings!.Update (json, "TestConfigurationManagerUpdateFromJson");
  727. Assert.Equal (KeyCode.Esc, Application.QuitKey.KeyCode);
  728. Assert.Equal (KeyCode.Z | KeyCode.AltMask, ((Key)Settings ["Application.QuitKey"].PropertyValue)!.KeyCode);
  729. Assert.Equal ("Default", Themes!.Theme);
  730. Assert.Equal (new Color (Color.White), Colors.ColorSchemes ["Base"]!.Normal.Foreground);
  731. Assert.Equal (new Color (Color.Blue), Colors.ColorSchemes ["Base"].Normal.Background);
  732. Dictionary<string, ColorScheme> colorSchemes =
  733. (Dictionary<string, ColorScheme>)Themes.First ().Value ["ColorSchemes"].PropertyValue;
  734. Assert.Equal (new Color (Color.White), colorSchemes! ["Base"].Normal.Foreground);
  735. Assert.Equal (new Color (Color.Blue), colorSchemes ["Base"].Normal.Background);
  736. // Now re-apply
  737. Apply ();
  738. Assert.Equal (KeyCode.Z | KeyCode.AltMask, Application.QuitKey.KeyCode);
  739. Assert.Equal ("Default", Themes.Theme);
  740. Assert.Equal (new Color (Color.White), Colors.ColorSchemes ["Base"].Normal.Foreground);
  741. Assert.Equal (new Color (Color.Blue), Colors.ColorSchemes ["Base"].Normal.Background);
  742. Reset ();
  743. Locations = savedLocations;
  744. }
  745. [Fact]
  746. public void UseWithoutResetDoesNotThrow ()
  747. {
  748. Initialize ();
  749. _ = Settings;
  750. }
  751. }