2
0

SchemeManagerTests.cs 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  1. #nullable enable
  2. using System.Collections.Concurrent;
  3. using System.Collections.Frozen;
  4. using System.Collections.Immutable;
  5. using System.Text.Json;
  6. using static Terminal.Gui.Configuration.ConfigurationManager;
  7. namespace UnitTests.ConfigurationTests;
  8. public class SchemeManagerTests
  9. {
  10. [Fact]
  11. public void GetSchemes_Not_Enabled_Gets_Schemes ()
  12. {
  13. Disable (true);
  14. Dictionary<string, Scheme?>? schemes = SchemeManager.GetSchemesForCurrentTheme ();
  15. Assert.NotNull (schemes);
  16. Assert.NotNull (schemes ["Base"]);
  17. Assert.True (schemes!.ContainsKey ("Base"));
  18. Assert.True (schemes.ContainsKey ("base"));
  19. Assert.Equal (SchemeManager.GetSchemes (), schemes);
  20. }
  21. [Fact]
  22. public void GetSchemes_Enabled_Gets_Current ()
  23. {
  24. try
  25. {
  26. Enable (ConfigLocations.HardCoded);
  27. Dictionary<string, Scheme?>? schemes = SchemeManager.GetSchemesForCurrentTheme ();
  28. Assert.NotNull (schemes);
  29. Assert.NotNull (schemes ["Base"]);
  30. Assert.True (schemes!.ContainsKey ("Base"));
  31. Assert.True (schemes.ContainsKey ("base"));
  32. Assert.Equal (SchemeManager.GetSchemes (), schemes);
  33. }
  34. finally
  35. {
  36. Disable (true);
  37. }
  38. }
  39. [Fact]
  40. public void GetSchemes_Get_Schemes_After_Load ()
  41. {
  42. try
  43. {
  44. Enable (ConfigLocations.HardCoded);
  45. Load (ConfigLocations.All);
  46. Apply ();
  47. Assert.Equal (SchemeManager.GetSchemes (), SchemeManager.GetSchemesForCurrentTheme ());
  48. }
  49. finally
  50. {
  51. Disable (true);
  52. }
  53. }
  54. [Fact]
  55. public void GetHardCodedSchemes_Gets_HardCoded_Theme_Schemes ()
  56. {
  57. ImmutableSortedDictionary<string, Scheme?>? hardCoded = SchemeManager.GetHardCodedSchemes ();
  58. Assert.Equal (Scheme.GetHardCodedSchemes (), actual: hardCoded!);
  59. }
  60. [Fact]
  61. public void GetHardCodedSchemes_Have_Expected_Normal_Attributes ()
  62. {
  63. var schemes = SchemeManager.GetHardCodedSchemes ();
  64. Assert.NotNull (schemes);
  65. // Base
  66. var baseScheme = schemes! ["Base"];
  67. Assert.NotNull (baseScheme);
  68. Assert.Equal (new Attribute (StandardColor.LightBlue, StandardColor.RaisinBlack), baseScheme!.Normal);
  69. // Dialog
  70. var dialogScheme = schemes ["Dialog"];
  71. Assert.NotNull (dialogScheme);
  72. Assert.Equal (new Attribute (StandardColor.LightSkyBlue, StandardColor.OuterSpace), dialogScheme!.Normal);
  73. // Error
  74. var errorScheme = schemes ["Error"];
  75. Assert.NotNull (errorScheme);
  76. Assert.Equal (new Attribute (StandardColor.IndianRed, StandardColor.RaisinBlack), errorScheme!.Normal);
  77. // Menu (Bold style)
  78. var menuScheme = schemes ["Menu"];
  79. Assert.NotNull (menuScheme);
  80. Assert.Equal (new Attribute (StandardColor.Charcoal, StandardColor.LightBlue, TextStyle.Bold), menuScheme!.Normal);
  81. // Toplevel
  82. var toplevelScheme = schemes ["Toplevel"];
  83. Assert.NotNull (toplevelScheme);
  84. Assert.Equal (new Attribute (StandardColor.CadetBlue, StandardColor.Charcoal).ToString (), toplevelScheme!.Normal.ToString ());
  85. }
  86. [Fact]
  87. public void GetHardCodedSchemes_Have_Expected_Normal_Attributes_LoadHardCodedDefaults ()
  88. {
  89. LoadHardCodedDefaults ();
  90. var schemes = SchemeManager.GetHardCodedSchemes ();
  91. Assert.NotNull (schemes);
  92. // Base
  93. var baseScheme = schemes! ["Base"];
  94. Assert.NotNull (baseScheme);
  95. Assert.Equal (new Attribute (StandardColor.LightBlue, StandardColor.RaisinBlack), baseScheme!.Normal);
  96. // Dialog
  97. var dialogScheme = schemes ["Dialog"];
  98. Assert.NotNull (dialogScheme);
  99. Assert.Equal (new Attribute (StandardColor.LightSkyBlue, StandardColor.OuterSpace), dialogScheme!.Normal);
  100. // Error
  101. var errorScheme = schemes ["Error"];
  102. Assert.NotNull (errorScheme);
  103. Assert.Equal (new Attribute (StandardColor.IndianRed, StandardColor.RaisinBlack), errorScheme!.Normal);
  104. // Menu (Bold style)
  105. var menuScheme = schemes ["Menu"];
  106. Assert.NotNull (menuScheme);
  107. Assert.Equal (new Attribute (StandardColor.Charcoal, StandardColor.LightBlue, TextStyle.Bold), menuScheme!.Normal);
  108. // Toplevel
  109. var toplevelScheme = schemes ["Toplevel"];
  110. Assert.NotNull (toplevelScheme);
  111. Assert.Equal (new Attribute (StandardColor.CadetBlue, StandardColor.Charcoal).ToString (), toplevelScheme!.Normal.ToString ());
  112. }
  113. [Fact]
  114. public void Not_Case_Sensitive_Disabled ()
  115. {
  116. Assert.False (IsEnabled);
  117. Dictionary<string, Scheme?>? current = SchemeManager.GetSchemesForCurrentTheme ();
  118. Assert.NotNull (current);
  119. Assert.True (current!.ContainsKey ("Base"));
  120. Assert.True (current.ContainsKey ("base"));
  121. }
  122. [Fact]
  123. public void Not_Case_Sensitive_Enabled ()
  124. {
  125. Assert.False (IsEnabled);
  126. try
  127. {
  128. Enable (ConfigLocations.HardCoded);
  129. Assert.True (SchemeManager.GetSchemesForCurrentTheme ()!.ContainsKey ("Base"));
  130. Assert.True (SchemeManager.GetSchemesForCurrentTheme ()!.ContainsKey ("base"));
  131. ResetToHardCodedDefaults ();
  132. Dictionary<string, Scheme?>? current = SchemeManager.GetSchemesForCurrentTheme ();
  133. Assert.NotNull (current);
  134. Assert.True (current!.ContainsKey ("Base"));
  135. Assert.True (current.ContainsKey ("base"));
  136. }
  137. finally
  138. {
  139. Disable (true);
  140. }
  141. }
  142. [Fact]
  143. public void Load_Adds ()
  144. {
  145. // arrange
  146. Enable (ConfigLocations.HardCoded);
  147. var theme = new ThemeScope ();
  148. theme.LoadHardCodedDefaults ();
  149. Assert.NotEmpty (theme);
  150. Assert.Equal (5, SchemeManager.GetSchemes ().Count);
  151. theme ["Schemes"].PropertyValue = SchemeManager.GetSchemes ();
  152. Dictionary<string, Scheme> schemes = (Dictionary<string, Scheme>)theme ["Schemes"].PropertyValue!;
  153. Assert.Equal (SchemeManager.GetSchemes ().Count, schemes.Count);
  154. var newTheme = new ThemeScope ();
  155. newTheme.LoadHardCodedDefaults ();
  156. var scheme = new Scheme
  157. {
  158. // note: Scheme's can't be partial; default for each attribute
  159. // is always White/Black
  160. Normal = new (Color.Red, Color.Green),
  161. Focus = new (Color.Cyan, Color.BrightCyan),
  162. HotNormal = new (Color.Yellow, Color.BrightYellow),
  163. HotFocus = new (Color.Green, Color.BrightGreen),
  164. Disabled = new (Color.Gray, Color.DarkGray)
  165. };
  166. newTheme ["Schemes"].PropertyValue = SchemeManager.GetSchemesForCurrentTheme ();
  167. Assert.Equal (5, SchemeManager.GetSchemes ().Count);
  168. // add a new Scheme to the newTheme
  169. ((Dictionary<string, Scheme>)theme ["Schemes"].PropertyValue!) ["Test"] = scheme;
  170. schemes = (Dictionary<string, Scheme>)theme ["Schemes"].PropertyValue!;
  171. Assert.Equal (SchemeManager.GetSchemes ().Count, schemes.Count);
  172. // Act
  173. theme.UpdateFrom (newTheme);
  174. // Assert
  175. schemes = (Dictionary<string, Scheme>)theme ["Schemes"].PropertyValue!;
  176. Assert.Equal (schemes ["Test"].Normal, scheme.Normal);
  177. Assert.Equal (schemes ["Test"].Focus, scheme.Focus);
  178. Disable (true);
  179. }
  180. [Fact]
  181. public void Load_Changes ()
  182. {
  183. // arrange
  184. Enable (ConfigLocations.HardCoded);
  185. var theme = new ThemeScope ();
  186. theme.LoadHardCodedDefaults ();
  187. Assert.NotEmpty (theme);
  188. var scheme = new Scheme
  189. {
  190. // note: Scheme's can't be partial; default for each attribute
  191. // is always White/Black
  192. Normal = new (Color.Red, Color.Green),
  193. Focus = new (Color.Cyan, Color.BrightCyan),
  194. HotNormal = new (Color.Yellow, Color.BrightYellow),
  195. HotFocus = new (Color.Green, Color.BrightGreen),
  196. Disabled = new (Color.Gray, Color.DarkGray)
  197. };
  198. theme ["Schemes"].PropertyValue = SchemeManager.GetSchemesForCurrentTheme ();
  199. ((Dictionary<string, Scheme>)theme ["Schemes"].PropertyValue!)! ["Test"] = scheme;
  200. Dictionary<string, Scheme>? schemes = (Dictionary<string, Scheme>)theme ["Schemes"].PropertyValue!;
  201. Assert.Equal (scheme.Normal, schemes ["Test"].Normal);
  202. Assert.Equal (scheme.Focus, schemes ["Test"].Focus);
  203. // Change just Normal
  204. var newTheme = new ThemeScope ();
  205. newTheme.LoadHardCodedDefaults ();
  206. var newScheme = new Scheme
  207. {
  208. Normal = new (Color.Blue, Color.BrightBlue),
  209. Focus = scheme.Focus,
  210. HotNormal = scheme.HotNormal,
  211. HotFocus = scheme.HotFocus,
  212. Disabled = scheme.Disabled
  213. };
  214. newTheme ["Schemes"].PropertyValue = SchemeManager.GetSchemesForCurrentTheme ();
  215. ((Dictionary<string, Scheme>)newTheme ["Schemes"].PropertyValue!)! ["Test"] = newScheme;
  216. // Act
  217. theme.UpdateFrom (newTheme);
  218. // Assert
  219. schemes = (Dictionary<string, Scheme>)theme ["Schemes"].PropertyValue!;
  220. // Normal should have changed
  221. Assert.Equal (new (Color.Blue, Color.BrightBlue), schemes ["Test"].Normal);
  222. Assert.Equal (Color.BrightBlue, schemes ["Test"].Normal.Background);
  223. Assert.Equal (Color.Cyan, schemes ["Test"].Focus.Foreground);
  224. Assert.Equal (Color.BrightCyan, schemes ["Test"].Focus.Background);
  225. Disable (true);
  226. }
  227. [Fact (Skip = "TODO: This should throw an exception")]
  228. public void Load_Null_Scheme_Throws ()
  229. {
  230. try
  231. {
  232. Enable (ConfigLocations.HardCoded);
  233. ThrowOnJsonErrors = true;
  234. // Create a test theme
  235. RuntimeConfig = """
  236. {
  237. "Theme": "TestTheme",
  238. "Themes": [
  239. {
  240. "TestTheme": {
  241. }
  242. }
  243. ]
  244. }
  245. """;
  246. // Load the test theme
  247. // TODO: This should throw an exception!
  248. Assert.Throws<JsonException> (() => Load (ConfigLocations.Runtime));
  249. Assert.Contains ("TestTheme", ThemeManager.Themes!);
  250. Assert.Equal ("TestTheme", ThemeManager.Theme);
  251. Assert.Throws<System.Collections.Generic.KeyNotFoundException> (SchemeManager.GetSchemes);
  252. }
  253. finally
  254. {
  255. Disable (true);
  256. }
  257. }
  258. [Fact]
  259. public void Load_Empty_Scheme_Throws ()
  260. {
  261. try
  262. {
  263. Enable (ConfigLocations.HardCoded);
  264. ThrowOnJsonErrors = true;
  265. // Create a test theme
  266. RuntimeConfig = """
  267. {
  268. "Theme": "TestTheme",
  269. "Themes": [
  270. {
  271. "TestTheme": {
  272. "Schemes": []
  273. }
  274. }
  275. ]
  276. }
  277. """;
  278. // Load the test theme
  279. Load (ConfigLocations.Runtime);
  280. Assert.Equal ("TestTheme", ThemeManager.Theme);
  281. // Now reset everything and reload
  282. ResetToHardCodedDefaults ();
  283. // Verify we're back to default
  284. Assert.Equal ("Default", ThemeManager.Theme);
  285. }
  286. finally
  287. {
  288. Disable (true);
  289. }
  290. }
  291. [Fact]
  292. public void Load_Custom_Scheme_Loads ()
  293. {
  294. try
  295. {
  296. Enable (ConfigLocations.HardCoded);
  297. ThrowOnJsonErrors = true;
  298. // Create a test theme
  299. RuntimeConfig = """
  300. {
  301. "Theme": "TestTheme",
  302. "Themes": [
  303. {
  304. "TestTheme": {
  305. "Schemes": [
  306. {
  307. "TopLevel": {
  308. "Normal": {
  309. "Foreground": "AntiqueWhite",
  310. "Background": "DimGray"
  311. },
  312. "Focus": {
  313. "Foreground": "White",
  314. "Background": "DarkGray"
  315. },
  316. "HotNormal": {
  317. "Foreground": "Wheat",
  318. "Background": "DarkGray",
  319. "Style": "Underline"
  320. },
  321. "HotFocus": {
  322. "Foreground": "LightYellow",
  323. "Background": "DimGray",
  324. "Style": "Underline"
  325. },
  326. "Disabled": {
  327. "Foreground": "Black",
  328. "Background": "DimGray"
  329. }
  330. }
  331. },
  332. {
  333. "Base": {
  334. "Normal": {
  335. "Foreground": "White",
  336. "Background": "Blue"
  337. },
  338. "Focus": {
  339. "Foreground": "DarkBlue",
  340. "Background": "LightGray"
  341. },
  342. "HotNormal": {
  343. "Foreground": "BrightCyan",
  344. "Background": "Blue"
  345. },
  346. "HotFocus": {
  347. "Foreground": "BrightBlue",
  348. "Background": "LightGray"
  349. },
  350. "Disabled": {
  351. "Foreground": "DarkGray",
  352. "Background": "Blue"
  353. }
  354. }
  355. },
  356. {
  357. "Dialog": {
  358. "Normal": {
  359. "Foreground": "Black",
  360. "Background": "LightGray"
  361. },
  362. "Focus": {
  363. "Foreground": "DarkGray",
  364. "Background": "LightGray"
  365. },
  366. "HotNormal": {
  367. "Foreground": "Blue",
  368. "Background": "LightGray"
  369. },
  370. "HotFocus": {
  371. "Foreground": "BrightBlue",
  372. "Background": "LightGray"
  373. },
  374. "Disabled": {
  375. "Foreground": "Gray",
  376. "Background": "DarkGray"
  377. }
  378. }
  379. },
  380. {
  381. "Menu": {
  382. "Normal": {
  383. "Foreground": "White",
  384. "Background": "DarkBlue",
  385. "Style": "Reverse" // Not default Bold
  386. },
  387. "Focus": {
  388. "Foreground": "White",
  389. "Background": "DarkBlue",
  390. "Style": "Bold,Reverse"
  391. },
  392. "HotNormal": {
  393. "Foreground": "BrightYellow",
  394. "Background": "DarkBlue",
  395. "Style": "Bold,Underline"
  396. },
  397. "HotFocus": {
  398. "Foreground": "Blue",
  399. "Background": "White",
  400. "Style": "Bold,Underline"
  401. },
  402. "Disabled": {
  403. "Foreground": "Gray",
  404. "Background": "DarkGray",
  405. "Style": "Faint"
  406. }
  407. }
  408. },
  409. {
  410. "Error": {
  411. "Normal": {
  412. "Foreground": "Red",
  413. "Background": "Pink"
  414. },
  415. "Focus": {
  416. "Foreground": "White",
  417. "Background": "BrightRed"
  418. },
  419. "HotNormal": {
  420. "Foreground": "Black",
  421. "Background": "Pink"
  422. },
  423. "HotFocus": {
  424. "Foreground": "Pink",
  425. "Background": "BrightRed"
  426. },
  427. "Disabled": {
  428. "Foreground": "DarkGray",
  429. "Background": "White"
  430. }
  431. }
  432. }
  433. ]
  434. }
  435. }
  436. ]
  437. }
  438. """;
  439. // Capture hardCoded hard-coded scheme colors
  440. ImmutableSortedDictionary<string, Scheme> hardCodedSchemes = SchemeManager.GetHardCodedSchemes ()!;
  441. Color hardCodedTopLevelNormalFg = hardCodedSchemes ["TopLevel"].Normal.Foreground;
  442. Assert.Equal (new Color (StandardColor.CadetBlue).ToString (), hardCodedTopLevelNormalFg.ToString ());
  443. Assert.Equal (hardCodedSchemes ["Menu"].Normal.Style, SchemeManager.GetSchemesForCurrentTheme () ["Menu"]!.Normal.Style);
  444. // Capture current scheme colors
  445. Dictionary<string, Scheme> currentSchemes = SchemeManager.GetSchemes ()!;
  446. Color currentTopLevelNormalFg = currentSchemes ["TopLevel"].Normal.Foreground;
  447. Assert.Equal (new Color (StandardColor.CadetBlue).ToString (), currentTopLevelNormalFg.ToString ());
  448. // Load the test theme
  449. Load (ConfigLocations.Runtime);
  450. Assert.Equal ("TestTheme", ThemeManager.Theme);
  451. Assert.Equal (TextStyle.Reverse, SchemeManager.GetSchemesForCurrentTheme () ["Menu"]!.Normal.Style);
  452. currentSchemes = SchemeManager.GetSchemesForCurrentTheme ()!;
  453. currentTopLevelNormalFg = currentSchemes ["TopLevel"].Normal.Foreground;
  454. Assert.NotEqual (hardCodedTopLevelNormalFg.ToString (), currentTopLevelNormalFg.ToString ());
  455. // Now reset everything and reload
  456. ResetToHardCodedDefaults ();
  457. // Verify we're back to default
  458. Assert.Equal ("Default", ThemeManager.Theme);
  459. currentSchemes = SchemeManager.GetSchemes ()!;
  460. currentTopLevelNormalFg = currentSchemes ["TopLevel"].Normal.Foreground;
  461. Assert.Equal (hardCodedTopLevelNormalFg.ToString (), currentTopLevelNormalFg.ToString ());
  462. }
  463. finally
  464. {
  465. Disable (true);
  466. }
  467. }
  468. [Fact]
  469. public void Load_Modified_Default_Scheme_Loads ()
  470. {
  471. try
  472. {
  473. Enable (ConfigLocations.HardCoded);
  474. ThrowOnJsonErrors = true;
  475. // Create a test theme
  476. RuntimeConfig = """
  477. {
  478. "Theme": "Default",
  479. "Themes": [
  480. {
  481. "Default": {
  482. "Schemes": [
  483. {
  484. "TopLevel": {
  485. "Normal": {
  486. "Foreground": "AntiqueWhite",
  487. "Background": "DimGray"
  488. },
  489. "Focus": {
  490. "Foreground": "White",
  491. "Background": "DarkGray"
  492. },
  493. "HotNormal": {
  494. "Foreground": "Wheat",
  495. "Background": "DarkGray",
  496. "Style": "Underline"
  497. },
  498. "HotFocus": {
  499. "Foreground": "LightYellow",
  500. "Background": "DimGray",
  501. "Style": "Underline"
  502. },
  503. "Disabled": {
  504. "Foreground": "Black",
  505. "Background": "DimGray"
  506. }
  507. }
  508. },
  509. {
  510. "Base": {
  511. "Normal": {
  512. "Foreground": "White",
  513. "Background": "Blue"
  514. },
  515. "Focus": {
  516. "Foreground": "DarkBlue",
  517. "Background": "LightGray"
  518. },
  519. "HotNormal": {
  520. "Foreground": "BrightCyan",
  521. "Background": "Blue"
  522. },
  523. "HotFocus": {
  524. "Foreground": "BrightBlue",
  525. "Background": "LightGray"
  526. },
  527. "Disabled": {
  528. "Foreground": "DarkGray",
  529. "Background": "Blue"
  530. }
  531. }
  532. },
  533. {
  534. "Dialog": {
  535. "Normal": {
  536. "Foreground": "Black",
  537. "Background": "LightGray"
  538. },
  539. "Focus": {
  540. "Foreground": "DarkGray",
  541. "Background": "LightGray"
  542. },
  543. "HotNormal": {
  544. "Foreground": "Blue",
  545. "Background": "LightGray"
  546. },
  547. "HotFocus": {
  548. "Foreground": "BrightBlue",
  549. "Background": "LightGray"
  550. },
  551. "Disabled": {
  552. "Foreground": "Gray",
  553. "Background": "DarkGray"
  554. }
  555. }
  556. },
  557. {
  558. "Menu": {
  559. "Normal": {
  560. "Foreground": "White",
  561. "Background": "DarkBlue",
  562. "Style": "Reverse" // Not default Bold
  563. },
  564. "Focus": {
  565. "Foreground": "White",
  566. "Background": "DarkBlue",
  567. "Style": "Bold,Reverse"
  568. },
  569. "HotNormal": {
  570. "Foreground": "BrightYellow",
  571. "Background": "DarkBlue",
  572. "Style": "Bold,Underline"
  573. },
  574. "HotFocus": {
  575. "Foreground": "Blue",
  576. "Background": "White",
  577. "Style": "Bold,Underline"
  578. },
  579. "Disabled": {
  580. "Foreground": "Gray",
  581. "Background": "DarkGray",
  582. "Style": "Faint"
  583. }
  584. }
  585. },
  586. {
  587. "Error": {
  588. "Normal": {
  589. "Foreground": "Red",
  590. "Background": "Pink"
  591. },
  592. "Focus": {
  593. "Foreground": "White",
  594. "Background": "BrightRed"
  595. },
  596. "HotNormal": {
  597. "Foreground": "Black",
  598. "Background": "Pink"
  599. },
  600. "HotFocus": {
  601. "Foreground": "Pink",
  602. "Background": "BrightRed"
  603. },
  604. "Disabled": {
  605. "Foreground": "DarkGray",
  606. "Background": "White"
  607. }
  608. }
  609. }
  610. ]
  611. }
  612. }
  613. ]
  614. }
  615. """;
  616. // Capture hardCoded hard-coded scheme colors
  617. ImmutableSortedDictionary<string, Scheme> hardCodedSchemes = SchemeManager.GetHardCodedSchemes ()!;
  618. Color hardCodedTopLevelNormalFg = hardCodedSchemes ["TopLevel"].Normal.Foreground;
  619. Assert.Equal (new Color (StandardColor.CadetBlue).ToString (), hardCodedTopLevelNormalFg.ToString ());
  620. Assert.Equal (hardCodedSchemes ["Menu"].Normal.Style, SchemeManager.GetSchemesForCurrentTheme () ["Menu"]!.Normal.Style);
  621. // Capture current scheme colors
  622. Dictionary<string, Scheme> currentSchemes = SchemeManager.GetSchemes ()!;
  623. Color currentTopLevelNormalFg = currentSchemes ["TopLevel"].Normal.Foreground;
  624. Assert.Equal (new Color (StandardColor.CadetBlue).ToString (), currentTopLevelNormalFg.ToString ());
  625. // Load the test theme
  626. Load (ConfigLocations.Runtime);
  627. Assert.Equal ("Default", ThemeManager.Theme);
  628. // BUGBUG: We did not Apply after loading, so schemes should NOT have been updated
  629. Assert.Equal (TextStyle.Reverse, SchemeManager.GetSchemesForCurrentTheme () ["Menu"]!.Normal.Style);
  630. currentSchemes = SchemeManager.GetSchemesForCurrentTheme ()!;
  631. currentTopLevelNormalFg = currentSchemes ["TopLevel"].Normal.Foreground;
  632. // BUGBUG: We did not Apply after loading, so schemes should NOT have been updated
  633. //Assert.Equal (hardCodedTopLevelNormalFg.ToString (), currentTopLevelNormalFg.ToString ());
  634. // Now reset everything and reload
  635. ResetToHardCodedDefaults ();
  636. // Verify we're back to default
  637. Assert.Equal ("Default", ThemeManager.Theme);
  638. currentSchemes = SchemeManager.GetSchemes ()!;
  639. currentTopLevelNormalFg = currentSchemes ["TopLevel"].Normal.Foreground;
  640. Assert.Equal (hardCodedTopLevelNormalFg.ToString (), currentTopLevelNormalFg.ToString ());
  641. }
  642. finally
  643. {
  644. Disable (true);
  645. }
  646. }
  647. [Fact]
  648. public void Load_From_Json_Does_Not_Corrupt_HardCodedSchemes ()
  649. {
  650. try
  651. {
  652. Enable (ConfigLocations.HardCoded);
  653. // Create a test theme
  654. string json = """
  655. {
  656. "Theme": "TestTheme",
  657. "Themes": [
  658. {
  659. "TestTheme": {
  660. "Schemes": [
  661. {
  662. "TopLevel": {
  663. "Normal": {
  664. "Foreground": "AntiqueWhite",
  665. "Background": "DimGray"
  666. },
  667. "Focus": {
  668. "Foreground": "White",
  669. "Background": "DarkGray"
  670. },
  671. "HotNormal": {
  672. "Foreground": "Wheat",
  673. "Background": "DarkGray",
  674. "Style": "Underline"
  675. },
  676. "HotFocus": {
  677. "Foreground": "LightYellow",
  678. "Background": "DimGray",
  679. "Style": "Underline"
  680. },
  681. "Disabled": {
  682. "Foreground": "Black",
  683. "Background": "DimGray"
  684. }
  685. }
  686. },
  687. {
  688. "Base": {
  689. "Normal": {
  690. "Foreground": "White",
  691. "Background": "Blue"
  692. },
  693. "Focus": {
  694. "Foreground": "DarkBlue",
  695. "Background": "LightGray"
  696. },
  697. "HotNormal": {
  698. "Foreground": "BrightCyan",
  699. "Background": "Blue"
  700. },
  701. "HotFocus": {
  702. "Foreground": "BrightBlue",
  703. "Background": "LightGray"
  704. },
  705. "Disabled": {
  706. "Foreground": "DarkGray",
  707. "Background": "Blue"
  708. }
  709. }
  710. },
  711. {
  712. "Dialog": {
  713. "Normal": {
  714. "Foreground": "Black",
  715. "Background": "LightGray"
  716. },
  717. "Focus": {
  718. "Foreground": "DarkGray",
  719. "Background": "LightGray"
  720. },
  721. "HotNormal": {
  722. "Foreground": "Blue",
  723. "Background": "LightGray"
  724. },
  725. "HotFocus": {
  726. "Foreground": "BrightBlue",
  727. "Background": "LightGray"
  728. },
  729. "Disabled": {
  730. "Foreground": "Gray",
  731. "Background": "DarkGray"
  732. }
  733. }
  734. },
  735. {
  736. "Menu": {
  737. "Normal": {
  738. "Foreground": "White",
  739. "Background": "DarkBlue",
  740. "Style": "Reverse" // Not default Bold
  741. },
  742. "Focus": {
  743. "Foreground": "White",
  744. "Background": "DarkBlue",
  745. "Style": "Bold,Reverse"
  746. },
  747. "HotNormal": {
  748. "Foreground": "BrightYellow",
  749. "Background": "DarkBlue",
  750. "Style": "Bold,Underline"
  751. },
  752. "HotFocus": {
  753. "Foreground": "Blue",
  754. "Background": "White",
  755. "Style": "Bold,Underline"
  756. },
  757. "Disabled": {
  758. "Foreground": "Gray",
  759. "Background": "DarkGray",
  760. "Style": "Faint"
  761. }
  762. }
  763. },
  764. {
  765. "Error": {
  766. "Normal": {
  767. "Foreground": "Red",
  768. "Background": "Pink"
  769. },
  770. "Focus": {
  771. "Foreground": "White",
  772. "Background": "BrightRed"
  773. },
  774. "HotNormal": {
  775. "Foreground": "Black",
  776. "Background": "Pink"
  777. },
  778. "HotFocus": {
  779. "Foreground": "Pink",
  780. "Background": "BrightRed"
  781. },
  782. "Disabled": {
  783. "Foreground": "DarkGray",
  784. "Background": "White"
  785. }
  786. }
  787. }
  788. ]
  789. }
  790. }
  791. ]
  792. }
  793. """;
  794. // Capture dynamically created hardCoded hard-coded scheme colors
  795. ImmutableSortedDictionary<string, Scheme> hardCodedSchemes = SchemeManager.GetHardCodedSchemes ()!;
  796. Color hardCodedTopLevelNormalFg = hardCodedSchemes ["TopLevel"].Normal.Foreground;
  797. Assert.Equal (new Color (StandardColor.CadetBlue).ToString (), hardCodedTopLevelNormalFg.ToString ());
  798. // Capture current scheme colors
  799. Dictionary<string, Scheme> currentSchemes = SchemeManager.GetSchemes ()!;
  800. Color currentTopLevelNormalFg = currentSchemes ["TopLevel"].Normal.Foreground;
  801. Assert.Equal (new Color (StandardColor.CadetBlue).ToString (), currentTopLevelNormalFg.ToString ());
  802. // Load the test theme
  803. ConfigurationManager.SourcesManager?.Load (Settings, json, "UpdateFromJson", ConfigLocations.Runtime);
  804. Assert.Equal ("TestTheme", ThemeManager.Theme);
  805. Assert.Equal (TextStyle.Reverse, SchemeManager.GetSchemesForCurrentTheme () ["Menu"]!.Normal.Style);
  806. Dictionary<string, Scheme>? hardCodedSchemesViaScope = GetHardCodedConfigPropertiesByScope ("ThemeScope")!.ToFrozenDictionary () ["Schemes"].PropertyValue as Dictionary<string, Scheme>;
  807. Assert.Equal (hardCodedTopLevelNormalFg.ToString (), hardCodedSchemesViaScope! ["TopLevel"].Normal.Foreground.ToString ());
  808. }
  809. finally
  810. {
  811. Disable (true);
  812. }
  813. }
  814. [Fact (Skip = "WIP")]
  815. public void Apply_UpdatesSchemes ()
  816. {
  817. Enable (ConfigLocations.HardCoded);
  818. Assert.False (SchemeManager.GetSchemes ()!.ContainsKey ("test"));
  819. Assert.Equal (5, SchemeManager.GetSchemes ().Count); // base, toplevel, menu, error, dialog
  820. var theme = new ThemeScope ();
  821. Assert.NotEmpty (theme);
  822. ThemeManager.Themes!.TryAdd ("testTheme", theme);
  823. var scheme = new Scheme { Normal = new (Color.Red, Color.Green) };
  824. theme ["Schemes"].PropertyValue = new Dictionary<string, Scheme> (StringComparer.InvariantCultureIgnoreCase) { { "test", scheme } };
  825. Assert.Equal (
  826. new (Color.Red),
  827. ((Dictionary<string, Scheme>)theme ["Schemes"].PropertyValue!) ["test"].Normal.Foreground
  828. );
  829. Assert.Equal (
  830. new (Color.Green),
  831. ((Dictionary<string, Scheme>)theme ["Schemes"].PropertyValue!) ["test"].Normal.Background
  832. );
  833. // Act
  834. ThemeManager.Theme = "testTheme";
  835. ThemeManager.Themes! [ThemeManager.Theme]!.Apply ();
  836. Assert.Equal (5, SchemeManager.GetSchemes ().Count); // base, toplevel, menu, error, dialog
  837. // Assert
  838. Scheme updatedScheme = SchemeManager.GetSchemes () ["test"]!;
  839. Assert.Equal (new (Color.Red), updatedScheme.Normal.Foreground);
  840. Assert.Equal (new (Color.Green), updatedScheme.Normal.Background);
  841. // remove test Scheme from Colors to avoid failures on others unit tests with Scheme
  842. SchemeManager.GetSchemes ().Remove ("test");
  843. Assert.Equal (5, SchemeManager.GetSchemes ().Count);
  844. Disable (true);
  845. }
  846. [Fact]
  847. public void AddScheme_Adds_And_Updates_Scheme ()
  848. {
  849. // Arrange
  850. var scheme = new Scheme (new Attribute (Color.Red, Color.Green));
  851. string schemeName = "CustomScheme";
  852. // Act
  853. SchemeManager.AddScheme (schemeName, scheme);
  854. // Assert
  855. Assert.Equal (scheme, SchemeManager.GetScheme (schemeName));
  856. // Update the scheme
  857. var updatedScheme = new Scheme (new Attribute (Color.Blue, Color.Yellow));
  858. SchemeManager.AddScheme (schemeName, updatedScheme);
  859. Assert.Equal (updatedScheme, SchemeManager.GetScheme (schemeName));
  860. // Cleanup
  861. SchemeManager.RemoveScheme (schemeName);
  862. }
  863. [Fact]
  864. public void RemoveScheme_Removes_Custom_Scheme ()
  865. {
  866. var scheme = new Scheme (new Attribute (Color.Red, Color.Green));
  867. string schemeName = "RemovableScheme";
  868. SchemeManager.AddScheme (schemeName, scheme);
  869. Assert.Equal (scheme, SchemeManager.GetScheme (schemeName));
  870. SchemeManager.RemoveScheme (schemeName);
  871. Assert.Throws<KeyNotFoundException> (() => SchemeManager.GetScheme (schemeName));
  872. }
  873. }