ToplevelTests.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  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 ("Dim.Fill(margin=0)", top.Width.ToString ());
  19. Assert.Equal ("Dim.Fill(margin=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(Key.CtrlMask | Key.Q, "~^Q~ 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. var top = new Toplevel ();
  130. var eventInvoked = "";
  131. top.ChildUnloaded += (e) => eventInvoked = "ChildUnloaded";
  132. top.OnChildUnloaded (top);
  133. Assert.Equal ("ChildUnloaded", eventInvoked);
  134. top.ChildLoaded += (e) => eventInvoked = "ChildLoaded";
  135. top.OnChildLoaded (top);
  136. Assert.Equal ("ChildLoaded", eventInvoked);
  137. top.Closed += (e) => eventInvoked = "Closed";
  138. top.OnClosed (top);
  139. Assert.Equal ("Closed", eventInvoked);
  140. top.Closing += (e) => eventInvoked = "Closing";
  141. top.OnClosing (new ToplevelClosingEventArgs (top));
  142. Assert.Equal ("Closing", eventInvoked);
  143. top.AllChildClosed += () => eventInvoked = "AllChildClosed";
  144. top.OnAllChildClosed ();
  145. Assert.Equal ("AllChildClosed", eventInvoked);
  146. top.ChildClosed += (e) => eventInvoked = "ChildClosed";
  147. top.OnChildClosed (top);
  148. Assert.Equal ("ChildClosed", eventInvoked);
  149. top.Deactivate += (e) => eventInvoked = "Deactivate";
  150. top.OnDeactivate (top);
  151. Assert.Equal ("Deactivate", eventInvoked);
  152. top.Activate += (e) => eventInvoked = "Activate";
  153. top.OnActivate (top);
  154. Assert.Equal ("Activate", eventInvoked);
  155. top.Loaded += () => eventInvoked = "Loaded";
  156. top.OnLoaded ();
  157. Assert.Equal ("Loaded", eventInvoked);
  158. top.Ready += () => eventInvoked = "Ready";
  159. top.OnReady ();
  160. Assert.Equal ("Ready", eventInvoked);
  161. top.Unloaded += () => eventInvoked = "Unloaded";
  162. top.OnUnloaded ();
  163. Assert.Equal ("Unloaded", eventInvoked);
  164. top.AddMenuStatusBar (new MenuBar ());
  165. Assert.NotNull (top.MenuBar);
  166. top.AddMenuStatusBar (new StatusBar ());
  167. Assert.NotNull (top.StatusBar);
  168. top.RemoveMenuStatusBar (top.MenuBar);
  169. Assert.Null (top.MenuBar);
  170. top.RemoveMenuStatusBar (top.StatusBar);
  171. Assert.Null (top.StatusBar);
  172. Application.Begin (top);
  173. Assert.Equal (top, Application.Top);
  174. // top is Application.Top without menu and status bar.
  175. var supView = top.EnsureVisibleBounds (top, 2, 2, out int nx, out int ny, out View mb, out View sb);
  176. Assert.Equal (Application.Top, supView);
  177. Assert.Equal (0, nx);
  178. Assert.Equal (0, ny);
  179. Assert.Null (mb);
  180. Assert.Null (sb);
  181. top.AddMenuStatusBar (new MenuBar ());
  182. Assert.NotNull (top.MenuBar);
  183. // top is Application.Top with a menu and without status bar.
  184. top.EnsureVisibleBounds (top, 2, 2, out nx, out ny, out mb, out sb);
  185. Assert.Equal (0, nx);
  186. Assert.Equal (1, ny);
  187. Assert.NotNull (mb);
  188. Assert.Null (sb);
  189. top.AddMenuStatusBar (new StatusBar ());
  190. Assert.NotNull (top.StatusBar);
  191. // top is Application.Top with a menu and status bar.
  192. top.EnsureVisibleBounds (top, 2, 2, out nx, out ny, out mb, out sb);
  193. Assert.Equal (0, nx);
  194. Assert.Equal (1, ny);
  195. Assert.NotNull (mb);
  196. Assert.NotNull (sb);
  197. top.RemoveMenuStatusBar (top.MenuBar);
  198. Assert.Null (top.MenuBar);
  199. // top is Application.Top without a menu and with a status bar.
  200. top.EnsureVisibleBounds (top, 2, 2, out nx, out ny, out mb, out sb);
  201. Assert.Equal (0, nx);
  202. Assert.Equal (0, ny);
  203. Assert.Null (mb);
  204. Assert.NotNull (sb);
  205. top.RemoveMenuStatusBar (top.StatusBar);
  206. Assert.Null (top.StatusBar);
  207. Assert.Null (top.MenuBar);
  208. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  209. top.Add (win);
  210. top.LayoutSubviews ();
  211. // The SuperView is always the same regardless of the caller.
  212. supView = top.EnsureVisibleBounds (win, 0, 0, out nx, out ny, out mb, out sb);
  213. Assert.Equal (Application.Top, supView);
  214. supView = win.EnsureVisibleBounds (win, 0, 0, out nx, out ny, out mb, out sb);
  215. Assert.Equal (Application.Top, supView);
  216. // top is Application.Top without menu and status bar.
  217. top.EnsureVisibleBounds (win, 0, 0, out nx, out ny, out mb, out sb);
  218. Assert.Equal (0, nx);
  219. Assert.Equal (0, ny);
  220. Assert.Null (mb);
  221. Assert.Null (sb);
  222. top.AddMenuStatusBar (new MenuBar ());
  223. Assert.NotNull (top.MenuBar);
  224. // top is Application.Top with a menu and without status bar.
  225. top.EnsureVisibleBounds (win, 2, 2, out nx, out ny, out mb, out sb);
  226. Assert.Equal (0, nx);
  227. Assert.Equal (1, ny);
  228. Assert.NotNull (mb);
  229. Assert.Null (sb);
  230. top.AddMenuStatusBar (new StatusBar ());
  231. Assert.NotNull (top.StatusBar);
  232. // top is Application.Top with a menu and status bar.
  233. top.EnsureVisibleBounds (win, 30, 20, out nx, out ny, out mb, out sb);
  234. Assert.Equal (0, nx);
  235. Assert.Equal (1, ny);
  236. Assert.NotNull (mb);
  237. Assert.NotNull (sb);
  238. top.RemoveMenuStatusBar (top.MenuBar);
  239. top.RemoveMenuStatusBar (top.StatusBar);
  240. Assert.Null (top.StatusBar);
  241. Assert.Null (top.MenuBar);
  242. top.Remove (win);
  243. win = new Window () { Width = 60, Height = 15 };
  244. top.Add (win);
  245. // top is Application.Top without menu and status bar.
  246. top.EnsureVisibleBounds (win, 0, 0, out nx, out ny, out mb, out sb);
  247. Assert.Equal (0, nx);
  248. Assert.Equal (0, ny);
  249. Assert.Null (mb);
  250. Assert.Null (sb);
  251. top.AddMenuStatusBar (new MenuBar ());
  252. Assert.NotNull (top.MenuBar);
  253. // top is Application.Top with a menu and without status bar.
  254. top.EnsureVisibleBounds (win, 2, 2, out nx, out ny, out mb, out sb);
  255. Assert.Equal (2, nx);
  256. Assert.Equal (2, ny);
  257. Assert.NotNull (mb);
  258. Assert.Null (sb);
  259. top.AddMenuStatusBar (new StatusBar ());
  260. Assert.NotNull (top.StatusBar);
  261. // top is Application.Top with a menu and status bar.
  262. top.EnsureVisibleBounds (win, 30, 20, out nx, out ny, out mb, out sb);
  263. Assert.Equal (20, nx); // 20+60=80
  264. Assert.Equal (9, ny); // 9+15+1(mb)=25
  265. Assert.NotNull (mb);
  266. Assert.NotNull (sb);
  267. top.PositionToplevels ();
  268. Assert.Equal (new Rect (0, 1, 60, 15), win.Frame);
  269. Assert.Null (Toplevel.dragPosition);
  270. win.MouseEvent (new MouseEvent () { X = 6, Y = 0, Flags = MouseFlags.Button1Pressed });
  271. Assert.Equal (new Point (6, 0), Toplevel.dragPosition);
  272. win.MouseEvent (new MouseEvent () { X = 6, Y = 0, Flags = MouseFlags.Button1Released });
  273. Assert.Null (Toplevel.dragPosition);
  274. win.CanFocus = false;
  275. win.MouseEvent (new MouseEvent () { X = 6, Y = 0, Flags = MouseFlags.Button1Pressed });
  276. Assert.Null (Toplevel.dragPosition);
  277. }
  278. [Fact]
  279. [AutoInitShutdown]
  280. public void KeyBindings_Command ()
  281. {
  282. var isRunning = false;
  283. var win1 = new Window ("Win1") { Width = Dim.Percent (50f), Height = Dim.Fill () };
  284. var lblTf1W1 = new Label ("Enter text in TextField on Win1:");
  285. var tf1W1 = new TextField ("Text1 on Win1") { X = Pos.Right (lblTf1W1) + 1, Width = Dim.Fill () };
  286. var lblTvW1 = new Label ("Enter text in TextView on Win1:") { Y = Pos.Bottom (lblTf1W1) + 1 };
  287. var tvW1 = new TextView () { X = Pos.Left (tf1W1), Width = Dim.Fill (), Height = 2, Text = "First line Win1\nSecond line Win1" };
  288. var lblTf2W1 = new Label ("Enter text in TextField on Win1:") { Y = Pos.Bottom (lblTvW1) + 1 };
  289. var tf2W1 = new TextField ("Text2 on Win1") { X = Pos.Left (tf1W1), Width = Dim.Fill () };
  290. win1.Add (lblTf1W1, tf1W1, lblTvW1, tvW1, lblTf2W1, tf2W1);
  291. var win2 = new Window ("Win2") { X = Pos.Right (win1) + 1, Width = Dim.Percent (50f), Height = Dim.Fill () };
  292. var lblTf1W2 = new Label ("Enter text in TextField on Win2:");
  293. var tf1W2 = new TextField ("Text1 on Win2") { X = Pos.Right (lblTf1W2) + 1, Width = Dim.Fill () };
  294. var lblTvW2 = new Label ("Enter text in TextView on Win2:") { Y = Pos.Bottom (lblTf1W2) + 1 };
  295. var tvW2 = new TextView () { X = Pos.Left (tf1W2), Width = Dim.Fill (), Height = 2, Text = "First line Win1\nSecond line Win2" };
  296. var lblTf2W2 = new Label ("Enter text in TextField on Win2:") { Y = Pos.Bottom (lblTvW2) + 1 };
  297. var tf2W2 = new TextField ("Text2 on Win2") { X = Pos.Left (tf1W2), Width = Dim.Fill () };
  298. win2.Add (lblTf1W2, tf1W2, lblTvW2, tvW2, lblTf2W2, tf2W2);
  299. var top = Application.Top;
  300. top.Add (win1, win2);
  301. top.Loaded += () => isRunning = true;
  302. top.Closing += (_) => isRunning = false;
  303. Application.Begin (top);
  304. top.Running = true;
  305. Assert.Equal (new Rect (0, 0, 40, 25), win1.Frame);
  306. Assert.Equal (new Rect (41, 0, 40, 25), win2.Frame);
  307. Assert.Equal (win1, top.Focused);
  308. Assert.Equal (tf1W1, top.MostFocused);
  309. Assert.True (isRunning);
  310. Assert.True (top.Focused.ProcessKey (new KeyEvent (Application.QuitKey, new KeyModifiers ())));
  311. Assert.False (isRunning);
  312. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Z | Key.CtrlMask, new KeyModifiers ())));
  313. Assert.False (top.Focused.ProcessKey (new KeyEvent (Key.F5, new KeyModifiers ())));
  314. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  315. Assert.Equal (win1, top.Focused);
  316. Assert.Equal (tvW1, top.MostFocused);
  317. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  318. Assert.Equal ($"\tFirst line Win1{Environment.NewLine}Second line Win1", tvW1.Text);
  319. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ())));
  320. Assert.Equal ($"First line Win1{Environment.NewLine}Second line Win1", tvW1.Text);
  321. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers ())));
  322. Assert.Equal (win1, top.Focused);
  323. Assert.Equal (tf2W1, top.MostFocused);
  324. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  325. Assert.Equal (win1, top.Focused);
  326. Assert.Equal (tf1W1, top.MostFocused);
  327. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  328. Assert.Equal (win1, top.Focused);
  329. Assert.Equal (tf1W1, top.MostFocused);
  330. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  331. Assert.Equal (win1, top.Focused);
  332. Assert.Equal (tvW1, top.MostFocused);
  333. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.I | Key.CtrlMask, new KeyModifiers ())));
  334. Assert.Equal (win1, top.Focused);
  335. Assert.Equal (tf2W1, top.MostFocused);
  336. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ())));
  337. Assert.Equal (win1, top.Focused);
  338. Assert.Equal (tvW1, top.MostFocused);
  339. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ())));
  340. Assert.Equal (win1, top.Focused);
  341. Assert.Equal (tf1W1, top.MostFocused);
  342. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ())));
  343. Assert.Equal (win1, top.Focused);
  344. Assert.Equal (tf2W1, top.MostFocused);
  345. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers ())));
  346. Assert.Equal (win2, top.Focused);
  347. Assert.Equal (tf1W2, top.MostFocused);
  348. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask | Key.ShiftMask, new KeyModifiers ())));
  349. Assert.Equal (win1, top.Focused);
  350. Assert.Equal (tf2W1, top.MostFocused);
  351. Assert.True (top.Focused.ProcessKey (new KeyEvent (Application.AlternateForwardKey, new KeyModifiers ())));
  352. Assert.Equal (win2, top.Focused);
  353. Assert.Equal (tf1W2, top.MostFocused);
  354. Assert.True (top.Focused.ProcessKey (new KeyEvent (Application.AlternateBackwardKey, new KeyModifiers ())));
  355. Assert.Equal (win1, top.Focused);
  356. Assert.Equal (tf2W1, top.MostFocused);
  357. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ())));
  358. Assert.Equal (win1, top.Focused);
  359. Assert.Equal (tvW1, top.MostFocused);
  360. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.B | Key.CtrlMask, new KeyModifiers ())));
  361. Assert.Equal (win1, top.Focused);
  362. Assert.Equal (tf1W1, top.MostFocused);
  363. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  364. Assert.Equal (win1, top.Focused);
  365. Assert.Equal (tvW1, top.MostFocused);
  366. Assert.Equal (new Point (0, 0), tvW1.CursorPosition);
  367. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.End | Key.CtrlMask, new KeyModifiers ())));
  368. Assert.Equal (win1, top.Focused);
  369. Assert.Equal (tvW1, top.MostFocused);
  370. Assert.Equal (new Point (16, 1), tvW1.CursorPosition);
  371. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.F | Key.CtrlMask, new KeyModifiers ())));
  372. Assert.Equal (win1, top.Focused);
  373. Assert.Equal (tf2W1, top.MostFocused);
  374. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.L | Key.CtrlMask, new KeyModifiers ())));
  375. }
  376. [Fact]
  377. [AutoInitShutdown]
  378. public void KeyBindings_Command_With_MdiTop ()
  379. {
  380. var top = Application.Top;
  381. Assert.Null (Application.MdiTop);
  382. top.IsMdiContainer = true;
  383. Application.Begin (top);
  384. Assert.Equal (Application.Top, Application.MdiTop);
  385. var isRunning = true;
  386. var win1 = new Window ("Win1") { Width = Dim.Percent (50f), Height = Dim.Fill () };
  387. var lblTf1W1 = new Label ("Enter text in TextField on Win1:");
  388. var tf1W1 = new TextField ("Text1 on Win1") { X = Pos.Right (lblTf1W1) + 1, Width = Dim.Fill () };
  389. var lblTvW1 = new Label ("Enter text in TextView on Win1:") { Y = Pos.Bottom (lblTf1W1) + 1 };
  390. var tvW1 = new TextView () { X = Pos.Left (tf1W1), Width = Dim.Fill (), Height = 2, Text = "First line Win1\nSecond line Win1" };
  391. var lblTf2W1 = new Label ("Enter text in TextField on Win1:") { Y = Pos.Bottom (lblTvW1) + 1 };
  392. var tf2W1 = new TextField ("Text2 on Win1") { X = Pos.Left (tf1W1), Width = Dim.Fill () };
  393. win1.Add (lblTf1W1, tf1W1, lblTvW1, tvW1, lblTf2W1, tf2W1);
  394. var win2 = new Window ("Win2") { Width = Dim.Percent (50f), Height = Dim.Fill () };
  395. var lblTf1W2 = new Label ("Enter text in TextField on Win2:");
  396. var tf1W2 = new TextField ("Text1 on Win2") { X = Pos.Right (lblTf1W2) + 1, Width = Dim.Fill () };
  397. var lblTvW2 = new Label ("Enter text in TextView on Win2:") { Y = Pos.Bottom (lblTf1W2) + 1 };
  398. var tvW2 = new TextView () { X = Pos.Left (tf1W2), Width = Dim.Fill (), Height = 2, Text = "First line Win1\nSecond line Win2" };
  399. var lblTf2W2 = new Label ("Enter text in TextField on Win2:") { Y = Pos.Bottom (lblTvW2) + 1 };
  400. var tf2W2 = new TextField ("Text2 on Win2") { X = Pos.Left (tf1W2), Width = Dim.Fill () };
  401. win2.Add (lblTf1W2, tf1W2, lblTvW2, tvW2, lblTf2W2, tf2W2);
  402. win1.Closing += (_) => isRunning = false;
  403. Assert.Null (top.Focused);
  404. Assert.Equal (top, Application.Current);
  405. Assert.True (top.IsCurrentTop);
  406. Assert.Equal (top, Application.MdiTop);
  407. Application.Begin (win1);
  408. Assert.Equal (new Rect (0, 0, 40, 25), win1.Frame);
  409. Assert.NotEqual (top, Application.Current);
  410. Assert.False (top.IsCurrentTop);
  411. Assert.Equal (win1, Application.Current);
  412. Assert.True (win1.IsCurrentTop);
  413. Assert.True (win1.IsMdiChild);
  414. Assert.Null (top.Focused);
  415. Assert.Null (top.MostFocused);
  416. Assert.Equal (win1.Subviews [0], win1.Focused);
  417. Assert.Equal (tf1W1, win1.MostFocused);
  418. Assert.True (win1.IsMdiChild);
  419. Assert.Single (Application.MdiChildes);
  420. Application.Begin (win2);
  421. Assert.Equal (new Rect (0, 0, 40, 25), win2.Frame);
  422. Assert.NotEqual (top, Application.Current);
  423. Assert.False (top.IsCurrentTop);
  424. Assert.Equal (win2, Application.Current);
  425. Assert.True (win2.IsCurrentTop);
  426. Assert.True (win2.IsMdiChild);
  427. Assert.Null (top.Focused);
  428. Assert.Null (top.MostFocused);
  429. Assert.Equal (win2.Subviews [0], win2.Focused);
  430. Assert.Equal (tf1W2, win2.MostFocused);
  431. Assert.Equal (2, Application.MdiChildes.Count);
  432. Application.ShowChild (win1);
  433. Assert.Equal (win1, Application.Current);
  434. Assert.Equal (win1, Application.MdiChildes [0]);
  435. win1.Running = true;
  436. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Application.QuitKey, new KeyModifiers ())));
  437. Assert.False (isRunning);
  438. Assert.False (win1.Running);
  439. Assert.Equal (win1, Application.MdiChildes [0]);
  440. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Z | Key.CtrlMask, new KeyModifiers ())));
  441. Assert.False (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.F5, new KeyModifiers ())));
  442. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  443. Assert.True (win1.IsCurrentTop);
  444. Assert.Equal (tvW1, win1.MostFocused);
  445. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  446. Assert.Equal ($"\tFirst line Win1{Environment.NewLine}Second line Win1", tvW1.Text);
  447. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ())));
  448. Assert.Equal ($"First line Win1{Environment.NewLine}Second line Win1", tvW1.Text);
  449. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers ())));
  450. Assert.Equal (win1, Application.MdiChildes [0]);
  451. Assert.Equal (tf2W1, win1.MostFocused);
  452. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  453. Assert.Equal (win1, Application.MdiChildes [0]);
  454. Assert.Equal (tf1W1, win1.MostFocused);
  455. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  456. Assert.Equal (win1, Application.MdiChildes [0]);
  457. Assert.Equal (tf1W1, win1.MostFocused);
  458. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  459. Assert.Equal (win1, Application.MdiChildes [0]);
  460. Assert.Equal (tvW1, win1.MostFocused);
  461. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.I | Key.CtrlMask, new KeyModifiers ())));
  462. Assert.Equal (win1, Application.MdiChildes [0]);
  463. Assert.Equal (tf2W1, win1.MostFocused);
  464. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ())));
  465. Assert.Equal (win1, Application.MdiChildes [0]);
  466. Assert.Equal (tvW1, win1.MostFocused);
  467. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ())));
  468. Assert.Equal (win1, Application.MdiChildes [0]);
  469. Assert.Equal (tf1W1, win1.MostFocused);
  470. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ())));
  471. Assert.Equal (win1, Application.MdiChildes [0]);
  472. Assert.Equal (tf2W1, win1.MostFocused);
  473. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  474. Assert.Equal (win1, Application.MdiChildes [0]);
  475. Assert.Equal (tf1W1, win1.MostFocused);
  476. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers ())));
  477. Assert.Equal (win2, Application.MdiChildes [0]);
  478. Assert.Equal (tf1W2, win2.MostFocused);
  479. tf2W2.SetFocus ();
  480. Assert.True (tf2W2.HasFocus);
  481. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask | Key.ShiftMask, new KeyModifiers ())));
  482. Assert.Equal (win1, Application.MdiChildes [0]);
  483. Assert.Equal (tf1W1, win1.MostFocused);
  484. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Application.AlternateForwardKey, new KeyModifiers ())));
  485. Assert.Equal (win2, Application.MdiChildes [0]);
  486. Assert.Equal (tf2W2, win2.MostFocused);
  487. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Application.AlternateBackwardKey, new KeyModifiers ())));
  488. Assert.Equal (win1, Application.MdiChildes [0]);
  489. Assert.Equal (tf1W1, win1.MostFocused);
  490. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  491. Assert.Equal (win1, Application.MdiChildes [0]);
  492. Assert.Equal (tvW1, win1.MostFocused);
  493. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.B | Key.CtrlMask, new KeyModifiers ())));
  494. Assert.Equal (win1, Application.MdiChildes [0]);
  495. Assert.Equal (tf1W1, win1.MostFocused);
  496. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  497. Assert.Equal (win1, Application.MdiChildes [0]);
  498. Assert.Equal (tvW1, win1.MostFocused);
  499. Assert.Equal (new Point (0, 0), tvW1.CursorPosition);
  500. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.End | Key.CtrlMask, new KeyModifiers ())));
  501. Assert.Equal (win1, Application.MdiChildes [0]);
  502. Assert.Equal (tvW1, win1.MostFocused);
  503. Assert.Equal (new Point (16, 1), tvW1.CursorPosition);
  504. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.F | Key.CtrlMask, new KeyModifiers ())));
  505. Assert.Equal (win1, Application.MdiChildes [0]);
  506. Assert.Equal (tf2W1, win1.MostFocused);
  507. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.L | Key.CtrlMask, new KeyModifiers ())));
  508. }
  509. [Fact]
  510. public void Added_Event_Should_Not_Be_Used_To_Initialize_Toplevel_Events ()
  511. {
  512. Key alternateForwardKey = default;
  513. Key alternateBackwardKey = default;
  514. Key quitKey = default;
  515. var wasAdded = false;
  516. var view = new View ();
  517. view.Added += View_Added;
  518. void View_Added (View obj)
  519. {
  520. Assert.Throws<NullReferenceException> (() => Application.Top.AlternateForwardKeyChanged += (e) => alternateForwardKey = e);
  521. Assert.Throws<NullReferenceException> (() => Application.Top.AlternateBackwardKeyChanged += (e) => alternateBackwardKey = e);
  522. Assert.Throws<NullReferenceException> (() => Application.Top.QuitKeyChanged += (e) => quitKey = e);
  523. Assert.False (wasAdded);
  524. wasAdded = true;
  525. view.Added -= View_Added;
  526. }
  527. var win = new Window ();
  528. win.Add (view);
  529. Application.Init (new FakeDriver ());
  530. var top = Application.Top;
  531. top.Add (win);
  532. Assert.True (wasAdded);
  533. Application.Shutdown ();
  534. }
  535. [Fact]
  536. [AutoInitShutdown]
  537. public void AlternateForwardKeyChanged_AlternateBackwardKeyChanged_QuitKeyChanged_Events ()
  538. {
  539. Key alternateForwardKey = default;
  540. Key alternateBackwardKey = default;
  541. Key quitKey = default;
  542. var view = new View ();
  543. view.Initialized += View_Initialized;
  544. void View_Initialized (object sender, EventArgs e)
  545. {
  546. Application.Top.AlternateForwardKeyChanged += (e) => alternateForwardKey = e;
  547. Application.Top.AlternateBackwardKeyChanged += (e) => alternateBackwardKey = e;
  548. Application.Top.QuitKeyChanged += (e) => quitKey = e;
  549. }
  550. var win = new Window ();
  551. win.Add (view);
  552. var top = Application.Top;
  553. top.Add (win);
  554. Application.Begin (top);
  555. Assert.Equal (Key.Null, alternateForwardKey);
  556. Assert.Equal (Key.Null, alternateBackwardKey);
  557. Assert.Equal (Key.Null, quitKey);
  558. Assert.Equal (Key.PageDown | Key.CtrlMask, Application.AlternateForwardKey);
  559. Assert.Equal (Key.PageUp | Key.CtrlMask, Application.AlternateBackwardKey);
  560. Assert.Equal (Key.Q | Key.CtrlMask, Application.QuitKey);
  561. Application.AlternateForwardKey = Key.A;
  562. Application.AlternateBackwardKey = Key.B;
  563. Application.QuitKey = Key.C;
  564. Assert.Equal (Key.PageDown | Key.CtrlMask, alternateForwardKey);
  565. Assert.Equal (Key.PageUp | Key.CtrlMask, alternateBackwardKey);
  566. Assert.Equal (Key.Q | Key.CtrlMask, quitKey);
  567. Assert.Equal (Key.A, Application.AlternateForwardKey);
  568. Assert.Equal (Key.B, Application.AlternateBackwardKey);
  569. Assert.Equal (Key.C, Application.QuitKey);
  570. // Replacing the defaults keys to avoid errors on others unit tests that are using it.
  571. Application.AlternateForwardKey = Key.PageDown | Key.CtrlMask;
  572. Application.AlternateBackwardKey = Key.PageUp | Key.CtrlMask;
  573. Application.QuitKey = Key.Q | Key.CtrlMask;
  574. Assert.Equal (Key.PageDown | Key.CtrlMask, Application.AlternateForwardKey);
  575. Assert.Equal (Key.PageUp | Key.CtrlMask, Application.AlternateBackwardKey);
  576. Assert.Equal (Key.Q | Key.CtrlMask, Application.QuitKey);
  577. }
  578. [Fact]
  579. [AutoInitShutdown]
  580. public void FileDialog_FileSystemWatcher ()
  581. {
  582. for (int i = 0; i < 8; i++) {
  583. var fd = new FileDialog ();
  584. fd.Ready += () => Application.RequestStop ();
  585. Application.Run (fd);
  586. }
  587. }
  588. [Fact, AutoInitShutdown]
  589. public void Mouse_Drag_On_Top_With_Superview_Null ()
  590. {
  591. var menu = new MenuBar (new MenuBarItem [] {
  592. new MenuBarItem("File", new MenuItem [] {
  593. new MenuItem("New", "", null)
  594. })
  595. });
  596. var sbar = new StatusBar (new StatusItem [] {
  597. new StatusItem(Key.N, "~CTRL-N~ New", null)
  598. });
  599. var win = new Window ("Window");
  600. var top = Application.Top;
  601. top.Add (menu, sbar, win);
  602. var iterations = -1;
  603. Application.Iteration = () => {
  604. iterations++;
  605. if (iterations == 0) {
  606. ((FakeDriver)Application.Driver).SetBufferSize (40, 15);
  607. MessageBox.Query ("About", "Hello Word", "Ok");
  608. } else if (iterations == 1) TestHelpers.AssertDriverContentsWithFrameAre (@"
  609. File
  610. ┌ Window ──────────────────────────────┐
  611. │ │
  612. │ │
  613. │ │
  614. │ ┌ About ───────────────┐ │
  615. │ │ Hello Word │ │
  616. │ │ │ │
  617. │ │ [◦ Ok ◦] │ │
  618. │ └──────────────────────┘ │
  619. │ │
  620. │ │
  621. │ │
  622. └──────────────────────────────────────┘
  623. CTRL-N New ", output);
  624. else if (iterations == 2) {
  625. Assert.Null (Application.MouseGrabView);
  626. // Grab the mouse
  627. ReflectionTools.InvokePrivate (
  628. typeof (Application),
  629. "ProcessMouseEvent",
  630. new MouseEvent () {
  631. X = 8,
  632. Y = 5,
  633. Flags = MouseFlags.Button1Pressed
  634. });
  635. Assert.Equal (Application.Current, Application.MouseGrabView);
  636. Assert.Equal (new Rect (8, 5, 24, 5), Application.MouseGrabView.Frame);
  637. } else if (iterations == 3) {
  638. Assert.Equal (Application.Current, Application.MouseGrabView);
  639. // Grab to left
  640. ReflectionTools.InvokePrivate (
  641. typeof (Application),
  642. "ProcessMouseEvent",
  643. new MouseEvent () {
  644. X = 7,
  645. Y = 5,
  646. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  647. });
  648. Assert.Equal (Application.Current, Application.MouseGrabView);
  649. Assert.Equal (new Rect (7, 5, 24, 5), Application.MouseGrabView.Frame);
  650. } else if (iterations == 4) {
  651. Assert.Equal (Application.Current, Application.MouseGrabView);
  652. TestHelpers.AssertDriverContentsWithFrameAre (@"
  653. File
  654. ┌ Window ──────────────────────────────┐
  655. │ │
  656. │ │
  657. │ │
  658. │ ┌ About ───────────────┐ │
  659. │ │ Hello Word │ │
  660. │ │ │ │
  661. │ │ [◦ Ok ◦] │ │
  662. │ └──────────────────────┘ │
  663. │ │
  664. │ │
  665. │ │
  666. └──────────────────────────────────────┘
  667. CTRL-N New ", output);
  668. Assert.Equal (Application.Current, Application.MouseGrabView);
  669. } else if (iterations == 5) {
  670. Assert.Equal (Application.Current, Application.MouseGrabView);
  671. // Grab to top
  672. ReflectionTools.InvokePrivate (
  673. typeof (Application),
  674. "ProcessMouseEvent",
  675. new MouseEvent () {
  676. X = 7,
  677. Y = 4,
  678. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  679. });
  680. Assert.Equal (Application.Current, Application.MouseGrabView);
  681. Assert.Equal (new Rect (7, 4, 24, 5), Application.MouseGrabView.Frame);
  682. } else if (iterations == 6) {
  683. Assert.Equal (Application.Current, Application.MouseGrabView);
  684. TestHelpers.AssertDriverContentsWithFrameAre (@"
  685. File
  686. ┌ Window ──────────────────────────────┐
  687. │ │
  688. │ │
  689. │ ┌ About ───────────────┐ │
  690. │ │ Hello Word │ │
  691. │ │ │ │
  692. │ │ [◦ Ok ◦] │ │
  693. │ └──────────────────────┘ │
  694. │ │
  695. │ │
  696. │ │
  697. │ │
  698. └──────────────────────────────────────┘
  699. CTRL-N New ", output);
  700. Assert.Equal (Application.Current, Application.MouseGrabView);
  701. Assert.Equal (new Rect (7, 4, 24, 5), Application.MouseGrabView.Frame);
  702. } else if (iterations == 7) {
  703. Assert.Equal (Application.Current, Application.MouseGrabView);
  704. // Ungrab the mouse
  705. ReflectionTools.InvokePrivate (
  706. typeof (Application),
  707. "ProcessMouseEvent",
  708. new MouseEvent () {
  709. X = 7,
  710. Y = 4,
  711. Flags = MouseFlags.Button1Released
  712. });
  713. Assert.Null (Application.MouseGrabView);
  714. } else if (iterations == 8) Application.RequestStop ();
  715. else if (iterations == 9) Application.RequestStop ();
  716. };
  717. Application.Run ();
  718. }
  719. [Fact, AutoInitShutdown]
  720. public void Mouse_Drag_On_Top_With_Superview_Not_Null ()
  721. {
  722. var menu = new MenuBar (new MenuBarItem [] {
  723. new MenuBarItem("File", new MenuItem [] {
  724. new MenuItem("New", "", null)
  725. })
  726. });
  727. var sbar = new StatusBar (new StatusItem [] {
  728. new StatusItem(Key.N, "~CTRL-N~ New", null)
  729. });
  730. var win = new Window ("Window") {
  731. X = 3,
  732. Y = 2,
  733. Width = Dim.Fill (10),
  734. Height = Dim.Fill (5)
  735. };
  736. var top = Application.Top;
  737. top.Add (menu, sbar, win);
  738. var iterations = -1;
  739. Application.Iteration = () => {
  740. iterations++;
  741. if (iterations == 0) {
  742. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  743. Assert.Null (Application.MouseGrabView);
  744. // Grab the mouse
  745. ReflectionTools.InvokePrivate (
  746. typeof (Application),
  747. "ProcessMouseEvent",
  748. new MouseEvent () {
  749. X = 4,
  750. Y = 2,
  751. Flags = MouseFlags.Button1Pressed
  752. });
  753. Assert.Equal (win, Application.MouseGrabView);
  754. Assert.Equal (new Rect (3, 2, 7, 3), Application.MouseGrabView.Frame);
  755. TestHelpers.AssertDriverContentsWithFrameAre (@"
  756. File
  757. ┌─────┐
  758. │ │
  759. └─────┘
  760. CTRL-N New", output);
  761. } else if (iterations == 1) {
  762. Assert.Equal (win, Application.MouseGrabView);
  763. // Grab to left
  764. ReflectionTools.InvokePrivate (
  765. typeof (Application),
  766. "ProcessMouseEvent",
  767. new MouseEvent () {
  768. X = 5,
  769. Y = 2,
  770. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  771. });
  772. Assert.Equal (win, Application.MouseGrabView);
  773. } else if (iterations == 2) {
  774. Assert.Equal (win, Application.MouseGrabView);
  775. TestHelpers.AssertDriverContentsWithFrameAre (@"
  776. File
  777. ┌────┐
  778. │ │
  779. └────┘
  780. CTRL-N New", output);
  781. Assert.Equal (win, Application.MouseGrabView);
  782. Assert.Equal (new Rect (4, 2, 6, 3), Application.MouseGrabView.Frame);
  783. } else if (iterations == 3) {
  784. Assert.Equal (win, Application.MouseGrabView);
  785. // Grab to top
  786. ReflectionTools.InvokePrivate (
  787. typeof (Application),
  788. "ProcessMouseEvent",
  789. new MouseEvent () {
  790. X = 5,
  791. Y = 1,
  792. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  793. });
  794. Assert.Equal (win, Application.MouseGrabView);
  795. } else if (iterations == 4) {
  796. Assert.Equal (win, Application.MouseGrabView);
  797. TestHelpers.AssertDriverContentsWithFrameAre (@"
  798. File
  799. ┌────┐
  800. │ │
  801. │ │
  802. └────┘
  803. CTRL-N New", output);
  804. Assert.Equal (win, Application.MouseGrabView);
  805. Assert.Equal (new Rect (4, 1, 6, 4), Application.MouseGrabView.Frame);
  806. } else if (iterations == 5) {
  807. Assert.Equal (win, Application.MouseGrabView);
  808. // Ungrab the mouse
  809. ReflectionTools.InvokePrivate (
  810. typeof (Application),
  811. "ProcessMouseEvent",
  812. new MouseEvent () {
  813. X = 7,
  814. Y = 4,
  815. Flags = MouseFlags.Button1Released
  816. });
  817. Assert.Null (Application.MouseGrabView);
  818. } else if (iterations == 8) Application.RequestStop ();
  819. };
  820. Application.Run ();
  821. }
  822. [Fact, AutoInitShutdown]
  823. public void EnsureVisibleBounds_With_Border_Null_Not_Throws ()
  824. {
  825. var top = new Toplevel ();
  826. Application.Begin (top);
  827. var exception = Record.Exception (() => ((FakeDriver)Application.Driver).SetBufferSize (0, 10));
  828. Assert.Null (exception);
  829. exception = Record.Exception (() => ((FakeDriver)Application.Driver).SetBufferSize (10, 0));
  830. Assert.Null (exception);
  831. }
  832. [Fact, AutoInitShutdown]
  833. public void OnEnter_OnLeave_Triggered_On_Application_Begin_End ()
  834. {
  835. var isEnter = false;
  836. var isLeave = false;
  837. var v = new View ();
  838. v.Enter += (_) => isEnter = true;
  839. v.Leave += (_) => isLeave = true;
  840. var top = Application.Top;
  841. top.Add (v);
  842. Assert.False (v.CanFocus);
  843. var exception = Record.Exception (() => top.OnEnter (top));
  844. Assert.Null (exception);
  845. exception = Record.Exception (() => top.OnLeave (top));
  846. Assert.Null (exception);
  847. v.CanFocus = true;
  848. Application.Begin (top);
  849. Assert.True (isEnter);
  850. Assert.False (isLeave);
  851. isEnter = false;
  852. var d = new Dialog ();
  853. var rs = Application.Begin (d);
  854. Assert.False (isEnter);
  855. Assert.True (isLeave);
  856. isLeave = false;
  857. Application.End (rs);
  858. Assert.True (isEnter);
  859. Assert.False (isLeave);
  860. }
  861. [Fact, AutoInitShutdown]
  862. public void PositionCursor_SetCursorVisibility_To_Invisible_If_Focused_Is_Null ()
  863. {
  864. var tf = new TextField ("test") { Width = 5 };
  865. var view = new View () { Width = 10, Height = 10 };
  866. view.Add (tf);
  867. Application.Top.Add (view);
  868. Application.Begin (Application.Top);
  869. Assert.True (tf.HasFocus);
  870. Application.Driver.GetCursorVisibility (out CursorVisibility cursor);
  871. Assert.Equal (CursorVisibility.Default, cursor);
  872. view.Enabled = false;
  873. Assert.False (tf.HasFocus);
  874. Application.Refresh ();
  875. Application.Driver.GetCursorVisibility (out cursor);
  876. Assert.Equal (CursorVisibility.Invisible, cursor);
  877. }
  878. }
  879. }