ToplevelTests.cs 66 KB

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