ToplevelTests.cs 60 KB

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