ColorTests.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using Xunit;
  3. // Alias Console to MockConsole so we don't accidentally use Console
  4. using Console = Terminal.Gui.FakeConsole;
  5. namespace Terminal.Gui.DriverTests {
  6. public class ColorTests {
  7. [Theory]
  8. [InlineData (typeof (FakeDriver))]
  9. [InlineData (typeof (NetDriver))]
  10. [InlineData (typeof (CursesDriver))]
  11. [InlineData (typeof (WindowsDriver))]
  12. public void SetColors_Changes_Colors (Type driverType)
  13. {
  14. var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
  15. Application.Init (driver);
  16. Assert.Equal (ConsoleColor.Gray, Console.ForegroundColor);
  17. Assert.Equal (ConsoleColor.Black, Console.BackgroundColor);
  18. Console.ForegroundColor = ConsoleColor.Red;
  19. Assert.Equal (ConsoleColor.Red, Console.ForegroundColor);
  20. Console.BackgroundColor = ConsoleColor.Green;
  21. Assert.Equal (ConsoleColor.Green, Console.BackgroundColor);
  22. Console.ResetColor ();
  23. Assert.Equal (ConsoleColor.Gray, Console.ForegroundColor);
  24. Assert.Equal (ConsoleColor.Black, Console.BackgroundColor);
  25. // Shutdown must be called to safely clean up Application if Init has been called
  26. Application.Shutdown ();
  27. }
  28. [Fact, AutoInitShutdown]
  29. public void ColorScheme_New ()
  30. {
  31. var scheme = new ColorScheme ();
  32. var lbl = new Label ();
  33. lbl.ColorScheme = scheme;
  34. lbl.Draw ();
  35. }
  36. [Fact]
  37. public void TestAllColors ()
  38. {
  39. var colors = System.Enum.GetValues (typeof (Color));
  40. Attribute [] attrs = new Attribute [colors.Length];
  41. int idx = 0;
  42. foreach (Color bg in colors) {
  43. attrs [idx] = new Attribute (bg, colors.Length - 1 - bg);
  44. idx++;
  45. }
  46. Assert.Equal (16, attrs.Length);
  47. Assert.Equal (new Attribute (Color.Black, Color.White), attrs [0]);
  48. Assert.Equal (new Attribute (Color.Blue, Color.BrightYellow), attrs [1]);
  49. Assert.Equal (new Attribute (Color.Green, Color.BrightMagenta), attrs [2]);
  50. Assert.Equal (new Attribute (Color.Cyan, Color.BrightRed), attrs [3]);
  51. Assert.Equal (new Attribute (Color.Red, Color.BrightCyan), attrs [4]);
  52. Assert.Equal (new Attribute (Color.Magenta, Color.BrightGreen), attrs [5]);
  53. Assert.Equal (new Attribute (Color.Brown, Color.BrightBlue), attrs [6]);
  54. Assert.Equal (new Attribute (Color.Gray, Color.DarkGray), attrs [7]);
  55. Assert.Equal (new Attribute (Color.DarkGray, Color.Gray), attrs [8]);
  56. Assert.Equal (new Attribute (Color.BrightBlue, Color.Brown), attrs [9]);
  57. Assert.Equal (new Attribute (Color.BrightGreen, Color.Magenta), attrs [10]);
  58. Assert.Equal (new Attribute (Color.BrightCyan, Color.Red), attrs [11]);
  59. Assert.Equal (new Attribute (Color.BrightRed, Color.Cyan), attrs [12]);
  60. Assert.Equal (new Attribute (Color.BrightMagenta, Color.Green), attrs [13]);
  61. Assert.Equal (new Attribute (Color.BrightYellow, Color.Blue), attrs [14]);
  62. Assert.Equal (new Attribute (Color.White, Color.Black), attrs [^1]);
  63. }
  64. [Theory]
  65. [InlineData (typeof (FakeDriver), false)]
  66. [InlineData (typeof (NetDriver), true)]
  67. [InlineData (typeof (CursesDriver), false)]
  68. [InlineData (typeof (WindowsDriver), true)] // Because we're not Windows Terminal
  69. public void SupportsTrueColor_Defaults (Type driverType, bool expectedSetting)
  70. {
  71. var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
  72. driver.Init (() => { });
  73. Assert.Equal (expectedSetting, driver.SupportsTrueColor);
  74. driver.End ();
  75. // Shutdown must be called to safely clean up Application if Init has been called
  76. Application.Shutdown ();
  77. }
  78. [Theory]
  79. [InlineData (typeof (FakeDriver))]
  80. [InlineData (typeof (NetDriver))]
  81. [InlineData (typeof (CursesDriver))]
  82. [InlineData (typeof (WindowsDriver))]
  83. public void Force16Colors_Sets (Type driverType)
  84. {
  85. var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
  86. driver.Init (() => { });
  87. driver.Force16Colors = true;
  88. Assert.True (driver.Force16Colors);
  89. driver.End ();
  90. // Shutdown must be called to safely clean up Application if Init has been called
  91. Application.Shutdown ();
  92. }
  93. }
  94. }