ToplevelTests.cs 74 KB

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