ConfigurationMangerTests.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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.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.IsMouseDisabled);
  199. Assert.True (Application.EnableConsoleScrolling);
  200. //act
  201. ConfigurationManager.Reset ();
  202. // assert
  203. Assert.NotEmpty (ConfigurationManager.Themes);
  204. Assert.Equal ("Default", ConfigurationManager.Themes.Theme);
  205. Assert.Equal (Key.Q | Key.CtrlMask, Application.QuitKey);
  206. Assert.Equal (Key.PageDown | Key.CtrlMask, Application.AlternateForwardKey);
  207. Assert.Equal (Key.PageUp | Key.CtrlMask, Application.AlternateBackwardKey);
  208. Assert.False (Application.IsMouseDisabled);
  209. Assert.False (Application.EnableConsoleScrolling);
  210. // arrange
  211. ConfigurationManager.Settings ["Application.QuitKey"].PropertyValue = Key.Q;
  212. ConfigurationManager.Settings ["Application.AlternateForwardKey"].PropertyValue = Key.F;
  213. ConfigurationManager.Settings ["Application.AlternateBackwardKey"].PropertyValue = Key.B;
  214. ConfigurationManager.Settings ["Application.IsMouseDisabled"].PropertyValue = true;
  215. ConfigurationManager.Settings ["Application.EnableConsoleScrolling"].PropertyValue = true;
  216. ConfigurationManager.Settings.Apply ();
  217. ConfigurationManager.Locations = ConfigLocations.DefaultOnly;
  218. // act
  219. ConfigurationManager.Reset ();
  220. ConfigurationManager.Load ();
  221. // assert
  222. Assert.NotEmpty (ConfigurationManager.Themes);
  223. Assert.Equal ("Default", ConfigurationManager.Themes.Theme);
  224. Assert.Equal (Key.Q | Key.CtrlMask, Application.QuitKey);
  225. Assert.Equal (Key.PageDown | Key.CtrlMask, Application.AlternateForwardKey);
  226. Assert.Equal (Key.PageUp | Key.CtrlMask, Application.AlternateBackwardKey);
  227. Assert.False (Application.IsMouseDisabled);
  228. Assert.False (Application.EnableConsoleScrolling);
  229. }
  230. [Fact]
  231. public void TestConfigProperties ()
  232. {
  233. ConfigurationManager.Locations = ConfigLocations.All;
  234. ConfigurationManager.Reset ();
  235. Assert.NotEmpty (ConfigurationManager.Settings);
  236. // test that all ConfigProperites have our attribute
  237. Assert.All (ConfigurationManager.Settings, item => Assert.NotEmpty (item.Value.PropertyInfo.CustomAttributes.Where (a => a.AttributeType == typeof (SerializableConfigurationProperty))));
  238. Assert.Empty (ConfigurationManager.Settings.Where (cp => cp.Value.PropertyInfo.GetCustomAttribute (typeof (SerializableConfigurationProperty)) == null));
  239. // Application is a static class
  240. PropertyInfo pi = typeof (Application).GetProperty ("QuitKey");
  241. Assert.Equal (pi, ConfigurationManager.Settings ["Application.QuitKey"].PropertyInfo);
  242. // FrameView is not a static class and DefaultBorderStyle is Scope.Scheme
  243. pi = typeof (FrameView).GetProperty ("DefaultBorderStyle");
  244. Assert.False (ConfigurationManager.Settings.ContainsKey ("FrameView.DefaultBorderStyle"));
  245. Assert.True (ConfigurationManager.Themes ["Default"].ContainsKey ("FrameView.DefaultBorderStyle"));
  246. }
  247. [Fact]
  248. public void TestConfigPropertyOmitClassName ()
  249. {
  250. // Color.ColorShemes is serialzied as "ColorSchemes", not "Colors.ColorSchemes"
  251. PropertyInfo pi = typeof (Colors).GetProperty ("ColorSchemes");
  252. var scp = ((SerializableConfigurationProperty)pi.GetCustomAttribute (typeof (SerializableConfigurationProperty)));
  253. Assert.True (scp.Scope == typeof (ThemeScope));
  254. Assert.True (scp.OmitClassName);
  255. ConfigurationManager.Reset ();
  256. Assert.Equal (pi, ConfigurationManager.Themes ["Default"] ["ColorSchemes"].PropertyInfo);
  257. }
  258. [Fact, AutoInitShutdown]
  259. public void TestConfigurationManagerToJson ()
  260. {
  261. ConfigurationManager.Reset ();
  262. ConfigurationManager.GetHardCodedDefaults ();
  263. var stream = ConfigurationManager.ToStream ();
  264. ConfigurationManager.Settings.Update (stream, "TestConfigurationManagerToJson");
  265. }
  266. [Fact, AutoInitShutdown (configLocation: ConfigLocations.None)]
  267. public void TestConfigurationManagerInitDriver_NoLocations ()
  268. {
  269. }
  270. [Fact, AutoInitShutdown (configLocation: ConfigLocations.DefaultOnly)]
  271. public void TestConfigurationManagerInitDriver ()
  272. {
  273. Assert.Equal ("Default", ConfigurationManager.Themes.Theme);
  274. Assert.True (ConfigurationManager.Themes.ContainsKey ("Default"));
  275. Assert.Equal (Key.Q | Key.CtrlMask, Application.QuitKey);
  276. Assert.Equal (Color.White, Colors.ColorSchemes ["Base"].Normal.Foreground);
  277. Assert.Equal (Color.Blue, Colors.ColorSchemes ["Base"].Normal.Background);
  278. // Change Base
  279. var json = ConfigurationManager.ToStream ();
  280. ConfigurationManager.Settings.Update (json, "TestConfigurationManagerInitDriver");
  281. var colorSchemes = ((Dictionary<string, ColorScheme>)ConfigurationManager.Themes [ConfigurationManager.Themes.Theme] ["ColorSchemes"].PropertyValue);
  282. Assert.Equal (Colors.Base, colorSchemes ["Base"]);
  283. Assert.Equal (Colors.TopLevel, colorSchemes ["TopLevel"]);
  284. Assert.Equal (Colors.Error, colorSchemes ["Error"]);
  285. Assert.Equal (Colors.Dialog, colorSchemes ["Dialog"]);
  286. Assert.Equal (Colors.Menu, colorSchemes ["Menu"]);
  287. Colors.Base = colorSchemes ["Base"];
  288. Colors.TopLevel = colorSchemes ["TopLevel"];
  289. Colors.Error = colorSchemes ["Error"];
  290. Colors.Dialog = colorSchemes ["Dialog"];
  291. Colors.Menu = colorSchemes ["Menu"];
  292. Assert.Equal (colorSchemes ["Base"], Colors.Base);
  293. Assert.Equal (colorSchemes ["TopLevel"], Colors.TopLevel);
  294. Assert.Equal (colorSchemes ["Error"], Colors.Error);
  295. Assert.Equal (colorSchemes ["Dialog"], Colors.Dialog);
  296. Assert.Equal (colorSchemes ["Menu"], Colors.Menu);
  297. }
  298. [Fact]
  299. public void TestConfigurationManagerUpdateFromJson ()
  300. {
  301. // Arrange
  302. string json = @"
  303. {
  304. ""$schema"": ""https://gui-cs.github.io/Terminal.Gui/schemas/tui-config-schema.json"",
  305. ""Application.QuitKey"": {
  306. ""Key"": ""Z"",
  307. ""Modifiers"": [
  308. ""Alt""
  309. ]
  310. },
  311. ""Theme"": ""Default"",
  312. ""Themes"": [
  313. {
  314. ""Default"": {
  315. ""ColorSchemes"": [
  316. {
  317. ""TopLevel"": {
  318. ""Normal"": {
  319. ""Foreground"": ""BrightGreen"",
  320. ""Background"": ""Black""
  321. },
  322. ""Focus"": {
  323. ""Foreground"": ""White"",
  324. ""Background"": ""Cyan""
  325. },
  326. ""HotNormal"": {
  327. ""Foreground"": ""Brown"",
  328. ""Background"": ""Black""
  329. },
  330. ""HotFocus"": {
  331. ""Foreground"": ""Blue"",
  332. ""Background"": ""Cyan""
  333. },
  334. ""Disabled"": {
  335. ""Foreground"": ""DarkGray"",
  336. ""Background"": ""Black""
  337. }
  338. }
  339. },
  340. {
  341. ""Base"": {
  342. ""Normal"": {
  343. ""Foreground"": ""White"",
  344. ""Background"": ""Blue""
  345. },
  346. ""Focus"": {
  347. ""Foreground"": ""Black"",
  348. ""Background"": ""Gray""
  349. },
  350. ""HotNormal"": {
  351. ""Foreground"": ""BrightCyan"",
  352. ""Background"": ""Blue""
  353. },
  354. ""HotFocus"": {
  355. ""Foreground"": ""BrightBlue"",
  356. ""Background"": ""Gray""
  357. },
  358. ""Disabled"": {
  359. ""Foreground"": ""DarkGray"",
  360. ""Background"": ""Blue""
  361. }
  362. }
  363. },
  364. {
  365. ""Dialog"": {
  366. ""Normal"": {
  367. ""Foreground"": ""Black"",
  368. ""Background"": ""Gray""
  369. },
  370. ""Focus"": {
  371. ""Foreground"": ""White"",
  372. ""Background"": ""DarkGray""
  373. },
  374. ""HotNormal"": {
  375. ""Foreground"": ""Blue"",
  376. ""Background"": ""Gray""
  377. },
  378. ""HotFocus"": {
  379. ""Foreground"": ""BrightYellow"",
  380. ""Background"": ""DarkGray""
  381. },
  382. ""Disabled"": {
  383. ""Foreground"": ""Gray"",
  384. ""Background"": ""DarkGray""
  385. }
  386. }
  387. },
  388. {
  389. ""Menu"": {
  390. ""Normal"": {
  391. ""Foreground"": ""White"",
  392. ""Background"": ""DarkGray""
  393. },
  394. ""Focus"": {
  395. ""Foreground"": ""White"",
  396. ""Background"": ""Black""
  397. },
  398. ""HotNormal"": {
  399. ""Foreground"": ""BrightYellow"",
  400. ""Background"": ""DarkGray""
  401. },
  402. ""HotFocus"": {
  403. ""Foreground"": ""BrightYellow"",
  404. ""Background"": ""Black""
  405. },
  406. ""Disabled"": {
  407. ""Foreground"": ""Gray"",
  408. ""Background"": ""DarkGray""
  409. }
  410. }
  411. },
  412. {
  413. ""Error"": {
  414. ""Normal"": {
  415. ""Foreground"": ""Red"",
  416. ""Background"": ""White""
  417. },
  418. ""Focus"": {
  419. ""Foreground"": ""Black"",
  420. ""Background"": ""BrightRed""
  421. },
  422. ""HotNormal"": {
  423. ""Foreground"": ""Black"",
  424. ""Background"": ""White""
  425. },
  426. ""HotFocus"": {
  427. ""Foreground"": ""White"",
  428. ""Background"": ""BrightRed""
  429. },
  430. ""Disabled"": {
  431. ""Foreground"": ""DarkGray"",
  432. ""Background"": ""White""
  433. }
  434. }
  435. }
  436. ],
  437. ""Dialog.DefaultButtonAlignment"": ""Center""
  438. }
  439. }
  440. ]
  441. }
  442. ";
  443. ConfigurationManager.Reset ();
  444. ConfigurationManager.ThrowOnJsonErrors = true;
  445. ConfigurationManager.Settings.Update (json, "TestConfigurationManagerUpdateFromJson");
  446. Assert.Equal (Key.Q | Key.CtrlMask, Application.QuitKey);
  447. Assert.Equal (Key.Z | Key.AltMask, ConfigurationManager.Settings ["Application.QuitKey"].PropertyValue);
  448. Assert.Equal ("Default", ConfigurationManager.Themes.Theme);
  449. Assert.Equal (Color.White, Colors.ColorSchemes ["Base"].Normal.Foreground);
  450. Assert.Equal (Color.Blue, Colors.ColorSchemes ["Base"].Normal.Background);
  451. var colorSchemes = (Dictionary<string, ColorScheme>)Themes.First().Value ["ColorSchemes"].PropertyValue;
  452. Assert.Equal (Color.White, colorSchemes ["Base"].Normal.Foreground);
  453. Assert.Equal (Color.Blue, colorSchemes ["Base"].Normal.Background);
  454. // Now re-apply
  455. ConfigurationManager.Apply ();
  456. Assert.Equal (Key.Z | Key.AltMask, Application.QuitKey);
  457. Assert.Equal ("Default", ConfigurationManager.Themes.Theme);
  458. Assert.Equal (Color.White, Colors.ColorSchemes ["Base"].Normal.Foreground);
  459. Assert.Equal (Color.Blue, Colors.ColorSchemes ["Base"].Normal.Background);
  460. }
  461. [Fact, AutoInitShutdown]
  462. public void TestConfigurationManagerInvalidJsonThrows ()
  463. {
  464. ConfigurationManager.ThrowOnJsonErrors = true;
  465. // "yellow" is not a color
  466. string json = @"
  467. {
  468. ""Themes"" : [
  469. {
  470. ""Default"" : {
  471. ""ColorSchemes"": [
  472. {
  473. ""UserDefined"": {
  474. ""hotNormal"": {
  475. ""foreground"": ""yellow"",
  476. ""background"": ""1234""
  477. }
  478. }
  479. }
  480. ]
  481. }
  482. }
  483. ]
  484. }";
  485. JsonException jsonException = Assert.Throws<JsonException> (() => ConfigurationManager.Settings.Update (json, "test"));
  486. Assert.Equal ("Invalid Color: 'yellow'", jsonException.Message);
  487. // AbNormal is not a ColorScheme attribute
  488. json = @"
  489. {
  490. ""Themes"" : [
  491. {
  492. ""Default"" : {
  493. ""ColorSchemes"": [
  494. {
  495. ""UserDefined"": {
  496. ""AbNormal"": {
  497. ""foreground"": ""green"",
  498. ""background"": ""1234""
  499. }
  500. }
  501. }
  502. ]
  503. }
  504. }
  505. ]
  506. }";
  507. jsonException = Assert.Throws<JsonException> (() => ConfigurationManager.Settings.Update (json, "test"));
  508. Assert.Equal ("Unrecognized ColorScheme Attribute name: AbNormal.", jsonException.Message);
  509. // Modify hotNormal background only
  510. json = @"
  511. {
  512. ""Themes"" : [
  513. {
  514. ""Default"" : {
  515. ""ColorSchemes"": [
  516. {
  517. ""UserDefined"": {
  518. ""hotNormal"": {
  519. ""background"": ""cyan""
  520. }
  521. }
  522. }
  523. ]
  524. }
  525. }
  526. ]
  527. }";
  528. jsonException = Assert.Throws<JsonException> (() => ConfigurationManager.Settings.Update (json, "test"));
  529. Assert.Equal ("Both Foreground and Background colors must be provided.", jsonException.Message);
  530. // Unknown proeprty
  531. json = @"
  532. {
  533. ""Unknown"" : ""Not known""
  534. }";
  535. jsonException = Assert.Throws<JsonException> (() => ConfigurationManager.Settings.Update (json, "test"));
  536. Assert.StartsWith ("Unknown property", jsonException.Message);
  537. Assert.Equal (0, ConfigurationManager.jsonErrors.Length);
  538. ConfigurationManager.ThrowOnJsonErrors = false;
  539. }
  540. [Fact]
  541. public void TestConfigurationManagerInvalidJsonLogs ()
  542. {
  543. Application.Init (new FakeDriver ());
  544. ConfigurationManager.ThrowOnJsonErrors = false;
  545. // "yellow" is not a color
  546. string json = @"
  547. {
  548. ""Themes"" : [
  549. {
  550. ""Default"" : {
  551. ""ColorSchemes"": [
  552. {
  553. ""UserDefined"": {
  554. ""hotNormal"": {
  555. ""foreground"": ""yellow"",
  556. ""background"": ""1234""
  557. }
  558. }
  559. }
  560. ]
  561. }
  562. }
  563. }
  564. }";
  565. ConfigurationManager.Settings.Update (json, "test");
  566. // AbNormal is not a ColorScheme attribute
  567. json = @"
  568. {
  569. ""Themes"" : [
  570. {
  571. ""Default"" : {
  572. ""ColorSchemes"": [
  573. {
  574. ""UserDefined"": {
  575. ""AbNormal"": {
  576. ""foreground"": ""green"",
  577. ""background"": ""1234""
  578. }
  579. }
  580. }
  581. ]
  582. }
  583. }
  584. }
  585. }";
  586. ConfigurationManager.Settings.Update (json, "test");
  587. // Modify hotNormal background only
  588. json = @"
  589. {
  590. ""Themes"" : [
  591. {
  592. ""Default"" : {
  593. ""ColorSchemes"": [
  594. {
  595. ""UserDefined"": {
  596. ""hotNormal"": {
  597. ""background"": ""cyan""
  598. }
  599. }
  600. }
  601. ]
  602. }
  603. }
  604. }
  605. }";
  606. ConfigurationManager.Settings.Update (json, "test");
  607. ConfigurationManager.Settings.Update ("{}}", "test");
  608. Assert.NotEqual (0, ConfigurationManager.jsonErrors.Length);
  609. Application.Shutdown ();
  610. ConfigurationManager.ThrowOnJsonErrors = false;
  611. }
  612. [Fact, AutoInitShutdown]
  613. public void LoadConfigurationFromAllSources_ShouldLoadSettingsFromAllSources ()
  614. {
  615. //var _configFilename = "config.json";
  616. //// Arrange
  617. //// Create a mock of the configuration files in all sources
  618. //// Home directory
  619. //string homeDir = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.UserProfile), ".tui");
  620. //if (!Directory.Exists (homeDir)) {
  621. // Directory.CreateDirectory (homeDir);
  622. //}
  623. //string globalConfigFile = Path.Combine (homeDir, _configFilename);
  624. //string appSpecificConfigFile = Path.Combine (homeDir, "appname.config.json");
  625. //File.WriteAllText (globalConfigFile, "{\"Settings\": {\"TestSetting\":\"Global\"}}");
  626. //File.WriteAllText (appSpecificConfigFile, "{\"Settings\": {\"TestSetting\":\"AppSpecific\"}}");
  627. //// App directory
  628. //string appDir = Directory.GetCurrentDirectory ();
  629. //string appDirGlobalConfigFile = Path.Combine (appDir, _configFilename);
  630. //string appDirAppSpecificConfigFile = Path.Combine (appDir, "appname.config.json");
  631. //File.WriteAllText (appDirGlobalConfigFile, "{\"Settings\": {\"TestSetting\":\"GlobalAppDir\"}}");
  632. //File.WriteAllText (appDirAppSpecificConfigFile, "{\"Settings\": {\"TestSetting\":\"AppSpecificAppDir\"}}");
  633. //// App resources
  634. //// ...
  635. //// Act
  636. //ConfigurationManager.Locations = ConfigurationManager.ConfigLocation.All;
  637. //ConfigurationManager.Load ();
  638. //// Assert
  639. //// Check that the settings from the highest precedence source are loaded
  640. //Assert.Equal ("AppSpecific", ConfigurationManager.Config.Settings.TestSetting);
  641. }
  642. [Fact]
  643. public void Load_FiresUpdated ()
  644. {
  645. ConfigurationManager.Reset ();
  646. ConfigurationManager.Settings ["Application.QuitKey"].PropertyValue = Key.Q;
  647. ConfigurationManager.Settings ["Application.AlternateForwardKey"].PropertyValue = Key.F;
  648. ConfigurationManager.Settings ["Application.AlternateBackwardKey"].PropertyValue = Key.B;
  649. ConfigurationManager.Settings ["Application.IsMouseDisabled"].PropertyValue = true;
  650. ConfigurationManager.Settings ["Application.EnableConsoleScrolling"].PropertyValue = true;
  651. ConfigurationManager.Updated += ConfigurationManager_Updated;
  652. bool fired = false;
  653. void ConfigurationManager_Updated (object sender, ConfigurationManagerEventArgs obj)
  654. {
  655. fired = true;
  656. // assert
  657. Assert.Equal (Key.Q | Key.CtrlMask, ConfigurationManager.Settings ["Application.QuitKey"].PropertyValue);
  658. Assert.Equal (Key.PageDown | Key.CtrlMask, ConfigurationManager.Settings ["Application.AlternateForwardKey"].PropertyValue);
  659. Assert.Equal (Key.PageUp | Key.CtrlMask, ConfigurationManager.Settings ["Application.AlternateBackwardKey"].PropertyValue);
  660. Assert.False ((bool)ConfigurationManager.Settings ["Application.IsMouseDisabled"].PropertyValue);
  661. Assert.False ((bool)ConfigurationManager.Settings ["Application.EnableConsoleScrolling"].PropertyValue);
  662. }
  663. ConfigurationManager.Load (true);
  664. // assert
  665. Assert.True (fired);
  666. ConfigurationManager.Updated -= ConfigurationManager_Updated;
  667. }
  668. [Fact]
  669. public void Apply_FiresApplied ()
  670. {
  671. ConfigurationManager.Reset ();
  672. ConfigurationManager.Applied += ConfigurationManager_Applied;
  673. bool fired = false;
  674. void ConfigurationManager_Applied (object sender, ConfigurationManagerEventArgs obj)
  675. {
  676. fired = true;
  677. // assert
  678. Assert.Equal (Key.Q, Application.QuitKey);
  679. Assert.Equal (Key.F, Application.AlternateForwardKey);
  680. Assert.Equal (Key.B, Application.AlternateBackwardKey);
  681. Assert.True (Application.IsMouseDisabled);
  682. Assert.True (Application.EnableConsoleScrolling);
  683. }
  684. // act
  685. ConfigurationManager.Settings ["Application.QuitKey"].PropertyValue = Key.Q;
  686. ConfigurationManager.Settings ["Application.AlternateForwardKey"].PropertyValue = Key.F;
  687. ConfigurationManager.Settings ["Application.AlternateBackwardKey"].PropertyValue = Key.B;
  688. ConfigurationManager.Settings ["Application.IsMouseDisabled"].PropertyValue = true;
  689. ConfigurationManager.Settings ["Application.EnableConsoleScrolling"].PropertyValue = true;
  690. ConfigurationManager.Apply ();
  691. // assert
  692. Assert.True (fired);
  693. ConfigurationManager.Applied -= ConfigurationManager_Applied;
  694. }
  695. }
  696. }