HexViewTests.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. using System.Text;
  2. namespace Terminal.Gui.ViewsTests;
  3. public class HexViewTests
  4. {
  5. [Fact]
  6. public void AllowEdits_Edits_ApplyEdits ()
  7. {
  8. var hv = new HexView (LoadStream (true)) { Width = 20, Height = 20 };
  9. // Needed because HexView relies on LayoutComplete to calc sizes
  10. hv.LayoutSubviews ();
  11. Assert.Empty (hv.Edits);
  12. hv.AllowEdits = false;
  13. Assert.True (hv.NewKeyDownEvent (Key.Home));
  14. Assert.False (hv.NewKeyDownEvent (Key.A));
  15. Assert.Empty (hv.Edits);
  16. Assert.Equal (126, hv.Source.Length);
  17. hv.AllowEdits = true;
  18. Assert.True (hv.NewKeyDownEvent (Key.D4));
  19. Assert.True (hv.NewKeyDownEvent (Key.D1));
  20. Assert.Single (hv.Edits);
  21. Assert.Equal (65, hv.Edits.ToList () [0].Value);
  22. Assert.Equal ('A', (char)hv.Edits.ToList () [0].Value);
  23. Assert.Equal (126, hv.Source.Length);
  24. // Appends byte
  25. Assert.True (hv.NewKeyDownEvent (Key.End));
  26. Assert.True (hv.NewKeyDownEvent (Key.D4));
  27. Assert.True (hv.NewKeyDownEvent (Key.D2));
  28. Assert.Equal (2, hv.Edits.Count);
  29. Assert.Equal (66, hv.Edits.ToList () [1].Value);
  30. Assert.Equal ('B', (char)hv.Edits.ToList () [1].Value);
  31. Assert.Equal (126, hv.Source.Length);
  32. hv.ApplyEdits ();
  33. Assert.Empty (hv.Edits);
  34. Assert.Equal (127, hv.Source.Length);
  35. }
  36. [Fact]
  37. public void ApplyEdits_With_Argument ()
  38. {
  39. byte [] buffer = Encoding.Default.GetBytes ("Fest");
  40. var original = new MemoryStream ();
  41. original.Write (buffer, 0, buffer.Length);
  42. original.Flush ();
  43. var copy = new MemoryStream ();
  44. original.Position = 0;
  45. original.CopyTo (copy);
  46. copy.Flush ();
  47. var hv = new HexView (copy) { Width = Dim.Fill (), Height = Dim.Fill () };
  48. // Needed because HexView relies on LayoutComplete to calc sizes
  49. hv.LayoutSubviews ();
  50. var readBuffer = new byte [hv.Source.Length];
  51. hv.Source.Position = 0;
  52. hv.Source.Read (readBuffer);
  53. Assert.Equal ("Fest", Encoding.Default.GetString (readBuffer));
  54. Assert.True (hv.NewKeyDownEvent (Key.D5));
  55. Assert.True (hv.NewKeyDownEvent (Key.D4));
  56. readBuffer [hv.Edits.ToList () [0].Key] = hv.Edits.ToList () [0].Value;
  57. Assert.Equal ("Test", Encoding.Default.GetString (readBuffer));
  58. hv.ApplyEdits (original);
  59. original.Position = 0;
  60. original.Read (buffer);
  61. copy.Position = 0;
  62. copy.Read (readBuffer);
  63. Assert.Equal ("Test", Encoding.Default.GetString (buffer));
  64. Assert.Equal ("Test", Encoding.Default.GetString (readBuffer));
  65. Assert.Equal (Encoding.Default.GetString (buffer), Encoding.Default.GetString (readBuffer));
  66. }
  67. [Fact]
  68. public void Constructors_Defaults ()
  69. {
  70. var hv = new HexView ();
  71. Assert.NotNull (hv.Source);
  72. Assert.IsAssignableFrom<MemoryStream> (hv.Source);
  73. Assert.True (hv.CanFocus);
  74. Assert.True (hv.AllowEdits);
  75. hv = new HexView (new MemoryStream ());
  76. Assert.NotNull (hv.Source);
  77. Assert.IsAssignableFrom<Stream> (hv.Source);
  78. Assert.True (hv.CanFocus);
  79. Assert.True (hv.AllowEdits);
  80. }
  81. [Fact]
  82. [AutoInitShutdown]
  83. public void CursorPosition_Encoding_Default ()
  84. {
  85. var hv = new HexView (LoadStream ()) { Width = Dim.Fill (), Height = Dim.Fill () };
  86. var top = new Toplevel ();
  87. top.Add (hv);
  88. Application.Begin (top);
  89. Assert.Equal (new Point (1, 1), hv.CursorPosition);
  90. Assert.True (hv.NewKeyDownEvent (Key.Enter));
  91. Assert.True (hv.NewKeyDownEvent (Key.CursorRight.WithCtrl));
  92. Assert.Equal (hv.CursorPosition.X, hv.BytesPerLine);
  93. Assert.True (hv.NewKeyDownEvent (Key.Home));
  94. Assert.True (hv.NewKeyDownEvent (Key.CursorRight));
  95. Assert.Equal (new Point (2, 1), hv.CursorPosition);
  96. Assert.True (hv.NewKeyDownEvent (Key.CursorDown));
  97. Assert.Equal (new Point (2, 2), hv.CursorPosition);
  98. Assert.True (hv.NewKeyDownEvent (Key.End));
  99. int col = hv.CursorPosition.X;
  100. int line = hv.CursorPosition.Y;
  101. int offset = (line - 1) * (hv.BytesPerLine - col);
  102. Assert.Equal (hv.Position, col * line + offset);
  103. }
  104. [Fact]
  105. [AutoInitShutdown]
  106. public void CursorPosition_Encoding_Unicode ()
  107. {
  108. var hv = new HexView (LoadStream (true)) { Width = Dim.Fill (), Height = Dim.Fill () };
  109. var top = new Toplevel ();
  110. top.Add (hv);
  111. Application.Begin (top);
  112. Assert.Equal (new Point (1, 1), hv.CursorPosition);
  113. Assert.True (hv.NewKeyDownEvent (Key.Enter));
  114. Assert.True (hv.NewKeyDownEvent (Key.CursorRight.WithCtrl));
  115. Assert.Equal (hv.CursorPosition.X, hv.BytesPerLine);
  116. Assert.True (hv.NewKeyDownEvent (Key.Home));
  117. Assert.True (hv.NewKeyDownEvent (Key.CursorRight));
  118. Assert.Equal (new Point (2, 1), hv.CursorPosition);
  119. Assert.True (hv.NewKeyDownEvent (Key.CursorDown));
  120. Assert.Equal (new Point (2, 2), hv.CursorPosition);
  121. Assert.True (hv.NewKeyDownEvent (Key.End));
  122. int col = hv.CursorPosition.X;
  123. int line = hv.CursorPosition.Y;
  124. int offset = (line - 1) * (hv.BytesPerLine - col);
  125. Assert.Equal (hv.Position, col * line + offset);
  126. }
  127. [Fact]
  128. public void DiscardEdits_Method ()
  129. {
  130. var hv = new HexView (LoadStream (true)) { Width = 20, Height = 20 };
  131. // Needed because HexView relies on LayoutComplete to calc sizes
  132. hv.LayoutSubviews ();
  133. Assert.True (hv.NewKeyDownEvent (Key.D4));
  134. Assert.True (hv.NewKeyDownEvent (Key.D1));
  135. Assert.Single (hv.Edits);
  136. Assert.Equal (65, hv.Edits.ToList () [0].Value);
  137. Assert.Equal ('A', (char)hv.Edits.ToList () [0].Value);
  138. Assert.Equal (126, hv.Source.Length);
  139. hv.DiscardEdits ();
  140. Assert.Empty (hv.Edits);
  141. }
  142. [Fact]
  143. public void DisplayStart_Source ()
  144. {
  145. var hv = new HexView (LoadStream (true)) { Width = 20, Height = 20 };
  146. // Needed because HexView relies on LayoutComplete to calc sizes
  147. hv.LayoutSubviews ();
  148. Assert.Equal (0, hv.DisplayStart);
  149. Assert.True (hv.NewKeyDownEvent (Key.PageDown));
  150. Assert.Equal (4 * hv.Frame.Height, hv.DisplayStart);
  151. Assert.Equal (hv.Source.Length, hv.Source.Position);
  152. Assert.True (hv.NewKeyDownEvent (Key.End));
  153. // already on last page and so the DisplayStart is the same as before
  154. Assert.Equal (4 * hv.Frame.Height, hv.DisplayStart);
  155. Assert.Equal (hv.Source.Length, hv.Source.Position);
  156. }
  157. [Fact]
  158. public void Edited_Event ()
  159. {
  160. var hv = new HexView (LoadStream (true)) { Width = 20, Height = 20 };
  161. // Needed because HexView relies on LayoutComplete to calc sizes
  162. hv.LayoutSubviews ();
  163. KeyValuePair<long, byte> keyValuePair = default;
  164. hv.Edited += (s, e) => keyValuePair = new KeyValuePair<long, byte> (e.Position, e.NewValue);
  165. Assert.True (hv.NewKeyDownEvent (Key.D4));
  166. Assert.True (hv.NewKeyDownEvent (Key.D6));
  167. Assert.Equal (0, (int)keyValuePair.Key);
  168. Assert.Equal (70, keyValuePair.Value);
  169. Assert.Equal ('F', (char)keyValuePair.Value);
  170. }
  171. [Fact]
  172. public void Exceptions_Tests ()
  173. {
  174. Assert.Throws<ArgumentNullException> (() => new HexView (null));
  175. Assert.Throws<ArgumentException> (() => new HexView (new NonSeekableStream (new MemoryStream ())));
  176. }
  177. [Fact]
  178. [AutoInitShutdown]
  179. public void KeyBindings_Command ()
  180. {
  181. var hv = new HexView (LoadStream ()) { Width = 20, Height = 10 };
  182. var top = new Toplevel ();
  183. top.Add (hv);
  184. Application.Begin (top);
  185. Assert.Equal (63, hv.Source.Length);
  186. Assert.Equal (1, hv.Position);
  187. Assert.Equal (4, hv.BytesPerLine);
  188. // right side only needed to press one time
  189. Assert.True (hv.NewKeyDownEvent (Key.Enter));
  190. Assert.True (hv.NewKeyDownEvent (Key.CursorRight));
  191. Assert.Equal (2, hv.Position);
  192. Assert.True (hv.NewKeyDownEvent (Key.CursorLeft));
  193. Assert.Equal (1, hv.Position);
  194. Assert.True (hv.NewKeyDownEvent (Key.CursorDown));
  195. Assert.Equal (5, hv.Position);
  196. Assert.True (hv.NewKeyDownEvent (Key.CursorUp));
  197. Assert.Equal (1, hv.Position);
  198. Assert.True (hv.NewKeyDownEvent (Key.V.WithCtrl));
  199. Assert.Equal (41, hv.Position);
  200. Assert.True (hv.NewKeyDownEvent (new Key (Key.V.WithAlt)));
  201. Assert.Equal (1, hv.Position);
  202. Assert.True (hv.NewKeyDownEvent (Key.PageDown));
  203. Assert.Equal (41, hv.Position);
  204. Assert.True (hv.NewKeyDownEvent (Key.PageUp));
  205. Assert.Equal (1, hv.Position);
  206. Assert.True (hv.NewKeyDownEvent (Key.End));
  207. Assert.Equal (64, hv.Position);
  208. Assert.True (hv.NewKeyDownEvent (Key.Home));
  209. Assert.Equal (1, hv.Position);
  210. Assert.True (hv.NewKeyDownEvent (Key.CursorRight.WithCtrl));
  211. Assert.Equal (4, hv.Position);
  212. Assert.True (hv.NewKeyDownEvent (Key.CursorLeft.WithCtrl));
  213. Assert.Equal (1, hv.Position);
  214. Assert.True (hv.NewKeyDownEvent (Key.CursorDown.WithCtrl));
  215. Assert.Equal (37, hv.Position);
  216. Assert.True (hv.NewKeyDownEvent (Key.CursorUp.WithCtrl));
  217. Assert.Equal (1, hv.Position);
  218. }
  219. [Fact]
  220. public void Position_Using_Encoding_Default ()
  221. {
  222. var hv = new HexView (LoadStream ()) { Width = 20, Height = 20 };
  223. // Needed because HexView relies on LayoutComplete to calc sizes
  224. hv.LayoutSubviews ();
  225. Assert.Equal (63, hv.Source.Length);
  226. Assert.Equal (63, hv.Source.Position);
  227. Assert.Equal (1, hv.Position);
  228. // left side needed to press twice
  229. Assert.True (hv.NewKeyDownEvent (Key.CursorRight));
  230. Assert.Equal (63, hv.Source.Position);
  231. Assert.Equal (1, hv.Position);
  232. Assert.True (hv.NewKeyDownEvent (Key.CursorRight));
  233. Assert.Equal (63, hv.Source.Position);
  234. Assert.Equal (2, hv.Position);
  235. // right side only needed to press one time
  236. Assert.True (hv.NewKeyDownEvent (Key.Enter));
  237. Assert.Equal (63, hv.Source.Position);
  238. Assert.Equal (2, hv.Position);
  239. Assert.True (hv.NewKeyDownEvent (Key.CursorLeft));
  240. Assert.Equal (63, hv.Source.Position);
  241. Assert.Equal (1, hv.Position);
  242. // last position is equal to the source length
  243. Assert.True (hv.NewKeyDownEvent (Key.End));
  244. Assert.Equal (63, hv.Source.Position);
  245. Assert.Equal (64, hv.Position);
  246. Assert.Equal (hv.Position - 1, hv.Source.Length);
  247. }
  248. [Fact]
  249. public void Position_Using_Encoding_Unicode ()
  250. {
  251. var hv = new HexView (LoadStream (true)) { Width = 20, Height = 20 };
  252. // Needed because HexView relies on LayoutComplete to calc sizes
  253. hv.LayoutSubviews ();
  254. Assert.Equal (126, hv.Source.Length);
  255. Assert.Equal (126, hv.Source.Position);
  256. Assert.Equal (1, hv.Position);
  257. // left side needed to press twice
  258. Assert.True (hv.NewKeyDownEvent (Key.CursorRight));
  259. Assert.Equal (126, hv.Source.Position);
  260. Assert.Equal (1, hv.Position);
  261. Assert.True (hv.NewKeyDownEvent (Key.CursorRight));
  262. Assert.Equal (126, hv.Source.Position);
  263. Assert.Equal (2, hv.Position);
  264. // right side only needed to press one time
  265. Assert.True (hv.NewKeyDownEvent (Key.Enter));
  266. Assert.Equal (126, hv.Source.Position);
  267. Assert.Equal (2, hv.Position);
  268. Assert.True (hv.NewKeyDownEvent (Key.CursorLeft));
  269. Assert.Equal (126, hv.Source.Position);
  270. Assert.Equal (1, hv.Position);
  271. // last position is equal to the source length
  272. Assert.True (hv.NewKeyDownEvent (Key.End));
  273. Assert.Equal (126, hv.Source.Position);
  274. Assert.Equal (127, hv.Position);
  275. Assert.Equal (hv.Position - 1, hv.Source.Length);
  276. }
  277. [Fact]
  278. [AutoInitShutdown]
  279. public void PositionChanged_Event ()
  280. {
  281. var hv = new HexView (LoadStream ()) { Width = Dim.Fill (), Height = Dim.Fill () };
  282. HexViewEventArgs hexViewEventArgs = null;
  283. hv.PositionChanged += (s, e) => hexViewEventArgs = e;
  284. var top = new Toplevel ();
  285. top.Add (hv);
  286. Application.Begin (top);
  287. Assert.True (hv.NewKeyDownEvent (Key.CursorRight)); // left side must press twice
  288. Assert.True (hv.NewKeyDownEvent (Key.CursorRight));
  289. Assert.True (hv.NewKeyDownEvent (Key.CursorDown));
  290. Assert.Equal (12, hexViewEventArgs.BytesPerLine);
  291. Assert.Equal (new Point (2, 2), hexViewEventArgs.CursorPosition);
  292. Assert.Equal (14, hexViewEventArgs.Position);
  293. }
  294. [Fact]
  295. [AutoInitShutdown]
  296. public void Source_Sets_DisplayStart_And_Position_To_Zero_If_Greater_Than_Source_Length ()
  297. {
  298. var hv = new HexView (LoadStream ()) { Width = 10, Height = 5 };
  299. var top = new Toplevel ();
  300. top.Add (hv);
  301. Application.Begin (top);
  302. Assert.True (hv.NewKeyDownEvent (Key.End));
  303. Assert.Equal (62, hv.DisplayStart);
  304. Assert.Equal (64, hv.Position);
  305. hv.Source = new MemoryStream ();
  306. Assert.Equal (0, hv.DisplayStart);
  307. Assert.Equal (0, hv.Position - 1);
  308. hv.Source = LoadStream ();
  309. hv.Width = Dim.Fill ();
  310. hv.Height = Dim.Fill ();
  311. top.LayoutSubviews ();
  312. Assert.Equal (0, hv.DisplayStart);
  313. Assert.Equal (0, hv.Position - 1);
  314. Assert.True (hv.NewKeyDownEvent (Key.End));
  315. Assert.Equal (0, hv.DisplayStart);
  316. Assert.Equal (64, hv.Position);
  317. hv.Source = new MemoryStream ();
  318. Assert.Equal (0, hv.DisplayStart);
  319. Assert.Equal (0, hv.Position - 1);
  320. }
  321. private Stream LoadStream (bool unicode = false)
  322. {
  323. var stream = new MemoryStream ();
  324. byte [] bArray;
  325. var memString = "Hello world.\nThis is a test of the Emergency Broadcast System.\n";
  326. Assert.Equal (63, memString.Length);
  327. if (unicode)
  328. {
  329. bArray = Encoding.Unicode.GetBytes (memString);
  330. Assert.Equal (126, bArray.Length);
  331. }
  332. else
  333. {
  334. bArray = Encoding.Default.GetBytes (memString);
  335. Assert.Equal (63, bArray.Length);
  336. }
  337. stream.Write (bArray);
  338. return stream;
  339. }
  340. private class NonSeekableStream : Stream
  341. {
  342. private readonly Stream m_stream;
  343. public NonSeekableStream (Stream baseStream) { m_stream = baseStream; }
  344. public override bool CanRead => m_stream.CanRead;
  345. public override bool CanSeek => false;
  346. public override bool CanWrite => m_stream.CanWrite;
  347. public override long Length => throw new NotSupportedException ();
  348. public override long Position
  349. {
  350. get => m_stream.Position;
  351. set => throw new NotSupportedException ();
  352. }
  353. public override void Flush () { m_stream.Flush (); }
  354. public override int Read (byte [] buffer, int offset, int count) { return m_stream.Read (buffer, offset, count); }
  355. public override long Seek (long offset, SeekOrigin origin) { throw new NotImplementedException (); }
  356. public override void SetLength (long value) { throw new NotSupportedException (); }
  357. public override void Write (byte [] buffer, int offset, int count) { m_stream.Write (buffer, offset, count); }
  358. }
  359. }