ConfigurationMangerTests.cs 27 KB

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