RuneCellTests.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. private TextView CreateTextView ()
  63. {
  64. return new TextView () { Width = 30, Height = 10 };
  65. }
  66. [Fact, AutoInitShutdown]
  67. public void RuneCell_LoadRuneCells_InheritsPreviousColorScheme ()
  68. {
  69. List<RuneCell> runeCells = new List<RuneCell> ();
  70. foreach (var color in Colors.ColorSchemes) {
  71. string csName = color.Key;
  72. foreach (var rune in csName.EnumerateRunes ()) {
  73. runeCells.Add (new RuneCell { Rune = rune, ColorScheme = color.Value });
  74. }
  75. runeCells.Add (new RuneCell { Rune = (Rune)'\n', ColorScheme = color.Value });
  76. }
  77. var tv = CreateTextView ();
  78. tv.Load (runeCells);
  79. Application.Top.Add (tv);
  80. var rs = Application.Begin (Application.Top);
  81. Assert.True (tv.InheritsPreviousColorScheme);
  82. var expectedText = @"
  83. TopLevel
  84. Base
  85. Dialog
  86. Menu
  87. Error ";
  88. TestHelpers.AssertDriverContentsWithFrameAre (expectedText, _output);
  89. var attributes = new Attribute [] {
  90. // 0
  91. Colors.TopLevel.Focus,
  92. // 1
  93. Colors.Base.Focus,
  94. // 2
  95. Colors.Dialog.Focus,
  96. // 3
  97. Colors.Menu.Focus,
  98. // 4
  99. Colors.Error.Focus
  100. };
  101. var expectedColor = @"
  102. 0000000000
  103. 1111000000
  104. 2222220000
  105. 3333000000
  106. 4444400000";
  107. TestHelpers.AssertDriverColorsAre (expectedColor, attributes);
  108. tv.WordWrap = true;
  109. Application.Refresh ();
  110. TestHelpers.AssertDriverContentsWithFrameAre (expectedText, _output);
  111. TestHelpers.AssertDriverColorsAre (expectedColor, attributes);
  112. tv.CursorPosition = new Point (6, 2);
  113. tv.SelectionStartColumn = 0;
  114. tv.SelectionStartRow = 0;
  115. Assert.Equal ($"TopLevel{Environment.NewLine}Base{Environment.NewLine}Dialog", tv.SelectedText);
  116. tv.Copy ();
  117. tv.Selecting = false;
  118. tv.CursorPosition = new Point (2, 4);
  119. tv.Paste ();
  120. Application.Refresh ();
  121. expectedText = @"
  122. TopLevel
  123. Base
  124. Dialog
  125. Menu
  126. ErTopLevel
  127. Base
  128. Dialogror ";
  129. TestHelpers.AssertDriverContentsWithFrameAre (expectedText, _output);
  130. expectedColor = @"
  131. 0000000000
  132. 1111000000
  133. 2222220000
  134. 3333000000
  135. 4444444444
  136. 4444000000
  137. 4444444440";
  138. TestHelpers.AssertDriverColorsAre (expectedColor, attributes);
  139. tv.Undo ();
  140. tv.CursorPosition = new Point (0, 3);
  141. tv.SelectionStartColumn = 0;
  142. tv.SelectionStartRow = 0;
  143. Assert.Equal ($"TopLevel{Environment.NewLine}Base{Environment.NewLine}Dialog{Environment.NewLine}", tv.SelectedText);
  144. tv.Copy ();
  145. tv.Selecting = false;
  146. tv.CursorPosition = new Point (2, 4);
  147. tv.Paste ();
  148. Application.Refresh ();
  149. expectedText = @"
  150. TopLevel
  151. Base
  152. Dialog
  153. Menu
  154. ErTopLevel
  155. Base
  156. Dialog
  157. ror ";
  158. TestHelpers.AssertDriverContentsWithFrameAre (expectedText, _output);
  159. expectedColor = @"
  160. 0000000000
  161. 1111000000
  162. 2222220000
  163. 3333000000
  164. 4444444444
  165. 4444000000
  166. 4444440000
  167. 4440000000";
  168. TestHelpers.AssertDriverColorsAre (expectedColor, attributes);
  169. Application.End (rs);
  170. }
  171. [Fact, AutoInitShutdown]
  172. public void RuneCell_LoadRuneCells_Without_ColorScheme_Is_Never_Null ()
  173. {
  174. var cells = new List<RuneCell> {
  175. new RuneCell{Rune = new Rune ('T')},
  176. new RuneCell{Rune = new Rune ('e')},
  177. new RuneCell{Rune = new Rune ('s')},
  178. new RuneCell{Rune = new Rune ('t')}
  179. };
  180. var tv = CreateTextView ();
  181. Application.Top.Add (tv);
  182. tv.Load (cells);
  183. for (int i = 0; i < tv.Lines; i++) {
  184. var line = tv.GetLine (i);
  185. foreach (var rc in line) {
  186. Assert.NotNull (rc.ColorScheme);
  187. }
  188. }
  189. }
  190. [Fact, AutoInitShutdown]
  191. public void RuneCellEventArgs_WordWrap_True ()
  192. {
  193. var eventCount = 0;
  194. var text = new List<List<RuneCell>> () { TextModel.ToRuneCells ("This is the first line.".ToRunes ()), TextModel.ToRuneCells ("This is the second line.".ToRunes ()) };
  195. var tv = CreateTextView ();
  196. tv.DrawNormalColor += _textView_DrawColor;
  197. tv.DrawReadOnlyColor += _textView_DrawColor;
  198. tv.DrawSelectionColor += _textView_DrawColor;
  199. tv.DrawUsedColor += _textView_DrawColor;
  200. void _textView_DrawColor (object sender, RuneCellEventArgs e)
  201. {
  202. Assert.Equal (e.Line [e.Col], text [e.UnwrappedPosition.Row] [e.UnwrappedPosition.Col]);
  203. eventCount++;
  204. }
  205. tv.Text = $"{TextModel.ToString (text [0])}\n{TextModel.ToString (text [1])}\n";
  206. Assert.False (tv.WordWrap);
  207. Application.Top.Add (tv);
  208. Application.Begin (Application.Top);
  209. TestHelpers.AssertDriverContentsWithFrameAre (@"
  210. This is the first line.
  211. This is the second line.", _output);
  212. tv.Width = 10;
  213. tv.Height = 25;
  214. tv.WordWrap = true;
  215. Application.Refresh ();
  216. TestHelpers.AssertDriverContentsWithFrameAre (@"
  217. This is
  218. the
  219. first
  220. line.
  221. This is
  222. the
  223. second
  224. line. ", _output);
  225. Assert.Equal (eventCount, (text [0].Count + text [1].Count) * 2);
  226. }
  227. }
  228. }