HexViewTests.cs 15 KB

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