ConfigurationMangerTests.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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 = new Key (KeyCode.Q);
  186. ConfigurationManager.Settings ["Application.AlternateForwardKey"].PropertyValue = new Key (KeyCode.F);
  187. ConfigurationManager.Settings ["Application.AlternateBackwardKey"].PropertyValue = new Key (KeyCode.B);
  188. ConfigurationManager.Settings ["Application.IsMouseDisabled"].PropertyValue = true;
  189. ConfigurationManager.Settings.Apply ();
  190. // assert apply worked
  191. Assert.Equal (KeyCode.Q, Application.QuitKey.KeyCode);
  192. Assert.Equal (KeyCode.F, Application.AlternateForwardKey.KeyCode);
  193. Assert.Equal (KeyCode.B, Application.AlternateBackwardKey.KeyCode);
  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 (KeyCode.Q | KeyCode.CtrlMask, Application.QuitKey.KeyCode);
  201. Assert.Equal (KeyCode.PageDown | KeyCode.CtrlMask, Application.AlternateForwardKey.KeyCode);
  202. Assert.Equal (KeyCode.PageUp | KeyCode.CtrlMask, Application.AlternateBackwardKey.KeyCode);
  203. Assert.False (Application.IsMouseDisabled);
  204. // arrange
  205. ConfigurationManager.Settings ["Application.QuitKey"].PropertyValue = new Key (KeyCode.Q);
  206. ConfigurationManager.Settings ["Application.AlternateForwardKey"].PropertyValue = new Key (KeyCode.F);
  207. ConfigurationManager.Settings ["Application.AlternateBackwardKey"].PropertyValue = new Key (KeyCode.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 (KeyCode.Q | KeyCode.CtrlMask, Application.QuitKey.KeyCode);
  218. Assert.Equal (KeyCode.PageDown | KeyCode.CtrlMask, Application.AlternateForwardKey.KeyCode);
  219. Assert.Equal (KeyCode.PageUp | KeyCode.CtrlMask, Application.AlternateBackwardKey.KeyCode);
  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 (KeyCode.Q | KeyCode.CtrlMask, Application.QuitKey.KeyCode);
  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"": ""Alt-Z""
  299. },
  300. ""Theme"": ""Default"",
  301. ""Themes"": [
  302. {
  303. ""Default"": {
  304. ""ColorSchemes"": [
  305. {
  306. ""TopLevel"": {
  307. ""Normal"": {
  308. ""Foreground"": ""BrightGreen"",
  309. ""Background"": ""Black""
  310. },
  311. ""Focus"": {
  312. ""Foreground"": ""White"",
  313. ""Background"": ""Cyan""
  314. },
  315. ""HotNormal"": {
  316. ""Foreground"": ""Yellow"",
  317. ""Background"": ""Black""
  318. },
  319. ""HotFocus"": {
  320. ""Foreground"": ""Blue"",
  321. ""Background"": ""Cyan""
  322. },
  323. ""Disabled"": {
  324. ""Foreground"": ""DarkGray"",
  325. ""Background"": ""Black""
  326. }
  327. }
  328. },
  329. {
  330. ""Base"": {
  331. ""Normal"": {
  332. ""Foreground"": ""White"",
  333. ""Background"": ""Blue""
  334. },
  335. ""Focus"": {
  336. ""Foreground"": ""Black"",
  337. ""Background"": ""Gray""
  338. },
  339. ""HotNormal"": {
  340. ""Foreground"": ""BrightCyan"",
  341. ""Background"": ""Blue""
  342. },
  343. ""HotFocus"": {
  344. ""Foreground"": ""BrightBlue"",
  345. ""Background"": ""Gray""
  346. },
  347. ""Disabled"": {
  348. ""Foreground"": ""DarkGray"",
  349. ""Background"": ""Blue""
  350. }
  351. }
  352. },
  353. {
  354. ""Dialog"": {
  355. ""Normal"": {
  356. ""Foreground"": ""Black"",
  357. ""Background"": ""Gray""
  358. },
  359. ""Focus"": {
  360. ""Foreground"": ""White"",
  361. ""Background"": ""DarkGray""
  362. },
  363. ""HotNormal"": {
  364. ""Foreground"": ""Blue"",
  365. ""Background"": ""Gray""
  366. },
  367. ""HotFocus"": {
  368. ""Foreground"": ""BrightYellow"",
  369. ""Background"": ""DarkGray""
  370. },
  371. ""Disabled"": {
  372. ""Foreground"": ""Gray"",
  373. ""Background"": ""DarkGray""
  374. }
  375. }
  376. },
  377. {
  378. ""Menu"": {
  379. ""Normal"": {
  380. ""Foreground"": ""White"",
  381. ""Background"": ""DarkGray""
  382. },
  383. ""Focus"": {
  384. ""Foreground"": ""White"",
  385. ""Background"": ""Black""
  386. },
  387. ""HotNormal"": {
  388. ""Foreground"": ""BrightYellow"",
  389. ""Background"": ""DarkGray""
  390. },
  391. ""HotFocus"": {
  392. ""Foreground"": ""BrightYellow"",
  393. ""Background"": ""Black""
  394. },
  395. ""Disabled"": {
  396. ""Foreground"": ""Gray"",
  397. ""Background"": ""DarkGray""
  398. }
  399. }
  400. },
  401. {
  402. ""Error"": {
  403. ""Normal"": {
  404. ""Foreground"": ""Red"",
  405. ""Background"": ""White""
  406. },
  407. ""Focus"": {
  408. ""Foreground"": ""Black"",
  409. ""Background"": ""BrightRed""
  410. },
  411. ""HotNormal"": {
  412. ""Foreground"": ""Black"",
  413. ""Background"": ""White""
  414. },
  415. ""HotFocus"": {
  416. ""Foreground"": ""White"",
  417. ""Background"": ""BrightRed""
  418. },
  419. ""Disabled"": {
  420. ""Foreground"": ""DarkGray"",
  421. ""Background"": ""White""
  422. }
  423. }
  424. }
  425. ],
  426. ""Dialog.DefaultButtonAlignment"": ""Center""
  427. }
  428. }
  429. ]
  430. }
  431. ";
  432. ConfigurationManager.Reset ();
  433. ConfigurationManager.ThrowOnJsonErrors = true;
  434. ConfigurationManager.Settings.Update (json, "TestConfigurationManagerUpdateFromJson");
  435. Assert.Equal (KeyCode.Q | KeyCode.CtrlMask, Application.QuitKey.KeyCode);
  436. Assert.Equal (KeyCode.Z | KeyCode.AltMask, ((Key)ConfigurationManager.Settings ["Application.QuitKey"].PropertyValue).KeyCode);
  437. Assert.Equal ("Default", ConfigurationManager.Themes.Theme);
  438. Assert.Equal (new Color (Color.White), Colors.ColorSchemes ["Base"].Normal.Foreground);
  439. Assert.Equal (new Color (Color.Blue), Colors.ColorSchemes ["Base"].Normal.Background);
  440. var colorSchemes = (Dictionary<string, ColorScheme>)Themes.First ().Value ["ColorSchemes"].PropertyValue;
  441. Assert.Equal (new Color (Color.White), colorSchemes ["Base"].Normal.Foreground);
  442. Assert.Equal (new Color (Color.Blue), colorSchemes ["Base"].Normal.Background);
  443. // Now re-apply
  444. ConfigurationManager.Apply ();
  445. Assert.Equal (KeyCode.Z | KeyCode.AltMask, Application.QuitKey.KeyCode);
  446. Assert.Equal ("Default", ConfigurationManager.Themes.Theme);
  447. Assert.Equal (new Color (Color.White), Colors.ColorSchemes ["Base"].Normal.Foreground);
  448. Assert.Equal (new Color (Color.Blue), Colors.ColorSchemes ["Base"].Normal.Background);
  449. }
  450. [Fact, AutoInitShutdown]
  451. public void TestConfigurationManagerInvalidJsonThrows ()
  452. {
  453. ConfigurationManager.ThrowOnJsonErrors = true;
  454. // "yellow" is not a color
  455. string json = @"
  456. {
  457. ""Themes"" : [
  458. {
  459. ""Default"" : {
  460. ""ColorSchemes"": [
  461. {
  462. ""UserDefined"": {
  463. ""hotNormal"": {
  464. ""foreground"": ""brown"",
  465. ""background"": ""1234""
  466. }
  467. }
  468. }
  469. ]
  470. }
  471. }
  472. ]
  473. }";
  474. JsonException jsonException = Assert.Throws<JsonException> (() => ConfigurationManager.Settings.Update (json, "test"));
  475. Assert.Equal ("Unexpected color name: brown.", jsonException.Message);
  476. // AbNormal is not a ColorScheme attribute
  477. json = @"
  478. {
  479. ""Themes"" : [
  480. {
  481. ""Default"" : {
  482. ""ColorSchemes"": [
  483. {
  484. ""UserDefined"": {
  485. ""AbNormal"": {
  486. ""foreground"": ""green"",
  487. ""background"": ""black""
  488. }
  489. }
  490. }
  491. ]
  492. }
  493. }
  494. ]
  495. }";
  496. jsonException = Assert.Throws<JsonException> (() => ConfigurationManager.Settings.Update (json, "test"));
  497. Assert.Equal ("Unrecognized ColorScheme Attribute name: AbNormal.", jsonException.Message);
  498. // Modify hotNormal background only
  499. json = @"
  500. {
  501. ""Themes"" : [
  502. {
  503. ""Default"" : {
  504. ""ColorSchemes"": [
  505. {
  506. ""UserDefined"": {
  507. ""hotNormal"": {
  508. ""background"": ""cyan""
  509. }
  510. }
  511. }
  512. ]
  513. }
  514. }
  515. ]
  516. }";
  517. jsonException = Assert.Throws<JsonException> (() => ConfigurationManager.Settings.Update (json, "test"));
  518. Assert.Equal ("Both Foreground and Background colors must be provided.", jsonException.Message);
  519. // Unknown proeprty
  520. json = @"
  521. {
  522. ""Unknown"" : ""Not known""
  523. }";
  524. jsonException = Assert.Throws<JsonException> (() => ConfigurationManager.Settings.Update (json, "test"));
  525. Assert.StartsWith ("Unknown property", jsonException.Message);
  526. Assert.Equal (0, ConfigurationManager.jsonErrors.Length);
  527. ConfigurationManager.ThrowOnJsonErrors = false;
  528. }
  529. [Fact]
  530. public void TestConfigurationManagerInvalidJsonLogs ()
  531. {
  532. Application.Init (new FakeDriver ());
  533. ConfigurationManager.ThrowOnJsonErrors = false;
  534. // "brown" is not a color
  535. string json = @"
  536. {
  537. ""Themes"" : [
  538. {
  539. ""Default"" : {
  540. ""ColorSchemes"": [
  541. {
  542. ""UserDefined"": {
  543. ""hotNormal"": {
  544. ""foreground"": ""brown"",
  545. ""background"": ""1234""
  546. }
  547. }
  548. }
  549. ]
  550. }
  551. }
  552. }
  553. }";
  554. ConfigurationManager.Settings.Update (json, "test");
  555. // AbNormal is not a ColorScheme attribute
  556. json = @"
  557. {
  558. ""Themes"" : [
  559. {
  560. ""Default"" : {
  561. ""ColorSchemes"": [
  562. {
  563. ""UserDefined"": {
  564. ""AbNormal"": {
  565. ""foreground"": ""green"",
  566. ""background"": ""black""
  567. }
  568. }
  569. }
  570. ]
  571. }
  572. }
  573. }
  574. }";
  575. ConfigurationManager.Settings.Update (json, "test");
  576. // Modify hotNormal background only
  577. json = @"
  578. {
  579. ""Themes"" : [
  580. {
  581. ""Default"" : {
  582. ""ColorSchemes"": [
  583. {
  584. ""UserDefined"": {
  585. ""hotNormal"": {
  586. ""background"": ""cyan""
  587. }
  588. }
  589. }
  590. ]
  591. }
  592. }
  593. }
  594. }";
  595. ConfigurationManager.Settings.Update (json, "test");
  596. ConfigurationManager.Settings.Update ("{}}", "test");
  597. Assert.NotEqual (0, ConfigurationManager.jsonErrors.Length);
  598. Application.Shutdown ();
  599. ConfigurationManager.ThrowOnJsonErrors = false;
  600. }
  601. [Fact, AutoInitShutdown]
  602. public void LoadConfigurationFromAllSources_ShouldLoadSettingsFromAllSources ()
  603. {
  604. //var _configFilename = "config.json";
  605. //// Arrange
  606. //// Create a mock of the configuration files in all sources
  607. //// Home directory
  608. //string homeDir = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.UserProfile), ".tui");
  609. //if (!Directory.Exists (homeDir)) {
  610. // Directory.CreateDirectory (homeDir);
  611. //}
  612. //string globalConfigFile = Path.Combine (homeDir, _configFilename);
  613. //string appSpecificConfigFile = Path.Combine (homeDir, "appname.config.json");
  614. //File.WriteAllText (globalConfigFile, "{\"Settings\": {\"TestSetting\":\"Global\"}}");
  615. //File.WriteAllText (appSpecificConfigFile, "{\"Settings\": {\"TestSetting\":\"AppSpecific\"}}");
  616. //// App directory
  617. //string appDir = Directory.GetCurrentDirectory ();
  618. //string appDirGlobalConfigFile = Path.Combine (appDir, _configFilename);
  619. //string appDirAppSpecificConfigFile = Path.Combine (appDir, "appname.config.json");
  620. //File.WriteAllText (appDirGlobalConfigFile, "{\"Settings\": {\"TestSetting\":\"GlobalAppDir\"}}");
  621. //File.WriteAllText (appDirAppSpecificConfigFile, "{\"Settings\": {\"TestSetting\":\"AppSpecificAppDir\"}}");
  622. //// App resources
  623. //// ...
  624. //// Act
  625. //ConfigurationManager.Locations = ConfigurationManager.ConfigLocation.All;
  626. //ConfigurationManager.Load ();
  627. //// Assert
  628. //// Check that the settings from the highest precedence source are loaded
  629. //Assert.Equal ("AppSpecific", ConfigurationManager.Config.Settings.TestSetting);
  630. }
  631. [Fact]
  632. public void Load_FiresUpdated ()
  633. {
  634. ConfigurationManager.Reset ();
  635. ConfigurationManager.Settings ["Application.QuitKey"].PropertyValue = new Key (KeyCode.Q);
  636. ConfigurationManager.Settings ["Application.AlternateForwardKey"].PropertyValue = new Key (KeyCode.F);
  637. ConfigurationManager.Settings ["Application.AlternateBackwardKey"].PropertyValue = new Key (KeyCode.B);
  638. ConfigurationManager.Settings ["Application.IsMouseDisabled"].PropertyValue = true;
  639. ConfigurationManager.Updated += ConfigurationManager_Updated;
  640. bool fired = false;
  641. void ConfigurationManager_Updated (object sender, ConfigurationManagerEventArgs obj)
  642. {
  643. fired = true;
  644. // assert
  645. Assert.Equal (KeyCode.Q | KeyCode.CtrlMask, ((Key)ConfigurationManager.Settings ["Application.QuitKey"].PropertyValue).KeyCode);
  646. Assert.Equal (KeyCode.PageDown | KeyCode.CtrlMask, ((Key)ConfigurationManager.Settings ["Application.AlternateForwardKey"].PropertyValue).KeyCode);
  647. Assert.Equal (KeyCode.PageUp | KeyCode.CtrlMask, ((Key)ConfigurationManager.Settings ["Application.AlternateBackwardKey"].PropertyValue).KeyCode);
  648. Assert.False ((bool)ConfigurationManager.Settings ["Application.IsMouseDisabled"].PropertyValue);
  649. }
  650. ConfigurationManager.Load (true);
  651. // assert
  652. Assert.True (fired);
  653. ConfigurationManager.Updated -= ConfigurationManager_Updated;
  654. }
  655. [Fact]
  656. public void Apply_FiresApplied ()
  657. {
  658. ConfigurationManager.Reset ();
  659. ConfigurationManager.Applied += ConfigurationManager_Applied;
  660. bool fired = false;
  661. void ConfigurationManager_Applied (object sender, ConfigurationManagerEventArgs obj)
  662. {
  663. fired = true;
  664. // assert
  665. Assert.Equal (KeyCode.Q, Application.QuitKey.KeyCode);
  666. Assert.Equal (KeyCode.F, Application.AlternateForwardKey.KeyCode);
  667. Assert.Equal (KeyCode.B, Application.AlternateBackwardKey.KeyCode);
  668. Assert.True (Application.IsMouseDisabled);
  669. }
  670. // act
  671. ConfigurationManager.Settings ["Application.QuitKey"].PropertyValue = new Key (KeyCode.Q);
  672. ConfigurationManager.Settings ["Application.AlternateForwardKey"].PropertyValue = new Key (KeyCode.F);
  673. ConfigurationManager.Settings ["Application.AlternateBackwardKey"].PropertyValue = new Key (KeyCode.B);
  674. ConfigurationManager.Settings ["Application.IsMouseDisabled"].PropertyValue = true;
  675. ConfigurationManager.Apply ();
  676. // assert
  677. Assert.True (fired);
  678. ConfigurationManager.Applied -= ConfigurationManager_Applied;
  679. }
  680. }
  681. }