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