ToplevelTests.cs 57 KB

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