ToplevelTests.cs 28 KB

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