ToplevelTests.cs 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
  1. using Xunit.Abstractions;
  2. namespace Terminal.Gui.ViewsTests;
  3. public partial class ToplevelTests (ITestOutputHelper output)
  4. {
  5. [Fact]
  6. public void Constructor_Default ()
  7. {
  8. var top = new Toplevel ();
  9. Assert.Equal (Colors.ColorSchemes ["TopLevel"], top.ColorScheme);
  10. Assert.Equal ("Fill(Absolute(0))", top.Width.ToString ());
  11. Assert.Equal ("Fill(Absolute(0))", top.Height.ToString ());
  12. Assert.False (top.Running);
  13. Assert.False (top.Modal);
  14. Assert.Null (top.MenuBar);
  15. Assert.Null (top.StatusBar);
  16. }
  17. [Fact]
  18. public void Arrangement_Default_Is_Fixed ()
  19. {
  20. var top = new Toplevel ();
  21. Assert.Equal (ViewArrangement.Fixed, top.Arrangement);
  22. }
  23. [Fact]
  24. [AutoInitShutdown]
  25. public void Internal_Tests ()
  26. {
  27. var top = new Toplevel ();
  28. var eventInvoked = "";
  29. top.ChildUnloaded += (s, e) => eventInvoked = "ChildUnloaded";
  30. top.OnChildUnloaded (top);
  31. Assert.Equal ("ChildUnloaded", eventInvoked);
  32. top.ChildLoaded += (s, e) => eventInvoked = "ChildLoaded";
  33. top.OnChildLoaded (top);
  34. Assert.Equal ("ChildLoaded", eventInvoked);
  35. top.Closed += (s, e) => eventInvoked = "Closed";
  36. top.OnClosed (top);
  37. Assert.Equal ("Closed", eventInvoked);
  38. top.Closing += (s, e) => eventInvoked = "Closing";
  39. top.OnClosing (new (top));
  40. Assert.Equal ("Closing", eventInvoked);
  41. top.AllChildClosed += (s, e) => eventInvoked = "AllChildClosed";
  42. top.OnAllChildClosed ();
  43. Assert.Equal ("AllChildClosed", eventInvoked);
  44. top.ChildClosed += (s, e) => eventInvoked = "ChildClosed";
  45. top.OnChildClosed (top);
  46. Assert.Equal ("ChildClosed", eventInvoked);
  47. top.Deactivate += (s, e) => eventInvoked = "Deactivate";
  48. top.OnDeactivate (top);
  49. Assert.Equal ("Deactivate", eventInvoked);
  50. top.Activate += (s, e) => eventInvoked = "Activate";
  51. top.OnActivate (top);
  52. Assert.Equal ("Activate", eventInvoked);
  53. top.Loaded += (s, e) => eventInvoked = "Loaded";
  54. top.OnLoaded ();
  55. Assert.Equal ("Loaded", eventInvoked);
  56. top.Ready += (s, e) => eventInvoked = "Ready";
  57. top.OnReady ();
  58. Assert.Equal ("Ready", eventInvoked);
  59. top.Unloaded += (s, e) => eventInvoked = "Unloaded";
  60. top.OnUnloaded ();
  61. Assert.Equal ("Unloaded", eventInvoked);
  62. top.Add (new MenuBar ());
  63. Assert.NotNull (top.MenuBar);
  64. top.Add (new StatusBar ());
  65. Assert.NotNull (top.StatusBar);
  66. var menuBar = top.MenuBar;
  67. top.Remove (top.MenuBar);
  68. Assert.Null (top.MenuBar);
  69. Assert.NotNull (menuBar);
  70. var statusBar = top.StatusBar;
  71. top.Remove (top.StatusBar);
  72. Assert.Null (top.StatusBar);
  73. Assert.NotNull (statusBar);
  74. #if DEBUG_IDISPOSABLE
  75. Assert.False (menuBar.WasDisposed);
  76. Assert.False (statusBar.WasDisposed);
  77. menuBar.Dispose ();
  78. statusBar.Dispose ();
  79. Assert.True (menuBar.WasDisposed);
  80. Assert.True (statusBar.WasDisposed);
  81. #endif
  82. Application.Begin (top);
  83. Assert.Equal (top, Application.Top);
  84. // Application.Top without menu and status bar.
  85. View supView = View.GetLocationEnsuringFullVisibility (top, 2, 2, out int nx, out int ny, out StatusBar sb);
  86. Assert.Equal (Application.Top, supView);
  87. Assert.Equal (0, nx);
  88. Assert.Equal (0, ny);
  89. Assert.Null (sb);
  90. top.Add (new MenuBar ());
  91. Assert.NotNull (top.MenuBar);
  92. // Application.Top with a menu and without status bar.
  93. View.GetLocationEnsuringFullVisibility (top, 2, 2, out nx, out ny, out sb);
  94. Assert.Equal (0, nx);
  95. Assert.Equal (1, ny);
  96. Assert.Null (sb);
  97. top.Add (new StatusBar ());
  98. Assert.NotNull (top.StatusBar);
  99. // Application.Top with a menu and status bar.
  100. View.GetLocationEnsuringFullVisibility (top, 2, 2, out nx, out ny, out sb);
  101. Assert.Equal (0, nx);
  102. // The available height is lower than the Application.Top height minus
  103. // the menu bar and status bar, then the top can go beyond the bottom
  104. Assert.Equal (2, ny);
  105. Assert.NotNull (sb);
  106. menuBar = top.MenuBar;
  107. top.Remove (top.MenuBar);
  108. Assert.Null (top.MenuBar);
  109. Assert.NotNull (menuBar);
  110. // Application.Top without a menu and with a status bar.
  111. View.GetLocationEnsuringFullVisibility (top, 2, 2, out nx, out ny, out sb);
  112. Assert.Equal (0, nx);
  113. // The available height is lower than the Application.Top height minus
  114. // the status bar, then the top can go beyond the bottom
  115. Assert.Equal (2, ny);
  116. Assert.NotNull (sb);
  117. statusBar = top.StatusBar;
  118. top.Remove (top.StatusBar);
  119. Assert.Null (top.StatusBar);
  120. Assert.NotNull (statusBar);
  121. Assert.Null (top.MenuBar);
  122. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  123. top.Add (win);
  124. top.LayoutSubviews ();
  125. // The SuperView is always the same regardless of the caller.
  126. supView = View.GetLocationEnsuringFullVisibility (win, 0, 0, out nx, out ny, out sb);
  127. Assert.Equal (Application.Top, supView);
  128. supView = View.GetLocationEnsuringFullVisibility (win, 0, 0, out nx, out ny, out sb);
  129. Assert.Equal (Application.Top, supView);
  130. // Application.Top without menu and status bar.
  131. View.GetLocationEnsuringFullVisibility (win, 0, 0, out nx, out ny, out sb);
  132. Assert.Equal (0, nx);
  133. Assert.Equal (0, ny);
  134. Assert.Null (sb);
  135. top.Add (new MenuBar ());
  136. Assert.NotNull (top.MenuBar);
  137. // Application.Top with a menu and without status bar.
  138. View.GetLocationEnsuringFullVisibility (win, 2, 2, out nx, out ny, out sb);
  139. Assert.Equal (0, nx);
  140. Assert.Equal (1, ny);
  141. Assert.Null (sb);
  142. top.Add (new StatusBar ());
  143. Assert.NotNull (top.StatusBar);
  144. // Application.Top with a menu and status bar.
  145. View.GetLocationEnsuringFullVisibility (win, 30, 20, out nx, out ny, out sb);
  146. Assert.Equal (0, nx);
  147. // The available height is lower than the Application.Top height minus
  148. // the menu bar and status bar, then the top can go beyond the bottom
  149. Assert.Equal (20, ny);
  150. Assert.NotNull (sb);
  151. menuBar = top.MenuBar;
  152. statusBar = top.StatusBar;
  153. top.Remove (top.MenuBar);
  154. Assert.Null (top.MenuBar);
  155. Assert.NotNull (menuBar);
  156. top.Remove (top.StatusBar);
  157. Assert.Null (top.StatusBar);
  158. Assert.NotNull (statusBar);
  159. top.Remove (win);
  160. win = new () { Width = 60, Height = 15 };
  161. top.Add (win);
  162. // Application.Top without menu and status bar.
  163. View.GetLocationEnsuringFullVisibility (win, 0, 0, out nx, out ny, out sb);
  164. Assert.Equal (0, nx);
  165. Assert.Equal (0, ny);
  166. Assert.Null (sb);
  167. top.Add (new MenuBar ());
  168. Assert.NotNull (top.MenuBar);
  169. // Application.Top with a menu and without status bar.
  170. View.GetLocationEnsuringFullVisibility (win, 2, 2, out nx, out ny, out sb);
  171. Assert.Equal (2, nx);
  172. Assert.Equal (2, ny);
  173. Assert.Null (sb);
  174. top.Add (new StatusBar ());
  175. Assert.NotNull (top.StatusBar);
  176. // Application.Top with a menu and status bar.
  177. View.GetLocationEnsuringFullVisibility (win, 30, 20, out nx, out ny, out sb);
  178. Assert.Equal (20, nx); // 20+60=80
  179. Assert.Equal (9, ny); // 9+15+1(mb)=25
  180. Assert.NotNull (sb);
  181. top.PositionToplevels ();
  182. //Assert.Equal (new (0, 1, 60, 15), win.Frame);
  183. //Assert.Null (Toplevel._dragPosition);
  184. win.NewMouseEvent (new () { Position = new (6, 0), Flags = MouseFlags.Button1Pressed });
  185. // Assert.Equal (new Point (6, 0), Toplevel._dragPosition);
  186. win.NewMouseEvent (new () { Position = new (6, 0), Flags = MouseFlags.Button1Released });
  187. //Assert.Null (Toplevel._dragPosition);
  188. win.CanFocus = false;
  189. win.NewMouseEvent (new () { Position = new (6, 0), Flags = MouseFlags.Button1Pressed });
  190. //Assert.Null (Toplevel._dragPosition);
  191. #if DEBUG_IDISPOSABLE
  192. Assert.False (top.MenuBar.WasDisposed);
  193. Assert.False (top.StatusBar.WasDisposed);
  194. #endif
  195. menuBar = top.MenuBar;
  196. statusBar = top.StatusBar;
  197. top.Dispose ();
  198. Assert.Null (top.MenuBar);
  199. Assert.Null (top.StatusBar);
  200. Assert.NotNull (menuBar);
  201. Assert.NotNull (statusBar);
  202. #if DEBUG_IDISPOSABLE
  203. Assert.True (menuBar.WasDisposed);
  204. Assert.True (statusBar.WasDisposed);
  205. #endif
  206. }
  207. [Fact (Skip = "#2491 - Test is broken until #2491 is more mature.")]
  208. [AutoInitShutdown]
  209. public void KeyBindings_Command ()
  210. {
  211. var isRunning = false;
  212. var win1 = new Window { Id = "win1", Width = Dim.Percent (50), Height = Dim.Fill () };
  213. var lblTf1W1 = new Label { Id = "lblTf1W1", Text = "Enter text in TextField on Win1:" };
  214. var tf1W1 = new TextField
  215. {
  216. Id = "tf1W1", X = Pos.Right (lblTf1W1) + 1, Width = Dim.Fill (), Text = "Text1 on Win1"
  217. };
  218. var lblTvW1 = new Label
  219. {
  220. Id = "lblTvW1", Y = Pos.Bottom (lblTf1W1) + 1, Text = "Enter text in TextView on Win1:"
  221. };
  222. var tvW1 = new TextView
  223. {
  224. Id = "tvW1",
  225. X = Pos.Left (tf1W1),
  226. Width = Dim.Fill (),
  227. Height = 2,
  228. Text = "First line Win1\nSecond line Win1"
  229. };
  230. var lblTf2W1 = new Label
  231. {
  232. Id = "lblTf2W1", Y = Pos.Bottom (lblTvW1) + 1, Text = "Enter text in TextField on Win1:"
  233. };
  234. var tf2W1 = new TextField { Id = "tf2W1", X = Pos.Left (tf1W1), Width = Dim.Fill (), Text = "Text2 on Win1" };
  235. win1.Add (lblTf1W1, tf1W1, lblTvW1, tvW1, lblTf2W1, tf2W1);
  236. var win2 = new Window
  237. {
  238. Id = "win2", X = Pos.Right (win1) + 1, Width = Dim.Percent (50), Height = Dim.Fill ()
  239. };
  240. var lblTf1W2 = new Label { Id = "lblTf1W2", Text = "Enter text in TextField on Win2:" };
  241. var tf1W2 = new TextField
  242. {
  243. Id = "tf1W2", X = Pos.Right (lblTf1W2) + 1, Width = Dim.Fill (), Text = "Text1 on Win2"
  244. };
  245. var lblTvW2 = new Label
  246. {
  247. Id = "lblTvW2", Y = Pos.Bottom (lblTf1W2) + 1, Text = "Enter text in TextView on Win2:"
  248. };
  249. var tvW2 = new TextView
  250. {
  251. Id = "tvW2",
  252. X = Pos.Left (tf1W2),
  253. Width = Dim.Fill (),
  254. Height = 2,
  255. Text = "First line Win1\nSecond line Win2"
  256. };
  257. var lblTf2W2 = new Label
  258. {
  259. Id = "lblTf2W2", Y = Pos.Bottom (lblTvW2) + 1, Text = "Enter text in TextField on Win2:"
  260. };
  261. var tf2W2 = new TextField { Id = "tf2W2", X = Pos.Left (tf1W2), Width = Dim.Fill (), Text = "Text2 on Win2" };
  262. win2.Add (lblTf1W2, tf1W2, lblTvW2, tvW2, lblTf2W2, tf2W2);
  263. Toplevel top = new ();
  264. top.Add (win1, win2);
  265. top.Loaded += (s, e) => isRunning = true;
  266. top.Closing += (s, e) => isRunning = false;
  267. Application.Begin (top);
  268. top.Running = true;
  269. Assert.Equal (new (0, 0, 40, 25), win1.Frame);
  270. Assert.Equal (new (41, 0, 40, 25), win2.Frame);
  271. Assert.Equal (win1, top.Focused);
  272. Assert.Equal (tf1W1, top.MostFocused);
  273. Assert.True (isRunning);
  274. Assert.True (Application.OnKeyDown (Application.QuitKey));
  275. Assert.False (isRunning);
  276. Assert.True (Application.OnKeyDown (Key.Z.WithCtrl));
  277. Assert.True (Application.OnKeyDown (Key.F5)); // refresh
  278. Assert.True (Application.OnKeyDown (Key.Tab));
  279. Assert.Equal (win1, top.Focused);
  280. Assert.Equal (tvW1, top.MostFocused);
  281. Assert.True (Application.OnKeyDown (Key.Tab));
  282. Assert.Equal ($"\tFirst line Win1{Environment.NewLine}Second line Win1", tvW1.Text);
  283. Assert.True (Application.OnKeyDown (Key.Tab.WithShift));
  284. Assert.Equal ($"First line Win1{Environment.NewLine}Second line Win1", tvW1.Text);
  285. var prevMostFocusedSubview = top.MostFocused;
  286. Assert.True (Application.OnKeyDown (Key.F6)); // move to next TabGroup (win2)
  287. Assert.Equal (win2, top.Focused);
  288. Assert.True (Application.OnKeyDown (Key.F6.WithShift)); // move to prev TabGroup (win1)
  289. Assert.Equal (win1, top.Focused);
  290. Assert.Equal (tf2W1, top.MostFocused); // BUGBUG: Should be prevMostFocusedSubview - We need to cache the last focused view in the TabGroup somehow
  291. prevMostFocusedSubview.SetFocus ();
  292. Assert.Equal (tvW1, top.MostFocused);
  293. tf2W1.SetFocus ();
  294. Assert.True (Application.OnKeyDown (Key.Tab)); // tf2W1 is last subview in win1 - tabbing should take us to first subview of win1
  295. Assert.Equal (win1, top.Focused);
  296. Assert.Equal (tf1W1, top.MostFocused);
  297. Assert.True (Application.OnKeyDown (Key.CursorRight)); // move char to right in tf1W1. We're at last char so nav to next view
  298. Assert.Equal (win1, top.Focused);
  299. Assert.Equal (tvW1, top.MostFocused);
  300. Assert.True (Application.OnKeyDown (Key.CursorDown)); // move down to next view (tvW1)
  301. Assert.Equal (win1, top.Focused);
  302. Assert.Equal (tvW1, top.MostFocused);
  303. #if UNIX_KEY_BINDINGS
  304. Assert.True (Application.OnKeyDown (new (Key.I.WithCtrl)));
  305. Assert.Equal (win1, top.GetFocused ());
  306. Assert.Equal (tf2W1, top.MostFocused);
  307. #endif
  308. Assert.True (Application.OnKeyDown (Key.Tab.WithShift)); // Ignored. TextView eats shift-tab by default
  309. Assert.Equal (win1, top.Focused);
  310. Assert.Equal (tvW1, top.MostFocused);
  311. tvW1.AllowsTab = false;
  312. Assert.True (Application.OnKeyDown (Key.Tab.WithShift));
  313. Assert.Equal (win1, top.Focused);
  314. Assert.Equal (tf1W1, top.MostFocused);
  315. Assert.True (Application.OnKeyDown (Key.CursorLeft));
  316. Assert.Equal (win1, top.Focused);
  317. Assert.Equal (tf2W1, top.MostFocused);
  318. Assert.True (Application.OnKeyDown (Key.CursorUp));
  319. Assert.Equal (win1, top.Focused);
  320. Assert.Equal (tvW1, top.MostFocused);
  321. // nav to win2
  322. Assert.True (Application.OnKeyDown (Key.F6));
  323. Assert.Equal (win2, top.Focused);
  324. Assert.Equal (tf1W2, top.MostFocused);
  325. Assert.True (Application.OnKeyDown (Key.F6.WithShift));
  326. Assert.Equal (win1, top.Focused);
  327. Assert.Equal (tf2W1, top.MostFocused);
  328. Assert.True (Application.OnKeyDown (Application.NextTabGroupKey));
  329. Assert.Equal (win2, top.Focused);
  330. Assert.Equal (tf1W2, top.MostFocused);
  331. Assert.True (Application.OnKeyDown (Application.PrevTabGroupKey));
  332. Assert.Equal (win1, top.Focused);
  333. Assert.Equal (tf2W1, top.MostFocused);
  334. Assert.True (Application.OnKeyDown (Key.CursorUp));
  335. Assert.Equal (win1, top.Focused);
  336. Assert.Equal (tvW1, top.MostFocused);
  337. top.Dispose ();
  338. }
  339. [Fact]
  340. public void Added_Event_Should_Not_Be_Used_To_Initialize_Toplevel_Events ()
  341. {
  342. var wasAdded = false;
  343. var view = new View ();
  344. view.Added += View_Added;
  345. void View_Added (object sender, SuperViewChangedEventArgs e)
  346. {
  347. Assert.False (wasAdded);
  348. wasAdded = true;
  349. view.Added -= View_Added;
  350. }
  351. var win = new Window ();
  352. win.Add (view);
  353. Application.Init (new FakeDriver ());
  354. Toplevel top = new ();
  355. top.Add (win);
  356. Assert.True (wasAdded);
  357. Application.Shutdown ();
  358. }
  359. [Fact]
  360. [AutoInitShutdown]
  361. public void Mouse_Drag_On_Top_With_Superview_Null ()
  362. {
  363. var win = new Window ();
  364. Toplevel top = new ();
  365. top.Add (win);
  366. int iterations = -1;
  367. Window testWindow;
  368. Application.Iteration += (s, a) =>
  369. {
  370. iterations++;
  371. if (iterations == 0)
  372. {
  373. ((FakeDriver)Application.Driver!).SetBufferSize (15, 7);
  374. // Don't use MessageBox here; it's too complicated for this unit test; just use Window
  375. testWindow = new ()
  376. {
  377. Text = "Hello",
  378. X = 2,
  379. Y = 2,
  380. Width = 10,
  381. Height = 3,
  382. Arrangement = ViewArrangement.Movable
  383. };
  384. Application.Run (testWindow);
  385. }
  386. else if (iterations == 1)
  387. {
  388. Assert.Equal (new (2, 2), Application.Top!.Frame.Location);
  389. }
  390. else if (iterations == 2)
  391. {
  392. Assert.Null (Application.MouseGrabView);
  393. // Grab the mouse
  394. Application.OnMouseEvent (new () { Position = new (3, 2), Flags = MouseFlags.Button1Pressed });
  395. Assert.Equal (Application.Top!.Border, Application.MouseGrabView);
  396. Assert.Equal (new (2, 2, 10, 3), Application.Top.Frame);
  397. }
  398. else if (iterations == 3)
  399. {
  400. Assert.Equal (Application.Top!.Border, Application.MouseGrabView);
  401. // Drag to left
  402. Application.OnMouseEvent (
  403. new ()
  404. {
  405. Position = new (2, 2), Flags = MouseFlags.Button1Pressed
  406. | MouseFlags.ReportMousePosition
  407. });
  408. Application.Refresh ();
  409. Assert.Equal (Application.Top.Border, Application.MouseGrabView);
  410. Assert.Equal (new (1, 2, 10, 3), Application.Top.Frame);
  411. }
  412. else if (iterations == 4)
  413. {
  414. Assert.Equal (Application.Top!.Border, Application.MouseGrabView);
  415. Assert.Equal (new (1, 2), Application.Top.Frame.Location);
  416. Assert.Equal (Application.Top.Border, Application.MouseGrabView);
  417. }
  418. else if (iterations == 5)
  419. {
  420. Assert.Equal (Application.Top!.Border, Application.MouseGrabView);
  421. // Drag up
  422. Application.OnMouseEvent (
  423. new ()
  424. {
  425. Position = new (2, 1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  426. });
  427. Application.Refresh ();
  428. Assert.Equal (Application.Top!.Border, Application.MouseGrabView);
  429. Assert.Equal (new (1, 1, 10, 3), Application.Top.Frame);
  430. }
  431. else if (iterations == 6)
  432. {
  433. Assert.Equal (Application.Top!.Border, Application.MouseGrabView);
  434. Assert.Equal (new (1, 1), Application.Top.Frame.Location);
  435. Assert.Equal (Application.Top.Border, Application.MouseGrabView);
  436. Assert.Equal (new (1, 1, 10, 3), Application.Top.Frame);
  437. }
  438. else if (iterations == 7)
  439. {
  440. Assert.Equal (Application.Top!.Border, Application.MouseGrabView);
  441. // Ungrab the mouse
  442. Application.OnMouseEvent (new () { Position = new (2, 1), Flags = MouseFlags.Button1Released });
  443. Application.Refresh ();
  444. Assert.Null (Application.MouseGrabView);
  445. }
  446. else if (iterations == 8)
  447. {
  448. Application.RequestStop ();
  449. }
  450. else if (iterations == 9)
  451. {
  452. Application.RequestStop ();
  453. }
  454. };
  455. Application.Run (top);
  456. top.Dispose ();
  457. }
  458. [Fact]
  459. [AutoInitShutdown]
  460. public void Mouse_Drag_On_Top_With_Superview_Not_Null ()
  461. {
  462. var win = new Window { X = 3, Y = 2, Width = 10, Height = 5, Arrangement = ViewArrangement.Movable };
  463. Toplevel top = new ();
  464. top.Add (win);
  465. int iterations = -1;
  466. var movex = 0;
  467. var movey = 0;
  468. var location = new Rectangle (win.Frame.X, win.Frame.Y, 7, 3);
  469. Application.Iteration += (s, a) =>
  470. {
  471. iterations++;
  472. if (iterations == 0)
  473. {
  474. ((FakeDriver)Application.Driver!).SetBufferSize (30, 10);
  475. }
  476. else if (iterations == 1)
  477. {
  478. location = win.Frame;
  479. Assert.Null (Application.MouseGrabView);
  480. // Grab the mouse
  481. Application.OnMouseEvent (
  482. new ()
  483. {
  484. Position = new (win.Frame.X, win.Frame.Y), Flags = MouseFlags.Button1Pressed
  485. });
  486. Assert.Equal (win.Border, Application.MouseGrabView);
  487. }
  488. else if (iterations == 2)
  489. {
  490. Assert.Equal (win.Border, Application.MouseGrabView);
  491. // Drag to left
  492. movex = 1;
  493. movey = 0;
  494. Application.OnMouseEvent (
  495. new ()
  496. {
  497. Position = new (win.Frame.X + movex, win.Frame.Y + movey), Flags =
  498. MouseFlags.Button1Pressed
  499. | MouseFlags.ReportMousePosition
  500. });
  501. Assert.Equal (win.Border, Application.MouseGrabView);
  502. }
  503. else if (iterations == 3)
  504. {
  505. // we should have moved +1, +0
  506. Assert.Equal (win.Border, Application.MouseGrabView);
  507. Assert.Equal (win.Border, Application.MouseGrabView);
  508. location.Offset (movex, movey);
  509. }
  510. else if (iterations == 4)
  511. {
  512. Assert.Equal (win.Border, Application.MouseGrabView);
  513. // Drag up
  514. movex = 0;
  515. movey = -1;
  516. Application.OnMouseEvent (
  517. new ()
  518. {
  519. Position = new (win.Frame.X + movex, win.Frame.Y + movey), Flags =
  520. MouseFlags.Button1Pressed
  521. | MouseFlags.ReportMousePosition
  522. });
  523. Assert.Equal (win.Border, Application.MouseGrabView);
  524. }
  525. else if (iterations == 5)
  526. {
  527. // we should have moved +0, -1
  528. Assert.Equal (win.Border, Application.MouseGrabView);
  529. location.Offset (movex, movey);
  530. Assert.Equal (location, win.Frame);
  531. }
  532. else if (iterations == 6)
  533. {
  534. Assert.Equal (win.Border, Application.MouseGrabView);
  535. // Ungrab the mouse
  536. movex = 0;
  537. movey = 0;
  538. Application.OnMouseEvent (
  539. new ()
  540. {
  541. Position = new (win.Frame.X + movex, win.Frame.Y + movey),
  542. Flags = MouseFlags.Button1Released
  543. });
  544. Assert.Null (Application.MouseGrabView);
  545. }
  546. else if (iterations == 7)
  547. {
  548. Application.RequestStop ();
  549. }
  550. };
  551. Application.Run (top);
  552. top.Dispose ();
  553. }
  554. [Fact]
  555. [SetupFakeDriver]
  556. public void GetLocationThatFits_With_Border_Null_Not_Throws ()
  557. {
  558. var top = new Toplevel ();
  559. top.BeginInit ();
  560. top.EndInit ();
  561. Exception exception = Record.Exception (() => ((FakeDriver)Application.Driver!).SetBufferSize (0, 10));
  562. Assert.Null (exception);
  563. exception = Record.Exception (() => ((FakeDriver)Application.Driver!).SetBufferSize (10, 0));
  564. Assert.Null (exception);
  565. }
  566. [Fact]
  567. [AutoInitShutdown]
  568. public void PositionCursor_SetCursorVisibility_To_Invisible_If_Focused_Is_Null ()
  569. {
  570. var tf = new TextField { Width = 5, Text = "test" };
  571. var view = new View { Width = 10, Height = 10, CanFocus = true };
  572. view.Add (tf);
  573. var top = new Toplevel ();
  574. top.Add (view);
  575. Application.Begin (top);
  576. Assert.True (tf.HasFocus);
  577. Application.PositionCursor ();
  578. Application.Driver!.GetCursorVisibility (out CursorVisibility cursor);
  579. Assert.Equal (CursorVisibility.Default, cursor);
  580. view.Enabled = false;
  581. Assert.False (tf.HasFocus);
  582. Application.PositionCursor ();
  583. Application.Driver!.GetCursorVisibility (out cursor);
  584. Assert.Equal (CursorVisibility.Invisible, cursor);
  585. top.Dispose ();
  586. }
  587. [Fact]
  588. [AutoInitShutdown]
  589. public void IsLoaded_Application_Begin ()
  590. {
  591. Toplevel top = new ();
  592. Assert.False (top.IsLoaded);
  593. Application.Begin (top);
  594. Assert.True (top.IsLoaded);
  595. top.Dispose ();
  596. }
  597. [Fact]
  598. [AutoInitShutdown]
  599. public void IsLoaded_With_Sub_Toplevel_Application_Begin_NeedDisplay ()
  600. {
  601. Toplevel top = new ();
  602. var subTop = new Toplevel ();
  603. var view = new View { Frame = new (0, 0, 20, 10) };
  604. subTop.Add (view);
  605. top.Add (subTop);
  606. Assert.False (top.IsLoaded);
  607. Assert.False (subTop.IsLoaded);
  608. Assert.Equal (new (0, 0, 20, 10), view.Frame);
  609. view.LayoutStarted += ViewLayoutStarted;
  610. void ViewLayoutStarted (object sender, LayoutEventArgs e)
  611. {
  612. Assert.Equal (new (0, 0, 20, 10), view._needsDisplayRect);
  613. view.LayoutStarted -= ViewLayoutStarted;
  614. }
  615. Application.Begin (top);
  616. Assert.True (top.IsLoaded);
  617. Assert.True (subTop.IsLoaded);
  618. Assert.Equal (new (0, 0, 20, 10), view.Frame);
  619. view.Frame = new (1, 3, 10, 5);
  620. Assert.Equal (new (1, 3, 10, 5), view.Frame);
  621. Assert.Equal (new (0, 0, 10, 5), view._needsDisplayRect);
  622. view.OnDrawContent (view.Viewport);
  623. view.Frame = new (1, 3, 10, 5);
  624. Assert.Equal (new (1, 3, 10, 5), view.Frame);
  625. Assert.Equal (new (0, 0, 10, 5), view._needsDisplayRect);
  626. top.Dispose ();
  627. }
  628. [Fact]
  629. [AutoInitShutdown]
  630. public void Toplevel_Inside_ScrollView_MouseGrabView ()
  631. {
  632. var scrollView = new ScrollView
  633. {
  634. X = 3,
  635. Y = 3,
  636. Width = 40,
  637. Height = 16
  638. };
  639. scrollView.SetContentSize (new (200, 100));
  640. var win = new Window { X = 3, Y = 3, Width = Dim.Fill (3), Height = Dim.Fill (3), Arrangement = ViewArrangement.Movable };
  641. scrollView.Add (win);
  642. Toplevel top = new ();
  643. top.Add (scrollView);
  644. Application.Begin (top);
  645. Assert.Equal (new (0, 0, 80, 25), top.Frame);
  646. Assert.Equal (new (3, 3, 40, 16), scrollView.Frame);
  647. Assert.Equal (new (0, 0, 200, 100), scrollView.Subviews [0].Frame);
  648. Assert.Equal (new (3, 3, 194, 94), win.Frame);
  649. Application.OnMouseEvent (new () { Position = new (6, 6), Flags = MouseFlags.Button1Pressed });
  650. Assert.Equal (win.Border, Application.MouseGrabView);
  651. Assert.Equal (new (3, 3, 194, 94), win.Frame);
  652. Application.OnMouseEvent (new () { Position = new (9, 9), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition });
  653. Assert.Equal (win.Border, Application.MouseGrabView);
  654. top.SetNeedsLayout ();
  655. top.LayoutSubviews ();
  656. Assert.Equal (new (6, 6, 191, 91), win.Frame);
  657. Application.Refresh ();
  658. Application.OnMouseEvent (
  659. new ()
  660. {
  661. Position = new (5, 5), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  662. });
  663. Assert.Equal (win.Border, Application.MouseGrabView);
  664. top.SetNeedsLayout ();
  665. top.LayoutSubviews ();
  666. Assert.Equal (new (2, 2, 195, 95), win.Frame);
  667. Application.Refresh ();
  668. Application.OnMouseEvent (new () { Position = new (5, 5), Flags = MouseFlags.Button1Released });
  669. // ScrollView always grab the mouse when the container's subview OnMouseEnter don't want grab the mouse
  670. Assert.Equal (scrollView, Application.MouseGrabView);
  671. Application.OnMouseEvent (new () { Position = new (4, 4), Flags = MouseFlags.ReportMousePosition });
  672. Assert.Equal (scrollView, Application.MouseGrabView);
  673. top.Dispose ();
  674. }
  675. [Fact]
  676. [AutoInitShutdown]
  677. public void Window_Viewport_Bigger_Than_Driver_Cols_And_Rows_Allow_Drag_Beyond_Left_Right_And_Bottom ()
  678. {
  679. Toplevel top = new ();
  680. var window = new Window { Width = 20, Height = 3, Arrangement = ViewArrangement.Movable };
  681. RunState rsTop = Application.Begin (top);
  682. ((FakeDriver)Application.Driver!).SetBufferSize (40, 10);
  683. RunState rsWindow = Application.Begin (window);
  684. Application.Refresh ();
  685. Assert.Equal (new (0, 0, 40, 10), top.Frame);
  686. Assert.Equal (new (0, 0, 20, 3), window.Frame);
  687. Assert.Null (Application.MouseGrabView);
  688. Application.OnMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1Pressed });
  689. Assert.Equal (window.Border, Application.MouseGrabView);
  690. Application.OnMouseEvent (
  691. new ()
  692. {
  693. Position = new (-11, -4), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  694. });
  695. Application.Refresh ();
  696. Assert.Equal (new (0, 0, 40, 10), top.Frame);
  697. Assert.Equal (new (0, 0, 20, 3), window.Frame);
  698. // Changes Top size to same size as Dialog more menu and scroll bar
  699. ((FakeDriver)Application.Driver!).SetBufferSize (20, 3);
  700. Application.OnMouseEvent (
  701. new ()
  702. {
  703. Position = new (-1, -1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  704. });
  705. Application.Refresh ();
  706. Assert.Equal (new (0, 0, 20, 3), top.Frame);
  707. Assert.Equal (new (0, 0, 20, 3), window.Frame);
  708. // Changes Top size smaller than Dialog size
  709. ((FakeDriver)Application.Driver!).SetBufferSize (19, 2);
  710. Application.OnMouseEvent (
  711. new ()
  712. {
  713. Position = new (-1, -1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  714. });
  715. Application.Refresh ();
  716. Assert.Equal (new (0, 0, 19, 2), top.Frame);
  717. Assert.Equal (new (-1, 0, 20, 3), window.Frame);
  718. Application.OnMouseEvent (
  719. new ()
  720. {
  721. Position = new (18, 1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  722. });
  723. Application.Refresh ();
  724. Assert.Equal (new (0, 0, 19, 2), top.Frame);
  725. Assert.Equal (new (18, 1, 20, 3), window.Frame);
  726. // On a real app we can't go beyond the SuperView bounds
  727. Application.OnMouseEvent (
  728. new ()
  729. {
  730. Position = new (19, 2), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  731. });
  732. Application.Refresh ();
  733. Assert.Equal (new (0, 0, 19, 2), top.Frame);
  734. Assert.Equal (new (19, 2, 20, 3), window.Frame);
  735. TestHelpers.AssertDriverContentsWithFrameAre (@"", output);
  736. Application.End (rsWindow);
  737. Application.End (rsTop);
  738. top.Dispose ();
  739. }
  740. [Fact]
  741. [AutoInitShutdown]
  742. public void Modal_As_Top_Will_Drag_Cleanly ()
  743. {
  744. // Don't use Dialog as a Top, use a Window instead - dialog has complex layout behavior that is not needed here.
  745. var window = new Window { Width = 10, Height = 3, Arrangement = ViewArrangement.Movable };
  746. window.Add (
  747. new Label
  748. {
  749. X = Pos.Center (),
  750. Y = Pos.Center (),
  751. Width = Dim.Fill (),
  752. Height = Dim.Fill (),
  753. TextAlignment = Alignment.Center,
  754. VerticalTextAlignment = Alignment.Center,
  755. Text = "Test"
  756. }
  757. );
  758. RunState rs = Application.Begin (window);
  759. Assert.Null (Application.MouseGrabView);
  760. Assert.Equal (new (0, 0, 10, 3), window.Frame);
  761. Application.OnMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1Pressed });
  762. var firstIteration = false;
  763. Application.RunIteration (ref rs, ref firstIteration);
  764. Assert.Equal (window.Border, Application.MouseGrabView);
  765. Assert.Equal (new (0, 0, 10, 3), window.Frame);
  766. Application.OnMouseEvent (
  767. new ()
  768. {
  769. Position = new (1, 1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  770. });
  771. firstIteration = false;
  772. Application.RunIteration (ref rs, ref firstIteration);
  773. Assert.Equal (window.Border, Application.MouseGrabView);
  774. Assert.Equal (new (1, 1, 10, 3), window.Frame);
  775. Application.End (rs);
  776. window.Dispose ();
  777. }
  778. [Fact]
  779. [AutoInitShutdown]
  780. public void Begin_With_Window_Sets_Size_Correctly ()
  781. {
  782. Toplevel top = new ();
  783. RunState rsTop = Application.Begin (top);
  784. ((FakeDriver)Application.Driver!).SetBufferSize (20, 20);
  785. var testWindow = new Window { X = 2, Y = 1, Width = 15, Height = 10 };
  786. Assert.Equal (new (2, 1, 15, 10), testWindow.Frame);
  787. RunState rsTestWindow = Application.Begin (testWindow);
  788. Assert.Equal (new (2, 1, 15, 10), testWindow.Frame);
  789. Application.End (rsTestWindow);
  790. Application.End (rsTop);
  791. top.Dispose ();
  792. }
  793. // Don't use Dialog as a Top, use a Window instead - dialog has complex layout behavior that is not needed here.
  794. [Fact]
  795. [AutoInitShutdown]
  796. public void Draw_A_Top_Subview_On_A_Window ()
  797. {
  798. Toplevel top = new ();
  799. var win = new Window ();
  800. top.Add (win);
  801. RunState rsTop = Application.Begin (top);
  802. ((FakeDriver)Application.Driver!).SetBufferSize (20, 20);
  803. Assert.Equal (new (0, 0, 20, 20), win.Frame);
  804. var btnPopup = new Button { Text = "Popup" };
  805. var testWindow = new Window { X = 2, Y = 1, Width = 15, Height = 10 };
  806. testWindow.Add (btnPopup);
  807. btnPopup.Accept += (s, e) =>
  808. {
  809. Rectangle viewToScreen = btnPopup.ViewportToScreen (top.Frame);
  810. var viewAddedToTop = new View
  811. {
  812. Text = "viewAddedToTop",
  813. X = 1,
  814. Y = viewToScreen.Y + 1,
  815. Width = 18,
  816. Height = 16,
  817. BorderStyle = LineStyle.Single
  818. };
  819. Assert.Equal (testWindow, Application.Top);
  820. Application.Top!.DrawContentComplete += OnDrawContentComplete;
  821. top.Add (viewAddedToTop);
  822. void OnDrawContentComplete (object sender, DrawEventArgs e)
  823. {
  824. Assert.Equal (new (1, 3, 18, 16), viewAddedToTop.Frame);
  825. viewAddedToTop.SetNeedsDisplay();
  826. viewAddedToTop.Draw ();
  827. top.Move (2, 15);
  828. View.Driver.AddStr ("One");
  829. top.Move (2, 16);
  830. View.Driver.AddStr ("Two");
  831. top.Move (2, 17);
  832. View.Driver.AddStr ("Three");
  833. Application.Top!.DrawContentComplete -= OnDrawContentComplete;
  834. }
  835. };
  836. RunState rsTestWindow = Application.Begin (testWindow);
  837. Assert.Equal (new (2, 1, 15, 10), testWindow.Frame);
  838. Application.OnMouseEvent (new () { Position = new (5, 2), Flags = MouseFlags.Button1Clicked });
  839. Application.Refresh();
  840. TestHelpers.AssertDriverContentsWithFrameAre (
  841. @$"
  842. ┌──────────────────┐
  843. │ ┌─────────────┐ │
  844. │ │{CM.Glyphs.LeftBracket} Popup {CM.Glyphs.RightBracket} │ │
  845. │┌────────────────┐│
  846. ││viewAddedToTop ││
  847. ││ ││
  848. ││ ││
  849. ││ ││
  850. ││ ││
  851. ││ ││
  852. ││ ││
  853. ││ ││
  854. ││ ││
  855. ││ ││
  856. ││ ││
  857. ││One ││
  858. ││Two ││
  859. ││Three ││
  860. │└────────────────┘│
  861. └──────────────────┘",
  862. output
  863. );
  864. Application.End (rsTestWindow);
  865. Application.End (rsTop);
  866. top.Dispose ();
  867. }
  868. private void OnDrawContentComplete (object sender, DrawEventArgs e) { throw new NotImplementedException (); }
  869. [Fact]
  870. [AutoInitShutdown]
  871. public void Activating_MenuBar_By_Alt_Key_Does_Not_Throw ()
  872. {
  873. var menu = new MenuBar
  874. {
  875. Menus =
  876. [
  877. new ("Child", new MenuItem [] { new ("_Create Child", "", null) })
  878. ]
  879. };
  880. var topChild = new Toplevel ();
  881. topChild.Add (menu);
  882. var top = new Toplevel ();
  883. top.Add (topChild);
  884. Application.Begin (top);
  885. Exception exception = Record.Exception (() => topChild.NewKeyDownEvent (KeyCode.AltMask));
  886. Assert.Null (exception);
  887. top.Dispose ();
  888. }
  889. [Fact]
  890. [TestRespondersDisposed]
  891. public void Multi_Thread_Toplevels ()
  892. {
  893. Application.Init (new FakeDriver ());
  894. Toplevel t = new ();
  895. var w = new Window ();
  896. t.Add (w);
  897. int count = 0, count1 = 0, count2 = 0;
  898. bool log = false, log1 = false, log2 = false;
  899. var fromTopStillKnowFirstIsRunning = false;
  900. var fromTopStillKnowSecondIsRunning = false;
  901. var fromFirstStillKnowSecondIsRunning = false;
  902. Application.AddTimeout (
  903. TimeSpan.FromMilliseconds (100),
  904. () =>
  905. {
  906. count++;
  907. if (count1 == 5)
  908. {
  909. log1 = true;
  910. }
  911. if (count1 == 14 && count2 == 10 && count == 15)
  912. {
  913. // count2 is already stopped
  914. fromTopStillKnowFirstIsRunning = true;
  915. }
  916. if (count1 == 7 && count2 == 7 && count == 8)
  917. {
  918. fromTopStillKnowSecondIsRunning = true;
  919. }
  920. if (count == 30)
  921. {
  922. Assert.Equal (30, count);
  923. Assert.Equal (20, count1);
  924. Assert.Equal (10, count2);
  925. Assert.True (log);
  926. Assert.True (log1);
  927. Assert.True (log2);
  928. Assert.True (fromTopStillKnowFirstIsRunning);
  929. Assert.True (fromTopStillKnowSecondIsRunning);
  930. Assert.True (fromFirstStillKnowSecondIsRunning);
  931. Application.RequestStop ();
  932. return false;
  933. }
  934. return true;
  935. }
  936. );
  937. t.Ready += FirstWindow;
  938. void FirstWindow (object sender, EventArgs args)
  939. {
  940. var firstWindow = new Window ();
  941. firstWindow.Ready += SecondWindow;
  942. Application.AddTimeout (
  943. TimeSpan.FromMilliseconds (100),
  944. () =>
  945. {
  946. count1++;
  947. if (count2 == 5)
  948. {
  949. log2 = true;
  950. }
  951. if (count2 == 4 && count1 == 5 && count == 5)
  952. {
  953. fromFirstStillKnowSecondIsRunning = true;
  954. }
  955. if (count1 == 20)
  956. {
  957. Assert.Equal (20, count1);
  958. Application.RequestStop ();
  959. return false;
  960. }
  961. return true;
  962. }
  963. );
  964. Application.Run (firstWindow);
  965. firstWindow.Dispose ();
  966. }
  967. void SecondWindow (object sender, EventArgs args)
  968. {
  969. var testWindow = new Window ();
  970. Application.AddTimeout (
  971. TimeSpan.FromMilliseconds (100),
  972. () =>
  973. {
  974. count2++;
  975. if (count < 30)
  976. {
  977. log = true;
  978. }
  979. if (count2 == 10)
  980. {
  981. Assert.Equal (10, count2);
  982. Application.RequestStop ();
  983. return false;
  984. }
  985. return true;
  986. }
  987. );
  988. Application.Run (testWindow);
  989. testWindow.Dispose ();
  990. }
  991. Application.Run (t);
  992. t.Dispose ();
  993. Application.Shutdown ();
  994. }
  995. [Fact]
  996. public void Remove_Do_Not_Dispose_MenuBar_Or_StatusBar ()
  997. {
  998. var mb = new MenuBar ();
  999. var sb = new StatusBar ();
  1000. var tl = new Toplevel ();
  1001. #if DEBUG
  1002. Assert.False (mb.WasDisposed);
  1003. Assert.False (sb.WasDisposed);
  1004. #endif
  1005. tl.Add (mb, sb);
  1006. Assert.NotNull (tl.MenuBar);
  1007. Assert.NotNull (tl.StatusBar);
  1008. #if DEBUG
  1009. Assert.False (mb.WasDisposed);
  1010. Assert.False (sb.WasDisposed);
  1011. #endif
  1012. tl.RemoveAll ();
  1013. Assert.Null (tl.MenuBar);
  1014. Assert.Null (tl.StatusBar);
  1015. #if DEBUG
  1016. Assert.False (mb.WasDisposed);
  1017. Assert.False (sb.WasDisposed);
  1018. #endif
  1019. }
  1020. }