ToplevelTests.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. using System;
  2. using Xunit;
  3. namespace Terminal.Gui.Core {
  4. public class ToplevelTests {
  5. [Fact]
  6. [AutoInitShutdown]
  7. public void Constructor_Default ()
  8. {
  9. var top = new Toplevel ();
  10. Assert.Equal (Colors.TopLevel, top.ColorScheme);
  11. Assert.Equal ("Dim.Fill(margin=0)", top.Width.ToString ());
  12. Assert.Equal ("Dim.Fill(margin=0)", top.Height.ToString ());
  13. Assert.False (top.Running);
  14. Assert.False (top.Modal);
  15. Assert.Null (top.MenuBar);
  16. Assert.Null (top.StatusBar);
  17. Assert.False (top.IsMdiContainer);
  18. Assert.False (top.IsMdiChild);
  19. }
  20. [Fact]
  21. [AutoInitShutdown]
  22. public void Create_Toplevel ()
  23. {
  24. var top = Toplevel.Create ();
  25. Assert.Equal (new Rect (0, 0, Application.Driver.Cols, Application.Driver.Rows), top.Bounds);
  26. }
  27. [Fact]
  28. [AutoInitShutdown]
  29. public void Application_Top_EnsureVisibleBounds_To_Driver_Rows_And_Cols ()
  30. {
  31. var iterations = 0;
  32. Application.Iteration += () => {
  33. if (iterations == 0) {
  34. Assert.Equal ("Top1", Application.Top.Text);
  35. Assert.Equal (0, Application.Top.Frame.X);
  36. Assert.Equal (0, Application.Top.Frame.Y);
  37. Assert.Equal (Application.Driver.Cols, Application.Top.Frame.Width);
  38. Assert.Equal (Application.Driver.Rows, Application.Top.Frame.Height);
  39. Application.Top.ProcessHotKey (new KeyEvent (Key.CtrlMask | Key.R, new KeyModifiers ()));
  40. } else if (iterations == 1) {
  41. Assert.Equal ("Top2", Application.Top.Text);
  42. Assert.Equal (0, Application.Top.Frame.X);
  43. Assert.Equal (0, Application.Top.Frame.Y);
  44. Assert.Equal (Application.Driver.Cols, Application.Top.Frame.Width);
  45. Assert.Equal (Application.Driver.Rows, Application.Top.Frame.Height);
  46. Application.Top.ProcessHotKey (new KeyEvent (Key.CtrlMask | Key.C, new KeyModifiers ()));
  47. } else if (iterations == 3) {
  48. Assert.Equal ("Top1", Application.Top.Text);
  49. Assert.Equal (0, Application.Top.Frame.X);
  50. Assert.Equal (0, Application.Top.Frame.Y);
  51. Assert.Equal (Application.Driver.Cols, Application.Top.Frame.Width);
  52. Assert.Equal (Application.Driver.Rows, Application.Top.Frame.Height);
  53. Application.Top.ProcessHotKey (new KeyEvent (Key.CtrlMask | Key.R, new KeyModifiers ()));
  54. } else if (iterations == 4) {
  55. Assert.Equal ("Top2", Application.Top.Text);
  56. Assert.Equal (0, Application.Top.Frame.X);
  57. Assert.Equal (0, Application.Top.Frame.Y);
  58. Assert.Equal (Application.Driver.Cols, Application.Top.Frame.Width);
  59. Assert.Equal (Application.Driver.Rows, Application.Top.Frame.Height);
  60. Application.Top.ProcessHotKey (new KeyEvent (Key.CtrlMask | Key.C, new KeyModifiers ()));
  61. } else if (iterations == 6) {
  62. Assert.Equal ("Top1", Application.Top.Text);
  63. Assert.Equal (0, Application.Top.Frame.X);
  64. Assert.Equal (0, Application.Top.Frame.Y);
  65. Assert.Equal (Application.Driver.Cols, Application.Top.Frame.Width);
  66. Assert.Equal (Application.Driver.Rows, Application.Top.Frame.Height);
  67. Application.Top.ProcessHotKey (new KeyEvent (Key.CtrlMask | Key.Q, new KeyModifiers ()));
  68. }
  69. iterations++;
  70. };
  71. Application.Run (Top1 ());
  72. Toplevel Top1 ()
  73. {
  74. var top = Application.Top;
  75. top.Text = "Top1";
  76. var menu = new MenuBar (new MenuBarItem [] {
  77. new MenuBarItem ("_Options", new MenuItem [] {
  78. new MenuItem ("_Run Top2", "", () => Application.Run (Top2 ()), null, null, Key.CtrlMask | Key.R),
  79. new MenuItem ("_Quit", "", () => Application.RequestStop(), null, null, Key.CtrlMask | Key.Q)
  80. })
  81. });
  82. top.Add (menu);
  83. var statusBar = new StatusBar (new [] {
  84. new StatusItem(Key.CtrlMask | Key.R, "~^R~ Run Top2", () => Application.Run (Top2 ())),
  85. new StatusItem(Key.CtrlMask | Key.Q, "~^Q~ Quit", () => Application.RequestStop())
  86. });
  87. top.Add (statusBar);
  88. var t1 = new Toplevel ();
  89. top.Add (t1);
  90. return top;
  91. }
  92. Toplevel Top2 ()
  93. {
  94. var top = new Toplevel (Application.Top.Frame);
  95. top.Text = "Top2";
  96. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  97. var menu = new MenuBar (new MenuBarItem [] {
  98. new MenuBarItem ("_Stage", new MenuItem [] {
  99. new MenuItem ("_Close", "", () => Application.RequestStop(), null, null, Key.CtrlMask | Key.C)
  100. })
  101. });
  102. top.Add (menu);
  103. var statusBar = new StatusBar (new [] {
  104. new StatusItem(Key.CtrlMask | Key.C, "~^C~ Close", () => Application.RequestStop()),
  105. });
  106. top.Add (statusBar);
  107. win.Add (new ListView () {
  108. X = 0,
  109. Y = 0,
  110. Width = Dim.Fill (),
  111. Height = Dim.Fill ()
  112. });
  113. top.Add (win);
  114. return top;
  115. }
  116. }
  117. [Fact]
  118. [AutoInitShutdown]
  119. public void Internal_Tests ()
  120. {
  121. var top = new Toplevel ();
  122. var eventInvoked = "";
  123. top.ChildUnloaded += (e) => eventInvoked = "ChildUnloaded";
  124. top.OnChildUnloaded (top);
  125. Assert.Equal ("ChildUnloaded", eventInvoked);
  126. top.ChildLoaded += (e) => eventInvoked = "ChildLoaded";
  127. top.OnChildLoaded (top);
  128. Assert.Equal ("ChildLoaded", eventInvoked);
  129. top.Closed += (e) => eventInvoked = "Closed";
  130. top.OnClosed (top);
  131. Assert.Equal ("Closed", eventInvoked);
  132. top.Closing += (e) => eventInvoked = "Closing";
  133. top.OnClosing (new ToplevelClosingEventArgs (top));
  134. Assert.Equal ("Closing", eventInvoked);
  135. top.AllChildClosed += () => eventInvoked = "AllChildClosed";
  136. top.OnAllChildClosed ();
  137. Assert.Equal ("AllChildClosed", eventInvoked);
  138. top.ChildClosed += (e) => eventInvoked = "ChildClosed";
  139. top.OnChildClosed (top);
  140. Assert.Equal ("ChildClosed", eventInvoked);
  141. top.Deactivate += (e) => eventInvoked = "Deactivate";
  142. top.OnDeactivate (top);
  143. Assert.Equal ("Deactivate", eventInvoked);
  144. top.Activate += (e) => eventInvoked = "Activate";
  145. top.OnActivate (top);
  146. Assert.Equal ("Activate", eventInvoked);
  147. top.Loaded += () => eventInvoked = "Loaded";
  148. top.OnLoaded ();
  149. Assert.Equal ("Loaded", eventInvoked);
  150. top.Ready += () => eventInvoked = "Ready";
  151. top.OnReady ();
  152. Assert.Equal ("Ready", eventInvoked);
  153. top.Unloaded += () => eventInvoked = "Unloaded";
  154. top.OnUnloaded ();
  155. Assert.Equal ("Unloaded", eventInvoked);
  156. top.AddMenuStatusBar (new MenuBar ());
  157. Assert.NotNull (top.MenuBar);
  158. top.AddMenuStatusBar (new StatusBar ());
  159. Assert.NotNull (top.StatusBar);
  160. top.RemoveMenuStatusBar (top.MenuBar);
  161. Assert.Null (top.MenuBar);
  162. top.RemoveMenuStatusBar (top.StatusBar);
  163. Assert.Null (top.StatusBar);
  164. Application.Begin (top);
  165. Assert.Equal (top, Application.Top);
  166. // top is Application.Top without menu and status bar.
  167. var supView = top.EnsureVisibleBounds (top, 2, 2, out int nx, out int ny, out View mb, out View sb);
  168. Assert.Equal (Application.Top, supView);
  169. Assert.Equal (0, nx);
  170. Assert.Equal (0, ny);
  171. Assert.Null (mb);
  172. Assert.Null (sb);
  173. top.AddMenuStatusBar (new MenuBar ());
  174. Assert.NotNull (top.MenuBar);
  175. // top is Application.Top with a menu and without status bar.
  176. top.EnsureVisibleBounds (top, 2, 2, out nx, out ny, out mb, out sb);
  177. Assert.Equal (0, nx);
  178. Assert.Equal (1, ny);
  179. Assert.NotNull (mb);
  180. Assert.Null (sb);
  181. top.AddMenuStatusBar (new StatusBar ());
  182. Assert.NotNull (top.StatusBar);
  183. // top is Application.Top with a menu and 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.NotNull (sb);
  189. top.RemoveMenuStatusBar (top.MenuBar);
  190. Assert.Null (top.MenuBar);
  191. // top is Application.Top without a menu and with a status bar.
  192. top.EnsureVisibleBounds (top, 2, 2, out nx, out ny, out mb, out sb);
  193. Assert.Equal (0, nx);
  194. Assert.Equal (0, ny);
  195. Assert.Null (mb);
  196. Assert.NotNull (sb);
  197. top.RemoveMenuStatusBar (top.StatusBar);
  198. Assert.Null (top.StatusBar);
  199. Assert.Null (top.MenuBar);
  200. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  201. top.Add (win);
  202. top.LayoutSubviews ();
  203. // The SuperView is always the same regardless of the caller.
  204. supView = top.EnsureVisibleBounds (win, 0, 0, out nx, out ny, out mb, out sb);
  205. Assert.Equal (Application.Top, supView);
  206. supView = win.EnsureVisibleBounds (win, 0, 0, out nx, out ny, out mb, out sb);
  207. Assert.Equal (Application.Top, supView);
  208. // top is Application.Top without menu and status bar.
  209. top.EnsureVisibleBounds (win, 0, 0, out nx, out ny, out mb, out sb);
  210. Assert.Equal (0, nx);
  211. Assert.Equal (0, ny);
  212. Assert.Null (mb);
  213. Assert.Null (sb);
  214. top.AddMenuStatusBar (new MenuBar ());
  215. Assert.NotNull (top.MenuBar);
  216. // top is Application.Top with a menu and without status bar.
  217. top.EnsureVisibleBounds (win, 2, 2, out nx, out ny, out mb, out sb);
  218. Assert.Equal (0, nx);
  219. Assert.Equal (1, ny);
  220. Assert.NotNull (mb);
  221. Assert.Null (sb);
  222. top.AddMenuStatusBar (new StatusBar ());
  223. Assert.NotNull (top.StatusBar);
  224. // top is Application.Top with a menu and status bar.
  225. top.EnsureVisibleBounds (win, 30, 20, out nx, out ny, out mb, out sb);
  226. Assert.Equal (0, nx);
  227. Assert.Equal (1, ny);
  228. Assert.NotNull (mb);
  229. Assert.NotNull (sb);
  230. top.RemoveMenuStatusBar (top.MenuBar);
  231. top.RemoveMenuStatusBar (top.StatusBar);
  232. Assert.Null (top.StatusBar);
  233. Assert.Null (top.MenuBar);
  234. top.Remove (win);
  235. win = new Window () { Width = 60, Height = 15 };
  236. top.Add (win);
  237. // top is Application.Top without menu and status bar.
  238. top.EnsureVisibleBounds (win, 0, 0, out nx, out ny, out mb, out sb);
  239. Assert.Equal (0, nx);
  240. Assert.Equal (0, ny);
  241. Assert.Null (mb);
  242. Assert.Null (sb);
  243. top.AddMenuStatusBar (new MenuBar ());
  244. Assert.NotNull (top.MenuBar);
  245. // top is Application.Top with a menu and without status bar.
  246. top.EnsureVisibleBounds (win, 2, 2, out nx, out ny, out mb, out sb);
  247. Assert.Equal (2, nx);
  248. Assert.Equal (2, ny);
  249. Assert.NotNull (mb);
  250. Assert.Null (sb);
  251. top.AddMenuStatusBar (new StatusBar ());
  252. Assert.NotNull (top.StatusBar);
  253. // top is Application.Top with a menu and status bar.
  254. top.EnsureVisibleBounds (win, 30, 20, out nx, out ny, out mb, out sb);
  255. Assert.Equal (20, nx); // 20+60=80
  256. Assert.Equal (9, ny); // 9+15+1(mb)=25
  257. Assert.NotNull (mb);
  258. Assert.NotNull (sb);
  259. top.PositionToplevels ();
  260. Assert.Equal (new Rect (0, 1, 60, 15), win.Frame);
  261. Assert.Null (Toplevel.dragPosition);
  262. win.MouseEvent (new MouseEvent () { X = 6, Y = 0, Flags = MouseFlags.Button1Pressed });
  263. Assert.Equal (new Point (6, 0), Toplevel.dragPosition);
  264. win.MouseEvent (new MouseEvent () { X = 6, Y = 0, Flags = MouseFlags.Button1Released });
  265. Assert.Null (Toplevel.dragPosition);
  266. win.CanFocus = false;
  267. win.MouseEvent (new MouseEvent () { X = 6, Y = 0, Flags = MouseFlags.Button1Pressed });
  268. Assert.Null (Toplevel.dragPosition);
  269. }
  270. [Fact]
  271. [AutoInitShutdown]
  272. public void KeyBindings_Command ()
  273. {
  274. var isRunning = false;
  275. var win1 = new Window ("Win1") { Width = Dim.Percent (50f), Height = Dim.Fill () };
  276. var lblTf1W1 = new Label ("Enter text in TextField on Win1:");
  277. var tf1W1 = new TextField ("Text1 on Win1") { X = Pos.Right (lblTf1W1) + 1, Width = Dim.Fill () };
  278. var lblTvW1 = new Label ("Enter text in TextView on Win1:") { Y = Pos.Bottom (lblTf1W1) + 1 };
  279. var tvW1 = new TextView () { X = Pos.Left (tf1W1), Width = Dim.Fill (), Height = 2, Text = "First line Win1\nSecond line Win1" };
  280. var lblTf2W1 = new Label ("Enter text in TextField on Win1:") { Y = Pos.Bottom (lblTvW1) + 1 };
  281. var tf2W1 = new TextField ("Text2 on Win1") { X = Pos.Left (tf1W1), Width = Dim.Fill () };
  282. win1.Add (lblTf1W1, tf1W1, lblTvW1, tvW1, lblTf2W1, tf2W1);
  283. var win2 = new Window ("Win2") { X = Pos.Right (win1) + 1, Width = Dim.Percent (50f), Height = Dim.Fill () };
  284. var lblTf1W2 = new Label ("Enter text in TextField on Win2:");
  285. var tf1W2 = new TextField ("Text1 on Win2") { X = Pos.Right (lblTf1W2) + 1, Width = Dim.Fill () };
  286. var lblTvW2 = new Label ("Enter text in TextView on Win2:") { Y = Pos.Bottom (lblTf1W2) + 1 };
  287. var tvW2 = new TextView () { X = Pos.Left (tf1W2), Width = Dim.Fill (), Height = 2, Text = "First line Win1\nSecond line Win2" };
  288. var lblTf2W2 = new Label ("Enter text in TextField on Win2:") { Y = Pos.Bottom (lblTvW2) + 1 };
  289. var tf2W2 = new TextField ("Text2 on Win2") { X = Pos.Left (tf1W2), Width = Dim.Fill () };
  290. win2.Add (lblTf1W2, tf1W2, lblTvW2, tvW2, lblTf2W2, tf2W2);
  291. var top = Application.Top;
  292. top.Add (win1, win2);
  293. top.Loaded += () => isRunning = true;
  294. top.Closing += (_) => isRunning = false;
  295. Application.Begin (top);
  296. top.Running = true;
  297. Assert.Equal (new Rect (0, 0, 40, 25), win1.Frame);
  298. Assert.Equal (new Rect (41, 0, 40, 25), win2.Frame);
  299. Assert.Equal (win1, top.Focused);
  300. Assert.Equal (tf1W1, top.MostFocused);
  301. Assert.True (isRunning);
  302. Assert.True (top.Focused.ProcessKey (new KeyEvent (Application.QuitKey, new KeyModifiers ())));
  303. Assert.False (isRunning);
  304. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Z | Key.CtrlMask, new KeyModifiers ())));
  305. Assert.False (top.Focused.ProcessKey (new KeyEvent (Key.F5, new KeyModifiers ())));
  306. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  307. Assert.Equal (win1, top.Focused);
  308. Assert.Equal (tvW1, top.MostFocused);
  309. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  310. Assert.Equal ($"\tFirst line Win1{Environment.NewLine}Second line Win1", tvW1.Text);
  311. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ())));
  312. Assert.Equal ($"First line Win1{Environment.NewLine}Second line Win1", tvW1.Text);
  313. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers ())));
  314. Assert.Equal (win1, top.Focused);
  315. Assert.Equal (tf2W1, top.MostFocused);
  316. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  317. Assert.Equal (win1, top.Focused);
  318. Assert.Equal (tf1W1, top.MostFocused);
  319. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  320. Assert.Equal (win1, top.Focused);
  321. Assert.Equal (tf1W1, top.MostFocused);
  322. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  323. Assert.Equal (win1, top.Focused);
  324. Assert.Equal (tvW1, top.MostFocused);
  325. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.I | Key.CtrlMask, new KeyModifiers ())));
  326. Assert.Equal (win1, top.Focused);
  327. Assert.Equal (tf2W1, top.MostFocused);
  328. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ())));
  329. Assert.Equal (win1, top.Focused);
  330. Assert.Equal (tvW1, top.MostFocused);
  331. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ())));
  332. Assert.Equal (win1, top.Focused);
  333. Assert.Equal (tf1W1, top.MostFocused);
  334. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ())));
  335. Assert.Equal (win1, top.Focused);
  336. Assert.Equal (tf2W1, top.MostFocused);
  337. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers ())));
  338. Assert.Equal (win2, top.Focused);
  339. Assert.Equal (tf1W2, top.MostFocused);
  340. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask | Key.ShiftMask, new KeyModifiers ())));
  341. Assert.Equal (win1, top.Focused);
  342. Assert.Equal (tf2W1, top.MostFocused);
  343. Assert.True (top.Focused.ProcessKey (new KeyEvent (Application.AlternateForwardKey, new KeyModifiers ())));
  344. Assert.Equal (win2, top.Focused);
  345. Assert.Equal (tf1W2, top.MostFocused);
  346. Assert.True (top.Focused.ProcessKey (new KeyEvent (Application.AlternateBackwardKey, new KeyModifiers ())));
  347. Assert.Equal (win1, top.Focused);
  348. Assert.Equal (tf2W1, top.MostFocused);
  349. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ())));
  350. Assert.Equal (win1, top.Focused);
  351. Assert.Equal (tvW1, top.MostFocused);
  352. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.B | Key.CtrlMask, new KeyModifiers ())));
  353. Assert.Equal (win1, top.Focused);
  354. Assert.Equal (tf1W1, top.MostFocused);
  355. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  356. Assert.Equal (win1, top.Focused);
  357. Assert.Equal (tvW1, top.MostFocused);
  358. Assert.Equal (new Point (0, 0), tvW1.CursorPosition);
  359. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.End | Key.CtrlMask, new KeyModifiers ())));
  360. Assert.Equal (win1, top.Focused);
  361. Assert.Equal (tvW1, top.MostFocused);
  362. Assert.Equal (new Point (16, 1), tvW1.CursorPosition);
  363. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.F | Key.CtrlMask, new KeyModifiers ())));
  364. Assert.Equal (win1, top.Focused);
  365. Assert.Equal (tf2W1, top.MostFocused);
  366. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.L | Key.CtrlMask, new KeyModifiers ())));
  367. }
  368. [Fact]
  369. [AutoInitShutdown]
  370. public void KeyBindings_Command_With_MdiTop ()
  371. {
  372. var top = Application.Top;
  373. Assert.Null (Application.MdiTop);
  374. top.IsMdiContainer = true;
  375. Assert.Equal (Application.Top, Application.MdiTop);
  376. var isRunning = true;
  377. var win1 = new Window ("Win1") { Width = Dim.Percent (50f), Height = Dim.Fill () };
  378. var lblTf1W1 = new Label ("Enter text in TextField on Win1:");
  379. var tf1W1 = new TextField ("Text1 on Win1") { X = Pos.Right (lblTf1W1) + 1, Width = Dim.Fill () };
  380. var lblTvW1 = new Label ("Enter text in TextView on Win1:") { Y = Pos.Bottom (lblTf1W1) + 1 };
  381. var tvW1 = new TextView () { X = Pos.Left (tf1W1), Width = Dim.Fill (), Height = 2, Text = "First line Win1\nSecond line Win1" };
  382. var lblTf2W1 = new Label ("Enter text in TextField on Win1:") { Y = Pos.Bottom (lblTvW1) + 1 };
  383. var tf2W1 = new TextField ("Text2 on Win1") { X = Pos.Left (tf1W1), Width = Dim.Fill () };
  384. win1.Add (lblTf1W1, tf1W1, lblTvW1, tvW1, lblTf2W1, tf2W1);
  385. var win2 = new Window ("Win2") { Width = Dim.Percent (50f), Height = Dim.Fill () };
  386. var lblTf1W2 = new Label ("Enter text in TextField on Win2:");
  387. var tf1W2 = new TextField ("Text1 on Win2") { X = Pos.Right (lblTf1W2) + 1, Width = Dim.Fill () };
  388. var lblTvW2 = new Label ("Enter text in TextView on Win2:") { Y = Pos.Bottom (lblTf1W2) + 1 };
  389. var tvW2 = new TextView () { X = Pos.Left (tf1W2), Width = Dim.Fill (), Height = 2, Text = "First line Win1\nSecond line Win2" };
  390. var lblTf2W2 = new Label ("Enter text in TextField on Win2:") { Y = Pos.Bottom (lblTvW2) + 1 };
  391. var tf2W2 = new TextField ("Text2 on Win2") { X = Pos.Left (tf1W2), Width = Dim.Fill () };
  392. win2.Add (lblTf1W2, tf1W2, lblTvW2, tvW2, lblTf2W2, tf2W2);
  393. win1.Closing += (_) => isRunning = false;
  394. Assert.Null (top.Focused);
  395. Assert.Equal (top, Application.Current);
  396. Assert.True (top.IsCurrentTop);
  397. Assert.Equal (top, Application.MdiTop);
  398. Application.Begin (win1);
  399. Assert.Equal (new Rect (0, 0, 40, 25), win1.Frame);
  400. Assert.NotEqual (top, Application.Current);
  401. Assert.False (top.IsCurrentTop);
  402. Assert.Equal (win1, Application.Current);
  403. Assert.True (win1.IsCurrentTop);
  404. Assert.True (win1.IsMdiChild);
  405. Assert.Null (top.Focused);
  406. Assert.Null (top.MostFocused);
  407. Assert.Equal (win1.Subviews [0], win1.Focused);
  408. Assert.Equal (tf1W1, win1.MostFocused);
  409. Assert.Single (Application.MdiChildes);
  410. Application.Begin (win2);
  411. Assert.Equal (new Rect (0, 0, 40, 25), win2.Frame);
  412. Assert.NotEqual (top, Application.Current);
  413. Assert.False (top.IsCurrentTop);
  414. Assert.Equal (win2, Application.Current);
  415. Assert.True (win2.IsCurrentTop);
  416. Assert.True (win2.IsMdiChild);
  417. Assert.Null (top.Focused);
  418. Assert.Null (top.MostFocused);
  419. Assert.Equal (win2.Subviews [0], win2.Focused);
  420. Assert.Equal (tf1W2, win2.MostFocused);
  421. Assert.Equal (2, Application.MdiChildes.Count);
  422. Application.ShowChild (win1);
  423. Assert.Equal (win1, Application.Current);
  424. Assert.Equal (win1, Application.MdiChildes [0]);
  425. win1.Running = true;
  426. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Application.QuitKey, new KeyModifiers ())));
  427. Assert.False (isRunning);
  428. Assert.False (win1.Running);
  429. Assert.Equal (win1, Application.MdiChildes [0]);
  430. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Z | Key.CtrlMask, new KeyModifiers ())));
  431. Assert.False (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.F5, new KeyModifiers ())));
  432. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  433. Assert.True (win1.IsCurrentTop);
  434. Assert.Equal (tvW1, win1.MostFocused);
  435. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  436. Assert.Equal ($"\tFirst line Win1{Environment.NewLine}Second line Win1", tvW1.Text);
  437. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ())));
  438. Assert.Equal ($"First line Win1{Environment.NewLine}Second line Win1", tvW1.Text);
  439. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers ())));
  440. Assert.Equal (win1, Application.MdiChildes [0]);
  441. Assert.Equal (tf2W1, win1.MostFocused);
  442. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  443. Assert.Equal (win1, Application.MdiChildes [0]);
  444. Assert.Equal (tf1W1, win1.MostFocused);
  445. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  446. Assert.Equal (win1, Application.MdiChildes [0]);
  447. Assert.Equal (tf1W1, win1.MostFocused);
  448. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  449. Assert.Equal (win1, Application.MdiChildes [0]);
  450. Assert.Equal (tvW1, win1.MostFocused);
  451. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.I | Key.CtrlMask, new KeyModifiers ())));
  452. Assert.Equal (win1, Application.MdiChildes [0]);
  453. Assert.Equal (tf2W1, win1.MostFocused);
  454. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ())));
  455. Assert.Equal (win1, Application.MdiChildes [0]);
  456. Assert.Equal (tvW1, win1.MostFocused);
  457. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ())));
  458. Assert.Equal (win1, Application.MdiChildes [0]);
  459. Assert.Equal (tf1W1, win1.MostFocused);
  460. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ())));
  461. Assert.Equal (win1, Application.MdiChildes [0]);
  462. Assert.Equal (tf2W1, win1.MostFocused);
  463. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Tab, 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.Tab | Key.CtrlMask, new KeyModifiers ())));
  467. Assert.Equal (win2, Application.MdiChildes [0]);
  468. Assert.Equal (tf1W2, win2.MostFocused);
  469. tf2W2.SetFocus ();
  470. Assert.True (tf2W2.HasFocus);
  471. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask | Key.ShiftMask, new KeyModifiers ())));
  472. Assert.Equal (win1, Application.MdiChildes [0]);
  473. Assert.Equal (tf1W1, win1.MostFocused);
  474. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Application.AlternateForwardKey, new KeyModifiers ())));
  475. Assert.Equal (win2, Application.MdiChildes [0]);
  476. Assert.Equal (tf2W2, win2.MostFocused);
  477. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Application.AlternateBackwardKey, new KeyModifiers ())));
  478. Assert.Equal (win1, Application.MdiChildes [0]);
  479. Assert.Equal (tf1W1, win1.MostFocused);
  480. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  481. Assert.Equal (win1, Application.MdiChildes [0]);
  482. Assert.Equal (tvW1, win1.MostFocused);
  483. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.B | Key.CtrlMask, new KeyModifiers ())));
  484. Assert.Equal (win1, Application.MdiChildes [0]);
  485. Assert.Equal (tf1W1, win1.MostFocused);
  486. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  487. Assert.Equal (win1, Application.MdiChildes [0]);
  488. Assert.Equal (tvW1, win1.MostFocused);
  489. Assert.Equal (new Point (0, 0), tvW1.CursorPosition);
  490. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.End | Key.CtrlMask, new KeyModifiers ())));
  491. Assert.Equal (win1, Application.MdiChildes [0]);
  492. Assert.Equal (tvW1, win1.MostFocused);
  493. Assert.Equal (new Point (16, 1), tvW1.CursorPosition);
  494. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.F | Key.CtrlMask, new KeyModifiers ())));
  495. Assert.Equal (win1, Application.MdiChildes [0]);
  496. Assert.Equal (tf2W1, win1.MostFocused);
  497. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.L | Key.CtrlMask, new KeyModifiers ())));
  498. }
  499. [Fact]
  500. public void Added_Event_Should_Not_Be_Used_To_Initialize_Toplevel_Events ()
  501. {
  502. Key alternateForwardKey = default;
  503. Key alternateBackwardKey = default;
  504. Key quitKey = default;
  505. var wasAdded = false;
  506. var view = new View ();
  507. view.Added += View_Added;
  508. void View_Added (View obj)
  509. {
  510. Assert.Throws<NullReferenceException> (() => Application.Top.AlternateForwardKeyChanged += (e) => alternateForwardKey = e);
  511. Assert.Throws<NullReferenceException> (() => Application.Top.AlternateBackwardKeyChanged += (e) => alternateBackwardKey = e);
  512. Assert.Throws<NullReferenceException> (() => Application.Top.QuitKeyChanged += (e) => quitKey = e);
  513. Assert.False (wasAdded);
  514. wasAdded = true;
  515. view.Added -= View_Added;
  516. }
  517. var win = new Window ();
  518. win.Add (view);
  519. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  520. var top = Application.Top;
  521. top.Add (win);
  522. Assert.True (wasAdded);
  523. Application.Shutdown ();
  524. }
  525. [Fact]
  526. [AutoInitShutdown]
  527. public void AlternateForwardKeyChanged_AlternateBackwardKeyChanged_QuitKeyChanged_Events ()
  528. {
  529. Key alternateForwardKey = default;
  530. Key alternateBackwardKey = default;
  531. Key quitKey = default;
  532. var view = new View ();
  533. view.Initialized += View_Initialized;
  534. void View_Initialized (object sender, EventArgs e)
  535. {
  536. Application.Top.AlternateForwardKeyChanged += (e) => alternateForwardKey = e;
  537. Application.Top.AlternateBackwardKeyChanged += (e) => alternateBackwardKey = e;
  538. Application.Top.QuitKeyChanged += (e) => quitKey = e;
  539. }
  540. var win = new Window ();
  541. win.Add (view);
  542. var top = Application.Top;
  543. top.Add (win);
  544. Application.Begin (top);
  545. Assert.Equal (Key.Null, alternateForwardKey);
  546. Assert.Equal (Key.Null, alternateBackwardKey);
  547. Assert.Equal (Key.Null, quitKey);
  548. Assert.Equal (Key.PageDown | Key.CtrlMask, Application.AlternateForwardKey);
  549. Assert.Equal (Key.PageUp | Key.CtrlMask, Application.AlternateBackwardKey);
  550. Assert.Equal (Key.Q | Key.CtrlMask, Application.QuitKey);
  551. Application.AlternateForwardKey = Key.A;
  552. Application.AlternateBackwardKey = Key.B;
  553. Application.QuitKey = Key.C;
  554. Assert.Equal (Key.PageDown | Key.CtrlMask, alternateForwardKey);
  555. Assert.Equal (Key.PageUp | Key.CtrlMask, alternateBackwardKey);
  556. Assert.Equal (Key.Q | Key.CtrlMask, quitKey);
  557. Assert.Equal (Key.A, Application.AlternateForwardKey);
  558. Assert.Equal (Key.B, Application.AlternateBackwardKey);
  559. Assert.Equal (Key.C, Application.QuitKey);
  560. // Replacing the defaults keys to avoid errors on others unit tests that are using it.
  561. Application.AlternateForwardKey = Key.PageDown | Key.CtrlMask;
  562. Application.AlternateBackwardKey = Key.PageUp | Key.CtrlMask;
  563. Application.QuitKey = Key.Q | Key.CtrlMask;
  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. }
  568. [Fact]
  569. [AutoInitShutdown]
  570. public void FileDialog_FileSystemWatcher ()
  571. {
  572. for (int i = 0; i < 256; i++) {
  573. var fd = new FileDialog ();
  574. fd.Ready += () => Application.RequestStop ();
  575. Application.Run (fd);
  576. }
  577. }
  578. }
  579. }