ToplevelTests.cs 92 KB

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