ColorTests.cs 4.0 KB

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