ToplevelTests.cs 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  1. using System;
  2. using Terminal.Gui;
  3. using Xunit;
  4. using Xunit.Abstractions;
  5. namespace Terminal.Gui.TopLevelTests {
  6. public class ToplevelTests {
  7. readonly ITestOutputHelper output;
  8. public ToplevelTests (ITestOutputHelper output)
  9. {
  10. this.output = output;
  11. }
  12. [Fact]
  13. [AutoInitShutdown]
  14. public void Constructor_Default ()
  15. {
  16. var top = new Toplevel ();
  17. Assert.Equal (Colors.TopLevel, top.ColorScheme);
  18. Assert.Equal ("Fill(0)", top.Width.ToString ());
  19. Assert.Equal ("Fill(0)", top.Height.ToString ());
  20. Assert.False (top.Running);
  21. Assert.False (top.Modal);
  22. Assert.Null (top.MenuBar);
  23. Assert.Null (top.StatusBar);
  24. Assert.False (top.IsMdiContainer);
  25. Assert.False (top.IsMdiChild);
  26. }
  27. [Fact]
  28. [AutoInitShutdown]
  29. public void Create_Toplevel ()
  30. {
  31. var top = Toplevel.Create ();
  32. Assert.Equal (new Rect (0, 0, Application.Driver.Cols, Application.Driver.Rows), top.Bounds);
  33. }
  34. [Fact]
  35. [AutoInitShutdown]
  36. public void Application_Top_EnsureVisibleBounds_To_Driver_Rows_And_Cols ()
  37. {
  38. var iterations = 0;
  39. Application.Iteration += () => {
  40. if (iterations == 0) {
  41. Assert.False (Application.Top.AutoSize);
  42. Assert.Equal ("Top1", Application.Top.Text);
  43. Assert.Equal (0, Application.Top.Frame.X);
  44. Assert.Equal (0, Application.Top.Frame.Y);
  45. Assert.Equal (Application.Driver.Cols, Application.Top.Frame.Width);
  46. Assert.Equal (Application.Driver.Rows, Application.Top.Frame.Height);
  47. Application.Top.ProcessHotKey (new KeyEvent (Key.CtrlMask | Key.R, new KeyModifiers ()));
  48. } else if (iterations == 1) {
  49. Assert.Equal ("Top2", Application.Top.Text);
  50. Assert.Equal (0, Application.Top.Frame.X);
  51. Assert.Equal (0, Application.Top.Frame.Y);
  52. Assert.Equal (Application.Driver.Cols, Application.Top.Frame.Width);
  53. Assert.Equal (Application.Driver.Rows, Application.Top.Frame.Height);
  54. Application.Top.ProcessHotKey (new KeyEvent (Key.CtrlMask | Key.C, new KeyModifiers ()));
  55. } else if (iterations == 3) {
  56. Assert.Equal ("Top1", Application.Top.Text);
  57. Assert.Equal (0, Application.Top.Frame.X);
  58. Assert.Equal (0, Application.Top.Frame.Y);
  59. Assert.Equal (Application.Driver.Cols, Application.Top.Frame.Width);
  60. Assert.Equal (Application.Driver.Rows, Application.Top.Frame.Height);
  61. Application.Top.ProcessHotKey (new KeyEvent (Key.CtrlMask | Key.R, new KeyModifiers ()));
  62. } else if (iterations == 4) {
  63. Assert.Equal ("Top2", Application.Top.Text);
  64. Assert.Equal (0, Application.Top.Frame.X);
  65. Assert.Equal (0, Application.Top.Frame.Y);
  66. Assert.Equal (Application.Driver.Cols, Application.Top.Frame.Width);
  67. Assert.Equal (Application.Driver.Rows, Application.Top.Frame.Height);
  68. Application.Top.ProcessHotKey (new KeyEvent (Key.CtrlMask | Key.C, new KeyModifiers ()));
  69. } else if (iterations == 6) {
  70. Assert.Equal ("Top1", Application.Top.Text);
  71. Assert.Equal (0, Application.Top.Frame.X);
  72. Assert.Equal (0, Application.Top.Frame.Y);
  73. Assert.Equal (Application.Driver.Cols, Application.Top.Frame.Width);
  74. Assert.Equal (Application.Driver.Rows, Application.Top.Frame.Height);
  75. Application.Top.ProcessHotKey (new KeyEvent (Key.CtrlMask | Key.Q, new KeyModifiers ()));
  76. }
  77. iterations++;
  78. };
  79. Application.Run (Top1 ());
  80. Toplevel Top1 ()
  81. {
  82. var top = Application.Top;
  83. top.Text = "Top1";
  84. var menu = new MenuBar (new MenuBarItem [] {
  85. new MenuBarItem ("_Options", new MenuItem [] {
  86. new MenuItem ("_Run Top2", "", () => Application.Run (Top2 ()), null, null, Key.CtrlMask | Key.R),
  87. new MenuItem ("_Quit", "", () => Application.RequestStop(), null, null, Key.CtrlMask | Key.Q)
  88. })
  89. });
  90. top.Add (menu);
  91. var statusBar = new StatusBar (new [] {
  92. new StatusItem(Key.CtrlMask | Key.R, "~^R~ Run Top2", () => Application.Run (Top2 ())),
  93. new StatusItem(Application.QuitKey, $"{Application.QuitKey} to Quit", () => Application.RequestStop())
  94. });
  95. top.Add (statusBar);
  96. var t1 = new Toplevel ();
  97. top.Add (t1);
  98. return top;
  99. }
  100. Toplevel Top2 ()
  101. {
  102. var top = new Toplevel (Application.Top.Frame);
  103. top.Text = "Top2";
  104. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  105. var menu = new MenuBar (new MenuBarItem [] {
  106. new MenuBarItem ("_Stage", new MenuItem [] {
  107. new MenuItem ("_Close", "", () => Application.RequestStop(), null, null, Key.CtrlMask | Key.C)
  108. })
  109. });
  110. top.Add (menu);
  111. var statusBar = new StatusBar (new [] {
  112. new StatusItem(Key.CtrlMask | Key.C, "~^C~ Close", () => Application.RequestStop()),
  113. });
  114. top.Add (statusBar);
  115. win.Add (new ListView () {
  116. X = 0,
  117. Y = 0,
  118. Width = Dim.Fill (),
  119. Height = Dim.Fill ()
  120. });
  121. top.Add (win);
  122. return top;
  123. }
  124. }
  125. [Fact]
  126. [AutoInitShutdown]
  127. public void Internal_Tests ()
  128. {
  129. Toplevel.dragPosition = null; // dragPosition is `static` and must be reset for each instance or unit tests will fail?
  130. var top = new Toplevel ();
  131. var eventInvoked = "";
  132. top.ChildUnloaded += (e) => eventInvoked = "ChildUnloaded";
  133. top.OnChildUnloaded (top);
  134. Assert.Equal ("ChildUnloaded", eventInvoked);
  135. top.ChildLoaded += (e) => eventInvoked = "ChildLoaded";
  136. top.OnChildLoaded (top);
  137. Assert.Equal ("ChildLoaded", eventInvoked);
  138. top.Closed += (e) => eventInvoked = "Closed";
  139. top.OnClosed (top);
  140. Assert.Equal ("Closed", eventInvoked);
  141. top.Closing += (e) => eventInvoked = "Closing";
  142. top.OnClosing (new ToplevelClosingEventArgs (top));
  143. Assert.Equal ("Closing", eventInvoked);
  144. top.AllChildClosed += () => eventInvoked = "AllChildClosed";
  145. top.OnAllChildClosed ();
  146. Assert.Equal ("AllChildClosed", eventInvoked);
  147. top.ChildClosed += (e) => eventInvoked = "ChildClosed";
  148. top.OnChildClosed (top);
  149. Assert.Equal ("ChildClosed", eventInvoked);
  150. top.Deactivate += (e) => eventInvoked = "Deactivate";
  151. top.OnDeactivate (top);
  152. Assert.Equal ("Deactivate", eventInvoked);
  153. top.Activate += (e) => eventInvoked = "Activate";
  154. top.OnActivate (top);
  155. Assert.Equal ("Activate", eventInvoked);
  156. top.Loaded += () => eventInvoked = "Loaded";
  157. top.OnLoaded ();
  158. Assert.Equal ("Loaded", eventInvoked);
  159. top.Ready += () => eventInvoked = "Ready";
  160. top.OnReady ();
  161. Assert.Equal ("Ready", eventInvoked);
  162. top.Unloaded += () => eventInvoked = "Unloaded";
  163. top.OnUnloaded ();
  164. Assert.Equal ("Unloaded", eventInvoked);
  165. top.AddMenuStatusBar (new MenuBar ());
  166. Assert.NotNull (top.MenuBar);
  167. top.AddMenuStatusBar (new StatusBar ());
  168. Assert.NotNull (top.StatusBar);
  169. top.RemoveMenuStatusBar (top.MenuBar);
  170. Assert.Null (top.MenuBar);
  171. top.RemoveMenuStatusBar (top.StatusBar);
  172. Assert.Null (top.StatusBar);
  173. Application.Begin (top);
  174. Assert.Equal (top, Application.Top);
  175. // top is Application.Top without menu and status bar.
  176. var supView = top.EnsureVisibleBounds (top, 2, 2, out int nx, out int ny, out View mb, out View sb);
  177. Assert.Equal (Application.Top, supView);
  178. Assert.Equal (0, nx);
  179. Assert.Equal (0, ny);
  180. Assert.Null (mb);
  181. Assert.Null (sb);
  182. top.AddMenuStatusBar (new MenuBar ());
  183. Assert.NotNull (top.MenuBar);
  184. // top is Application.Top with a menu and without status bar.
  185. top.EnsureVisibleBounds (top, 2, 2, out nx, out ny, out mb, out sb);
  186. Assert.Equal (0, nx);
  187. Assert.Equal (1, ny);
  188. Assert.NotNull (mb);
  189. Assert.Null (sb);
  190. top.AddMenuStatusBar (new StatusBar ());
  191. Assert.NotNull (top.StatusBar);
  192. // top is Application.Top with a menu and status bar.
  193. top.EnsureVisibleBounds (top, 2, 2, out nx, out ny, out mb, out sb);
  194. Assert.Equal (0, nx);
  195. Assert.Equal (1, ny);
  196. Assert.NotNull (mb);
  197. Assert.NotNull (sb);
  198. top.RemoveMenuStatusBar (top.MenuBar);
  199. Assert.Null (top.MenuBar);
  200. // top is Application.Top without a menu and with a status bar.
  201. top.EnsureVisibleBounds (top, 2, 2, out nx, out ny, out mb, out sb);
  202. Assert.Equal (0, nx);
  203. Assert.Equal (0, ny);
  204. Assert.Null (mb);
  205. Assert.NotNull (sb);
  206. top.RemoveMenuStatusBar (top.StatusBar);
  207. Assert.Null (top.StatusBar);
  208. Assert.Null (top.MenuBar);
  209. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  210. top.Add (win);
  211. top.LayoutSubviews ();
  212. // The SuperView is always the same regardless of the caller.
  213. supView = top.EnsureVisibleBounds (win, 0, 0, out nx, out ny, out mb, out sb);
  214. Assert.Equal (Application.Top, supView);
  215. supView = win.EnsureVisibleBounds (win, 0, 0, out nx, out ny, out mb, out sb);
  216. Assert.Equal (Application.Top, supView);
  217. // top is Application.Top without menu and status bar.
  218. top.EnsureVisibleBounds (win, 0, 0, out nx, out ny, out mb, out sb);
  219. Assert.Equal (0, nx);
  220. Assert.Equal (0, ny);
  221. Assert.Null (mb);
  222. Assert.Null (sb);
  223. top.AddMenuStatusBar (new MenuBar ());
  224. Assert.NotNull (top.MenuBar);
  225. // top is Application.Top with a menu and without status bar.
  226. top.EnsureVisibleBounds (win, 2, 2, out nx, out ny, out mb, out sb);
  227. Assert.Equal (0, nx);
  228. Assert.Equal (1, ny);
  229. Assert.NotNull (mb);
  230. Assert.Null (sb);
  231. top.AddMenuStatusBar (new StatusBar ());
  232. Assert.NotNull (top.StatusBar);
  233. // top is Application.Top with a menu and status bar.
  234. top.EnsureVisibleBounds (win, 30, 20, out nx, out ny, out mb, out sb);
  235. Assert.Equal (0, nx);
  236. Assert.Equal (1, ny);
  237. Assert.NotNull (mb);
  238. Assert.NotNull (sb);
  239. top.RemoveMenuStatusBar (top.MenuBar);
  240. top.RemoveMenuStatusBar (top.StatusBar);
  241. Assert.Null (top.StatusBar);
  242. Assert.Null (top.MenuBar);
  243. top.Remove (win);
  244. win = new Window () { Width = 60, Height = 15 };
  245. top.Add (win);
  246. // top is Application.Top without menu and status bar.
  247. top.EnsureVisibleBounds (win, 0, 0, out nx, out ny, out mb, out sb);
  248. Assert.Equal (0, nx);
  249. Assert.Equal (0, ny);
  250. Assert.Null (mb);
  251. Assert.Null (sb);
  252. top.AddMenuStatusBar (new MenuBar ());
  253. Assert.NotNull (top.MenuBar);
  254. // top is Application.Top with a menu and without status bar.
  255. top.EnsureVisibleBounds (win, 2, 2, out nx, out ny, out mb, out sb);
  256. Assert.Equal (2, nx);
  257. Assert.Equal (2, ny);
  258. Assert.NotNull (mb);
  259. Assert.Null (sb);
  260. top.AddMenuStatusBar (new StatusBar ());
  261. Assert.NotNull (top.StatusBar);
  262. // top is Application.Top with a menu and status bar.
  263. top.EnsureVisibleBounds (win, 30, 20, out nx, out ny, out mb, out sb);
  264. Assert.Equal (20, nx); // 20+60=80
  265. Assert.Equal (9, ny); // 9+15+1(mb)=25
  266. Assert.NotNull (mb);
  267. Assert.NotNull (sb);
  268. top.PositionToplevels ();
  269. Assert.Equal (new Rect (0, 1, 60, 15), win.Frame);
  270. Assert.Null (Toplevel.dragPosition);
  271. win.MouseEvent (new MouseEvent () { X = 6, Y = 0, Flags = MouseFlags.Button1Pressed });
  272. Assert.Equal (new Point (6, 0), Toplevel.dragPosition);
  273. win.MouseEvent (new MouseEvent () { X = 6, Y = 0, Flags = MouseFlags.Button1Released });
  274. Assert.Null (Toplevel.dragPosition);
  275. win.CanFocus = false;
  276. win.MouseEvent (new MouseEvent () { X = 6, Y = 0, Flags = MouseFlags.Button1Pressed });
  277. Assert.Null (Toplevel.dragPosition);
  278. }
  279. [Fact]
  280. [AutoInitShutdown]
  281. public void KeyBindings_Command ()
  282. {
  283. var isRunning = false;
  284. var win1 = new Window ("Win1") { Width = Dim.Percent (50f), Height = Dim.Fill () };
  285. var lblTf1W1 = new Label ("Enter text in TextField on Win1:");
  286. var tf1W1 = new TextField ("Text1 on Win1") { X = Pos.Right (lblTf1W1) + 1, Width = Dim.Fill () };
  287. var lblTvW1 = new Label ("Enter text in TextView on Win1:") { Y = Pos.Bottom (lblTf1W1) + 1 };
  288. var tvW1 = new TextView () { X = Pos.Left (tf1W1), Width = Dim.Fill (), Height = 2, Text = "First line Win1\nSecond line Win1" };
  289. var lblTf2W1 = new Label ("Enter text in TextField on Win1:") { Y = Pos.Bottom (lblTvW1) + 1 };
  290. var tf2W1 = new TextField ("Text2 on Win1") { X = Pos.Left (tf1W1), Width = Dim.Fill () };
  291. win1.Add (lblTf1W1, tf1W1, lblTvW1, tvW1, lblTf2W1, tf2W1);
  292. var win2 = new Window ("Win2") { X = Pos.Right (win1) + 1, Width = Dim.Percent (50f), Height = Dim.Fill () };
  293. var lblTf1W2 = new Label ("Enter text in TextField on Win2:");
  294. var tf1W2 = new TextField ("Text1 on Win2") { X = Pos.Right (lblTf1W2) + 1, Width = Dim.Fill () };
  295. var lblTvW2 = new Label ("Enter text in TextView on Win2:") { Y = Pos.Bottom (lblTf1W2) + 1 };
  296. var tvW2 = new TextView () { X = Pos.Left (tf1W2), Width = Dim.Fill (), Height = 2, Text = "First line Win1\nSecond line Win2" };
  297. var lblTf2W2 = new Label ("Enter text in TextField on Win2:") { Y = Pos.Bottom (lblTvW2) + 1 };
  298. var tf2W2 = new TextField ("Text2 on Win2") { X = Pos.Left (tf1W2), Width = Dim.Fill () };
  299. win2.Add (lblTf1W2, tf1W2, lblTvW2, tvW2, lblTf2W2, tf2W2);
  300. var top = Application.Top;
  301. top.Add (win1, win2);
  302. top.Loaded += () => isRunning = true;
  303. top.Closing += (_) => isRunning = false;
  304. Application.Begin (top);
  305. top.Running = true;
  306. Assert.Equal (new Rect (0, 0, 40, 25), win1.Frame);
  307. Assert.Equal (new Rect (41, 0, 40, 25), win2.Frame);
  308. Assert.Equal (win1, top.Focused);
  309. Assert.Equal (tf1W1, top.MostFocused);
  310. Assert.True (isRunning);
  311. Assert.True (top.Focused.ProcessKey (new KeyEvent (Application.QuitKey, new KeyModifiers ())));
  312. Assert.False (isRunning);
  313. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Z | Key.CtrlMask, new KeyModifiers ())));
  314. Assert.False (top.Focused.ProcessKey (new KeyEvent (Key.F5, new KeyModifiers ())));
  315. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  316. Assert.Equal (win1, top.Focused);
  317. Assert.Equal (tvW1, top.MostFocused);
  318. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  319. Assert.Equal ($"\tFirst line Win1{Environment.NewLine}Second line Win1", tvW1.Text);
  320. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ())));
  321. Assert.Equal ($"First line Win1{Environment.NewLine}Second line Win1", tvW1.Text);
  322. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers ())));
  323. Assert.Equal (win1, top.Focused);
  324. Assert.Equal (tf2W1, top.MostFocused);
  325. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  326. Assert.Equal (win1, top.Focused);
  327. Assert.Equal (tf1W1, top.MostFocused);
  328. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  329. Assert.Equal (win1, top.Focused);
  330. Assert.Equal (tf1W1, top.MostFocused);
  331. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  332. Assert.Equal (win1, top.Focused);
  333. Assert.Equal (tvW1, top.MostFocused);
  334. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.I | Key.CtrlMask, new KeyModifiers ())));
  335. Assert.Equal (win1, top.Focused);
  336. Assert.Equal (tf2W1, top.MostFocused);
  337. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ())));
  338. Assert.Equal (win1, top.Focused);
  339. Assert.Equal (tvW1, top.MostFocused);
  340. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ())));
  341. Assert.Equal (win1, top.Focused);
  342. Assert.Equal (tf1W1, top.MostFocused);
  343. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ())));
  344. Assert.Equal (win1, top.Focused);
  345. Assert.Equal (tf2W1, top.MostFocused);
  346. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers ())));
  347. Assert.Equal (win2, top.Focused);
  348. Assert.Equal (tf1W2, top.MostFocused);
  349. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask | Key.ShiftMask, new KeyModifiers ())));
  350. Assert.Equal (win1, top.Focused);
  351. Assert.Equal (tf2W1, top.MostFocused);
  352. Assert.True (top.Focused.ProcessKey (new KeyEvent (Application.AlternateForwardKey, new KeyModifiers ())));
  353. Assert.Equal (win2, top.Focused);
  354. Assert.Equal (tf1W2, top.MostFocused);
  355. Assert.True (top.Focused.ProcessKey (new KeyEvent (Application.AlternateBackwardKey, new KeyModifiers ())));
  356. Assert.Equal (win1, top.Focused);
  357. Assert.Equal (tf2W1, top.MostFocused);
  358. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ())));
  359. Assert.Equal (win1, top.Focused);
  360. Assert.Equal (tvW1, top.MostFocused);
  361. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.B | Key.CtrlMask, new KeyModifiers ())));
  362. Assert.Equal (win1, top.Focused);
  363. Assert.Equal (tf1W1, top.MostFocused);
  364. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  365. Assert.Equal (win1, top.Focused);
  366. Assert.Equal (tvW1, top.MostFocused);
  367. Assert.Equal (new Point (0, 0), tvW1.CursorPosition);
  368. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.End | Key.CtrlMask, new KeyModifiers ())));
  369. Assert.Equal (win1, top.Focused);
  370. Assert.Equal (tvW1, top.MostFocused);
  371. Assert.Equal (new Point (16, 1), tvW1.CursorPosition);
  372. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.F | Key.CtrlMask, new KeyModifiers ())));
  373. Assert.Equal (win1, top.Focused);
  374. Assert.Equal (tf2W1, top.MostFocused);
  375. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.L | Key.CtrlMask, new KeyModifiers ())));
  376. }
  377. [Fact]
  378. [AutoInitShutdown]
  379. public void KeyBindings_Command_With_MdiTop ()
  380. {
  381. var top = Application.Top;
  382. Assert.Null (Application.MdiTop);
  383. top.IsMdiContainer = true;
  384. Application.Begin (top);
  385. Assert.Equal (Application.Top, Application.MdiTop);
  386. var isRunning = true;
  387. var win1 = new Window ("Win1") { Width = Dim.Percent (50f), Height = Dim.Fill () };
  388. var lblTf1W1 = new Label ("Enter text in TextField on Win1:");
  389. var tf1W1 = new TextField ("Text1 on Win1") { X = Pos.Right (lblTf1W1) + 1, Width = Dim.Fill () };
  390. var lblTvW1 = new Label ("Enter text in TextView on Win1:") { Y = Pos.Bottom (lblTf1W1) + 1 };
  391. var tvW1 = new TextView () { X = Pos.Left (tf1W1), Width = Dim.Fill (), Height = 2, Text = "First line Win1\nSecond line Win1" };
  392. var lblTf2W1 = new Label ("Enter text in TextField on Win1:") { Y = Pos.Bottom (lblTvW1) + 1 };
  393. var tf2W1 = new TextField ("Text2 on Win1") { X = Pos.Left (tf1W1), Width = Dim.Fill () };
  394. win1.Add (lblTf1W1, tf1W1, lblTvW1, tvW1, lblTf2W1, tf2W1);
  395. var win2 = new Window ("Win2") { Width = Dim.Percent (50f), Height = Dim.Fill () };
  396. var lblTf1W2 = new Label ("Enter text in TextField on Win2:");
  397. var tf1W2 = new TextField ("Text1 on Win2") { X = Pos.Right (lblTf1W2) + 1, Width = Dim.Fill () };
  398. var lblTvW2 = new Label ("Enter text in TextView on Win2:") { Y = Pos.Bottom (lblTf1W2) + 1 };
  399. var tvW2 = new TextView () { X = Pos.Left (tf1W2), Width = Dim.Fill (), Height = 2, Text = "First line Win1\nSecond line Win2" };
  400. var lblTf2W2 = new Label ("Enter text in TextField on Win2:") { Y = Pos.Bottom (lblTvW2) + 1 };
  401. var tf2W2 = new TextField ("Text2 on Win2") { X = Pos.Left (tf1W2), Width = Dim.Fill () };
  402. win2.Add (lblTf1W2, tf1W2, lblTvW2, tvW2, lblTf2W2, tf2W2);
  403. win1.Closing += (_) => isRunning = false;
  404. Assert.Null (top.Focused);
  405. Assert.Equal (top, Application.Current);
  406. Assert.True (top.IsCurrentTop);
  407. Assert.Equal (top, Application.MdiTop);
  408. Application.Begin (win1);
  409. Assert.Equal (new Rect (0, 0, 40, 25), win1.Frame);
  410. Assert.NotEqual (top, Application.Current);
  411. Assert.False (top.IsCurrentTop);
  412. Assert.Equal (win1, Application.Current);
  413. Assert.True (win1.IsCurrentTop);
  414. Assert.True (win1.IsMdiChild);
  415. Assert.Null (top.Focused);
  416. Assert.Null (top.MostFocused);
  417. Assert.Equal (win1.Subviews [0], win1.Focused);
  418. Assert.Equal (tf1W1, win1.MostFocused);
  419. Assert.True (win1.IsMdiChild);
  420. Assert.Single (Application.MdiChildes);
  421. Application.Begin (win2);
  422. Assert.Equal (new Rect (0, 0, 40, 25), win2.Frame);
  423. Assert.NotEqual (top, Application.Current);
  424. Assert.False (top.IsCurrentTop);
  425. Assert.Equal (win2, Application.Current);
  426. Assert.True (win2.IsCurrentTop);
  427. Assert.True (win2.IsMdiChild);
  428. Assert.Null (top.Focused);
  429. Assert.Null (top.MostFocused);
  430. Assert.Equal (win2.Subviews [0], win2.Focused);
  431. Assert.Equal (tf1W2, win2.MostFocused);
  432. Assert.Equal (2, Application.MdiChildes.Count);
  433. Application.ShowChild (win1);
  434. Assert.Equal (win1, Application.Current);
  435. Assert.Equal (win1, Application.MdiChildes [0]);
  436. win1.Running = true;
  437. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Application.QuitKey, new KeyModifiers ())));
  438. Assert.False (isRunning);
  439. Assert.False (win1.Running);
  440. Assert.Equal (win1, Application.MdiChildes [0]);
  441. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Z | Key.CtrlMask, new KeyModifiers ())));
  442. Assert.False (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.F5, new KeyModifiers ())));
  443. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  444. Assert.True (win1.IsCurrentTop);
  445. Assert.Equal (tvW1, win1.MostFocused);
  446. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  447. Assert.Equal ($"\tFirst line Win1{Environment.NewLine}Second line Win1", tvW1.Text);
  448. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ())));
  449. Assert.Equal ($"First line Win1{Environment.NewLine}Second line Win1", tvW1.Text);
  450. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers ())));
  451. Assert.Equal (win1, Application.MdiChildes [0]);
  452. Assert.Equal (tf2W1, win1.MostFocused);
  453. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  454. Assert.Equal (win1, Application.MdiChildes [0]);
  455. Assert.Equal (tf1W1, win1.MostFocused);
  456. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  457. Assert.Equal (win1, Application.MdiChildes [0]);
  458. Assert.Equal (tf1W1, win1.MostFocused);
  459. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  460. Assert.Equal (win1, Application.MdiChildes [0]);
  461. Assert.Equal (tvW1, win1.MostFocused);
  462. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.I | Key.CtrlMask, new KeyModifiers ())));
  463. Assert.Equal (win1, Application.MdiChildes [0]);
  464. Assert.Equal (tf2W1, win1.MostFocused);
  465. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ())));
  466. Assert.Equal (win1, Application.MdiChildes [0]);
  467. Assert.Equal (tvW1, win1.MostFocused);
  468. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ())));
  469. Assert.Equal (win1, Application.MdiChildes [0]);
  470. Assert.Equal (tf1W1, win1.MostFocused);
  471. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ())));
  472. Assert.Equal (win1, Application.MdiChildes [0]);
  473. Assert.Equal (tf2W1, win1.MostFocused);
  474. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  475. Assert.Equal (win1, Application.MdiChildes [0]);
  476. Assert.Equal (tf1W1, win1.MostFocused);
  477. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers ())));
  478. Assert.Equal (win2, Application.MdiChildes [0]);
  479. Assert.Equal (tf1W2, win2.MostFocused);
  480. tf2W2.SetFocus ();
  481. Assert.True (tf2W2.HasFocus);
  482. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask | Key.ShiftMask, new KeyModifiers ())));
  483. Assert.Equal (win1, Application.MdiChildes [0]);
  484. Assert.Equal (tf1W1, win1.MostFocused);
  485. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Application.AlternateForwardKey, new KeyModifiers ())));
  486. Assert.Equal (win2, Application.MdiChildes [0]);
  487. Assert.Equal (tf2W2, win2.MostFocused);
  488. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Application.AlternateBackwardKey, new KeyModifiers ())));
  489. Assert.Equal (win1, Application.MdiChildes [0]);
  490. Assert.Equal (tf1W1, win1.MostFocused);
  491. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  492. Assert.Equal (win1, Application.MdiChildes [0]);
  493. Assert.Equal (tvW1, win1.MostFocused);
  494. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.B | Key.CtrlMask, new KeyModifiers ())));
  495. Assert.Equal (win1, Application.MdiChildes [0]);
  496. Assert.Equal (tf1W1, win1.MostFocused);
  497. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  498. Assert.Equal (win1, Application.MdiChildes [0]);
  499. Assert.Equal (tvW1, win1.MostFocused);
  500. Assert.Equal (new Point (0, 0), tvW1.CursorPosition);
  501. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.End | Key.CtrlMask, new KeyModifiers ())));
  502. Assert.Equal (win1, Application.MdiChildes [0]);
  503. Assert.Equal (tvW1, win1.MostFocused);
  504. Assert.Equal (new Point (16, 1), tvW1.CursorPosition);
  505. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.F | Key.CtrlMask, new KeyModifiers ())));
  506. Assert.Equal (win1, Application.MdiChildes [0]);
  507. Assert.Equal (tf2W1, win1.MostFocused);
  508. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.L | Key.CtrlMask, new KeyModifiers ())));
  509. }
  510. [Fact]
  511. public void Added_Event_Should_Not_Be_Used_To_Initialize_Toplevel_Events ()
  512. {
  513. Key alternateForwardKey = default;
  514. Key alternateBackwardKey = default;
  515. Key quitKey = default;
  516. var wasAdded = false;
  517. var view = new View ();
  518. view.Added += View_Added;
  519. void View_Added (View obj)
  520. {
  521. Assert.Throws<NullReferenceException> (() => Application.Top.AlternateForwardKeyChanged += (e) => alternateForwardKey = e);
  522. Assert.Throws<NullReferenceException> (() => Application.Top.AlternateBackwardKeyChanged += (e) => alternateBackwardKey = e);
  523. Assert.Throws<NullReferenceException> (() => Application.Top.QuitKeyChanged += (e) => quitKey = e);
  524. Assert.False (wasAdded);
  525. wasAdded = true;
  526. view.Added -= View_Added;
  527. }
  528. var win = new Window ();
  529. win.Add (view);
  530. Application.Init (new FakeDriver ());
  531. var top = Application.Top;
  532. top.Add (win);
  533. Assert.True (wasAdded);
  534. Application.Shutdown ();
  535. }
  536. [Fact]
  537. [AutoInitShutdown]
  538. public void AlternateForwardKeyChanged_AlternateBackwardKeyChanged_QuitKeyChanged_Events ()
  539. {
  540. Key alternateForwardKey = default;
  541. Key alternateBackwardKey = default;
  542. Key quitKey = default;
  543. var view = new View ();
  544. view.Initialized += View_Initialized;
  545. void View_Initialized (object sender, EventArgs e)
  546. {
  547. Application.Top.AlternateForwardKeyChanged += (e) => alternateForwardKey = e;
  548. Application.Top.AlternateBackwardKeyChanged += (e) => alternateBackwardKey = e;
  549. Application.Top.QuitKeyChanged += (e) => quitKey = e;
  550. }
  551. var win = new Window ();
  552. win.Add (view);
  553. var top = Application.Top;
  554. top.Add (win);
  555. Application.Begin (top);
  556. Assert.Equal (Key.Null, alternateForwardKey);
  557. Assert.Equal (Key.Null, alternateBackwardKey);
  558. Assert.Equal (Key.Null, quitKey);
  559. Assert.Equal (Key.PageDown | Key.CtrlMask, Application.AlternateForwardKey);
  560. Assert.Equal (Key.PageUp | Key.CtrlMask, Application.AlternateBackwardKey);
  561. Assert.Equal (Key.Q | Key.CtrlMask, Application.QuitKey);
  562. Application.AlternateForwardKey = Key.A;
  563. Application.AlternateBackwardKey = Key.B;
  564. Application.QuitKey = Key.C;
  565. Assert.Equal (Key.PageDown | Key.CtrlMask, alternateForwardKey);
  566. Assert.Equal (Key.PageUp | Key.CtrlMask, alternateBackwardKey);
  567. Assert.Equal (Key.Q | Key.CtrlMask, quitKey);
  568. Assert.Equal (Key.A, Application.AlternateForwardKey);
  569. Assert.Equal (Key.B, Application.AlternateBackwardKey);
  570. Assert.Equal (Key.C, Application.QuitKey);
  571. // Replacing the defaults keys to avoid errors on others unit tests that are using it.
  572. Application.AlternateForwardKey = Key.PageDown | Key.CtrlMask;
  573. Application.AlternateBackwardKey = Key.PageUp | Key.CtrlMask;
  574. Application.QuitKey = Key.Q | Key.CtrlMask;
  575. Assert.Equal (Key.PageDown | Key.CtrlMask, Application.AlternateForwardKey);
  576. Assert.Equal (Key.PageUp | Key.CtrlMask, Application.AlternateBackwardKey);
  577. Assert.Equal (Key.Q | Key.CtrlMask, Application.QuitKey);
  578. }
  579. [Fact]
  580. [AutoInitShutdown]
  581. public void FileDialog_FileSystemWatcher ()
  582. {
  583. for (int i = 0; i < 8; i++) {
  584. var fd = new FileDialog ();
  585. fd.Ready += () => Application.RequestStop ();
  586. Application.Run (fd);
  587. }
  588. }
  589. [Fact, AutoInitShutdown]
  590. public void Mouse_Drag_On_Top_With_Superview_Null ()
  591. {
  592. var menu = new MenuBar (new MenuBarItem [] {
  593. new MenuBarItem("File", new MenuItem [] {
  594. new MenuItem("New", "", null)
  595. })
  596. });
  597. var sbar = new StatusBar (new StatusItem [] {
  598. new StatusItem(Key.N, "~CTRL-N~ New", null)
  599. });
  600. var win = new Window ("Window");
  601. var top = Application.Top;
  602. top.Add (menu, sbar, win);
  603. var iterations = -1;
  604. Application.Iteration = () => {
  605. iterations++;
  606. if (iterations == 0) {
  607. ((FakeDriver)Application.Driver).SetBufferSize (40, 15);
  608. MessageBox.Query ("About", "Hello Word", "Ok");
  609. } else if (iterations == 1) TestHelpers.AssertDriverContentsWithFrameAre (@"
  610. File
  611. ┌ Window ──────────────────────────────┐
  612. │ │
  613. │ │
  614. │ │
  615. │ ┌ About ───────────────┐ │
  616. │ │ Hello Word │ │
  617. │ │ │ │
  618. │ │ [◦ Ok ◦] │ │
  619. │ └──────────────────────┘ │
  620. │ │
  621. │ │
  622. │ │
  623. └──────────────────────────────────────┘
  624. CTRL-N New ", output);
  625. else if (iterations == 2) {
  626. Assert.Null (Application.MouseGrabView);
  627. // Grab the mouse
  628. ReflectionTools.InvokePrivate (
  629. typeof (Application),
  630. "ProcessMouseEvent",
  631. new MouseEvent () {
  632. X = 8,
  633. Y = 5,
  634. Flags = MouseFlags.Button1Pressed
  635. });
  636. Assert.Equal (Application.Current, Application.MouseGrabView);
  637. Assert.Equal (new Rect (8, 5, 24, 5), Application.MouseGrabView.Frame);
  638. } else if (iterations == 3) {
  639. Assert.Equal (Application.Current, Application.MouseGrabView);
  640. // Grab to left
  641. ReflectionTools.InvokePrivate (
  642. typeof (Application),
  643. "ProcessMouseEvent",
  644. new MouseEvent () {
  645. X = 7,
  646. Y = 5,
  647. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  648. });
  649. Assert.Equal (Application.Current, Application.MouseGrabView);
  650. Assert.Equal (new Rect (7, 5, 24, 5), Application.MouseGrabView.Frame);
  651. } else if (iterations == 4) {
  652. Assert.Equal (Application.Current, Application.MouseGrabView);
  653. TestHelpers.AssertDriverContentsWithFrameAre (@"
  654. File
  655. ┌ Window ──────────────────────────────┐
  656. │ │
  657. │ │
  658. │ │
  659. │ ┌ About ───────────────┐ │
  660. │ │ Hello Word │ │
  661. │ │ │ │
  662. │ │ [◦ Ok ◦] │ │
  663. │ └──────────────────────┘ │
  664. │ │
  665. │ │
  666. │ │
  667. └──────────────────────────────────────┘
  668. CTRL-N New ", output);
  669. Assert.Equal (Application.Current, Application.MouseGrabView);
  670. } else if (iterations == 5) {
  671. Assert.Equal (Application.Current, Application.MouseGrabView);
  672. // Grab to top
  673. ReflectionTools.InvokePrivate (
  674. typeof (Application),
  675. "ProcessMouseEvent",
  676. new MouseEvent () {
  677. X = 7,
  678. Y = 4,
  679. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  680. });
  681. Assert.Equal (Application.Current, Application.MouseGrabView);
  682. Assert.Equal (new Rect (7, 4, 24, 5), Application.MouseGrabView.Frame);
  683. } else if (iterations == 6) {
  684. Assert.Equal (Application.Current, Application.MouseGrabView);
  685. TestHelpers.AssertDriverContentsWithFrameAre (@"
  686. File
  687. ┌ Window ──────────────────────────────┐
  688. │ │
  689. │ │
  690. │ ┌ About ───────────────┐ │
  691. │ │ Hello Word │ │
  692. │ │ │ │
  693. │ │ [◦ Ok ◦] │ │
  694. │ └──────────────────────┘ │
  695. │ │
  696. │ │
  697. │ │
  698. │ │
  699. └──────────────────────────────────────┘
  700. CTRL-N New ", output);
  701. Assert.Equal (Application.Current, Application.MouseGrabView);
  702. Assert.Equal (new Rect (7, 4, 24, 5), Application.MouseGrabView.Frame);
  703. } else if (iterations == 7) {
  704. Assert.Equal (Application.Current, Application.MouseGrabView);
  705. // Ungrab the mouse
  706. ReflectionTools.InvokePrivate (
  707. typeof (Application),
  708. "ProcessMouseEvent",
  709. new MouseEvent () {
  710. X = 7,
  711. Y = 4,
  712. Flags = MouseFlags.Button1Released
  713. });
  714. Assert.Null (Application.MouseGrabView);
  715. } else if (iterations == 8) Application.RequestStop ();
  716. else if (iterations == 9) Application.RequestStop ();
  717. };
  718. Application.Run ();
  719. }
  720. [Fact, AutoInitShutdown]
  721. public void Mouse_Drag_On_Top_With_Superview_Not_Null ()
  722. {
  723. var menu = new MenuBar (new MenuBarItem [] {
  724. new MenuBarItem("File", new MenuItem [] {
  725. new MenuItem("New", "", null)
  726. })
  727. });
  728. var sbar = new StatusBar (new StatusItem [] {
  729. new StatusItem(Key.N, "~CTRL-N~ New", null)
  730. });
  731. var win = new Window ("Window") {
  732. X = 3,
  733. Y = 2,
  734. Width = Dim.Fill (10),
  735. Height = Dim.Fill (5)
  736. };
  737. var top = Application.Top;
  738. top.Add (menu, sbar, win);
  739. var iterations = -1;
  740. Application.Iteration = () => {
  741. iterations++;
  742. if (iterations == 0) {
  743. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  744. Assert.Null (Application.MouseGrabView);
  745. // Grab the mouse
  746. ReflectionTools.InvokePrivate (
  747. typeof (Application),
  748. "ProcessMouseEvent",
  749. new MouseEvent () {
  750. X = 4,
  751. Y = 2,
  752. Flags = MouseFlags.Button1Pressed
  753. });
  754. Assert.Equal (win, Application.MouseGrabView);
  755. Assert.Equal (new Rect (3, 2, 7, 3), Application.MouseGrabView.Frame);
  756. TestHelpers.AssertDriverContentsWithFrameAre (@"
  757. File
  758. ┌─────┐
  759. │ │
  760. └─────┘
  761. CTRL-N New", output);
  762. } else if (iterations == 1) {
  763. Assert.Equal (win, Application.MouseGrabView);
  764. // Grab to left
  765. ReflectionTools.InvokePrivate (
  766. typeof (Application),
  767. "ProcessMouseEvent",
  768. new MouseEvent () {
  769. X = 5,
  770. Y = 2,
  771. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  772. });
  773. Assert.Equal (win, Application.MouseGrabView);
  774. } else if (iterations == 2) {
  775. Assert.Equal (win, Application.MouseGrabView);
  776. TestHelpers.AssertDriverContentsWithFrameAre (@"
  777. File
  778. ┌────┐
  779. │ │
  780. └────┘
  781. CTRL-N New", output);
  782. Assert.Equal (win, Application.MouseGrabView);
  783. Assert.Equal (new Rect (4, 2, 6, 3), Application.MouseGrabView.Frame);
  784. } else if (iterations == 3) {
  785. Assert.Equal (win, Application.MouseGrabView);
  786. // Grab to top
  787. ReflectionTools.InvokePrivate (
  788. typeof (Application),
  789. "ProcessMouseEvent",
  790. new MouseEvent () {
  791. X = 5,
  792. Y = 1,
  793. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  794. });
  795. Assert.Equal (win, Application.MouseGrabView);
  796. } else if (iterations == 4) {
  797. Assert.Equal (win, Application.MouseGrabView);
  798. TestHelpers.AssertDriverContentsWithFrameAre (@"
  799. File
  800. ┌────┐
  801. │ │
  802. │ │
  803. └────┘
  804. CTRL-N New", output);
  805. Assert.Equal (win, Application.MouseGrabView);
  806. Assert.Equal (new Rect (4, 1, 6, 4), Application.MouseGrabView.Frame);
  807. } else if (iterations == 5) {
  808. Assert.Equal (win, Application.MouseGrabView);
  809. // Ungrab the mouse
  810. ReflectionTools.InvokePrivate (
  811. typeof (Application),
  812. "ProcessMouseEvent",
  813. new MouseEvent () {
  814. X = 7,
  815. Y = 4,
  816. Flags = MouseFlags.Button1Released
  817. });
  818. Assert.Null (Application.MouseGrabView);
  819. } else if (iterations == 8) Application.RequestStop ();
  820. };
  821. Application.Run ();
  822. }
  823. [Fact, AutoInitShutdown]
  824. public void EnsureVisibleBounds_With_Border_Null_Not_Throws ()
  825. {
  826. var top = new Toplevel ();
  827. Application.Begin (top);
  828. var exception = Record.Exception (() => ((FakeDriver)Application.Driver).SetBufferSize (0, 10));
  829. Assert.Null (exception);
  830. exception = Record.Exception (() => ((FakeDriver)Application.Driver).SetBufferSize (10, 0));
  831. Assert.Null (exception);
  832. }
  833. [Fact, AutoInitShutdown]
  834. public void OnEnter_OnLeave_Triggered_On_Application_Begin_End ()
  835. {
  836. var isEnter = false;
  837. var isLeave = false;
  838. var v = new View ();
  839. v.Enter += (_) => isEnter = true;
  840. v.Leave += (_) => isLeave = true;
  841. var top = Application.Top;
  842. top.Add (v);
  843. Assert.False (v.CanFocus);
  844. var exception = Record.Exception (() => top.OnEnter (top));
  845. Assert.Null (exception);
  846. exception = Record.Exception (() => top.OnLeave (top));
  847. Assert.Null (exception);
  848. v.CanFocus = true;
  849. Application.Begin (top);
  850. Assert.True (isEnter);
  851. Assert.False (isLeave);
  852. isEnter = false;
  853. var d = new Dialog ();
  854. var rs = Application.Begin (d);
  855. Assert.False (isEnter);
  856. Assert.True (isLeave);
  857. isLeave = false;
  858. Application.End (rs);
  859. Assert.True (isEnter);
  860. Assert.False (isLeave);
  861. }
  862. [Fact, AutoInitShutdown]
  863. public void PositionCursor_SetCursorVisibility_To_Invisible_If_Focused_Is_Null ()
  864. {
  865. var tf = new TextField ("test") { Width = 5 };
  866. var view = new View () { Width = 10, Height = 10 };
  867. view.Add (tf);
  868. Application.Top.Add (view);
  869. Application.Begin (Application.Top);
  870. Assert.True (tf.HasFocus);
  871. Application.Driver.GetCursorVisibility (out CursorVisibility cursor);
  872. Assert.Equal (CursorVisibility.Default, cursor);
  873. view.Enabled = false;
  874. Assert.False (tf.HasFocus);
  875. Application.Refresh ();
  876. Application.Driver.GetCursorVisibility (out cursor);
  877. Assert.Equal (CursorVisibility.Invisible, cursor);
  878. }
  879. [Fact, AutoInitShutdown]
  880. public void IsLoaded_Application_Begin ()
  881. {
  882. var top = Application.Top;
  883. Assert.False (top.IsLoaded);
  884. Application.Begin (top);
  885. Assert.True (top.IsLoaded);
  886. }
  887. [Fact, AutoInitShutdown]
  888. public void IsLoaded_With_Sub_Toplevel_Application_Begin_NeedDisplay ()
  889. {
  890. var top = Application.Top;
  891. var subTop = new Toplevel ();
  892. var view = new View (new Rect (0, 0, 20, 10));
  893. subTop.Add (view);
  894. top.Add (subTop);
  895. Assert.False (top.IsLoaded);
  896. Assert.False (subTop.IsLoaded);
  897. Assert.Equal (new Rect (0, 0, 20, 10), view.Frame);
  898. Assert.Equal (new Rect (0, 0, 20, 10), view.NeedDisplay);
  899. view.LayoutStarted += view_LayoutStarted;
  900. void view_LayoutStarted (View.LayoutEventArgs e)
  901. {
  902. Assert.Equal (new Rect (0, 0, 20, 10), view.NeedDisplay);
  903. view.LayoutStarted -= view_LayoutStarted;
  904. }
  905. Application.Begin (top);
  906. Assert.True (top.IsLoaded);
  907. Assert.True (subTop.IsLoaded);
  908. Assert.Equal (new Rect (0, 0, 20, 10), view.Frame);
  909. view.Frame = new Rect (1, 3, 10, 5);
  910. Assert.Equal (new Rect (1, 3, 10, 5), view.Frame);
  911. Assert.Equal (new Rect (0, 0, 10, 5), view.NeedDisplay);
  912. view.Redraw (view.Bounds);
  913. view.Frame = new Rect (1, 3, 10, 5);
  914. Assert.Equal (new Rect (1, 3, 10, 5), view.Frame);
  915. Assert.Equal (new Rect (0, 0, 10, 5), view.NeedDisplay);
  916. }
  917. [Fact, AutoInitShutdown]
  918. public void Single_Smaller_Top_Will_Have_Cleaning_Trails_Chunk_On_Move ()
  919. {
  920. var dialog = new Dialog ("Single smaller Dialog") { Width = 30, Height = 10 };
  921. dialog.Add (new Label (
  922. "How should I've to react. Cleaning all chunk trails or setting the 'Cols' and 'Rows' to this dialog length?\n" +
  923. "Cleaning is more easy to fix this.") {
  924. X = Pos.Center (),
  925. Y = Pos.Center (),
  926. Width = Dim.Fill (),
  927. Height = Dim.Fill (),
  928. TextAlignment = TextAlignment.Centered,
  929. VerticalTextAlignment = VerticalTextAlignment.Middle,
  930. AutoSize = false
  931. });
  932. var rs = Application.Begin (dialog);
  933. Assert.Null (Application.MouseGrabView);
  934. Assert.Equal (new Rect (25, 7, 30, 10), dialog.Frame);
  935. TestHelpers.AssertDriverContentsWithFrameAre (@"
  936. ┌ Single smaller Dialog ─────┐
  937. │ How should I've to react. │
  938. │Cleaning all chunk trails or│
  939. │ setting the 'Cols' and │
  940. │ 'Rows' to this dialog │
  941. │ length? │
  942. │Cleaning is more easy to fix│
  943. │ this. │
  944. │ │
  945. └────────────────────────────┘", output);
  946. ReflectionTools.InvokePrivate (
  947. typeof (Application),
  948. "ProcessMouseEvent",
  949. new MouseEvent () {
  950. X = 25,
  951. Y = 7,
  952. Flags = MouseFlags.Button1Pressed
  953. });
  954. var firstIteration = false;
  955. Application.RunMainLoopIteration (ref rs, true, ref firstIteration); Assert.Equal (dialog, Application.MouseGrabView);
  956. Assert.Equal (new Rect (25, 7, 30, 10), dialog.Frame);
  957. TestHelpers.AssertDriverContentsWithFrameAre (@"
  958. ┌ Single smaller Dialog ─────┐
  959. │ How should I've to react. │
  960. │Cleaning all chunk trails or│
  961. │ setting the 'Cols' and │
  962. │ 'Rows' to this dialog │
  963. │ length? │
  964. │Cleaning is more easy to fix│
  965. │ this. │
  966. │ │
  967. └────────────────────────────┘", output);
  968. ReflectionTools.InvokePrivate (
  969. typeof (Application),
  970. "ProcessMouseEvent",
  971. new MouseEvent () {
  972. X = 20,
  973. Y = 10,
  974. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  975. });
  976. firstIteration = false;
  977. Application.RunMainLoopIteration (ref rs, true, ref firstIteration);
  978. Assert.Equal (dialog, Application.MouseGrabView);
  979. Assert.Equal (new Rect (20, 10, 30, 10), dialog.Frame);
  980. TestHelpers.AssertDriverContentsWithFrameAre (@"
  981. ┌ Single smaller Dialog ─────┐
  982. │ How should I've to react. │
  983. │Cleaning all chunk trails or│
  984. │ setting the 'Cols' and │
  985. │ 'Rows' to this dialog │
  986. │ length? │
  987. │Cleaning is more easy to fix│
  988. │ this. │
  989. │ │
  990. └────────────────────────────┘", output);
  991. Application.End (rs);
  992. }
  993. }
  994. }