ToplevelTests.cs 91 KB

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