ConfigurationMangerTests.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. using System.Reflection;
  2. using System.Text.Json;
  3. using static Terminal.Gui.ConfigurationManager;
  4. namespace Terminal.Gui.ConfigurationTests;
  5. public class ConfigurationManagerTests
  6. {
  7. public static readonly JsonSerializerOptions _jsonOptions = new ()
  8. {
  9. Converters = { new AttributeJsonConverter (), new ColorJsonConverter () }
  10. };
  11. [Fact]
  12. public void Apply_FiresApplied ()
  13. {
  14. Reset ();
  15. Applied += ConfigurationManager_Applied;
  16. var fired = false;
  17. void ConfigurationManager_Applied (object sender, ConfigurationManagerEventArgs obj)
  18. {
  19. fired = true;
  20. // assert
  21. Assert.Equal (KeyCode.Q, Application.QuitKey.KeyCode);
  22. Assert.Equal (KeyCode.F, Application.AlternateForwardKey.KeyCode);
  23. Assert.Equal (KeyCode.B, Application.AlternateBackwardKey.KeyCode);
  24. Assert.True (Application.IsMouseDisabled);
  25. }
  26. // act
  27. Settings ["Application.QuitKey"].PropertyValue = Key.Q;
  28. Settings ["Application.AlternateForwardKey"].PropertyValue = Key.F;
  29. Settings ["Application.AlternateBackwardKey"].PropertyValue = Key.B;
  30. Settings ["Application.IsMouseDisabled"].PropertyValue = true;
  31. Apply ();
  32. // assert
  33. Assert.True (fired);
  34. Applied -= ConfigurationManager_Applied;
  35. }
  36. [Fact]
  37. public void DeepMemberwiseCopyTest ()
  38. {
  39. // Value types
  40. var stringDest = "Destination";
  41. var stringSrc = "Source";
  42. object stringCopy = DeepMemberwiseCopy (stringSrc, stringDest);
  43. Assert.Equal (stringSrc, stringCopy);
  44. stringDest = "Destination";
  45. stringSrc = "Destination";
  46. stringCopy = DeepMemberwiseCopy (stringSrc, stringDest);
  47. Assert.Equal (stringSrc, stringCopy);
  48. stringDest = "Destination";
  49. stringSrc = null;
  50. stringCopy = DeepMemberwiseCopy (stringSrc, stringDest);
  51. Assert.Equal (stringSrc, stringCopy);
  52. stringDest = "Destination";
  53. stringSrc = string.Empty;
  54. stringCopy = DeepMemberwiseCopy (stringSrc, stringDest);
  55. Assert.Equal (stringSrc, stringCopy);
  56. var boolDest = true;
  57. var boolSrc = false;
  58. object boolCopy = DeepMemberwiseCopy (boolSrc, boolDest);
  59. Assert.Equal (boolSrc, boolCopy);
  60. boolDest = false;
  61. boolSrc = true;
  62. boolCopy = DeepMemberwiseCopy (boolSrc, boolDest);
  63. Assert.Equal (boolSrc, boolCopy);
  64. boolDest = true;
  65. boolSrc = true;
  66. boolCopy = DeepMemberwiseCopy (boolSrc, boolDest);
  67. Assert.Equal (boolSrc, boolCopy);
  68. boolDest = false;
  69. boolSrc = false;
  70. boolCopy = DeepMemberwiseCopy (boolSrc, boolDest);
  71. Assert.Equal (boolSrc, boolCopy);
  72. // Structs
  73. var attrDest = new Attribute (Color.Black);
  74. var attrSrc = new Attribute (Color.White);
  75. object attrCopy = DeepMemberwiseCopy (attrSrc, attrDest);
  76. Assert.Equal (attrSrc, attrCopy);
  77. // Classes
  78. var colorschemeDest = new ColorScheme { Disabled = new Attribute (Color.Black) };
  79. var colorschemeSrc = new ColorScheme { Disabled = new Attribute (Color.White) };
  80. object colorschemeCopy = DeepMemberwiseCopy (colorschemeSrc, colorschemeDest);
  81. Assert.Equal (colorschemeSrc, colorschemeCopy);
  82. // Dictionaries
  83. Dictionary<string, Attribute> dictDest = new () { { "Disabled", new Attribute (Color.Black) } };
  84. Dictionary<string, Attribute> dictSrc = new () { { "Disabled", new Attribute (Color.White) } };
  85. Dictionary<string, Attribute> dictCopy = (Dictionary<string, Attribute>)DeepMemberwiseCopy (dictSrc, dictDest);
  86. Assert.Equal (dictSrc, dictCopy);
  87. dictDest = new Dictionary<string, Attribute> { { "Disabled", new Attribute (Color.Black) } };
  88. dictSrc = new Dictionary<string, Attribute>
  89. {
  90. { "Disabled", new Attribute (Color.White) }, { "Normal", new Attribute (Color.Blue) }
  91. };
  92. dictCopy = (Dictionary<string, Attribute>)DeepMemberwiseCopy (dictSrc, dictDest);
  93. Assert.Equal (dictSrc, dictCopy);
  94. // src adds an item
  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 (2, dictCopy.Count);
  102. Assert.Equal (dictSrc ["Disabled"], dictCopy ["Disabled"]);
  103. Assert.Equal (dictSrc ["Normal"], dictCopy ["Normal"]);
  104. // src updates only one item
  105. dictDest = new Dictionary<string, Attribute>
  106. {
  107. { "Disabled", new Attribute (Color.Black) }, { "Normal", new Attribute (Color.White) }
  108. };
  109. dictSrc = new Dictionary<string, Attribute> { { "Disabled", new Attribute (Color.White) } };
  110. dictCopy = (Dictionary<string, Attribute>)DeepMemberwiseCopy (dictSrc, dictDest);
  111. Assert.Equal (2, dictCopy.Count);
  112. Assert.Equal (dictSrc ["Disabled"], dictCopy ["Disabled"]);
  113. Assert.Equal (dictDest ["Normal"], dictCopy ["Normal"]);
  114. }
  115. [Fact]
  116. public void Load_FiresUpdated ()
  117. {
  118. Reset ();
  119. Settings ["Application.QuitKey"].PropertyValue = Key.Q;
  120. Settings ["Application.AlternateForwardKey"].PropertyValue = Key.F;
  121. Settings ["Application.AlternateBackwardKey"].PropertyValue = Key.B;
  122. Settings ["Application.IsMouseDisabled"].PropertyValue = true;
  123. Updated += ConfigurationManager_Updated;
  124. var fired = false;
  125. void ConfigurationManager_Updated (object sender, ConfigurationManagerEventArgs obj)
  126. {
  127. fired = true;
  128. // assert
  129. Assert.Equal (KeyCode.Q | KeyCode.CtrlMask, ((Key)Settings ["Application.QuitKey"].PropertyValue).KeyCode);
  130. Assert.Equal (
  131. KeyCode.PageDown | KeyCode.CtrlMask,
  132. ((Key)Settings ["Application.AlternateForwardKey"].PropertyValue).KeyCode
  133. );
  134. Assert.Equal (
  135. KeyCode.PageUp | KeyCode.CtrlMask,
  136. ((Key)Settings ["Application.AlternateBackwardKey"].PropertyValue).KeyCode
  137. );
  138. Assert.False ((bool)Settings ["Application.IsMouseDisabled"].PropertyValue);
  139. }
  140. Load (true);
  141. // assert
  142. Assert.True (fired);
  143. Updated -= ConfigurationManager_Updated;
  144. }
  145. [Fact]
  146. [AutoInitShutdown]
  147. public void LoadConfigurationFromAllSources_ShouldLoadSettingsFromAllSources ()
  148. {
  149. //var _configFilename = "config.json";
  150. //// Arrange
  151. //// Create a mock of the configuration files in all sources
  152. //// Home directory
  153. //string homeDir = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.UserProfile), ".tui");
  154. //if (!Directory.Exists (homeDir)) {
  155. // Directory.CreateDirectory (homeDir);
  156. //}
  157. //string globalConfigFile = Path.Combine (homeDir, _configFilename);
  158. //string appSpecificConfigFile = Path.Combine (homeDir, "appname.config.json");
  159. //File.WriteAllText (globalConfigFile, "{\"Settings\": {\"TestSetting\":\"Global\"}}");
  160. //File.WriteAllText (appSpecificConfigFile, "{\"Settings\": {\"TestSetting\":\"AppSpecific\"}}");
  161. //// App directory
  162. //string appDir = Directory.GetCurrentDirectory ();
  163. //string appDirGlobalConfigFile = Path.Combine (appDir, _configFilename);
  164. //string appDirAppSpecificConfigFile = Path.Combine (appDir, "appname.config.json");
  165. //File.WriteAllText (appDirGlobalConfigFile, "{\"Settings\": {\"TestSetting\":\"GlobalAppDir\"}}");
  166. //File.WriteAllText (appDirAppSpecificConfigFile, "{\"Settings\": {\"TestSetting\":\"AppSpecificAppDir\"}}");
  167. //// App resources
  168. //// ...
  169. //// Act
  170. //ConfigurationManager.Locations = ConfigurationManager.ConfigLocation.All;
  171. //ConfigurationManager.Load ();
  172. //// Assert
  173. //// Check that the settings from the highest precedence source are loaded
  174. //Assert.Equal ("AppSpecific", ConfigurationManager.Config.Settings.TestSetting);
  175. }
  176. [Fact]
  177. public void Reset_and_ResetLoadWithLibraryResourcesOnly_are_same ()
  178. {
  179. Locations = ConfigLocations.DefaultOnly;
  180. // arrange
  181. Reset ();
  182. Settings ["Application.QuitKey"].PropertyValue = Key.Q;
  183. Settings ["Application.AlternateForwardKey"].PropertyValue = Key.F;
  184. Settings ["Application.AlternateBackwardKey"].PropertyValue = Key.B;
  185. Settings ["Application.IsMouseDisabled"].PropertyValue = true;
  186. Settings.Apply ();
  187. // assert apply worked
  188. Assert.Equal (KeyCode.Q, Application.QuitKey.KeyCode);
  189. Assert.Equal (KeyCode.F, Application.AlternateForwardKey.KeyCode);
  190. Assert.Equal (KeyCode.B, Application.AlternateBackwardKey.KeyCode);
  191. Assert.True (Application.IsMouseDisabled);
  192. //act
  193. Reset ();
  194. // assert
  195. Assert.NotEmpty (Themes);
  196. Assert.Equal ("Default", Themes.Theme);
  197. Assert.Equal (KeyCode.Q | KeyCode.CtrlMask, Application.QuitKey.KeyCode);
  198. Assert.Equal (KeyCode.PageDown | KeyCode.CtrlMask, Application.AlternateForwardKey.KeyCode);
  199. Assert.Equal (KeyCode.PageUp | KeyCode.CtrlMask, Application.AlternateBackwardKey.KeyCode);
  200. Assert.False (Application.IsMouseDisabled);
  201. // arrange
  202. Settings ["Application.QuitKey"].PropertyValue = Key.Q;
  203. Settings ["Application.AlternateForwardKey"].PropertyValue = Key.F;
  204. Settings ["Application.AlternateBackwardKey"].PropertyValue = Key.B;
  205. Settings ["Application.IsMouseDisabled"].PropertyValue = true;
  206. Settings.Apply ();
  207. Locations = ConfigLocations.DefaultOnly;
  208. // act
  209. Reset ();
  210. Load ();
  211. // assert
  212. Assert.NotEmpty (Themes);
  213. Assert.Equal ("Default", Themes.Theme);
  214. Assert.Equal (KeyCode.Q | KeyCode.CtrlMask, Application.QuitKey.KeyCode);
  215. Assert.Equal (KeyCode.PageDown | KeyCode.CtrlMask, Application.AlternateForwardKey.KeyCode);
  216. Assert.Equal (KeyCode.PageUp | KeyCode.CtrlMask, Application.AlternateBackwardKey.KeyCode);
  217. Assert.False (Application.IsMouseDisabled);
  218. }
  219. [Fact]
  220. public void Reset_Resets ()
  221. {
  222. Locations = ConfigLocations.DefaultOnly;
  223. Reset ();
  224. Assert.NotEmpty (Themes);
  225. Assert.Equal ("Default", Themes.Theme);
  226. }
  227. //[Fact ()]
  228. //public void LoadFromJsonTest ()
  229. //{
  230. // Assert.True (false, "This test needs an implementation");
  231. //}
  232. //[Fact ()]
  233. //public void ToJsonTest ()
  234. //{
  235. // Assert.True (false, "This test needs an implementation");
  236. //}
  237. //[Fact ()]
  238. //public void UpdateConfigurationTest ()
  239. //{
  240. // Assert.True (false, "This test needs an implementation");
  241. //}
  242. //[Fact ()]
  243. //public void UpdateConfigurationFromFileTest ()
  244. //{
  245. // Assert.True (false, "This test needs an implementation");
  246. //}
  247. //[Fact ()]
  248. //public void SaveHardCodedDefaultsTest ()
  249. //{
  250. // Assert.True (false, "This test needs an implementation");
  251. //}
  252. //[Fact ()]
  253. //public void LoadGlobalFromLibraryResourceTest ()
  254. //{
  255. // Assert.True (false, "This test needs an implementation");
  256. //}
  257. //[Fact ()]
  258. //public void LoadGlobalFromAppDirectoryTest ()
  259. //{
  260. // Assert.True (false, "This test needs an implementation");
  261. //}
  262. //[Fact ()]
  263. //public void LoadGlobalFromHomeDirectoryTest ()
  264. //{
  265. // Assert.True (false, "This test needs an implementation");
  266. //}
  267. //[Fact ()]
  268. //public void LoadAppFromAppResourcesTest ()
  269. //{
  270. // Assert.True (false, "This test needs an implementation");
  271. //}
  272. //[Fact ()]
  273. //public void LoadAppFromAppDirectoryTest ()
  274. //{
  275. // Assert.True (false, "This test needs an implementation");
  276. //}
  277. //[Fact ()]
  278. //public void LoadAppFromHomeDirectoryTest ()
  279. //{
  280. // Assert.True (false, "This test needs an implementation");
  281. //}
  282. //[Fact ()]
  283. //public void LoadTest ()
  284. //{
  285. // Assert.True (false, "This test needs an implementation");
  286. //}
  287. /// <summary>Save the `config.json` file; this can be used to update the file in `Terminal.Gui.Resources.config.json'.</summary>
  288. /// <remarks>
  289. /// IMPORTANT: For the file generated to be valid, this must be the ONLY test run. Config Properties are all
  290. /// static and thus can be overwritten by other tests.
  291. /// </remarks>
  292. [Fact]
  293. public void SaveDefaults ()
  294. {
  295. Initialize ();
  296. Reset ();
  297. // Get the hard coded settings
  298. GetHardCodedDefaults ();
  299. // Serialize to a JSON string
  300. string json = ToJson ();
  301. // Write the JSON string to the file
  302. File.WriteAllText ("config.json", json);
  303. }
  304. [Fact]
  305. public void TestConfigProperties ()
  306. {
  307. Locations = ConfigLocations.All;
  308. Reset ();
  309. Assert.NotEmpty (Settings);
  310. // test that all ConfigProperites have our attribute
  311. Assert.All (
  312. Settings,
  313. item => Assert.NotEmpty (
  314. item.Value.PropertyInfo.CustomAttributes.Where (
  315. a => a.AttributeType == typeof (SerializableConfigurationProperty)
  316. )
  317. )
  318. );
  319. Assert.Empty (
  320. Settings.Where (
  321. cp => cp.Value.PropertyInfo.GetCustomAttribute (
  322. typeof (SerializableConfigurationProperty)
  323. )
  324. == null
  325. )
  326. );
  327. // Application is a static class
  328. PropertyInfo pi = typeof (Application).GetProperty ("QuitKey");
  329. Assert.Equal (pi, Settings ["Application.QuitKey"].PropertyInfo);
  330. // FrameView is not a static class and DefaultBorderStyle is Scope.Scheme
  331. pi = typeof (FrameView).GetProperty ("DefaultBorderStyle");
  332. Assert.False (Settings.ContainsKey ("FrameView.DefaultBorderStyle"));
  333. Assert.True (Themes ["Default"].ContainsKey ("FrameView.DefaultBorderStyle"));
  334. }
  335. [Fact]
  336. public void TestConfigPropertyOmitClassName ()
  337. {
  338. // Color.ColorShemes is serialzied as "ColorSchemes", not "Colors.ColorSchemes"
  339. PropertyInfo pi = typeof (Colors).GetProperty ("ColorSchemes");
  340. var scp = (SerializableConfigurationProperty)pi.GetCustomAttribute (typeof (SerializableConfigurationProperty));
  341. Assert.True (scp.Scope == typeof (ThemeScope));
  342. Assert.True (scp.OmitClassName);
  343. Reset ();
  344. Assert.Equal (pi, Themes ["Default"] ["ColorSchemes"].PropertyInfo);
  345. }
  346. [Fact]
  347. [AutoInitShutdown (configLocation: ConfigLocations.DefaultOnly)]
  348. public void TestConfigurationManagerInitDriver ()
  349. {
  350. Assert.Equal ("Default", Themes.Theme);
  351. Assert.True (Themes.ContainsKey ("Default"));
  352. Assert.Equal (KeyCode.Q | KeyCode.CtrlMask, Application.QuitKey.KeyCode);
  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 proeprty
  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. ""Dialog.DefaultButtonAlignment"": ""Center""
  675. }
  676. }
  677. ]
  678. }
  679. ";
  680. Reset ();
  681. ThrowOnJsonErrors = true;
  682. Settings.Update (json, "TestConfigurationManagerUpdateFromJson");
  683. Assert.Equal (KeyCode.Q | KeyCode.CtrlMask, Application.QuitKey.KeyCode);
  684. Assert.Equal (KeyCode.Z | KeyCode.AltMask, ((Key)Settings ["Application.QuitKey"].PropertyValue).KeyCode);
  685. Assert.Equal ("Default", Themes.Theme);
  686. Assert.Equal (new Color (Color.White), Colors.ColorSchemes ["Base"].Normal.Foreground);
  687. Assert.Equal (new Color (Color.Blue), Colors.ColorSchemes ["Base"].Normal.Background);
  688. Dictionary<string, ColorScheme> colorSchemes =
  689. (Dictionary<string, ColorScheme>)Themes.First ().Value ["ColorSchemes"].PropertyValue;
  690. Assert.Equal (new Color (Color.White), colorSchemes ["Base"].Normal.Foreground);
  691. Assert.Equal (new Color (Color.Blue), colorSchemes ["Base"].Normal.Background);
  692. // Now re-apply
  693. Apply ();
  694. Assert.Equal (KeyCode.Z | KeyCode.AltMask, Application.QuitKey.KeyCode);
  695. Assert.Equal ("Default", Themes.Theme);
  696. Assert.Equal (new Color (Color.White), Colors.ColorSchemes ["Base"].Normal.Foreground);
  697. Assert.Equal (new Color (Color.Blue), Colors.ColorSchemes ["Base"].Normal.Background);
  698. }
  699. [Fact]
  700. public void UseWithoutResetAsserts ()
  701. {
  702. Initialize ();
  703. Assert.Throws<InvalidOperationException> (() => _ = Settings);
  704. }
  705. }