HexViewTests.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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 (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 (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 (2, 1), hv.CursorPosition);
  96. Assert.True (hv.NewKeyDownEvent (Key.CursorDown));
  97. Assert.Equal (new (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. top.Dispose ();
  104. }
  105. [Fact]
  106. [AutoInitShutdown]
  107. public void CursorPosition_Encoding_Unicode ()
  108. {
  109. var hv = new HexView (LoadStream (true)) { Width = Dim.Fill (), Height = Dim.Fill () };
  110. var top = new Toplevel ();
  111. top.Add (hv);
  112. Application.Begin (top);
  113. Assert.Equal (new (1, 1), hv.CursorPosition);
  114. Assert.True (hv.NewKeyDownEvent (Key.Enter));
  115. Assert.True (hv.NewKeyDownEvent (Key.CursorRight.WithCtrl));
  116. Assert.Equal (hv.CursorPosition.X, hv.BytesPerLine);
  117. Assert.True (hv.NewKeyDownEvent (Key.Home));
  118. Assert.True (hv.NewKeyDownEvent (Key.CursorRight));
  119. Assert.Equal (new (2, 1), hv.CursorPosition);
  120. Assert.True (hv.NewKeyDownEvent (Key.CursorDown));
  121. Assert.Equal (new (2, 2), hv.CursorPosition);
  122. Assert.True (hv.NewKeyDownEvent (Key.End));
  123. int col = hv.CursorPosition.X;
  124. int line = hv.CursorPosition.Y;
  125. int offset = (line - 1) * (hv.BytesPerLine - col);
  126. Assert.Equal (hv.Position, col * line + offset);
  127. top.Dispose ();
  128. }
  129. [Fact]
  130. public void DiscardEdits_Method ()
  131. {
  132. var hv = new HexView (LoadStream (true)) { Width = 20, Height = 20 };
  133. // Needed because HexView relies on LayoutComplete to calc sizes
  134. hv.LayoutSubviews ();
  135. Assert.True (hv.NewKeyDownEvent (Key.D4));
  136. Assert.True (hv.NewKeyDownEvent (Key.D1));
  137. Assert.Single (hv.Edits);
  138. Assert.Equal (65, hv.Edits.ToList () [0].Value);
  139. Assert.Equal ('A', (char)hv.Edits.ToList () [0].Value);
  140. Assert.Equal (126, hv.Source.Length);
  141. hv.DiscardEdits ();
  142. Assert.Empty (hv.Edits);
  143. }
  144. [Fact]
  145. public void DisplayStart_Source ()
  146. {
  147. var hv = new HexView (LoadStream (true)) { Width = 20, Height = 20 };
  148. // Needed because HexView relies on LayoutComplete to calc sizes
  149. hv.LayoutSubviews ();
  150. Assert.Equal (0, hv.DisplayStart);
  151. Assert.True (hv.NewKeyDownEvent (Key.PageDown));
  152. Assert.Equal (4 * hv.Frame.Height, hv.DisplayStart);
  153. Assert.Equal (hv.Source.Length, hv.Source.Position);
  154. Assert.True (hv.NewKeyDownEvent (Key.End));
  155. // already on last page and so the DisplayStart is the same as before
  156. Assert.Equal (4 * hv.Frame.Height, hv.DisplayStart);
  157. Assert.Equal (hv.Source.Length, hv.Source.Position);
  158. }
  159. [Fact]
  160. public void Edited_Event ()
  161. {
  162. var hv = new HexView (LoadStream (true)) { Width = 20, Height = 20 };
  163. // Needed because HexView relies on LayoutComplete to calc sizes
  164. hv.LayoutSubviews ();
  165. KeyValuePair<long, byte> keyValuePair = default;
  166. hv.Edited += (s, e) => keyValuePair = new (e.Position, e.NewValue);
  167. Assert.True (hv.NewKeyDownEvent (Key.D4));
  168. Assert.True (hv.NewKeyDownEvent (Key.D6));
  169. Assert.Equal (0, (int)keyValuePair.Key);
  170. Assert.Equal (70, keyValuePair.Value);
  171. Assert.Equal ('F', (char)keyValuePair.Value);
  172. }
  173. [Fact]
  174. public void Exceptions_Tests ()
  175. {
  176. Assert.Throws<ArgumentNullException> (() => new HexView (null));
  177. Assert.Throws<ArgumentException> (() => new HexView (new NonSeekableStream (new MemoryStream ())));
  178. }
  179. [Fact]
  180. [AutoInitShutdown]
  181. public void KeyBindings_Command ()
  182. {
  183. var hv = new HexView (LoadStream ()) { Width = 20, Height = 10 };
  184. var top = new Toplevel ();
  185. top.Add (hv);
  186. Application.Begin (top);
  187. Assert.Equal (63, hv.Source.Length);
  188. Assert.Equal (1, hv.Position);
  189. Assert.Equal (4, hv.BytesPerLine);
  190. // right side only needed to press one time
  191. Assert.True (hv.NewKeyDownEvent (Key.Enter));
  192. Assert.True (hv.NewKeyDownEvent (Key.CursorRight));
  193. Assert.Equal (2, hv.Position);
  194. Assert.True (hv.NewKeyDownEvent (Key.CursorLeft));
  195. Assert.Equal (1, hv.Position);
  196. Assert.True (hv.NewKeyDownEvent (Key.CursorDown));
  197. Assert.Equal (5, hv.Position);
  198. Assert.True (hv.NewKeyDownEvent (Key.CursorUp));
  199. Assert.Equal (1, hv.Position);
  200. Assert.True (hv.NewKeyDownEvent (Key.V.WithCtrl));
  201. Assert.Equal (41, hv.Position);
  202. Assert.True (hv.NewKeyDownEvent (new (Key.V.WithAlt)));
  203. Assert.Equal (1, hv.Position);
  204. Assert.True (hv.NewKeyDownEvent (Key.PageDown));
  205. Assert.Equal (41, hv.Position);
  206. Assert.True (hv.NewKeyDownEvent (Key.PageUp));
  207. Assert.Equal (1, hv.Position);
  208. Assert.True (hv.NewKeyDownEvent (Key.End));
  209. Assert.Equal (64, hv.Position);
  210. Assert.True (hv.NewKeyDownEvent (Key.Home));
  211. Assert.Equal (1, hv.Position);
  212. Assert.True (hv.NewKeyDownEvent (Key.CursorRight.WithCtrl));
  213. Assert.Equal (4, hv.Position);
  214. Assert.True (hv.NewKeyDownEvent (Key.CursorLeft.WithCtrl));
  215. Assert.Equal (1, hv.Position);
  216. Assert.True (hv.NewKeyDownEvent (Key.CursorDown.WithCtrl));
  217. Assert.Equal (37, hv.Position);
  218. Assert.True (hv.NewKeyDownEvent (Key.CursorUp.WithCtrl));
  219. Assert.Equal (1, hv.Position);
  220. top.Dispose ();
  221. }
  222. [Fact]
  223. public void Position_Using_Encoding_Default ()
  224. {
  225. var hv = new HexView (LoadStream ()) { Width = 20, Height = 20 };
  226. // Needed because HexView relies on LayoutComplete to calc sizes
  227. hv.LayoutSubviews ();
  228. Assert.Equal (63, hv.Source.Length);
  229. Assert.Equal (63, hv.Source.Position);
  230. Assert.Equal (1, hv.Position);
  231. // left side needed to press twice
  232. Assert.True (hv.NewKeyDownEvent (Key.CursorRight));
  233. Assert.Equal (63, hv.Source.Position);
  234. Assert.Equal (1, hv.Position);
  235. Assert.True (hv.NewKeyDownEvent (Key.CursorRight));
  236. Assert.Equal (63, hv.Source.Position);
  237. Assert.Equal (2, hv.Position);
  238. // right side only needed to press one time
  239. Assert.True (hv.NewKeyDownEvent (Key.Enter));
  240. Assert.Equal (63, hv.Source.Position);
  241. Assert.Equal (2, hv.Position);
  242. Assert.True (hv.NewKeyDownEvent (Key.CursorLeft));
  243. Assert.Equal (63, hv.Source.Position);
  244. Assert.Equal (1, hv.Position);
  245. // last position is equal to the source length
  246. Assert.True (hv.NewKeyDownEvent (Key.End));
  247. Assert.Equal (63, hv.Source.Position);
  248. Assert.Equal (64, hv.Position);
  249. Assert.Equal (hv.Position - 1, hv.Source.Length);
  250. }
  251. [Fact]
  252. public void Position_Using_Encoding_Unicode ()
  253. {
  254. var hv = new HexView (LoadStream (true)) { Width = 20, Height = 20 };
  255. // Needed because HexView relies on LayoutComplete to calc sizes
  256. hv.LayoutSubviews ();
  257. Assert.Equal (126, hv.Source.Length);
  258. Assert.Equal (126, hv.Source.Position);
  259. Assert.Equal (1, hv.Position);
  260. // left side needed to press twice
  261. Assert.True (hv.NewKeyDownEvent (Key.CursorRight));
  262. Assert.Equal (126, hv.Source.Position);
  263. Assert.Equal (1, hv.Position);
  264. Assert.True (hv.NewKeyDownEvent (Key.CursorRight));
  265. Assert.Equal (126, hv.Source.Position);
  266. Assert.Equal (2, hv.Position);
  267. // right side only needed to press one time
  268. Assert.True (hv.NewKeyDownEvent (Key.Enter));
  269. Assert.Equal (126, hv.Source.Position);
  270. Assert.Equal (2, hv.Position);
  271. Assert.True (hv.NewKeyDownEvent (Key.CursorLeft));
  272. Assert.Equal (126, hv.Source.Position);
  273. Assert.Equal (1, hv.Position);
  274. // last position is equal to the source length
  275. Assert.True (hv.NewKeyDownEvent (Key.End));
  276. Assert.Equal (126, hv.Source.Position);
  277. Assert.Equal (127, hv.Position);
  278. Assert.Equal (hv.Position - 1, hv.Source.Length);
  279. }
  280. [Fact]
  281. [AutoInitShutdown]
  282. public void PositionChanged_Event ()
  283. {
  284. var hv = new HexView (LoadStream ()) { Width = Dim.Fill (), Height = Dim.Fill () };
  285. HexViewEventArgs hexViewEventArgs = null;
  286. hv.PositionChanged += (s, e) => hexViewEventArgs = e;
  287. var top = new Toplevel ();
  288. top.Add (hv);
  289. Application.Begin (top);
  290. Assert.True (hv.NewKeyDownEvent (Key.CursorRight)); // left side must press twice
  291. Assert.True (hv.NewKeyDownEvent (Key.CursorRight));
  292. Assert.True (hv.NewKeyDownEvent (Key.CursorDown));
  293. Assert.Equal (12, hexViewEventArgs.BytesPerLine);
  294. Assert.Equal (new (2, 2), hexViewEventArgs.CursorPosition);
  295. Assert.Equal (14, hexViewEventArgs.Position);
  296. top.Dispose ();
  297. }
  298. [Fact]
  299. [AutoInitShutdown]
  300. public void Source_Sets_DisplayStart_And_Position_To_Zero_If_Greater_Than_Source_Length ()
  301. {
  302. var hv = new HexView (LoadStream ()) { Width = 10, Height = 5 };
  303. var top = new Toplevel ();
  304. top.Add (hv);
  305. Application.Begin (top);
  306. Assert.True (hv.NewKeyDownEvent (Key.End));
  307. Assert.Equal (62, hv.DisplayStart);
  308. Assert.Equal (64, hv.Position);
  309. hv.Source = new MemoryStream ();
  310. Assert.Equal (0, hv.DisplayStart);
  311. Assert.Equal (0, hv.Position - 1);
  312. hv.Source = LoadStream ();
  313. hv.Width = Dim.Fill ();
  314. hv.Height = Dim.Fill ();
  315. top.LayoutSubviews ();
  316. Assert.Equal (0, hv.DisplayStart);
  317. Assert.Equal (0, hv.Position - 1);
  318. Assert.True (hv.NewKeyDownEvent (Key.End));
  319. Assert.Equal (0, hv.DisplayStart);
  320. Assert.Equal (64, hv.Position);
  321. hv.Source = new MemoryStream ();
  322. Assert.Equal (0, hv.DisplayStart);
  323. Assert.Equal (0, hv.Position - 1);
  324. top.Dispose ();
  325. }
  326. private Stream LoadStream (bool unicode = false)
  327. {
  328. var stream = new MemoryStream ();
  329. byte [] bArray;
  330. var memString = "Hello world.\nThis is a test of the Emergency Broadcast System.\n";
  331. Assert.Equal (63, memString.Length);
  332. if (unicode)
  333. {
  334. bArray = Encoding.Unicode.GetBytes (memString);
  335. Assert.Equal (126, bArray.Length);
  336. }
  337. else
  338. {
  339. bArray = Encoding.Default.GetBytes (memString);
  340. Assert.Equal (63, bArray.Length);
  341. }
  342. stream.Write (bArray);
  343. return stream;
  344. }
  345. private class NonSeekableStream : Stream
  346. {
  347. private readonly Stream m_stream;
  348. public NonSeekableStream (Stream baseStream) { m_stream = baseStream; }
  349. public override bool CanRead => m_stream.CanRead;
  350. public override bool CanSeek => false;
  351. public override bool CanWrite => m_stream.CanWrite;
  352. public override long Length => throw new NotSupportedException ();
  353. public override long Position
  354. {
  355. get => m_stream.Position;
  356. set => throw new NotSupportedException ();
  357. }
  358. public override void Flush () { m_stream.Flush (); }
  359. public override int Read (byte [] buffer, int offset, int count) { return m_stream.Read (buffer, offset, count); }
  360. public override long Seek (long offset, SeekOrigin origin) { throw new NotImplementedException (); }
  361. public override void SetLength (long value) { throw new NotSupportedException (); }
  362. public override void Write (byte [] buffer, int offset, int count) { m_stream.Write (buffer, offset, count); }
  363. }
  364. }