ToplevelTests.cs 73 KB

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