2
0

ToplevelTests.cs 64 KB

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