ConfigurationMangerTests.cs 26 KB

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