ToplevelTests.cs 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755
  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_Default_Is_Fixed ()
  25. {
  26. var top = new Toplevel ();
  27. Assert.Equal (ViewArrangement.Fixed, 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 { Position = new (6, 0), Flags = MouseFlags.Button1Pressed });
  325. // Assert.Equal (new Point (6, 0), Toplevel._dragPosition);
  326. win.NewMouseEvent (new MouseEvent { Position = new (6, 0), Flags = MouseFlags.Button1Released });
  327. //Assert.Null (Toplevel._dragPosition);
  328. win.CanFocus = false;
  329. win.NewMouseEvent (new MouseEvent { Position = new (6, 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 (50), 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 (50), 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 (50), 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 (50), 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. Arrangement = ViewArrangement.Movable
  755. };
  756. Application.Run (testWindow);
  757. }
  758. else if (iterations == 1)
  759. {
  760. Assert.Equal(new Point(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 Rectangle (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 (new ()
  775. {
  776. Position = new (2, 2), Flags = MouseFlags.Button1Pressed
  777. | MouseFlags.ReportMousePosition
  778. });
  779. Application.Refresh ();
  780. Assert.Equal (Application.Current.Border, Application.MouseGrabView);
  781. Assert.Equal (new Rectangle (1, 2, 10, 3), Application.Current.Frame);
  782. }
  783. else if (iterations == 4)
  784. {
  785. Assert.Equal (Application.Current.Border, Application.MouseGrabView);
  786. Assert.Equal (new Point (1, 2), Application.Current.Frame.Location);
  787. Assert.Equal (Application.Current.Border, Application.MouseGrabView);
  788. }
  789. else if (iterations == 5)
  790. {
  791. Assert.Equal (Application.Current.Border, Application.MouseGrabView);
  792. // Drag up
  793. Application.OnMouseEvent (new ()
  794. {
  795. Position = new (2, 1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  796. });
  797. Application.Refresh ();
  798. Assert.Equal (Application.Current.Border, Application.MouseGrabView);
  799. Assert.Equal (new Rectangle (1, 1, 10, 3), Application.Current.Frame);
  800. }
  801. else if (iterations == 6)
  802. {
  803. Assert.Equal (Application.Current.Border, Application.MouseGrabView);
  804. Assert.Equal (new Point (1, 1), Application.Current.Frame.Location);
  805. Assert.Equal (Application.Current.Border, Application.MouseGrabView);
  806. Assert.Equal (new Rectangle (1, 1, 10, 3), Application.Current.Frame);
  807. }
  808. else if (iterations == 7)
  809. {
  810. Assert.Equal (Application.Current.Border, Application.MouseGrabView);
  811. // Ungrab the mouse
  812. Application.OnMouseEvent (new () { Position = new (2, 1), Flags = MouseFlags.Button1Released });
  813. Application.Refresh ();
  814. Assert.Null (Application.MouseGrabView);
  815. }
  816. else if (iterations == 8)
  817. {
  818. Application.RequestStop ();
  819. }
  820. else if (iterations == 9)
  821. {
  822. Application.RequestStop ();
  823. }
  824. };
  825. Application.Run (top);
  826. }
  827. [Fact]
  828. [AutoInitShutdown]
  829. public void Mouse_Drag_On_Top_With_Superview_Not_Null ()
  830. {
  831. var win = new Window { X = 3, Y = 2, Width = 10, Height = 5, Arrangement = ViewArrangement.Movable };
  832. Toplevel top = new ();
  833. top.Add (win);
  834. int iterations = -1;
  835. var movex = 0;
  836. var movey = 0;
  837. var location = new Rectangle (win.Frame.X, win.Frame.Y, 7, 3);
  838. Application.Iteration += (s, a) =>
  839. {
  840. iterations++;
  841. if (iterations == 0)
  842. {
  843. ((FakeDriver)Application.Driver).SetBufferSize (30, 10);
  844. }
  845. else if (iterations == 1)
  846. {
  847. location = win.Frame;
  848. Assert.Null (Application.MouseGrabView);
  849. // Grab the mouse
  850. Application.OnMouseEvent (new MouseEvent
  851. {
  852. Position = new (win.Frame.X, win.Frame.Y), Flags = MouseFlags.Button1Pressed
  853. });
  854. Assert.Equal (win.Border, Application.MouseGrabView);
  855. }
  856. else if (iterations == 2)
  857. {
  858. Assert.Equal (win.Border, Application.MouseGrabView);
  859. // Drag to left
  860. movex = 1;
  861. movey = 0;
  862. Application.OnMouseEvent (new MouseEvent
  863. {
  864. Position = new (win.Frame.X + movex, win.Frame.Y + movey), Flags = MouseFlags.Button1Pressed
  865. | MouseFlags.ReportMousePosition
  866. });
  867. Assert.Equal (win.Border, Application.MouseGrabView);
  868. }
  869. else if (iterations == 3)
  870. {
  871. // we should have moved +1, +0
  872. Assert.Equal (win.Border, Application.MouseGrabView);
  873. Assert.Equal (win.Border, Application.MouseGrabView);
  874. location.Offset (movex, movey);
  875. }
  876. else if (iterations == 4)
  877. {
  878. Assert.Equal (win.Border, Application.MouseGrabView);
  879. // Drag up
  880. movex = 0;
  881. movey = -1;
  882. Application.OnMouseEvent (new MouseEvent
  883. {
  884. Position = new (win.Frame.X + movex, win.Frame.Y + movey), Flags = MouseFlags.Button1Pressed
  885. | MouseFlags.ReportMousePosition
  886. });
  887. Assert.Equal (win.Border, Application.MouseGrabView);
  888. }
  889. else if (iterations == 5)
  890. {
  891. // we should have moved +0, -1
  892. Assert.Equal (win.Border, Application.MouseGrabView);
  893. location.Offset (movex, movey);
  894. Assert.Equal (location, win.Frame);
  895. }
  896. else if (iterations == 6)
  897. {
  898. Assert.Equal (win.Border, Application.MouseGrabView);
  899. // Ungrab the mouse
  900. movex = 0;
  901. movey = 0;
  902. Application.OnMouseEvent (new MouseEvent
  903. {
  904. Position = new (win.Frame.X + movex, win.Frame.Y + movey), Flags = MouseFlags.Button1Released
  905. });
  906. Assert.Null (Application.MouseGrabView);
  907. }
  908. else if (iterations == 7)
  909. {
  910. Application.RequestStop ();
  911. }
  912. };
  913. Application.Run (top);
  914. }
  915. [Fact]
  916. [AutoInitShutdown]
  917. public void GetLocationThatFits_With_Border_Null_Not_Throws ()
  918. {
  919. var top = new Toplevel ();
  920. Application.Begin (top);
  921. Exception exception = Record.Exception (() => ((FakeDriver)Application.Driver).SetBufferSize (0, 10));
  922. Assert.Null (exception);
  923. exception = Record.Exception (() => ((FakeDriver)Application.Driver).SetBufferSize (10, 0));
  924. Assert.Null (exception);
  925. }
  926. [Fact]
  927. [AutoInitShutdown]
  928. public void OnEnter_OnLeave_Triggered_On_Application_Begin_End ()
  929. {
  930. var isEnter = false;
  931. var isLeave = false;
  932. var v = new View ();
  933. v.Enter += (s, _) => isEnter = true;
  934. v.Leave += (s, _) => isLeave = true;
  935. Toplevel top = new ();
  936. top.Add (v);
  937. Assert.False (v.CanFocus);
  938. Exception exception = Record.Exception (() => top.OnEnter (top));
  939. Assert.Null (exception);
  940. exception = Record.Exception (() => top.OnLeave (top));
  941. Assert.Null (exception);
  942. v.CanFocus = true;
  943. RunState rsTop = Application.Begin (top);
  944. // From the v view
  945. Assert.True (isEnter);
  946. // The Leave event is only raised on the End method
  947. // and the top is still running
  948. Assert.False (isLeave);
  949. isEnter = false;
  950. var d = new Dialog ();
  951. var dv = new View { CanFocus = true };
  952. dv.Enter += (s, _) => isEnter = true;
  953. dv.Leave += (s, _) => isLeave = true;
  954. d.Add (dv);
  955. RunState rsDialog = Application.Begin (d);
  956. // From the dv view
  957. Assert.True (isEnter);
  958. Assert.False (isLeave);
  959. Assert.True (dv.HasFocus);
  960. isEnter = false;
  961. Application.End (rsDialog);
  962. // From the v view
  963. Assert.True (isEnter);
  964. // From the dv view
  965. Assert.True (isLeave);
  966. Assert.True (v.HasFocus);
  967. Application.End (rsTop);
  968. }
  969. [Fact]
  970. [AutoInitShutdown]
  971. public void OnEnter_OnLeave_Triggered_On_Application_Begin_End_With_More_Toplevels ()
  972. {
  973. var iterations = 0;
  974. var steps = new int [4];
  975. var isEnterTop = false;
  976. var isLeaveTop = false;
  977. var vt = new View ();
  978. Toplevel top = new ();
  979. var diag = new Dialog ();
  980. vt.Enter += (s, e) =>
  981. {
  982. iterations++;
  983. isEnterTop = true;
  984. if (iterations == 1)
  985. {
  986. steps [0] = iterations;
  987. Assert.Null (e.View);
  988. }
  989. else
  990. {
  991. steps [3] = iterations;
  992. Assert.Equal (diag, e.View);
  993. }
  994. };
  995. vt.Leave += (s, e) =>
  996. {
  997. // This will never be raised
  998. iterations++;
  999. isLeaveTop = true;
  1000. Assert.Equal (diag, e.View);
  1001. };
  1002. top.Add (vt);
  1003. Assert.False (vt.CanFocus);
  1004. Exception exception = Record.Exception (() => top.OnEnter (top));
  1005. Assert.Null (exception);
  1006. exception = Record.Exception (() => top.OnLeave (top));
  1007. Assert.Null (exception);
  1008. vt.CanFocus = true;
  1009. RunState rsTop = Application.Begin (top);
  1010. Assert.True (isEnterTop);
  1011. Assert.False (isLeaveTop);
  1012. isEnterTop = false;
  1013. var isEnterDiag = false;
  1014. var isLeaveDiag = false;
  1015. var vd = new View ();
  1016. vd.Enter += (s, e) =>
  1017. {
  1018. iterations++;
  1019. steps [1] = iterations;
  1020. isEnterDiag = true;
  1021. Assert.Null (e.View);
  1022. };
  1023. vd.Leave += (s, e) =>
  1024. {
  1025. iterations++;
  1026. steps [2] = iterations;
  1027. isLeaveDiag = true;
  1028. Assert.Equal (top, e.View);
  1029. };
  1030. diag.Add (vd);
  1031. Assert.False (vd.CanFocus);
  1032. exception = Record.Exception (() => diag.OnEnter (diag));
  1033. Assert.Null (exception);
  1034. exception = Record.Exception (() => diag.OnLeave (diag));
  1035. Assert.Null (exception);
  1036. vd.CanFocus = true;
  1037. RunState rsDiag = Application.Begin (diag);
  1038. Assert.True (isEnterDiag);
  1039. Assert.False (isLeaveDiag);
  1040. Assert.False (isEnterTop);
  1041. // The Leave event is only raised on the End method
  1042. // and the top is still running
  1043. Assert.False (isLeaveTop);
  1044. isEnterDiag = false;
  1045. isLeaveTop = false;
  1046. Application.End (rsDiag);
  1047. Assert.False (isEnterDiag);
  1048. Assert.True (isLeaveDiag);
  1049. Assert.True (isEnterTop);
  1050. // Leave event on top cannot be raised
  1051. // because Current is null on the End method
  1052. Assert.False (isLeaveTop);
  1053. Assert.True (vt.HasFocus);
  1054. Application.End (rsTop);
  1055. Assert.Equal (1, steps [0]);
  1056. Assert.Equal (2, steps [1]);
  1057. Assert.Equal (3, steps [2]);
  1058. Assert.Equal (4, steps [^1]);
  1059. }
  1060. [Fact]
  1061. [AutoInitShutdown]
  1062. public void PositionCursor_SetCursorVisibility_To_Invisible_If_Focused_Is_Null ()
  1063. {
  1064. var tf = new TextField { Width = 5, Text = "test" };
  1065. var view = new View { Width = 10, Height = 10 };
  1066. view.Add (tf);
  1067. var top = new Toplevel ();
  1068. top.Add (view);
  1069. Application.Begin (top);
  1070. Assert.True (tf.HasFocus);
  1071. Application.PositionCursor (top);
  1072. Application.Driver.GetCursorVisibility (out CursorVisibility cursor);
  1073. Assert.Equal (CursorVisibility.Default, cursor);
  1074. view.Enabled = false;
  1075. Assert.False (tf.HasFocus);
  1076. Application.PositionCursor (top);
  1077. Application.Driver.GetCursorVisibility (out cursor);
  1078. Assert.Equal (CursorVisibility.Invisible, cursor);
  1079. }
  1080. [Fact]
  1081. [AutoInitShutdown]
  1082. public void IsLoaded_Application_Begin ()
  1083. {
  1084. Toplevel top = new ();
  1085. Assert.False (top.IsLoaded);
  1086. Application.Begin (top);
  1087. Assert.True (top.IsLoaded);
  1088. }
  1089. [Fact]
  1090. [AutoInitShutdown]
  1091. public void IsLoaded_With_Sub_Toplevel_Application_Begin_NeedDisplay ()
  1092. {
  1093. Toplevel top = new ();
  1094. var subTop = new Toplevel ();
  1095. var view = new View { Frame = new Rectangle (0, 0, 20, 10) };
  1096. subTop.Add (view);
  1097. top.Add (subTop);
  1098. Assert.False (top.IsLoaded);
  1099. Assert.False (subTop.IsLoaded);
  1100. Assert.Equal (new Rectangle (0, 0, 20, 10), view.Frame);
  1101. view.LayoutStarted += view_LayoutStarted;
  1102. void view_LayoutStarted (object sender, LayoutEventArgs e)
  1103. {
  1104. Assert.Equal (new Rectangle (0, 0, 20, 10), view._needsDisplayRect);
  1105. view.LayoutStarted -= view_LayoutStarted;
  1106. }
  1107. Application.Begin (top);
  1108. Assert.True (top.IsLoaded);
  1109. Assert.True (subTop.IsLoaded);
  1110. Assert.Equal (new Rectangle (0, 0, 20, 10), view.Frame);
  1111. view.Frame = new (1, 3, 10, 5);
  1112. Assert.Equal (new (1, 3, 10, 5), view.Frame);
  1113. Assert.Equal (new (0, 0, 10, 5), view._needsDisplayRect);
  1114. view.OnDrawContent (view.Viewport);
  1115. view.Frame = new (1, 3, 10, 5);
  1116. Assert.Equal (new (1, 3, 10, 5), view.Frame);
  1117. Assert.Equal (new (0, 0, 10, 5), view._needsDisplayRect);
  1118. }
  1119. [Fact]
  1120. [AutoInitShutdown]
  1121. public void Toplevel_Inside_ScrollView_MouseGrabView ()
  1122. {
  1123. var scrollView = new ScrollView
  1124. {
  1125. X = 3,
  1126. Y = 3,
  1127. Width = 40,
  1128. Height = 16,
  1129. };
  1130. scrollView.SetContentSize (new (200, 100));
  1131. var win = new Window { X = 3, Y = 3, Width = Dim.Fill (3), Height = Dim.Fill (3), Arrangement = ViewArrangement.Movable };
  1132. scrollView.Add (win);
  1133. Toplevel top = new ();
  1134. top.Add (scrollView);
  1135. Application.Begin (top);
  1136. Assert.Equal (new (0, 0, 80, 25), top.Frame);
  1137. Assert.Equal (new (3, 3, 40, 16), scrollView.Frame);
  1138. Assert.Equal (new (0, 0, 200, 100), scrollView.Subviews [0].Frame);
  1139. Assert.Equal (new (3, 3, 194, 94), win.Frame);
  1140. Application.OnMouseEvent (new MouseEvent { Position = new (6, 6), Flags = MouseFlags.Button1Pressed });
  1141. Assert.Equal (win.Border, Application.MouseGrabView);
  1142. Assert.Equal (new (3, 3, 194, 94), win.Frame);
  1143. Application.OnMouseEvent (new MouseEvent { Position = new (9, 9), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition });
  1144. Assert.Equal (win.Border, Application.MouseGrabView);
  1145. top.SetNeedsLayout ();
  1146. top.LayoutSubviews ();
  1147. Assert.Equal (new Rectangle (6, 6, 191, 91), win.Frame);
  1148. Application.Refresh ();
  1149. Application.OnMouseEvent (new MouseEvent
  1150. {
  1151. Position = new (5, 5), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1152. });
  1153. Assert.Equal (win.Border, Application.MouseGrabView);
  1154. top.SetNeedsLayout ();
  1155. top.LayoutSubviews ();
  1156. Assert.Equal (new Rectangle (2, 2, 195, 95), win.Frame);
  1157. Application.Refresh ();
  1158. Application.OnMouseEvent (new MouseEvent { Position = new (5, 5), Flags = MouseFlags.Button1Released });
  1159. // ScrollView always grab the mouse when the container's subview OnMouseEnter don't want grab the mouse
  1160. Assert.Equal (scrollView, Application.MouseGrabView);
  1161. Application.OnMouseEvent (new MouseEvent { Position = new (4, 4), Flags = MouseFlags.ReportMousePosition });
  1162. Assert.Equal (scrollView, Application.MouseGrabView);
  1163. }
  1164. [Fact]
  1165. [AutoInitShutdown]
  1166. public void Window_Viewport_Bigger_Than_Driver_Cols_And_Rows_Allow_Drag_Beyond_Left_Right_And_Bottom ()
  1167. {
  1168. Toplevel top = new ();
  1169. var window = new Window { Width = 20, Height = 3, Arrangement = ViewArrangement.Movable};
  1170. RunState rsTop = Application.Begin (top);
  1171. ((FakeDriver)Application.Driver).SetBufferSize (40, 10);
  1172. RunState rsWindow = Application.Begin (window);
  1173. Application.Refresh ();
  1174. Assert.Equal (new Rectangle (0, 0, 40, 10), top.Frame);
  1175. Assert.Equal (new Rectangle (0, 0, 20, 3), window.Frame);
  1176. Assert.Null (Application.MouseGrabView);
  1177. Application.OnMouseEvent (new MouseEvent { Position = new (0, 0), Flags = MouseFlags.Button1Pressed });
  1178. Assert.Equal (window.Border, Application.MouseGrabView);
  1179. Application.OnMouseEvent (new MouseEvent
  1180. {
  1181. Position = new (-11, -4), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1182. });
  1183. Application.Refresh ();
  1184. Assert.Equal (new Rectangle (0, 0, 40, 10), top.Frame);
  1185. Assert.Equal (new Rectangle (0, 0, 20, 3), window.Frame);
  1186. // Changes Top size to same size as Dialog more menu and scroll bar
  1187. ((FakeDriver)Application.Driver).SetBufferSize (20, 3);
  1188. Application.OnMouseEvent (new MouseEvent
  1189. {
  1190. Position = new (-1, -1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1191. });
  1192. Application.Refresh ();
  1193. Assert.Equal (new Rectangle (0, 0, 20, 3), top.Frame);
  1194. Assert.Equal (new Rectangle (0, 0, 20, 3), window.Frame);
  1195. // Changes Top size smaller than Dialog size
  1196. ((FakeDriver)Application.Driver).SetBufferSize (19, 2);
  1197. Application.OnMouseEvent (new MouseEvent
  1198. {
  1199. Position = new (-1, -1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1200. });
  1201. Application.Refresh ();
  1202. Assert.Equal (new Rectangle (0, 0, 19, 2), top.Frame);
  1203. Assert.Equal (new Rectangle (-1, 0, 20, 3), window.Frame);
  1204. Application.OnMouseEvent (new MouseEvent
  1205. {
  1206. Position = new (18, 1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1207. });
  1208. Application.Refresh ();
  1209. Assert.Equal (new Rectangle (0, 0, 19, 2), top.Frame);
  1210. Assert.Equal (new Rectangle (18, 1, 20, 3), window.Frame);
  1211. // On a real app we can't go beyond the SuperView bounds
  1212. Application.OnMouseEvent (new MouseEvent
  1213. {
  1214. Position = new (19, 2), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1215. });
  1216. Application.Refresh ();
  1217. Assert.Equal (new Rectangle (0, 0, 19, 2), top.Frame);
  1218. Assert.Equal (new Rectangle (19, 2, 20, 3), window.Frame);
  1219. TestHelpers.AssertDriverContentsWithFrameAre (@"", _output);
  1220. Application.End (rsWindow);
  1221. Application.End (rsTop);
  1222. }
  1223. [Fact]
  1224. [AutoInitShutdown]
  1225. public void Modal_As_Top_Will_Drag_Cleanly ()
  1226. {
  1227. // Don't use Dialog as a Top, use a Window instead - dialog has complex layout behavior that is not needed here.
  1228. var window = new Window { Width = 10, Height = 3, Arrangement = ViewArrangement.Movable };
  1229. window.Add (
  1230. new Label
  1231. {
  1232. X = Pos.Center (),
  1233. Y = Pos.Center (),
  1234. Width = Dim.Fill (),
  1235. Height = Dim.Fill (),
  1236. TextAlignment = Alignment.Center,
  1237. VerticalTextAlignment = Alignment.Center,
  1238. Text = "Test"
  1239. }
  1240. );
  1241. RunState rs = Application.Begin (window);
  1242. Assert.Null (Application.MouseGrabView);
  1243. Assert.Equal (new Rectangle (0, 0, 10, 3), window.Frame);
  1244. Application.OnMouseEvent (new MouseEvent { Position = new (0, 0), Flags = MouseFlags.Button1Pressed });
  1245. var firstIteration = false;
  1246. Application.RunIteration (ref rs, ref firstIteration);
  1247. Assert.Equal (window.Border, Application.MouseGrabView);
  1248. Assert.Equal (new Rectangle (0, 0, 10, 3), window.Frame);
  1249. Application.OnMouseEvent (new MouseEvent
  1250. {
  1251. Position = new (1, 1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1252. });
  1253. firstIteration = false;
  1254. Application.RunIteration (ref rs, ref firstIteration);
  1255. Assert.Equal (window.Border, Application.MouseGrabView);
  1256. Assert.Equal (new Rectangle (1, 1, 10, 3), window.Frame);
  1257. Application.End (rs);
  1258. }
  1259. [Fact]
  1260. [AutoInitShutdown]
  1261. public void Begin_With_Window_Sets_Size_Correctly ()
  1262. {
  1263. Toplevel top = new ();
  1264. RunState rsTop = Application.Begin (top);
  1265. ((FakeDriver)Application.Driver).SetBufferSize (20, 20);
  1266. var testWindow = new Window { X = 2, Y = 1, Width = 15, Height = 10 };
  1267. Assert.Equal (new Rectangle (2, 1, 15, 10), testWindow.Frame);
  1268. RunState rsTestWindow = Application.Begin (testWindow);
  1269. Assert.Equal (new Rectangle (2, 1, 15, 10), testWindow.Frame);
  1270. Application.End (rsTestWindow);
  1271. Application.End (rsTop);
  1272. }
  1273. // Don't use Dialog as a Top, use a Window instead - dialog has complex layout behavior that is not needed here.
  1274. [Fact]
  1275. [AutoInitShutdown]
  1276. public void Draw_A_Top_Subview_On_A_Window ()
  1277. {
  1278. Toplevel top = new ();
  1279. var win = new Window ();
  1280. top.Add (win);
  1281. RunState rsTop = Application.Begin (top);
  1282. ((FakeDriver)Application.Driver).SetBufferSize (20, 20);
  1283. Assert.Equal (new Rectangle (0, 0, 20, 20), win.Frame);
  1284. var btnPopup = new Button { Text = "Popup" };
  1285. var testWindow = new Window { X = 2, Y = 1, Width = 15, Height = 10 };
  1286. testWindow.Add (btnPopup);
  1287. btnPopup.Accept += (s, e) =>
  1288. {
  1289. Rectangle viewToScreen = btnPopup.ViewportToScreen (top.Frame);
  1290. var viewAddedToTop = new View
  1291. {
  1292. Text = "viewAddedToTop",
  1293. X = 1,
  1294. Y = viewToScreen.Y + 1,
  1295. Width = 18,
  1296. Height = 16,
  1297. BorderStyle = LineStyle.Single
  1298. };
  1299. Assert.Equal (testWindow, Application.Current);
  1300. Application.Current.DrawContentComplete += OnDrawContentComplete;
  1301. top.Add (viewAddedToTop);
  1302. void OnDrawContentComplete (object sender, DrawEventArgs e)
  1303. {
  1304. Assert.Equal (new Rectangle (1, 3, 18, 16), viewAddedToTop.Frame);
  1305. Rectangle savedClip = Application.Driver.Clip;
  1306. Application.Driver.Clip = top.Frame;
  1307. viewAddedToTop.Draw ();
  1308. top.Move (2, 15);
  1309. View.Driver.AddStr ("One");
  1310. top.Move (2, 16);
  1311. View.Driver.AddStr ("Two");
  1312. top.Move (2, 17);
  1313. View.Driver.AddStr ("Three");
  1314. Application.Driver.Clip = savedClip;
  1315. Application.Current.DrawContentComplete -= OnDrawContentComplete;
  1316. }
  1317. };
  1318. RunState rsTestWindow = Application.Begin (testWindow);
  1319. Assert.Equal (new Rectangle (2, 1, 15, 10), testWindow.Frame);
  1320. Application.OnMouseEvent (new () { Position = new (5, 2), Flags = MouseFlags.Button1Clicked });
  1321. Application.Top.Draw ();
  1322. var firstIteration = false;
  1323. Application.RunIteration (ref rsTestWindow, ref firstIteration);
  1324. TestHelpers.AssertDriverContentsWithFrameAre (
  1325. @$"
  1326. ┌──────────────────┐
  1327. │ ┌─────────────┐ │
  1328. │ │{CM.Glyphs.LeftBracket} Popup {CM.Glyphs.RightBracket} │ │
  1329. │┌────────────────┐│
  1330. ││viewAddedToTop ││
  1331. ││ ││
  1332. ││ ││
  1333. ││ ││
  1334. ││ ││
  1335. ││ ││
  1336. ││ ││
  1337. ││ ││
  1338. ││ ││
  1339. ││ ││
  1340. ││ ││
  1341. ││One ││
  1342. ││Two ││
  1343. ││Three ││
  1344. │└────────────────┘│
  1345. └──────────────────┘",
  1346. _output
  1347. );
  1348. Application.End (rsTestWindow);
  1349. Application.End (rsTop);
  1350. }
  1351. private void OnDrawContentComplete (object sender, DrawEventArgs e) { throw new NotImplementedException (); }
  1352. [Fact]
  1353. [AutoInitShutdown]
  1354. public void Activating_MenuBar_By_Alt_Key_Does_Not_Throw ()
  1355. {
  1356. var menu = new MenuBar
  1357. {
  1358. Menus =
  1359. [
  1360. new MenuBarItem ("Child", new MenuItem [] { new ("_Create Child", "", null) })
  1361. ]
  1362. };
  1363. var topChild = new Toplevel ();
  1364. topChild.Add (menu);
  1365. var top = new Toplevel ();
  1366. top.Add (topChild);
  1367. Application.Begin (top);
  1368. Exception exception = Record.Exception (() => topChild.NewKeyDownEvent (KeyCode.AltMask));
  1369. Assert.Null (exception);
  1370. }
  1371. [Fact]
  1372. [TestRespondersDisposed]
  1373. public void Multi_Thread_Toplevels ()
  1374. {
  1375. Application.Init (new FakeDriver ());
  1376. Toplevel t = new ();
  1377. var w = new Window ();
  1378. t.Add (w);
  1379. int count = 0, count1 = 0, count2 = 0;
  1380. bool log = false, log1 = false, log2 = false;
  1381. var fromTopStillKnowFirstIsRunning = false;
  1382. var fromTopStillKnowSecondIsRunning = false;
  1383. var fromFirstStillKnowSecondIsRunning = false;
  1384. Application.AddTimeout (
  1385. TimeSpan.FromMilliseconds (100),
  1386. () =>
  1387. {
  1388. count++;
  1389. if (count1 == 5)
  1390. {
  1391. log1 = true;
  1392. }
  1393. if (count1 == 14 && count2 == 10 && count == 15)
  1394. {
  1395. // count2 is already stopped
  1396. fromTopStillKnowFirstIsRunning = true;
  1397. }
  1398. if (count1 == 7 && count2 == 7 && count == 8)
  1399. {
  1400. fromTopStillKnowSecondIsRunning = true;
  1401. }
  1402. if (count == 30)
  1403. {
  1404. Assert.Equal (30, count);
  1405. Assert.Equal (20, count1);
  1406. Assert.Equal (10, count2);
  1407. Assert.True (log);
  1408. Assert.True (log1);
  1409. Assert.True (log2);
  1410. Assert.True (fromTopStillKnowFirstIsRunning);
  1411. Assert.True (fromTopStillKnowSecondIsRunning);
  1412. Assert.True (fromFirstStillKnowSecondIsRunning);
  1413. Application.RequestStop ();
  1414. return false;
  1415. }
  1416. return true;
  1417. }
  1418. );
  1419. t.Ready += FirstWindow;
  1420. void FirstWindow (object sender, EventArgs args)
  1421. {
  1422. var firstWindow = new Window ();
  1423. firstWindow.Ready += SecondWindow;
  1424. Application.AddTimeout (
  1425. TimeSpan.FromMilliseconds (100),
  1426. () =>
  1427. {
  1428. count1++;
  1429. if (count2 == 5)
  1430. {
  1431. log2 = true;
  1432. }
  1433. if (count2 == 4 && count1 == 5 && count == 5)
  1434. {
  1435. fromFirstStillKnowSecondIsRunning = true;
  1436. }
  1437. if (count1 == 20)
  1438. {
  1439. Assert.Equal (20, count1);
  1440. Application.RequestStop ();
  1441. return false;
  1442. }
  1443. return true;
  1444. }
  1445. );
  1446. Application.Run (firstWindow);
  1447. firstWindow.Dispose ();
  1448. }
  1449. void SecondWindow (object sender, EventArgs args)
  1450. {
  1451. var testWindow = new Window ();
  1452. Application.AddTimeout (
  1453. TimeSpan.FromMilliseconds (100),
  1454. () =>
  1455. {
  1456. count2++;
  1457. if (count < 30)
  1458. {
  1459. log = true;
  1460. }
  1461. if (count2 == 10)
  1462. {
  1463. Assert.Equal (10, count2);
  1464. Application.RequestStop ();
  1465. return false;
  1466. }
  1467. return true;
  1468. }
  1469. );
  1470. Application.Run (testWindow);
  1471. testWindow.Dispose ();
  1472. }
  1473. Application.Run (t);
  1474. t.Dispose ();
  1475. Application.Shutdown ();
  1476. }
  1477. }