ConfigurationMangerTests.cs 25 KB

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