ToplevelTests.cs 65 KB

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