HexViewTests.cs 15 KB

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