ToplevelTests.cs 65 KB

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