ToplevelTests.cs 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  1. using System;
  2. using Terminal.Gui;
  3. using Xunit;
  4. using Xunit.Abstractions;
  5. namespace Terminal.Gui.TopLevelTests {
  6. public class ToplevelTests {
  7. readonly ITestOutputHelper output;
  8. public ToplevelTests (ITestOutputHelper output)
  9. {
  10. this.output = output;
  11. }
  12. [Fact]
  13. [AutoInitShutdown]
  14. public void Constructor_Default ()
  15. {
  16. var top = new Toplevel ();
  17. Assert.Equal (Colors.TopLevel, top.ColorScheme);
  18. Assert.Equal ("Fill(0)", top.Width.ToString ());
  19. Assert.Equal ("Fill(0)", top.Height.ToString ());
  20. Assert.False (top.Running);
  21. Assert.False (top.Modal);
  22. Assert.Null (top.MenuBar);
  23. Assert.Null (top.StatusBar);
  24. Assert.False (top.IsMdiContainer);
  25. Assert.False (top.IsMdiChild);
  26. }
  27. [Fact]
  28. [AutoInitShutdown]
  29. public void Create_Toplevel ()
  30. {
  31. var top = Toplevel.Create ();
  32. Assert.Equal (new Rect (0, 0, Application.Driver.Cols, Application.Driver.Rows), top.Bounds);
  33. }
  34. [Fact]
  35. [AutoInitShutdown]
  36. public void Application_Top_EnsureVisibleBounds_To_Driver_Rows_And_Cols ()
  37. {
  38. var iterations = 0;
  39. Application.Iteration += () => {
  40. if (iterations == 0) {
  41. Assert.False (Application.Top.AutoSize);
  42. Assert.Equal ("Top1", Application.Top.Text);
  43. Assert.Equal (0, Application.Top.Frame.X);
  44. Assert.Equal (0, Application.Top.Frame.Y);
  45. Assert.Equal (Application.Driver.Cols, Application.Top.Frame.Width);
  46. Assert.Equal (Application.Driver.Rows, Application.Top.Frame.Height);
  47. Application.Top.ProcessHotKey (new KeyEvent (Key.CtrlMask | Key.R, new KeyModifiers ()));
  48. } else if (iterations == 1) {
  49. Assert.Equal ("Top2", Application.Top.Text);
  50. Assert.Equal (0, Application.Top.Frame.X);
  51. Assert.Equal (0, Application.Top.Frame.Y);
  52. Assert.Equal (Application.Driver.Cols, Application.Top.Frame.Width);
  53. Assert.Equal (Application.Driver.Rows, Application.Top.Frame.Height);
  54. Application.Top.ProcessHotKey (new KeyEvent (Key.CtrlMask | Key.C, new KeyModifiers ()));
  55. } else if (iterations == 3) {
  56. Assert.Equal ("Top1", Application.Top.Text);
  57. Assert.Equal (0, Application.Top.Frame.X);
  58. Assert.Equal (0, Application.Top.Frame.Y);
  59. Assert.Equal (Application.Driver.Cols, Application.Top.Frame.Width);
  60. Assert.Equal (Application.Driver.Rows, Application.Top.Frame.Height);
  61. Application.Top.ProcessHotKey (new KeyEvent (Key.CtrlMask | Key.R, new KeyModifiers ()));
  62. } else if (iterations == 4) {
  63. Assert.Equal ("Top2", Application.Top.Text);
  64. Assert.Equal (0, Application.Top.Frame.X);
  65. Assert.Equal (0, Application.Top.Frame.Y);
  66. Assert.Equal (Application.Driver.Cols, Application.Top.Frame.Width);
  67. Assert.Equal (Application.Driver.Rows, Application.Top.Frame.Height);
  68. Application.Top.ProcessHotKey (new KeyEvent (Key.CtrlMask | Key.C, new KeyModifiers ()));
  69. } else if (iterations == 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.Top.ProcessHotKey (new KeyEvent (Key.CtrlMask | Key.Q, new KeyModifiers ()));
  76. }
  77. iterations++;
  78. };
  79. Application.Run (Top1 ());
  80. Toplevel Top1 ()
  81. {
  82. var top = Application.Top;
  83. top.Text = "Top1";
  84. var menu = new MenuBar (new MenuBarItem [] {
  85. new MenuBarItem ("_Options", new MenuItem [] {
  86. new MenuItem ("_Run Top2", "", () => Application.Run (Top2 ()), null, null, Key.CtrlMask | Key.R),
  87. new MenuItem ("_Quit", "", () => Application.RequestStop(), null, null, Key.CtrlMask | Key.Q)
  88. })
  89. });
  90. top.Add (menu);
  91. var statusBar = new StatusBar (new [] {
  92. new StatusItem(Key.CtrlMask | Key.R, "~^R~ Run Top2", () => Application.Run (Top2 ())),
  93. new StatusItem(Application.QuitKey, $"{Application.QuitKey} to Quit", () => Application.RequestStop())
  94. });
  95. top.Add (statusBar);
  96. var t1 = new Toplevel ();
  97. top.Add (t1);
  98. return top;
  99. }
  100. Toplevel Top2 ()
  101. {
  102. var top = new Toplevel (Application.Top.Frame);
  103. top.Text = "Top2";
  104. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  105. var menu = new MenuBar (new MenuBarItem [] {
  106. new MenuBarItem ("_Stage", new MenuItem [] {
  107. new MenuItem ("_Close", "", () => Application.RequestStop(), null, null, Key.CtrlMask | Key.C)
  108. })
  109. });
  110. top.Add (menu);
  111. var statusBar = new StatusBar (new [] {
  112. new StatusItem(Key.CtrlMask | Key.C, "~^C~ Close", () => Application.RequestStop()),
  113. });
  114. top.Add (statusBar);
  115. win.Add (new ListView () {
  116. X = 0,
  117. Y = 0,
  118. Width = Dim.Fill (),
  119. Height = Dim.Fill ()
  120. });
  121. top.Add (win);
  122. return top;
  123. }
  124. }
  125. [Fact]
  126. [AutoInitShutdown]
  127. public void Internal_Tests ()
  128. {
  129. var top = new Toplevel ();
  130. var eventInvoked = "";
  131. top.ChildUnloaded += (s, e) => eventInvoked = "ChildUnloaded";
  132. top.OnChildUnloaded (top);
  133. Assert.Equal ("ChildUnloaded", eventInvoked);
  134. top.ChildLoaded += (s, e) => eventInvoked = "ChildLoaded";
  135. top.OnChildLoaded (top);
  136. Assert.Equal ("ChildLoaded", eventInvoked);
  137. top.Closed += (s, e) => eventInvoked = "Closed";
  138. top.OnClosed (top);
  139. Assert.Equal ("Closed", eventInvoked);
  140. top.Closing += (s, e) => eventInvoked = "Closing";
  141. top.OnClosing (new ToplevelClosingEventArgs (top));
  142. Assert.Equal ("Closing", eventInvoked);
  143. top.AllChildClosed += (s, e) => eventInvoked = "AllChildClosed";
  144. top.OnAllChildClosed ();
  145. Assert.Equal ("AllChildClosed", eventInvoked);
  146. top.ChildClosed += (s, e) => eventInvoked = "ChildClosed";
  147. top.OnChildClosed (top);
  148. Assert.Equal ("ChildClosed", eventInvoked);
  149. top.Deactivate += (s, e) => eventInvoked = "Deactivate";
  150. top.OnDeactivate (top);
  151. Assert.Equal ("Deactivate", eventInvoked);
  152. top.Activate += (s, e) => eventInvoked = "Activate";
  153. top.OnActivate (top);
  154. Assert.Equal ("Activate", eventInvoked);
  155. top.Loaded += (s, e) => eventInvoked = "Loaded";
  156. top.OnLoaded ();
  157. Assert.Equal ("Loaded", eventInvoked);
  158. top.Ready += (s, e) => eventInvoked = "Ready";
  159. top.OnReady ();
  160. Assert.Equal ("Ready", eventInvoked);
  161. top.Unloaded += (s, e) => eventInvoked = "Unloaded";
  162. top.OnUnloaded ();
  163. Assert.Equal ("Unloaded", eventInvoked);
  164. top.AddMenuStatusBar (new MenuBar ());
  165. Assert.NotNull (top.MenuBar);
  166. top.AddMenuStatusBar (new StatusBar ());
  167. Assert.NotNull (top.StatusBar);
  168. top.RemoveMenuStatusBar (top.MenuBar);
  169. Assert.Null (top.MenuBar);
  170. top.RemoveMenuStatusBar (top.StatusBar);
  171. Assert.Null (top.StatusBar);
  172. Application.Begin (top);
  173. Assert.Equal (top, Application.Top);
  174. // Application.Top without menu and status bar.
  175. var supView = top.EnsureVisibleBounds (top, 2, 2, out int nx, out int ny, out MenuBar mb, out StatusBar sb);
  176. Assert.Equal (Application.Top, supView);
  177. Assert.Equal (0, nx);
  178. Assert.Equal (0, ny);
  179. Assert.Null (mb);
  180. Assert.Null (sb);
  181. top.AddMenuStatusBar (new MenuBar ());
  182. Assert.NotNull (top.MenuBar);
  183. // Application.Top with a menu and without status bar.
  184. top.EnsureVisibleBounds (top, 2, 2, out nx, out ny, out mb, out sb);
  185. Assert.Equal (0, nx);
  186. Assert.Equal (1, ny);
  187. Assert.NotNull (mb);
  188. Assert.Null (sb);
  189. top.AddMenuStatusBar (new StatusBar ());
  190. Assert.NotNull (top.StatusBar);
  191. // Application.Top with a menu and status bar.
  192. top.EnsureVisibleBounds (top, 2, 2, out nx, out ny, out mb, out sb);
  193. Assert.Equal (0, nx);
  194. // The available height is lower than the Application.Top height minus
  195. // the menu bar and status bar, then the top can go beyond the bottom
  196. Assert.Equal (2, ny);
  197. Assert.NotNull (mb);
  198. Assert.NotNull (sb);
  199. top.RemoveMenuStatusBar (top.MenuBar);
  200. Assert.Null (top.MenuBar);
  201. // Application.Top without a menu and with a status bar.
  202. top.EnsureVisibleBounds (top, 2, 2, out nx, out ny, out mb, out sb);
  203. Assert.Equal (0, nx);
  204. // The available height is lower than the Application.Top height minus
  205. // the status bar, then the top can go beyond the bottom
  206. Assert.Equal (2, ny);
  207. Assert.Null (mb);
  208. Assert.NotNull (sb);
  209. top.RemoveMenuStatusBar (top.StatusBar);
  210. Assert.Null (top.StatusBar);
  211. Assert.Null (top.MenuBar);
  212. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  213. top.Add (win);
  214. top.LayoutSubviews ();
  215. // The SuperView is always the same regardless of the caller.
  216. supView = top.EnsureVisibleBounds (win, 0, 0, out nx, out ny, out mb, out sb);
  217. Assert.Equal (Application.Top, supView);
  218. supView = win.EnsureVisibleBounds (win, 0, 0, out nx, out ny, out mb, out sb);
  219. Assert.Equal (Application.Top, supView);
  220. // Application.Top without menu and status bar.
  221. top.EnsureVisibleBounds (win, 0, 0, out nx, out ny, out mb, out sb);
  222. Assert.Equal (0, nx);
  223. Assert.Equal (0, ny);
  224. Assert.Null (mb);
  225. Assert.Null (sb);
  226. top.AddMenuStatusBar (new MenuBar ());
  227. Assert.NotNull (top.MenuBar);
  228. // Application.Top with a menu and without status bar.
  229. top.EnsureVisibleBounds (win, 2, 2, out nx, out ny, out mb, out sb);
  230. Assert.Equal (0, nx);
  231. Assert.Equal (1, ny);
  232. Assert.NotNull (mb);
  233. Assert.Null (sb);
  234. top.AddMenuStatusBar (new StatusBar ());
  235. Assert.NotNull (top.StatusBar);
  236. // Application.Top with a menu and status bar.
  237. top.EnsureVisibleBounds (win, 30, 20, out nx, out ny, out mb, out sb);
  238. Assert.Equal (0, nx);
  239. // The available height is lower than the Application.Top height minus
  240. // the menu bar and status bar, then the top can go beyond the bottom
  241. Assert.Equal (20, ny);
  242. Assert.NotNull (mb);
  243. Assert.NotNull (sb);
  244. top.RemoveMenuStatusBar (top.MenuBar);
  245. top.RemoveMenuStatusBar (top.StatusBar);
  246. Assert.Null (top.StatusBar);
  247. Assert.Null (top.MenuBar);
  248. top.Remove (win);
  249. win = new Window () { Width = 60, Height = 15 };
  250. top.Add (win);
  251. // Application.Top without menu and status bar.
  252. top.EnsureVisibleBounds (win, 0, 0, out nx, out ny, out mb, out sb);
  253. Assert.Equal (0, nx);
  254. Assert.Equal (0, ny);
  255. Assert.Null (mb);
  256. Assert.Null (sb);
  257. top.AddMenuStatusBar (new MenuBar ());
  258. Assert.NotNull (top.MenuBar);
  259. // Application.Top with a menu and without status bar.
  260. top.EnsureVisibleBounds (win, 2, 2, out nx, out ny, out mb, out sb);
  261. Assert.Equal (2, nx);
  262. Assert.Equal (2, ny);
  263. Assert.NotNull (mb);
  264. Assert.Null (sb);
  265. top.AddMenuStatusBar (new StatusBar ());
  266. Assert.NotNull (top.StatusBar);
  267. // Application.Top with a menu and status bar.
  268. top.EnsureVisibleBounds (win, 30, 20, out nx, out ny, out mb, out sb);
  269. Assert.Equal (20, nx); // 20+60=80
  270. Assert.Equal (9, ny); // 9+15+1(mb)=25
  271. Assert.NotNull (mb);
  272. Assert.NotNull (sb);
  273. top.PositionToplevels ();
  274. Assert.Equal (new Rect (0, 1, 60, 15), win.Frame);
  275. Assert.Null (Toplevel.dragPosition);
  276. win.MouseEvent (new MouseEvent () { X = 6, Y = 0, Flags = MouseFlags.Button1Pressed });
  277. Assert.Equal (new Point (6, 0), Toplevel.dragPosition);
  278. win.MouseEvent (new MouseEvent () { X = 6, Y = 0, Flags = MouseFlags.Button1Released });
  279. Assert.Null (Toplevel.dragPosition);
  280. win.CanFocus = false;
  281. win.MouseEvent (new MouseEvent () { X = 6, Y = 0, Flags = MouseFlags.Button1Pressed });
  282. Assert.Null (Toplevel.dragPosition);
  283. }
  284. [Fact]
  285. [AutoInitShutdown]
  286. public void KeyBindings_Command ()
  287. {
  288. var isRunning = false;
  289. var win1 = new Window ("Win1") { Width = Dim.Percent (50f), Height = Dim.Fill () };
  290. var lblTf1W1 = new Label ("Enter text in TextField on Win1:");
  291. var tf1W1 = new TextField ("Text1 on Win1") { X = Pos.Right (lblTf1W1) + 1, Width = Dim.Fill () };
  292. var lblTvW1 = new Label ("Enter text in TextView on Win1:") { Y = Pos.Bottom (lblTf1W1) + 1 };
  293. var tvW1 = new TextView () { X = Pos.Left (tf1W1), Width = Dim.Fill (), Height = 2, Text = "First line Win1\nSecond line Win1" };
  294. var lblTf2W1 = new Label ("Enter text in TextField on Win1:") { Y = Pos.Bottom (lblTvW1) + 1 };
  295. var tf2W1 = new TextField ("Text2 on Win1") { X = Pos.Left (tf1W1), Width = Dim.Fill () };
  296. win1.Add (lblTf1W1, tf1W1, lblTvW1, tvW1, lblTf2W1, tf2W1);
  297. var win2 = new Window ("Win2") { X = Pos.Right (win1) + 1, Width = Dim.Percent (50f), Height = Dim.Fill () };
  298. var lblTf1W2 = new Label ("Enter text in TextField on Win2:");
  299. var tf1W2 = new TextField ("Text1 on Win2") { X = Pos.Right (lblTf1W2) + 1, Width = Dim.Fill () };
  300. var lblTvW2 = new Label ("Enter text in TextView on Win2:") { Y = Pos.Bottom (lblTf1W2) + 1 };
  301. var tvW2 = new TextView () { X = Pos.Left (tf1W2), Width = Dim.Fill (), Height = 2, Text = "First line Win1\nSecond line Win2" };
  302. var lblTf2W2 = new Label ("Enter text in TextField on Win2:") { Y = Pos.Bottom (lblTvW2) + 1 };
  303. var tf2W2 = new TextField ("Text2 on Win2") { X = Pos.Left (tf1W2), Width = Dim.Fill () };
  304. win2.Add (lblTf1W2, tf1W2, lblTvW2, tvW2, lblTf2W2, tf2W2);
  305. var top = Application.Top;
  306. top.Add (win1, win2);
  307. top.Loaded += (s, e) => isRunning = true;
  308. top.Closing += (s, e) => isRunning = false;
  309. Application.Begin (top);
  310. top.Running = true;
  311. Assert.Equal (new Rect (0, 0, 40, 25), win1.Frame);
  312. Assert.Equal (new Rect (41, 0, 40, 25), win2.Frame);
  313. Assert.Equal (win1, top.Focused);
  314. Assert.Equal (tf1W1, top.MostFocused);
  315. Assert.True (isRunning);
  316. Assert.True (top.Focused.ProcessKey (new KeyEvent (Application.QuitKey, new KeyModifiers ())));
  317. Assert.False (isRunning);
  318. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Z | Key.CtrlMask, new KeyModifiers ())));
  319. Assert.False (top.Focused.ProcessKey (new KeyEvent (Key.F5, new KeyModifiers ())));
  320. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  321. Assert.Equal (win1, top.Focused);
  322. Assert.Equal (tvW1, top.MostFocused);
  323. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  324. Assert.Equal ($"\tFirst line Win1{Environment.NewLine}Second line Win1", tvW1.Text);
  325. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ())));
  326. Assert.Equal ($"First line Win1{Environment.NewLine}Second line Win1", tvW1.Text);
  327. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers ())));
  328. Assert.Equal (win1, top.Focused);
  329. Assert.Equal (tf2W1, top.MostFocused);
  330. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  331. Assert.Equal (win1, top.Focused);
  332. Assert.Equal (tf1W1, top.MostFocused);
  333. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  334. Assert.Equal (win1, top.Focused);
  335. Assert.Equal (tf1W1, top.MostFocused);
  336. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  337. Assert.Equal (win1, top.Focused);
  338. Assert.Equal (tvW1, top.MostFocused);
  339. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.I | Key.CtrlMask, new KeyModifiers ())));
  340. Assert.Equal (win1, top.Focused);
  341. Assert.Equal (tf2W1, top.MostFocused);
  342. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ())));
  343. Assert.Equal (win1, top.Focused);
  344. Assert.Equal (tvW1, top.MostFocused);
  345. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ())));
  346. Assert.Equal (win1, top.Focused);
  347. Assert.Equal (tf1W1, top.MostFocused);
  348. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ())));
  349. Assert.Equal (win1, top.Focused);
  350. Assert.Equal (tf2W1, top.MostFocused);
  351. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers ())));
  352. Assert.Equal (win2, top.Focused);
  353. Assert.Equal (tf1W2, top.MostFocused);
  354. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask | Key.ShiftMask, new KeyModifiers ())));
  355. Assert.Equal (win1, top.Focused);
  356. Assert.Equal (tf2W1, top.MostFocused);
  357. Assert.True (top.Focused.ProcessKey (new KeyEvent (Application.AlternateForwardKey, new KeyModifiers ())));
  358. Assert.Equal (win2, top.Focused);
  359. Assert.Equal (tf1W2, top.MostFocused);
  360. Assert.True (top.Focused.ProcessKey (new KeyEvent (Application.AlternateBackwardKey, new KeyModifiers ())));
  361. Assert.Equal (win1, top.Focused);
  362. Assert.Equal (tf2W1, top.MostFocused);
  363. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ())));
  364. Assert.Equal (win1, top.Focused);
  365. Assert.Equal (tvW1, top.MostFocused);
  366. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.B | Key.CtrlMask, new KeyModifiers ())));
  367. Assert.Equal (win1, top.Focused);
  368. Assert.Equal (tf1W1, top.MostFocused);
  369. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  370. Assert.Equal (win1, top.Focused);
  371. Assert.Equal (tvW1, top.MostFocused);
  372. Assert.Equal (new Point (0, 0), tvW1.CursorPosition);
  373. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.End | Key.CtrlMask, new KeyModifiers ())));
  374. Assert.Equal (win1, top.Focused);
  375. Assert.Equal (tvW1, top.MostFocused);
  376. Assert.Equal (new Point (16, 1), tvW1.CursorPosition);
  377. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.F | Key.CtrlMask, new KeyModifiers ())));
  378. Assert.Equal (win1, top.Focused);
  379. Assert.Equal (tf2W1, top.MostFocused);
  380. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.L | Key.CtrlMask, new KeyModifiers ())));
  381. }
  382. [Fact]
  383. [AutoInitShutdown]
  384. public void KeyBindings_Command_With_MdiTop ()
  385. {
  386. var top = Application.Top;
  387. Assert.Null (Application.MdiTop);
  388. top.IsMdiContainer = true;
  389. Application.Begin (top);
  390. Assert.Equal (Application.Top, Application.MdiTop);
  391. var isRunning = true;
  392. var win1 = new Window ("Win1") { Width = Dim.Percent (50f), Height = Dim.Fill () };
  393. var lblTf1W1 = new Label ("Enter text in TextField on Win1:");
  394. var tf1W1 = new TextField ("Text1 on Win1") { X = Pos.Right (lblTf1W1) + 1, Width = Dim.Fill () };
  395. var lblTvW1 = new Label ("Enter text in TextView on Win1:") { Y = Pos.Bottom (lblTf1W1) + 1 };
  396. var tvW1 = new TextView () { X = Pos.Left (tf1W1), Width = Dim.Fill (), Height = 2, Text = "First line Win1\nSecond line Win1" };
  397. var lblTf2W1 = new Label ("Enter text in TextField on Win1:") { Y = Pos.Bottom (lblTvW1) + 1 };
  398. var tf2W1 = new TextField ("Text2 on Win1") { X = Pos.Left (tf1W1), Width = Dim.Fill () };
  399. win1.Add (lblTf1W1, tf1W1, lblTvW1, tvW1, lblTf2W1, tf2W1);
  400. var win2 = new Window ("Win2") { Width = Dim.Percent (50f), Height = Dim.Fill () };
  401. var lblTf1W2 = new Label ("Enter text in TextField on Win2:");
  402. var tf1W2 = new TextField ("Text1 on Win2") { X = Pos.Right (lblTf1W2) + 1, Width = Dim.Fill () };
  403. var lblTvW2 = new Label ("Enter text in TextView on Win2:") { Y = Pos.Bottom (lblTf1W2) + 1 };
  404. var tvW2 = new TextView () { X = Pos.Left (tf1W2), Width = Dim.Fill (), Height = 2, Text = "First line Win1\nSecond line Win2" };
  405. var lblTf2W2 = new Label ("Enter text in TextField on Win2:") { Y = Pos.Bottom (lblTvW2) + 1 };
  406. var tf2W2 = new TextField ("Text2 on Win2") { X = Pos.Left (tf1W2), Width = Dim.Fill () };
  407. win2.Add (lblTf1W2, tf1W2, lblTvW2, tvW2, lblTf2W2, tf2W2);
  408. win1.Closing += (s, e) => isRunning = false;
  409. Assert.Null (top.Focused);
  410. Assert.Equal (top, Application.Current);
  411. Assert.True (top.IsCurrentTop);
  412. Assert.Equal (top, Application.MdiTop);
  413. Application.Begin (win1);
  414. Assert.Equal (new Rect (0, 0, 40, 25), win1.Frame);
  415. Assert.NotEqual (top, Application.Current);
  416. Assert.False (top.IsCurrentTop);
  417. Assert.Equal (win1, Application.Current);
  418. Assert.True (win1.IsCurrentTop);
  419. Assert.True (win1.IsMdiChild);
  420. Assert.Null (top.Focused);
  421. Assert.Null (top.MostFocused);
  422. Assert.Equal (win1.Subviews [0], win1.Focused);
  423. Assert.Equal (tf1W1, win1.MostFocused);
  424. Assert.True (win1.IsMdiChild);
  425. Assert.Single (Application.MdiChildes);
  426. Application.Begin (win2);
  427. Assert.Equal (new Rect (0, 0, 40, 25), win2.Frame);
  428. Assert.NotEqual (top, Application.Current);
  429. Assert.False (top.IsCurrentTop);
  430. Assert.Equal (win2, Application.Current);
  431. Assert.True (win2.IsCurrentTop);
  432. Assert.True (win2.IsMdiChild);
  433. Assert.Null (top.Focused);
  434. Assert.Null (top.MostFocused);
  435. Assert.Equal (win2.Subviews [0], win2.Focused);
  436. Assert.Equal (tf1W2, win2.MostFocused);
  437. Assert.Equal (2, Application.MdiChildes.Count);
  438. Application.ShowChild (win1);
  439. Assert.Equal (win1, Application.Current);
  440. Assert.Equal (win1, Application.MdiChildes [0]);
  441. win1.Running = true;
  442. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Application.QuitKey, new KeyModifiers ())));
  443. Assert.False (isRunning);
  444. Assert.False (win1.Running);
  445. Assert.Equal (win1, Application.MdiChildes [0]);
  446. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Z | Key.CtrlMask, new KeyModifiers ())));
  447. Assert.False (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.F5, new KeyModifiers ())));
  448. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  449. Assert.True (win1.IsCurrentTop);
  450. Assert.Equal (tvW1, win1.MostFocused);
  451. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  452. Assert.Equal ($"\tFirst line Win1{Environment.NewLine}Second line Win1", tvW1.Text);
  453. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ())));
  454. Assert.Equal ($"First line Win1{Environment.NewLine}Second line Win1", tvW1.Text);
  455. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers ())));
  456. Assert.Equal (win1, Application.MdiChildes [0]);
  457. Assert.Equal (tf2W1, win1.MostFocused);
  458. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  459. Assert.Equal (win1, Application.MdiChildes [0]);
  460. Assert.Equal (tf1W1, win1.MostFocused);
  461. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  462. Assert.Equal (win1, Application.MdiChildes [0]);
  463. Assert.Equal (tf1W1, win1.MostFocused);
  464. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  465. Assert.Equal (win1, Application.MdiChildes [0]);
  466. Assert.Equal (tvW1, win1.MostFocused);
  467. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.I | Key.CtrlMask, new KeyModifiers ())));
  468. Assert.Equal (win1, Application.MdiChildes [0]);
  469. Assert.Equal (tf2W1, win1.MostFocused);
  470. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ())));
  471. Assert.Equal (win1, Application.MdiChildes [0]);
  472. Assert.Equal (tvW1, win1.MostFocused);
  473. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ())));
  474. Assert.Equal (win1, Application.MdiChildes [0]);
  475. Assert.Equal (tf1W1, win1.MostFocused);
  476. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ())));
  477. Assert.Equal (win1, Application.MdiChildes [0]);
  478. Assert.Equal (tf2W1, win1.MostFocused);
  479. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  480. Assert.Equal (win1, Application.MdiChildes [0]);
  481. Assert.Equal (tf1W1, win1.MostFocused);
  482. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers ())));
  483. Assert.Equal (win2, Application.MdiChildes [0]);
  484. Assert.Equal (tf1W2, win2.MostFocused);
  485. tf2W2.SetFocus ();
  486. Assert.True (tf2W2.HasFocus);
  487. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask | Key.ShiftMask, new KeyModifiers ())));
  488. Assert.Equal (win1, Application.MdiChildes [0]);
  489. Assert.Equal (tf1W1, win1.MostFocused);
  490. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Application.AlternateForwardKey, new KeyModifiers ())));
  491. Assert.Equal (win2, Application.MdiChildes [0]);
  492. Assert.Equal (tf2W2, win2.MostFocused);
  493. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Application.AlternateBackwardKey, new KeyModifiers ())));
  494. Assert.Equal (win1, Application.MdiChildes [0]);
  495. Assert.Equal (tf1W1, win1.MostFocused);
  496. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  497. Assert.Equal (win1, Application.MdiChildes [0]);
  498. Assert.Equal (tvW1, win1.MostFocused);
  499. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.B | Key.CtrlMask, new KeyModifiers ())));
  500. Assert.Equal (win1, Application.MdiChildes [0]);
  501. Assert.Equal (tf1W1, win1.MostFocused);
  502. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  503. Assert.Equal (win1, Application.MdiChildes [0]);
  504. Assert.Equal (tvW1, win1.MostFocused);
  505. Assert.Equal (new Point (0, 0), tvW1.CursorPosition);
  506. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.End | Key.CtrlMask, new KeyModifiers ())));
  507. Assert.Equal (win1, Application.MdiChildes [0]);
  508. Assert.Equal (tvW1, win1.MostFocused);
  509. Assert.Equal (new Point (16, 1), tvW1.CursorPosition);
  510. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.F | Key.CtrlMask, new KeyModifiers ())));
  511. Assert.Equal (win1, Application.MdiChildes [0]);
  512. Assert.Equal (tf2W1, win1.MostFocused);
  513. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.L | Key.CtrlMask, new KeyModifiers ())));
  514. }
  515. [Fact]
  516. public void Added_Event_Should_Not_Be_Used_To_Initialize_Toplevel_Events ()
  517. {
  518. Key alternateForwardKey = default;
  519. Key alternateBackwardKey = default;
  520. Key quitKey = default;
  521. var wasAdded = false;
  522. var view = new View ();
  523. view.Added += View_Added;
  524. void View_Added (object sender, SuperViewChangedEventArgs e)
  525. {
  526. Assert.Throws<NullReferenceException> (() => Application.Top.AlternateForwardKeyChanged += (s, e) => alternateForwardKey = e.OldKey);
  527. Assert.Throws<NullReferenceException> (() => Application.Top.AlternateBackwardKeyChanged += (s, e) => alternateBackwardKey = e.OldKey);
  528. Assert.Throws<NullReferenceException> (() => Application.Top.QuitKeyChanged += (s, e) => quitKey = e.OldKey);
  529. Assert.False (wasAdded);
  530. wasAdded = true;
  531. view.Added -= View_Added;
  532. }
  533. var win = new Window ();
  534. win.Add (view);
  535. Application.Init (new FakeDriver ());
  536. var top = Application.Top;
  537. top.Add (win);
  538. Assert.True (wasAdded);
  539. Application.Shutdown ();
  540. }
  541. [Fact]
  542. [AutoInitShutdown]
  543. public void AlternateForwardKeyChanged_AlternateBackwardKeyChanged_QuitKeyChanged_Events ()
  544. {
  545. Key alternateForwardKey = default;
  546. Key alternateBackwardKey = default;
  547. Key quitKey = default;
  548. var view = new View ();
  549. view.Initialized += View_Initialized;
  550. void View_Initialized (object sender, EventArgs e)
  551. {
  552. Application.Top.AlternateForwardKeyChanged += (s, e) => alternateForwardKey = e.OldKey;
  553. Application.Top.AlternateBackwardKeyChanged += (s, e) => alternateBackwardKey = e.OldKey;
  554. Application.Top.QuitKeyChanged += (s, e) => quitKey = e.OldKey;
  555. }
  556. var win = new Window ();
  557. win.Add (view);
  558. var top = Application.Top;
  559. top.Add (win);
  560. Application.Begin (top);
  561. Assert.Equal (Key.Null, alternateForwardKey);
  562. Assert.Equal (Key.Null, alternateBackwardKey);
  563. Assert.Equal (Key.Null, quitKey);
  564. Assert.Equal (Key.PageDown | Key.CtrlMask, Application.AlternateForwardKey);
  565. Assert.Equal (Key.PageUp | Key.CtrlMask, Application.AlternateBackwardKey);
  566. Assert.Equal (Key.Q | Key.CtrlMask, Application.QuitKey);
  567. Application.AlternateForwardKey = Key.A;
  568. Application.AlternateBackwardKey = Key.B;
  569. Application.QuitKey = Key.C;
  570. Assert.Equal (Key.PageDown | Key.CtrlMask, alternateForwardKey);
  571. Assert.Equal (Key.PageUp | Key.CtrlMask, alternateBackwardKey);
  572. Assert.Equal (Key.Q | Key.CtrlMask, quitKey);
  573. Assert.Equal (Key.A, Application.AlternateForwardKey);
  574. Assert.Equal (Key.B, Application.AlternateBackwardKey);
  575. Assert.Equal (Key.C, Application.QuitKey);
  576. // Replacing the defaults keys to avoid errors on others unit tests that are using it.
  577. Application.AlternateForwardKey = Key.PageDown | Key.CtrlMask;
  578. Application.AlternateBackwardKey = Key.PageUp | Key.CtrlMask;
  579. Application.QuitKey = Key.Q | Key.CtrlMask;
  580. Assert.Equal (Key.PageDown | Key.CtrlMask, Application.AlternateForwardKey);
  581. Assert.Equal (Key.PageUp | Key.CtrlMask, Application.AlternateBackwardKey);
  582. Assert.Equal (Key.Q | Key.CtrlMask, Application.QuitKey);
  583. }
  584. [Fact]
  585. [AutoInitShutdown]
  586. public void FileDialog_FileSystemWatcher ()
  587. {
  588. for (int i = 0; i < 8; i++) {
  589. var fd = new FileDialog ();
  590. fd.Ready += (s, e) => Application.RequestStop ();
  591. Application.Run (fd);
  592. }
  593. }
  594. [Fact, AutoInitShutdown]
  595. public void Mouse_Drag_On_Top_With_Superview_Null ()
  596. {
  597. var menu = new MenuBar (new MenuBarItem [] {
  598. new MenuBarItem("File", new MenuItem [] {
  599. new MenuItem("New", "", null)
  600. })
  601. });
  602. var sbar = new StatusBar (new StatusItem [] {
  603. new StatusItem(Key.N, "~CTRL-N~ New", null)
  604. });
  605. var win = new Window ("Window");
  606. var top = Application.Top;
  607. top.Add (menu, sbar, win);
  608. var iterations = -1;
  609. Application.Iteration = () => {
  610. iterations++;
  611. if (iterations == 0) {
  612. ((FakeDriver)Application.Driver).SetBufferSize (40, 15);
  613. MessageBox.Query ("About", "Hello Word", "Ok");
  614. } else if (iterations == 1) TestHelpers.AssertDriverContentsWithFrameAre (@"
  615. File
  616. ┌ Window ──────────────────────────────┐
  617. │ │
  618. │ │
  619. │ │
  620. │ ┌ About ───────────────┐ │
  621. │ │ Hello Word │ │
  622. │ │ │ │
  623. │ │ [◦ Ok ◦] │ │
  624. │ └──────────────────────┘ │
  625. │ │
  626. │ │
  627. │ │
  628. └──────────────────────────────────────┘
  629. CTRL-N New ", output);
  630. else if (iterations == 2) {
  631. Assert.Null (Application.MouseGrabView);
  632. // Grab the mouse
  633. ReflectionTools.InvokePrivate (
  634. typeof (Application),
  635. "ProcessMouseEvent",
  636. new MouseEvent () {
  637. X = 8,
  638. Y = 5,
  639. Flags = MouseFlags.Button1Pressed
  640. });
  641. Assert.Equal (Application.Current, Application.MouseGrabView);
  642. Assert.Equal (new Rect (8, 5, 24, 5), Application.MouseGrabView.Frame);
  643. } else if (iterations == 3) {
  644. Assert.Equal (Application.Current, Application.MouseGrabView);
  645. // Grab to left
  646. ReflectionTools.InvokePrivate (
  647. typeof (Application),
  648. "ProcessMouseEvent",
  649. new MouseEvent () {
  650. X = 7,
  651. Y = 5,
  652. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  653. });
  654. Assert.Equal (Application.Current, Application.MouseGrabView);
  655. Assert.Equal (new Rect (7, 5, 24, 5), Application.MouseGrabView.Frame);
  656. } else if (iterations == 4) {
  657. Assert.Equal (Application.Current, Application.MouseGrabView);
  658. TestHelpers.AssertDriverContentsWithFrameAre (@"
  659. File
  660. ┌ Window ──────────────────────────────┐
  661. │ │
  662. │ │
  663. │ │
  664. │ ┌ About ───────────────┐ │
  665. │ │ Hello Word │ │
  666. │ │ │ │
  667. │ │ [◦ Ok ◦] │ │
  668. │ └──────────────────────┘ │
  669. │ │
  670. │ │
  671. │ │
  672. └──────────────────────────────────────┘
  673. CTRL-N New ", output);
  674. Assert.Equal (Application.Current, Application.MouseGrabView);
  675. } else if (iterations == 5) {
  676. Assert.Equal (Application.Current, Application.MouseGrabView);
  677. // Grab to top
  678. ReflectionTools.InvokePrivate (
  679. typeof (Application),
  680. "ProcessMouseEvent",
  681. new MouseEvent () {
  682. X = 7,
  683. Y = 4,
  684. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  685. });
  686. Assert.Equal (Application.Current, Application.MouseGrabView);
  687. Assert.Equal (new Rect (7, 4, 24, 5), Application.MouseGrabView.Frame);
  688. } else if (iterations == 6) {
  689. Assert.Equal (Application.Current, Application.MouseGrabView);
  690. TestHelpers.AssertDriverContentsWithFrameAre (@"
  691. File
  692. ┌ Window ──────────────────────────────┐
  693. │ │
  694. │ │
  695. │ ┌ About ───────────────┐ │
  696. │ │ Hello Word │ │
  697. │ │ │ │
  698. │ │ [◦ Ok ◦] │ │
  699. │ └──────────────────────┘ │
  700. │ │
  701. │ │
  702. │ │
  703. │ │
  704. └──────────────────────────────────────┘
  705. CTRL-N New ", output);
  706. Assert.Equal (Application.Current, Application.MouseGrabView);
  707. Assert.Equal (new Rect (7, 4, 24, 5), Application.MouseGrabView.Frame);
  708. } else if (iterations == 7) {
  709. Assert.Equal (Application.Current, Application.MouseGrabView);
  710. // Ungrab the mouse
  711. ReflectionTools.InvokePrivate (
  712. typeof (Application),
  713. "ProcessMouseEvent",
  714. new MouseEvent () {
  715. X = 7,
  716. Y = 4,
  717. Flags = MouseFlags.Button1Released
  718. });
  719. Assert.Null (Application.MouseGrabView);
  720. } else if (iterations == 8) Application.RequestStop ();
  721. else if (iterations == 9) Application.RequestStop ();
  722. };
  723. Application.Run ();
  724. }
  725. [Fact, AutoInitShutdown]
  726. public void Mouse_Drag_On_Top_With_Superview_Not_Null ()
  727. {
  728. var menu = new MenuBar (new MenuBarItem [] {
  729. new MenuBarItem("File", new MenuItem [] {
  730. new MenuItem("New", "", null)
  731. })
  732. });
  733. var sbar = new StatusBar (new StatusItem [] {
  734. new StatusItem(Key.N, "~CTRL-N~ New", null)
  735. });
  736. var win = new Window ("Window") {
  737. X = 3,
  738. Y = 2,
  739. Width = Dim.Fill (10),
  740. Height = Dim.Fill (5)
  741. };
  742. var top = Application.Top;
  743. top.Add (menu, sbar, win);
  744. var iterations = -1;
  745. Application.Iteration = () => {
  746. iterations++;
  747. if (iterations == 0) {
  748. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  749. Assert.Null (Application.MouseGrabView);
  750. // Grab the mouse
  751. ReflectionTools.InvokePrivate (
  752. typeof (Application),
  753. "ProcessMouseEvent",
  754. new MouseEvent () {
  755. X = 4,
  756. Y = 2,
  757. Flags = MouseFlags.Button1Pressed
  758. });
  759. Assert.Equal (win, Application.MouseGrabView);
  760. Assert.Equal (new Rect (3, 2, 7, 3), Application.MouseGrabView.Frame);
  761. TestHelpers.AssertDriverContentsWithFrameAre (@"
  762. File
  763. ┌─────┐
  764. │ │
  765. └─────┘
  766. CTRL-N New", output);
  767. } else if (iterations == 1) {
  768. Assert.Equal (win, Application.MouseGrabView);
  769. // Grab to left
  770. ReflectionTools.InvokePrivate (
  771. typeof (Application),
  772. "ProcessMouseEvent",
  773. new MouseEvent () {
  774. X = 5,
  775. Y = 2,
  776. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  777. });
  778. Assert.Equal (win, Application.MouseGrabView);
  779. } else if (iterations == 2) {
  780. Assert.Equal (win, Application.MouseGrabView);
  781. TestHelpers.AssertDriverContentsWithFrameAre (@"
  782. File
  783. ┌────┐
  784. │ │
  785. └────┘
  786. CTRL-N New", output);
  787. Assert.Equal (win, Application.MouseGrabView);
  788. Assert.Equal (new Rect (4, 2, 6, 3), Application.MouseGrabView.Frame);
  789. } else if (iterations == 3) {
  790. Assert.Equal (win, Application.MouseGrabView);
  791. // Grab to top
  792. ReflectionTools.InvokePrivate (
  793. typeof (Application),
  794. "ProcessMouseEvent",
  795. new MouseEvent () {
  796. X = 5,
  797. Y = 1,
  798. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  799. });
  800. Assert.Equal (win, Application.MouseGrabView);
  801. } else if (iterations == 4) {
  802. Assert.Equal (win, Application.MouseGrabView);
  803. TestHelpers.AssertDriverContentsWithFrameAre (@"
  804. File
  805. ┌────┐
  806. │ │
  807. │ │
  808. └────┘
  809. CTRL-N New", output);
  810. Assert.Equal (win, Application.MouseGrabView);
  811. Assert.Equal (new Rect (4, 1, 6, 4), Application.MouseGrabView.Frame);
  812. } else if (iterations == 5) {
  813. Assert.Equal (win, Application.MouseGrabView);
  814. // Ungrab the mouse
  815. ReflectionTools.InvokePrivate (
  816. typeof (Application),
  817. "ProcessMouseEvent",
  818. new MouseEvent () {
  819. X = 7,
  820. Y = 4,
  821. Flags = MouseFlags.Button1Released
  822. });
  823. Assert.Null (Application.MouseGrabView);
  824. } else if (iterations == 8) Application.RequestStop ();
  825. };
  826. Application.Run ();
  827. }
  828. [Fact, AutoInitShutdown]
  829. public void EnsureVisibleBounds_With_Border_Null_Not_Throws ()
  830. {
  831. var top = new Toplevel ();
  832. Application.Begin (top);
  833. var exception = Record.Exception (() => ((FakeDriver)Application.Driver).SetBufferSize (0, 10));
  834. Assert.Null (exception);
  835. exception = Record.Exception (() => ((FakeDriver)Application.Driver).SetBufferSize (10, 0));
  836. Assert.Null (exception);
  837. }
  838. [Fact, AutoInitShutdown]
  839. public void OnEnter_OnLeave_Triggered_On_Application_Begin_End ()
  840. {
  841. var isEnter = false;
  842. var isLeave = false;
  843. var v = new View ();
  844. v.Enter += (s, _) => isEnter = true;
  845. v.Leave += (s, _) => isLeave = true;
  846. var top = Application.Top;
  847. top.Add (v);
  848. Assert.False (v.CanFocus);
  849. var exception = Record.Exception (() => top.OnEnter (top));
  850. Assert.Null (exception);
  851. exception = Record.Exception (() => top.OnLeave (top));
  852. Assert.Null (exception);
  853. v.CanFocus = true;
  854. Application.Begin (top);
  855. Assert.True (isEnter);
  856. Assert.False (isLeave);
  857. isEnter = false;
  858. var d = new Dialog ();
  859. var rs = Application.Begin (d);
  860. Assert.False (isEnter);
  861. Assert.True (isLeave);
  862. isLeave = false;
  863. Application.End (rs);
  864. Assert.True (isEnter);
  865. Assert.False (isLeave);
  866. }
  867. [Fact, AutoInitShutdown]
  868. public void PositionCursor_SetCursorVisibility_To_Invisible_If_Focused_Is_Null ()
  869. {
  870. var tf = new TextField ("test") { Width = 5 };
  871. var view = new View () { Width = 10, Height = 10 };
  872. view.Add (tf);
  873. Application.Top.Add (view);
  874. Application.Begin (Application.Top);
  875. Assert.True (tf.HasFocus);
  876. Application.Driver.GetCursorVisibility (out CursorVisibility cursor);
  877. Assert.Equal (CursorVisibility.Default, cursor);
  878. view.Enabled = false;
  879. Assert.False (tf.HasFocus);
  880. Application.Refresh ();
  881. Application.Driver.GetCursorVisibility (out cursor);
  882. Assert.Equal (CursorVisibility.Invisible, cursor);
  883. }
  884. [Fact, AutoInitShutdown]
  885. public void IsLoaded_Application_Begin ()
  886. {
  887. var top = Application.Top;
  888. Assert.False (top.IsLoaded);
  889. Application.Begin (top);
  890. Assert.True (top.IsLoaded);
  891. }
  892. [Fact, AutoInitShutdown]
  893. public void IsLoaded_With_Sub_Toplevel_Application_Begin_NeedDisplay ()
  894. {
  895. var top = Application.Top;
  896. var subTop = new Toplevel ();
  897. var view = new View (new Rect (0, 0, 20, 10));
  898. subTop.Add (view);
  899. top.Add (subTop);
  900. Assert.False (top.IsLoaded);
  901. Assert.False (subTop.IsLoaded);
  902. Assert.Equal (new Rect (0, 0, 20, 10), view.Frame);
  903. Assert.Equal (new Rect (0, 0, 20, 10), view.NeedDisplay);
  904. view.LayoutStarted += view_LayoutStarted;
  905. void view_LayoutStarted (object sender, LayoutEventArgs e)
  906. {
  907. Assert.Equal (new Rect (0, 0, 20, 10), view.NeedDisplay);
  908. view.LayoutStarted -= view_LayoutStarted;
  909. }
  910. Application.Begin (top);
  911. Assert.True (top.IsLoaded);
  912. Assert.True (subTop.IsLoaded);
  913. Assert.Equal (new Rect (0, 0, 20, 10), view.Frame);
  914. view.Frame = new Rect (1, 3, 10, 5);
  915. Assert.Equal (new Rect (1, 3, 10, 5), view.Frame);
  916. Assert.Equal (new Rect (0, 0, 10, 5), view.NeedDisplay);
  917. view.Redraw (view.Bounds);
  918. view.Frame = new Rect (1, 3, 10, 5);
  919. Assert.Equal (new Rect (1, 3, 10, 5), view.Frame);
  920. Assert.Equal (new Rect (0, 0, 10, 5), view.NeedDisplay);
  921. }
  922. [Fact, AutoInitShutdown]
  923. public void Toplevel_Inside_ScrollView_MouseGrabView ()
  924. {
  925. var scrollView = new ScrollView () {
  926. X = 3,
  927. Y = 3,
  928. Width = 40,
  929. Height = 16,
  930. ContentSize = new Size (200, 100)
  931. };
  932. var win = new Window ("Window") { X = 3, Y = 3, Width = Dim.Fill (3), Height = Dim.Fill (3) };
  933. scrollView.Add (win);
  934. var top = Application.Top;
  935. top.Add (scrollView);
  936. Application.Begin (top);
  937. Assert.Equal (new Rect (0, 0, 80, 25), top.Frame);
  938. Assert.Equal (new Rect (3, 3, 40, 16), scrollView.Frame);
  939. Assert.Equal (new Rect (0, 0, 200, 100), scrollView.Subviews [0].Frame);
  940. Assert.Equal (new Rect (3, 3, 194, 94), win.Frame);
  941. TestHelpers.AssertDriverContentsWithFrameAre (@"
  942. ┌ Window ───────────────────────────┴
  943. │ ░
  944. │ ░
  945. │ ░
  946. │ ░
  947. │ ░
  948. │ ░
  949. │ ░
  950. │ ░
  951. │ ░
  952. │ ░
  953. │ ▼
  954. ◄├──────┤░░░░░░░░░░░░░░░░░░░░░░░░░░░░░► ", output);
  955. ReflectionTools.InvokePrivate (
  956. typeof (Application),
  957. "ProcessMouseEvent",
  958. new MouseEvent () {
  959. X = 6,
  960. Y = 6,
  961. Flags = MouseFlags.Button1Pressed
  962. });
  963. Assert.Equal (win, Application.MouseGrabView);
  964. Assert.Equal (new Rect (3, 3, 194, 94), win.Frame);
  965. ReflectionTools.InvokePrivate (
  966. typeof (Application),
  967. "ProcessMouseEvent",
  968. new MouseEvent () {
  969. X = 9,
  970. Y = 9,
  971. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  972. });
  973. Assert.Equal (win, Application.MouseGrabView);
  974. top.SetNeedsLayout ();
  975. top.LayoutSubviews ();
  976. Assert.Equal (new Rect (6, 6, 191, 91), win.Frame);
  977. Application.Refresh ();
  978. TestHelpers.AssertDriverContentsWithFrameAre (@"
  979. ┌ Window ────────────────────────░
  980. │ ░
  981. │ ░
  982. │ ░
  983. │ ░
  984. │ ░
  985. │ ░
  986. │ ░
  987. │ ▼
  988. ◄├──────┤░░░░░░░░░░░░░░░░░░░░░░░░░░░░░► ", output);
  989. ReflectionTools.InvokePrivate (
  990. typeof (Application),
  991. "ProcessMouseEvent",
  992. new MouseEvent () {
  993. X = 5,
  994. Y = 5,
  995. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  996. });
  997. Assert.Equal (win, Application.MouseGrabView);
  998. top.SetNeedsLayout ();
  999. top.LayoutSubviews ();
  1000. Assert.Equal (new Rect (2, 2, 195, 95), win.Frame);
  1001. Application.Refresh ();
  1002. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1003. ┌ Window ────────────────────────────│
  1004. │ ┴
  1005. │ ░
  1006. │ ░
  1007. │ ░
  1008. │ ░
  1009. │ ░
  1010. │ ░
  1011. │ ░
  1012. │ ░
  1013. │ ░
  1014. │ ░
  1015. │ ▼
  1016. ◄├──────┤░░░░░░░░░░░░░░░░░░░░░░░░░░░░░► ", output);
  1017. ReflectionTools.InvokePrivate (
  1018. typeof (Application),
  1019. "ProcessMouseEvent",
  1020. new MouseEvent () {
  1021. X = 5,
  1022. Y = 5,
  1023. Flags = MouseFlags.Button1Released
  1024. });
  1025. Assert.Null (Application.MouseGrabView);
  1026. ReflectionTools.InvokePrivate (
  1027. typeof (Application),
  1028. "ProcessMouseEvent",
  1029. new MouseEvent () {
  1030. X = 4,
  1031. Y = 4,
  1032. Flags = MouseFlags.ReportMousePosition
  1033. });
  1034. Assert.Equal (scrollView, Application.MouseGrabView);
  1035. }
  1036. [Fact, AutoInitShutdown]
  1037. public void Dialog_Bounds_Bigger_Than_Driver_Cols_And_Rows_Allow_Drag_Beyond_Left_Right_And_Bottom ()
  1038. {
  1039. var menu = new MenuBar (new MenuBarItem [] {
  1040. new MenuBarItem("File", new MenuItem [] {
  1041. new MenuItem("New", "", null)
  1042. })
  1043. });
  1044. var sb = new StatusBar (new StatusItem [] {
  1045. new StatusItem(Key.N, "~CTRL-N~ New", null)
  1046. });
  1047. var top = Application.Top;
  1048. top.Add (menu, sb);
  1049. var dialog = new Dialog ("Dialog", 20, 3, new Button ("Ok"));
  1050. Application.Begin (top);
  1051. ((FakeDriver)Application.Driver).SetBufferSize (40, 10);
  1052. Application.Begin (dialog);
  1053. Application.Refresh ();
  1054. Assert.Equal (new Rect (0, 0, 40, 10), top.Frame);
  1055. Assert.Equal (new Rect (10, 3, 20, 3), dialog.Frame);
  1056. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1057. File
  1058. ┌ Dialog ──────────┐
  1059. │ [ Ok ] │
  1060. └──────────────────┘
  1061. CTRL-N New ", output);
  1062. Assert.Null (Application.MouseGrabView);
  1063. ReflectionTools.InvokePrivate (
  1064. typeof (Application),
  1065. "ProcessMouseEvent",
  1066. new MouseEvent () {
  1067. X = 10,
  1068. Y = 3,
  1069. Flags = MouseFlags.Button1Pressed
  1070. });
  1071. Assert.Equal (dialog, Application.MouseGrabView);
  1072. ReflectionTools.InvokePrivate (
  1073. typeof (Application),
  1074. "ProcessMouseEvent",
  1075. new MouseEvent () {
  1076. X = -11,
  1077. Y = -4,
  1078. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1079. });
  1080. Application.Refresh ();
  1081. Assert.Equal (new Rect (0, 0, 40, 10), top.Frame);
  1082. Assert.Equal (new Rect (0, 1, 20, 3), dialog.Frame);
  1083. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1084. File
  1085. ┌ Dialog ──────────┐
  1086. │ [ Ok ] │
  1087. └──────────────────┘
  1088. CTRL-N New ", output);
  1089. // Changes Top size to same size as Dialog more menu and scroll bar
  1090. ((FakeDriver)Application.Driver).SetBufferSize (20, 5);
  1091. ReflectionTools.InvokePrivate (
  1092. typeof (Application),
  1093. "ProcessMouseEvent",
  1094. new MouseEvent () {
  1095. X = -1,
  1096. Y = -1,
  1097. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1098. });
  1099. Application.Refresh ();
  1100. Assert.Equal (new Rect (0, 0, 20, 5), top.Frame);
  1101. Assert.Equal (new Rect (0, 1, 20, 3), dialog.Frame);
  1102. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1103. File
  1104. ┌ Dialog ──────────┐
  1105. │ [ Ok ] │
  1106. └──────────────────┘
  1107. CTRL-N New ", output);
  1108. // Changes Top size smaller than Dialog size
  1109. ((FakeDriver)Application.Driver).SetBufferSize (19, 3);
  1110. ReflectionTools.InvokePrivate (
  1111. typeof (Application),
  1112. "ProcessMouseEvent",
  1113. new MouseEvent () {
  1114. X = -1,
  1115. Y = -1,
  1116. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1117. });
  1118. Application.Refresh ();
  1119. Assert.Equal (new Rect (0, 0, 19, 3), top.Frame);
  1120. Assert.Equal (new Rect (-1, 1, 20, 3), dialog.Frame);
  1121. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1122. File
  1123. Dialog ──────────┐
  1124. [ Ok ] │", output);
  1125. ReflectionTools.InvokePrivate (
  1126. typeof (Application),
  1127. "ProcessMouseEvent",
  1128. new MouseEvent () {
  1129. X = 18,
  1130. Y = 3,
  1131. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1132. });
  1133. Application.Refresh ();
  1134. Assert.Equal (new Rect (0, 0, 19, 3), top.Frame);
  1135. Assert.Equal (new Rect (18, 2, 20, 3), dialog.Frame);
  1136. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1137. File
  1138. CTRL-N New ┌", output);
  1139. // On a real app we can't go beyond the SuperView bounds
  1140. ReflectionTools.InvokePrivate (
  1141. typeof (Application),
  1142. "ProcessMouseEvent",
  1143. new MouseEvent () {
  1144. X = 19,
  1145. Y = 4,
  1146. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1147. });
  1148. Application.Refresh ();
  1149. Assert.Equal (new Rect (0, 0, 19, 3), top.Frame);
  1150. Assert.Equal (new Rect (19, 2, 20, 3), dialog.Frame);
  1151. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1152. File
  1153. CTRL-N New", output);
  1154. }
  1155. }
  1156. }