ToplevelTests.cs 62 KB

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