ConfigurationMangerTests.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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. Assert.Empty (
  321. Settings.Where (
  322. cp => cp.Value.PropertyInfo!.GetCustomAttribute (
  323. typeof (SerializableConfigurationProperty)
  324. )
  325. == null
  326. )
  327. );
  328. // Application is a static class
  329. PropertyInfo pi = typeof (Application).GetProperty ("QuitKey");
  330. Assert.Equal (pi, Settings ["Application.QuitKey"].PropertyInfo);
  331. // FrameView is not a static class and DefaultBorderStyle is Scope.Scheme
  332. pi = typeof (FrameView).GetProperty ("DefaultBorderStyle");
  333. Assert.False (Settings.ContainsKey ("FrameView.DefaultBorderStyle"));
  334. Assert.True (Themes! ["Default"].ContainsKey ("FrameView.DefaultBorderStyle"));
  335. Assert.Equal (pi, Themes! ["Default"] ["FrameView.DefaultBorderStyle"].PropertyInfo);
  336. }
  337. [Fact]
  338. public void TestConfigPropertyOmitClassName ()
  339. {
  340. // Color.ColorSchemes is serialized as "ColorSchemes", not "Colors.ColorSchemes"
  341. PropertyInfo pi = typeof (Colors).GetProperty ("ColorSchemes");
  342. var scp = (SerializableConfigurationProperty)pi!.GetCustomAttribute (typeof (SerializableConfigurationProperty));
  343. Assert.True (scp!.Scope == typeof (ThemeScope));
  344. Assert.True (scp.OmitClassName);
  345. Reset ();
  346. Assert.Equal (pi, Themes! ["Default"] ["ColorSchemes"].PropertyInfo);
  347. }
  348. [Fact]
  349. [AutoInitShutdown (configLocation: ConfigLocations.DefaultOnly)]
  350. public void TestConfigurationManagerInitDriver ()
  351. {
  352. Assert.Equal ("Default", Themes!.Theme);
  353. Assert.Equal (new Color (Color.White), Colors.ColorSchemes ["Base"]!.Normal.Foreground);
  354. Assert.Equal (new Color (Color.Blue), Colors.ColorSchemes ["Base"].Normal.Background);
  355. // Change Base
  356. Stream json = ToStream ();
  357. Settings!.Update (json, "TestConfigurationManagerInitDriver");
  358. Dictionary<string, ColorScheme> colorSchemes =
  359. (Dictionary<string, ColorScheme>)Themes [Themes.Theme] ["ColorSchemes"].PropertyValue;
  360. Assert.Equal (Colors.ColorSchemes ["Base"], colorSchemes! ["Base"]);
  361. Assert.Equal (Colors.ColorSchemes ["TopLevel"], colorSchemes ["TopLevel"]);
  362. Assert.Equal (Colors.ColorSchemes ["Error"], colorSchemes ["Error"]);
  363. Assert.Equal (Colors.ColorSchemes ["Dialog"], colorSchemes ["Dialog"]);
  364. Assert.Equal (Colors.ColorSchemes ["Menu"], colorSchemes ["Menu"]);
  365. Colors.ColorSchemes ["Base"] = colorSchemes ["Base"];
  366. Colors.ColorSchemes ["TopLevel"] = colorSchemes ["TopLevel"];
  367. Colors.ColorSchemes ["Error"] = colorSchemes ["Error"];
  368. Colors.ColorSchemes ["Dialog"] = colorSchemes ["Dialog"];
  369. Colors.ColorSchemes ["Menu"] = colorSchemes ["Menu"];
  370. Assert.Equal (colorSchemes ["Base"], Colors.ColorSchemes ["Base"]);
  371. Assert.Equal (colorSchemes ["TopLevel"], Colors.ColorSchemes ["TopLevel"]);
  372. Assert.Equal (colorSchemes ["Error"], Colors.ColorSchemes ["Error"]);
  373. Assert.Equal (colorSchemes ["Dialog"], Colors.ColorSchemes ["Dialog"]);
  374. Assert.Equal (colorSchemes ["Menu"], Colors.ColorSchemes ["Menu"]);
  375. }
  376. [Fact]
  377. [AutoInitShutdown (configLocation: ConfigLocations.None)]
  378. public void TestConfigurationManagerInitDriver_NoLocations () { }
  379. [Fact]
  380. public void TestConfigurationManagerInvalidJsonLogs ()
  381. {
  382. Application.Init (new FakeDriver ());
  383. ThrowOnJsonErrors = false;
  384. // "brown" is not a color
  385. var json = @"
  386. {
  387. ""Themes"" : [
  388. {
  389. ""Default"" : {
  390. ""ColorSchemes"": [
  391. {
  392. ""UserDefined"": {
  393. ""hotNormal"": {
  394. ""foreground"": ""brown"",
  395. ""background"": ""1234""
  396. }
  397. }
  398. }
  399. ]
  400. }
  401. }
  402. }
  403. }";
  404. Settings!.Update (json, "test");
  405. // AbNormal is not a ColorScheme attribute
  406. json = @"
  407. {
  408. ""Themes"" : [
  409. {
  410. ""Default"" : {
  411. ""ColorSchemes"": [
  412. {
  413. ""UserDefined"": {
  414. ""AbNormal"": {
  415. ""foreground"": ""green"",
  416. ""background"": ""black""
  417. }
  418. }
  419. }
  420. ]
  421. }
  422. }
  423. }
  424. }";
  425. Settings.Update (json, "test");
  426. // Modify hotNormal background only
  427. json = @"
  428. {
  429. ""Themes"" : [
  430. {
  431. ""Default"" : {
  432. ""ColorSchemes"": [
  433. {
  434. ""UserDefined"": {
  435. ""hotNormal"": {
  436. ""background"": ""cyan""
  437. }
  438. }
  439. }
  440. ]
  441. }
  442. }
  443. }
  444. }";
  445. Settings.Update (json, "test");
  446. Settings.Update ("{}}", "test");
  447. Assert.NotEqual (0, _jsonErrors.Length);
  448. Application.Shutdown ();
  449. ThrowOnJsonErrors = false;
  450. }
  451. [Fact]
  452. [AutoInitShutdown]
  453. public void TestConfigurationManagerInvalidJsonThrows ()
  454. {
  455. ThrowOnJsonErrors = true;
  456. // "yellow" is not a color
  457. var json = @"
  458. {
  459. ""Themes"" : [
  460. {
  461. ""Default"" : {
  462. ""ColorSchemes"": [
  463. {
  464. ""UserDefined"": {
  465. ""hotNormal"": {
  466. ""foreground"": ""brown"",
  467. ""background"": ""1234""
  468. }
  469. }
  470. }
  471. ]
  472. }
  473. }
  474. ]
  475. }";
  476. var jsonException = Assert.Throws<JsonException> (() => Settings!.Update (json, "test"));
  477. Assert.Equal ("Unexpected color name: brown.", jsonException.Message);
  478. // AbNormal is not a ColorScheme attribute
  479. json = @"
  480. {
  481. ""Themes"" : [
  482. {
  483. ""Default"" : {
  484. ""ColorSchemes"": [
  485. {
  486. ""UserDefined"": {
  487. ""AbNormal"": {
  488. ""foreground"": ""green"",
  489. ""background"": ""black""
  490. }
  491. }
  492. }
  493. ]
  494. }
  495. }
  496. ]
  497. }";
  498. jsonException = Assert.Throws<JsonException> (() => Settings!.Update (json, "test"));
  499. Assert.Equal ("Unrecognized ColorScheme Attribute name: AbNormal.", jsonException.Message);
  500. // Modify hotNormal background only
  501. json = @"
  502. {
  503. ""Themes"" : [
  504. {
  505. ""Default"" : {
  506. ""ColorSchemes"": [
  507. {
  508. ""UserDefined"": {
  509. ""hotNormal"": {
  510. ""background"": ""cyan""
  511. }
  512. }
  513. }
  514. ]
  515. }
  516. }
  517. ]
  518. }";
  519. jsonException = Assert.Throws<JsonException> (() => Settings!.Update (json, "test"));
  520. Assert.Equal ("Both Foreground and Background colors must be provided.", jsonException.Message);
  521. // Unknown property
  522. json = @"
  523. {
  524. ""Unknown"" : ""Not known""
  525. }";
  526. jsonException = Assert.Throws<JsonException> (() => Settings!.Update (json, "test"));
  527. Assert.StartsWith ("Unknown property", jsonException.Message);
  528. Assert.Equal (0, _jsonErrors.Length);
  529. ThrowOnJsonErrors = false;
  530. }
  531. [Fact]
  532. [AutoInitShutdown]
  533. public void TestConfigurationManagerToJson ()
  534. {
  535. Reset ();
  536. GetHardCodedDefaults ();
  537. Stream stream = ToStream ();
  538. Settings!.Update (stream, "TestConfigurationManagerToJson");
  539. }
  540. [Fact]
  541. public void TestConfigurationManagerUpdateFromJson ()
  542. {
  543. // Arrange
  544. var json = @"
  545. {
  546. ""$schema"": ""https://gui-cs.github.io/Terminal.Gui/schemas/tui-config-schema.json"",
  547. ""Application.QuitKey"": ""Alt-Z"",
  548. ""Theme"": ""Default"",
  549. ""Themes"": [
  550. {
  551. ""Default"": {
  552. ""ColorSchemes"": [
  553. {
  554. ""TopLevel"": {
  555. ""Normal"": {
  556. ""Foreground"": ""BrightGreen"",
  557. ""Background"": ""Black""
  558. },
  559. ""Focus"": {
  560. ""Foreground"": ""White"",
  561. ""Background"": ""Cyan""
  562. },
  563. ""HotNormal"": {
  564. ""Foreground"": ""Yellow"",
  565. ""Background"": ""Black""
  566. },
  567. ""HotFocus"": {
  568. ""Foreground"": ""Blue"",
  569. ""Background"": ""Cyan""
  570. },
  571. ""Disabled"": {
  572. ""Foreground"": ""DarkGray"",
  573. ""Background"": ""Black""
  574. }
  575. }
  576. },
  577. {
  578. ""Base"": {
  579. ""Normal"": {
  580. ""Foreground"": ""White"",
  581. ""Background"": ""Blue""
  582. },
  583. ""Focus"": {
  584. ""Foreground"": ""Black"",
  585. ""Background"": ""Gray""
  586. },
  587. ""HotNormal"": {
  588. ""Foreground"": ""BrightCyan"",
  589. ""Background"": ""Blue""
  590. },
  591. ""HotFocus"": {
  592. ""Foreground"": ""BrightBlue"",
  593. ""Background"": ""Gray""
  594. },
  595. ""Disabled"": {
  596. ""Foreground"": ""DarkGray"",
  597. ""Background"": ""Blue""
  598. }
  599. }
  600. },
  601. {
  602. ""Dialog"": {
  603. ""Normal"": {
  604. ""Foreground"": ""Black"",
  605. ""Background"": ""Gray""
  606. },
  607. ""Focus"": {
  608. ""Foreground"": ""White"",
  609. ""Background"": ""DarkGray""
  610. },
  611. ""HotNormal"": {
  612. ""Foreground"": ""Blue"",
  613. ""Background"": ""Gray""
  614. },
  615. ""HotFocus"": {
  616. ""Foreground"": ""BrightYellow"",
  617. ""Background"": ""DarkGray""
  618. },
  619. ""Disabled"": {
  620. ""Foreground"": ""Gray"",
  621. ""Background"": ""DarkGray""
  622. }
  623. }
  624. },
  625. {
  626. ""Menu"": {
  627. ""Normal"": {
  628. ""Foreground"": ""White"",
  629. ""Background"": ""DarkGray""
  630. },
  631. ""Focus"": {
  632. ""Foreground"": ""White"",
  633. ""Background"": ""Black""
  634. },
  635. ""HotNormal"": {
  636. ""Foreground"": ""BrightYellow"",
  637. ""Background"": ""DarkGray""
  638. },
  639. ""HotFocus"": {
  640. ""Foreground"": ""BrightYellow"",
  641. ""Background"": ""Black""
  642. },
  643. ""Disabled"": {
  644. ""Foreground"": ""Gray"",
  645. ""Background"": ""DarkGray""
  646. }
  647. }
  648. },
  649. {
  650. ""Error"": {
  651. ""Normal"": {
  652. ""Foreground"": ""Red"",
  653. ""Background"": ""White""
  654. },
  655. ""Focus"": {
  656. ""Foreground"": ""Black"",
  657. ""Background"": ""BrightRed""
  658. },
  659. ""HotNormal"": {
  660. ""Foreground"": ""Black"",
  661. ""Background"": ""White""
  662. },
  663. ""HotFocus"": {
  664. ""Foreground"": ""White"",
  665. ""Background"": ""BrightRed""
  666. },
  667. ""Disabled"": {
  668. ""Foreground"": ""DarkGray"",
  669. ""Background"": ""White""
  670. }
  671. }
  672. }
  673. ]
  674. }
  675. }
  676. ]
  677. }
  678. ";
  679. Reset ();
  680. ThrowOnJsonErrors = true;
  681. Settings!.Update (json, "TestConfigurationManagerUpdateFromJson");
  682. Assert.Equal (KeyCode.Esc, Application.QuitKey.KeyCode);
  683. Assert.Equal (KeyCode.Z | KeyCode.AltMask, ((Key)Settings ["Application.QuitKey"].PropertyValue)!.KeyCode);
  684. Assert.Equal ("Default", Themes!.Theme);
  685. Assert.Equal (new Color (Color.White), Colors.ColorSchemes ["Base"]!.Normal.Foreground);
  686. Assert.Equal (new Color (Color.Blue), Colors.ColorSchemes ["Base"].Normal.Background);
  687. Dictionary<string, ColorScheme> colorSchemes =
  688. (Dictionary<string, ColorScheme>)Themes.First ().Value ["ColorSchemes"].PropertyValue;
  689. Assert.Equal (new Color (Color.White), colorSchemes! ["Base"].Normal.Foreground);
  690. Assert.Equal (new Color (Color.Blue), colorSchemes ["Base"].Normal.Background);
  691. // Now re-apply
  692. Apply ();
  693. Assert.Equal (KeyCode.Z | KeyCode.AltMask, Application.QuitKey.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. Reset ();
  698. }
  699. [Fact]
  700. public void UseWithoutResetDoesNotThrow ()
  701. {
  702. Initialize ();
  703. _ = Settings;
  704. }
  705. }