ToplevelTests.cs 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  1. using System;
  2. using Terminal.Gui;
  3. using Xunit;
  4. using Xunit.Abstractions;
  5. namespace Terminal.Gui.ViewsTests {
  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. top.BeginInit ();
  33. top.EndInit ();
  34. Assert.Equal (new Rect (0, 0, Application.Driver.Cols, Application.Driver.Rows), top.Bounds);
  35. }
  36. [Fact]
  37. [AutoInitShutdown]
  38. public void Application_Top_EnsureVisibleBounds_To_Driver_Rows_And_Cols ()
  39. {
  40. var iterations = 0;
  41. Application.Iteration += () => {
  42. if (iterations == 0) {
  43. Assert.False (Application.Top.AutoSize);
  44. Assert.Equal ("Top1", Application.Top.Text);
  45. Assert.Equal (0, Application.Top.Frame.X);
  46. Assert.Equal (0, Application.Top.Frame.Y);
  47. Assert.Equal (Application.Driver.Cols, Application.Top.Frame.Width);
  48. Assert.Equal (Application.Driver.Rows, Application.Top.Frame.Height);
  49. Application.Top.ProcessHotKey (new KeyEvent (Key.CtrlMask | Key.R, new KeyModifiers ()));
  50. } else if (iterations == 1) {
  51. Assert.Equal ("Top2", Application.Top.Text);
  52. Assert.Equal (0, Application.Top.Frame.X);
  53. Assert.Equal (0, Application.Top.Frame.Y);
  54. Assert.Equal (Application.Driver.Cols, Application.Top.Frame.Width);
  55. Assert.Equal (Application.Driver.Rows, Application.Top.Frame.Height);
  56. Application.Top.ProcessHotKey (new KeyEvent (Key.CtrlMask | Key.C, new KeyModifiers ()));
  57. } else if (iterations == 3) {
  58. Assert.Equal ("Top1", Application.Top.Text);
  59. Assert.Equal (0, Application.Top.Frame.X);
  60. Assert.Equal (0, Application.Top.Frame.Y);
  61. Assert.Equal (Application.Driver.Cols, Application.Top.Frame.Width);
  62. Assert.Equal (Application.Driver.Rows, Application.Top.Frame.Height);
  63. Application.Top.ProcessHotKey (new KeyEvent (Key.CtrlMask | Key.R, new KeyModifiers ()));
  64. } else if (iterations == 4) {
  65. Assert.Equal ("Top2", Application.Top.Text);
  66. Assert.Equal (0, Application.Top.Frame.X);
  67. Assert.Equal (0, Application.Top.Frame.Y);
  68. Assert.Equal (Application.Driver.Cols, Application.Top.Frame.Width);
  69. Assert.Equal (Application.Driver.Rows, Application.Top.Frame.Height);
  70. Application.Top.ProcessHotKey (new KeyEvent (Key.CtrlMask | Key.C, new KeyModifiers ()));
  71. } else if (iterations == 6) {
  72. Assert.Equal ("Top1", Application.Top.Text);
  73. Assert.Equal (0, Application.Top.Frame.X);
  74. Assert.Equal (0, Application.Top.Frame.Y);
  75. Assert.Equal (Application.Driver.Cols, Application.Top.Frame.Width);
  76. Assert.Equal (Application.Driver.Rows, Application.Top.Frame.Height);
  77. Application.Top.ProcessHotKey (new KeyEvent (Key.CtrlMask | Key.Q, new KeyModifiers ()));
  78. }
  79. iterations++;
  80. };
  81. Application.Run (Top1 ());
  82. Toplevel Top1 ()
  83. {
  84. var top = Application.Top;
  85. top.Text = "Top1";
  86. var menu = new MenuBar (new MenuBarItem [] {
  87. new MenuBarItem ("_Options", new MenuItem [] {
  88. new MenuItem ("_Run Top2", "", () => Application.Run (Top2 ()), null, null, Key.CtrlMask | Key.R),
  89. new MenuItem ("_Quit", "", () => Application.RequestStop(), null, null, Key.CtrlMask | Key.Q)
  90. })
  91. });
  92. top.Add (menu);
  93. var statusBar = new StatusBar (new [] {
  94. new StatusItem(Key.CtrlMask | Key.R, "~^R~ Run Top2", () => Application.Run (Top2 ())),
  95. new StatusItem(Application.QuitKey, $"{Application.QuitKey} to Quit", () => Application.RequestStop())
  96. });
  97. top.Add (statusBar);
  98. var t1 = new Toplevel ();
  99. top.Add (t1);
  100. return top;
  101. }
  102. Toplevel Top2 ()
  103. {
  104. var top = new Toplevel (Application.Top.Frame);
  105. top.Text = "Top2";
  106. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  107. var menu = new MenuBar (new MenuBarItem [] {
  108. new MenuBarItem ("_Stage", new MenuItem [] {
  109. new MenuItem ("_Close", "", () => Application.RequestStop(), null, null, Key.CtrlMask | Key.C)
  110. })
  111. });
  112. top.Add (menu);
  113. var statusBar = new StatusBar (new [] {
  114. new StatusItem(Key.CtrlMask | Key.C, "~^C~ Close", () => Application.RequestStop()),
  115. });
  116. top.Add (statusBar);
  117. win.Add (new ListView () {
  118. X = 0,
  119. Y = 0,
  120. Width = Dim.Fill (),
  121. Height = Dim.Fill ()
  122. });
  123. top.Add (win);
  124. return top;
  125. }
  126. }
  127. [Fact]
  128. [AutoInitShutdown]
  129. public void Internal_Tests ()
  130. {
  131. var top = new Toplevel ();
  132. var eventInvoked = "";
  133. top.ChildUnloaded += (s, e) => eventInvoked = "ChildUnloaded";
  134. top.OnChildUnloaded (top);
  135. Assert.Equal ("ChildUnloaded", eventInvoked);
  136. top.ChildLoaded += (s, e) => eventInvoked = "ChildLoaded";
  137. top.OnChildLoaded (top);
  138. Assert.Equal ("ChildLoaded", eventInvoked);
  139. top.Closed += (s, e) => eventInvoked = "Closed";
  140. top.OnClosed (top);
  141. Assert.Equal ("Closed", eventInvoked);
  142. top.Closing += (s, e) => eventInvoked = "Closing";
  143. top.OnClosing (new ToplevelClosingEventArgs (top));
  144. Assert.Equal ("Closing", eventInvoked);
  145. top.AllChildClosed += (s, e) => eventInvoked = "AllChildClosed";
  146. top.OnAllChildClosed ();
  147. Assert.Equal ("AllChildClosed", eventInvoked);
  148. top.ChildClosed += (s, e) => eventInvoked = "ChildClosed";
  149. top.OnChildClosed (top);
  150. Assert.Equal ("ChildClosed", eventInvoked);
  151. top.Deactivate += (s, e) => eventInvoked = "Deactivate";
  152. top.OnDeactivate (top);
  153. Assert.Equal ("Deactivate", eventInvoked);
  154. top.Activate += (s, e) => eventInvoked = "Activate";
  155. top.OnActivate (top);
  156. Assert.Equal ("Activate", eventInvoked);
  157. top.Loaded += (s, e) => eventInvoked = "Loaded";
  158. top.OnLoaded ();
  159. Assert.Equal ("Loaded", eventInvoked);
  160. top.Ready += (s, e) => eventInvoked = "Ready";
  161. top.OnReady ();
  162. Assert.Equal ("Ready", eventInvoked);
  163. top.Unloaded += (s, e) => eventInvoked = "Unloaded";
  164. top.OnUnloaded ();
  165. Assert.Equal ("Unloaded", eventInvoked);
  166. top.AddMenuStatusBar (new MenuBar ());
  167. Assert.NotNull (top.MenuBar);
  168. top.AddMenuStatusBar (new StatusBar ());
  169. Assert.NotNull (top.StatusBar);
  170. top.RemoveMenuStatusBar (top.MenuBar);
  171. Assert.Null (top.MenuBar);
  172. top.RemoveMenuStatusBar (top.StatusBar);
  173. Assert.Null (top.StatusBar);
  174. Application.Begin (top);
  175. Assert.Equal (top, Application.Top);
  176. // Application.Top without menu and status bar.
  177. var supView = top.EnsureVisibleBounds (top, 2, 2, out int nx, out int ny, out MenuBar mb, out StatusBar sb);
  178. Assert.Equal (Application.Top, supView);
  179. Assert.Equal (0, nx);
  180. Assert.Equal (0, ny);
  181. Assert.Null (mb);
  182. Assert.Null (sb);
  183. top.AddMenuStatusBar (new MenuBar ());
  184. Assert.NotNull (top.MenuBar);
  185. // Application.Top with a menu and without status bar.
  186. top.EnsureVisibleBounds (top, 2, 2, out nx, out ny, out mb, out sb);
  187. Assert.Equal (0, nx);
  188. Assert.Equal (1, ny);
  189. Assert.NotNull (mb);
  190. Assert.Null (sb);
  191. top.AddMenuStatusBar (new StatusBar ());
  192. Assert.NotNull (top.StatusBar);
  193. // Application.Top with a menu and status bar.
  194. top.EnsureVisibleBounds (top, 2, 2, out nx, out ny, out mb, out sb);
  195. Assert.Equal (0, nx);
  196. // The available height is lower than the Application.Top height minus
  197. // the menu bar and status bar, then the top can go beyond the bottom
  198. Assert.Equal (2, ny);
  199. Assert.NotNull (mb);
  200. Assert.NotNull (sb);
  201. top.RemoveMenuStatusBar (top.MenuBar);
  202. Assert.Null (top.MenuBar);
  203. // Application.Top without a menu and with a status bar.
  204. top.EnsureVisibleBounds (top, 2, 2, out nx, out ny, out mb, out sb);
  205. Assert.Equal (0, nx);
  206. // The available height is lower than the Application.Top height minus
  207. // the status bar, then the top can go beyond the bottom
  208. Assert.Equal (2, ny);
  209. Assert.Null (mb);
  210. Assert.NotNull (sb);
  211. top.RemoveMenuStatusBar (top.StatusBar);
  212. Assert.Null (top.StatusBar);
  213. Assert.Null (top.MenuBar);
  214. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  215. top.Add (win);
  216. top.LayoutSubviews ();
  217. // The SuperView is always the same regardless of the caller.
  218. supView = top.EnsureVisibleBounds (win, 0, 0, out nx, out ny, out mb, out sb);
  219. Assert.Equal (Application.Top, supView);
  220. supView = win.EnsureVisibleBounds (win, 0, 0, out nx, out ny, out mb, out sb);
  221. Assert.Equal (Application.Top, supView);
  222. // Application.Top without menu and status bar.
  223. top.EnsureVisibleBounds (win, 0, 0, out nx, out ny, out mb, out sb);
  224. Assert.Equal (0, nx);
  225. Assert.Equal (0, ny);
  226. Assert.Null (mb);
  227. Assert.Null (sb);
  228. top.AddMenuStatusBar (new MenuBar ());
  229. Assert.NotNull (top.MenuBar);
  230. // Application.Top with a menu and without status bar.
  231. top.EnsureVisibleBounds (win, 2, 2, out nx, out ny, out mb, out sb);
  232. Assert.Equal (0, nx);
  233. Assert.Equal (1, ny);
  234. Assert.NotNull (mb);
  235. Assert.Null (sb);
  236. top.AddMenuStatusBar (new StatusBar ());
  237. Assert.NotNull (top.StatusBar);
  238. // Application.Top with a menu and status bar.
  239. top.EnsureVisibleBounds (win, 30, 20, out nx, out ny, out mb, out sb);
  240. Assert.Equal (0, nx);
  241. // The available height is lower than the Application.Top height minus
  242. // the menu bar and status bar, then the top can go beyond the bottom
  243. Assert.Equal (20, ny);
  244. Assert.NotNull (mb);
  245. Assert.NotNull (sb);
  246. top.RemoveMenuStatusBar (top.MenuBar);
  247. top.RemoveMenuStatusBar (top.StatusBar);
  248. Assert.Null (top.StatusBar);
  249. Assert.Null (top.MenuBar);
  250. top.Remove (win);
  251. win = new Window () { Width = 60, Height = 15 };
  252. top.Add (win);
  253. // Application.Top without menu and status bar.
  254. top.EnsureVisibleBounds (win, 0, 0, out nx, out ny, out mb, out sb);
  255. Assert.Equal (0, nx);
  256. Assert.Equal (0, ny);
  257. Assert.Null (mb);
  258. Assert.Null (sb);
  259. top.AddMenuStatusBar (new MenuBar ());
  260. Assert.NotNull (top.MenuBar);
  261. // Application.Top with a menu and without status bar.
  262. top.EnsureVisibleBounds (win, 2, 2, out nx, out ny, out mb, out sb);
  263. Assert.Equal (2, nx);
  264. Assert.Equal (2, ny);
  265. Assert.NotNull (mb);
  266. Assert.Null (sb);
  267. top.AddMenuStatusBar (new StatusBar ());
  268. Assert.NotNull (top.StatusBar);
  269. // Application.Top with a menu and status bar.
  270. top.EnsureVisibleBounds (win, 30, 20, out nx, out ny, out mb, out sb);
  271. Assert.Equal (20, nx); // 20+60=80
  272. Assert.Equal (9, ny); // 9+15+1(mb)=25
  273. Assert.NotNull (mb);
  274. Assert.NotNull (sb);
  275. top.PositionToplevels ();
  276. Assert.Equal (new Rect (0, 1, 60, 15), win.Frame);
  277. Assert.Null (Toplevel.dragPosition);
  278. win.MouseEvent (new MouseEvent () { X = 6, Y = 0, Flags = MouseFlags.Button1Pressed });
  279. Assert.Equal (new Point (6, 0), Toplevel.dragPosition);
  280. win.MouseEvent (new MouseEvent () { X = 6, Y = 0, Flags = MouseFlags.Button1Released });
  281. Assert.Null (Toplevel.dragPosition);
  282. win.CanFocus = false;
  283. win.MouseEvent (new MouseEvent () { X = 6, Y = 0, Flags = MouseFlags.Button1Pressed });
  284. Assert.Null (Toplevel.dragPosition);
  285. }
  286. [Fact]
  287. [AutoInitShutdown]
  288. public void KeyBindings_Command ()
  289. {
  290. var isRunning = false;
  291. var win1 = new Window ("Win1") { Id = "win1", Width = Dim.Percent (50f), Height = Dim.Fill () };
  292. var lblTf1W1 = new Label ("Enter text in TextField on Win1:") { Id = "lblTf1W1" };
  293. var tf1W1 = new TextField ("Text1 on Win1") { Id = "tf1W1", X = Pos.Right (lblTf1W1) + 1, Width = Dim.Fill () };
  294. var lblTvW1 = new Label ("Enter text in TextView on Win1:") { Id = "lblTvW1", Y = Pos.Bottom (lblTf1W1) + 1 };
  295. var tvW1 = new TextView () { Id = "tvW1", X = Pos.Left (tf1W1), Width = Dim.Fill (), Height = 2, Text = "First line Win1\nSecond line Win1" };
  296. var lblTf2W1 = new Label ("Enter text in TextField on Win1:") { Id = "lblTf2W1", Y = Pos.Bottom (lblTvW1) + 1 };
  297. var tf2W1 = new TextField ("Text2 on Win1") { Id = "tf2W1", X = Pos.Left (tf1W1), Width = Dim.Fill () };
  298. win1.Add (lblTf1W1, tf1W1, lblTvW1, tvW1, lblTf2W1, tf2W1);
  299. var win2 = new Window ("Win2") { Id = "win2", X = Pos.Right (win1) + 1, Width = Dim.Percent (50f), Height = Dim.Fill () };
  300. var lblTf1W2 = new Label ("Enter text in TextField on Win2:") { Id = "lblTf1W2" };
  301. var tf1W2 = new TextField ("Text1 on Win2") { Id = "tf1W2", X = Pos.Right (lblTf1W2) + 1, Width = Dim.Fill () };
  302. var lblTvW2 = new Label ("Enter text in TextView on Win2:") { Id = "lblTvW2", Y = Pos.Bottom (lblTf1W2) + 1 };
  303. var tvW2 = new TextView () { Id = "tvW2", X = Pos.Left (tf1W2), Width = Dim.Fill (), Height = 2, Text = "First line Win1\nSecond line Win2" };
  304. var lblTf2W2 = new Label ("Enter text in TextField on Win2:") { Id = "lblTf2W2", Y = Pos.Bottom (lblTvW2) + 1 };
  305. var tf2W2 = new TextField ("Text2 on Win2") { Id = "tf2W2", X = Pos.Left (tf1W2), Width = Dim.Fill () };
  306. win2.Add (lblTf1W2, tf1W2, lblTvW2, tvW2, lblTf2W2, tf2W2);
  307. var top = Application.Top;
  308. top.Add (win1, win2);
  309. top.Loaded += (s, e) => isRunning = true;
  310. top.Closing += (s, e) => isRunning = false;
  311. Application.Begin (top);
  312. top.Running = true;
  313. Assert.Equal (new Rect (0, 0, 40, 25), win1.Frame);
  314. Assert.Equal (new Rect (41, 0, 40, 25), win2.Frame);
  315. Assert.Equal (win1, top.Focused);
  316. Assert.Equal (tf1W1, top.MostFocused);
  317. Assert.True (isRunning);
  318. Assert.True (top.Focused.ProcessKey (new KeyEvent (Application.QuitKey, new KeyModifiers ())));
  319. Assert.False (isRunning);
  320. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Z | Key.CtrlMask, new KeyModifiers ())));
  321. Assert.False (top.Focused.ProcessKey (new KeyEvent (Key.F5, new KeyModifiers ())));
  322. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  323. Assert.Equal (win1, top.Focused);
  324. Assert.Equal (tvW1, top.MostFocused);
  325. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  326. Assert.Equal ($"\tFirst line Win1{Environment.NewLine}Second line Win1", tvW1.Text);
  327. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ())));
  328. Assert.Equal ($"First line Win1{Environment.NewLine}Second line Win1", tvW1.Text);
  329. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers ())));
  330. Assert.Equal (win1, top.Focused);
  331. Assert.Equal (tf2W1, top.MostFocused);
  332. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  333. Assert.Equal (win1, top.Focused);
  334. Assert.Equal (tf1W1, top.MostFocused);
  335. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  336. Assert.Equal (win1, top.Focused);
  337. Assert.Equal (tf1W1, top.MostFocused);
  338. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  339. Assert.Equal (win1, top.Focused);
  340. Assert.Equal (tvW1, top.MostFocused);
  341. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.I | Key.CtrlMask, new KeyModifiers ())));
  342. Assert.Equal (win1, top.Focused);
  343. Assert.Equal (tf2W1, top.MostFocused);
  344. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ())));
  345. Assert.Equal (win1, top.Focused);
  346. Assert.Equal (tvW1, top.MostFocused);
  347. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ())));
  348. Assert.Equal (win1, top.Focused);
  349. Assert.Equal (tf1W1, top.MostFocused);
  350. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ())));
  351. Assert.Equal (win1, top.Focused);
  352. Assert.Equal (tf2W1, top.MostFocused);
  353. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers ())));
  354. Assert.Equal (win2, top.Focused);
  355. Assert.Equal (tf1W2, top.MostFocused);
  356. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask | Key.ShiftMask, new KeyModifiers ())));
  357. Assert.Equal (win1, top.Focused);
  358. Assert.Equal (tf2W1, top.MostFocused);
  359. Assert.True (top.Focused.ProcessKey (new KeyEvent (Application.AlternateForwardKey, new KeyModifiers ())));
  360. Assert.Equal (win2, top.Focused);
  361. Assert.Equal (tf1W2, top.MostFocused);
  362. Assert.True (top.Focused.ProcessKey (new KeyEvent (Application.AlternateBackwardKey, new KeyModifiers ())));
  363. Assert.Equal (win1, top.Focused);
  364. Assert.Equal (tf2W1, top.MostFocused);
  365. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ())));
  366. Assert.Equal (win1, top.Focused);
  367. Assert.Equal (tvW1, top.MostFocused);
  368. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.B | Key.CtrlMask, new KeyModifiers ())));
  369. Assert.Equal (win1, top.Focused);
  370. Assert.Equal (tf1W1, top.MostFocused);
  371. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  372. Assert.Equal (win1, top.Focused);
  373. Assert.Equal (tvW1, top.MostFocused);
  374. Assert.Equal (new Point (0, 0), tvW1.CursorPosition);
  375. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.End | Key.CtrlMask, new KeyModifiers ())));
  376. Assert.Equal (win1, top.Focused);
  377. Assert.Equal (tvW1, top.MostFocused);
  378. Assert.Equal (new Point (16, 1), tvW1.CursorPosition);
  379. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.F | Key.CtrlMask, new KeyModifiers ())));
  380. Assert.Equal (win1, top.Focused);
  381. Assert.Equal (tf2W1, top.MostFocused);
  382. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.L | Key.CtrlMask, new KeyModifiers ())));
  383. }
  384. [Fact]
  385. [AutoInitShutdown]
  386. public void KeyBindings_Command_With_MdiTop ()
  387. {
  388. var top = Application.Top;
  389. Assert.Null (Application.MdiTop);
  390. top.IsMdiContainer = true;
  391. Application.Begin (top);
  392. Assert.Equal (Application.Top, Application.MdiTop);
  393. var isRunning = true;
  394. var win1 = new Window ("Win1") { Width = Dim.Percent (50f), Height = Dim.Fill () };
  395. var lblTf1W1 = new Label ("Enter text in TextField on Win1:");
  396. var tf1W1 = new TextField ("Text1 on Win1") { X = Pos.Right (lblTf1W1) + 1, Width = Dim.Fill () };
  397. var lblTvW1 = new Label ("Enter text in TextView on Win1:") { Y = Pos.Bottom (lblTf1W1) + 1 };
  398. var tvW1 = new TextView () { X = Pos.Left (tf1W1), Width = Dim.Fill (), Height = 2, Text = "First line Win1\nSecond line Win1" };
  399. var lblTf2W1 = new Label ("Enter text in TextField on Win1:") { Y = Pos.Bottom (lblTvW1) + 1 };
  400. var tf2W1 = new TextField ("Text2 on Win1") { X = Pos.Left (tf1W1), Width = Dim.Fill () };
  401. win1.Add (lblTf1W1, tf1W1, lblTvW1, tvW1, lblTf2W1, tf2W1);
  402. var win2 = new Window ("Win2") { Width = Dim.Percent (50f), Height = Dim.Fill () };
  403. var lblTf1W2 = new Label ("Enter text in TextField on Win2:");
  404. var tf1W2 = new TextField ("Text1 on Win2") { X = Pos.Right (lblTf1W2) + 1, Width = Dim.Fill () };
  405. var lblTvW2 = new Label ("Enter text in TextView on Win2:") { Y = Pos.Bottom (lblTf1W2) + 1 };
  406. var tvW2 = new TextView () { X = Pos.Left (tf1W2), Width = Dim.Fill (), Height = 2, Text = "First line Win1\nSecond line Win2" };
  407. var lblTf2W2 = new Label ("Enter text in TextField on Win2:") { Y = Pos.Bottom (lblTvW2) + 1 };
  408. var tf2W2 = new TextField ("Text2 on Win2") { X = Pos.Left (tf1W2), Width = Dim.Fill () };
  409. win2.Add (lblTf1W2, tf1W2, lblTvW2, tvW2, lblTf2W2, tf2W2);
  410. win1.Closing += (s, e) => isRunning = false;
  411. Assert.Null (top.Focused);
  412. Assert.Equal (top, Application.Current);
  413. Assert.True (top.IsCurrentTop);
  414. Assert.Equal (top, Application.MdiTop);
  415. Application.Begin (win1);
  416. Assert.Equal (new Rect (0, 0, 40, 25), win1.Frame);
  417. Assert.NotEqual (top, Application.Current);
  418. Assert.False (top.IsCurrentTop);
  419. Assert.Equal (win1, Application.Current);
  420. Assert.True (win1.IsCurrentTop);
  421. Assert.True (win1.IsMdiChild);
  422. Assert.Null (top.Focused);
  423. Assert.Null (top.MostFocused);
  424. Assert.Equal (tf1W1, win1.MostFocused);
  425. Assert.True (win1.IsMdiChild);
  426. Assert.Single (Application.MdiChildes);
  427. Application.Begin (win2);
  428. Assert.Equal (new Rect (0, 0, 40, 25), win2.Frame);
  429. Assert.NotEqual (top, Application.Current);
  430. Assert.False (top.IsCurrentTop);
  431. Assert.Equal (win2, Application.Current);
  432. Assert.True (win2.IsCurrentTop);
  433. Assert.True (win2.IsMdiChild);
  434. Assert.Null (top.Focused);
  435. Assert.Null (top.MostFocused);
  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 win = new Window ();
  598. var top = Application.Top;
  599. top.Add (win);
  600. var iterations = -1;
  601. Application.Iteration = () => {
  602. iterations++;
  603. if (iterations == 0) {
  604. ((FakeDriver)Application.Driver).SetBufferSize (40, 15);
  605. MessageBox.Query ("", "Hello Word", "Ok");
  606. } else if (iterations == 1) {
  607. TestHelpers.AssertDriverContentsWithFrameAre (@"
  608. ┌──────────────────────────────────────┐
  609. │ │
  610. │ │
  611. │ │
  612. │ │
  613. │ ┌──────────────────────┐ │
  614. │ │ Hello Word │ │
  615. │ │ │ │
  616. │ │ [◦ Ok ◦] │ │
  617. │ └──────────────────────┘ │
  618. │ │
  619. │ │
  620. │ │
  621. │ │
  622. └──────────────────────────────────────┘
  623. ", output);
  624. } else if (iterations == 2) {
  625. Assert.Null (Application.MouseGrabView);
  626. // Grab the mouse
  627. ReflectionTools.InvokePrivate (
  628. typeof (Application),
  629. "ProcessMouseEvent",
  630. new MouseEvent () {
  631. X = 8,
  632. Y = 5,
  633. Flags = MouseFlags.Button1Pressed
  634. });
  635. Assert.Equal (Application.Current, Application.MouseGrabView);
  636. Assert.Equal (new Rect (8, 5, 24, 5), Application.MouseGrabView.Frame);
  637. } else if (iterations == 3) {
  638. Assert.Equal (Application.Current, Application.MouseGrabView);
  639. // Drag to left
  640. ReflectionTools.InvokePrivate (
  641. typeof (Application),
  642. "ProcessMouseEvent",
  643. new MouseEvent () {
  644. X = 7,
  645. Y = 5,
  646. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  647. });
  648. Assert.Equal (Application.Current, Application.MouseGrabView);
  649. Assert.Equal (new Rect (7, 5, 24, 5), Application.MouseGrabView.Frame);
  650. } else if (iterations == 4) {
  651. Assert.Equal (Application.Current, Application.MouseGrabView);
  652. TestHelpers.AssertDriverContentsWithFrameAre (@"
  653. ┌──────────────────────────────────────┐
  654. │ │
  655. │ │
  656. │ │
  657. │ │
  658. │ ┌──────────────────────┐ │
  659. │ │ Hello Word │ │
  660. │ │ │ │
  661. │ │ [◦ Ok ◦] │ │
  662. │ └──────────────────────┘ │
  663. │ │
  664. │ │
  665. │ │
  666. │ │
  667. └──────────────────────────────────────┘", output);
  668. Assert.Equal (Application.Current, Application.MouseGrabView);
  669. } else if (iterations == 5) {
  670. Assert.Equal (Application.Current, Application.MouseGrabView);
  671. // Drag up
  672. ReflectionTools.InvokePrivate (
  673. typeof (Application),
  674. "ProcessMouseEvent",
  675. new MouseEvent () {
  676. X = 7,
  677. Y = 4,
  678. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  679. });
  680. Assert.Equal (Application.Current, Application.MouseGrabView);
  681. Assert.Equal (new Rect (7, 4, 24, 5), Application.MouseGrabView.Frame);
  682. } else if (iterations == 6) {
  683. Assert.Equal (Application.Current, Application.MouseGrabView);
  684. TestHelpers.AssertDriverContentsWithFrameAre (@"
  685. ┌──────────────────────────────────────┐
  686. │ │
  687. │ │
  688. │ │
  689. │ ┌──────────────────────┐ │
  690. │ │ Hello Word │ │
  691. │ │ │ │
  692. │ │ [◦ Ok ◦] │ │
  693. │ └──────────────────────┘ │
  694. │ │
  695. │ │
  696. │ │
  697. │ │
  698. │ │
  699. └──────────────────────────────────────┘", output);
  700. Assert.Equal (Application.Current, Application.MouseGrabView);
  701. Assert.Equal (new Rect (7, 4, 24, 5), Application.MouseGrabView.Frame);
  702. } else if (iterations == 7) {
  703. Assert.Equal (Application.Current, Application.MouseGrabView);
  704. // Ungrab the mouse
  705. ReflectionTools.InvokePrivate (
  706. typeof (Application),
  707. "ProcessMouseEvent",
  708. new MouseEvent () {
  709. X = 7,
  710. Y = 4,
  711. Flags = MouseFlags.Button1Released
  712. });
  713. Assert.Null (Application.MouseGrabView);
  714. } else if (iterations == 8) Application.RequestStop ();
  715. else if (iterations == 9) Application.RequestStop ();
  716. };
  717. Application.Run ();
  718. }
  719. [Fact, AutoInitShutdown]
  720. public void Mouse_Drag_On_Top_With_Superview_Not_Null ()
  721. {
  722. var win = new Window () {
  723. X = 3,
  724. Y = 2,
  725. Width = Dim.Fill (10),
  726. Height = Dim.Fill (5)
  727. };
  728. var top = Application.Top;
  729. top.Add (win);
  730. var iterations = -1;
  731. int movex = 0;
  732. int movey = 0;
  733. var location = new Rect (win.Frame.X, win.Frame.Y, 7, 3);
  734. Application.Iteration = () => {
  735. iterations++;
  736. if (iterations == 0) {
  737. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  738. Assert.Null (Application.MouseGrabView);
  739. // Grab the mouse
  740. ReflectionTools.InvokePrivate (
  741. typeof (Application),
  742. "ProcessMouseEvent",
  743. new MouseEvent () {
  744. X = win.Frame.X,
  745. Y = win.Frame.Y,
  746. Flags = MouseFlags.Button1Pressed
  747. });
  748. Assert.Equal (win, Application.MouseGrabView);
  749. Assert.Equal (location, Application.MouseGrabView.Frame);
  750. } else if (iterations == 1) {
  751. Assert.Equal (win, Application.MouseGrabView);
  752. // Drag to left
  753. movex = 1;
  754. movey = 0;
  755. ReflectionTools.InvokePrivate (
  756. typeof (Application),
  757. "ProcessMouseEvent",
  758. new MouseEvent () {
  759. X = win.Frame.X + movex,
  760. Y = win.Frame.Y + movey,
  761. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  762. });
  763. Assert.Equal (win, Application.MouseGrabView);
  764. } else if (iterations == 2) {
  765. // we should have moved +1, +0
  766. Assert.Equal (win, Application.MouseGrabView);
  767. Assert.Equal (win, Application.MouseGrabView);
  768. location.Offset (movex, movey);
  769. Assert.Equal (location, Application.MouseGrabView.Frame);
  770. } else if (iterations == 3) {
  771. Assert.Equal (win, Application.MouseGrabView);
  772. // Drag up
  773. movex = 0;
  774. movey = -1;
  775. ReflectionTools.InvokePrivate (
  776. typeof (Application),
  777. "ProcessMouseEvent",
  778. new MouseEvent () {
  779. X = win.Frame.X + movex,
  780. Y = win.Frame.Y + movey,
  781. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  782. });
  783. Assert.Equal (win, Application.MouseGrabView);
  784. } else if (iterations == 4) {
  785. // we should have moved +0, -1
  786. Assert.Equal (win, Application.MouseGrabView);
  787. location.Offset (movex, movey);
  788. Assert.Equal (location, Application.MouseGrabView.Frame);
  789. } else if (iterations == 5) {
  790. Assert.Equal (win, Application.MouseGrabView);
  791. // Ungrab the mouse
  792. movex = 0;
  793. movey = 0;
  794. ReflectionTools.InvokePrivate (
  795. typeof (Application),
  796. "ProcessMouseEvent",
  797. new MouseEvent () {
  798. X = win.Frame.X + movex,
  799. Y = win.Frame.Y + movey,
  800. Flags = MouseFlags.Button1Released
  801. });
  802. Assert.Null (Application.MouseGrabView);
  803. } else if (iterations == 8) {
  804. Application.RequestStop ();
  805. }
  806. };
  807. Application.Run ();
  808. }
  809. [Fact, AutoInitShutdown]
  810. public void EnsureVisibleBounds_With_Border_Null_Not_Throws ()
  811. {
  812. var top = new Toplevel ();
  813. Application.Begin (top);
  814. var exception = Record.Exception (() => ((FakeDriver)Application.Driver).SetBufferSize (0, 10));
  815. Assert.Null (exception);
  816. exception = Record.Exception (() => ((FakeDriver)Application.Driver).SetBufferSize (10, 0));
  817. Assert.Null (exception);
  818. }
  819. [Fact, AutoInitShutdown]
  820. public void OnEnter_OnLeave_Triggered_On_Application_Begin_End ()
  821. {
  822. var isEnter = false;
  823. var isLeave = false;
  824. var v = new View ();
  825. v.Enter += (s, _) => isEnter = true;
  826. v.Leave += (s, _) => isLeave = true;
  827. var top = Application.Top;
  828. top.Add (v);
  829. Assert.False (v.CanFocus);
  830. var exception = Record.Exception (() => top.OnEnter (top));
  831. Assert.Null (exception);
  832. exception = Record.Exception (() => top.OnLeave (top));
  833. Assert.Null (exception);
  834. v.CanFocus = true;
  835. Application.Begin (top);
  836. Assert.True (isEnter);
  837. Assert.False (isLeave);
  838. isEnter = false;
  839. var d = new Dialog ();
  840. var rs = Application.Begin (d);
  841. Assert.False (isEnter);
  842. Assert.True (isLeave);
  843. isLeave = false;
  844. Application.End (rs);
  845. Assert.True (isEnter);
  846. Assert.False (isLeave);
  847. }
  848. [Fact, AutoInitShutdown]
  849. public void PositionCursor_SetCursorVisibility_To_Invisible_If_Focused_Is_Null ()
  850. {
  851. var tf = new TextField ("test") { Width = 5 };
  852. var view = new View () { Width = 10, Height = 10 };
  853. view.Add (tf);
  854. Application.Top.Add (view);
  855. Application.Begin (Application.Top);
  856. Assert.True (tf.HasFocus);
  857. Application.Driver.GetCursorVisibility (out CursorVisibility cursor);
  858. Assert.Equal (CursorVisibility.Default, cursor);
  859. view.Enabled = false;
  860. Assert.False (tf.HasFocus);
  861. Application.Refresh ();
  862. Application.Driver.GetCursorVisibility (out cursor);
  863. Assert.Equal (CursorVisibility.Invisible, cursor);
  864. }
  865. [Fact, AutoInitShutdown]
  866. public void IsLoaded_Application_Begin ()
  867. {
  868. var top = Application.Top;
  869. Assert.False (top.IsLoaded);
  870. Application.Begin (top);
  871. Assert.True (top.IsLoaded);
  872. }
  873. [Fact, AutoInitShutdown]
  874. public void IsLoaded_With_Sub_Toplevel_Application_Begin_NeedDisplay ()
  875. {
  876. var top = Application.Top;
  877. var subTop = new Toplevel ();
  878. var view = new View (new Rect (0, 0, 20, 10));
  879. subTop.Add (view);
  880. top.Add (subTop);
  881. Assert.False (top.IsLoaded);
  882. Assert.False (subTop.IsLoaded);
  883. Assert.Equal (new Rect (0, 0, 20, 10), view.Frame);
  884. view.LayoutStarted += view_LayoutStarted;
  885. void view_LayoutStarted (object sender, LayoutEventArgs e)
  886. {
  887. Assert.Equal (new Rect (0, 0, 20, 10), view._needsDisplay);
  888. view.LayoutStarted -= view_LayoutStarted;
  889. }
  890. Application.Begin (top);
  891. Assert.True (top.IsLoaded);
  892. Assert.True (subTop.IsLoaded);
  893. Assert.Equal (new Rect (0, 0, 20, 10), view.Frame);
  894. view.Frame = new Rect (1, 3, 10, 5);
  895. Assert.Equal (new Rect (1, 3, 10, 5), view.Frame);
  896. Assert.Equal (new Rect (0, 0, 10, 5), view._needsDisplay);
  897. view.Redraw (view.Bounds);
  898. view.Frame = new Rect (1, 3, 10, 5);
  899. Assert.Equal (new Rect (1, 3, 10, 5), view.Frame);
  900. Assert.Equal (new Rect (0, 0, 10, 5), view._needsDisplay);
  901. }
  902. [Fact, AutoInitShutdown]
  903. public void Toplevel_Inside_ScrollView_MouseGrabView ()
  904. {
  905. var scrollView = new ScrollView () {
  906. X = 3,
  907. Y = 3,
  908. Width = 40,
  909. Height = 16,
  910. ContentSize = new Size (200, 100)
  911. };
  912. var win = new Window ("Window") { X = 3, Y = 3, Width = Dim.Fill (3), Height = Dim.Fill (3) };
  913. scrollView.Add (win);
  914. var top = Application.Top;
  915. top.Add (scrollView);
  916. Application.Begin (top);
  917. Assert.Equal (new Rect (0, 0, 80, 25), top.Frame);
  918. Assert.Equal (new Rect (3, 3, 40, 16), scrollView.Frame);
  919. Assert.Equal (new Rect (0, 0, 200, 100), scrollView.Subviews [0].Frame);
  920. Assert.Equal (new Rect (3, 3, 194, 94), win.Frame);
  921. TestHelpers.AssertDriverContentsWithFrameAre (@"
  922. ┌┤Window├───────────────────────────┴
  923. │ ░
  924. │ ░
  925. │ ░
  926. │ ░
  927. │ ░
  928. │ ░
  929. │ ░
  930. │ ░
  931. │ ░
  932. │ ░
  933. │ ▼
  934. ◄├──────┤░░░░░░░░░░░░░░░░░░░░░░░░░░░░░► ", output);
  935. ReflectionTools.InvokePrivate (
  936. typeof (Application),
  937. "ProcessMouseEvent",
  938. new MouseEvent () {
  939. X = 6,
  940. Y = 6,
  941. Flags = MouseFlags.Button1Pressed
  942. });
  943. Assert.Equal (win, Application.MouseGrabView);
  944. Assert.Equal (new Rect (3, 3, 194, 94), win.Frame);
  945. ReflectionTools.InvokePrivate (
  946. typeof (Application),
  947. "ProcessMouseEvent",
  948. new MouseEvent () {
  949. X = 9,
  950. Y = 9,
  951. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  952. });
  953. Assert.Equal (win, Application.MouseGrabView);
  954. top.SetNeedsLayout ();
  955. top.LayoutSubviews ();
  956. Assert.Equal (new Rect (6, 6, 191, 91), win.Frame);
  957. Application.Refresh ();
  958. TestHelpers.AssertDriverContentsWithFrameAre (@"
  959. ┌┤Window├────────────────────────░
  960. │ ░
  961. │ ░
  962. │ ░
  963. │ ░
  964. │ ░
  965. │ ░
  966. │ ░
  967. │ ▼
  968. ◄├──────┤░░░░░░░░░░░░░░░░░░░░░░░░░░░░░► ", output);
  969. ReflectionTools.InvokePrivate (
  970. typeof (Application),
  971. "ProcessMouseEvent",
  972. new MouseEvent () {
  973. X = 5,
  974. Y = 5,
  975. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  976. });
  977. Assert.Equal (win, Application.MouseGrabView);
  978. top.SetNeedsLayout ();
  979. top.LayoutSubviews ();
  980. Assert.Equal (new Rect (2, 2, 195, 95), win.Frame);
  981. Application.Refresh ();
  982. TestHelpers.AssertDriverContentsWithFrameAre (@"
  983. ┌┤Window├────────────────────────────│
  984. │ ┴
  985. │ ░
  986. │ ░
  987. │ ░
  988. │ ░
  989. │ ░
  990. │ ░
  991. │ ░
  992. │ ░
  993. │ ░
  994. │ ░
  995. │ ▼
  996. ◄├──────┤░░░░░░░░░░░░░░░░░░░░░░░░░░░░░► ", output);
  997. ReflectionTools.InvokePrivate (
  998. typeof (Application),
  999. "ProcessMouseEvent",
  1000. new MouseEvent () {
  1001. X = 5,
  1002. Y = 5,
  1003. Flags = MouseFlags.Button1Released
  1004. });
  1005. Assert.Null (Application.MouseGrabView);
  1006. ReflectionTools.InvokePrivate (
  1007. typeof (Application),
  1008. "ProcessMouseEvent",
  1009. new MouseEvent () {
  1010. X = 4,
  1011. Y = 4,
  1012. Flags = MouseFlags.ReportMousePosition
  1013. });
  1014. Assert.Equal (scrollView, Application.MouseGrabView);
  1015. }
  1016. [Fact, AutoInitShutdown]
  1017. public void Dialog_Bounds_Bigger_Than_Driver_Cols_And_Rows_Allow_Drag_Beyond_Left_Right_And_Bottom ()
  1018. {
  1019. var top = Application.Top;
  1020. var dialog = new Dialog ("", 20, 3, new Button ("Ok"));
  1021. Application.Begin (top);
  1022. ((FakeDriver)Application.Driver).SetBufferSize (40, 10);
  1023. Application.Begin (dialog);
  1024. Application.Refresh ();
  1025. Assert.Equal (new Rect (0, 0, 40, 10), top.Frame);
  1026. Assert.Equal (new Rect (10, 3, 20, 3), dialog.Frame);
  1027. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1028. ┌──────────────────┐
  1029. │ [ Ok ] │
  1030. └──────────────────┘
  1031. ", output);
  1032. Assert.Null (Application.MouseGrabView);
  1033. ReflectionTools.InvokePrivate (
  1034. typeof (Application),
  1035. "ProcessMouseEvent",
  1036. new MouseEvent () {
  1037. X = 10,
  1038. Y = 3,
  1039. Flags = MouseFlags.Button1Pressed
  1040. });
  1041. Assert.Equal (dialog, Application.MouseGrabView);
  1042. ReflectionTools.InvokePrivate (
  1043. typeof (Application),
  1044. "ProcessMouseEvent",
  1045. new MouseEvent () {
  1046. X = -11,
  1047. Y = -4,
  1048. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1049. });
  1050. Application.Refresh ();
  1051. Assert.Equal (new Rect (0, 0, 40, 10), top.Frame);
  1052. Assert.Equal (new Rect (0, 0, 20, 3), dialog.Frame);
  1053. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1054. ┌──────────────────┐
  1055. │ [ Ok ] │
  1056. └──────────────────┘
  1057. ", output);
  1058. // Changes Top size to same size as Dialog more menu and scroll bar
  1059. ((FakeDriver)Application.Driver).SetBufferSize (20, 3);
  1060. ReflectionTools.InvokePrivate (
  1061. typeof (Application),
  1062. "ProcessMouseEvent",
  1063. new MouseEvent () {
  1064. X = -1,
  1065. Y = -1,
  1066. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1067. });
  1068. Application.Refresh ();
  1069. Assert.Equal (new Rect (0, 0, 20, 3), top.Frame);
  1070. Assert.Equal (new Rect (0, 0, 20, 3), dialog.Frame);
  1071. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1072. ┌──────────────────┐
  1073. │ [ Ok ] │
  1074. └──────────────────┘
  1075. ", output);
  1076. // Changes Top size smaller than Dialog size
  1077. ((FakeDriver)Application.Driver).SetBufferSize (19, 2);
  1078. ReflectionTools.InvokePrivate (
  1079. typeof (Application),
  1080. "ProcessMouseEvent",
  1081. new MouseEvent () {
  1082. X = -1,
  1083. Y = -1,
  1084. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1085. });
  1086. Application.Refresh ();
  1087. Assert.Equal (new Rect (0, 0, 19, 2), top.Frame);
  1088. Assert.Equal (new Rect (-1, 0, 20, 3), dialog.Frame);
  1089. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1090. ──────────────────┐
  1091. [ Ok ] │
  1092. ", output);
  1093. ReflectionTools.InvokePrivate (
  1094. typeof (Application),
  1095. "ProcessMouseEvent",
  1096. new MouseEvent () {
  1097. X = 18,
  1098. Y = 1,
  1099. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1100. });
  1101. Application.Refresh ();
  1102. Assert.Equal (new Rect (0, 0, 19, 2), top.Frame);
  1103. Assert.Equal (new Rect (18, 1, 20, 3), dialog.Frame);
  1104. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1105. ┌", output);
  1106. // On a real app we can't go beyond the SuperView bounds
  1107. ReflectionTools.InvokePrivate (
  1108. typeof (Application),
  1109. "ProcessMouseEvent",
  1110. new MouseEvent () {
  1111. X = 19,
  1112. Y = 2,
  1113. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1114. });
  1115. Application.Refresh ();
  1116. Assert.Equal (new Rect (0, 0, 19, 2), top.Frame);
  1117. Assert.Equal (new Rect (19, 2, 20, 3), dialog.Frame);
  1118. TestHelpers.AssertDriverContentsWithFrameAre (@"", output);
  1119. }
  1120. [Fact, AutoInitShutdown]
  1121. public void Single_Smaller_Top_Will_Have_Cleaning_Trails_Chunk_On_Move ()
  1122. {
  1123. var dialog = new Dialog ("Single smaller Dialog") { Width = 30, Height = 10 };
  1124. dialog.Add (new Label (
  1125. "How should I've to react. Cleaning all chunk trails or setting the 'Cols' and 'Rows' to this dialog length?\n" +
  1126. "Cleaning is more easy to fix this.") {
  1127. X = Pos.Center (),
  1128. Y = Pos.Center (),
  1129. Width = Dim.Fill (),
  1130. Height = Dim.Fill (),
  1131. TextAlignment = TextAlignment.Centered,
  1132. VerticalTextAlignment = VerticalTextAlignment.Middle,
  1133. AutoSize = false
  1134. });
  1135. var rs = Application.Begin (dialog);
  1136. Assert.Null (Application.MouseGrabView);
  1137. Assert.Equal (new Rect (25, 7, 30, 10), dialog.Frame);
  1138. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1139. ┌┤Single smaller Dialog├─────┐
  1140. │ How should I've to react. │
  1141. │Cleaning all chunk trails or│
  1142. │ setting the 'Cols' and │
  1143. │ 'Rows' to this dialog │
  1144. │ length? │
  1145. │Cleaning is more easy to fix│
  1146. │ this. │
  1147. │ │
  1148. └────────────────────────────┘", output);
  1149. ReflectionTools.InvokePrivate (
  1150. typeof (Application),
  1151. "ProcessMouseEvent",
  1152. new MouseEvent () {
  1153. X = 25,
  1154. Y = 7,
  1155. Flags = MouseFlags.Button1Pressed
  1156. });
  1157. var firstIteration = false;
  1158. Application.RunMainLoopIteration (ref rs, true, ref firstIteration);
  1159. Assert.Equal (dialog, Application.MouseGrabView);
  1160. Assert.Equal (new Rect (25, 7, 30, 10), dialog.Frame);
  1161. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1162. ┌┤Single smaller Dialog├─────┐
  1163. │ How should I've to react. │
  1164. │Cleaning all chunk trails or│
  1165. │ setting the 'Cols' and │
  1166. │ 'Rows' to this dialog │
  1167. │ length? │
  1168. │Cleaning is more easy to fix│
  1169. │ this. │
  1170. │ │
  1171. └────────────────────────────┘", output);
  1172. ReflectionTools.InvokePrivate (
  1173. typeof (Application),
  1174. "ProcessMouseEvent",
  1175. new MouseEvent () {
  1176. X = 20,
  1177. Y = 10,
  1178. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1179. });
  1180. firstIteration = false;
  1181. Application.RunMainLoopIteration (ref rs, true, ref firstIteration);
  1182. Assert.Equal (dialog, Application.MouseGrabView);
  1183. Assert.Equal (new Rect (20, 10, 30, 10), dialog.Frame);
  1184. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1185. ┌┤Single smaller Dialog├─────┐
  1186. │ How should I've to react. │
  1187. │Cleaning all chunk trails or│
  1188. │ setting the 'Cols' and │
  1189. │ 'Rows' to this dialog │
  1190. │ length? │
  1191. │Cleaning is more easy to fix│
  1192. │ this. │
  1193. │ │
  1194. └────────────────────────────┘", output);
  1195. Application.End (rs);
  1196. }
  1197. [Fact, AutoInitShutdown]
  1198. public void Draw_A_Top_Subview_On_A_Dialog ()
  1199. {
  1200. var top = Application.Top;
  1201. var win = new Window ();
  1202. top.Add (win);
  1203. Application.Begin (top);
  1204. ((FakeDriver)Application.Driver).SetBufferSize (20, 20);
  1205. Assert.Equal (new Rect (0, 0, 20, 20), win.Frame);
  1206. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1207. ┌──────────────────┐
  1208. │ │
  1209. │ │
  1210. │ │
  1211. │ │
  1212. │ │
  1213. │ │
  1214. │ │
  1215. │ │
  1216. │ │
  1217. │ │
  1218. │ │
  1219. │ │
  1220. │ │
  1221. │ │
  1222. │ │
  1223. │ │
  1224. │ │
  1225. │ │
  1226. └──────────────────┘", output);
  1227. var btnPopup = new Button ("Popup");
  1228. btnPopup.Clicked += (s, e) => {
  1229. var viewToScreen = btnPopup.ViewToScreen (top.Frame);
  1230. var view = new View () {
  1231. X = 1,
  1232. Y = viewToScreen.Y + 1,
  1233. Width = 18,
  1234. Height = 5,
  1235. Border = new Border () { BorderStyle = BorderStyle.Single }
  1236. };
  1237. Application.Current.DrawContentComplete += Current_DrawContentComplete;
  1238. top.Add (view);
  1239. void Current_DrawContentComplete (object sender, DrawEventArgs e)
  1240. {
  1241. Assert.Equal (new Rect (1, 14, 18, 5), view.Frame);
  1242. var savedClip = Application.Driver.Clip;
  1243. Application.Driver.Clip = top.Frame;
  1244. view.Redraw (view.Bounds);
  1245. top.Move (2, 15);
  1246. View.Driver.AddStr ("One");
  1247. top.Move (2, 16);
  1248. View.Driver.AddStr ("Two");
  1249. top.Move (2, 17);
  1250. View.Driver.AddStr ("Three");
  1251. Application.Driver.Clip = savedClip;
  1252. Application.Current.DrawContentComplete -= Current_DrawContentComplete;
  1253. }
  1254. };
  1255. var dialog = new Dialog ("", 15, 10, btnPopup);
  1256. var rs = Application.Begin (dialog);
  1257. Assert.Equal (new Rect (2, 5, 15, 10), dialog.Frame);
  1258. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1259. ┌──────────────────┐
  1260. │ │
  1261. │ │
  1262. │ │
  1263. │ │
  1264. │ ┌─────────────┐ │
  1265. │ │ │ │
  1266. │ │ │ │
  1267. │ │ │ │
  1268. │ │ │ │
  1269. │ │ │ │
  1270. │ │ │ │
  1271. │ │ │ │
  1272. │ │ [ Popup ] │ │
  1273. │ └─────────────┘ │
  1274. │ │
  1275. │ │
  1276. │ │
  1277. │ │
  1278. └──────────────────┘", output);
  1279. ReflectionTools.InvokePrivate (
  1280. typeof (Application),
  1281. "ProcessMouseEvent",
  1282. new MouseEvent () {
  1283. X = 9,
  1284. Y = 13,
  1285. Flags = MouseFlags.Button1Clicked
  1286. });
  1287. var firstIteration = false;
  1288. Application.RunMainLoopIteration (ref rs, true, ref firstIteration);
  1289. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1290. ┌──────────────────┐
  1291. │ │
  1292. │ │
  1293. │ │
  1294. │ │
  1295. │ ┌─────────────┐ │
  1296. │ │ │ │
  1297. │ │ │ │
  1298. │ │ │ │
  1299. │ │ │ │
  1300. │ │ │ │
  1301. │ │ │ │
  1302. │ │ │ │
  1303. │ │ [ Popup ] │ │
  1304. │┌────────────────┐│
  1305. ││One ││
  1306. ││Two ││
  1307. ││Three ││
  1308. │└────────────────┘│
  1309. └──────────────────┘", output);
  1310. Application.End (rs);
  1311. }
  1312. }
  1313. }