ConfigurationMangerTests.cs 27 KB

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