HexViewTests.cs 15 KB

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