ToplevelTests.cs 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453
  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._needsDisplay);
  904. view.LayoutStarted += view_LayoutStarted;
  905. void view_LayoutStarted (object sender, LayoutEventArgs e)
  906. {
  907. Assert.Equal (new Rect (0, 0, 20, 10), view._needsDisplay);
  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._needsDisplay);
  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._needsDisplay);
  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. [Fact, AutoInitShutdown]
  1156. public void Single_Smaller_Top_Will_Have_Cleaning_Trails_Chunk_On_Move ()
  1157. {
  1158. var dialog = new Dialog ("Single smaller Dialog") { Width = 30, Height = 10 };
  1159. dialog.Add (new Label (
  1160. "How should I've to react. Cleaning all chunk trails or setting the 'Cols' and 'Rows' to this dialog length?\n" +
  1161. "Cleaning is more easy to fix this.") {
  1162. X = Pos.Center (),
  1163. Y = Pos.Center (),
  1164. Width = Dim.Fill (),
  1165. Height = Dim.Fill (),
  1166. TextAlignment = TextAlignment.Centered,
  1167. VerticalTextAlignment = VerticalTextAlignment.Middle,
  1168. AutoSize = false
  1169. });
  1170. var rs = Application.Begin (dialog);
  1171. Assert.Null (Application.MouseGrabView);
  1172. Assert.Equal (new Rect (25, 7, 30, 10), dialog.Frame);
  1173. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1174. ┌┤Single smaller Dialog├─────┐
  1175. │ How should I've to react. │
  1176. │Cleaning all chunk trails or│
  1177. │ setting the 'Cols' and │
  1178. │ 'Rows' to this dialog │
  1179. │ length? │
  1180. │Cleaning is more easy to fix│
  1181. │ this. │
  1182. │ │
  1183. └────────────────────────────┘", output);
  1184. ReflectionTools.InvokePrivate (
  1185. typeof (Application),
  1186. "ProcessMouseEvent",
  1187. new MouseEvent () {
  1188. X = 25,
  1189. Y = 7,
  1190. Flags = MouseFlags.Button1Pressed
  1191. });
  1192. var firstIteration = false;
  1193. Application.RunMainLoopIteration (ref rs, true, ref firstIteration); Assert.Equal (dialog, Application.MouseGrabView);
  1194. Assert.Equal (new Rect (25, 7, 30, 10), dialog.Frame);
  1195. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1196. ┌┤Single smaller Dialog├─────┐
  1197. │ How should I've to react. │
  1198. │Cleaning all chunk trails or│
  1199. │ setting the 'Cols' and │
  1200. │ 'Rows' to this dialog │
  1201. │ length? │
  1202. │Cleaning is more easy to fix│
  1203. │ this. │
  1204. │ │
  1205. └────────────────────────────┘", output);
  1206. ReflectionTools.InvokePrivate (
  1207. typeof (Application),
  1208. "ProcessMouseEvent",
  1209. new MouseEvent () {
  1210. X = 20,
  1211. Y = 10,
  1212. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1213. });
  1214. firstIteration = false;
  1215. Application.RunMainLoopIteration (ref rs, true, ref firstIteration);
  1216. Assert.Equal (dialog, Application.MouseGrabView);
  1217. Assert.Equal (new Rect (20, 10, 30, 10), dialog.Frame);
  1218. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1219. ┌┤Single smaller Dialog├─────┐
  1220. │ How should I've to react. │
  1221. │Cleaning all chunk trails or│
  1222. │ setting the 'Cols' and │
  1223. │ 'Rows' to this dialog │
  1224. │ length? │
  1225. │Cleaning is more easy to fix│
  1226. │ this. │
  1227. │ │
  1228. └────────────────────────────┘", output);
  1229. Application.End (rs);
  1230. }
  1231. }
  1232. }