ToplevelTests.cs 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  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. }
  862. }