HexViewTests.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. #nullable enable
  2. using System.Text;
  3. using UnitTests;
  4. namespace ViewsTests;
  5. public class HexViewTests : FakeDriverBase
  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 ReadOnly_Prevents_Edits ()
  29. {
  30. var hv = new HexView (LoadStream (null, out _, true)) { Width = 20, Height = 20 };
  31. IApplication app = Application.Create ();
  32. Runnable<bool> runnable = new ();
  33. app.Begin (runnable);
  34. runnable.Add (hv);
  35. // Needed because HexView relies on LayoutComplete to calc sizes
  36. hv.LayoutSubViews ();
  37. Assert.True (app.Keyboard.RaiseKeyDownEvent (Key.Tab)); // Move to left side
  38. Assert.Empty (hv.Edits);
  39. hv.ReadOnly = true;
  40. Assert.True (app.Keyboard.RaiseKeyDownEvent (Key.Home));
  41. Assert.False (app.Keyboard.RaiseKeyDownEvent (Key.A));
  42. Assert.Empty (hv.Edits);
  43. Assert.Equal (126, hv.Source!.Length);
  44. hv.ReadOnly = false;
  45. Assert.True (app.Keyboard.RaiseKeyDownEvent (Key.D4));
  46. Assert.True (app.Keyboard.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 (app.Keyboard.RaiseKeyDownEvent (Key.End));
  53. Assert.True (app.Keyboard.RaiseKeyDownEvent (Key.D4));
  54. Assert.True (app.Keyboard.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. }
  63. [Fact]
  64. public void ApplyEdits_With_Argument ()
  65. {
  66. IApplication app = Application.Create ();
  67. Runnable<bool> runnable = new ();
  68. app.Begin (runnable);
  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. runnable.Add (hv);
  79. // Needed because HexView relies on LayoutComplete to calc sizes
  80. hv.LayoutSubViews ();
  81. var readBuffer = new byte [hv.Source!.Length];
  82. hv.Source.Position = 0;
  83. hv.Source.Read (readBuffer);
  84. Assert.Equal ("Fest", Encoding.Default.GetString (readBuffer));
  85. Assert.True (app.Keyboard.RaiseKeyDownEvent (Key.Tab)); // Move to left side
  86. Assert.True (app.Keyboard.RaiseKeyDownEvent (Key.D5));
  87. Assert.True (app.Keyboard.RaiseKeyDownEvent (Key.D4));
  88. readBuffer [hv.Edits.ToList () [0].Key] = hv.Edits.ToList () [0].Value;
  89. Assert.Equal ("Test", Encoding.Default.GetString (readBuffer));
  90. Assert.True (app.Keyboard.RaiseKeyDownEvent (Key.Tab)); // Move to right side
  91. Assert.True (app.Keyboard.RaiseKeyDownEvent (Key.CursorLeft));
  92. Assert.True (app.Keyboard.RaiseKeyDownEvent (Key.Z.WithShift));
  93. readBuffer [hv.Edits.ToList () [0].Key] = hv.Edits.ToList () [0].Value;
  94. Assert.Equal ("Zest", Encoding.Default.GetString (readBuffer));
  95. hv.ApplyEdits (original);
  96. original.Position = 0;
  97. original.Read (buffer);
  98. copy.Position = 0;
  99. copy.Read (readBuffer);
  100. Assert.Equal ("Zest", Encoding.Default.GetString (buffer));
  101. Assert.Equal ("Zest", Encoding.Default.GetString (readBuffer));
  102. Assert.Equal (Encoding.Default.GetString (buffer), Encoding.Default.GetString (readBuffer));
  103. }
  104. [Fact]
  105. public void Constructors_Defaults ()
  106. {
  107. var hv = new HexView ();
  108. Assert.NotNull (hv.Source);
  109. Assert.IsAssignableFrom<MemoryStream> (hv.Source);
  110. Assert.True (hv.CanFocus);
  111. Assert.True (!hv.ReadOnly);
  112. hv = new (new MemoryStream ());
  113. Assert.NotNull (hv.Source);
  114. Assert.IsAssignableFrom<Stream> (hv.Source);
  115. Assert.True (hv.CanFocus);
  116. Assert.True (!hv.ReadOnly);
  117. }
  118. [Fact]
  119. public void Position_Encoding_Default ()
  120. {
  121. var hv = new HexView (LoadStream (null, out _)) { Width = 100, Height = 100 };
  122. IApplication app = Application.Create ();
  123. Runnable<bool> runnable = new ();
  124. app.Begin (runnable);
  125. runnable.Add (hv);
  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 (app.Keyboard.RaiseKeyDownEvent (Key.Tab));
  130. Assert.Equal (new (0, 0), hv.GetPosition (hv.Address));
  131. Assert.True (app.Keyboard.RaiseKeyDownEvent (Key.CursorRight.WithCtrl));
  132. Assert.Equal (hv.BytesPerLine - 1, hv.GetPosition (hv.Address).X);
  133. Assert.True (app.Keyboard.RaiseKeyDownEvent (Key.Home));
  134. Assert.True (app.Keyboard.RaiseKeyDownEvent (Key.CursorRight));
  135. Assert.True (app.Keyboard.RaiseKeyDownEvent (Key.CursorRight));
  136. Assert.Equal (new (1, 0), hv.GetPosition (hv.Address));
  137. Assert.True (app.Keyboard.RaiseKeyDownEvent (Key.CursorDown));
  138. Assert.Equal (new (1, 1), hv.GetPosition (hv.Address));
  139. Assert.True (app.Keyboard.RaiseKeyDownEvent (Key.End));
  140. Assert.Equal (new (3, 3), hv.GetPosition (hv.Address));
  141. Assert.Equal (hv.Source!.Length, hv.Address);
  142. }
  143. [Fact]
  144. public void Position_Encoding_Unicode ()
  145. {
  146. var hv = new HexView (LoadStream (null, out _, true)) { Width = 100, Height = 100 };
  147. IApplication app = Application.Create ();
  148. Runnable<bool> runnable = new ();
  149. app.Begin (runnable);
  150. runnable.Add (hv);
  151. app.LayoutAndDraw ();
  152. Assert.Equal (126, hv.Source!.Length);
  153. Assert.Equal (20, hv.BytesPerLine);
  154. Assert.Equal (new (0, 0), hv.GetPosition (hv.Address));
  155. Assert.True (app.Keyboard.RaiseKeyDownEvent (Key.Tab));
  156. Assert.True (app.Keyboard.RaiseKeyDownEvent (Key.CursorRight.WithCtrl));
  157. Assert.Equal (hv.BytesPerLine - 1, hv.GetPosition (hv.Address).X);
  158. Assert.True (app.Keyboard.RaiseKeyDownEvent (Key.Home));
  159. Assert.True (app.Keyboard.RaiseKeyDownEvent (Key.CursorRight));
  160. Assert.True (app.Keyboard.RaiseKeyDownEvent (Key.CursorRight));
  161. Assert.Equal (new (1, 0), hv.GetPosition (hv.Address));
  162. Assert.True (app.Keyboard.RaiseKeyDownEvent (Key.CursorDown));
  163. Assert.Equal (new (1, 1), hv.GetPosition (hv.Address));
  164. Assert.True (app.Keyboard.RaiseKeyDownEvent (Key.End));
  165. Assert.Equal (new (6, 6), hv.GetPosition (hv.Address));
  166. Assert.Equal (hv.Source!.Length, hv.Address);
  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. var hv = new HexView (LoadStream (null, out _)) { Width = 20, Height = 10 };
  207. IApplication app = Application.Create ();
  208. Runnable<bool> runnable = new ();
  209. app.Begin (runnable);
  210. runnable.Add (hv);
  211. app.LayoutAndDraw ();
  212. Assert.Equal (MEM_STRING_LENGTH, hv.Source!.Length);
  213. Assert.Equal (0, hv.Address);
  214. Assert.Equal (4, hv.BytesPerLine);
  215. // Default internal focus is on right side. Move back to left.
  216. Assert.True (app.Keyboard.RaiseKeyDownEvent (Key.Tab.WithShift));
  217. Assert.True (app.Keyboard.RaiseKeyDownEvent (Key.CursorRight));
  218. Assert.Equal (0, hv.Address);
  219. Assert.True (app.Keyboard.RaiseKeyDownEvent (Key.CursorRight));
  220. Assert.Equal (1, hv.Address);
  221. Assert.True (app.Keyboard.RaiseKeyDownEvent (Key.CursorLeft));
  222. Assert.Equal (0, hv.Address);
  223. Assert.True (app.Keyboard.RaiseKeyDownEvent (Key.CursorDown));
  224. Assert.Equal (4, hv.Address);
  225. Assert.True (app.Keyboard.RaiseKeyDownEvent (Key.CursorUp));
  226. Assert.Equal (0, hv.Address);
  227. Assert.True (app.Keyboard.RaiseKeyDownEvent (Key.PageDown));
  228. Assert.Equal (40, hv.Address);
  229. Assert.True (app.Keyboard.RaiseKeyDownEvent (Key.PageUp));
  230. Assert.Equal (0, hv.Address);
  231. Assert.True (app.Keyboard.RaiseKeyDownEvent (Key.End));
  232. Assert.Equal (MEM_STRING_LENGTH, hv.Address);
  233. Assert.True (app.Keyboard.RaiseKeyDownEvent (Key.Home));
  234. Assert.Equal (0, hv.Address);
  235. Assert.True (app.Keyboard.RaiseKeyDownEvent (Key.CursorRight.WithCtrl));
  236. Assert.Equal (3, hv.Address);
  237. Assert.True (app.Keyboard.RaiseKeyDownEvent (Key.CursorLeft.WithCtrl));
  238. Assert.Equal (0, hv.Address);
  239. Assert.True (app.Keyboard.RaiseKeyDownEvent (Key.CursorDown.WithCtrl));
  240. Assert.Equal (36, hv.Address);
  241. Assert.True (app.Keyboard.RaiseKeyDownEvent (Key.CursorUp.WithCtrl));
  242. Assert.Equal (0, hv.Address);
  243. }
  244. [Fact]
  245. public void PositionChanged_Event ()
  246. {
  247. var hv = new HexView (LoadStream (null, out _)) { Width = 20, Height = 10 };
  248. hv.Layout ();
  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. }
  259. [Fact]
  260. public void Source_Sets_Address_To_Zero_If_Greater_Than_Source_Length ()
  261. {
  262. var hv = new HexView (LoadStream (null, out _)) { Width = 10, Height = 5 };
  263. IApplication app = Application.Create ();
  264. Runnable<bool> runnable = new ();
  265. app.Begin (runnable);
  266. runnable.Add (hv);
  267. Assert.True (hv.NewKeyDownEvent (Key.End));
  268. Assert.Equal (MEM_STRING_LENGTH, hv.Address);
  269. hv.Source = new MemoryStream ();
  270. runnable.Layout ();
  271. Assert.Equal (0, hv.Address);
  272. hv.Source = LoadStream (null, out _);
  273. hv.Width = Dim.Fill ();
  274. hv.Height = Dim.Fill ();
  275. runnable.Layout ();
  276. Assert.Equal (0, hv.Address);
  277. Assert.True (hv.NewKeyDownEvent (Key.End));
  278. Assert.Equal (MEM_STRING_LENGTH, hv.Address);
  279. hv.Source = new MemoryStream ();
  280. runnable.Layout ();
  281. Assert.Equal (0, hv.Address);
  282. }
  283. private const string MEM_STRING = "Hello world.\nThis is a test of the Emergency Broadcast System.\n";
  284. private const int MEM_STRING_LENGTH = 63;
  285. private Stream LoadStream (string? memString, out long numBytesInMemString, bool unicode = false)
  286. {
  287. var stream = new MemoryStream ();
  288. byte [] bArray;
  289. Assert.Equal (MEM_STRING_LENGTH, MEM_STRING.Length);
  290. if (memString is null)
  291. {
  292. memString = MEM_STRING;
  293. }
  294. if (unicode)
  295. {
  296. bArray = Encoding.Unicode.GetBytes (memString);
  297. }
  298. else
  299. {
  300. bArray = Encoding.Default.GetBytes (memString);
  301. }
  302. numBytesInMemString = bArray.Length;
  303. stream.Write (bArray);
  304. return stream;
  305. }
  306. private class NonSeekableStream (Stream baseStream) : Stream
  307. {
  308. public override bool CanRead => baseStream.CanRead;
  309. public override bool CanSeek => false;
  310. public override bool CanWrite => baseStream.CanWrite;
  311. public override long Length => throw new NotSupportedException ();
  312. public override long Position
  313. {
  314. get => baseStream.Position;
  315. set => throw new NotSupportedException ();
  316. }
  317. public override void Flush () { baseStream.Flush (); }
  318. public override int Read (byte [] buffer, int offset, int count) => baseStream.Read (buffer, offset, count);
  319. public override long Seek (long offset, SeekOrigin origin) => throw new NotImplementedException ();
  320. public override void SetLength (long value) { throw new NotSupportedException (); }
  321. public override void Write (byte [] buffer, int offset, int count) { baseStream.Write (buffer, offset, count); }
  322. }
  323. }