ConfigurationMangerTests.cs 27 KB

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