HexViewTests.cs 15 KB

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