HexViewTests.cs 14 KB

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