HexViewTests.cs 14 KB

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