ToplevelTests.cs 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  1. using System;
  2. using Terminal.Gui;
  3. using Xunit;
  4. using Xunit.Abstractions;
  5. namespace Terminal.Gui.TopLevelTests {
  6. public class ToplevelTests {
  7. readonly ITestOutputHelper output;
  8. public ToplevelTests (ITestOutputHelper output)
  9. {
  10. this.output = output;
  11. }
  12. [Fact]
  13. [AutoInitShutdown]
  14. public void Constructor_Default ()
  15. {
  16. var top = new Toplevel ();
  17. Assert.Equal (Colors.TopLevel, top.ColorScheme);
  18. Assert.Equal ("Fill(0)", top.Width.ToString ());
  19. Assert.Equal ("Fill(0)", top.Height.ToString ());
  20. Assert.False (top.Running);
  21. Assert.False (top.Modal);
  22. Assert.Null (top.MenuBar);
  23. Assert.Null (top.StatusBar);
  24. Assert.False (top.IsMdiContainer);
  25. Assert.False (top.IsMdiChild);
  26. }
  27. [Fact]
  28. [AutoInitShutdown]
  29. public void Create_Toplevel ()
  30. {
  31. var top = Toplevel.Create ();
  32. Assert.Equal (new Rect (0, 0, Application.Driver.Cols, Application.Driver.Rows), top.Bounds);
  33. }
  34. [Fact]
  35. [AutoInitShutdown]
  36. public void Application_Top_EnsureVisibleBounds_To_Driver_Rows_And_Cols ()
  37. {
  38. var iterations = 0;
  39. Application.Iteration += () => {
  40. if (iterations == 0) {
  41. Assert.False (Application.Top.AutoSize);
  42. Assert.Equal ("Top1", Application.Top.Text);
  43. Assert.Equal (0, Application.Top.Frame.X);
  44. Assert.Equal (0, Application.Top.Frame.Y);
  45. Assert.Equal (Application.Driver.Cols, Application.Top.Frame.Width);
  46. Assert.Equal (Application.Driver.Rows, Application.Top.Frame.Height);
  47. Application.Top.ProcessHotKey (new KeyEvent (Key.CtrlMask | Key.R, new KeyModifiers ()));
  48. } else if (iterations == 1) {
  49. Assert.Equal ("Top2", Application.Top.Text);
  50. Assert.Equal (0, Application.Top.Frame.X);
  51. Assert.Equal (0, Application.Top.Frame.Y);
  52. Assert.Equal (Application.Driver.Cols, Application.Top.Frame.Width);
  53. Assert.Equal (Application.Driver.Rows, Application.Top.Frame.Height);
  54. Application.Top.ProcessHotKey (new KeyEvent (Key.CtrlMask | Key.C, new KeyModifiers ()));
  55. } else if (iterations == 3) {
  56. Assert.Equal ("Top1", Application.Top.Text);
  57. Assert.Equal (0, Application.Top.Frame.X);
  58. Assert.Equal (0, Application.Top.Frame.Y);
  59. Assert.Equal (Application.Driver.Cols, Application.Top.Frame.Width);
  60. Assert.Equal (Application.Driver.Rows, Application.Top.Frame.Height);
  61. Application.Top.ProcessHotKey (new KeyEvent (Key.CtrlMask | Key.R, new KeyModifiers ()));
  62. } else if (iterations == 4) {
  63. Assert.Equal ("Top2", Application.Top.Text);
  64. Assert.Equal (0, Application.Top.Frame.X);
  65. Assert.Equal (0, Application.Top.Frame.Y);
  66. Assert.Equal (Application.Driver.Cols, Application.Top.Frame.Width);
  67. Assert.Equal (Application.Driver.Rows, Application.Top.Frame.Height);
  68. Application.Top.ProcessHotKey (new KeyEvent (Key.CtrlMask | Key.C, new KeyModifiers ()));
  69. } else if (iterations == 6) {
  70. Assert.Equal ("Top1", Application.Top.Text);
  71. Assert.Equal (0, Application.Top.Frame.X);
  72. Assert.Equal (0, Application.Top.Frame.Y);
  73. Assert.Equal (Application.Driver.Cols, Application.Top.Frame.Width);
  74. Assert.Equal (Application.Driver.Rows, Application.Top.Frame.Height);
  75. Application.Top.ProcessHotKey (new KeyEvent (Key.CtrlMask | Key.Q, new KeyModifiers ()));
  76. }
  77. iterations++;
  78. };
  79. Application.Run (Top1 ());
  80. Toplevel Top1 ()
  81. {
  82. var top = Application.Top;
  83. top.Text = "Top1";
  84. var menu = new MenuBar (new MenuBarItem [] {
  85. new MenuBarItem ("_Options", new MenuItem [] {
  86. new MenuItem ("_Run Top2", "", () => Application.Run (Top2 ()), null, null, Key.CtrlMask | Key.R),
  87. new MenuItem ("_Quit", "", () => Application.RequestStop(), null, null, Key.CtrlMask | Key.Q)
  88. })
  89. });
  90. top.Add (menu);
  91. var statusBar = new StatusBar (new [] {
  92. new StatusItem(Key.CtrlMask | Key.R, "~^R~ Run Top2", () => Application.Run (Top2 ())),
  93. new StatusItem(Key.CtrlMask | Key.Q, "~^Q~ Quit", () => Application.RequestStop())
  94. });
  95. top.Add (statusBar);
  96. var t1 = new Toplevel ();
  97. top.Add (t1);
  98. return top;
  99. }
  100. Toplevel Top2 ()
  101. {
  102. var top = new Toplevel (Application.Top.Frame);
  103. top.Text = "Top2";
  104. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  105. var menu = new MenuBar (new MenuBarItem [] {
  106. new MenuBarItem ("_Stage", new MenuItem [] {
  107. new MenuItem ("_Close", "", () => Application.RequestStop(), null, null, Key.CtrlMask | Key.C)
  108. })
  109. });
  110. top.Add (menu);
  111. var statusBar = new StatusBar (new [] {
  112. new StatusItem(Key.CtrlMask | Key.C, "~^C~ Close", () => Application.RequestStop()),
  113. });
  114. top.Add (statusBar);
  115. win.Add (new ListView () {
  116. X = 0,
  117. Y = 0,
  118. Width = Dim.Fill (),
  119. Height = Dim.Fill ()
  120. });
  121. top.Add (win);
  122. return top;
  123. }
  124. }
  125. [Fact]
  126. [AutoInitShutdown]
  127. public void Internal_Tests ()
  128. {
  129. Toplevel.dragPosition = null; // dragPosition is `static` and must be reset for each instance or unit tests will fail?
  130. var top = new Toplevel ();
  131. var eventInvoked = "";
  132. top.ChildUnloaded += (e) => eventInvoked = "ChildUnloaded";
  133. top.OnChildUnloaded (top);
  134. Assert.Equal ("ChildUnloaded", eventInvoked);
  135. top.ChildLoaded += (e) => eventInvoked = "ChildLoaded";
  136. top.OnChildLoaded (top);
  137. Assert.Equal ("ChildLoaded", eventInvoked);
  138. top.Closed += (e) => eventInvoked = "Closed";
  139. top.OnClosed (top);
  140. Assert.Equal ("Closed", eventInvoked);
  141. top.Closing += (e) => eventInvoked = "Closing";
  142. top.OnClosing (new ToplevelClosingEventArgs (top));
  143. Assert.Equal ("Closing", eventInvoked);
  144. top.AllChildClosed += () => eventInvoked = "AllChildClosed";
  145. top.OnAllChildClosed ();
  146. Assert.Equal ("AllChildClosed", eventInvoked);
  147. top.ChildClosed += (e) => eventInvoked = "ChildClosed";
  148. top.OnChildClosed (top);
  149. Assert.Equal ("ChildClosed", eventInvoked);
  150. top.Deactivate += (e) => eventInvoked = "Deactivate";
  151. top.OnDeactivate (top);
  152. Assert.Equal ("Deactivate", eventInvoked);
  153. top.Activate += (e) => eventInvoked = "Activate";
  154. top.OnActivate (top);
  155. Assert.Equal ("Activate", eventInvoked);
  156. top.Loaded += () => eventInvoked = "Loaded";
  157. top.OnLoaded ();
  158. Assert.Equal ("Loaded", eventInvoked);
  159. top.Ready += () => eventInvoked = "Ready";
  160. top.OnReady ();
  161. Assert.Equal ("Ready", eventInvoked);
  162. top.Unloaded += () => eventInvoked = "Unloaded";
  163. top.OnUnloaded ();
  164. Assert.Equal ("Unloaded", eventInvoked);
  165. top.AddMenuStatusBar (new MenuBar ());
  166. Assert.NotNull (top.MenuBar);
  167. top.AddMenuStatusBar (new StatusBar ());
  168. Assert.NotNull (top.StatusBar);
  169. top.RemoveMenuStatusBar (top.MenuBar);
  170. Assert.Null (top.MenuBar);
  171. top.RemoveMenuStatusBar (top.StatusBar);
  172. Assert.Null (top.StatusBar);
  173. Application.Begin (top);
  174. Assert.Equal (top, Application.Top);
  175. // top is Application.Top without menu and status bar.
  176. var supView = top.EnsureVisibleBounds (top, 2, 2, out int nx, out int ny, out MenuBar mb, out StatusBar sb);
  177. Assert.Equal (Application.Top, supView);
  178. Assert.Equal (0, nx);
  179. Assert.Equal (0, ny);
  180. Assert.Null (mb);
  181. Assert.Null (sb);
  182. top.AddMenuStatusBar (new MenuBar ());
  183. Assert.NotNull (top.MenuBar);
  184. // top is Application.Top with a menu and without 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.Null (sb);
  190. top.AddMenuStatusBar (new StatusBar ());
  191. Assert.NotNull (top.StatusBar);
  192. // top is Application.Top with a menu and status bar.
  193. top.EnsureVisibleBounds (top, 2, 2, out nx, out ny, out mb, out sb);
  194. Assert.Equal (0, nx);
  195. Assert.Equal (1, ny);
  196. Assert.NotNull (mb);
  197. Assert.NotNull (sb);
  198. top.RemoveMenuStatusBar (top.MenuBar);
  199. Assert.Null (top.MenuBar);
  200. // top is Application.Top without a menu and with a status bar.
  201. top.EnsureVisibleBounds (top, 2, 2, out nx, out ny, out mb, out sb);
  202. Assert.Equal (0, nx);
  203. Assert.Equal (0, ny);
  204. Assert.Null (mb);
  205. Assert.NotNull (sb);
  206. top.RemoveMenuStatusBar (top.StatusBar);
  207. Assert.Null (top.StatusBar);
  208. Assert.Null (top.MenuBar);
  209. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  210. top.Add (win);
  211. top.LayoutSubviews ();
  212. // The SuperView is always the same regardless of the caller.
  213. supView = top.EnsureVisibleBounds (win, 0, 0, out nx, out ny, out mb, out sb);
  214. Assert.Equal (Application.Top, supView);
  215. supView = win.EnsureVisibleBounds (win, 0, 0, out nx, out ny, out mb, out sb);
  216. Assert.Equal (Application.Top, supView);
  217. // top is Application.Top without menu and status bar.
  218. top.EnsureVisibleBounds (win, 0, 0, out nx, out ny, out mb, out sb);
  219. Assert.Equal (0, nx);
  220. Assert.Equal (0, ny);
  221. Assert.Null (mb);
  222. Assert.Null (sb);
  223. top.AddMenuStatusBar (new MenuBar ());
  224. Assert.NotNull (top.MenuBar);
  225. // top is Application.Top with a menu and without status bar.
  226. top.EnsureVisibleBounds (win, 2, 2, out nx, out ny, out mb, out sb);
  227. Assert.Equal (0, nx);
  228. Assert.Equal (1, ny);
  229. Assert.NotNull (mb);
  230. Assert.Null (sb);
  231. top.AddMenuStatusBar (new StatusBar ());
  232. Assert.NotNull (top.StatusBar);
  233. // top is Application.Top with a menu and status bar.
  234. top.EnsureVisibleBounds (win, 30, 20, out nx, out ny, out mb, out sb);
  235. Assert.Equal (0, nx);
  236. Assert.Equal (1, ny);
  237. Assert.NotNull (mb);
  238. Assert.NotNull (sb);
  239. top.RemoveMenuStatusBar (top.MenuBar);
  240. top.RemoveMenuStatusBar (top.StatusBar);
  241. Assert.Null (top.StatusBar);
  242. Assert.Null (top.MenuBar);
  243. top.Remove (win);
  244. win = new Window () { Width = 60, Height = 15 };
  245. top.Add (win);
  246. // top is Application.Top without menu and status bar.
  247. top.EnsureVisibleBounds (win, 0, 0, out nx, out ny, out mb, out sb);
  248. Assert.Equal (0, nx);
  249. Assert.Equal (0, ny);
  250. Assert.Null (mb);
  251. Assert.Null (sb);
  252. top.AddMenuStatusBar (new MenuBar ());
  253. Assert.NotNull (top.MenuBar);
  254. // top is Application.Top with a menu and without status bar.
  255. top.EnsureVisibleBounds (win, 2, 2, out nx, out ny, out mb, out sb);
  256. Assert.Equal (2, nx);
  257. Assert.Equal (2, ny);
  258. Assert.NotNull (mb);
  259. Assert.Null (sb);
  260. top.AddMenuStatusBar (new StatusBar ());
  261. Assert.NotNull (top.StatusBar);
  262. // top is Application.Top with a menu and status bar.
  263. top.EnsureVisibleBounds (win, 30, 20, out nx, out ny, out mb, out sb);
  264. Assert.Equal (20, nx); // 20+60=80
  265. Assert.Equal (9, ny); // 9+15+1(mb)=25
  266. Assert.NotNull (mb);
  267. Assert.NotNull (sb);
  268. top.PositionToplevels ();
  269. Assert.Equal (new Rect (0, 1, 60, 15), win.Frame);
  270. Assert.Null (Toplevel.dragPosition);
  271. win.MouseEvent (new MouseEvent () { X = 6, Y = 0, Flags = MouseFlags.Button1Pressed });
  272. Assert.Equal (new Point (6, 0), Toplevel.dragPosition);
  273. win.MouseEvent (new MouseEvent () { X = 6, Y = 0, Flags = MouseFlags.Button1Released });
  274. Assert.Null (Toplevel.dragPosition);
  275. win.CanFocus = false;
  276. win.MouseEvent (new MouseEvent () { X = 6, Y = 0, Flags = MouseFlags.Button1Pressed });
  277. Assert.Null (Toplevel.dragPosition);
  278. }
  279. [Fact]
  280. [AutoInitShutdown]
  281. public void KeyBindings_Command ()
  282. {
  283. var isRunning = false;
  284. var win1 = new Window ("Win1") { Width = Dim.Percent (50f), Height = Dim.Fill () };
  285. var lblTf1W1 = new Label ("Enter text in TextField on Win1:");
  286. var tf1W1 = new TextField ("Text1 on Win1") { X = Pos.Right (lblTf1W1) + 1, Width = Dim.Fill () };
  287. var lblTvW1 = new Label ("Enter text in TextView on Win1:") { Y = Pos.Bottom (lblTf1W1) + 1 };
  288. var tvW1 = new TextView () { X = Pos.Left (tf1W1), Width = Dim.Fill (), Height = 2, Text = "First line Win1\nSecond line Win1" };
  289. var lblTf2W1 = new Label ("Enter text in TextField on Win1:") { Y = Pos.Bottom (lblTvW1) + 1 };
  290. var tf2W1 = new TextField ("Text2 on Win1") { X = Pos.Left (tf1W1), Width = Dim.Fill () };
  291. win1.Add (lblTf1W1, tf1W1, lblTvW1, tvW1, lblTf2W1, tf2W1);
  292. var win2 = new Window ("Win2") { X = Pos.Right (win1) + 1, Width = Dim.Percent (50f), Height = Dim.Fill () };
  293. var lblTf1W2 = new Label ("Enter text in TextField on Win2:");
  294. var tf1W2 = new TextField ("Text1 on Win2") { X = Pos.Right (lblTf1W2) + 1, Width = Dim.Fill () };
  295. var lblTvW2 = new Label ("Enter text in TextView on Win2:") { Y = Pos.Bottom (lblTf1W2) + 1 };
  296. var tvW2 = new TextView () { X = Pos.Left (tf1W2), Width = Dim.Fill (), Height = 2, Text = "First line Win1\nSecond line Win2" };
  297. var lblTf2W2 = new Label ("Enter text in TextField on Win2:") { Y = Pos.Bottom (lblTvW2) + 1 };
  298. var tf2W2 = new TextField ("Text2 on Win2") { X = Pos.Left (tf1W2), Width = Dim.Fill () };
  299. win2.Add (lblTf1W2, tf1W2, lblTvW2, tvW2, lblTf2W2, tf2W2);
  300. var top = Application.Top;
  301. top.Add (win1, win2);
  302. top.Loaded += () => isRunning = true;
  303. top.Closing += (_) => isRunning = false;
  304. Application.Begin (top);
  305. top.Running = true;
  306. Assert.Equal (new Rect (0, 0, 40, 25), win1.Frame);
  307. Assert.Equal (new Rect (41, 0, 40, 25), win2.Frame);
  308. Assert.Equal (win1, top.Focused);
  309. Assert.Equal (tf1W1, top.MostFocused);
  310. Assert.True (isRunning);
  311. Assert.True (top.Focused.ProcessKey (new KeyEvent (Application.QuitKey, new KeyModifiers ())));
  312. Assert.False (isRunning);
  313. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Z | Key.CtrlMask, new KeyModifiers ())));
  314. Assert.False (top.Focused.ProcessKey (new KeyEvent (Key.F5, new KeyModifiers ())));
  315. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  316. Assert.Equal (win1, top.Focused);
  317. Assert.Equal (tvW1, top.MostFocused);
  318. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  319. Assert.Equal ($"\tFirst line Win1{Environment.NewLine}Second line Win1", tvW1.Text);
  320. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ())));
  321. Assert.Equal ($"First line Win1{Environment.NewLine}Second line Win1", tvW1.Text);
  322. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers ())));
  323. Assert.Equal (win1, top.Focused);
  324. Assert.Equal (tf2W1, top.MostFocused);
  325. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  326. Assert.Equal (win1, top.Focused);
  327. Assert.Equal (tf1W1, top.MostFocused);
  328. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  329. Assert.Equal (win1, top.Focused);
  330. Assert.Equal (tf1W1, top.MostFocused);
  331. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  332. Assert.Equal (win1, top.Focused);
  333. Assert.Equal (tvW1, top.MostFocused);
  334. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.I | Key.CtrlMask, new KeyModifiers ())));
  335. Assert.Equal (win1, top.Focused);
  336. Assert.Equal (tf2W1, top.MostFocused);
  337. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ())));
  338. Assert.Equal (win1, top.Focused);
  339. Assert.Equal (tvW1, top.MostFocused);
  340. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ())));
  341. Assert.Equal (win1, top.Focused);
  342. Assert.Equal (tf1W1, top.MostFocused);
  343. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ())));
  344. Assert.Equal (win1, top.Focused);
  345. Assert.Equal (tf2W1, top.MostFocused);
  346. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers ())));
  347. Assert.Equal (win2, top.Focused);
  348. Assert.Equal (tf1W2, top.MostFocused);
  349. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask | Key.ShiftMask, new KeyModifiers ())));
  350. Assert.Equal (win1, top.Focused);
  351. Assert.Equal (tf2W1, top.MostFocused);
  352. Assert.True (top.Focused.ProcessKey (new KeyEvent (Application.AlternateForwardKey, new KeyModifiers ())));
  353. Assert.Equal (win2, top.Focused);
  354. Assert.Equal (tf1W2, top.MostFocused);
  355. Assert.True (top.Focused.ProcessKey (new KeyEvent (Application.AlternateBackwardKey, new KeyModifiers ())));
  356. Assert.Equal (win1, top.Focused);
  357. Assert.Equal (tf2W1, top.MostFocused);
  358. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ())));
  359. Assert.Equal (win1, top.Focused);
  360. Assert.Equal (tvW1, top.MostFocused);
  361. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.B | Key.CtrlMask, new KeyModifiers ())));
  362. Assert.Equal (win1, top.Focused);
  363. Assert.Equal (tf1W1, top.MostFocused);
  364. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  365. Assert.Equal (win1, top.Focused);
  366. Assert.Equal (tvW1, top.MostFocused);
  367. Assert.Equal (new Point (0, 0), tvW1.CursorPosition);
  368. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.End | Key.CtrlMask, new KeyModifiers ())));
  369. Assert.Equal (win1, top.Focused);
  370. Assert.Equal (tvW1, top.MostFocused);
  371. Assert.Equal (new Point (16, 1), tvW1.CursorPosition);
  372. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.F | Key.CtrlMask, new KeyModifiers ())));
  373. Assert.Equal (win1, top.Focused);
  374. Assert.Equal (tf2W1, top.MostFocused);
  375. Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.L | Key.CtrlMask, new KeyModifiers ())));
  376. }
  377. [Fact]
  378. [AutoInitShutdown]
  379. public void KeyBindings_Command_With_MdiTop ()
  380. {
  381. var top = Application.Top;
  382. Assert.Null (Application.MdiTop);
  383. top.IsMdiContainer = true;
  384. Application.Begin (top);
  385. Assert.Equal (Application.Top, Application.MdiTop);
  386. var isRunning = true;
  387. var win1 = new Window ("Win1") { Width = Dim.Percent (50f), Height = Dim.Fill () };
  388. var lblTf1W1 = new Label ("Enter text in TextField on Win1:");
  389. var tf1W1 = new TextField ("Text1 on Win1") { X = Pos.Right (lblTf1W1) + 1, Width = Dim.Fill () };
  390. var lblTvW1 = new Label ("Enter text in TextView on Win1:") { Y = Pos.Bottom (lblTf1W1) + 1 };
  391. var tvW1 = new TextView () { X = Pos.Left (tf1W1), Width = Dim.Fill (), Height = 2, Text = "First line Win1\nSecond line Win1" };
  392. var lblTf2W1 = new Label ("Enter text in TextField on Win1:") { Y = Pos.Bottom (lblTvW1) + 1 };
  393. var tf2W1 = new TextField ("Text2 on Win1") { X = Pos.Left (tf1W1), Width = Dim.Fill () };
  394. win1.Add (lblTf1W1, tf1W1, lblTvW1, tvW1, lblTf2W1, tf2W1);
  395. var win2 = new Window ("Win2") { Width = Dim.Percent (50f), Height = Dim.Fill () };
  396. var lblTf1W2 = new Label ("Enter text in TextField on Win2:");
  397. var tf1W2 = new TextField ("Text1 on Win2") { X = Pos.Right (lblTf1W2) + 1, Width = Dim.Fill () };
  398. var lblTvW2 = new Label ("Enter text in TextView on Win2:") { Y = Pos.Bottom (lblTf1W2) + 1 };
  399. var tvW2 = new TextView () { X = Pos.Left (tf1W2), Width = Dim.Fill (), Height = 2, Text = "First line Win1\nSecond line Win2" };
  400. var lblTf2W2 = new Label ("Enter text in TextField on Win2:") { Y = Pos.Bottom (lblTvW2) + 1 };
  401. var tf2W2 = new TextField ("Text2 on Win2") { X = Pos.Left (tf1W2), Width = Dim.Fill () };
  402. win2.Add (lblTf1W2, tf1W2, lblTvW2, tvW2, lblTf2W2, tf2W2);
  403. win1.Closing += (_) => isRunning = false;
  404. Assert.Null (top.Focused);
  405. Assert.Equal (top, Application.Current);
  406. Assert.True (top.IsCurrentTop);
  407. Assert.Equal (top, Application.MdiTop);
  408. Application.Begin (win1);
  409. Assert.Equal (new Rect (0, 0, 40, 25), win1.Frame);
  410. Assert.NotEqual (top, Application.Current);
  411. Assert.False (top.IsCurrentTop);
  412. Assert.Equal (win1, Application.Current);
  413. Assert.True (win1.IsCurrentTop);
  414. Assert.True (win1.IsMdiChild);
  415. Assert.Null (top.Focused);
  416. Assert.Null (top.MostFocused);
  417. Assert.Equal (win1.Subviews [0], win1.Focused);
  418. Assert.Equal (tf1W1, win1.MostFocused);
  419. Assert.True (win1.IsMdiChild);
  420. Assert.Single (Application.MdiChildes);
  421. Application.Begin (win2);
  422. Assert.Equal (new Rect (0, 0, 40, 25), win2.Frame);
  423. Assert.NotEqual (top, Application.Current);
  424. Assert.False (top.IsCurrentTop);
  425. Assert.Equal (win2, Application.Current);
  426. Assert.True (win2.IsCurrentTop);
  427. Assert.True (win2.IsMdiChild);
  428. Assert.Null (top.Focused);
  429. Assert.Null (top.MostFocused);
  430. Assert.Equal (win2.Subviews [0], win2.Focused);
  431. Assert.Equal (tf1W2, win2.MostFocused);
  432. Assert.Equal (2, Application.MdiChildes.Count);
  433. Application.ShowChild (win1);
  434. Assert.Equal (win1, Application.Current);
  435. Assert.Equal (win1, Application.MdiChildes [0]);
  436. win1.Running = true;
  437. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Application.QuitKey, new KeyModifiers ())));
  438. Assert.False (isRunning);
  439. Assert.False (win1.Running);
  440. Assert.Equal (win1, Application.MdiChildes [0]);
  441. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Z | Key.CtrlMask, new KeyModifiers ())));
  442. Assert.False (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.F5, new KeyModifiers ())));
  443. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  444. Assert.True (win1.IsCurrentTop);
  445. Assert.Equal (tvW1, win1.MostFocused);
  446. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  447. Assert.Equal ($"\tFirst line Win1{Environment.NewLine}Second line Win1", tvW1.Text);
  448. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ())));
  449. Assert.Equal ($"First line Win1{Environment.NewLine}Second line Win1", tvW1.Text);
  450. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers ())));
  451. Assert.Equal (win1, Application.MdiChildes [0]);
  452. Assert.Equal (tf2W1, win1.MostFocused);
  453. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  454. Assert.Equal (win1, Application.MdiChildes [0]);
  455. Assert.Equal (tf1W1, win1.MostFocused);
  456. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  457. Assert.Equal (win1, Application.MdiChildes [0]);
  458. Assert.Equal (tf1W1, win1.MostFocused);
  459. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  460. Assert.Equal (win1, Application.MdiChildes [0]);
  461. Assert.Equal (tvW1, win1.MostFocused);
  462. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.I | Key.CtrlMask, new KeyModifiers ())));
  463. Assert.Equal (win1, Application.MdiChildes [0]);
  464. Assert.Equal (tf2W1, win1.MostFocused);
  465. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ())));
  466. Assert.Equal (win1, Application.MdiChildes [0]);
  467. Assert.Equal (tvW1, win1.MostFocused);
  468. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ())));
  469. Assert.Equal (win1, Application.MdiChildes [0]);
  470. Assert.Equal (tf1W1, win1.MostFocused);
  471. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ())));
  472. Assert.Equal (win1, Application.MdiChildes [0]);
  473. Assert.Equal (tf2W1, win1.MostFocused);
  474. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  475. Assert.Equal (win1, Application.MdiChildes [0]);
  476. Assert.Equal (tf1W1, win1.MostFocused);
  477. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers ())));
  478. Assert.Equal (win2, Application.MdiChildes [0]);
  479. Assert.Equal (tf1W2, win2.MostFocused);
  480. tf2W2.SetFocus ();
  481. Assert.True (tf2W2.HasFocus);
  482. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask | Key.ShiftMask, new KeyModifiers ())));
  483. Assert.Equal (win1, Application.MdiChildes [0]);
  484. Assert.Equal (tf1W1, win1.MostFocused);
  485. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Application.AlternateForwardKey, new KeyModifiers ())));
  486. Assert.Equal (win2, Application.MdiChildes [0]);
  487. Assert.Equal (tf2W2, win2.MostFocused);
  488. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Application.AlternateBackwardKey, new KeyModifiers ())));
  489. Assert.Equal (win1, Application.MdiChildes [0]);
  490. Assert.Equal (tf1W1, win1.MostFocused);
  491. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  492. Assert.Equal (win1, Application.MdiChildes [0]);
  493. Assert.Equal (tvW1, win1.MostFocused);
  494. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.B | Key.CtrlMask, new KeyModifiers ())));
  495. Assert.Equal (win1, Application.MdiChildes [0]);
  496. Assert.Equal (tf1W1, win1.MostFocused);
  497. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  498. Assert.Equal (win1, Application.MdiChildes [0]);
  499. Assert.Equal (tvW1, win1.MostFocused);
  500. Assert.Equal (new Point (0, 0), tvW1.CursorPosition);
  501. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.End | Key.CtrlMask, new KeyModifiers ())));
  502. Assert.Equal (win1, Application.MdiChildes [0]);
  503. Assert.Equal (tvW1, win1.MostFocused);
  504. Assert.Equal (new Point (16, 1), tvW1.CursorPosition);
  505. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.F | Key.CtrlMask, new KeyModifiers ())));
  506. Assert.Equal (win1, Application.MdiChildes [0]);
  507. Assert.Equal (tf2W1, win1.MostFocused);
  508. Assert.True (Application.MdiChildes [0].ProcessKey (new KeyEvent (Key.L | Key.CtrlMask, new KeyModifiers ())));
  509. }
  510. [Fact]
  511. public void Added_Event_Should_Not_Be_Used_To_Initialize_Toplevel_Events ()
  512. {
  513. Key alternateForwardKey = default;
  514. Key alternateBackwardKey = default;
  515. Key quitKey = default;
  516. var wasAdded = false;
  517. var view = new View ();
  518. view.Added += View_Added;
  519. void View_Added (View obj)
  520. {
  521. Assert.Throws<NullReferenceException> (() => Application.Top.AlternateForwardKeyChanged += (e) => alternateForwardKey = e);
  522. Assert.Throws<NullReferenceException> (() => Application.Top.AlternateBackwardKeyChanged += (e) => alternateBackwardKey = e);
  523. Assert.Throws<NullReferenceException> (() => Application.Top.QuitKeyChanged += (e) => quitKey = e);
  524. Assert.False (wasAdded);
  525. wasAdded = true;
  526. view.Added -= View_Added;
  527. }
  528. var win = new Window ();
  529. win.Add (view);
  530. Application.Init (new FakeDriver ());
  531. var top = Application.Top;
  532. top.Add (win);
  533. Assert.True (wasAdded);
  534. Application.Shutdown ();
  535. }
  536. [Fact]
  537. [AutoInitShutdown]
  538. public void AlternateForwardKeyChanged_AlternateBackwardKeyChanged_QuitKeyChanged_Events ()
  539. {
  540. Key alternateForwardKey = default;
  541. Key alternateBackwardKey = default;
  542. Key quitKey = default;
  543. var view = new View ();
  544. view.Initialized += View_Initialized;
  545. void View_Initialized (object sender, EventArgs e)
  546. {
  547. Application.Top.AlternateForwardKeyChanged += (e) => alternateForwardKey = e;
  548. Application.Top.AlternateBackwardKeyChanged += (e) => alternateBackwardKey = e;
  549. Application.Top.QuitKeyChanged += (e) => quitKey = e;
  550. }
  551. var win = new Window ();
  552. win.Add (view);
  553. var top = Application.Top;
  554. top.Add (win);
  555. Application.Begin (top);
  556. Assert.Equal (Key.Null, alternateForwardKey);
  557. Assert.Equal (Key.Null, alternateBackwardKey);
  558. Assert.Equal (Key.Null, quitKey);
  559. Assert.Equal (Key.PageDown | Key.CtrlMask, Application.AlternateForwardKey);
  560. Assert.Equal (Key.PageUp | Key.CtrlMask, Application.AlternateBackwardKey);
  561. Assert.Equal (Key.Q | Key.CtrlMask, Application.QuitKey);
  562. Application.AlternateForwardKey = Key.A;
  563. Application.AlternateBackwardKey = Key.B;
  564. Application.QuitKey = Key.C;
  565. Assert.Equal (Key.PageDown | Key.CtrlMask, alternateForwardKey);
  566. Assert.Equal (Key.PageUp | Key.CtrlMask, alternateBackwardKey);
  567. Assert.Equal (Key.Q | Key.CtrlMask, quitKey);
  568. Assert.Equal (Key.A, Application.AlternateForwardKey);
  569. Assert.Equal (Key.B, Application.AlternateBackwardKey);
  570. Assert.Equal (Key.C, Application.QuitKey);
  571. // Replacing the defaults keys to avoid errors on others unit tests that are using it.
  572. Application.AlternateForwardKey = Key.PageDown | Key.CtrlMask;
  573. Application.AlternateBackwardKey = Key.PageUp | Key.CtrlMask;
  574. Application.QuitKey = Key.Q | Key.CtrlMask;
  575. Assert.Equal (Key.PageDown | Key.CtrlMask, Application.AlternateForwardKey);
  576. Assert.Equal (Key.PageUp | Key.CtrlMask, Application.AlternateBackwardKey);
  577. Assert.Equal (Key.Q | Key.CtrlMask, Application.QuitKey);
  578. }
  579. [Fact]
  580. [AutoInitShutdown]
  581. public void FileDialog_FileSystemWatcher ()
  582. {
  583. for (int i = 0; i < 8; i++) {
  584. var fd = new FileDialog ();
  585. fd.Ready += () => Application.RequestStop ();
  586. Application.Run (fd);
  587. }
  588. }
  589. [Fact, AutoInitShutdown]
  590. public void Mouse_Drag_On_Top_With_Superview_Null ()
  591. {
  592. var menu = new MenuBar (new MenuBarItem [] {
  593. new MenuBarItem("File", new MenuItem [] {
  594. new MenuItem("New", "", null)
  595. })
  596. });
  597. var sbar = new StatusBar (new StatusItem [] {
  598. new StatusItem(Key.N, "~CTRL-N~ New", null)
  599. });
  600. var win = new Window ("Window");
  601. var top = Application.Top;
  602. top.Add (menu, sbar, win);
  603. var iterations = -1;
  604. Application.Iteration = () => {
  605. iterations++;
  606. if (iterations == 0) {
  607. ((FakeDriver)Application.Driver).SetBufferSize (40, 15);
  608. MessageBox.Query ("About", "Hello Word", "Ok");
  609. } else if (iterations == 1) TestHelpers.AssertDriverContentsWithFrameAre (@"
  610. File
  611. ┌ Window ──────────────────────────────┐
  612. │ │
  613. │ │
  614. │ │
  615. │ ┌ About ───────────────┐ │
  616. │ │ Hello Word │ │
  617. │ │ │ │
  618. │ │ [◦ Ok ◦] │ │
  619. │ └──────────────────────┘ │
  620. │ │
  621. │ │
  622. │ │
  623. └──────────────────────────────────────┘
  624. CTRL-N New ", output);
  625. else if (iterations == 2) {
  626. Assert.Null (Application.MouseGrabView);
  627. // Grab the mouse
  628. ReflectionTools.InvokePrivate (
  629. typeof (Application),
  630. "ProcessMouseEvent",
  631. new MouseEvent () {
  632. X = 8,
  633. Y = 5,
  634. Flags = MouseFlags.Button1Pressed
  635. });
  636. Assert.Equal (Application.Current, Application.MouseGrabView);
  637. Assert.Equal (new Rect (8, 5, 24, 5), Application.MouseGrabView.Frame);
  638. } else if (iterations == 3) {
  639. Assert.Equal (Application.Current, Application.MouseGrabView);
  640. // Grab to left
  641. ReflectionTools.InvokePrivate (
  642. typeof (Application),
  643. "ProcessMouseEvent",
  644. new MouseEvent () {
  645. X = 7,
  646. Y = 5,
  647. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  648. });
  649. Assert.Equal (Application.Current, Application.MouseGrabView);
  650. Assert.Equal (new Rect (7, 5, 24, 5), Application.MouseGrabView.Frame);
  651. } else if (iterations == 4) {
  652. Assert.Equal (Application.Current, Application.MouseGrabView);
  653. TestHelpers.AssertDriverContentsWithFrameAre (@"
  654. File
  655. ┌ Window ──────────────────────────────┐
  656. │ │
  657. │ │
  658. │ │
  659. │ ┌ About ───────────────┐ │
  660. │ │ Hello Word │ │
  661. │ │ │ │
  662. │ │ [◦ Ok ◦] │ │
  663. │ └──────────────────────┘ │
  664. │ │
  665. │ │
  666. │ │
  667. └──────────────────────────────────────┘
  668. CTRL-N New ", output);
  669. Assert.Equal (Application.Current, Application.MouseGrabView);
  670. } else if (iterations == 5) {
  671. Assert.Equal (Application.Current, Application.MouseGrabView);
  672. // Grab to top
  673. ReflectionTools.InvokePrivate (
  674. typeof (Application),
  675. "ProcessMouseEvent",
  676. new MouseEvent () {
  677. X = 7,
  678. Y = 4,
  679. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  680. });
  681. Assert.Equal (Application.Current, Application.MouseGrabView);
  682. Assert.Equal (new Rect (7, 4, 24, 5), Application.MouseGrabView.Frame);
  683. } else if (iterations == 6) {
  684. Assert.Equal (Application.Current, Application.MouseGrabView);
  685. TestHelpers.AssertDriverContentsWithFrameAre (@"
  686. File
  687. ┌ Window ──────────────────────────────┐
  688. │ │
  689. │ │
  690. │ ┌ About ───────────────┐ │
  691. │ │ Hello Word │ │
  692. │ │ │ │
  693. │ │ [◦ Ok ◦] │ │
  694. │ └──────────────────────┘ │
  695. │ │
  696. │ │
  697. │ │
  698. │ │
  699. └──────────────────────────────────────┘
  700. CTRL-N New ", output);
  701. Assert.Equal (Application.Current, Application.MouseGrabView);
  702. Assert.Equal (new Rect (7, 4, 24, 5), Application.MouseGrabView.Frame);
  703. } else if (iterations == 7) {
  704. Assert.Equal (Application.Current, Application.MouseGrabView);
  705. // Ungrab the mouse
  706. ReflectionTools.InvokePrivate (
  707. typeof (Application),
  708. "ProcessMouseEvent",
  709. new MouseEvent () {
  710. X = 7,
  711. Y = 4,
  712. Flags = MouseFlags.Button1Released
  713. });
  714. Assert.Null (Application.MouseGrabView);
  715. } else if (iterations == 8) Application.RequestStop ();
  716. else if (iterations == 9) Application.RequestStop ();
  717. };
  718. Application.Run ();
  719. }
  720. [Fact, AutoInitShutdown]
  721. public void Mouse_Drag_On_Top_With_Superview_Not_Null ()
  722. {
  723. var menu = new MenuBar (new MenuBarItem [] {
  724. new MenuBarItem("File", new MenuItem [] {
  725. new MenuItem("New", "", null)
  726. })
  727. });
  728. var sbar = new StatusBar (new StatusItem [] {
  729. new StatusItem(Key.N, "~CTRL-N~ New", null)
  730. });
  731. var win = new Window ("Window") {
  732. X = 3,
  733. Y = 2,
  734. Width = Dim.Fill (10),
  735. Height = Dim.Fill (5)
  736. };
  737. var top = Application.Top;
  738. top.Add (menu, sbar, win);
  739. var iterations = -1;
  740. Application.Iteration = () => {
  741. iterations++;
  742. if (iterations == 0) {
  743. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  744. Assert.Null (Application.MouseGrabView);
  745. // Grab the mouse
  746. ReflectionTools.InvokePrivate (
  747. typeof (Application),
  748. "ProcessMouseEvent",
  749. new MouseEvent () {
  750. X = 4,
  751. Y = 2,
  752. Flags = MouseFlags.Button1Pressed
  753. });
  754. Assert.Equal (win, Application.MouseGrabView);
  755. Assert.Equal (new Rect (3, 2, 7, 3), Application.MouseGrabView.Frame);
  756. TestHelpers.AssertDriverContentsWithFrameAre (@"
  757. File
  758. ┌─────┐
  759. │ │
  760. └─────┘
  761. CTRL-N New", output);
  762. } else if (iterations == 1) {
  763. Assert.Equal (win, Application.MouseGrabView);
  764. // Grab to left
  765. ReflectionTools.InvokePrivate (
  766. typeof (Application),
  767. "ProcessMouseEvent",
  768. new MouseEvent () {
  769. X = 5,
  770. Y = 2,
  771. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  772. });
  773. Assert.Equal (win, Application.MouseGrabView);
  774. } else if (iterations == 2) {
  775. Assert.Equal (win, Application.MouseGrabView);
  776. TestHelpers.AssertDriverContentsWithFrameAre (@"
  777. File
  778. ┌────┐
  779. │ │
  780. └────┘
  781. CTRL-N New", output);
  782. Assert.Equal (win, Application.MouseGrabView);
  783. Assert.Equal (new Rect (4, 2, 6, 3), Application.MouseGrabView.Frame);
  784. } else if (iterations == 3) {
  785. Assert.Equal (win, Application.MouseGrabView);
  786. // Grab to top
  787. ReflectionTools.InvokePrivate (
  788. typeof (Application),
  789. "ProcessMouseEvent",
  790. new MouseEvent () {
  791. X = 5,
  792. Y = 1,
  793. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  794. });
  795. Assert.Equal (win, Application.MouseGrabView);
  796. } else if (iterations == 4) {
  797. Assert.Equal (win, Application.MouseGrabView);
  798. TestHelpers.AssertDriverContentsWithFrameAre (@"
  799. File
  800. ┌────┐
  801. │ │
  802. │ │
  803. └────┘
  804. CTRL-N New", output);
  805. Assert.Equal (win, Application.MouseGrabView);
  806. Assert.Equal (new Rect (4, 1, 6, 4), Application.MouseGrabView.Frame);
  807. } else if (iterations == 5) {
  808. Assert.Equal (win, Application.MouseGrabView);
  809. // Ungrab the mouse
  810. ReflectionTools.InvokePrivate (
  811. typeof (Application),
  812. "ProcessMouseEvent",
  813. new MouseEvent () {
  814. X = 7,
  815. Y = 4,
  816. Flags = MouseFlags.Button1Released
  817. });
  818. Assert.Null (Application.MouseGrabView);
  819. } else if (iterations == 8) Application.RequestStop ();
  820. };
  821. Application.Run ();
  822. }
  823. [Fact, AutoInitShutdown]
  824. public void EnsureVisibleBounds_With_Border_Null_Not_Throws ()
  825. {
  826. var top = new Toplevel ();
  827. Application.Begin (top);
  828. var exception = Record.Exception (() => ((FakeDriver)Application.Driver).SetBufferSize (0, 10));
  829. Assert.Null (exception);
  830. exception = Record.Exception (() => ((FakeDriver)Application.Driver).SetBufferSize (10, 0));
  831. Assert.Null (exception);
  832. }
  833. [Fact, AutoInitShutdown]
  834. public void OnEnter_OnLeave_Triggered_On_Application_Begin_End ()
  835. {
  836. var isEnter = false;
  837. var isLeave = false;
  838. var v = new View ();
  839. v.Enter += (_) => isEnter = true;
  840. v.Leave += (_) => isLeave = true;
  841. var top = Application.Top;
  842. top.Add (v);
  843. Assert.False (v.CanFocus);
  844. var exception = Record.Exception (() => top.OnEnter (top));
  845. Assert.Null (exception);
  846. exception = Record.Exception (() => top.OnLeave (top));
  847. Assert.Null (exception);
  848. v.CanFocus = true;
  849. Application.Begin (top);
  850. Assert.True (isEnter);
  851. Assert.False (isLeave);
  852. isEnter = false;
  853. var d = new Dialog ();
  854. var rs = Application.Begin (d);
  855. Assert.False (isEnter);
  856. Assert.True (isLeave);
  857. isLeave = false;
  858. Application.End (rs);
  859. Assert.True (isEnter);
  860. Assert.False (isLeave);
  861. }
  862. [Fact, AutoInitShutdown]
  863. public void PositionCursor_SetCursorVisibility_To_Invisible_If_Focused_Is_Null ()
  864. {
  865. var tf = new TextField ("test") { Width = 5 };
  866. var view = new View () { Width = 10, Height = 10 };
  867. view.Add (tf);
  868. Application.Top.Add (view);
  869. Application.Begin (Application.Top);
  870. Assert.True (tf.HasFocus);
  871. Application.Driver.GetCursorVisibility (out CursorVisibility cursor);
  872. Assert.Equal (CursorVisibility.Default, cursor);
  873. view.Enabled = false;
  874. Assert.False (tf.HasFocus);
  875. Application.Refresh ();
  876. Application.Driver.GetCursorVisibility (out cursor);
  877. Assert.Equal (CursorVisibility.Invisible, cursor);
  878. }
  879. [Fact, AutoInitShutdown]
  880. public void IsLoaded_Application_Begin ()
  881. {
  882. var top = Application.Top;
  883. Assert.False (top.IsLoaded);
  884. Application.Begin (top);
  885. Assert.True (top.IsLoaded);
  886. }
  887. [Fact, AutoInitShutdown]
  888. public void IsLoaded_With_Sub_Toplevel_Application_Begin_NeedDisplay ()
  889. {
  890. var top = Application.Top;
  891. var subTop = new Toplevel ();
  892. var view = new View (new Rect (0, 0, 20, 10));
  893. subTop.Add (view);
  894. top.Add (subTop);
  895. Assert.False (top.IsLoaded);
  896. Assert.False (subTop.IsLoaded);
  897. Assert.Equal (new Rect (0, 0, 20, 10), view.Frame);
  898. Assert.Equal (new Rect (0, 0, 20, 10), view._needsDisplay);
  899. view.LayoutStarted += view_LayoutStarted;
  900. void view_LayoutStarted (View.LayoutEventArgs e)
  901. {
  902. Assert.Equal (new Rect (0, 0, 20, 10), view._needsDisplay);
  903. view.LayoutStarted -= view_LayoutStarted;
  904. }
  905. Application.Begin (top);
  906. Assert.True (top.IsLoaded);
  907. Assert.True (subTop.IsLoaded);
  908. Assert.Equal (new Rect (0, 0, 20, 10), view.Frame);
  909. view.Frame = new Rect (1, 3, 10, 5);
  910. Assert.Equal (new Rect (1, 3, 10, 5), view.Frame);
  911. Assert.Equal (new Rect (0, 0, 10, 5), view._needsDisplay);
  912. view.Redraw (view.Bounds);
  913. view.Frame = new Rect (1, 3, 10, 5);
  914. Assert.Equal (new Rect (1, 3, 10, 5), view.Frame);
  915. Assert.Equal (new Rect (0, 0, 10, 5), view._needsDisplay);
  916. }
  917. }
  918. }