ConfigurationMangerTests.cs 25 KB

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