HexViewTests.cs 15 KB

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