RuneCellTests.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Xunit;
  5. using Xunit.Abstractions;
  6. namespace Terminal.Gui.ViewsTests {
  7. public class RuneCellTests {
  8. readonly ITestOutputHelper _output;
  9. public RuneCellTests (ITestOutputHelper output)
  10. {
  11. _output = output;
  12. }
  13. [Fact]
  14. public void Constructor_Defaults ()
  15. {
  16. var rc = new RuneCell ();
  17. Assert.NotNull (rc);
  18. Assert.Equal (0, rc.Rune.Value);
  19. Assert.Null (rc.ColorScheme);
  20. }
  21. [Fact]
  22. public void Equals_True ()
  23. {
  24. var rc1 = new RuneCell ();
  25. var rc2 = new RuneCell ();
  26. Assert.True (rc1.Equals (rc2));
  27. Assert.True (rc2.Equals (rc1));
  28. rc1.Rune = new Rune ('a');
  29. rc1.ColorScheme = new ColorScheme ();
  30. rc2.Rune = new Rune ('a');
  31. rc2.ColorScheme = new ColorScheme ();
  32. Assert.True (rc1.Equals (rc2));
  33. Assert.True (rc2.Equals (rc1));
  34. }
  35. [Fact]
  36. public void Equals_False ()
  37. {
  38. var rc1 = new RuneCell ();
  39. var rc2 = new RuneCell () {
  40. Rune = new Rune ('a'),
  41. ColorScheme = new ColorScheme () { Normal = new Attribute (Color.Red) }
  42. };
  43. Assert.False (rc1.Equals (rc2));
  44. Assert.False (rc2.Equals (rc1));
  45. rc1.Rune = new Rune ('a');
  46. rc1.ColorScheme = new ColorScheme ();
  47. Assert.Equal (rc1.Rune, rc2.Rune);
  48. Assert.False (rc1.Equals (rc2));
  49. Assert.False (rc2.Equals (rc1));
  50. }
  51. [Fact]
  52. public void ToString_Override ()
  53. {
  54. var rc1 = new RuneCell ();
  55. var rc2 = new RuneCell () {
  56. Rune = new Rune ('a'),
  57. ColorScheme = new ColorScheme () { Normal = new Attribute (Color.Red) }
  58. };
  59. Assert.Equal ("U+0000 '\0'; null", rc1.ToString ());
  60. Assert.Equal ("U+0061 'a'; Normal: Red,Red; Focus: White,Black; HotNormal: White,Black; HotFocus: White,Black; Disabled: White,Black", rc2.ToString ());
  61. }
  62. // TODO: Move the tests below to View or Color - they test ColorScheme, not RuneCell primitives.
  63. private TextView CreateTextView ()
  64. {
  65. return new TextView () { Width = 30, Height = 10 };
  66. }
  67. [Fact, AutoInitShutdown]
  68. public void RuneCell_LoadRuneCells_InheritsPreviousColorScheme ()
  69. {
  70. List<RuneCell> runeCells = new List<RuneCell> ();
  71. foreach (var color in Colors.ColorSchemes) {
  72. string csName = color.Key;
  73. foreach (var rune in csName.EnumerateRunes ()) {
  74. runeCells.Add (new RuneCell { Rune = rune, ColorScheme = color.Value });
  75. }
  76. runeCells.Add (new RuneCell { Rune = (Rune)'\n', ColorScheme = color.Value });
  77. }
  78. var tv = CreateTextView ();
  79. tv.Load (runeCells);
  80. Application.Top.Add (tv);
  81. var rs = Application.Begin (Application.Top);
  82. Assert.True (tv.InheritsPreviousColorScheme);
  83. var expectedText = @"
  84. TopLevel
  85. Base
  86. Dialog
  87. Menu
  88. Error ";
  89. TestHelpers.AssertDriverContentsWithFrameAre (expectedText, _output);
  90. var attributes = new Attribute [] {
  91. // 0
  92. Colors.TopLevel.Focus,
  93. // 1
  94. Colors.Base.Focus,
  95. // 2
  96. Colors.Dialog.Focus,
  97. // 3
  98. Colors.Menu.Focus,
  99. // 4
  100. Colors.Error.Focus
  101. };
  102. var expectedColor = @"
  103. 0000000000
  104. 1111000000
  105. 2222220000
  106. 3333000000
  107. 4444400000";
  108. TestHelpers.AssertDriverAttributesAre (expectedColor, driver: Application.Driver, attributes);
  109. tv.WordWrap = true;
  110. Application.Refresh ();
  111. TestHelpers.AssertDriverContentsWithFrameAre (expectedText, _output);
  112. TestHelpers.AssertDriverAttributesAre (expectedColor, driver: Application.Driver, attributes);
  113. tv.CursorPosition = new Point (6, 2);
  114. tv.SelectionStartColumn = 0;
  115. tv.SelectionStartRow = 0;
  116. Assert.Equal ($"TopLevel{Environment.NewLine}Base{Environment.NewLine}Dialog", tv.SelectedText);
  117. tv.Copy ();
  118. tv.Selecting = false;
  119. tv.CursorPosition = new Point (2, 4);
  120. tv.Paste ();
  121. Application.Refresh ();
  122. expectedText = @"
  123. TopLevel
  124. Base
  125. Dialog
  126. Menu
  127. ErTopLevel
  128. Base
  129. Dialogror ";
  130. TestHelpers.AssertDriverContentsWithFrameAre (expectedText, _output);
  131. expectedColor = @"
  132. 0000000000
  133. 1111000000
  134. 2222220000
  135. 3333000000
  136. 4444444444
  137. 4444000000
  138. 4444444440";
  139. TestHelpers.AssertDriverAttributesAre (expectedColor, driver: Application.Driver, attributes);
  140. tv.Undo ();
  141. tv.CursorPosition = new Point (0, 3);
  142. tv.SelectionStartColumn = 0;
  143. tv.SelectionStartRow = 0;
  144. Assert.Equal ($"TopLevel{Environment.NewLine}Base{Environment.NewLine}Dialog{Environment.NewLine}", tv.SelectedText);
  145. tv.Copy ();
  146. tv.Selecting = false;
  147. tv.CursorPosition = new Point (2, 4);
  148. tv.Paste ();
  149. Application.Refresh ();
  150. expectedText = @"
  151. TopLevel
  152. Base
  153. Dialog
  154. Menu
  155. ErTopLevel
  156. Base
  157. Dialog
  158. ror ";
  159. TestHelpers.AssertDriverContentsWithFrameAre (expectedText, _output);
  160. expectedColor = @"
  161. 0000000000
  162. 1111000000
  163. 2222220000
  164. 3333000000
  165. 4444444444
  166. 4444000000
  167. 4444440000
  168. 4440000000";
  169. TestHelpers.AssertDriverAttributesAre (expectedColor, driver: Application.Driver, attributes);
  170. Application.End (rs);
  171. }
  172. [Fact, AutoInitShutdown]
  173. public void RuneCell_LoadRuneCells_Without_ColorScheme_Is_Never_Null ()
  174. {
  175. var cells = new List<RuneCell> {
  176. new RuneCell{Rune = new Rune ('T')},
  177. new RuneCell{Rune = new Rune ('e')},
  178. new RuneCell{Rune = new Rune ('s')},
  179. new RuneCell{Rune = new Rune ('t')}
  180. };
  181. var tv = CreateTextView ();
  182. Application.Top.Add (tv);
  183. tv.Load (cells);
  184. for (int i = 0; i < tv.Lines; i++) {
  185. var line = tv.GetLine (i);
  186. foreach (var rc in line) {
  187. Assert.NotNull (rc.ColorScheme);
  188. }
  189. }
  190. }
  191. [Fact, AutoInitShutdown]
  192. public void RuneCellEventArgs_WordWrap_True ()
  193. {
  194. var eventCount = 0;
  195. var text = new List<List<RuneCell>> () { TextModel.ToRuneCells ("This is the first line.".ToRunes ()), TextModel.ToRuneCells ("This is the second line.".ToRunes ()) };
  196. var tv = CreateTextView ();
  197. tv.DrawNormalColor += _textView_DrawColor;
  198. tv.DrawReadOnlyColor += _textView_DrawColor;
  199. tv.DrawSelectionColor += _textView_DrawColor;
  200. tv.DrawUsedColor += _textView_DrawColor;
  201. void _textView_DrawColor (object sender, RuneCellEventArgs e)
  202. {
  203. Assert.Equal (e.Line [e.Col], text [e.UnwrappedPosition.Row] [e.UnwrappedPosition.Col]);
  204. eventCount++;
  205. }
  206. tv.Text = $"{TextModel.ToString (text [0])}\n{TextModel.ToString (text [1])}\n";
  207. Assert.False (tv.WordWrap);
  208. Application.Top.Add (tv);
  209. Application.Begin (Application.Top);
  210. TestHelpers.AssertDriverContentsWithFrameAre (@"
  211. This is the first line.
  212. This is the second line.", _output);
  213. tv.Width = 10;
  214. tv.Height = 25;
  215. tv.WordWrap = true;
  216. Application.Refresh ();
  217. TestHelpers.AssertDriverContentsWithFrameAre (@"
  218. This is
  219. the
  220. first
  221. line.
  222. This is
  223. the
  224. second
  225. line. ", _output);
  226. Assert.Equal (eventCount, (text [0].Count + text [1].Count) * 2);
  227. }
  228. }
  229. }