ToplevelTests.cs 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702
  1. using System;
  2. using Terminal.Gui;
  3. using Xunit;
  4. using Xunit.Abstractions;
  5. namespace Terminal.Gui.ViewsTests {
  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.IsOverlappedContainer);
  25. Assert.False (top.IsOverlapped);
  26. }
  27. [Fact]
  28. [AutoInitShutdown]
  29. public void Create_Toplevel ()
  30. {
  31. var top = Toplevel.Create ();
  32. top.BeginInit ();
  33. top.EndInit ();
  34. Assert.Equal (new Rect (0, 0, Application.Driver.Cols, Application.Driver.Rows), top.Bounds);
  35. }
  36. [Fact]
  37. [AutoInitShutdown]
  38. public void Application_Top_GetLocationThatFits_To_Driver_Rows_And_Cols ()
  39. {
  40. var iterations = 0;
  41. Application.Iteration += () => {
  42. if (iterations == 0) {
  43. Assert.False (Application.Top.AutoSize);
  44. Assert.Equal ("Top1", Application.Top.Text);
  45. Assert.Equal (0, Application.Top.Frame.X);
  46. Assert.Equal (0, Application.Top.Frame.Y);
  47. Assert.Equal (Application.Driver.Cols, Application.Top.Frame.Width);
  48. Assert.Equal (Application.Driver.Rows, Application.Top.Frame.Height);
  49. Application.Top.ProcessHotKey (new KeyEvent (Key.CtrlMask | Key.R, new KeyModifiers ()));
  50. } else if (iterations == 1) {
  51. Assert.Equal ("Top2", Application.Top.Text);
  52. Assert.Equal (0, Application.Top.Frame.X);
  53. Assert.Equal (0, Application.Top.Frame.Y);
  54. Assert.Equal (Application.Driver.Cols, Application.Top.Frame.Width);
  55. Assert.Equal (Application.Driver.Rows, Application.Top.Frame.Height);
  56. Application.Top.ProcessHotKey (new KeyEvent (Key.CtrlMask | Key.C, new KeyModifiers ()));
  57. } else if (iterations == 3) {
  58. Assert.Equal ("Top1", Application.Top.Text);
  59. Assert.Equal (0, Application.Top.Frame.X);
  60. Assert.Equal (0, Application.Top.Frame.Y);
  61. Assert.Equal (Application.Driver.Cols, Application.Top.Frame.Width);
  62. Assert.Equal (Application.Driver.Rows, Application.Top.Frame.Height);
  63. Application.Top.ProcessHotKey (new KeyEvent (Key.CtrlMask | Key.R, new KeyModifiers ()));
  64. } else if (iterations == 4) {
  65. Assert.Equal ("Top2", Application.Top.Text);
  66. Assert.Equal (0, Application.Top.Frame.X);
  67. Assert.Equal (0, Application.Top.Frame.Y);
  68. Assert.Equal (Application.Driver.Cols, Application.Top.Frame.Width);
  69. Assert.Equal (Application.Driver.Rows, Application.Top.Frame.Height);
  70. Application.Top.ProcessHotKey (new KeyEvent (Key.CtrlMask | Key.C, new KeyModifiers ()));
  71. } else if (iterations == 6) {
  72. Assert.Equal ("Top1", Application.Top.Text);
  73. Assert.Equal (0, Application.Top.Frame.X);
  74. Assert.Equal (0, Application.Top.Frame.Y);
  75. Assert.Equal (Application.Driver.Cols, Application.Top.Frame.Width);
  76. Assert.Equal (Application.Driver.Rows, Application.Top.Frame.Height);
  77. Application.Top.ProcessHotKey (new KeyEvent (Key.CtrlMask | Key.Q, new KeyModifiers ()));
  78. }
  79. iterations++;
  80. };
  81. Application.Run (Top1 ());
  82. Toplevel Top1 ()
  83. {
  84. var top = Application.Top;
  85. top.Text = "Top1";
  86. var menu = new MenuBar (new MenuBarItem [] {
  87. new MenuBarItem ("_Options", new MenuItem [] {
  88. new MenuItem ("_Run Top2", "", () => Application.Run (Top2 ()), null, null, Key.CtrlMask | Key.R),
  89. new MenuItem ("_Quit", "", () => Application.RequestStop(), null, null, Key.CtrlMask | Key.Q)
  90. })
  91. });
  92. top.Add (menu);
  93. var statusBar = new StatusBar (new [] {
  94. new StatusItem(Key.CtrlMask | Key.R, "~^R~ Run Top2", () => Application.Run (Top2 ())),
  95. new StatusItem(Application.QuitKey, $"{Application.QuitKey} to Quit", () => Application.RequestStop())
  96. });
  97. top.Add (statusBar);
  98. var t1 = new Toplevel ();
  99. top.Add (t1);
  100. return top;
  101. }
  102. Toplevel Top2 ()
  103. {
  104. var top = new Toplevel (Application.Top.Frame);
  105. top.Text = "Top2";
  106. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  107. var menu = new MenuBar (new MenuBarItem [] {
  108. new MenuBarItem ("_Stage", new MenuItem [] {
  109. new MenuItem ("_Close", "", () => Application.RequestStop(), null, null, Key.CtrlMask | Key.C)
  110. })
  111. });
  112. top.Add (menu);
  113. var statusBar = new StatusBar (new [] {
  114. new StatusItem(Key.CtrlMask | Key.C, "~^C~ Close", () => Application.RequestStop()),
  115. });
  116. top.Add (statusBar);
  117. win.Add (new ListView () {
  118. X = 0,
  119. Y = 0,
  120. Width = Dim.Fill (),
  121. Height = Dim.Fill ()
  122. });
  123. top.Add (win);
  124. return top;
  125. }
  126. }
  127. [Fact]
  128. [AutoInitShutdown]
  129. public void Internal_Tests ()
  130. {
  131. var top = new Toplevel ();
  132. var eventInvoked = "";
  133. top.ChildUnloaded += (s, e) => eventInvoked = "ChildUnloaded";
  134. top.OnChildUnloaded (top);
  135. Assert.Equal ("ChildUnloaded", eventInvoked);
  136. top.ChildLoaded += (s, e) => eventInvoked = "ChildLoaded";
  137. top.OnChildLoaded (top);
  138. Assert.Equal ("ChildLoaded", eventInvoked);
  139. top.Closed += (s, e) => eventInvoked = "Closed";
  140. top.OnClosed (top);
  141. Assert.Equal ("Closed", eventInvoked);
  142. top.Closing += (s, e) => eventInvoked = "Closing";
  143. top.OnClosing (new ToplevelClosingEventArgs (top));
  144. Assert.Equal ("Closing", eventInvoked);
  145. top.AllChildClosed += (s, e) => eventInvoked = "AllChildClosed";
  146. top.OnAllChildClosed ();
  147. Assert.Equal ("AllChildClosed", eventInvoked);
  148. top.ChildClosed += (s, e) => eventInvoked = "ChildClosed";
  149. top.OnChildClosed (top);
  150. Assert.Equal ("ChildClosed", eventInvoked);
  151. top.Deactivate += (s, e) => eventInvoked = "Deactivate";
  152. top.OnDeactivate (top);
  153. Assert.Equal ("Deactivate", eventInvoked);
  154. top.Activate += (s, e) => eventInvoked = "Activate";
  155. top.OnActivate (top);
  156. Assert.Equal ("Activate", eventInvoked);
  157. top.Loaded += (s, e) => eventInvoked = "Loaded";
  158. top.OnLoaded ();
  159. Assert.Equal ("Loaded", eventInvoked);
  160. top.Ready += (s, e) => eventInvoked = "Ready";
  161. top.OnReady ();
  162. Assert.Equal ("Ready", eventInvoked);
  163. top.Unloaded += (s, e) => eventInvoked = "Unloaded";
  164. top.OnUnloaded ();
  165. Assert.Equal ("Unloaded", eventInvoked);
  166. top.AddMenuStatusBar (new MenuBar ());
  167. Assert.NotNull (top.MenuBar);
  168. top.AddMenuStatusBar (new StatusBar ());
  169. Assert.NotNull (top.StatusBar);
  170. top.RemoveMenuStatusBar (top.MenuBar);
  171. Assert.Null (top.MenuBar);
  172. top.RemoveMenuStatusBar (top.StatusBar);
  173. Assert.Null (top.StatusBar);
  174. Application.Begin (top);
  175. Assert.Equal (top, Application.Top);
  176. // Application.Top without menu and status bar.
  177. var supView = top.GetLocationThatFits (top, 2, 2, out int nx, out int ny, out MenuBar mb, out StatusBar sb);
  178. Assert.Equal (Application.Top, supView);
  179. Assert.Equal (0, nx);
  180. Assert.Equal (0, ny);
  181. Assert.Null (mb);
  182. Assert.Null (sb);
  183. top.AddMenuStatusBar (new MenuBar ());
  184. Assert.NotNull (top.MenuBar);
  185. // Application.Top with a menu and without status bar.
  186. top.GetLocationThatFits (top, 2, 2, out nx, out ny, out mb, out sb);
  187. Assert.Equal (0, nx);
  188. Assert.Equal (1, ny);
  189. Assert.NotNull (mb);
  190. Assert.Null (sb);
  191. top.AddMenuStatusBar (new StatusBar ());
  192. Assert.NotNull (top.StatusBar);
  193. // Application.Top with a menu and status bar.
  194. top.GetLocationThatFits (top, 2, 2, out nx, out ny, out mb, out sb);
  195. Assert.Equal (0, nx);
  196. // The available height is lower than the Application.Top height minus
  197. // the menu bar and status bar, then the top can go beyond the bottom
  198. Assert.Equal (2, ny);
  199. Assert.NotNull (mb);
  200. Assert.NotNull (sb);
  201. top.RemoveMenuStatusBar (top.MenuBar);
  202. Assert.Null (top.MenuBar);
  203. // Application.Top without a menu and with a status bar.
  204. top.GetLocationThatFits (top, 2, 2, out nx, out ny, out mb, out sb);
  205. Assert.Equal (0, nx);
  206. // The available height is lower than the Application.Top height minus
  207. // the status bar, then the top can go beyond the bottom
  208. Assert.Equal (2, ny);
  209. Assert.Null (mb);
  210. Assert.NotNull (sb);
  211. top.RemoveMenuStatusBar (top.StatusBar);
  212. Assert.Null (top.StatusBar);
  213. Assert.Null (top.MenuBar);
  214. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  215. top.Add (win);
  216. top.LayoutSubviews ();
  217. // The SuperView is always the same regardless of the caller.
  218. supView = top.GetLocationThatFits (win, 0, 0, out nx, out ny, out mb, out sb);
  219. Assert.Equal (Application.Top, supView);
  220. supView = win.GetLocationThatFits (win, 0, 0, out nx, out ny, out mb, out sb);
  221. Assert.Equal (Application.Top, supView);
  222. // Application.Top without menu and status bar.
  223. top.GetLocationThatFits (win, 0, 0, out nx, out ny, out mb, out sb);
  224. Assert.Equal (0, nx);
  225. Assert.Equal (0, ny);
  226. Assert.Null (mb);
  227. Assert.Null (sb);
  228. top.AddMenuStatusBar (new MenuBar ());
  229. Assert.NotNull (top.MenuBar);
  230. // Application.Top with a menu and without status bar.
  231. top.GetLocationThatFits (win, 2, 2, out nx, out ny, out mb, out sb);
  232. Assert.Equal (0, nx);
  233. Assert.Equal (1, ny);
  234. Assert.NotNull (mb);
  235. Assert.Null (sb);
  236. top.AddMenuStatusBar (new StatusBar ());
  237. Assert.NotNull (top.StatusBar);
  238. // Application.Top with a menu and status bar.
  239. top.GetLocationThatFits (win, 30, 20, out nx, out ny, out mb, out sb);
  240. Assert.Equal (0, nx);
  241. // The available height is lower than the Application.Top height minus
  242. // the menu bar and status bar, then the top can go beyond the bottom
  243. Assert.Equal (20, ny);
  244. Assert.NotNull (mb);
  245. Assert.NotNull (sb);
  246. top.RemoveMenuStatusBar (top.MenuBar);
  247. top.RemoveMenuStatusBar (top.StatusBar);
  248. Assert.Null (top.StatusBar);
  249. Assert.Null (top.MenuBar);
  250. top.Remove (win);
  251. win = new Window () { Width = 60, Height = 15 };
  252. top.Add (win);
  253. // Application.Top without menu and status bar.
  254. top.GetLocationThatFits (win, 0, 0, out nx, out ny, out mb, out sb);
  255. Assert.Equal (0, nx);
  256. Assert.Equal (0, ny);
  257. Assert.Null (mb);
  258. Assert.Null (sb);
  259. top.AddMenuStatusBar (new MenuBar ());
  260. Assert.NotNull (top.MenuBar);
  261. // Application.Top with a menu and without status bar.
  262. top.GetLocationThatFits (win, 2, 2, out nx, out ny, out mb, out sb);
  263. Assert.Equal (2, nx);
  264. Assert.Equal (2, ny);
  265. Assert.NotNull (mb);
  266. Assert.Null (sb);
  267. top.AddMenuStatusBar (new StatusBar ());
  268. Assert.NotNull (top.StatusBar);
  269. // Application.Top with a menu and status bar.
  270. top.GetLocationThatFits (win, 30, 20, out nx, out ny, out mb, out sb);
  271. Assert.Equal (20, nx); // 20+60=80
  272. Assert.Equal (9, ny); // 9+15+1(mb)=25
  273. Assert.NotNull (mb);
  274. Assert.NotNull (sb);
  275. top.PositionToplevels ();
  276. Assert.Equal (new Rect (0, 1, 60, 15), win.Frame);
  277. Assert.Null (Toplevel._dragPosition);
  278. win.MouseEvent (new MouseEvent () { X = 6, Y = 0, Flags = MouseFlags.Button1Pressed });
  279. Assert.Equal (new Point (6, 0), Toplevel._dragPosition);
  280. win.MouseEvent (new MouseEvent () { X = 6, Y = 0, Flags = MouseFlags.Button1Released });
  281. Assert.Null (Toplevel._dragPosition);
  282. win.CanFocus = false;
  283. win.MouseEvent (new MouseEvent () { X = 6, Y = 0, Flags = MouseFlags.Button1Pressed });
  284. Assert.Null (Toplevel._dragPosition);
  285. }
  286. [Fact]
  287. [AutoInitShutdown]
  288. public void KeyBindings_Command ()
  289. {
  290. var isRunning = false;
  291. var win1 = new Window () { Id = "win1", Width = Dim.Percent (50f), Height = Dim.Fill () };
  292. var lblTf1W1 = new Label ("Enter text in TextField on Win1:") { Id = "lblTf1W1" };
  293. var tf1W1 = new TextField ("Text1 on Win1") { Id = "tf1W1", X = Pos.Right (lblTf1W1) + 1, Width = Dim.Fill () };
  294. var lblTvW1 = new Label ("Enter text in TextView on Win1:") { Id = "lblTvW1", Y = Pos.Bottom (lblTf1W1) + 1 };
  295. var tvW1 = new TextView () { Id = "tvW1", X = Pos.Left (tf1W1), Width = Dim.Fill (), Height = 2, Text = "First line Win1\nSecond line Win1" };
  296. var lblTf2W1 = new Label ("Enter text in TextField on Win1:") { Id = "lblTf2W1", Y = Pos.Bottom (lblTvW1) + 1 };
  297. var tf2W1 = new TextField ("Text2 on Win1") { Id = "tf2W1", X = Pos.Left (tf1W1), Width = Dim.Fill () };
  298. win1.Add (lblTf1W1, tf1W1, lblTvW1, tvW1, lblTf2W1, tf2W1);
  299. var win2 = new Window () { Id = "win2", X = Pos.Right (win1) + 1, Width = Dim.Percent (50f), Height = Dim.Fill () };
  300. var lblTf1W2 = new Label ("Enter text in TextField on Win2:") { Id = "lblTf1W2" };
  301. var tf1W2 = new TextField ("Text1 on Win2") { Id = "tf1W2", X = Pos.Right (lblTf1W2) + 1, Width = Dim.Fill () };
  302. var lblTvW2 = new Label ("Enter text in TextView on Win2:") { Id = "lblTvW2", Y = Pos.Bottom (lblTf1W2) + 1 };
  303. var tvW2 = new TextView () { Id = "tvW2", X = Pos.Left (tf1W2), Width = Dim.Fill (), Height = 2, Text = "First line Win1\nSecond line Win2" };
  304. var lblTf2W2 = new Label ("Enter text in TextField on Win2:") { Id = "lblTf2W2", Y = Pos.Bottom (lblTvW2) + 1 };
  305. var tf2W2 = new TextField ("Text2 on Win2") { Id = "tf2W2", X = Pos.Left (tf1W2), Width = Dim.Fill () };
  306. win2.Add (lblTf1W2, tf1W2, lblTvW2, tvW2, lblTf2W2, tf2W2);
  307. var top = Application.Top;
  308. top.Add (win1, win2);
  309. top.Loaded += (s, e) => isRunning = true;
  310. top.Closing += (s, e) => isRunning = false;
  311. Application.Begin (top);
  312. top.Running = true;
  313. Assert.Equal (new Rect (0, 0, 40, 25), win1.Frame);
  314. Assert.Equal (new Rect (41, 0, 40, 25), win2.Frame);
  315. Assert.Equal (win1, top.Focused);
  316. Assert.Equal (tf1W1, top.MostFocused);
  317. Assert.True (isRunning);
  318. Assert.True (top.Focused.ProcessKey (new KeyEvent (Application.QuitKey, new KeyModifiers ())));
  319. Assert.False (isRunning);
  320. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Z | Key.CtrlMask, new KeyModifiers ())));
  321. Assert.False (top.Focused.ProcessKey (new KeyEvent (Key.F5, new KeyModifiers ())));
  322. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  323. Assert.Equal (win1, top.Focused);
  324. Assert.Equal (tvW1, top.MostFocused);
  325. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  326. Assert.Equal ($"\tFirst line Win1{Environment.NewLine}Second line Win1", tvW1.Text);
  327. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ())));
  328. Assert.Equal ($"First line Win1{Environment.NewLine}Second line Win1", tvW1.Text);
  329. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers ())));
  330. Assert.Equal (win1, top.Focused);
  331. Assert.Equal (tf2W1, top.MostFocused);
  332. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  333. Assert.Equal (win1, top.Focused);
  334. Assert.Equal (tf1W1, top.MostFocused);
  335. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  336. Assert.Equal (win1, top.Focused);
  337. Assert.Equal (tf1W1, top.MostFocused);
  338. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  339. Assert.Equal (win1, top.Focused);
  340. Assert.Equal (tvW1, top.MostFocused);
  341. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.I | Key.CtrlMask, new KeyModifiers ())));
  342. Assert.Equal (win1, top.Focused);
  343. Assert.Equal (tf2W1, top.MostFocused);
  344. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ())));
  345. Assert.Equal (win1, top.Focused);
  346. Assert.Equal (tvW1, top.MostFocused);
  347. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ())));
  348. Assert.Equal (win1, top.Focused);
  349. Assert.Equal (tf1W1, top.MostFocused);
  350. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ())));
  351. Assert.Equal (win1, top.Focused);
  352. Assert.Equal (tf2W1, top.MostFocused);
  353. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers ())));
  354. Assert.Equal (win2, top.Focused);
  355. Assert.Equal (tf1W2, top.MostFocused);
  356. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask | Key.ShiftMask, new KeyModifiers ())));
  357. Assert.Equal (win1, top.Focused);
  358. Assert.Equal (tf2W1, top.MostFocused);
  359. Assert.True (top.Focused.ProcessKey (new KeyEvent (Application.AlternateForwardKey, new KeyModifiers ())));
  360. Assert.Equal (win2, top.Focused);
  361. Assert.Equal (tf1W2, top.MostFocused);
  362. Assert.True (top.Focused.ProcessKey (new KeyEvent (Application.AlternateBackwardKey, new KeyModifiers ())));
  363. Assert.Equal (win1, top.Focused);
  364. Assert.Equal (tf2W1, top.MostFocused);
  365. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ())));
  366. Assert.Equal (win1, top.Focused);
  367. Assert.Equal (tvW1, top.MostFocused);
  368. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.B | Key.CtrlMask, new KeyModifiers ())));
  369. Assert.Equal (win1, top.Focused);
  370. Assert.Equal (tf1W1, top.MostFocused);
  371. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  372. Assert.Equal (win1, top.Focused);
  373. Assert.Equal (tvW1, top.MostFocused);
  374. Assert.Equal (new Point (0, 0), tvW1.CursorPosition);
  375. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.End | Key.CtrlMask, new KeyModifiers ())));
  376. Assert.Equal (win1, top.Focused);
  377. Assert.Equal (tvW1, top.MostFocused);
  378. Assert.Equal (new Point (16, 1), tvW1.CursorPosition);
  379. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.F | Key.CtrlMask, new KeyModifiers ())));
  380. Assert.Equal (win1, top.Focused);
  381. Assert.Equal (tf2W1, top.MostFocused);
  382. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.L | Key.CtrlMask, new KeyModifiers ())));
  383. }
  384. [Fact]
  385. [AutoInitShutdown]
  386. public void KeyBindings_Command_With_OverlappedTop ()
  387. {
  388. var top = Application.Top;
  389. Assert.Null (Application.OverlappedTop);
  390. top.IsOverlappedContainer = true;
  391. Application.Begin (top);
  392. Assert.Equal (Application.Top, Application.OverlappedTop);
  393. var isRunning = true;
  394. var win1 = new Window () { Id = "win1", Width = Dim.Percent (50f), Height = Dim.Fill () };
  395. var lblTf1W1 = new Label ("Enter text in TextField on Win1:");
  396. var tf1W1 = new TextField ("Text1 on Win1") { X = Pos.Right (lblTf1W1) + 1, Width = Dim.Fill () };
  397. var lblTvW1 = new Label ("Enter text in TextView on Win1:") { Y = Pos.Bottom (lblTf1W1) + 1 };
  398. var tvW1 = new TextView () { X = Pos.Left (tf1W1), Width = Dim.Fill (), Height = 2, Text = "First line Win1\nSecond line Win1" };
  399. var lblTf2W1 = new Label ("Enter text in TextField on Win1:") { Y = Pos.Bottom (lblTvW1) + 1 };
  400. var tf2W1 = new TextField ("Text2 on Win1") { X = Pos.Left (tf1W1), Width = Dim.Fill () };
  401. win1.Add (lblTf1W1, tf1W1, lblTvW1, tvW1, lblTf2W1, tf2W1);
  402. var win2 = new Window () { Id = "win2", Width = Dim.Percent (50f), Height = Dim.Fill () };
  403. var lblTf1W2 = new Label ("Enter text in TextField on Win2:");
  404. var tf1W2 = new TextField ("Text1 on Win2") { X = Pos.Right (lblTf1W2) + 1, Width = Dim.Fill () };
  405. var lblTvW2 = new Label ("Enter text in TextView on Win2:") { Y = Pos.Bottom (lblTf1W2) + 1 };
  406. var tvW2 = new TextView () { X = Pos.Left (tf1W2), Width = Dim.Fill (), Height = 2, Text = "First line Win1\nSecond line Win2" };
  407. var lblTf2W2 = new Label ("Enter text in TextField on Win2:") { Y = Pos.Bottom (lblTvW2) + 1 };
  408. var tf2W2 = new TextField ("Text2 on Win2") { X = Pos.Left (tf1W2), Width = Dim.Fill () };
  409. win2.Add (lblTf1W2, tf1W2, lblTvW2, tvW2, lblTf2W2, tf2W2);
  410. win1.Closing += (s, e) => isRunning = false;
  411. Assert.Null (top.Focused);
  412. Assert.Equal (top, Application.Current);
  413. Assert.True (top.IsCurrentTop);
  414. Assert.Equal (top, Application.OverlappedTop);
  415. Application.Begin (win1);
  416. Assert.Equal (new Rect (0, 0, 40, 25), win1.Frame);
  417. Assert.NotEqual (top, Application.Current);
  418. Assert.False (top.IsCurrentTop);
  419. Assert.Equal (win1, Application.Current);
  420. Assert.True (win1.IsCurrentTop);
  421. Assert.True (win1.IsOverlapped);
  422. Assert.Null (top.Focused);
  423. Assert.Null (top.MostFocused);
  424. Assert.Equal (tf1W1, win1.MostFocused);
  425. Assert.True (win1.IsOverlapped);
  426. Assert.Single (Application.OverlappedChildren);
  427. Application.Begin (win2);
  428. Assert.Equal (new Rect (0, 0, 40, 25), win2.Frame);
  429. Assert.NotEqual (top, Application.Current);
  430. Assert.False (top.IsCurrentTop);
  431. Assert.Equal (win2, Application.Current);
  432. Assert.True (win2.IsCurrentTop);
  433. Assert.True (win2.IsOverlapped);
  434. Assert.Null (top.Focused);
  435. Assert.Null (top.MostFocused);
  436. Assert.Equal (tf1W2, win2.MostFocused);
  437. Assert.Equal (2, Application.OverlappedChildren.Count);
  438. Application.MoveToOverlappedChild (win1);
  439. Assert.Equal (win1, Application.Current);
  440. Assert.Equal (win1, Application.OverlappedChildren [0]);
  441. win1.Running = true;
  442. Assert.True (Application.OverlappedChildren [0].ProcessKey (new KeyEvent (Application.QuitKey, new KeyModifiers ())));
  443. Assert.False (isRunning);
  444. Assert.False (win1.Running);
  445. Assert.Equal (win1, Application.OverlappedChildren [0]);
  446. Assert.True (Application.OverlappedChildren [0].ProcessKey (new KeyEvent (Key.Z | Key.CtrlMask, new KeyModifiers ())));
  447. Assert.False (Application.OverlappedChildren [0].ProcessKey (new KeyEvent (Key.F5, new KeyModifiers ())));
  448. Assert.True (Application.OverlappedChildren [0].ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  449. Assert.True (win1.IsCurrentTop);
  450. Assert.Equal (tvW1, win1.MostFocused);
  451. Assert.True (Application.OverlappedChildren [0].ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  452. Assert.Equal ($"\tFirst line Win1{Environment.NewLine}Second line Win1", tvW1.Text);
  453. Assert.True (Application.OverlappedChildren [0].ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ())));
  454. Assert.Equal ($"First line Win1{Environment.NewLine}Second line Win1", tvW1.Text);
  455. Assert.True (Application.OverlappedChildren [0].ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers ())));
  456. Assert.Equal (win1, Application.OverlappedChildren [0]);
  457. Assert.Equal (tf2W1, win1.MostFocused);
  458. Assert.True (Application.OverlappedChildren [0].ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  459. Assert.Equal (win1, Application.OverlappedChildren [0]);
  460. Assert.Equal (tf1W1, win1.MostFocused);
  461. Assert.True (Application.OverlappedChildren [0].ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  462. Assert.Equal (win1, Application.OverlappedChildren [0]);
  463. Assert.Equal (tf1W1, win1.MostFocused);
  464. Assert.True (Application.OverlappedChildren [0].ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  465. Assert.Equal (win1, Application.OverlappedChildren [0]);
  466. Assert.Equal (tvW1, win1.MostFocused);
  467. Assert.True (Application.OverlappedChildren [0].ProcessKey (new KeyEvent (Key.I | Key.CtrlMask, new KeyModifiers ())));
  468. Assert.Equal (win1, Application.OverlappedChildren [0]);
  469. Assert.Equal (tf2W1, win1.MostFocused);
  470. Assert.True (Application.OverlappedChildren [0].ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ())));
  471. Assert.Equal (win1, Application.OverlappedChildren [0]);
  472. Assert.Equal (tvW1, win1.MostFocused);
  473. Assert.True (Application.OverlappedChildren [0].ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ())));
  474. Assert.Equal (win1, Application.OverlappedChildren [0]);
  475. Assert.Equal (tf1W1, win1.MostFocused);
  476. Assert.True (Application.OverlappedChildren [0].ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ())));
  477. Assert.Equal (win1, Application.OverlappedChildren [0]);
  478. Assert.Equal (tf2W1, win1.MostFocused);
  479. Assert.True (Application.OverlappedChildren [0].ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  480. Assert.Equal (win1, Application.OverlappedChildren [0]);
  481. Assert.Equal (tf1W1, win1.MostFocused);
  482. Assert.True (Application.OverlappedChildren [0].ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers ())));
  483. Assert.Equal (win2, Application.OverlappedChildren [0]);
  484. Assert.Equal (tf1W2, win2.MostFocused);
  485. tf2W2.SetFocus ();
  486. Assert.True (tf2W2.HasFocus);
  487. Assert.True (Application.OverlappedChildren [0].ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask | Key.ShiftMask, new KeyModifiers ())));
  488. Assert.Equal (win1, Application.OverlappedChildren [0]);
  489. Assert.Equal (tf1W1, win1.MostFocused);
  490. Assert.True (Application.OverlappedChildren [0].ProcessKey (new KeyEvent (Application.AlternateForwardKey, new KeyModifiers ())));
  491. Assert.Equal (win2, Application.OverlappedChildren [0]);
  492. Assert.Equal (tf2W2, win2.MostFocused);
  493. Assert.True (Application.OverlappedChildren [0].ProcessKey (new KeyEvent (Application.AlternateBackwardKey, new KeyModifiers ())));
  494. Assert.Equal (win1, Application.OverlappedChildren [0]);
  495. Assert.Equal (tf1W1, win1.MostFocused);
  496. Assert.True (Application.OverlappedChildren [0].ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  497. Assert.Equal (win1, Application.OverlappedChildren [0]);
  498. Assert.Equal (tvW1, win1.MostFocused);
  499. Assert.True (Application.OverlappedChildren [0].ProcessKey (new KeyEvent (Key.B | Key.CtrlMask, new KeyModifiers ())));
  500. Assert.Equal (win1, Application.OverlappedChildren [0]);
  501. Assert.Equal (tf1W1, win1.MostFocused);
  502. Assert.True (Application.OverlappedChildren [0].ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  503. Assert.Equal (win1, Application.OverlappedChildren [0]);
  504. Assert.Equal (tvW1, win1.MostFocused);
  505. Assert.Equal (new Point (0, 0), tvW1.CursorPosition);
  506. Assert.True (Application.OverlappedChildren [0].ProcessKey (new KeyEvent (Key.End | Key.CtrlMask, new KeyModifiers ())));
  507. Assert.Equal (win1, Application.OverlappedChildren [0]);
  508. Assert.Equal (tvW1, win1.MostFocused);
  509. Assert.Equal (new Point (16, 1), tvW1.CursorPosition);
  510. Assert.True (Application.OverlappedChildren [0].ProcessKey (new KeyEvent (Key.F | Key.CtrlMask, new KeyModifiers ())));
  511. Assert.Equal (win1, Application.OverlappedChildren [0]);
  512. Assert.Equal (tf2W1, win1.MostFocused);
  513. Assert.True (Application.OverlappedChildren [0].ProcessKey (new KeyEvent (Key.L | Key.CtrlMask, new KeyModifiers ())));
  514. }
  515. [Fact]
  516. public void Added_Event_Should_Not_Be_Used_To_Initialize_Toplevel_Events ()
  517. {
  518. Key alternateForwardKey = default;
  519. Key alternateBackwardKey = default;
  520. Key quitKey = default;
  521. var wasAdded = false;
  522. var view = new View ();
  523. view.Added += View_Added;
  524. void View_Added (object sender, SuperViewChangedEventArgs e)
  525. {
  526. Assert.Throws<NullReferenceException> (() => Application.Top.AlternateForwardKeyChanged += (s, e) => alternateForwardKey = e.OldKey);
  527. Assert.Throws<NullReferenceException> (() => Application.Top.AlternateBackwardKeyChanged += (s, e) => alternateBackwardKey = e.OldKey);
  528. Assert.Throws<NullReferenceException> (() => Application.Top.QuitKeyChanged += (s, e) => quitKey = e.OldKey);
  529. Assert.False (wasAdded);
  530. wasAdded = true;
  531. view.Added -= View_Added;
  532. }
  533. var win = new Window ();
  534. win.Add (view);
  535. Application.Init (new FakeDriver ());
  536. var top = Application.Top;
  537. top.Add (win);
  538. Assert.True (wasAdded);
  539. Application.Shutdown ();
  540. }
  541. [Fact]
  542. [AutoInitShutdown]
  543. public void AlternateForwardKeyChanged_AlternateBackwardKeyChanged_QuitKeyChanged_Events ()
  544. {
  545. Key alternateForwardKey = default;
  546. Key alternateBackwardKey = default;
  547. Key quitKey = default;
  548. var view = new View ();
  549. view.Initialized += View_Initialized;
  550. void View_Initialized (object sender, EventArgs e)
  551. {
  552. Application.Top.AlternateForwardKeyChanged += (s, e) => alternateForwardKey = e.OldKey;
  553. Application.Top.AlternateBackwardKeyChanged += (s, e) => alternateBackwardKey = e.OldKey;
  554. Application.Top.QuitKeyChanged += (s, e) => quitKey = e.OldKey;
  555. }
  556. var win = new Window ();
  557. win.Add (view);
  558. var top = Application.Top;
  559. top.Add (win);
  560. Application.Begin (top);
  561. Assert.Equal (Key.Null, alternateForwardKey);
  562. Assert.Equal (Key.Null, alternateBackwardKey);
  563. Assert.Equal (Key.Null, quitKey);
  564. Assert.Equal (Key.PageDown | Key.CtrlMask, Application.AlternateForwardKey);
  565. Assert.Equal (Key.PageUp | Key.CtrlMask, Application.AlternateBackwardKey);
  566. Assert.Equal (Key.Q | Key.CtrlMask, Application.QuitKey);
  567. Application.AlternateForwardKey = Key.A;
  568. Application.AlternateBackwardKey = Key.B;
  569. Application.QuitKey = Key.C;
  570. Assert.Equal (Key.PageDown | Key.CtrlMask, alternateForwardKey);
  571. Assert.Equal (Key.PageUp | Key.CtrlMask, alternateBackwardKey);
  572. Assert.Equal (Key.Q | Key.CtrlMask, quitKey);
  573. Assert.Equal (Key.A, Application.AlternateForwardKey);
  574. Assert.Equal (Key.B, Application.AlternateBackwardKey);
  575. Assert.Equal (Key.C, Application.QuitKey);
  576. // Replacing the defaults keys to avoid errors on others unit tests that are using it.
  577. Application.AlternateForwardKey = Key.PageDown | Key.CtrlMask;
  578. Application.AlternateBackwardKey = Key.PageUp | Key.CtrlMask;
  579. Application.QuitKey = Key.Q | Key.CtrlMask;
  580. Assert.Equal (Key.PageDown | Key.CtrlMask, Application.AlternateForwardKey);
  581. Assert.Equal (Key.PageUp | Key.CtrlMask, Application.AlternateBackwardKey);
  582. Assert.Equal (Key.Q | Key.CtrlMask, Application.QuitKey);
  583. }
  584. [Fact, AutoInitShutdown]
  585. public void Mouse_Drag_On_Top_With_Superview_Null ()
  586. {
  587. var win = new Window ();
  588. var top = Application.Top;
  589. top.Add (win);
  590. var iterations = -1;
  591. Application.Iteration = () => {
  592. iterations++;
  593. if (iterations == 0) {
  594. ((FakeDriver)Application.Driver).SetBufferSize (40, 15);
  595. MessageBox.Query ("", "Hello Word", "Ok");
  596. } else if (iterations == 1) {
  597. TestHelpers.AssertDriverContentsWithFrameAre (@$"
  598. ┌──────────────────────────────────────┐
  599. │ │
  600. │ │
  601. │ │
  602. │ │
  603. │ ┌──────────────────────┐ │
  604. │ │ Hello Word │ │
  605. │ │ │ │
  606. │ │ {CM.Glyphs.LeftBracket}{CM.Glyphs.LeftDefaultIndicator} Ok {CM.Glyphs.RightDefaultIndicator}{CM.Glyphs.RightBracket} │ │
  607. │ └──────────────────────┘ │
  608. │ │
  609. │ │
  610. │ │
  611. │ │
  612. └──────────────────────────────────────┘
  613. ", output);
  614. } else if (iterations == 2) {
  615. Assert.Null (Application.MouseGrabView);
  616. // Grab the mouse
  617. ReflectionTools.InvokePrivate (
  618. typeof (Application),
  619. "ProcessMouseEvent",
  620. new MouseEvent () {
  621. X = 8,
  622. Y = 5,
  623. Flags = MouseFlags.Button1Pressed
  624. });
  625. Assert.Equal (Application.Current, Application.MouseGrabView);
  626. Assert.Equal (new Rect (8, 5, 24, 5), Application.MouseGrabView.Frame);
  627. } else if (iterations == 3) {
  628. Assert.Equal (Application.Current, Application.MouseGrabView);
  629. // Drag to left
  630. ReflectionTools.InvokePrivate (
  631. typeof (Application),
  632. "ProcessMouseEvent",
  633. new MouseEvent () {
  634. X = 7,
  635. Y = 5,
  636. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  637. });
  638. Assert.Equal (Application.Current, Application.MouseGrabView);
  639. Assert.Equal (new Rect (7, 5, 24, 5), Application.MouseGrabView.Frame);
  640. } else if (iterations == 4) {
  641. Assert.Equal (Application.Current, Application.MouseGrabView);
  642. TestHelpers.AssertDriverContentsWithFrameAre (@$"
  643. ┌──────────────────────────────────────┐
  644. │ │
  645. │ │
  646. │ │
  647. │ │
  648. │ ┌──────────────────────┐ │
  649. │ │ Hello Word │ │
  650. │ │ │ │
  651. │ │ {CM.Glyphs.LeftBracket}{CM.Glyphs.LeftDefaultIndicator} Ok {CM.Glyphs.RightDefaultIndicator}{CM.Glyphs.RightBracket} │ │
  652. │ └──────────────────────┘ │
  653. │ │
  654. │ │
  655. │ │
  656. │ │
  657. └──────────────────────────────────────┘", output);
  658. Assert.Equal (Application.Current, Application.MouseGrabView);
  659. } else if (iterations == 5) {
  660. Assert.Equal (Application.Current, Application.MouseGrabView);
  661. // Drag up
  662. ReflectionTools.InvokePrivate (
  663. typeof (Application),
  664. "ProcessMouseEvent",
  665. new MouseEvent () {
  666. X = 7,
  667. Y = 4,
  668. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  669. });
  670. Assert.Equal (Application.Current, Application.MouseGrabView);
  671. Assert.Equal (new Rect (7, 4, 24, 5), Application.MouseGrabView.Frame);
  672. } else if (iterations == 6) {
  673. Assert.Equal (Application.Current, Application.MouseGrabView);
  674. TestHelpers.AssertDriverContentsWithFrameAre (@$"
  675. ┌──────────────────────────────────────┐
  676. │ │
  677. │ │
  678. │ │
  679. │ ┌──────────────────────┐ │
  680. │ │ Hello Word │ │
  681. │ │ │ │
  682. │ │ {CM.Glyphs.LeftBracket}{CM.Glyphs.LeftDefaultIndicator} Ok {CM.Glyphs.RightDefaultIndicator}{CM.Glyphs.RightBracket} │ │
  683. │ └──────────────────────┘ │
  684. │ │
  685. │ │
  686. │ │
  687. │ │
  688. │ │
  689. └──────────────────────────────────────┘", output);
  690. Assert.Equal (Application.Current, Application.MouseGrabView);
  691. Assert.Equal (new Rect (7, 4, 24, 5), Application.MouseGrabView.Frame);
  692. } else if (iterations == 7) {
  693. Assert.Equal (Application.Current, Application.MouseGrabView);
  694. // Ungrab the mouse
  695. ReflectionTools.InvokePrivate (
  696. typeof (Application),
  697. "ProcessMouseEvent",
  698. new MouseEvent () {
  699. X = 7,
  700. Y = 4,
  701. Flags = MouseFlags.Button1Released
  702. });
  703. Assert.Null (Application.MouseGrabView);
  704. } else if (iterations == 8) Application.RequestStop ();
  705. else if (iterations == 9) Application.RequestStop ();
  706. };
  707. Application.Run ();
  708. }
  709. [Fact, AutoInitShutdown]
  710. public void Mouse_Drag_On_Top_With_Superview_Not_Null ()
  711. {
  712. var win = new Window () {
  713. X = 3,
  714. Y = 2,
  715. Width = 10,
  716. Height = 5
  717. };
  718. var top = Application.Top;
  719. top.Add (win);
  720. var iterations = -1;
  721. int movex = 0;
  722. int movey = 0;
  723. var location = new Rect (win.Frame.X, win.Frame.Y, 7, 3);
  724. Application.Iteration = () => {
  725. iterations++;
  726. if (iterations == 0) {
  727. ((FakeDriver)Application.Driver).SetBufferSize (30, 10);
  728. } else if (iterations == 1) {
  729. location = win.Frame;
  730. Assert.Null (Application.MouseGrabView);
  731. // Grab the mouse
  732. ReflectionTools.InvokePrivate (
  733. typeof (Application),
  734. "ProcessMouseEvent",
  735. new MouseEvent () {
  736. X = win.Frame.X,
  737. Y = win.Frame.Y,
  738. Flags = MouseFlags.Button1Pressed
  739. });
  740. Assert.Equal (win, Application.MouseGrabView);
  741. Assert.Equal (location, Application.MouseGrabView.Frame);
  742. } else if (iterations == 2) {
  743. Assert.Equal (win, Application.MouseGrabView);
  744. // Drag to left
  745. movex = 1;
  746. movey = 0;
  747. ReflectionTools.InvokePrivate (
  748. typeof (Application),
  749. "ProcessMouseEvent",
  750. new MouseEvent () {
  751. X = win.Frame.X + movex,
  752. Y = win.Frame.Y + movey,
  753. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  754. });
  755. Assert.Equal (win, Application.MouseGrabView);
  756. } else if (iterations == 3) {
  757. // we should have moved +1, +0
  758. Assert.Equal (win, Application.MouseGrabView);
  759. Assert.Equal (win, Application.MouseGrabView);
  760. location.Offset (movex, movey);
  761. Assert.Equal (location, Application.MouseGrabView.Frame);
  762. } else if (iterations == 4) {
  763. Assert.Equal (win, Application.MouseGrabView);
  764. // Drag up
  765. movex = 0;
  766. movey = -1;
  767. ReflectionTools.InvokePrivate (
  768. typeof (Application),
  769. "ProcessMouseEvent",
  770. new MouseEvent () {
  771. X = win.Frame.X + movex,
  772. Y = win.Frame.Y + movey,
  773. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  774. });
  775. Assert.Equal (win, Application.MouseGrabView);
  776. } else if (iterations == 5) {
  777. // we should have moved +0, -1
  778. Assert.Equal (win, Application.MouseGrabView);
  779. location.Offset (movex, movey);
  780. Assert.Equal (location, Application.MouseGrabView.Frame);
  781. } else if (iterations == 6) {
  782. Assert.Equal (win, Application.MouseGrabView);
  783. // Ungrab the mouse
  784. movex = 0;
  785. movey = 0;
  786. ReflectionTools.InvokePrivate (
  787. typeof (Application),
  788. "ProcessMouseEvent",
  789. new MouseEvent () {
  790. X = win.Frame.X + movex,
  791. Y = win.Frame.Y + movey,
  792. Flags = MouseFlags.Button1Released
  793. });
  794. Assert.Null (Application.MouseGrabView);
  795. } else if (iterations == 7) {
  796. Application.RequestStop ();
  797. }
  798. };
  799. Application.Run ();
  800. }
  801. [Fact, AutoInitShutdown]
  802. public void GetLocationThatFits_With_Border_Null_Not_Throws ()
  803. {
  804. var top = new Toplevel ();
  805. Application.Begin (top);
  806. var exception = Record.Exception (() => ((FakeDriver)Application.Driver).SetBufferSize (0, 10));
  807. Assert.Null (exception);
  808. exception = Record.Exception (() => ((FakeDriver)Application.Driver).SetBufferSize (10, 0));
  809. Assert.Null (exception);
  810. }
  811. [Fact, AutoInitShutdown]
  812. public void OnEnter_OnLeave_Triggered_On_Application_Begin_End ()
  813. {
  814. var isEnter = false;
  815. var isLeave = false;
  816. var v = new View ();
  817. v.Enter += (s, _) => isEnter = true;
  818. v.Leave += (s, _) => isLeave = true;
  819. var top = Application.Top;
  820. top.Add (v);
  821. Assert.False (v.CanFocus);
  822. var exception = Record.Exception (() => top.OnEnter (top));
  823. Assert.Null (exception);
  824. exception = Record.Exception (() => top.OnLeave (top));
  825. Assert.Null (exception);
  826. v.CanFocus = true;
  827. Application.Begin (top);
  828. Assert.True (isEnter);
  829. Assert.False (isLeave);
  830. isEnter = false;
  831. var d = new Dialog ();
  832. var rs = Application.Begin (d);
  833. Assert.False (isEnter);
  834. Assert.True (isLeave);
  835. isLeave = false;
  836. Application.End (rs);
  837. Assert.True (isEnter);
  838. Assert.False (isLeave); // Leave event cannot be trigger because it v.Enter was performed and v is focused
  839. Assert.True (v.HasFocus);
  840. }
  841. [Fact, AutoInitShutdown]
  842. public void OnEnter_OnLeave_Triggered_On_Application_Begin_End_With_More_Toplevels ()
  843. {
  844. var iterations = 0;
  845. var steps = new int [5];
  846. var isEnterTop = false;
  847. var isLeaveTop = false;
  848. var vt = new View ();
  849. var top = Application.Top;
  850. var diag = new Dialog ();
  851. vt.Enter += (s, e) => {
  852. iterations++;
  853. isEnterTop = true;
  854. if (iterations == 1) {
  855. steps [0] = iterations;
  856. Assert.Null (e.View);
  857. } else {
  858. steps [4] = iterations;
  859. Assert.Equal (diag, e.View);
  860. }
  861. };
  862. vt.Leave += (s, e) => {
  863. iterations++;
  864. steps [1] = iterations;
  865. isLeaveTop = true;
  866. Assert.Equal (diag, e.View);
  867. };
  868. top.Add (vt);
  869. Assert.False (vt.CanFocus);
  870. var exception = Record.Exception (() => top.OnEnter (top));
  871. Assert.Null (exception);
  872. exception = Record.Exception (() => top.OnLeave (top));
  873. Assert.Null (exception);
  874. vt.CanFocus = true;
  875. Application.Begin (top);
  876. Assert.True (isEnterTop);
  877. Assert.False (isLeaveTop);
  878. isEnterTop = false;
  879. var isEnterDiag = false;
  880. var isLeaveDiag = false;
  881. var vd = new View ();
  882. vd.Enter += (s, e) => {
  883. iterations++;
  884. steps [2] = iterations;
  885. isEnterDiag = true;
  886. Assert.Null (e.View);
  887. };
  888. vd.Leave += (s, e) => {
  889. iterations++;
  890. steps [3] = iterations;
  891. isLeaveDiag = true;
  892. Assert.Equal (top, e.View);
  893. };
  894. diag.Add (vd);
  895. Assert.False (vd.CanFocus);
  896. exception = Record.Exception (() => diag.OnEnter (diag));
  897. Assert.Null (exception);
  898. exception = Record.Exception (() => diag.OnLeave (diag));
  899. Assert.Null (exception);
  900. vd.CanFocus = true;
  901. var rs = Application.Begin (diag);
  902. Assert.True (isEnterDiag);
  903. Assert.False (isLeaveDiag);
  904. Assert.False (isEnterTop);
  905. Assert.True (isLeaveTop);
  906. isEnterDiag = false;
  907. isLeaveTop = false;
  908. Application.End (rs);
  909. Assert.False (isEnterDiag);
  910. Assert.True (isLeaveDiag);
  911. Assert.True (isEnterTop);
  912. Assert.False (isLeaveTop); // Leave event cannot be trigger because it v.Enter was performed and v is focused
  913. Assert.True (vt.HasFocus);
  914. Assert.Equal (1, steps [0]);
  915. Assert.Equal (2, steps [1]);
  916. Assert.Equal (3, steps [2]);
  917. Assert.Equal (4, steps [3]);
  918. Assert.Equal (5, steps [^1]);
  919. }
  920. [Fact, AutoInitShutdown]
  921. public void PositionCursor_SetCursorVisibility_To_Invisible_If_Focused_Is_Null ()
  922. {
  923. var tf = new TextField ("test") { Width = 5 };
  924. var view = new View () { Width = 10, Height = 10 };
  925. view.Add (tf);
  926. Application.Top.Add (view);
  927. Application.Begin (Application.Top);
  928. Assert.True (tf.HasFocus);
  929. Application.Driver.GetCursorVisibility (out CursorVisibility cursor);
  930. Assert.Equal (CursorVisibility.Default, cursor);
  931. view.Enabled = false;
  932. Assert.False (tf.HasFocus);
  933. Application.Refresh ();
  934. Application.Driver.GetCursorVisibility (out cursor);
  935. Assert.Equal (CursorVisibility.Invisible, cursor);
  936. }
  937. [Fact, AutoInitShutdown]
  938. public void IsLoaded_Application_Begin ()
  939. {
  940. var top = Application.Top;
  941. Assert.False (top.IsLoaded);
  942. Application.Begin (top);
  943. Assert.True (top.IsLoaded);
  944. }
  945. [Fact, AutoInitShutdown]
  946. public void IsLoaded_With_Sub_Toplevel_Application_Begin_NeedDisplay ()
  947. {
  948. var top = Application.Top;
  949. var subTop = new Toplevel ();
  950. var view = new View (new Rect (0, 0, 20, 10));
  951. subTop.Add (view);
  952. top.Add (subTop);
  953. Assert.False (top.IsLoaded);
  954. Assert.False (subTop.IsLoaded);
  955. Assert.Equal (new Rect (0, 0, 20, 10), view.Frame);
  956. view.LayoutStarted += view_LayoutStarted;
  957. void view_LayoutStarted (object sender, LayoutEventArgs e)
  958. {
  959. Assert.Equal (new Rect (0, 0, 20, 10), view._needsDisplayRect);
  960. view.LayoutStarted -= view_LayoutStarted;
  961. }
  962. Application.Begin (top);
  963. Assert.True (top.IsLoaded);
  964. Assert.True (subTop.IsLoaded);
  965. Assert.Equal (new Rect (0, 0, 20, 10), view.Frame);
  966. view.Frame = new Rect (1, 3, 10, 5);
  967. Assert.Equal (new Rect (1, 3, 10, 5), view.Frame);
  968. Assert.Equal (new Rect (0, 0, 10, 5), view._needsDisplayRect);
  969. view.OnDrawContent (view.Bounds);
  970. view.Frame = new Rect (1, 3, 10, 5);
  971. Assert.Equal (new Rect (1, 3, 10, 5), view.Frame);
  972. Assert.Equal (new Rect (0, 0, 10, 5), view._needsDisplayRect);
  973. }
  974. // BUGBUG: Broke this test with #2483 - @bdisp I need your help figuring out why
  975. [Fact, AutoInitShutdown]
  976. public void Toplevel_Inside_ScrollView_MouseGrabView ()
  977. {
  978. var scrollView = new ScrollView () {
  979. X = 3,
  980. Y = 3,
  981. Width = 40,
  982. Height = 16,
  983. ContentSize = new Size (200, 100)
  984. };
  985. var win = new Window () { X = 3, Y = 3, Width = Dim.Fill (3), Height = Dim.Fill (3) };
  986. scrollView.Add (win);
  987. var top = Application.Top;
  988. top.Add (scrollView);
  989. Application.Begin (top);
  990. Assert.Equal (new Rect (0, 0, 80, 25), top.Frame);
  991. Assert.Equal (new Rect (3, 3, 40, 16), scrollView.Frame);
  992. Assert.Equal (new Rect (0, 0, 200, 100), scrollView.Subviews [0].Frame);
  993. Assert.Equal (new Rect (3, 3, 194, 94), win.Frame);
  994. TestHelpers.AssertDriverContentsWithFrameAre (@"
  995. ┌───────────────────────────────────┴
  996. │ ░
  997. │ ░
  998. │ ░
  999. │ ░
  1000. │ ░
  1001. │ ░
  1002. │ ░
  1003. │ ░
  1004. │ ░
  1005. │ ░
  1006. │ ▼
  1007. ◄├──────┤░░░░░░░░░░░░░░░░░░░░░░░░░░░░░► ", output);
  1008. ReflectionTools.InvokePrivate (
  1009. typeof (Application),
  1010. "ProcessMouseEvent",
  1011. new MouseEvent () {
  1012. X = 6,
  1013. Y = 6,
  1014. Flags = MouseFlags.Button1Pressed
  1015. });
  1016. Assert.Equal (win, Application.MouseGrabView);
  1017. Assert.Equal (new Rect (3, 3, 194, 94), win.Frame);
  1018. ReflectionTools.InvokePrivate (
  1019. typeof (Application),
  1020. "ProcessMouseEvent",
  1021. new MouseEvent () {
  1022. X = 9,
  1023. Y = 9,
  1024. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1025. });
  1026. Assert.Equal (win, Application.MouseGrabView);
  1027. top.SetNeedsLayout ();
  1028. top.LayoutSubviews ();
  1029. Assert.Equal (new Rect (6, 6, 191, 91), win.Frame);
  1030. Application.Refresh ();
  1031. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1032. ┌────────────────────────────────░
  1033. │ ░
  1034. │ ░
  1035. │ ░
  1036. │ ░
  1037. │ ░
  1038. │ ░
  1039. │ ░
  1040. │ ▼
  1041. ◄├──────┤░░░░░░░░░░░░░░░░░░░░░░░░░░░░░► ", output);
  1042. ReflectionTools.InvokePrivate (
  1043. typeof (Application),
  1044. "ProcessMouseEvent",
  1045. new MouseEvent () {
  1046. X = 5,
  1047. Y = 5,
  1048. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1049. });
  1050. Assert.Equal (win, Application.MouseGrabView);
  1051. top.SetNeedsLayout ();
  1052. top.LayoutSubviews ();
  1053. Assert.Equal (new Rect (2, 2, 195, 95), win.Frame);
  1054. Application.Refresh ();
  1055. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1056. ┌────────────────────────────────────│
  1057. │ ┴
  1058. │ ░
  1059. │ ░
  1060. │ ░
  1061. │ ░
  1062. │ ░
  1063. │ ░
  1064. │ ░
  1065. │ ░
  1066. │ ░
  1067. │ ░
  1068. │ ▼
  1069. ◄├──────┤░░░░░░░░░░░░░░░░░░░░░░░░░░░░░► ", output);
  1070. ReflectionTools.InvokePrivate (
  1071. typeof (Application),
  1072. "ProcessMouseEvent",
  1073. new MouseEvent () {
  1074. X = 5,
  1075. Y = 5,
  1076. Flags = MouseFlags.Button1Released
  1077. });
  1078. Assert.Null (Application.MouseGrabView);
  1079. ReflectionTools.InvokePrivate (
  1080. typeof (Application),
  1081. "ProcessMouseEvent",
  1082. new MouseEvent () {
  1083. X = 4,
  1084. Y = 4,
  1085. Flags = MouseFlags.ReportMousePosition
  1086. });
  1087. Assert.Equal (scrollView, Application.MouseGrabView);
  1088. }
  1089. [Fact, AutoInitShutdown]
  1090. public void Dialog_Bounds_Bigger_Than_Driver_Cols_And_Rows_Allow_Drag_Beyond_Left_Right_And_Bottom ()
  1091. {
  1092. var top = Application.Top;
  1093. var dialog = new Dialog (new Button ("Ok")) { Width = 20, Height = 3 };
  1094. Application.Begin (top);
  1095. ((FakeDriver)Application.Driver).SetBufferSize (40, 10);
  1096. Application.Begin (dialog);
  1097. Application.Refresh ();
  1098. Assert.Equal (new Rect (0, 0, 40, 10), top.Frame);
  1099. Assert.Equal (new Rect (10, 3, 20, 3), dialog.Frame);
  1100. TestHelpers.AssertDriverContentsWithFrameAre (@$"
  1101. ┌──────────────────┐
  1102. │ {CM.Glyphs.LeftBracket} Ok {CM.Glyphs.RightBracket} │
  1103. └──────────────────┘
  1104. ", output);
  1105. Assert.Null (Application.MouseGrabView);
  1106. ReflectionTools.InvokePrivate (
  1107. typeof (Application),
  1108. "ProcessMouseEvent",
  1109. new MouseEvent () {
  1110. X = 10,
  1111. Y = 3,
  1112. Flags = MouseFlags.Button1Pressed
  1113. });
  1114. Assert.Equal (dialog, Application.MouseGrabView);
  1115. ReflectionTools.InvokePrivate (
  1116. typeof (Application),
  1117. "ProcessMouseEvent",
  1118. new MouseEvent () {
  1119. X = -11,
  1120. Y = -4,
  1121. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1122. });
  1123. Application.Refresh ();
  1124. Assert.Equal (new Rect (0, 0, 40, 10), top.Frame);
  1125. Assert.Equal (new Rect (0, 0, 20, 3), dialog.Frame);
  1126. TestHelpers.AssertDriverContentsWithFrameAre (@$"
  1127. ┌──────────────────┐
  1128. │ {CM.Glyphs.LeftBracket} Ok {CM.Glyphs.RightBracket} │
  1129. └──────────────────┘
  1130. ", output);
  1131. // Changes Top size to same size as Dialog more menu and scroll bar
  1132. ((FakeDriver)Application.Driver).SetBufferSize (20, 3);
  1133. ReflectionTools.InvokePrivate (
  1134. typeof (Application),
  1135. "ProcessMouseEvent",
  1136. new MouseEvent () {
  1137. X = -1,
  1138. Y = -1,
  1139. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1140. });
  1141. Application.Refresh ();
  1142. Assert.Equal (new Rect (0, 0, 20, 3), top.Frame);
  1143. Assert.Equal (new Rect (0, 0, 20, 3), dialog.Frame);
  1144. TestHelpers.AssertDriverContentsWithFrameAre (@$"
  1145. ┌──────────────────┐
  1146. │ {CM.Glyphs.LeftBracket} Ok {CM.Glyphs.RightBracket} │
  1147. └──────────────────┘
  1148. ", output);
  1149. // Changes Top size smaller than Dialog size
  1150. ((FakeDriver)Application.Driver).SetBufferSize (19, 2);
  1151. ReflectionTools.InvokePrivate (
  1152. typeof (Application),
  1153. "ProcessMouseEvent",
  1154. new MouseEvent () {
  1155. X = -1,
  1156. Y = -1,
  1157. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1158. });
  1159. Application.Refresh ();
  1160. Assert.Equal (new Rect (0, 0, 19, 2), top.Frame);
  1161. Assert.Equal (new Rect (-1, 0, 20, 3), dialog.Frame);
  1162. TestHelpers.AssertDriverContentsWithFrameAre (@$"
  1163. ──────────────────┐
  1164. {CM.Glyphs.LeftBracket} Ok {CM.Glyphs.RightBracket} │
  1165. ", output);
  1166. ReflectionTools.InvokePrivate (
  1167. typeof (Application),
  1168. "ProcessMouseEvent",
  1169. new MouseEvent () {
  1170. X = 18,
  1171. Y = 1,
  1172. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1173. });
  1174. Application.Refresh ();
  1175. Assert.Equal (new Rect (0, 0, 19, 2), top.Frame);
  1176. Assert.Equal (new Rect (18, 1, 20, 3), dialog.Frame);
  1177. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1178. ┌", output);
  1179. // On a real app we can't go beyond the SuperView bounds
  1180. ReflectionTools.InvokePrivate (
  1181. typeof (Application),
  1182. "ProcessMouseEvent",
  1183. new MouseEvent () {
  1184. X = 19,
  1185. Y = 2,
  1186. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1187. });
  1188. Application.Refresh ();
  1189. Assert.Equal (new Rect (0, 0, 19, 2), top.Frame);
  1190. Assert.Equal (new Rect (19, 2, 20, 3), dialog.Frame);
  1191. TestHelpers.AssertDriverContentsWithFrameAre (@"", output);
  1192. }
  1193. [Fact, AutoInitShutdown]
  1194. public void Modal_As_Top_Will_Drag_Cleanly ()
  1195. {
  1196. var dialog = new Dialog () { Width = 30, Height = 10 };
  1197. dialog.Add (new Label (
  1198. "How should I've to react. Cleaning all chunk trails or setting the 'Cols' and 'Rows' to this dialog length?\n" +
  1199. "Cleaning is more easy to fix this.") {
  1200. X = Pos.Center (),
  1201. Y = Pos.Center (),
  1202. Width = Dim.Fill (),
  1203. Height = Dim.Fill (),
  1204. TextAlignment = TextAlignment.Centered,
  1205. VerticalTextAlignment = VerticalTextAlignment.Middle,
  1206. AutoSize = false
  1207. });
  1208. var rs = Application.Begin (dialog);
  1209. Assert.Null (Application.MouseGrabView);
  1210. Assert.Equal (new Rect (25, 7, 30, 10), dialog.Frame);
  1211. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1212. ┌────────────────────────────┐
  1213. │ How should I've to react. │
  1214. │Cleaning all chunk trails or│
  1215. │ setting the 'Cols' and │
  1216. │ 'Rows' to this dialog │
  1217. │ length? │
  1218. │Cleaning is more easy to fix│
  1219. │ this. │
  1220. │ │
  1221. └────────────────────────────┘", output);
  1222. ReflectionTools.InvokePrivate (
  1223. typeof (Application),
  1224. "ProcessMouseEvent",
  1225. new MouseEvent () {
  1226. X = 25,
  1227. Y = 7,
  1228. Flags = MouseFlags.Button1Pressed
  1229. });
  1230. var firstIteration = false;
  1231. Application.RunMainLoopIteration (ref rs, ref firstIteration);
  1232. Assert.Equal (dialog, Application.MouseGrabView);
  1233. Assert.Equal (new Rect (25, 7, 30, 10), dialog.Frame);
  1234. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1235. ┌────────────────────────────┐
  1236. │ How should I've to react. │
  1237. │Cleaning all chunk trails or│
  1238. │ setting the 'Cols' and │
  1239. │ 'Rows' to this dialog │
  1240. │ length? │
  1241. │Cleaning is more easy to fix│
  1242. │ this. │
  1243. │ │
  1244. └────────────────────────────┘", output);
  1245. ReflectionTools.InvokePrivate (
  1246. typeof (Application),
  1247. "ProcessMouseEvent",
  1248. new MouseEvent () {
  1249. X = 20,
  1250. Y = 10,
  1251. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1252. });
  1253. firstIteration = false;
  1254. Application.RunMainLoopIteration (ref rs, ref firstIteration);
  1255. Assert.Equal (dialog, Application.MouseGrabView);
  1256. Assert.Equal (new Rect (20, 10, 30, 10), dialog.Frame);
  1257. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1258. ┌────────────────────────────┐
  1259. │ How should I've to react. │
  1260. │Cleaning all chunk trails or│
  1261. │ setting the 'Cols' and │
  1262. │ 'Rows' to this dialog │
  1263. │ length? │
  1264. │Cleaning is more easy to fix│
  1265. │ this. │
  1266. │ │
  1267. └────────────────────────────┘", output);
  1268. Application.End (rs);
  1269. }
  1270. // BUGBUG: Broke this test with #2483 - @bdisp I need your help figuring out why
  1271. [Fact, AutoInitShutdown]
  1272. public void Draw_A_Top_Subview_On_A_Dialog ()
  1273. {
  1274. var top = Application.Top;
  1275. var win = new Window ();
  1276. top.Add (win);
  1277. Application.Begin (top);
  1278. ((FakeDriver)Application.Driver).SetBufferSize (20, 20);
  1279. Assert.Equal (new Rect (0, 0, 20, 20), win.Frame);
  1280. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1281. ┌──────────────────┐
  1282. │ │
  1283. │ │
  1284. │ │
  1285. │ │
  1286. │ │
  1287. │ │
  1288. │ │
  1289. │ │
  1290. │ │
  1291. │ │
  1292. │ │
  1293. │ │
  1294. │ │
  1295. │ │
  1296. │ │
  1297. │ │
  1298. │ │
  1299. │ │
  1300. └──────────────────┘", output);
  1301. var btnPopup = new Button ("Popup");
  1302. btnPopup.Clicked += (s, e) => {
  1303. var viewToScreen = btnPopup.ViewToScreen (top.Frame);
  1304. var view = new View () {
  1305. X = 1,
  1306. Y = viewToScreen.Y + 1,
  1307. Width = 18,
  1308. Height = 5,
  1309. BorderStyle = LineStyle.Single
  1310. };
  1311. Application.Current.DrawContentComplete += Current_DrawContentComplete;
  1312. top.Add (view);
  1313. void Current_DrawContentComplete (object sender, DrawEventArgs e)
  1314. {
  1315. Assert.Equal (new Rect (1, 14, 18, 5), view.Frame);
  1316. var savedClip = Application.Driver.Clip;
  1317. Application.Driver.Clip = top.Frame;
  1318. view.Draw ();
  1319. top.Move (2, 15);
  1320. View.Driver.AddStr ("One");
  1321. top.Move (2, 16);
  1322. View.Driver.AddStr ("Two");
  1323. top.Move (2, 17);
  1324. View.Driver.AddStr ("Three");
  1325. Application.Driver.Clip = savedClip;
  1326. Application.Current.DrawContentComplete -= Current_DrawContentComplete;
  1327. }
  1328. };
  1329. var dialog = new Dialog (btnPopup) { Width = 15, Height = 10 };
  1330. var rs = Application.Begin (dialog);
  1331. Assert.Equal (new Rect (2, 5, 15, 10), dialog.Frame);
  1332. TestHelpers.AssertDriverContentsWithFrameAre (@$"
  1333. ┌──────────────────┐
  1334. │ │
  1335. │ │
  1336. │ │
  1337. │ │
  1338. │ ┌─────────────┐ │
  1339. │ │ │ │
  1340. │ │ │ │
  1341. │ │ │ │
  1342. │ │ │ │
  1343. │ │ │ │
  1344. │ │ │ │
  1345. │ │ │ │
  1346. │ │ {CM.Glyphs.LeftBracket} Popup {CM.Glyphs.RightBracket} │ │
  1347. │ └─────────────┘ │
  1348. │ │
  1349. │ │
  1350. │ │
  1351. │ │
  1352. └──────────────────┘", output);
  1353. ReflectionTools.InvokePrivate (
  1354. typeof (Application),
  1355. "ProcessMouseEvent",
  1356. new MouseEvent () {
  1357. X = 9,
  1358. Y = 13,
  1359. Flags = MouseFlags.Button1Clicked
  1360. });
  1361. var firstIteration = false;
  1362. Application.RunMainLoopIteration (ref rs, ref firstIteration);
  1363. TestHelpers.AssertDriverContentsWithFrameAre (@$"
  1364. ┌──────────────────┐
  1365. │ │
  1366. │ │
  1367. │ │
  1368. │ │
  1369. │ ┌─────────────┐ │
  1370. │ │ │ │
  1371. │ │ │ │
  1372. │ │ │ │
  1373. │ │ │ │
  1374. │ │ │ │
  1375. │ │ │ │
  1376. │ │ │ │
  1377. │ │ {CM.Glyphs.LeftBracket} Popup {CM.Glyphs.RightBracket} │ │
  1378. │┌────────────────┐│
  1379. ││One ││
  1380. ││Two ││
  1381. ││Three ││
  1382. │└────────────────┘│
  1383. └──────────────────┘", output);
  1384. Application.End (rs);
  1385. }
  1386. [Fact, AutoInitShutdown]
  1387. public void Activating_MenuBar_By_Alt_Key_Does_Not_Throw ()
  1388. {
  1389. var menu = new MenuBar (new MenuBarItem [] {
  1390. new MenuBarItem ("Child", new MenuItem [] {
  1391. new MenuItem ("_Create Child", "", null)
  1392. })
  1393. });
  1394. var topChild = new Toplevel ();
  1395. topChild.Add (menu);
  1396. Application.Top.Add (topChild);
  1397. Application.Begin (Application.Top);
  1398. var exception = Record.Exception (() => topChild.ProcessHotKey (new KeyEvent (Key.AltMask, new KeyModifiers { Alt = true })));
  1399. Assert.Null (exception);
  1400. }
  1401. [Fact, TestRespondersDisposed]
  1402. public void Multi_Thread_Toplevels ()
  1403. {
  1404. Application.Init (new FakeDriver ());
  1405. var t = Application.Top;
  1406. var w = new Window ();
  1407. t.Add (w);
  1408. int count = 0, count1 = 0, count2 = 0;
  1409. bool log = false, log1 = false, log2 = false;
  1410. bool fromTopStillKnowFirstIsRunning = false;
  1411. bool fromTopStillKnowSecondIsRunning = false;
  1412. bool fromFirstStillKnowSecondIsRunning = false;
  1413. Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), (_) => {
  1414. count++;
  1415. if (count1 == 5) {
  1416. log1 = true;
  1417. }
  1418. if (count1 == 14 && count2 == 10 && count == 15) { // count2 is already stopped
  1419. fromTopStillKnowFirstIsRunning = true;
  1420. }
  1421. if (count1 == 7 && count2 == 7 && count == 8) {
  1422. fromTopStillKnowSecondIsRunning = true;
  1423. }
  1424. if (count == 30) {
  1425. Assert.Equal (30, count);
  1426. Assert.Equal (20, count1);
  1427. Assert.Equal (10, count2);
  1428. Assert.True (log);
  1429. Assert.True (log1);
  1430. Assert.True (log2);
  1431. Assert.True (fromTopStillKnowFirstIsRunning);
  1432. Assert.True (fromTopStillKnowSecondIsRunning);
  1433. Assert.True (fromFirstStillKnowSecondIsRunning);
  1434. Application.RequestStop ();
  1435. return false;
  1436. }
  1437. return true;
  1438. });
  1439. t.Ready += FirstDialogToplevel;
  1440. void FirstDialogToplevel (object sender, EventArgs args)
  1441. {
  1442. var od = new OpenDialog ();
  1443. od.Ready += SecondDialogToplevel;
  1444. Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), (_) => {
  1445. count1++;
  1446. if (count2 == 5) {
  1447. log2 = true;
  1448. }
  1449. if (count2 == 4 && count1 == 5 && count == 5) {
  1450. fromFirstStillKnowSecondIsRunning = true;
  1451. }
  1452. if (count1 == 20) {
  1453. Assert.Equal (20, count1);
  1454. Application.RequestStop ();
  1455. return false;
  1456. }
  1457. return true;
  1458. });
  1459. Application.Run (od);
  1460. }
  1461. void SecondDialogToplevel (object sender, EventArgs args)
  1462. {
  1463. var d = new Dialog ();
  1464. Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), (_) => {
  1465. count2++;
  1466. if (count < 30) {
  1467. log = true;
  1468. }
  1469. if (count2 == 10) {
  1470. Assert.Equal (10, count2);
  1471. Application.RequestStop ();
  1472. return false;
  1473. }
  1474. return true;
  1475. });
  1476. Application.Run (d);
  1477. }
  1478. Application.Run ();
  1479. Application.Shutdown ();
  1480. }
  1481. }
  1482. }