ColorTests.cs 4.1 KB

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