ToplevelTests.cs 65 KB

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