ToplevelTests.cs 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572
  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 (Skip = "#2491 - Test is broken until #2491 is more mature.")]
  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.F6)); // move to next TabGroup (win2)
  409. Assert.Equal (win2, top.Focused);
  410. Assert.True (Application.OnKeyDown (Key.F6.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.F6));
  445. Assert.Equal (win2, top.Focused);
  446. Assert.Equal (tf1W2, top.MostFocused);
  447. Assert.True (Application.OnKeyDown (Key.F6.WithShift));
  448. Assert.Equal (win1, top.Focused);
  449. Assert.Equal (tf2W1, top.MostFocused);
  450. Assert.True (Application.OnKeyDown (Application.NextTabGroupKey));
  451. Assert.Equal (win2, top.Focused);
  452. Assert.Equal (tf1W2, top.MostFocused);
  453. Assert.True (Application.OnKeyDown (Application.PrevTabGroupKey));
  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. #if V2_NEW_FOCUS_IMPL
  689. [Fact]
  690. [AutoInitShutdown]
  691. public void OnEnter_OnLeave_Triggered_On_Application_Begin_End ()
  692. {
  693. var viewEnterInvoked = false;
  694. var viewLeaveInvoked = false;
  695. var v = new View ();
  696. v.Enter += (s, _) => viewEnterInvoked = true;
  697. v.Leave += (s, _) => viewLeaveInvoked = true;
  698. Toplevel top = new ();
  699. top.Add (v);
  700. Assert.False (v.CanFocus);
  701. Exception exception = Record.Exception (() => top.OnEnter (top));
  702. Assert.Null (exception);
  703. exception = Record.Exception (() => top.OnLeave (top));
  704. Assert.Null (exception);
  705. v.CanFocus = true;
  706. RunState rsTop = Application.Begin (top);
  707. Assert.True (top.HasFocus);
  708. Assert.True (v.HasFocus);
  709. // From the v view
  710. Assert.True (viewEnterInvoked);
  711. // The Leave event is only raised on the End method
  712. // and the top is still running
  713. Assert.False (viewLeaveInvoked);
  714. Assert.False (viewLeaveInvoked);
  715. Application.End (rsTop);
  716. Assert.True (viewLeaveInvoked);
  717. top.Dispose ();
  718. }
  719. [Fact]
  720. [AutoInitShutdown]
  721. public void OnEnter_OnLeave_Triggered_On_Application_Begin_End_With_Modal ()
  722. {
  723. var viewEnterInvoked = false;
  724. var viewLeaveInvoked = false;
  725. var v = new View ();
  726. v.Enter += (s, _) => viewEnterInvoked = true;
  727. v.Leave += (s, _) => viewLeaveInvoked = true;
  728. Toplevel top = new ();
  729. top.Add (v);
  730. Assert.False (v.CanFocus);
  731. Exception exception = Record.Exception (() => top.OnEnter (top));
  732. Assert.Null (exception);
  733. exception = Record.Exception (() => top.OnLeave (top));
  734. Assert.Null (exception);
  735. v.CanFocus = true;
  736. RunState rsTop = Application.Begin (top);
  737. // From the v view
  738. Assert.True (viewEnterInvoked);
  739. // The Leave event is only raised on the End method
  740. // and the top is still running
  741. Assert.False (viewLeaveInvoked);
  742. var dlgEnterInvoked = false;
  743. var dlgLeaveInvoked = false;
  744. var d = new Dialog ();
  745. var dv = new View { CanFocus = true };
  746. dv.Enter += (s, _) => dlgEnterInvoked = true;
  747. dv.Leave += (s, _) => dlgLeaveInvoked = true;
  748. d.Add (dv);
  749. RunState rsDialog = Application.Begin (d);
  750. // From the dv view
  751. Assert.True (dlgEnterInvoked);
  752. Assert.False (dlgLeaveInvoked);
  753. Assert.True (dv.HasFocus);
  754. Assert.True (viewLeaveInvoked);
  755. viewEnterInvoked = false;
  756. viewLeaveInvoked = false;
  757. Application.End (rsDialog);
  758. d.Dispose ();
  759. // From the v view
  760. Assert.True (viewEnterInvoked);
  761. // From the dv view
  762. Assert.True (dlgEnterInvoked);
  763. Assert.True (dlgLeaveInvoked);
  764. Assert.True (v.HasFocus);
  765. Assert.False (viewLeaveInvoked);
  766. Application.End (rsTop);
  767. Assert.True (viewLeaveInvoked);
  768. top.Dispose ();
  769. }
  770. [Fact (Skip = "2491: This is a bogus test that is impossible to figure out. Replace with something simpler.")]
  771. [AutoInitShutdown]
  772. public void OnEnter_OnLeave_Triggered_On_Application_Begin_End_With_More_Toplevels ()
  773. {
  774. var iterations = 0;
  775. var steps = new int [4];
  776. var isEnterTop = false;
  777. var isLeaveTop = false;
  778. var subViewofTop = new View ();
  779. Toplevel top = new ();
  780. var dlg = new Dialog ();
  781. subViewofTop.Enter += (s, e) =>
  782. {
  783. iterations++;
  784. isEnterTop = true;
  785. if (iterations == 1)
  786. {
  787. steps [0] = iterations;
  788. Assert.Null (e.Leaving);
  789. }
  790. else
  791. {
  792. steps [3] = iterations;
  793. Assert.Equal (dlg, e.Leaving);
  794. }
  795. };
  796. subViewofTop.Leave += (s, e) =>
  797. {
  798. // This will never be raised
  799. iterations++;
  800. isLeaveTop = true;
  801. //Assert.Equal (dlg, e.Leaving);
  802. };
  803. top.Add (subViewofTop);
  804. Assert.False (subViewofTop.CanFocus);
  805. Exception exception = Record.Exception (() => top.OnEnter (top));
  806. Assert.Null (exception);
  807. exception = Record.Exception (() => top.OnLeave (top));
  808. Assert.Null (exception);
  809. subViewofTop.CanFocus = true;
  810. RunState rsTop = Application.Begin (top);
  811. Assert.True (isEnterTop);
  812. Assert.False (isLeaveTop);
  813. isEnterTop = false;
  814. var isEnterDiag = false;
  815. var isLeaveDiag = false;
  816. var subviewOfDlg = new View ();
  817. subviewOfDlg.Enter += (s, e) =>
  818. {
  819. iterations++;
  820. steps [1] = iterations;
  821. isEnterDiag = true;
  822. Assert.Null (e.Leaving);
  823. };
  824. subviewOfDlg.Leave += (s, e) =>
  825. {
  826. iterations++;
  827. steps [2] = iterations;
  828. isLeaveDiag = true;
  829. Assert.Equal (top, e.Entering);
  830. };
  831. dlg.Add (subviewOfDlg);
  832. Assert.False (subviewOfDlg.CanFocus);
  833. exception = Record.Exception (() => dlg.OnEnter (dlg));
  834. Assert.Null (exception);
  835. exception = Record.Exception (() => dlg.OnLeave (dlg));
  836. Assert.Null (exception);
  837. subviewOfDlg.CanFocus = true;
  838. RunState rsDiag = Application.Begin (dlg);
  839. Assert.True (isEnterDiag);
  840. Assert.False (isLeaveDiag);
  841. Assert.False (isEnterTop);
  842. // The Leave event is only raised on the End method
  843. // and the top is still running
  844. Assert.False (isLeaveTop);
  845. isEnterDiag = false;
  846. isLeaveTop = false;
  847. Application.End (rsDiag);
  848. dlg.Dispose ();
  849. Assert.False (isEnterDiag);
  850. Assert.True (isLeaveDiag);
  851. Assert.True (isEnterTop);
  852. // Leave event on top cannot be raised
  853. // because Current is null on the End method
  854. Assert.False (isLeaveTop);
  855. Assert.True (subViewofTop.HasFocus);
  856. Application.End (rsTop);
  857. Assert.Equal (1, steps [0]);
  858. Assert.Equal (2, steps [1]);
  859. Assert.Equal (3, steps [2]);
  860. Assert.Equal (4, steps [^1]);
  861. top.Dispose ();
  862. }
  863. #endif
  864. [Fact]
  865. [AutoInitShutdown]
  866. public void PositionCursor_SetCursorVisibility_To_Invisible_If_Focused_Is_Null ()
  867. {
  868. var tf = new TextField { Width = 5, Text = "test" };
  869. var view = new View { Width = 10, Height = 10 };
  870. view.Add (tf);
  871. var top = new Toplevel ();
  872. top.Add (view);
  873. Application.Begin (top);
  874. Assert.True (tf.HasFocus);
  875. Application.PositionCursor (top);
  876. Application.Driver!.GetCursorVisibility (out CursorVisibility cursor);
  877. Assert.Equal (CursorVisibility.Default, cursor);
  878. view.Enabled = false;
  879. Assert.False (tf.HasFocus);
  880. Application.PositionCursor (top);
  881. Application.Driver!.GetCursorVisibility (out cursor);
  882. Assert.Equal (CursorVisibility.Invisible, cursor);
  883. top.Dispose ();
  884. }
  885. [Fact]
  886. [AutoInitShutdown]
  887. public void IsLoaded_Application_Begin ()
  888. {
  889. Toplevel top = new ();
  890. Assert.False (top.IsLoaded);
  891. Application.Begin (top);
  892. Assert.True (top.IsLoaded);
  893. top.Dispose ();
  894. }
  895. [Fact]
  896. [AutoInitShutdown]
  897. public void IsLoaded_With_Sub_Toplevel_Application_Begin_NeedDisplay ()
  898. {
  899. Toplevel top = new ();
  900. var subTop = new Toplevel ();
  901. var view = new View { Frame = new (0, 0, 20, 10) };
  902. subTop.Add (view);
  903. top.Add (subTop);
  904. Assert.False (top.IsLoaded);
  905. Assert.False (subTop.IsLoaded);
  906. Assert.Equal (new (0, 0, 20, 10), view.Frame);
  907. view.LayoutStarted += ViewLayoutStarted;
  908. void ViewLayoutStarted (object sender, LayoutEventArgs e)
  909. {
  910. Assert.Equal (new (0, 0, 20, 10), view._needsDisplayRect);
  911. view.LayoutStarted -= ViewLayoutStarted;
  912. }
  913. Application.Begin (top);
  914. Assert.True (top.IsLoaded);
  915. Assert.True (subTop.IsLoaded);
  916. Assert.Equal (new (0, 0, 20, 10), view.Frame);
  917. view.Frame = new (1, 3, 10, 5);
  918. Assert.Equal (new (1, 3, 10, 5), view.Frame);
  919. Assert.Equal (new (0, 0, 10, 5), view._needsDisplayRect);
  920. view.OnDrawContent (view.Viewport);
  921. view.Frame = new (1, 3, 10, 5);
  922. Assert.Equal (new (1, 3, 10, 5), view.Frame);
  923. Assert.Equal (new (0, 0, 10, 5), view._needsDisplayRect);
  924. top.Dispose ();
  925. }
  926. [Fact]
  927. [AutoInitShutdown]
  928. public void Toplevel_Inside_ScrollView_MouseGrabView ()
  929. {
  930. var scrollView = new ScrollView
  931. {
  932. X = 3,
  933. Y = 3,
  934. Width = 40,
  935. Height = 16
  936. };
  937. scrollView.SetContentSize (new (200, 100));
  938. var win = new Window { X = 3, Y = 3, Width = Dim.Fill (3), Height = Dim.Fill (3), Arrangement = ViewArrangement.Movable };
  939. scrollView.Add (win);
  940. Toplevel top = new ();
  941. top.Add (scrollView);
  942. Application.Begin (top);
  943. Assert.Equal (new (0, 0, 80, 25), top.Frame);
  944. Assert.Equal (new (3, 3, 40, 16), scrollView.Frame);
  945. Assert.Equal (new (0, 0, 200, 100), scrollView.Subviews [0].Frame);
  946. Assert.Equal (new (3, 3, 194, 94), win.Frame);
  947. Application.OnMouseEvent (new () { Position = new (6, 6), Flags = MouseFlags.Button1Pressed });
  948. Assert.Equal (win.Border, Application.MouseGrabView);
  949. Assert.Equal (new (3, 3, 194, 94), win.Frame);
  950. Application.OnMouseEvent (new () { Position = new (9, 9), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition });
  951. Assert.Equal (win.Border, Application.MouseGrabView);
  952. top.SetNeedsLayout ();
  953. top.LayoutSubviews ();
  954. Assert.Equal (new (6, 6, 191, 91), win.Frame);
  955. Application.Refresh ();
  956. Application.OnMouseEvent (
  957. new ()
  958. {
  959. Position = new (5, 5), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  960. });
  961. Assert.Equal (win.Border, Application.MouseGrabView);
  962. top.SetNeedsLayout ();
  963. top.LayoutSubviews ();
  964. Assert.Equal (new (2, 2, 195, 95), win.Frame);
  965. Application.Refresh ();
  966. Application.OnMouseEvent (new () { Position = new (5, 5), Flags = MouseFlags.Button1Released });
  967. // ScrollView always grab the mouse when the container's subview OnMouseEnter don't want grab the mouse
  968. Assert.Equal (scrollView, Application.MouseGrabView);
  969. Application.OnMouseEvent (new () { Position = new (4, 4), Flags = MouseFlags.ReportMousePosition });
  970. Assert.Equal (scrollView, Application.MouseGrabView);
  971. top.Dispose ();
  972. }
  973. [Fact]
  974. [AutoInitShutdown]
  975. public void Window_Viewport_Bigger_Than_Driver_Cols_And_Rows_Allow_Drag_Beyond_Left_Right_And_Bottom ()
  976. {
  977. Toplevel top = new ();
  978. var window = new Window { Width = 20, Height = 3, Arrangement = ViewArrangement.Movable };
  979. RunState rsTop = Application.Begin (top);
  980. ((FakeDriver)Application.Driver!).SetBufferSize (40, 10);
  981. RunState rsWindow = Application.Begin (window);
  982. Application.Refresh ();
  983. Assert.Equal (new (0, 0, 40, 10), top.Frame);
  984. Assert.Equal (new (0, 0, 20, 3), window.Frame);
  985. Assert.Null (Application.MouseGrabView);
  986. Application.OnMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1Pressed });
  987. Assert.Equal (window.Border, Application.MouseGrabView);
  988. Application.OnMouseEvent (
  989. new ()
  990. {
  991. Position = new (-11, -4), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  992. });
  993. Application.Refresh ();
  994. Assert.Equal (new (0, 0, 40, 10), top.Frame);
  995. Assert.Equal (new (0, 0, 20, 3), window.Frame);
  996. // Changes Top size to same size as Dialog more menu and scroll bar
  997. ((FakeDriver)Application.Driver!).SetBufferSize (20, 3);
  998. Application.OnMouseEvent (
  999. new ()
  1000. {
  1001. Position = new (-1, -1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1002. });
  1003. Application.Refresh ();
  1004. Assert.Equal (new (0, 0, 20, 3), top.Frame);
  1005. Assert.Equal (new (0, 0, 20, 3), window.Frame);
  1006. // Changes Top size smaller than Dialog size
  1007. ((FakeDriver)Application.Driver!).SetBufferSize (19, 2);
  1008. Application.OnMouseEvent (
  1009. new ()
  1010. {
  1011. Position = new (-1, -1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1012. });
  1013. Application.Refresh ();
  1014. Assert.Equal (new (0, 0, 19, 2), top.Frame);
  1015. Assert.Equal (new (-1, 0, 20, 3), window.Frame);
  1016. Application.OnMouseEvent (
  1017. new ()
  1018. {
  1019. Position = new (18, 1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1020. });
  1021. Application.Refresh ();
  1022. Assert.Equal (new (0, 0, 19, 2), top.Frame);
  1023. Assert.Equal (new (18, 1, 20, 3), window.Frame);
  1024. // On a real app we can't go beyond the SuperView bounds
  1025. Application.OnMouseEvent (
  1026. new ()
  1027. {
  1028. Position = new (19, 2), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1029. });
  1030. Application.Refresh ();
  1031. Assert.Equal (new (0, 0, 19, 2), top.Frame);
  1032. Assert.Equal (new (19, 2, 20, 3), window.Frame);
  1033. TestHelpers.AssertDriverContentsWithFrameAre (@"", output);
  1034. Application.End (rsWindow);
  1035. Application.End (rsTop);
  1036. top.Dispose ();
  1037. }
  1038. [Fact]
  1039. [AutoInitShutdown]
  1040. public void Modal_As_Top_Will_Drag_Cleanly ()
  1041. {
  1042. // Don't use Dialog as a Top, use a Window instead - dialog has complex layout behavior that is not needed here.
  1043. var window = new Window { Width = 10, Height = 3, Arrangement = ViewArrangement.Movable };
  1044. window.Add (
  1045. new Label
  1046. {
  1047. X = Pos.Center (),
  1048. Y = Pos.Center (),
  1049. Width = Dim.Fill (),
  1050. Height = Dim.Fill (),
  1051. TextAlignment = Alignment.Center,
  1052. VerticalTextAlignment = Alignment.Center,
  1053. Text = "Test"
  1054. }
  1055. );
  1056. RunState rs = Application.Begin (window);
  1057. Assert.Null (Application.MouseGrabView);
  1058. Assert.Equal (new (0, 0, 10, 3), window.Frame);
  1059. Application.OnMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1Pressed });
  1060. var firstIteration = false;
  1061. Application.RunIteration (ref rs, ref firstIteration);
  1062. Assert.Equal (window.Border, Application.MouseGrabView);
  1063. Assert.Equal (new (0, 0, 10, 3), window.Frame);
  1064. Application.OnMouseEvent (
  1065. new ()
  1066. {
  1067. Position = new (1, 1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1068. });
  1069. firstIteration = false;
  1070. Application.RunIteration (ref rs, ref firstIteration);
  1071. Assert.Equal (window.Border, Application.MouseGrabView);
  1072. Assert.Equal (new (1, 1, 10, 3), window.Frame);
  1073. Application.End (rs);
  1074. window.Dispose ();
  1075. }
  1076. [Fact]
  1077. [AutoInitShutdown]
  1078. public void Begin_With_Window_Sets_Size_Correctly ()
  1079. {
  1080. Toplevel top = new ();
  1081. RunState rsTop = Application.Begin (top);
  1082. ((FakeDriver)Application.Driver!).SetBufferSize (20, 20);
  1083. var testWindow = new Window { X = 2, Y = 1, Width = 15, Height = 10 };
  1084. Assert.Equal (new (2, 1, 15, 10), testWindow.Frame);
  1085. RunState rsTestWindow = Application.Begin (testWindow);
  1086. Assert.Equal (new (2, 1, 15, 10), testWindow.Frame);
  1087. Application.End (rsTestWindow);
  1088. Application.End (rsTop);
  1089. top.Dispose ();
  1090. }
  1091. // Don't use Dialog as a Top, use a Window instead - dialog has complex layout behavior that is not needed here.
  1092. [Fact]
  1093. [AutoInitShutdown]
  1094. public void Draw_A_Top_Subview_On_A_Window ()
  1095. {
  1096. Toplevel top = new ();
  1097. var win = new Window ();
  1098. top.Add (win);
  1099. RunState rsTop = Application.Begin (top);
  1100. ((FakeDriver)Application.Driver!).SetBufferSize (20, 20);
  1101. Assert.Equal (new (0, 0, 20, 20), win.Frame);
  1102. var btnPopup = new Button { Text = "Popup" };
  1103. var testWindow = new Window { X = 2, Y = 1, Width = 15, Height = 10 };
  1104. testWindow.Add (btnPopup);
  1105. btnPopup.Accept += (s, e) =>
  1106. {
  1107. Rectangle viewToScreen = btnPopup.ViewportToScreen (top.Frame);
  1108. var viewAddedToTop = new View
  1109. {
  1110. Text = "viewAddedToTop",
  1111. X = 1,
  1112. Y = viewToScreen.Y + 1,
  1113. Width = 18,
  1114. Height = 16,
  1115. BorderStyle = LineStyle.Single
  1116. };
  1117. Assert.Equal (testWindow, Application.Current);
  1118. Application.Current.DrawContentComplete += OnDrawContentComplete;
  1119. top.Add (viewAddedToTop);
  1120. void OnDrawContentComplete (object sender, DrawEventArgs e)
  1121. {
  1122. Assert.Equal (new (1, 3, 18, 16), viewAddedToTop.Frame);
  1123. Rectangle savedClip = Application.Driver!.Clip;
  1124. Application.Driver!.Clip = top.Frame;
  1125. viewAddedToTop.Draw ();
  1126. top.Move (2, 15);
  1127. View.Driver.AddStr ("One");
  1128. top.Move (2, 16);
  1129. View.Driver.AddStr ("Two");
  1130. top.Move (2, 17);
  1131. View.Driver.AddStr ("Three");
  1132. Application.Driver!.Clip = savedClip;
  1133. Application.Current.DrawContentComplete -= OnDrawContentComplete;
  1134. }
  1135. };
  1136. RunState rsTestWindow = Application.Begin (testWindow);
  1137. Assert.Equal (new (2, 1, 15, 10), testWindow.Frame);
  1138. Application.OnMouseEvent (new () { Position = new (5, 2), Flags = MouseFlags.Button1Clicked });
  1139. Application.Top.Draw ();
  1140. var firstIteration = false;
  1141. Application.RunIteration (ref rsTestWindow, ref firstIteration);
  1142. TestHelpers.AssertDriverContentsWithFrameAre (
  1143. @$"
  1144. ┌──────────────────┐
  1145. │ ┌─────────────┐ │
  1146. │ │{CM.Glyphs.LeftBracket} Popup {CM.Glyphs.RightBracket} │ │
  1147. │┌────────────────┐│
  1148. ││viewAddedToTop ││
  1149. ││ ││
  1150. ││ ││
  1151. ││ ││
  1152. ││ ││
  1153. ││ ││
  1154. ││ ││
  1155. ││ ││
  1156. ││ ││
  1157. ││ ││
  1158. ││ ││
  1159. ││One ││
  1160. ││Two ││
  1161. ││Three ││
  1162. │└────────────────┘│
  1163. └──────────────────┘",
  1164. output
  1165. );
  1166. Application.End (rsTestWindow);
  1167. Application.End (rsTop);
  1168. top.Dispose ();
  1169. }
  1170. private void OnDrawContentComplete (object sender, DrawEventArgs e) { throw new NotImplementedException (); }
  1171. [Fact]
  1172. [AutoInitShutdown]
  1173. public void Activating_MenuBar_By_Alt_Key_Does_Not_Throw ()
  1174. {
  1175. var menu = new MenuBar
  1176. {
  1177. Menus =
  1178. [
  1179. new ("Child", new MenuItem [] { new ("_Create Child", "", null) })
  1180. ]
  1181. };
  1182. var topChild = new Toplevel ();
  1183. topChild.Add (menu);
  1184. var top = new Toplevel ();
  1185. top.Add (topChild);
  1186. Application.Begin (top);
  1187. Exception exception = Record.Exception (() => topChild.NewKeyDownEvent (KeyCode.AltMask));
  1188. Assert.Null (exception);
  1189. top.Dispose ();
  1190. }
  1191. [Fact]
  1192. [TestRespondersDisposed]
  1193. public void Multi_Thread_Toplevels ()
  1194. {
  1195. Application.Init (new FakeDriver ());
  1196. Toplevel t = new ();
  1197. var w = new Window ();
  1198. t.Add (w);
  1199. int count = 0, count1 = 0, count2 = 0;
  1200. bool log = false, log1 = false, log2 = false;
  1201. var fromTopStillKnowFirstIsRunning = false;
  1202. var fromTopStillKnowSecondIsRunning = false;
  1203. var fromFirstStillKnowSecondIsRunning = false;
  1204. Application.AddTimeout (
  1205. TimeSpan.FromMilliseconds (100),
  1206. () =>
  1207. {
  1208. count++;
  1209. if (count1 == 5)
  1210. {
  1211. log1 = true;
  1212. }
  1213. if (count1 == 14 && count2 == 10 && count == 15)
  1214. {
  1215. // count2 is already stopped
  1216. fromTopStillKnowFirstIsRunning = true;
  1217. }
  1218. if (count1 == 7 && count2 == 7 && count == 8)
  1219. {
  1220. fromTopStillKnowSecondIsRunning = true;
  1221. }
  1222. if (count == 30)
  1223. {
  1224. Assert.Equal (30, count);
  1225. Assert.Equal (20, count1);
  1226. Assert.Equal (10, count2);
  1227. Assert.True (log);
  1228. Assert.True (log1);
  1229. Assert.True (log2);
  1230. Assert.True (fromTopStillKnowFirstIsRunning);
  1231. Assert.True (fromTopStillKnowSecondIsRunning);
  1232. Assert.True (fromFirstStillKnowSecondIsRunning);
  1233. Application.RequestStop ();
  1234. return false;
  1235. }
  1236. return true;
  1237. }
  1238. );
  1239. t.Ready += FirstWindow;
  1240. void FirstWindow (object sender, EventArgs args)
  1241. {
  1242. var firstWindow = new Window ();
  1243. firstWindow.Ready += SecondWindow;
  1244. Application.AddTimeout (
  1245. TimeSpan.FromMilliseconds (100),
  1246. () =>
  1247. {
  1248. count1++;
  1249. if (count2 == 5)
  1250. {
  1251. log2 = true;
  1252. }
  1253. if (count2 == 4 && count1 == 5 && count == 5)
  1254. {
  1255. fromFirstStillKnowSecondIsRunning = true;
  1256. }
  1257. if (count1 == 20)
  1258. {
  1259. Assert.Equal (20, count1);
  1260. Application.RequestStop ();
  1261. return false;
  1262. }
  1263. return true;
  1264. }
  1265. );
  1266. Application.Run (firstWindow);
  1267. firstWindow.Dispose ();
  1268. }
  1269. void SecondWindow (object sender, EventArgs args)
  1270. {
  1271. var testWindow = new Window ();
  1272. Application.AddTimeout (
  1273. TimeSpan.FromMilliseconds (100),
  1274. () =>
  1275. {
  1276. count2++;
  1277. if (count < 30)
  1278. {
  1279. log = true;
  1280. }
  1281. if (count2 == 10)
  1282. {
  1283. Assert.Equal (10, count2);
  1284. Application.RequestStop ();
  1285. return false;
  1286. }
  1287. return true;
  1288. }
  1289. );
  1290. Application.Run (testWindow);
  1291. testWindow.Dispose ();
  1292. }
  1293. Application.Run (t);
  1294. t.Dispose ();
  1295. Application.Shutdown ();
  1296. }
  1297. }