ColorTests.cs 4.0 KB

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