ToplevelTests.cs 75 KB

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