ToplevelTests.cs 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456
  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. 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") { Width = Dim.Percent (50f), Height = Dim.Fill () };
  292. var lblTf1W1 = new Label ("Enter text in TextField on Win1:");
  293. var tf1W1 = new TextField ("Text1 on Win1") { X = Pos.Right (lblTf1W1) + 1, Width = Dim.Fill () };
  294. var lblTvW1 = new Label ("Enter text in TextView on Win1:") { Y = Pos.Bottom (lblTf1W1) + 1 };
  295. var tvW1 = new TextView () { 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:") { Y = Pos.Bottom (lblTvW1) + 1 };
  297. var tf2W1 = new TextField ("Text2 on Win1") { X = Pos.Left (tf1W1), Width = Dim.Fill () };
  298. win1.Add (lblTf1W1, tf1W1, lblTvW1, tvW1, lblTf2W1, tf2W1);
  299. var win2 = new Window ("Win2") { X = Pos.Right (win1) + 1, Width = Dim.Percent (50f), Height = Dim.Fill () };
  300. var lblTf1W2 = new Label ("Enter text in TextField on Win2:");
  301. var tf1W2 = new TextField ("Text1 on Win2") { X = Pos.Right (lblTf1W2) + 1, Width = Dim.Fill () };
  302. var lblTvW2 = new Label ("Enter text in TextView on Win2:") { Y = Pos.Bottom (lblTf1W2) + 1 };
  303. var tvW2 = new TextView () { 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:") { Y = Pos.Bottom (lblTvW2) + 1 };
  305. var tf2W2 = new TextField ("Text2 on Win2") { 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 (win1.Subviews [0], win1.Focused);
  425. Assert.Equal (tf1W1, win1.MostFocused);
  426. Assert.True (win1.IsMdiChild);
  427. Assert.Single (Application.MdiChildes);
  428. Application.Begin (win2);
  429. Assert.Equal (new Rect (0, 0, 40, 25), win2.Frame);
  430. Assert.NotEqual (top, Application.Current);
  431. Assert.False (top.IsCurrentTop);
  432. Assert.Equal (win2, Application.Current);
  433. Assert.True (win2.IsCurrentTop);
  434. Assert.True (win2.IsMdiChild);
  435. Assert.Null (top.Focused);
  436. Assert.Null (top.MostFocused);
  437. Assert.Equal (win2.Subviews [0], win2.Focused);
  438. Assert.Equal (tf1W2, win2.MostFocused);
  439. Assert.Equal (2, Application.MdiChildes.Count);
  440. Application.ShowChild (win1);
  441. Assert.Equal (win1, Application.Current);
  442. Assert.Equal (win1, Application.MdiChildes [0]);
  443. win1.Running = true;
  444. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Application.QuitKey, new KeyModifiers ())));
  445. Assert.False (isRunning);
  446. Assert.False (win1.Running);
  447. Assert.Equal (win1, Application.MdiChildes [0]);
  448. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Z | Key.CtrlMask, new KeyModifiers ())));
  449. Assert.False (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.F5, new KeyModifiers ())));
  450. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  451. Assert.True (win1.IsCurrentTop);
  452. Assert.Equal (tvW1, win1.MostFocused);
  453. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  454. Assert.Equal ($"\tFirst line Win1{Environment.NewLine}Second line Win1", tvW1.Text);
  455. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ())));
  456. Assert.Equal ($"First line Win1{Environment.NewLine}Second line Win1", tvW1.Text);
  457. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers ())));
  458. Assert.Equal (win1, Application.MdiChildes [0]);
  459. Assert.Equal (tf2W1, win1.MostFocused);
  460. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  461. Assert.Equal (win1, Application.MdiChildes [0]);
  462. Assert.Equal (tf1W1, win1.MostFocused);
  463. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  464. Assert.Equal (win1, Application.MdiChildes [0]);
  465. Assert.Equal (tf1W1, win1.MostFocused);
  466. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  467. Assert.Equal (win1, Application.MdiChildes [0]);
  468. Assert.Equal (tvW1, win1.MostFocused);
  469. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.I | Key.CtrlMask, new KeyModifiers ())));
  470. Assert.Equal (win1, Application.MdiChildes [0]);
  471. Assert.Equal (tf2W1, win1.MostFocused);
  472. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ())));
  473. Assert.Equal (win1, Application.MdiChildes [0]);
  474. Assert.Equal (tvW1, win1.MostFocused);
  475. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ())));
  476. Assert.Equal (win1, Application.MdiChildes [0]);
  477. Assert.Equal (tf1W1, win1.MostFocused);
  478. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ())));
  479. Assert.Equal (win1, Application.MdiChildes [0]);
  480. Assert.Equal (tf2W1, win1.MostFocused);
  481. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  482. Assert.Equal (win1, Application.MdiChildes [0]);
  483. Assert.Equal (tf1W1, win1.MostFocused);
  484. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers ())));
  485. Assert.Equal (win2, Application.MdiChildes [0]);
  486. Assert.Equal (tf1W2, win2.MostFocused);
  487. tf2W2.SetFocus ();
  488. Assert.True (tf2W2.HasFocus);
  489. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask | Key.ShiftMask, new KeyModifiers ())));
  490. Assert.Equal (win1, Application.MdiChildes [0]);
  491. Assert.Equal (tf1W1, win1.MostFocused);
  492. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Application.AlternateForwardKey, new KeyModifiers ())));
  493. Assert.Equal (win2, Application.MdiChildes [0]);
  494. Assert.Equal (tf2W2, win2.MostFocused);
  495. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Application.AlternateBackwardKey, new KeyModifiers ())));
  496. Assert.Equal (win1, Application.MdiChildes [0]);
  497. Assert.Equal (tf1W1, win1.MostFocused);
  498. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  499. Assert.Equal (win1, Application.MdiChildes [0]);
  500. Assert.Equal (tvW1, win1.MostFocused);
  501. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.B | Key.CtrlMask, new KeyModifiers ())));
  502. Assert.Equal (win1, Application.MdiChildes [0]);
  503. Assert.Equal (tf1W1, win1.MostFocused);
  504. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  505. Assert.Equal (win1, Application.MdiChildes [0]);
  506. Assert.Equal (tvW1, win1.MostFocused);
  507. Assert.Equal (new Point (0, 0), tvW1.CursorPosition);
  508. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.End | Key.CtrlMask, new KeyModifiers ())));
  509. Assert.Equal (win1, Application.MdiChildes [0]);
  510. Assert.Equal (tvW1, win1.MostFocused);
  511. Assert.Equal (new Point (16, 1), tvW1.CursorPosition);
  512. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.F | Key.CtrlMask, new KeyModifiers ())));
  513. Assert.Equal (win1, Application.MdiChildes [0]);
  514. Assert.Equal (tf2W1, win1.MostFocused);
  515. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.L | Key.CtrlMask, new KeyModifiers ())));
  516. }
  517. [Fact]
  518. public void Added_Event_Should_Not_Be_Used_To_Initialize_Toplevel_Events ()
  519. {
  520. Key alternateForwardKey = default;
  521. Key alternateBackwardKey = default;
  522. Key quitKey = default;
  523. var wasAdded = false;
  524. var view = new View ();
  525. view.Added += View_Added;
  526. void View_Added (object sender, SuperViewChangedEventArgs e)
  527. {
  528. Assert.Throws<NullReferenceException> (() => Application.Top.AlternateForwardKeyChanged += (s, e) => alternateForwardKey = e.OldKey);
  529. Assert.Throws<NullReferenceException> (() => Application.Top.AlternateBackwardKeyChanged += (s, e) => alternateBackwardKey = e.OldKey);
  530. Assert.Throws<NullReferenceException> (() => Application.Top.QuitKeyChanged += (s, e) => quitKey = e.OldKey);
  531. Assert.False (wasAdded);
  532. wasAdded = true;
  533. view.Added -= View_Added;
  534. }
  535. var win = new Window ();
  536. win.Add (view);
  537. Application.Init (new FakeDriver ());
  538. var top = Application.Top;
  539. top.Add (win);
  540. Assert.True (wasAdded);
  541. Application.Shutdown ();
  542. }
  543. [Fact]
  544. [AutoInitShutdown]
  545. public void AlternateForwardKeyChanged_AlternateBackwardKeyChanged_QuitKeyChanged_Events ()
  546. {
  547. Key alternateForwardKey = default;
  548. Key alternateBackwardKey = default;
  549. Key quitKey = default;
  550. var view = new View ();
  551. view.Initialized += View_Initialized;
  552. void View_Initialized (object sender, EventArgs e)
  553. {
  554. Application.Top.AlternateForwardKeyChanged += (s, e) => alternateForwardKey = e.OldKey;
  555. Application.Top.AlternateBackwardKeyChanged += (s, e) => alternateBackwardKey = e.OldKey;
  556. Application.Top.QuitKeyChanged += (s, e) => quitKey = e.OldKey;
  557. }
  558. var win = new Window ();
  559. win.Add (view);
  560. var top = Application.Top;
  561. top.Add (win);
  562. Application.Begin (top);
  563. Assert.Equal (Key.Null, alternateForwardKey);
  564. Assert.Equal (Key.Null, alternateBackwardKey);
  565. Assert.Equal (Key.Null, quitKey);
  566. Assert.Equal (Key.PageDown | Key.CtrlMask, Application.AlternateForwardKey);
  567. Assert.Equal (Key.PageUp | Key.CtrlMask, Application.AlternateBackwardKey);
  568. Assert.Equal (Key.Q | Key.CtrlMask, Application.QuitKey);
  569. Application.AlternateForwardKey = Key.A;
  570. Application.AlternateBackwardKey = Key.B;
  571. Application.QuitKey = Key.C;
  572. Assert.Equal (Key.PageDown | Key.CtrlMask, alternateForwardKey);
  573. Assert.Equal (Key.PageUp | Key.CtrlMask, alternateBackwardKey);
  574. Assert.Equal (Key.Q | Key.CtrlMask, quitKey);
  575. Assert.Equal (Key.A, Application.AlternateForwardKey);
  576. Assert.Equal (Key.B, Application.AlternateBackwardKey);
  577. Assert.Equal (Key.C, Application.QuitKey);
  578. // Replacing the defaults keys to avoid errors on others unit tests that are using it.
  579. Application.AlternateForwardKey = Key.PageDown | Key.CtrlMask;
  580. Application.AlternateBackwardKey = Key.PageUp | Key.CtrlMask;
  581. Application.QuitKey = Key.Q | Key.CtrlMask;
  582. Assert.Equal (Key.PageDown | Key.CtrlMask, Application.AlternateForwardKey);
  583. Assert.Equal (Key.PageUp | Key.CtrlMask, Application.AlternateBackwardKey);
  584. Assert.Equal (Key.Q | Key.CtrlMask, Application.QuitKey);
  585. }
  586. [Fact]
  587. [AutoInitShutdown]
  588. public void FileDialog_FileSystemWatcher ()
  589. {
  590. for (int i = 0; i < 8; i++) {
  591. var fd = new FileDialog ();
  592. fd.Ready += (s, e) => Application.RequestStop ();
  593. Application.Run (fd);
  594. }
  595. }
  596. [Fact, AutoInitShutdown]
  597. public void Mouse_Drag_On_Top_With_Superview_Null ()
  598. {
  599. var menu = new MenuBar (new MenuBarItem [] {
  600. new MenuBarItem("File", new MenuItem [] {
  601. new MenuItem("New", "", null)
  602. })
  603. });
  604. var sbar = new StatusBar (new StatusItem [] {
  605. new StatusItem(Key.N, "~CTRL-N~ New", null)
  606. });
  607. var win = new Window ("Window");
  608. var top = Application.Top;
  609. top.Add (menu, sbar, win);
  610. var iterations = -1;
  611. Application.Iteration = () => {
  612. iterations++;
  613. if (iterations == 0) {
  614. ((FakeDriver)Application.Driver).SetBufferSize (40, 15);
  615. MessageBox.Query ("About", "Hello Word", "Ok");
  616. } else if (iterations == 1) TestHelpers.AssertDriverContentsWithFrameAre (@"
  617. File
  618. ┌┤Window├──────────────────────────────┐
  619. │ │
  620. │ │
  621. │ │
  622. │ ┌┤About├───────────────┐ │
  623. │ │ Hello Word │ │
  624. │ │ │ │
  625. │ │ [◦ Ok ◦] │ │
  626. │ └──────────────────────┘ │
  627. │ │
  628. │ │
  629. │ │
  630. └──────────────────────────────────────┘
  631. CTRL-N New ", output);
  632. else if (iterations == 2) {
  633. Assert.Null (Application.MouseGrabView);
  634. // Grab the mouse
  635. ReflectionTools.InvokePrivate (
  636. typeof (Application),
  637. "ProcessMouseEvent",
  638. new MouseEvent () {
  639. X = 8,
  640. Y = 5,
  641. Flags = MouseFlags.Button1Pressed
  642. });
  643. Assert.Equal (Application.Current, Application.MouseGrabView);
  644. Assert.Equal (new Rect (8, 5, 24, 5), Application.MouseGrabView.Frame);
  645. } else if (iterations == 3) {
  646. Assert.Equal (Application.Current, Application.MouseGrabView);
  647. // Grab to left
  648. ReflectionTools.InvokePrivate (
  649. typeof (Application),
  650. "ProcessMouseEvent",
  651. new MouseEvent () {
  652. X = 7,
  653. Y = 5,
  654. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  655. });
  656. Assert.Equal (Application.Current, Application.MouseGrabView);
  657. Assert.Equal (new Rect (7, 5, 24, 5), Application.MouseGrabView.Frame);
  658. } else if (iterations == 4) {
  659. Assert.Equal (Application.Current, Application.MouseGrabView);
  660. TestHelpers.AssertDriverContentsWithFrameAre (@"
  661. File
  662. ┌┤Window├──────────────────────────────┐
  663. │ │
  664. │ │
  665. │ │
  666. │ ┌┤About├───────────────┐ │
  667. │ │ Hello Word │ │
  668. │ │ │ │
  669. │ │ [◦ Ok ◦] │ │
  670. │ └──────────────────────┘ │
  671. │ │
  672. │ │
  673. │ │
  674. └──────────────────────────────────────┘
  675. CTRL-N New ", output);
  676. Assert.Equal (Application.Current, Application.MouseGrabView);
  677. } else if (iterations == 5) {
  678. Assert.Equal (Application.Current, Application.MouseGrabView);
  679. // Grab to top
  680. ReflectionTools.InvokePrivate (
  681. typeof (Application),
  682. "ProcessMouseEvent",
  683. new MouseEvent () {
  684. X = 7,
  685. Y = 4,
  686. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  687. });
  688. Assert.Equal (Application.Current, Application.MouseGrabView);
  689. Assert.Equal (new Rect (7, 4, 24, 5), Application.MouseGrabView.Frame);
  690. } else if (iterations == 6) {
  691. Assert.Equal (Application.Current, Application.MouseGrabView);
  692. TestHelpers.AssertDriverContentsWithFrameAre (@"
  693. File
  694. ┌┤Window├──────────────────────────────┐
  695. │ │
  696. │ │
  697. │ ┌┤About├───────────────┐ │
  698. │ │ Hello Word │ │
  699. │ │ │ │
  700. │ │ [◦ Ok ◦] │ │
  701. │ └──────────────────────┘ │
  702. │ │
  703. │ │
  704. │ │
  705. │ │
  706. └──────────────────────────────────────┘
  707. CTRL-N New ", output);
  708. Assert.Equal (Application.Current, Application.MouseGrabView);
  709. Assert.Equal (new Rect (7, 4, 24, 5), Application.MouseGrabView.Frame);
  710. } else if (iterations == 7) {
  711. Assert.Equal (Application.Current, Application.MouseGrabView);
  712. // Ungrab the mouse
  713. ReflectionTools.InvokePrivate (
  714. typeof (Application),
  715. "ProcessMouseEvent",
  716. new MouseEvent () {
  717. X = 7,
  718. Y = 4,
  719. Flags = MouseFlags.Button1Released
  720. });
  721. Assert.Null (Application.MouseGrabView);
  722. } else if (iterations == 8) Application.RequestStop ();
  723. else if (iterations == 9) Application.RequestStop ();
  724. };
  725. Application.Run ();
  726. }
  727. [Fact, AutoInitShutdown]
  728. public void Mouse_Drag_On_Top_With_Superview_Not_Null ()
  729. {
  730. var menu = new MenuBar (new MenuBarItem [] {
  731. new MenuBarItem("File", new MenuItem [] {
  732. new MenuItem("New", "", null)
  733. })
  734. });
  735. var sbar = new StatusBar (new StatusItem [] {
  736. new StatusItem(Key.N, "~CTRL-N~ New", null)
  737. });
  738. var win = new Window ("Window") {
  739. X = 3,
  740. Y = 2,
  741. Width = Dim.Fill (10),
  742. Height = Dim.Fill (5)
  743. };
  744. var top = Application.Top;
  745. top.Add (menu, sbar, win);
  746. var iterations = -1;
  747. Application.Iteration = () => {
  748. iterations++;
  749. if (iterations == 0) {
  750. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  751. Assert.Null (Application.MouseGrabView);
  752. // Grab the mouse
  753. ReflectionTools.InvokePrivate (
  754. typeof (Application),
  755. "ProcessMouseEvent",
  756. new MouseEvent () {
  757. X = 4,
  758. Y = 2,
  759. Flags = MouseFlags.Button1Pressed
  760. });
  761. Assert.Equal (win, Application.MouseGrabView);
  762. Assert.Equal (new Rect (3, 2, 7, 3), Application.MouseGrabView.Frame);
  763. TestHelpers.AssertDriverContentsWithFrameAre (@"
  764. File
  765. ┌─────┐
  766. │ │
  767. └─────┘
  768. CTRL-N New", output);
  769. } else if (iterations == 1) {
  770. Assert.Equal (win, Application.MouseGrabView);
  771. // Grab to left
  772. ReflectionTools.InvokePrivate (
  773. typeof (Application),
  774. "ProcessMouseEvent",
  775. new MouseEvent () {
  776. X = 5,
  777. Y = 2,
  778. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  779. });
  780. Assert.Equal (win, Application.MouseGrabView);
  781. } else if (iterations == 2) {
  782. Assert.Equal (win, Application.MouseGrabView);
  783. TestHelpers.AssertDriverContentsWithFrameAre (@"
  784. File
  785. ┌────┐
  786. │ │
  787. └────┘
  788. CTRL-N New", output);
  789. Assert.Equal (win, Application.MouseGrabView);
  790. Assert.Equal (new Rect (4, 2, 6, 3), Application.MouseGrabView.Frame);
  791. } else if (iterations == 3) {
  792. Assert.Equal (win, Application.MouseGrabView);
  793. // Grab to top
  794. ReflectionTools.InvokePrivate (
  795. typeof (Application),
  796. "ProcessMouseEvent",
  797. new MouseEvent () {
  798. X = 5,
  799. Y = 1,
  800. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  801. });
  802. Assert.Equal (win, Application.MouseGrabView);
  803. } else if (iterations == 4) {
  804. Assert.Equal (win, Application.MouseGrabView);
  805. TestHelpers.AssertDriverContentsWithFrameAre (@"
  806. File
  807. ┌────┐
  808. │ │
  809. │ │
  810. └────┘
  811. CTRL-N New", output);
  812. Assert.Equal (win, Application.MouseGrabView);
  813. Assert.Equal (new Rect (4, 1, 6, 4), Application.MouseGrabView.Frame);
  814. } else if (iterations == 5) {
  815. Assert.Equal (win, Application.MouseGrabView);
  816. // Ungrab the mouse
  817. ReflectionTools.InvokePrivate (
  818. typeof (Application),
  819. "ProcessMouseEvent",
  820. new MouseEvent () {
  821. X = 7,
  822. Y = 4,
  823. Flags = MouseFlags.Button1Released
  824. });
  825. Assert.Null (Application.MouseGrabView);
  826. } else if (iterations == 8) Application.RequestStop ();
  827. };
  828. Application.Run ();
  829. }
  830. [Fact, AutoInitShutdown]
  831. public void EnsureVisibleBounds_With_Border_Null_Not_Throws ()
  832. {
  833. var top = new Toplevel ();
  834. Application.Begin (top);
  835. var exception = Record.Exception (() => ((FakeDriver)Application.Driver).SetBufferSize (0, 10));
  836. Assert.Null (exception);
  837. exception = Record.Exception (() => ((FakeDriver)Application.Driver).SetBufferSize (10, 0));
  838. Assert.Null (exception);
  839. }
  840. [Fact, AutoInitShutdown]
  841. public void OnEnter_OnLeave_Triggered_On_Application_Begin_End ()
  842. {
  843. var isEnter = false;
  844. var isLeave = false;
  845. var v = new View ();
  846. v.Enter += (s, _) => isEnter = true;
  847. v.Leave += (s, _) => isLeave = true;
  848. var top = Application.Top;
  849. top.Add (v);
  850. Assert.False (v.CanFocus);
  851. var exception = Record.Exception (() => top.OnEnter (top));
  852. Assert.Null (exception);
  853. exception = Record.Exception (() => top.OnLeave (top));
  854. Assert.Null (exception);
  855. v.CanFocus = true;
  856. Application.Begin (top);
  857. Assert.True (isEnter);
  858. Assert.False (isLeave);
  859. isEnter = false;
  860. var d = new Dialog ();
  861. var rs = Application.Begin (d);
  862. Assert.False (isEnter);
  863. Assert.True (isLeave);
  864. isLeave = false;
  865. Application.End (rs);
  866. Assert.True (isEnter);
  867. Assert.False (isLeave);
  868. }
  869. [Fact, AutoInitShutdown]
  870. public void PositionCursor_SetCursorVisibility_To_Invisible_If_Focused_Is_Null ()
  871. {
  872. var tf = new TextField ("test") { Width = 5 };
  873. var view = new View () { Width = 10, Height = 10 };
  874. view.Add (tf);
  875. Application.Top.Add (view);
  876. Application.Begin (Application.Top);
  877. Assert.True (tf.HasFocus);
  878. Application.Driver.GetCursorVisibility (out CursorVisibility cursor);
  879. Assert.Equal (CursorVisibility.Default, cursor);
  880. view.Enabled = false;
  881. Assert.False (tf.HasFocus);
  882. Application.Refresh ();
  883. Application.Driver.GetCursorVisibility (out cursor);
  884. Assert.Equal (CursorVisibility.Invisible, cursor);
  885. }
  886. [Fact, AutoInitShutdown]
  887. public void IsLoaded_Application_Begin ()
  888. {
  889. var top = Application.Top;
  890. Assert.False (top.IsLoaded);
  891. Application.Begin (top);
  892. Assert.True (top.IsLoaded);
  893. }
  894. [Fact, AutoInitShutdown]
  895. public void IsLoaded_With_Sub_Toplevel_Application_Begin_NeedDisplay ()
  896. {
  897. var top = Application.Top;
  898. var subTop = new Toplevel ();
  899. var view = new View (new Rect (0, 0, 20, 10));
  900. subTop.Add (view);
  901. top.Add (subTop);
  902. Assert.False (top.IsLoaded);
  903. Assert.False (subTop.IsLoaded);
  904. Assert.Equal (new Rect (0, 0, 20, 10), view.Frame);
  905. Assert.Equal (new Rect (0, 0, 20, 10), view._needsDisplay);
  906. view.LayoutStarted += view_LayoutStarted;
  907. void view_LayoutStarted (object sender, LayoutEventArgs e)
  908. {
  909. Assert.Equal (new Rect (0, 0, 20, 10), view._needsDisplay);
  910. view.LayoutStarted -= view_LayoutStarted;
  911. }
  912. Application.Begin (top);
  913. Assert.True (top.IsLoaded);
  914. Assert.True (subTop.IsLoaded);
  915. Assert.Equal (new Rect (0, 0, 20, 10), view.Frame);
  916. view.Frame = new Rect (1, 3, 10, 5);
  917. Assert.Equal (new Rect (1, 3, 10, 5), view.Frame);
  918. Assert.Equal (new Rect (0, 0, 10, 5), view._needsDisplay);
  919. view.Redraw (view.Bounds);
  920. view.Frame = new Rect (1, 3, 10, 5);
  921. Assert.Equal (new Rect (1, 3, 10, 5), view.Frame);
  922. Assert.Equal (new Rect (0, 0, 10, 5), view._needsDisplay);
  923. }
  924. [Fact, AutoInitShutdown]
  925. public void Toplevel_Inside_ScrollView_MouseGrabView ()
  926. {
  927. var scrollView = new ScrollView () {
  928. X = 3,
  929. Y = 3,
  930. Width = 40,
  931. Height = 16,
  932. ContentSize = new Size (200, 100)
  933. };
  934. var win = new Window ("Window") { X = 3, Y = 3, Width = Dim.Fill (3), Height = Dim.Fill (3) };
  935. scrollView.Add (win);
  936. var top = Application.Top;
  937. top.Add (scrollView);
  938. Application.Begin (top);
  939. Assert.Equal (new Rect (0, 0, 80, 25), top.Frame);
  940. Assert.Equal (new Rect (3, 3, 40, 16), scrollView.Frame);
  941. Assert.Equal (new Rect (0, 0, 200, 100), scrollView.Subviews [0].Frame);
  942. Assert.Equal (new Rect (3, 3, 194, 94), win.Frame);
  943. TestHelpers.AssertDriverContentsWithFrameAre (@"
  944. ┌┤Window├───────────────────────────┴
  945. │ ░
  946. │ ░
  947. │ ░
  948. │ ░
  949. │ ░
  950. │ ░
  951. │ ░
  952. │ ░
  953. │ ░
  954. │ ░
  955. │ ▼
  956. ◄├──────┤░░░░░░░░░░░░░░░░░░░░░░░░░░░░░► ", output);
  957. ReflectionTools.InvokePrivate (
  958. typeof (Application),
  959. "ProcessMouseEvent",
  960. new MouseEvent () {
  961. X = 6,
  962. Y = 6,
  963. Flags = MouseFlags.Button1Pressed
  964. });
  965. Assert.Equal (win, Application.MouseGrabView);
  966. Assert.Equal (new Rect (3, 3, 194, 94), win.Frame);
  967. ReflectionTools.InvokePrivate (
  968. typeof (Application),
  969. "ProcessMouseEvent",
  970. new MouseEvent () {
  971. X = 9,
  972. Y = 9,
  973. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  974. });
  975. Assert.Equal (win, Application.MouseGrabView);
  976. top.SetNeedsLayout ();
  977. top.LayoutSubviews ();
  978. Assert.Equal (new Rect (6, 6, 191, 91), win.Frame);
  979. Application.Refresh ();
  980. TestHelpers.AssertDriverContentsWithFrameAre (@"
  981. ┌┤Window├────────────────────────░
  982. │ ░
  983. │ ░
  984. │ ░
  985. │ ░
  986. │ ░
  987. │ ░
  988. │ ░
  989. │ ▼
  990. ◄├──────┤░░░░░░░░░░░░░░░░░░░░░░░░░░░░░► ", output);
  991. ReflectionTools.InvokePrivate (
  992. typeof (Application),
  993. "ProcessMouseEvent",
  994. new MouseEvent () {
  995. X = 5,
  996. Y = 5,
  997. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  998. });
  999. Assert.Equal (win, Application.MouseGrabView);
  1000. top.SetNeedsLayout ();
  1001. top.LayoutSubviews ();
  1002. Assert.Equal (new Rect (2, 2, 195, 95), win.Frame);
  1003. Application.Refresh ();
  1004. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1005. ┌┤Window├────────────────────────────│
  1006. │ ┴
  1007. │ ░
  1008. │ ░
  1009. │ ░
  1010. │ ░
  1011. │ ░
  1012. │ ░
  1013. │ ░
  1014. │ ░
  1015. │ ░
  1016. │ ░
  1017. │ ▼
  1018. ◄├──────┤░░░░░░░░░░░░░░░░░░░░░░░░░░░░░► ", output);
  1019. ReflectionTools.InvokePrivate (
  1020. typeof (Application),
  1021. "ProcessMouseEvent",
  1022. new MouseEvent () {
  1023. X = 5,
  1024. Y = 5,
  1025. Flags = MouseFlags.Button1Released
  1026. });
  1027. Assert.Null (Application.MouseGrabView);
  1028. ReflectionTools.InvokePrivate (
  1029. typeof (Application),
  1030. "ProcessMouseEvent",
  1031. new MouseEvent () {
  1032. X = 4,
  1033. Y = 4,
  1034. Flags = MouseFlags.ReportMousePosition
  1035. });
  1036. Assert.Equal (scrollView, Application.MouseGrabView);
  1037. }
  1038. [Fact, AutoInitShutdown]
  1039. public void Dialog_Bounds_Bigger_Than_Driver_Cols_And_Rows_Allow_Drag_Beyond_Left_Right_And_Bottom ()
  1040. {
  1041. var menu = new MenuBar (new MenuBarItem [] {
  1042. new MenuBarItem("File", new MenuItem [] {
  1043. new MenuItem("New", "", null)
  1044. })
  1045. });
  1046. var sb = new StatusBar (new StatusItem [] {
  1047. new StatusItem(Key.N, "~CTRL-N~ New", null)
  1048. });
  1049. var top = Application.Top;
  1050. top.Add (menu, sb);
  1051. var dialog = new Dialog ("Dialog", 20, 3, new Button ("Ok"));
  1052. Application.Begin (top);
  1053. ((FakeDriver)Application.Driver).SetBufferSize (40, 10);
  1054. Application.Begin (dialog);
  1055. Application.Refresh ();
  1056. Assert.Equal (new Rect (0, 0, 40, 10), top.Frame);
  1057. Assert.Equal (new Rect (10, 3, 20, 3), dialog.Frame);
  1058. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1059. File
  1060. ┌┤Dialog├──────────┐
  1061. │ [ Ok ] │
  1062. └──────────────────┘
  1063. CTRL-N New ", output);
  1064. Assert.Null (Application.MouseGrabView);
  1065. ReflectionTools.InvokePrivate (
  1066. typeof (Application),
  1067. "ProcessMouseEvent",
  1068. new MouseEvent () {
  1069. X = 10,
  1070. Y = 3,
  1071. Flags = MouseFlags.Button1Pressed
  1072. });
  1073. Assert.Equal (dialog, Application.MouseGrabView);
  1074. ReflectionTools.InvokePrivate (
  1075. typeof (Application),
  1076. "ProcessMouseEvent",
  1077. new MouseEvent () {
  1078. X = -11,
  1079. Y = -4,
  1080. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1081. });
  1082. Application.Refresh ();
  1083. Assert.Equal (new Rect (0, 0, 40, 10), top.Frame);
  1084. Assert.Equal (new Rect (0, 1, 20, 3), dialog.Frame);
  1085. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1086. File
  1087. ┌┤Dialog├──────────┐
  1088. │ [ Ok ] │
  1089. └──────────────────┘
  1090. CTRL-N New ", output);
  1091. // Changes Top size to same size as Dialog more menu and scroll bar
  1092. ((FakeDriver)Application.Driver).SetBufferSize (20, 5);
  1093. ReflectionTools.InvokePrivate (
  1094. typeof (Application),
  1095. "ProcessMouseEvent",
  1096. new MouseEvent () {
  1097. X = -1,
  1098. Y = -1,
  1099. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1100. });
  1101. Application.Refresh ();
  1102. Assert.Equal (new Rect (0, 0, 20, 5), top.Frame);
  1103. Assert.Equal (new Rect (0, 1, 20, 3), dialog.Frame);
  1104. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1105. File
  1106. ┌┤Dialog├──────────┐
  1107. │ [ Ok ] │
  1108. └──────────────────┘
  1109. CTRL-N New ", output);
  1110. // Changes Top size smaller than Dialog size
  1111. ((FakeDriver)Application.Driver).SetBufferSize (19, 3);
  1112. ReflectionTools.InvokePrivate (
  1113. typeof (Application),
  1114. "ProcessMouseEvent",
  1115. new MouseEvent () {
  1116. X = -1,
  1117. Y = -1,
  1118. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1119. });
  1120. Application.Refresh ();
  1121. Assert.Equal (new Rect (0, 0, 19, 3), top.Frame);
  1122. Assert.Equal (new Rect (-1, 1, 20, 3), dialog.Frame);
  1123. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1124. File
  1125. Dialog ──────────┐
  1126. [ Ok ] │", output);
  1127. ReflectionTools.InvokePrivate (
  1128. typeof (Application),
  1129. "ProcessMouseEvent",
  1130. new MouseEvent () {
  1131. X = 18,
  1132. Y = 3,
  1133. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1134. });
  1135. Application.Refresh ();
  1136. Assert.Equal (new Rect (0, 0, 19, 3), top.Frame);
  1137. Assert.Equal (new Rect (18, 2, 20, 3), dialog.Frame);
  1138. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1139. File
  1140. CTRL-N New ┌", output);
  1141. // On a real app we can't go beyond the SuperView bounds
  1142. ReflectionTools.InvokePrivate (
  1143. typeof (Application),
  1144. "ProcessMouseEvent",
  1145. new MouseEvent () {
  1146. X = 19,
  1147. Y = 4,
  1148. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1149. });
  1150. Application.Refresh ();
  1151. Assert.Equal (new Rect (0, 0, 19, 3), top.Frame);
  1152. Assert.Equal (new Rect (19, 2, 20, 3), dialog.Frame);
  1153. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1154. File
  1155. CTRL-N New", output);
  1156. }
  1157. [Fact, AutoInitShutdown]
  1158. public void Single_Smaller_Top_Will_Have_Cleaning_Trails_Chunk_On_Move ()
  1159. {
  1160. var dialog = new Dialog ("Single smaller Dialog") { Width = 30, Height = 10 };
  1161. dialog.Add (new Label (
  1162. "How should I've to react. Cleaning all chunk trails or setting the 'Cols' and 'Rows' to this dialog length?\n" +
  1163. "Cleaning is more easy to fix this.") {
  1164. X = Pos.Center (),
  1165. Y = Pos.Center (),
  1166. Width = Dim.Fill (),
  1167. Height = Dim.Fill (),
  1168. TextAlignment = TextAlignment.Centered,
  1169. VerticalTextAlignment = VerticalTextAlignment.Middle,
  1170. AutoSize = false
  1171. });
  1172. var rs = Application.Begin (dialog);
  1173. Assert.Null (Application.MouseGrabView);
  1174. Assert.Equal (new Rect (25, 7, 30, 10), dialog.Frame);
  1175. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1176. ┌┤Single smaller Dialog├─────┐
  1177. │ How should I've to react. │
  1178. │Cleaning all chunk trails or│
  1179. │ setting the 'Cols' and │
  1180. │ 'Rows' to this dialog │
  1181. │ length? │
  1182. │Cleaning is more easy to fix│
  1183. │ this. │
  1184. │ │
  1185. └────────────────────────────┘", output);
  1186. ReflectionTools.InvokePrivate (
  1187. typeof (Application),
  1188. "ProcessMouseEvent",
  1189. new MouseEvent () {
  1190. X = 25,
  1191. Y = 7,
  1192. Flags = MouseFlags.Button1Pressed
  1193. });
  1194. var firstIteration = false;
  1195. Application.RunMainLoopIteration (ref rs, true, ref firstIteration);
  1196. Assert.Equal (dialog, Application.MouseGrabView);
  1197. Assert.Equal (new Rect (25, 7, 30, 10), dialog.Frame);
  1198. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1199. ┌┤Single smaller Dialog├─────┐
  1200. │ How should I've to react. │
  1201. │Cleaning all chunk trails or│
  1202. │ setting the 'Cols' and │
  1203. │ 'Rows' to this dialog │
  1204. │ length? │
  1205. │Cleaning is more easy to fix│
  1206. │ this. │
  1207. │ │
  1208. └────────────────────────────┘", output);
  1209. ReflectionTools.InvokePrivate (
  1210. typeof (Application),
  1211. "ProcessMouseEvent",
  1212. new MouseEvent () {
  1213. X = 20,
  1214. Y = 10,
  1215. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1216. });
  1217. firstIteration = false;
  1218. Application.RunMainLoopIteration (ref rs, true, ref firstIteration);
  1219. Assert.Equal (dialog, Application.MouseGrabView);
  1220. Assert.Equal (new Rect (20, 10, 30, 10), dialog.Frame);
  1221. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1222. ┌┤Single smaller Dialog├─────┐
  1223. │ How should I've to react. │
  1224. │Cleaning all chunk trails or│
  1225. │ setting the 'Cols' and │
  1226. │ 'Rows' to this dialog │
  1227. │ length? │
  1228. │Cleaning is more easy to fix│
  1229. │ this. │
  1230. │ │
  1231. └────────────────────────────┘", output);
  1232. Application.End (rs);
  1233. }
  1234. }
  1235. }